summaryrefslogtreecommitdiffstats
path: root/views/widget/tooltip_window_gtk.cc
blob: 444e67ec006c7e27575136a3708e7aed5455099d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/widget/tooltip_window_gtk.h"

#include <gtk/gtk.h>

#include "base/utf_string_conversions.h"

namespace views {

TooltipWindowGtk::TooltipWindowGtk(GtkWidget* widget)
    : host_(widget),
      window_(NULL),
      alignment_(NULL),
      label_(NULL) {
  Init();
}

TooltipWindowGtk::~TooltipWindowGtk() {
  if (window_)
    gtk_widget_destroy(window_);
}

void TooltipWindowGtk::SetTooltipText(const std::wstring& text) {
  const std::string& utf8 = WideToUTF8(text);

  gtk_label_set_text(label(), utf8.c_str());
}

GtkLabel* TooltipWindowGtk::label() {
  return GTK_LABEL(label_);
}

void  TooltipWindowGtk::Init() {
  // Creates and setup tooltip window.
  window_ = gtk_window_new(GTK_WINDOW_POPUP);
  gtk_window_set_type_hint(GTK_WINDOW(window_), GDK_WINDOW_TYPE_HINT_TOOLTIP);
  gtk_widget_set_app_paintable(window_, TRUE);
  gtk_window_set_resizable(GTK_WINDOW(window_), FALSE);
  gtk_widget_set_name(window_, "gtk-tooltip");

  GdkColormap* rgba_colormap =
      gdk_screen_get_rgba_colormap(gdk_screen_get_default());
  if (rgba_colormap)
    gtk_widget_set_colormap(window_, rgba_colormap);

  g_signal_connect(G_OBJECT(window_), "expose-event",
                   G_CALLBACK(&OnPaintThunk), this);
  g_signal_connect(G_OBJECT(window_), "style-set",
                   G_CALLBACK(&OnStyleSetThunk), this);

  alignment_ = gtk_alignment_new(0.5, 0.5, 1.0, 1.0);
  gtk_container_add(GTK_CONTAINER(window_), alignment_);
  gtk_widget_show(alignment_);

  label_ = gtk_label_new("");
  gtk_label_set_line_wrap(GTK_LABEL(label_), TRUE);
  gtk_container_add(GTK_CONTAINER(alignment_), label_);
  gtk_widget_show(label_);

  // Associates the tooltip window with given widget
  gtk_widget_set_tooltip_window(host_, GTK_WINDOW(window_));
}

// Paints our customized tooltip window.
gboolean TooltipWindowGtk::OnPaint(GtkWidget* widget, GdkEventExpose* event) {
  gtk_paint_flat_box(widget->style,
      widget->window,
      GTK_STATE_NORMAL,
      GTK_SHADOW_OUT,
      NULL,
      widget,
      "tooltip",
      0, 0,
      widget->allocation.width,
      widget->allocation.height);

  return FALSE;
}

// Style change handler.
void TooltipWindowGtk::OnStyleSet(GtkWidget* widget,
                                  GtkStyle* previous_style) {
  gtk_alignment_set_padding(GTK_ALIGNMENT(alignment_),
      widget->style->ythickness,
      widget->style->ythickness,
      widget->style->xthickness,
      widget->style->xthickness);

  gtk_widget_queue_draw(widget);
}

}  // namespace views