fixed error about 'expose-event' (signal replaced by 'draw') when compiled with...
[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 #include "ui_sensorpref.h"
24
25 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
26 {
27         ui_psensor_quit((struct ui_psensor *)data);
28 }
29
30 static void cb_preferences(GtkMenuItem *mi, gpointer data)
31 {
32         ui_pref_dialog_run((struct ui_psensor *)data);
33 }
34
35 static void cb_about(GtkMenuItem *mi, gpointer data)
36 {
37         ui_show_about_dialog();
38 }
39
40 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
41 {
42         struct ui_psensor *ui = data;
43
44         if (ui->sensors && *ui->sensors)
45                 ui_sensorpref_dialog_run(*ui->sensors, ui);
46 }
47
48 static const char *menu_desc =
49 "<ui>"
50 "  <popup name='MainMenu'>"
51 "      <menuitem name='Preferences' action='PreferencesAction' />"
52 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
53 "      <separator />"
54 "      <menuitem name='About' action='AboutAction' />"
55 "      <separator />"
56 "      <menuitem name='Quit' action='QuitAction' />"
57 "  </popup>"
58 "</ui>";
59
60 static GtkActionEntry entries[] = {
61   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
62
63   { "PreferencesAction", GTK_STOCK_PREFERENCES,     /* name, stock id */
64     "_Preferences", NULL,                           /* label, accelerator */
65     "Preferences",                                  /* tooltip */
66     G_CALLBACK(cb_preferences) },
67
68   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,/* name, stock id */
69     "_Sensor Preferences", NULL,                     /* label, accelerator */
70     "Sensor Preferences",                            /* tooltip */
71     G_CALLBACK(cb_sensor_preferences) },
72
73   { "AboutAction", NULL,
74     "_About", NULL,
75     "About",
76     G_CALLBACK(cb_about) },
77
78   { "QuitAction",
79     GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
80 };
81 static guint n_entries = G_N_ELEMENTS(entries);
82
83 static GtkWidget *get_menu(struct ui_psensor *ui)
84 {
85         GtkActionGroup      *action_group;
86         GtkUIManager        *menu_manager;
87         GError              *error;
88
89         action_group = gtk_action_group_new("PsensorActions");
90         gtk_action_group_set_translation_domain(action_group, PACKAGE);
91         menu_manager = gtk_ui_manager_new();
92
93         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
94         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
95
96         error = NULL;
97         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
98
99         if (error)
100                 g_error(_("building menus failed: %s"), error->message);
101
102         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
103 }
104
105
106 static int
107 on_graph_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
108 {
109         GtkWidget *menu;
110
111         if (event->type != GDK_BUTTON_PRESS)
112                 return FALSE;
113
114         menu = get_menu((struct ui_psensor *)data);
115
116         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
117                        event->button, event->time);
118
119         return TRUE;
120 }
121
122 static gboolean
123 on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
124 {
125         struct ui_psensor *ui_psensor = (struct ui_psensor *)data;
126
127         graph_update(ui_psensor->sensors,
128                      ui_psensor->w_graph, ui_psensor->config);
129
130         return FALSE;
131 }
132
133 GtkWidget *ui_graph_create(struct ui_psensor * ui)
134 {
135         GtkWidget *w_graph;
136
137         w_graph = gtk_drawing_area_new();
138
139         if (GTK_MAJOR_VERSION == 2)
140                 g_signal_connect(GTK_WIDGET(w_graph),
141                                  "expose-event",
142                                  G_CALLBACK(on_expose_event),
143                                  ui);
144         else
145                 g_signal_connect(GTK_WIDGET(w_graph),
146                                  "draw",
147                                  G_CALLBACK(on_expose_event),
148                                  ui);
149
150         gtk_widget_add_events(w_graph, GDK_BUTTON_PRESS_MASK);
151
152         g_signal_connect(GTK_WIDGET(w_graph),
153                            "button_press_event",
154                            (GCallback) on_graph_clicked, ui);
155
156         return w_graph;
157 }