Add GtkTooltipWindow private sub-class

GtkTooltip does a lot of set up on the GtkWindow it uses internally. We
should move that code to a separate class to keep it contained.
This commit is contained in:
Emmanuele Bassi 2015-11-08 15:46:23 +00:00
parent c2d0aa7851
commit a846fd586d
4 changed files with 312 additions and 128 deletions

View File

@ -542,6 +542,7 @@ gtk_private_h_sources = \
gtktoolbarprivate.h \
gtktoolpaletteprivate.h \
gtktooltipprivate.h \
gtktooltipwindowprivate.h \
gtktreedatalist.h \
gtktreeprivate.h \
gtkwidgetprivate.h \
@ -871,6 +872,7 @@ gtk_base_c_sources = \
gtktoolpalette.c \
gtktoolshell.c \
gtktooltip.c \
gtktooltipwindow.c \
gtktrashmonitor.c \
gtktreedatalist.c \
gtktreednd.c \

View File

@ -34,10 +34,10 @@
#include "gtksettings.h"
#include "gtksizerequest.h"
#include "gtkstylecontext.h"
#include "gtktooltipwindowprivate.h"
#include "gtkwindowprivate.h"
#include "gtkaccessible.h"
#ifdef GDK_WINDOWING_WAYLAND
#include "wayland/gdkwayland.h"
#endif
@ -169,13 +169,6 @@ gtk_tooltip_class_init (GtkTooltipClass *klass)
static void
gtk_tooltip_init (GtkTooltip *tooltip)
{
GtkStyleContext *context;
GtkWidget *window;
GtkWidget *box;
GtkWidget *image;
GtkWidget *label;
AtkObject *atk_obj;
tooltip->timeout_id = 0;
tooltip->browse_mode_timeout_id = 0;
@ -189,43 +182,10 @@ gtk_tooltip_init (GtkTooltip *tooltip)
tooltip->last_window = NULL;
window = gtk_window_new (GTK_WINDOW_POPUP);
gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
gtk_window_set_use_subsurface (GTK_WINDOW (window), TRUE);
g_signal_connect (window, "hide",
G_CALLBACK (gtk_tooltip_window_hide), tooltip);
_gtk_window_request_csd (GTK_WINDOW (window));
context = gtk_widget_get_style_context (window);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
atk_obj = gtk_widget_get_accessible (window);
if (GTK_IS_ACCESSIBLE (atk_obj))
atk_object_set_role (atk_obj, ATK_ROLE_TOOL_TIP);
/* FIXME: don't hardcode the padding */
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
gtk_widget_set_margin_start (box, 6);
gtk_widget_set_margin_end (box, 6);
gtk_widget_set_margin_top (box, 6);
gtk_widget_set_margin_bottom (box, 6);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show (box);
image = gtk_image_new ();
gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
label = gtk_label_new ("");
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_label_set_max_width_chars (GTK_LABEL (label), 70);
gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
tooltip->window = window;
tooltip->box = box;
tooltip->image = image;
tooltip->label = label;
tooltip->custom_widget = NULL;
tooltip->window = gtk_tooltip_window_new ();
g_signal_connect (tooltip->window, "hide",
G_CALLBACK (gtk_tooltip_window_hide),
tooltip);
}
static void
@ -282,15 +242,7 @@ gtk_tooltip_set_markup (GtkTooltip *tooltip,
{
g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
if (markup)
{
gtk_label_set_markup (GTK_LABEL (tooltip->label), markup);
gtk_widget_show (tooltip->label);
}
else
{
gtk_widget_hide (tooltip->label);
}
gtk_tooltip_window_set_label_markup (GTK_TOOLTIP_WINDOW (tooltip->window), markup);
}
/**
@ -309,15 +261,7 @@ gtk_tooltip_set_text (GtkTooltip *tooltip,
{
g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
if (text)
{
gtk_label_set_text (GTK_LABEL (tooltip->label), text);
gtk_widget_show (tooltip->label);
}
else
{
gtk_widget_hide (tooltip->label);
}
gtk_tooltip_window_set_label_text (GTK_TOOLTIP_WINDOW (tooltip->window), text);
}
/**
@ -335,18 +279,9 @@ gtk_tooltip_set_icon (GtkTooltip *tooltip,
GdkPixbuf *pixbuf)
{
g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
if (pixbuf)
g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
if (pixbuf)
{
gtk_image_set_from_pixbuf (GTK_IMAGE (tooltip->image), pixbuf);
gtk_widget_show (tooltip->image);
}
else
{
gtk_widget_hide (tooltip->image);
}
gtk_tooltip_window_set_image_icon (GTK_TOOLTIP_WINDOW (tooltip->window), pixbuf);
}
/**
@ -370,17 +305,9 @@ gtk_tooltip_set_icon_from_stock (GtkTooltip *tooltip,
{
g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
if (stock_id)
{
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
gtk_image_set_from_stock (GTK_IMAGE (tooltip->image), stock_id, size);
G_GNUC_END_IGNORE_DEPRECATIONS;
gtk_widget_show (tooltip->image);
}
else
{
gtk_widget_hide (tooltip->image);
}
gtk_tooltip_window_set_image_icon_from_stock (GTK_TOOLTIP_WINDOW (tooltip->window),
stock_id,
size);
}
/**
@ -402,15 +329,9 @@ gtk_tooltip_set_icon_from_icon_name (GtkTooltip *tooltip,
{
g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
if (icon_name)
{
gtk_image_set_from_icon_name (GTK_IMAGE (tooltip->image), icon_name, size);
gtk_widget_show (tooltip->image);
}
else
{
gtk_widget_hide (tooltip->image);
}
gtk_tooltip_window_set_image_icon_from_name (GTK_TOOLTIP_WINDOW (tooltip->window),
icon_name,
size);
}
/**
@ -432,15 +353,9 @@ gtk_tooltip_set_icon_from_gicon (GtkTooltip *tooltip,
{
g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
if (gicon)
{
gtk_image_set_from_gicon (GTK_IMAGE (tooltip->image), gicon, size);
gtk_widget_show (tooltip->image);
}
else
{
gtk_widget_hide (tooltip->image);
}
gtk_tooltip_window_set_image_icon_from_gicon (GTK_TOOLTIP_WINDOW (tooltip->window),
gicon,
size);
}
/**
@ -463,37 +378,14 @@ gtk_tooltip_set_custom (GtkTooltip *tooltip,
GtkWidget *custom_widget)
{
g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
if (custom_widget)
g_return_if_fail (GTK_IS_WIDGET (custom_widget));
g_return_if_fail (custom_widget == NULL || GTK_IS_WIDGET (custom_widget));
/* The custom widget has been updated from the query-tooltip
* callback, so we do not want to reset the custom widget later on.
*/
tooltip->custom_was_reset = TRUE;
/* No need to do anything if the custom widget stays the same */
if (tooltip->custom_widget == custom_widget)
return;
if (tooltip->custom_widget)
{
GtkWidget *custom = tooltip->custom_widget;
/* Note: We must reset tooltip->custom_widget first,
* since gtk_container_remove() will recurse into
* gtk_tooltip_set_custom()
*/
tooltip->custom_widget = NULL;
gtk_container_remove (GTK_CONTAINER (tooltip->box), custom);
g_object_unref (custom);
}
if (custom_widget)
{
tooltip->custom_widget = g_object_ref (custom_widget);
gtk_container_add (GTK_CONTAINER (tooltip->box), custom_widget);
gtk_widget_show (custom_widget);
}
gtk_tooltip_window_set_custom_widget (GTK_TOOLTIP_WINDOW (tooltip->window), custom_widget);
}
/**

230
gtk/gtktooltipwindow.c Normal file
View File

@ -0,0 +1,230 @@
/* 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 "gtkaccessible.h"
#include "gtkbox.h"
#include "gtkimage.h"
#include "gtklabel.h"
#include "gtkmain.h"
#include "gtksettings.h"
#include "gtksizerequest.h"
#include "gtkstylecontext.h"
#include "gtkwindowprivate.h"
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_accessible_role (widget_class, ATK_ROLE_TOOL_TIP);
}
static void
gtk_tooltip_window_init (GtkTooltipWindow *self)
{
GtkWindow *window = GTK_WINDOW (self);
GtkStyleContext *context;
gtk_window_set_type_hint (window, GDK_WINDOW_TYPE_HINT_TOOLTIP);
gtk_window_set_resizable (window, FALSE);
gtk_window_set_use_subsurface (window, TRUE);
_gtk_window_request_csd (window);
context = gtk_widget_get_style_context (GTK_WIDGET (self));
gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
/* FIXME: don't hardcode the padding */
self->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
gtk_widget_set_margin_start (self->box, 6);
gtk_widget_set_margin_end (self->box, 6);
gtk_widget_set_margin_top (self->box, 6);
gtk_widget_set_margin_bottom (self->box, 6);
gtk_container_add (GTK_CONTAINER (self), self->box);
gtk_widget_show (self->box);
self->image = gtk_image_new ();
gtk_box_pack_start (GTK_BOX (self->box), self->image, FALSE, FALSE, 0);
self->label = gtk_label_new ("");
gtk_label_set_line_wrap (GTK_LABEL (self->label), TRUE);
gtk_label_set_max_width_chars (GTK_LABEL (self->label), 70);
gtk_box_pack_start (GTK_BOX (self->box), self->label, FALSE, FALSE, 0);
}
GtkWidget *
gtk_tooltip_window_new (void)
{
return g_object_new (GTK_TYPE_TOOLTIP_WINDOW, 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);
}
}

