root/sweep/trunk/src/sw_chooser.c

Revision 691, 10.0 kB (checked in by erikd, 2 years ago)

Remove needless #defines of buffer lengths.

Code had many instances of:

#undef BUF_LEN
#define BUF_LEN 64
char buf[BUF_LEN];
snprintf (buf, BUF_LEN, ....);

which can be replaced with:

char buf[64];
snprintf (buf, sizeof (buf), ....);

which is far cleaner and more difficult to get wrong.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Sweep, a sound wave editor.
3  *
4  * Copyright (C) 2000 Conrad Parker
5  * Copyright (C) 2002 Commonwealth Scientific and Industrial Research
6  * Organisation (CSIRO), Australia
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <glib.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtk.h>
34
35 #include <sweep/sweep_i18n.h>
36 #include <sweep/sweep_types.h>
37 #include <sweep/sweep_sample.h>
38
39 #include "interface.h"
40
41 #include "sw_chooser.h"
42
43 typedef struct {
44   int number;
45   char * name;
46 } sw_choice;
47
48 /* first choice must be _("Custom") */
49
50 static sw_choice samplerate_choices[] = {
51   {     -1, N_("Custom") },
52   { 192000, N_("192000 Hz (Studio quality)") },
53   {  96000, N_(" 96000 Hz (High quality)") },
54   {  48000, N_(" 48000 Hz (DAT quality)") },
55   {  44100, N_(" 44100 Hz (CD quality)") },
56   {  32000, N_(" 32000 Hz (Ultra-wideband voice quality)") },
57   {  22050, N_(" 22050 Hz") },
58   {  16000, N_(" 16000 Hz (Wideband voice quality)") },
59   {  11025, N_(" 11025 Hz") },
60   {   8000, N_("  8000 Hz (Narrowband voice quality)") },
61   {   4000, N_("  4000 Hz (Low quality)") },
62   {      0, NULL }
63 };
64
65 static sw_choice channelcount_choices[] = {
66   { -1, N_("Custom") },
67   {  1, N_("Mono") },
68   {  2, N_("Stereo") },
69   {  4, N_("Quadraphonic") },
70   /*  {  6, N_("5.1") },*/
71   {  0, NULL }
72 };
73
74 enum {
75   NUMBER_CHANGED_SIGNAL,
76   LAST_SIGNAL
77 };
78
79 static gint sw_chooser_signals[LAST_SIGNAL] = { 0 };
80
81 static void
82 sw_chooser_class_init(SWChooserClass * class)
83 {
84   GtkObjectClass *object_class;
85
86   object_class = (GtkObjectClass *) class;
87
88   sw_chooser_signals[NUMBER_CHANGED_SIGNAL] = g_signal_new ("number-changed",
89                                                                   G_TYPE_FROM_CLASS (class),
90                                       G_SIGNAL_RUN_FIRST,
91                                       G_STRUCT_OFFSET (SWChooserClass, number_changed),
92                                   NULL,
93                                   NULL,               
94                                                                   g_cclosure_marshal_VOID__INT,
95                                   G_TYPE_NONE, 1,
96                                                                   G_TYPE_INT);
97
98  
99   class->number_changed = NULL;
100 }
101
102 static void
103 sw_chooser_init (GtkWidget * chooser)
104 {
105 }
106
107
108 GType
109 sw_chooser_get_type (void)
110 {
111   static GType sw_chooser_type = 0;
112
113   if (!sw_chooser_type)
114     {
115       static const GTypeInfo sw_chooser_info =
116       {
117                  
118       sizeof(SWChooserClass),
119         NULL, /* base_init */
120         NULL, /* base_finalize */
121         (GClassInitFunc) sw_chooser_class_init,
122         NULL, /* class_finalize */
123         NULL, /* class_data */
124         sizeof (SWChooser),
125         0,    /* n_preallocs */
126         (GInstanceInitFunc) sw_chooser_init,     
127
128     };
129
130       sw_chooser_type = g_type_register_static (GTK_TYPE_FRAME, "SWChooser", &sw_chooser_info, 0);
131   }
132
133   return sw_chooser_type;
134 }
135
136
137 static int
138 chooser_get_number (GtkWidget * chooser)
139 {
140   return
141     GPOINTER_TO_INT (g_object_get_data (G_OBJECT(chooser), "number"));
142 }
143
144 static int
145 chooser_set_number_direct (GtkWidget * chooser, int number)
146 {
147   GtkWidget * direct_entry;
148   char buf[16];
149
150   direct_entry =
151     GTK_WIDGET (g_object_get_data (G_OBJECT(chooser), "direct_entry"));
152
153   /*
154    * Print the number in the direct_entry, but leave it blank for zero.
155    * (otherwise, if zero is printed as '0', the GtkEntry behaves wierdly).
156    */
157   if (number > 0) {
158     snprintf (buf, sizeof (buf), "%d", number);
159   } else {
160     buf[0] = '\0';
161   }
162
163   gtk_entry_set_text (GTK_ENTRY(direct_entry), buf);
164
165   g_object_set_data (G_OBJECT(chooser), "number",
166                        GINT_TO_POINTER(number));
167   g_signal_emit(G_OBJECT(chooser), sw_chooser_signals[NUMBER_CHANGED_SIGNAL], 0, number);
168
169   return number;
170 }
171
172 static int
173 chooser_set_number (GtkWidget * chooser, int number, sw_choice * choices)
174 {
175   GtkWidget * combo_entry;
176   int i;
177
178   combo_entry =
179     GTK_WIDGET (g_object_get_data (G_OBJECT(chooser), "combo_entry"));
180
181   for (i = 0; choices[i].name != NULL; i++) {
182     if (number == choices[i].number) {
183       gtk_entry_set_text (GTK_ENTRY(combo_entry), _(choices[i].name));
184       return number;
185     }
186   }
187
188   /* not in the entry -- assume first choice is "Custom" and set that */
189   gtk_entry_set_text (GTK_ENTRY(combo_entry), _(choices[0].name));
190
191   return chooser_set_number_direct (chooser, number);;
192 }
193
194 static void
195 chooser_combo_changed_cb (GtkWidget * widget, gpointer data)
196 {
197   GtkWidget * chooser = (GtkWidget *)data;
198   sw_choice * choices;
199   GtkWidget * direct_hbox;
200   const gchar * text;
201   int i, number = -1;
202
203   /* find what number the chosen menu string corresponds to */
204  
205   text = gtk_entry_get_text (GTK_ENTRY(widget));
206
207   choices = g_object_get_data (G_OBJECT(chooser), "choices");
208
209   for (i = 0; choices[i].name != NULL; i++) {
210     if (strcmp (text, _(choices[i].name)) == 0) {
211       number = choices[i].number;
212       break;
213     }
214   }
215
216   /* set the direct hbox sensitive if "Custom", else insensitive */
217
218   direct_hbox =
219     GTK_WIDGET(g_object_get_data (G_OBJECT(chooser), "direct_hbox"));
220
221   if (number == -1) {
222     gtk_widget_set_sensitive (direct_hbox, TRUE);
223   } else {
224     gtk_widget_set_sensitive (direct_hbox, FALSE);
225
226     /* change the direct_entry to reflect the new value */
227     chooser_set_number_direct (chooser, number);
228   }
229 }
230
231 static void
232 chooser_entry_changed_cb (GtkWidget * widget, gpointer data)
233 {
234   GtkWidget * chooser = (GtkWidget *)data;
235   GtkWidget * combo_entry;
236   sw_choice * choices;
237
238   const gchar * text;
239   int number = -1;
240
241   text = gtk_entry_get_text (GTK_ENTRY(widget));
242   number = atoi (text);
243
244   choices = g_object_get_data (G_OBJECT(chooser), "choices");
245
246   combo_entry =
247     GTK_WIDGET (g_object_get_data (G_OBJECT(chooser), "combo_entry"));
248
249         g_signal_handlers_block_matched (GTK_OBJECT(widget), G_SIGNAL_MATCH_DATA, 0,
250                                                                         0, 0, 0, chooser);
251         g_signal_handlers_block_matched (GTK_OBJECT(combo_entry), G_SIGNAL_MATCH_DATA, 0,
252                                                                         0, 0, 0, chooser);
253   chooser_set_number (chooser, number, choices);
254
255     g_signal_handlers_unblock_matched (GTK_OBJECT(combo_entry), G_SIGNAL_MATCH_DATA, 0,
256                                                                         0, 0, 0, chooser);
257     g_signal_handlers_unblock_matched (GTK_OBJECT(widget), G_SIGNAL_MATCH_DATA, 0,
258                                                                         0, 0, 0, chooser);
259 }
260
261 static void
262 sw_chooser_build (GtkWidget * chooser)
263 {
264   GtkWidget * frame;
265   GtkWidget * vbox; 
266   GList * choice_names = NULL;
267   GtkWidget * combo;
268   GtkWidget * hbox;
269   GtkWidget * combo_entry, * direct_entry;
270   GtkWidget * label;
271   sw_choice * choices;
272
273   int i;
274
275   frame = chooser;
276   gtk_frame_set_label (GTK_FRAME (frame), SW_CHOOSER(chooser)->title);
277
278   vbox = gtk_vbox_new (FALSE, 0);
279   gtk_container_add (GTK_CONTAINER(frame), vbox);
280   gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
281   gtk_widget_show (vbox);
282
283   choices = (sw_choice *) (SW_CHOOSER(chooser)->choices);
284
285   choice_names = NULL;
286   for (i = 0; choices[i].name != NULL; i++) {
287     choice_names = g_list_append (choice_names, _(choices[i].name));
288   }
289  
290   combo = gtk_combo_new ();
291   gtk_combo_set_popdown_strings (GTK_COMBO(combo), choice_names);
292   gtk_combo_set_value_in_list (GTK_COMBO(combo), TRUE, FALSE);
293
294   combo_entry = GTK_COMBO(combo)->entry;
295
296   gtk_editable_set_editable (GTK_EDITABLE(combo_entry), FALSE);
297
298   gtk_box_pack_start (GTK_BOX (vbox), combo, TRUE, FALSE, 4);
299   gtk_widget_show (combo);
300
301   hbox = gtk_hbox_new (FALSE, 0);
302   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 4);
303   gtk_widget_show (hbox);
304
305   label = gtk_label_new (_("Custom: "));
306   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
307   gtk_widget_show (label);
308
309   direct_entry = gtk_entry_new ();
310   gtk_box_pack_start (GTK_BOX (hbox), direct_entry, FALSE, FALSE, 4);
311   gtk_widget_show (direct_entry);
312
313   if (SW_CHOOSER(chooser)->units != NULL) {
314     label = gtk_label_new (SW_CHOOSER(chooser)->units);
315     gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
316     gtk_widget_show (label);
317   }
318
319   g_signal_connect (G_OBJECT(combo_entry), "changed",
320                       G_CALLBACK(chooser_combo_changed_cb), chooser);
321  
322   g_signal_connect (G_OBJECT(direct_entry), "changed",
323                       G_CALLBACK(chooser_entry_changed_cb), chooser);
324
325   g_object_set_data (G_OBJECT(chooser), "combo_entry", combo_entry);
326   g_object_set_data (G_OBJECT(chooser), "direct_entry", direct_entry);
327   g_object_set_data (G_OBJECT(chooser), "direct_hbox", hbox);
328   g_object_set_data (G_OBJECT(chooser), "choices", choices);
329
330   /* fake a change event to set the number data */
331   chooser_combo_changed_cb (combo_entry, chooser);
332 }
333
334
335 GtkWidget *
336 samplerate_chooser_new (gchar * title)
337 {
338   SWChooser * chooser = SW_CHOOSER (g_object_new (sw_chooser_get_type (), NULL));
339
340   chooser->title = title ? title : _("Sampling rate");
341   chooser->choices = (gpointer)samplerate_choices;
342   chooser->units = _("Hz");
343
344   sw_chooser_build (GTK_WIDGET (chooser));
345
346   return GTK_WIDGET (chooser);
347 }
348
349 int
350 samplerate_chooser_get_rate (GtkWidget * chooser)
351 {
352   return chooser_get_number (chooser);
353 }
354
355 int
356 samplerate_chooser_set_rate (GtkWidget * chooser, int rate)
357 {
358   return chooser_set_number (chooser, rate, samplerate_choices);
359 }
360
361 GtkWidget *
362 channelcount_chooser_new (gchar * title)
363 {
364   SWChooser * chooser = SW_CHOOSER (g_object_new (sw_chooser_get_type (), NULL));
365
366   chooser->title = title ? title : _("Channels");
367   chooser->choices = channelcount_choices;
368   chooser->units = _("channels");
369
370   sw_chooser_build (GTK_WIDGET (chooser));
371
372   return GTK_WIDGET (chooser);
373 }
374
375 int
376 channelcount_chooser_get_count (GtkWidget * chooser)
377 {
378   return chooser_get_number (chooser);
379 }
380
381 int
382 channelcount_chooser_set_count (GtkWidget * chooser, int count)
383 {
384   return chooser_set_number (chooser, count, channelcount_choices);
385 }
Note: See TracBrowser for help on using the browser.