From: Marco Pesenti Gritti Date: Sat, 3 Sep 2005 13:10:53 +0000 (+0000) Subject: Simple widget to show tooltips at a custom position X-Git-Tag: EVINCE_0_5_0~186 X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=633afd6e16f6ae9561611edcfab737e81cfdc7f4;p=evince.git Simple widget to show tooltips at a custom position 2005-09-03 Marco Pesenti Gritti * lib/Makefile.am: * lib/ev-tooltip.c: (ev_tooltip_expose_event), (ev_tooltip_class_init), (ev_tooltip_init), (ev_tooltip_new), (ev_tooltip_set_text), (ev_tooltip_set_position): * lib/ev-tooltip.h: Simple widget to show tooltips at a custom position * shell/ev-view.c: (tip_from_link), (ev_view_motion_notify_event), (ev_view_destroy): Use it for links. First go, needs work --- diff --git a/ChangeLog b/ChangeLog index 29e1765d..2148ed67 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2005-09-03 Marco Pesenti Gritti + + * lib/Makefile.am: + * lib/ev-tooltip.c: (ev_tooltip_expose_event), + (ev_tooltip_class_init), (ev_tooltip_init), (ev_tooltip_new), + (ev_tooltip_set_text), (ev_tooltip_set_position): + * lib/ev-tooltip.h: + + Simple widget to show tooltips at a custom position + + * shell/ev-view.c: (tip_from_link), (ev_view_motion_notify_event), + (ev_view_destroy): + + Use it for links. First go, needs work + 2005-08-17 Dennis Cranston * shell/ev-window.c: (ev_window_cmd_edit_toolbar): A couple small diff --git a/lib/ev-tooltip.c b/lib/ev-tooltip.c new file mode 100644 index 00000000..14326ab2 --- /dev/null +++ b/lib/ev-tooltip.c @@ -0,0 +1,117 @@ +/* this file is part of evince, a gnome document viewer + * + * Copyright (C) 2004 Red Hat, Inc. + * + * Author: + * Jonathan Blandford + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "ev-tooltip.h" + +#include + +struct _EvTooltipPrivate { + GtkWidget *label; +}; + +G_DEFINE_TYPE (EvTooltip, ev_tooltip, GTK_TYPE_WINDOW) + +#define EV_TOOLTIP_GET_PRIVATE(object) \ + (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_TOOLTIP, EvTooltipPrivate)) + +static gboolean +ev_tooltip_expose_event (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 GTK_WIDGET_CLASS (ev_tooltip_parent_class)->expose_event (widget, event); +} + +static void +ev_tooltip_class_init (EvTooltipClass *class) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (class); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); + + widget_class->expose_event = ev_tooltip_expose_event; + + g_type_class_add_private (g_object_class, sizeof (EvTooltipPrivate)); +} + +static void +ev_tooltip_init (EvTooltip *tooltip) +{ + GtkWidget *window = GTK_WIDGET (tooltip); + GtkWidget *label; + + tooltip->priv = EV_TOOLTIP_GET_PRIVATE (tooltip); + + gtk_widget_set_name (window, "gtk-tooltips"); + + label = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); + gtk_container_add (GTK_CONTAINER (window), label); + gtk_container_set_border_width (GTK_CONTAINER (window), 3); + tooltip->priv->label = label; + + gtk_widget_show (label); +} + +/* Public functions */ + +GtkWidget * +ev_tooltip_new (GtkWidget *parent) +{ + GtkWidget *tooltip; + GtkWidget *toplevel; + + tooltip = g_object_new (EV_TYPE_TOOLTIP, NULL); + + GTK_WINDOW (tooltip)->type = GTK_WINDOW_POPUP; + EV_TOOLTIP (tooltip)->parent = parent; + + toplevel = gtk_widget_get_toplevel (parent); + gtk_window_set_transient_for (GTK_WINDOW (tooltip), GTK_WINDOW (toplevel)); + + return tooltip; +} + +void +ev_tooltip_set_text (EvTooltip *tooltip, const char *text) +{ + gtk_label_set_text (GTK_LABEL (tooltip->priv->label), text); +} + +void +ev_tooltip_set_position (EvTooltip *tooltip, int x, int y) +{ + int root_x = 0, root_y = 0; + + if (tooltip->parent != NULL) { + gdk_window_get_origin (tooltip->parent->window, &root_x, &root_y); + } + + gtk_window_move (GTK_WINDOW (tooltip), x + root_x, y + root_y); +} diff --git a/lib/ev-tooltip.h b/lib/ev-tooltip.h new file mode 100644 index 00000000..b57368c3 --- /dev/null +++ b/lib/ev-tooltip.h @@ -0,0 +1,66 @@ +/* ev-sidebar.h + * this file is part of evince, a gnome document viewer + * + * Copyright (C) 2005 Red Hat, Inc. + * + * Author: + * Marco Pesenti Gritti + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __EV_TOOLTIP_H__ +#define __EV_TOOLTIP_H__ + +#include + +G_BEGIN_DECLS + +typedef struct _EvTooltip EvTooltip; +typedef struct _EvTooltipClass EvTooltipClass; +typedef struct _EvTooltipPrivate EvTooltipPrivate; + +#define EV_TYPE_TOOLTIP (ev_tooltip_get_type()) +#define EV_TOOLTIP(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_TOOLTIP, EvTooltip)) +#define EV_TOOLTIP_CLASS(klass) (G_TYPE_CHACK_CLASS_CAST((klass), EV_TYPE_TOOLTIP, EvTooltipClass)) +#define EV_IS_TOOLTIP(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_TOOLTIP)) +#define EV_IS_TOOLTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), EV_TYPE_TOOLTIP)) +#define EV_TOOLTIP_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), EV_TYPE_TOOLTIP, EvTooltipClass)) + +struct _EvTooltip { + GtkWindow base_instance; + + GtkWidget *parent; + + EvTooltipPrivate *priv; +}; + +struct _EvTooltipClass { + GtkWindowClass base_class; +}; + +GType ev_tooltip_get_type (void); +GtkWidget *ev_tooltip_new (GtkWidget *parent); +void ev_tooltip_set_text (EvTooltip *tooltip, + const char *text); +void ev_tooltip_set_position (EvTooltip *tooltip, + int x, + int y); + +G_END_DECLS + +#endif /* __EV_TOOLTIP_H__ */ + + diff --git a/shell/ev-view.c b/shell/ev-view.c index 815f69d9..f5b22fd1 100644 --- a/shell/ev-view.c +++ b/shell/ev-view.c @@ -37,6 +37,7 @@ #include "ev-job-queue.h" #include "ev-page-cache.h" #include "ev-pixbuf-cache.h" +#include "ev-tooltip.h" #define EV_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_VIEW, EvViewClass)) #define EV_IS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_VIEW)) @@ -148,6 +149,7 @@ struct _EvView { int pressed_button; EvViewCursor cursor; + GtkWidget *link_tooltip; EvPageCache *page_cache; EvPixbufCache *pixbuf_cache; @@ -244,7 +246,7 @@ static EvLink* get_link_at_location (EvView gdouble y); static void go_to_link (EvView *view, EvLink *link); -static char* status_message_from_link (EvView *view, +static char* tip_from_link (EvView *view, EvLink *link); /*** GtkWidget implementation ***/ @@ -1076,7 +1078,7 @@ go_to_link (EvView *view, EvLink *link) } static char * -status_message_from_link (EvView *view, EvLink *link) +tip_from_link (EvView *view, EvLink *link) { EvLinkType type; char *msg = NULL; @@ -1528,13 +1530,23 @@ ev_view_motion_notify_event (GtkWidget *widget, EvLink *link; link = get_link_at_location (view, event->x + view->scroll_x, event->y + view->scroll_y); + + if (!link && view->link_tooltip) { + gtk_widget_hide (view->link_tooltip); + } + if (link) { - char *msg; + char *msg = tip_from_link (view, link); - msg = status_message_from_link (view, link); - ev_view_set_status (view, msg); - ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK); + if (view->link_tooltip == NULL) { + view->link_tooltip = ev_tooltip_new (GTK_WIDGET (view)); + } + ev_tooltip_set_position (EV_TOOLTIP (view->link_tooltip), event->x, event->y); + ev_tooltip_set_text (EV_TOOLTIP (view->link_tooltip), msg); + gtk_widget_show (view->link_tooltip); g_free (msg); + + ev_view_set_cursor (view, EV_VIEW_CURSOR_LINK); } else if (location_in_text (view, event->x + view->scroll_x, event->y + view->scroll_y)) { ev_view_set_cursor (view, EV_VIEW_CURSOR_IBEAM); } else { @@ -1881,6 +1893,11 @@ ev_view_destroy (GtkObject *object) g_object_unref (view->pixbuf_cache); view->pixbuf_cache = NULL; } + if (view->link_tooltip) { + gtk_widget_destroy (view->link_tooltip); + view->link_tooltip = NULL; + } + ev_view_set_scroll_adjustments (view, NULL, NULL); GTK_OBJECT_CLASS (ev_view_parent_class)->destroy (object);