Imported Upstream version 0.6.2.17
[psensor-pkg-debian.git] / src / ui_graph.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * 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 #include "graph.h"
20 #include "ui_graph.h"
21 #include "ui_pref.h"
22 #include "ui_sensorpref.h"
23
24 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
25 {
26         ui_psensor_quit((struct ui_psensor *)data);
27 }
28
29 static void cb_preferences(GtkMenuItem *mi, gpointer data)
30 {
31         ui_pref_dialog_run((struct ui_psensor *)data);
32 }
33
34 static void cb_about(GtkMenuItem *mi, gpointer data)
35 {
36         ui_show_about_dialog();
37 }
38
39 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
40 {
41         struct ui_psensor *ui = data;
42
43         if (ui->sensors && *ui->sensors)
44                 ui_sensorpref_dialog_run(*ui->sensors, ui);
45 }
46
47 static const char *menu_desc =
48 "<ui>"
49 "  <popup name='MainMenu'>"
50 "      <menuitem name='Preferences' action='PreferencesAction' />"
51 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
52 "      <separator />"
53 "      <menuitem name='About' action='AboutAction' />"
54 "      <separator />"
55 "      <menuitem name='Quit' action='QuitAction' />"
56 "  </popup>"
57 "</ui>";
58
59 static GtkActionEntry entries[] = {
60         { "PsensorMenuAction", NULL, "_Psensor" },
61
62         { "PreferencesAction", GTK_STOCK_PREFERENCES,
63           "_Preferences", NULL,
64           "Preferences",
65           G_CALLBACK(cb_preferences) },
66
67         { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
68           "_Sensor Preferences", NULL,
69           "Sensor Preferences",
70           G_CALLBACK(cb_sensor_preferences) },
71
72         { "AboutAction", NULL,
73           "_About", NULL,
74           "About",
75           G_CALLBACK(cb_about) },
76
77         { "QuitAction",
78           GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
79 };
80 static guint n_entries = G_N_ELEMENTS(entries);
81
82 static GtkWidget *get_menu(struct ui_psensor *ui)
83 {
84         GtkActionGroup      *action_group;
85         GtkUIManager        *menu_manager;
86         GError              *error;
87
88         action_group = gtk_action_group_new("PsensorActions");
89         gtk_action_group_set_translation_domain(action_group, PACKAGE);
90         menu_manager = gtk_ui_manager_new();
91
92         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
93         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
94
95         error = NULL;
96         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
97
98         if (error)
99                 g_error(_("building menus failed: %s"), error->message);
100
101         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
102 }
103
104
105 static int
106 on_graph_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
107 {
108         GtkWidget *menu;
109
110         if (event->type != GDK_BUTTON_PRESS)
111                 return FALSE;
112
113         menu = get_menu((struct ui_psensor *)data);
114
115         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
116                        event->button, event->time);
117
118         return TRUE;
119 }
120
121 static gboolean
122 on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
123 {
124         struct ui_psensor *ui_psensor = (struct ui_psensor *)data;
125
126         graph_update(ui_psensor->sensors,
127                      ui_psensor->w_graph, ui_psensor->config);
128
129         return FALSE;
130 }
131
132 GtkWidget *ui_graph_create(struct ui_psensor * ui)
133 {
134         GtkWidget *w_graph;
135
136         w_graph = gtk_drawing_area_new();
137
138         if (GTK_MAJOR_VERSION == 2)
139                 g_signal_connect(GTK_WIDGET(w_graph),
140                                  "expose-event",
141                                  G_CALLBACK(on_expose_event),
142                                  ui);
143         else
144                 g_signal_connect(GTK_WIDGET(w_graph),
145                                  "draw",
146                                  G_CALLBACK(on_expose_event),
147                                  ui);
148
149         gtk_widget_add_events(w_graph, GDK_BUTTON_PRESS_MASK);
150
151         g_signal_connect(GTK_WIDGET(w_graph),
152                            "button_press_event",
153                            (GCallback) on_graph_clicked, ui);
154
155         return w_graph;
156 }