Updated Spanish translation
[anjuta-git-plugin.git] / plugins / gtodo / tray-icon.c
blob18b8a6b066efeb7a0825b71ffea9d3588e754c68
1 #include <config.h>
2 #include <gtk/gtk.h>
3 #include <glib/gi18n.h>
4 #include <string.h>
5 #include "main.h"
6 #include "eggtrayicon.h"
7 #include "tray-icon.h"
8 #include "debug_printf.h"
10 EggTrayIcon *tray_icon = NULL;
11 GtkWidget *image = NULL;
12 GtkWidget *tip = NULL;
13 PangoLayout *tray_layout_tooltip = NULL;
16 /****************************************
17 * The table mouse menu *
18 ***************************************/
20 static GtkItemFactoryEntry tray_icon_menu[] =
22 {N_("/_New"), "", gui_add_todo_item, 0, "<StockItem>", GTK_STOCK_NEW},
23 {N_("/_Hide"), "", tray_hide_show_window, 0, "<StockItem>", GTK_STOCK_CONVERT},
24 {N_("/_Show"), "", tray_hide_show_window, 0, "<StockItem>", GTK_STOCK_CONVERT},
25 {"sep1", NULL, 0, 0, "<Separator>" },
26 {N_("/_Quit"), "", gtk_main_quit, 0, "<StockItem>", GTK_STOCK_QUIT}
29 static int ntray_icon_menu = sizeof (tray_icon_menu) / sizeof (tray_icon_menu[0]);
32 static
33 void tray_paint_tip(GtkWidget *widget, GdkEventExpose *event)
35 int width = 0, height = 0;
36 GTodoList *list = NULL;
38 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_OUT,
39 NULL, widget, "tooltip", 0, 0, -1, -1);
43 list = gtodo_client_get_todo_item_list(cl, NULL);
44 if(list != NULL)
48 int m_height =0 , m_width = 0;
49 gchar *string = NULL;
50 GTodoItem *item = gtodo_client_get_todo_item_from_list(list);
52 string = gtodo_todo_item_get_summary(item);
54 pango_layout_set_markup(tray_layout_tooltip, string, strlen(string));
55 pango_layout_get_size(tray_layout_tooltip, &m_width, &m_height);
56 if(!gtodo_todo_item_get_done(item))
58 gtk_paint_arrow(widget->style, widget->window, GTK_STATE_NORMAL,
59 GTK_SHADOW_IN,NULL, widget, "tooltip",GTK_ARROW_RIGHT, TRUE, 4, 4+height, PANGO_PIXELS(m_height),
60 PANGO_PIXELS(m_height));
62 gtk_paint_layout (widget->style, widget->window, GTK_STATE_NORMAL, TRUE,
63 NULL, widget, "tooltip", 18,4+height, tray_layout_tooltip);
65 width = MAX(PANGO_PIXELS(width), m_width);
66 height = height + PANGO_PIXELS(m_height);
69 }while(gtodo_client_get_list_next(list));
70 gtodo_client_free_todo_item_list(cl, list);
72 if(height == 0)
74 pango_layout_set_markup(tray_layout_tooltip, _("Todo List"), strlen(_("Todo List")));
75 pango_layout_get_size(tray_layout_tooltip, &width, &height);
78 gtk_paint_layout (widget->style, widget->window, GTK_STATE_NORMAL, TRUE,
79 NULL, widget, "tooltip", 4,4, tray_layout_tooltip);
83 static
84 gboolean tray_motion_cb (GtkWidget *tv, GdkEventCrossing *event, gpointer n)
86 int width = 0,height = 0;
87 GdkRectangle msize;
88 int x,y;
89 char *tooltiptext = NULL;
90 GTodoList *list = NULL;
91 int monitor = gdk_screen_get_monitor_at_window(
92 gtk_widget_get_screen(tv), tv->window);
93 if(tip != NULL) return FALSE;
94 tooltiptext = g_strdup("getting height");
98 tip = gtk_window_new(GTK_WINDOW_POPUP);
99 gtk_widget_set_app_paintable(tip, TRUE);
100 gtk_window_set_resizable(GTK_WINDOW(tip), FALSE);
101 gtk_widget_set_name(tip, "gtk-tooltips");
102 gtk_widget_ensure_style (tip);
104 tray_layout_tooltip = gtk_widget_create_pango_layout (tip, NULL);
105 pango_layout_set_wrap(tray_layout_tooltip, PANGO_WRAP_WORD);
107 list = gtodo_client_get_todo_item_list(cl, NULL);
108 if(list != NULL)
112 int m_height =0 , m_width = 0;
113 gchar *string = NULL;
114 GTodoItem *item = gtodo_client_get_todo_item_from_list(list);
116 string = gtodo_todo_item_get_summary(item);
118 pango_layout_set_markup(tray_layout_tooltip, string, strlen(string));
119 pango_layout_get_size(tray_layout_tooltip, &m_width, &m_height);
121 if(!gtodo_todo_item_get_done(item))
123 width = MAX(width, m_width);
124 height = height + m_height;
127 }while(gtodo_client_get_list_next(list));
128 gtodo_client_free_todo_item_list(cl, list);
130 if(height == 0)
132 pango_layout_set_markup(tray_layout_tooltip, _("Todo List"), strlen(_("Todo List")));
133 pango_layout_get_size(tray_layout_tooltip, &width, &height);
134 width = width - 18*PANGO_SCALE;
137 gdk_screen_get_monitor_geometry(gtk_widget_get_screen(tv), monitor, &msize);
139 g_signal_connect(G_OBJECT(tip), "expose_event",
140 G_CALLBACK(tray_paint_tip), NULL);
143 width= PANGO_PIXELS(width) +26;
144 height= PANGO_PIXELS(height)+8;
148 /* set size */
149 gtk_widget_set_usize(tip, width,height);
151 /* calculate position */
152 x = (int)event->x_root - event->x+tv->allocation.width/2 - width/2;
153 y = (int)event->y_root+(tv->allocation.height - event->y) +5;
155 /* check borders left, right*/
156 if((x+width) > msize.width+msize.x)
158 x = msize.x+msize.width-(width);
160 else if(x < 0)
162 x= 0;
164 /* check up down.. if can't place it below, place it above */
165 if( y+height > msize.height+msize.y)
167 y = event->y_root - event->y -5-(height);
169 /* place the window */
170 gtk_window_move(GTK_WINDOW(tip), x, y);
172 gtk_widget_show_all(tip);
175 g_free(tooltiptext);
176 gtk_widget_queue_draw(tip);
177 return TRUE;
182 static
183 void tray_leave_cb (GtkWidget *w, GdkEventCrossing *e, gpointer n)
185 // if(tray_timeout != -1) g_source_remove(tray_timeout);
186 // tray_timeout = -1;
187 if(tip != NULL)
189 gtk_widget_destroy(tip);
190 g_object_unref(tray_layout_tooltip);
193 tip = NULL;
196 void tray_icon_remove()
198 gtk_widget_destroy(GTK_WIDGET(tray_icon));
199 tray_icon = NULL;
203 /* if the tray is destroyed, recreate it again */
204 static
205 void tray_icon_destroy(GtkWidget *ticon, GtkWidget *window)
207 if(gconf_client_get_bool(client, "/apps/gtodo/view/enable-tray",NULL))
209 tray_init(window);
213 void tray_hide_show_window()
215 GtkWidget *window;
217 window = g_object_get_data (G_OBJECT(tray_icon), "window");
219 if(GTK_WIDGET_VISIBLE(window))
221 debug_printf(DEBUG_INFO, "Hiding window");
222 gtk_widget_hide(window);
224 else
226 debug_printf(DEBUG_INFO, "Showing window");
227 gtk_widget_show(window);
231 static
232 int tray_mouse_click(GtkWidget *wid, GdkEventButton *event)
234 GtkWidget *window;
236 window = g_object_get_data (G_OBJECT(tray_icon), "window");
238 /* left mouse button */
239 /* Show/hide the window*/
240 if(event->button == 1)
242 tray_hide_show_window();
244 /* right mouse menu */
245 else if (event->button == 3)
247 GtkItemFactory *item = gtk_item_factory_new(GTK_TYPE_MENU, "<tablepop>", NULL);
248 gtk_item_factory_create_items (item, ntray_icon_menu, tray_icon_menu, NULL);
249 /* add it */
250 if(GTK_WIDGET_VISIBLE(window))
252 gtk_widget_hide(gtk_item_factory_get_widget(item, "/Show"));
254 else
256 gtk_widget_hide(gtk_item_factory_get_widget(item, "/Hide"));
258 gtk_menu_popup(GTK_MENU(gtk_item_factory_get_widget(GTK_ITEM_FACTORY(item), "<tablepop>")),
259 NULL,NULL,NULL, NULL, event->button, event->time);
261 return TRUE;
265 /* create the tray icon */
266 void tray_init(GtkWidget *window)
268 GtkWidget *eventbox = NULL;
269 GdkPixbuf *pixbuf = NULL;
271 if(!gconf_client_get_bool(client, "/apps/gtodo/view/enable-tray",NULL))
273 return;
275 if(tray_icon != NULL) return;
278 debug_printf(DEBUG_INFO,"Creating Tray Icon\n");
280 /* setup the tray icon */
281 tray_icon = egg_tray_icon_new(_("Todo List Manager"));
283 /* event box */
284 eventbox = gtk_event_box_new();
286 /* add a image (in an event box) to the tray icon */
289 * the icon is a fixed size, I have no idea (yet) how to get the size of the tray widget,
290 * Withouth doing the drawing myself.
292 pixbuf = gdk_pixbuf_new_from_file_at_size(PIXMAP_PATH"/gtodo_tray.png", 20,20,NULL);
293 image = gtk_image_new_from_pixbuf(pixbuf);
294 g_object_unref(pixbuf);
296 /* add everything */
297 gtk_container_add(GTK_CONTAINER(eventbox), image);
298 gtk_container_add(GTK_CONTAINER(tray_icon), eventbox);
301 /* connect signals to the tray icon */
302 g_signal_connect(G_OBJECT(tray_icon), "destroy", G_CALLBACK(tray_icon_destroy), window);
303 g_signal_connect(G_OBJECT(tray_icon), "button-release-event", G_CALLBACK(tray_mouse_click), NULL);
304 g_signal_connect(G_OBJECT(eventbox), "enter-notify-event", G_CALLBACK(tray_motion_cb), NULL);
305 g_signal_connect(G_OBJECT(eventbox), "leave-notify-event", G_CALLBACK(tray_leave_cb), NULL);
307 /* show the tray icon */
308 gtk_widget_show_all(GTK_WIDGET(tray_icon));
309 g_object_set_data (G_OBJECT(tray_icon), "window", window);