]> www.fi.muni.cz Git - evince.git/blobdiff - lib/ev-tooltip.c
Added French translation Added fr to DOC_LINGUAS
[evince.git] / lib / ev-tooltip.c
index 14326ab2b9b19a17a9395ad0d51557affe8bcded..f66f22f53f4ad831dc490be294905283d6b74c7a 100644 (file)
@@ -3,7 +3,7 @@
  *  Copyright (C) 2004 Red Hat, Inc.
  *
  *  Author:
  *  Copyright (C) 2004 Red Hat, Inc.
  *
  *  Author:
- *    Jonathan Blandford <jrb@alum.mit.edu>
+ *    Marco Pesenti Gritti <marco@gnome.org>
  *
  * Evince is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  *
  * Evince is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
 
 #include <gtk/gtklabel.h>
 
 
 #include <gtk/gtklabel.h>
 
+#define DEFAULT_DELAY 500
+#define STICKY_DELAY 500
+#define STICKY_REVERT_DELAY 1000
+#define SPACE_FROM_CURSOR 10
+
 struct _EvTooltipPrivate {
        GtkWidget *label;
 struct _EvTooltipPrivate {
        GtkWidget *label;
+       GTimeVal last_deactivate;
+       int timer_tag;
+       gboolean active;
 };
 
 G_DEFINE_TYPE (EvTooltip, ev_tooltip, GTK_TYPE_WINDOW)
 };
 
 G_DEFINE_TYPE (EvTooltip, ev_tooltip, GTK_TYPE_WINDOW)
@@ -49,12 +57,24 @@ ev_tooltip_expose_event (GtkWidget      *widget,
        return GTK_WIDGET_CLASS (ev_tooltip_parent_class)->expose_event (widget, event);
 }
 
        return GTK_WIDGET_CLASS (ev_tooltip_parent_class)->expose_event (widget, event);
 }
 
+static void
+ev_tooltip_dispose (GObject *object)
+{
+       EvTooltip *tooltip = EV_TOOLTIP (object);
+
+       if (tooltip->priv->timer_tag) {
+               g_source_remove (tooltip->priv->timer_tag);
+               tooltip->priv->timer_tag = 0;
+       }
+}
+
 static void
 ev_tooltip_class_init (EvTooltipClass *class)
 {
        GObjectClass *g_object_class = G_OBJECT_CLASS (class);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
 
 static void
 ev_tooltip_class_init (EvTooltipClass *class)
 {
        GObjectClass *g_object_class = G_OBJECT_CLASS (class);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
 
+       g_object_class->dispose = ev_tooltip_dispose;
        widget_class->expose_event = ev_tooltip_expose_event;
 
        g_type_class_add_private (g_object_class, sizeof (EvTooltipPrivate));
        widget_class->expose_event = ev_tooltip_expose_event;
 
        g_type_class_add_private (g_object_class, sizeof (EvTooltipPrivate));
@@ -68,6 +88,8 @@ ev_tooltip_init (EvTooltip *tooltip)
 
        tooltip->priv = EV_TOOLTIP_GET_PRIVATE (tooltip);
 
 
        tooltip->priv = EV_TOOLTIP_GET_PRIVATE (tooltip);
 
+       gtk_widget_set_app_paintable (GTK_WIDGET (tooltip), TRUE);
+       gtk_window_set_resizable (GTK_WINDOW (tooltip), FALSE);
        gtk_widget_set_name (window, "gtk-tooltips");
 
        label = gtk_label_new (NULL);
        gtk_widget_set_name (window, "gtk-tooltips");
 
        label = gtk_label_new (NULL);
@@ -107,11 +129,77 @@ ev_tooltip_set_text (EvTooltip *tooltip, const char *text)
 void
 ev_tooltip_set_position (EvTooltip *tooltip, int x, int y)
 {
 void
 ev_tooltip_set_position (EvTooltip *tooltip, int x, int y)
 {
-       int root_x = 0, root_y = 0;
+       int root_x, root_y;
 
        if (tooltip->parent != NULL) {
                gdk_window_get_origin (tooltip->parent->window, &root_x, &root_y);
        }
 
 
        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);
+       gtk_window_move (GTK_WINDOW (tooltip),
+                        x + root_x + SPACE_FROM_CURSOR,
+                        y + root_y + SPACE_FROM_CURSOR);
+}
+
+static gboolean
+ev_tooltip_recently_shown (EvTooltip *tooltip)
+{
+       GTimeVal now;
+       glong msec;
+  
+       g_get_current_time (&now);
+
+       msec = (now.tv_sec  - tooltip->priv->last_deactivate.tv_sec) * 1000 +
+              (now.tv_usec - tooltip->priv->last_deactivate.tv_usec) / 1000;
+
+       return (msec < STICKY_REVERT_DELAY);
+}
+
+static gint
+ev_tooltip_timeout (gpointer data)
+{
+       GtkWidget *tooltip = GTK_WIDGET (data);
+
+       gtk_widget_show (tooltip);
+
+       return FALSE;
+}
+
+void
+ev_tooltip_activate (EvTooltip *tooltip)
+{
+       int delay;
+
+       if (tooltip->priv->active) {
+               return;
+       } else {
+               tooltip->priv->active = TRUE;
+       }
+
+       if (ev_tooltip_recently_shown (tooltip)) {
+               delay = STICKY_DELAY;
+       } else {
+               delay = DEFAULT_DELAY;
+       }
+
+       tooltip->priv->timer_tag = g_timeout_add (delay, ev_tooltip_timeout,
+                                                 (gpointer)tooltip);
+}
+
+void
+ev_tooltip_deactivate (EvTooltip *tooltip)
+{
+       if (!tooltip->priv->active) {
+               return;
+       } else {
+               tooltip->priv->active = FALSE;
+       }
+
+       if (tooltip->priv->timer_tag) {
+               g_source_remove (tooltip->priv->timer_tag);
+               tooltip->priv->timer_tag = 0;
+       }
+
+       gtk_widget_hide (GTK_WIDGET (tooltip));
+
+       g_get_current_time (&tooltip->priv->last_deactivate);
 }
 }