summaryrefslogtreecommitdiff
path: root/gtk/gtktooltipwindow.c
blob: 4c9d51715a689610e2d45ed9ffc5be76c7eb4e8b (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* GTK - The GIMP Toolkit
 * Copyright 2015  Emmanuele Bassi 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#include "config.h"

#include "gtktooltipwindowprivate.h"

#include "gtkprivate.h"
#include "gtkintl.h"

#include "gtkaccessible.h"
#include "gtkbox.h"
#include "gtkimage.h"
#include "gtklabel.h"
#include "gtkmain.h"
#include "gtksettings.h"
#include "gtksizerequest.h"
#include "gtkwindowprivate.h"
#include "gtkwidgetprivate.h"

#define MAX_TOOLTIP_LINE_WIDTH  70

struct _GtkTooltipWindow
{
  GtkWindow parent_type;

  GtkWidget *box;
  GtkWidget *image;
  GtkWidget *label;
  GtkWidget *custom_widget;
};

struct _GtkTooltipWindowClass
{
  GtkWindowClass parent_class;
};

G_DEFINE_TYPE (GtkTooltipWindow, gtk_tooltip_window, GTK_TYPE_WINDOW)

static void
gtk_tooltip_window_class_init (GtkTooltipWindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_css_name (widget_class, I_("tooltip"));
  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_TOOL_TIP);
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtktooltipwindow.ui");

  gtk_widget_class_bind_template_child (widget_class, GtkTooltipWindow, box);
  gtk_widget_class_bind_template_child (widget_class, GtkTooltipWindow, image);
  gtk_widget_class_bind_template_child (widget_class, GtkTooltipWindow, label);
}

static void
gtk_tooltip_window_init (GtkTooltipWindow *self)
{
  GtkWindow *window = GTK_WINDOW (self);

  gtk_widget_init_template (GTK_WIDGET (self));

  gtk_window_set_use_subsurface (window, TRUE);
  _gtk_window_request_csd (window);
}

GtkWidget *
gtk_tooltip_window_new (void)
{
  return g_object_new (GTK_TYPE_TOOLTIP_WINDOW,
                       "type", GTK_WINDOW_POPUP,
                       NULL);
}

void
gtk_tooltip_window_set_label_markup (GtkTooltipWindow *window,
                                     const char       *markup)
{
  if (markup != NULL)
    {
      gtk_label_set_markup (GTK_LABEL (window->label), markup);
      gtk_widget_show (window->label);
    }
  else
    {
      gtk_widget_hide (window->label);
    }
}

void
gtk_tooltip_window_set_label_text (GtkTooltipWindow *window,
                                   const char       *text)
{
  if (text != NULL)
    {
      gtk_label_set_text (GTK_LABEL (window->label), text);
      gtk_widget_show (window->label);
    }
  else
    {
      gtk_widget_hide (window->label);
    }
}

void
gtk_tooltip_window_set_image_icon (GtkTooltipWindow *window,
                                   GdkPixbuf        *pixbuf)
{

  if (pixbuf != NULL)
    {
      gtk_image_set_from_pixbuf (GTK_IMAGE (window->image), pixbuf);
      gtk_widget_show (window->image);
    }
  else
    {
      gtk_widget_hide (window->image);
    }
}

void
gtk_tooltip_window_set_image_icon_from_stock (GtkTooltipWindow *window,
                                              const char       *stock_id,
                                              GtkIconSize       icon_size)
{
  if (stock_id != NULL)
    {
 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_image_set_from_stock (GTK_IMAGE (window->image), stock_id, icon_size);
 G_GNUC_END_IGNORE_DEPRECATIONS;

      gtk_widget_show (window->image);
    }
  else
    {
      gtk_widget_hide (window->image);
    }
}

void
gtk_tooltip_window_set_image_icon_from_name (GtkTooltipWindow *window,
                                             const char       *icon_name,
                                             GtkIconSize       icon_size)
{
  if (icon_name)
    {
      gtk_image_set_from_icon_name (GTK_IMAGE (window->image), icon_name, icon_size);
      gtk_widget_show (window->image);
    }
  else
    {
      gtk_widget_hide (window->image);
    }
}

void
gtk_tooltip_window_set_image_icon_from_gicon (GtkTooltipWindow *window,
                                              GIcon            *gicon,
                                              GtkIconSize       icon_size)
{
  if (gicon != NULL)
    {
      gtk_image_set_from_gicon (GTK_IMAGE (window->image), gicon, icon_size);
      gtk_widget_show (window->image);
    }
  else
    {
      gtk_widget_hide (window->image);
    }
}

void
gtk_tooltip_window_set_custom_widget (GtkTooltipWindow *window,
                                      GtkWidget        *custom_widget)
{
  /* No need to do anything if the custom widget stays the same */
  if (window->custom_widget == custom_widget)
    return;

  if (window->custom_widget != NULL)
    {
      GtkWidget *custom = window->custom_widget;

      /* Note: We must reset window->custom_widget first,
       * since gtk_container_remove() will recurse into
       * gtk_tooltip_set_custom()
       */
      window->custom_widget = NULL;
      gtk_container_remove (GTK_CONTAINER (window->box), custom);
      g_object_unref (custom);
    }

  if (custom_widget != NULL)
    {
      window->custom_widget = g_object_ref (custom_widget);

      gtk_container_add (GTK_CONTAINER (window->box), custom_widget);
      gtk_widget_show (custom_widget);
    }
}