added 'sensor preferences' in the popup menu
[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();
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_sensor_preferences(GtkMenuItem *mi, gpointer data)
36 {
37         struct ui_psensor *ui = data;
38
39         if (ui->sensors && *ui->sensors)
40                 ui_sensorpref_dialog_run(*ui->sensors, ui);
41 }
42
43 static const char *menu_desc =
44 "<ui>"
45 "  <popup name='MainMenu'>"
46 "      <menuitem name='Preferences' action='PreferencesAction' />"
47 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
48 "      <separator />"
49 "      <menuitem name='Quit' action='QuitAction' />"
50 "  </popup>"
51 "</ui>";
52
53 static GtkActionEntry entries[] = {
54   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
55
56   { "PreferencesAction", GTK_STOCK_PREFERENCES,     /* name, stock id */
57     "_Preferences", NULL,                           /* label, accelerator */
58     "Preferences",                                  /* tooltip */
59     G_CALLBACK(cb_preferences) },
60
61   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,/* name, stock id */
62     "_Sensor Preferences", NULL,                     /* label, accelerator */
63     "Sensor Preferences",                            /* tooltip */
64     G_CALLBACK(cb_sensor_preferences) },
65
66   { "QuitAction",
67     GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
68 };
69 static guint n_entries = G_N_ELEMENTS(entries);
70
71 static GtkWidget *get_menu(struct ui_psensor *ui)
72 {
73         GtkActionGroup      *action_group;
74         GtkUIManager        *menu_manager;
75         GError              *error;
76
77         action_group = gtk_action_group_new("PsensorActions");
78         gtk_action_group_set_translation_domain(action_group, PACKAGE);
79         menu_manager = gtk_ui_manager_new();
80
81         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
82         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
83
84         error = NULL;
85         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
86
87         if (error)
88                 g_error(_("building menus failed: %s"), error->message);
89
90         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
91 }
92
93
94 static int 
95 on_graph_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
96 {
97         GtkWidget *menu;
98
99         if (event->type != GDK_BUTTON_PRESS)
100                 return FALSE;
101
102         menu = get_menu((struct ui_psensor *)data);
103
104         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
105                        event->button, event->time);
106
107         return TRUE;
108 }
109
110 static gboolean
111 on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
112 {
113         struct ui_psensor *ui_psensor = (struct ui_psensor *)data;
114
115         graph_update(ui_psensor->sensors,
116                      ui_psensor->w_graph, ui_psensor->config);
117
118         return FALSE;
119 }
120
121 GtkWidget *ui_graph_create(struct ui_psensor * ui)
122 {
123         GtkWidget *w_graph;
124
125         w_graph = gtk_drawing_area_new();
126
127         g_signal_connect(GTK_WIDGET(w_graph),
128                          "expose-event", G_CALLBACK(on_expose_event), ui);
129
130         gtk_widget_add_events(w_graph, GDK_BUTTON_PRESS_MASK);
131
132         g_signal_connect(GTK_WIDGET(w_graph),
133                            "button_press_event",
134                            (GCallback) on_graph_clicked, ui);
135
136         return w_graph;
137 }