/[sido]/callbacks.c
ViewVC logotype

Contents of /callbacks.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations)
Fri Oct 16 20:27:45 2009 UTC (11 years, 1 month ago) by ben
File MIME type: text/plain
File size: 7709 byte(s)
rewrote volume widget because of curious behaviour on leave-notify-event
1 /*
2 * callbacks.c Copyright © 2009 by Benoît Rouits <brouits@free.fr>
3 * Published under the terms of the GNU General Public License v2 (GPLv2).
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 * see the COPYING file included in the jackie package or
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt for the full licence
22 *
23 */
24
25 #include <gtk/gtk.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include "controller.h"
29 #include "menu.h"
30 #include "about.h"
31 #include "version.h"
32 #define _(string) gettext (string)
33
34 /* refresh the tooltip */
35 gboolean tooltip_display (gpointer app_data) {
36 AppData* d = (AppData*) app_data;
37 gchar* tooltip;
38
39 tooltip = mpd_get_song(d->sock);
40 gtk_status_icon_set_tooltip(d->status_icon, tooltip);
41 g_free(tooltip);
42 return TRUE;
43 }
44
45 /* handler for the volume button drag */
46 void volume_on_event (GtkWidget* instance, gpointer app_data) {
47 gchar* cmd = NULL;
48 gdouble value = 0;
49 AppData* d = (AppData*) app_data;
50
51 value = gtk_range_get_value(GTK_RANGE(d->volume));
52 cmd = g_strdup_printf ("setvol %.0f", value);
53 mpd_control (d->sock, cmd);
54 g_free (cmd);
55 instance = NULL;
56 }
57
58 /* handler for left-button click */
59 void status_icon_on_left_click(GtkStatusIcon* instance, gpointer app_data) {
60 AppData* d = (AppData*) app_data;
61 GtkWidget* window;
62 gdouble curvol;
63
64 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
65 gtk_window_set_decorated (GTK_WINDOW(window), FALSE);
66 gtk_window_set_default_size(GTK_WINDOW(window), 24, 128);
67 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
68 d->volume = gtk_vscale_new_with_range (0, 100, 1);
69 gtk_range_set_inverted(GTK_RANGE(d->volume), TRUE);
70 curvol = mpd_get_vol(d->sock);
71 gtk_range_set_value(GTK_RANGE(d->volume), curvol);
72 gtk_container_add (GTK_CONTAINER(window), d->volume);
73 g_signal_connect(G_OBJECT(d->volume), "value-changed", G_CALLBACK(volume_on_event), app_data);
74 g_signal_connect_swapped(G_OBJECT(d->volume), "leave-notify-event", G_CALLBACK(gtk_widget_destroy), window);
75 gtk_widget_show_all(window);
76 instance = NULL;
77 }
78
79 /* handler for right-button click */
80 void status_icon_on_right_click(GtkStatusIcon* instance, guint button, guint activate_time, gpointer app_data) {
81 AppData* d = (AppData*) app_data;
82
83 if (app_data)
84 menu_show (GTK_MENU(d->right_menu), button, activate_time);
85 instance = NULL;
86 }
87
88 /* handler for the "Play" menu item */
89 void menu_item_on_play(GtkMenuItem* instance, gpointer app_data) {
90 AppData* d = (AppData*) app_data;
91
92 mpd_control(d->sock, "play");
93 instance = NULL;
94 }
95
96 /* handler for the "Pause" menu item */
97 void menu_item_on_pause(GtkMenuItem* instance, gpointer app_data) {
98 AppData* d = (AppData*) app_data;
99
100 mpd_control(d->sock, "pause");
101 instance = NULL;
102 }
103
104 void menu_item_on_next(GtkMenuItem* instance, gpointer app_data) {
105 AppData* d = (AppData*) app_data;
106
107 mpd_control(d->sock, "next");
108 instance = NULL;
109 }
110
111 void menu_item_on_prev(GtkMenuItem* instance, gpointer app_data) {
112 AppData* d = (AppData*) app_data;
113
114 mpd_control(d->sock, "previous");
115 instance = NULL;
116 }
117
118 void menu_item_on_stop(GtkMenuItem* instance, gpointer app_data) {
119 AppData* d = (AppData*) app_data;
120
121 mpd_control(d->sock, "stop");
122 instance = NULL;
123 }
124
125 void menu_item_on_shuffle(GtkMenuItem* instance, gpointer app_data) {
126 AppData* d = (AppData*) app_data;
127
128 mpd_control(d->sock, "shuffle");
129 instance = NULL;
130 }
131
132 /* handler for the "About" menu item (see version.h) */
133 void menu_item_on_about(GtkMenuItem* instance, gpointer unused) {
134 GtkAboutDialog* about;
135 const gchar* authors [] = {
136 PROG_AUTHOR0,
137 NULL
138 };
139
140 about = about_create (PROG_NAME, PROG_VERSION, PROG_COPYRIGHT,
141 PROG_COMMENT, PROG_LICENSE, PROG_WEBSITE,
142 authors);
143 about_show (about);
144 unused = NULL; /* avoid compiler warnings */
145 instance = NULL; /* _ */
146 return;
147 }
148
149 /* callback on Preferences window closed */
150 void pref_window_on_close (gpointer app_data) {
151 AppData* d = (AppData*) app_data;
152
153 g_free(d->ip);
154 d->ip = g_strdup (gtk_entry_get_text(d->ip_entry));
155
156 d->port = gtk_spin_button_get_value_as_int (d->port_spin);
157
158 /* write it to $HOME/.fala */
159 write_config (d);
160 gtk_widget_hide (GTK_WIDGET(d->pref_window));
161 gtk_widget_destroy (GTK_WIDGET(d->pref_window));
162 gtk_widget_set_sensitive (GTK_WIDGET(d->pref_item), TRUE);
163 d->pref_window = NULL;
164 mpd_disconnect (d->sock);
165 d->sock = -1;
166 d->sock = mpd_connect (d->ip, d->port);
167 }
168
169 /* handler for the "Preference" menu item */
170 void menu_item_on_pref(GtkMenuItem* instance, gpointer app_data) {
171 AppData* d = (AppData*) app_data;
172 GtkVBox* vbox;
173 GtkHBox* hbox1;
174 GtkHBox* hbox2;
175 GtkHBox* hbox3;
176 GtkLabel* ip_label;
177 GtkLabel* port_label;
178 GtkLabel* empty_label;
179 GtkButton* close_button;
180
181 d->pref_window = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
182 gtk_window_set_title (GTK_WINDOW(d->pref_window), _("Preferences"));
183 gtk_window_set_default_size (GTK_WINDOW(d->pref_window), 300, 200);
184 gtk_window_set_resizable (GTK_WINDOW(d->pref_window), TRUE);
185
186 vbox = GTK_VBOX(gtk_vbox_new (TRUE, 0));
187 gtk_container_add (GTK_CONTAINER(d->pref_window), GTK_WIDGET(vbox));
188
189 hbox1 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
190 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox1), TRUE, FALSE, 10);
191
192 hbox2 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
193 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox2), TRUE, FALSE, 10);
194
195 hbox3 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
196 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox3), TRUE, FALSE, 10);
197
198 ip_label = GTK_LABEL(gtk_label_new (_("MPD IP Address:")));
199 gtk_box_pack_start (GTK_BOX(hbox1), GTK_WIDGET(ip_label), TRUE, FALSE, 10);
200 port_label = GTK_LABEL(gtk_label_new (_("MPD TCP Port:")));
201 gtk_box_pack_start (GTK_BOX(hbox2), GTK_WIDGET(port_label), TRUE, FALSE, 10);
202
203 d->ip_entry = GTK_ENTRY(gtk_entry_new ());
204 gtk_entry_set_max_length (d->ip_entry, 15);
205 gtk_entry_set_width_chars (d->ip_entry, 15);
206 gtk_entry_set_text (d->ip_entry, d->ip); /* MPD ip address */
207 gtk_box_pack_start( GTK_BOX(hbox1), GTK_WIDGET(d->ip_entry), TRUE, FALSE, 10);
208 d->port_spin = GTK_SPIN_BUTTON(gtk_spin_button_new_with_range (1, 65535, 1));
209 gtk_spin_button_set_value (d->port_spin, (gdouble)d->port); /* MPD tcp port */
210 gtk_box_pack_start (GTK_BOX(hbox2), GTK_WIDGET(d->port_spin), TRUE, FALSE, 10);
211
212 empty_label = GTK_LABEL(gtk_label_new (""));
213 gtk_box_pack_start (GTK_BOX(hbox3), GTK_WIDGET(empty_label), TRUE, FALSE, 10);
214
215 close_button = GTK_BUTTON(gtk_button_new_from_stock (GTK_STOCK_CLOSE));
216 gtk_box_pack_start (GTK_BOX(hbox3), GTK_WIDGET(close_button), FALSE, FALSE, 10);
217
218 g_signal_connect_swapped (G_OBJECT(close_button), "clicked", G_CALLBACK(pref_window_on_close), (gpointer)d);
219 g_signal_connect_swapped (G_OBJECT(d->pref_window), "delete-event", G_CALLBACK(pref_window_on_close), (gpointer)d);
220
221 gtk_widget_show_all (GTK_WIDGET(d->pref_window));
222 gtk_widget_set_sensitive (GTK_WIDGET(instance), FALSE);
223 }
224
225 /* handler for the "Quit" menu item */
226 void menu_item_on_quit(GtkMenuItem* instance, gpointer app_data) {
227 AppData* d = (AppData*) app_data;
228 quit (d);
229 instance = NULL; /* useless but does not warn at compile time */
230 }
231

  ViewVC Help
Powered by ViewVC 1.1.26