switched from wpitchoune@gmail.com to jeanfi@gmail.com (my usual email)
[psensor.git] / src / ui_graph.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19
20 #include "graph.h"
21 #include "ui_graph.h"
22 #include "ui_pref.h"
23
24 static void
25 cb_preferences(gpointer data, guint callback_action, GtkWidget *item)
26 {
27         ui_pref_dialog_run((struct ui_psensor *)data);
28 }
29
30 static GtkItemFactoryEntry menu_items[] = {
31         {N_("/Preferences"),
32          NULL, cb_preferences, 0, "<Item>"},
33
34         {"/sep1",
35          NULL, NULL, 0, "<Separator>"},
36
37         {N_("/Quit"),
38          "", ui_psensor_exit, 0, "<StockItem>", GTK_STOCK_QUIT},
39 };
40
41 static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
42
43 GtkWidget *ui_graph_create_popupmenu(struct ui_psensor *ui)
44 {
45         GtkItemFactory *item_factory;
46
47         item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
48         gtk_item_factory_create_items(item_factory,
49                                       nmenu_items, menu_items, ui);
50         return gtk_item_factory_get_widget(item_factory, "<main>");
51 }
52
53 int on_graph_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
54 {
55         GtkWidget *menu;
56
57         if (event->type != GDK_BUTTON_PRESS)
58                 return FALSE;
59
60         menu = ui_graph_create_popupmenu((struct ui_psensor *)data);
61
62         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
63                        event->button, event->time);
64
65         return TRUE;
66 }
67
68 gboolean
69 on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
70 {
71         struct ui_psensor *ui_psensor = (struct ui_psensor *)data;
72
73         graph_update(ui_psensor->sensors,
74                      ui_psensor->w_graph, ui_psensor->config);
75
76         return FALSE;
77 }
78
79 GtkWidget *ui_graph_create(struct ui_psensor * ui)
80 {
81         GtkWidget *w_graph;
82
83         w_graph = gtk_drawing_area_new();
84
85         g_signal_connect(G_OBJECT(w_graph),
86                          "expose-event", G_CALLBACK(on_expose_event), ui);
87
88         gtk_widget_add_events(w_graph, GDK_BUTTON_PRESS_MASK);
89         gtk_signal_connect(GTK_OBJECT(w_graph),
90                            "button_press_event",
91                            (GCallback) on_graph_clicked, ui);
92
93         return w_graph;
94 }