View File

@ -0,0 +1,60 @@
/* 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/.
*/
#ifndef __GTK_TOOLTIP_WINDOW_PRIVATE_H__
#define __GTK_TOOLTIP_WINDOW_PRIVATE_H__
#include <gio/gio.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtkwindow.h>
G_BEGIN_DECLS
#define GTK_TYPE_TOOLTIP_WINDOW (gtk_tooltip_window_get_type ())
G_DECLARE_FINAL_TYPE (GtkTooltipWindow, gtk_tooltip_window, GTK, TOOLTIP_WINDOW, GtkWindow)
GtkWidget * gtk_tooltip_window_new (void);
void gtk_tooltip_window_set_label_markup (GtkTooltipWindow *window,
const char *markup);
void gtk_tooltip_window_set_label_text (GtkTooltipWindow *window,
const char *text);
void gtk_tooltip_window_set_image_icon (GtkTooltipWindow *window,
GdkPixbuf *pixbuf);
void gtk_tooltip_window_set_image_icon_from_stock (GtkTooltipWindow *window,
const char *stock_id,
GtkIconSize icon_size);
void gtk_tooltip_window_set_image_icon_from_name (GtkTooltipWindow *window,
const char *icon_name,
GtkIconSize icon_size);
void gtk_tooltip_window_set_image_icon_from_gicon (GtkTooltipWindow *window,
GIcon *gicon,
GtkIconSize icon_size);
void gtk_tooltip_window_set_custom_widget (GtkTooltipWindow *window,
GtkWidget *custom_widget);
G_END_DECLS
#endif /* __GTK_TOOLTIP_WINDOW_PRIVATE_H__ */