* ./src/interface/main.c: tidied the help output code.
[vlc.git] / plugins / gtk / gtk_preferences.c
1 /*****************************************************************************
2  * gtk_preferences.c: functions to handle the preferences dialog box.
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: gtk_preferences.c,v 1.26 2002/04/23 14:16:20 sam Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Loïc Minier <lool@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble: Our main job is to build a nice interface from the modules config
27  *   structure. Once this is done, we need to track each change made by the
28  *   user to the data contained in this interface so that when/if he decides to
29  *   apply his changes we can quickly commit them into the modules config
30  *   structure. (for this last task we use a GHashTable to accumulate the
31  *   changes. To commit them, we then just have to circle through it )
32  *
33  *****************************************************************************/
34 #include <sys/types.h>                                              /* off_t */
35 #include <stdlib.h>
36
37 #include <videolan/vlc.h>
38
39 #ifdef MODULE_NAME_IS_gnome
40 #   include <gnome.h>
41 #else
42 #   include <gtk/gtk.h>
43 #endif
44
45 #include <string.h>
46
47 #include "interface.h"
48
49 #include "gtk_support.h"
50 #include "gtk_common.h"
51 #include "gtk_preferences.h"
52
53 /* local functions */
54 static void GtkCreateConfigDialog( char *, intf_thread_t * );
55
56 static void GtkConfigOk          ( GtkButton *, gpointer );
57 static void GtkConfigApply       ( GtkButton *, gpointer );
58 static void GtkConfigCancel      ( GtkButton *, gpointer );
59 static void GtkConfigSave        ( GtkButton *, gpointer );
60
61 static void GtkConfigDialogDestroyed ( GtkObject *, gpointer );
62
63 static void GtkStringChanged     ( GtkEditable *, gpointer );
64 static void GtkIntChanged        ( GtkEditable *, gpointer );
65 static void GtkFloatChanged      ( GtkEditable *, gpointer );
66 static void GtkBoolChanged       ( GtkToggleButton *, gpointer );
67
68 static void GtkFreeHashTable     ( gpointer );
69 static void GtkFreeHashValue     ( gpointer, gpointer, gpointer );
70 static void GtkSaveHashValue     ( gpointer, gpointer, gpointer );
71
72 static void GtkModuleConfigure   ( GtkButton *, gpointer );
73 static void GtkModuleSelected    ( GtkButton *, gpointer );
74 static void GtkModuleHighlighted ( GtkCList *, int, int, GdkEventButton *,
75                                    gpointer );
76
77 /****************************************************************************
78  * Callback for menuitems: display configuration interface window
79  ****************************************************************************/
80 void GtkPreferencesActivate( GtkMenuItem * menuitem, gpointer user_data )
81 {
82     intf_thread_t * p_intf;
83
84     p_intf = GetIntf( GTK_WIDGET(menuitem), (char*)user_data );
85
86     GtkCreateConfigDialog( "main", p_intf );
87 }
88
89 /****************************************************************************
90  * GtkCreateConfigDialog: dynamically creates the configuration dialog
91  * box from all the configuration data provided by the selected module.
92  ****************************************************************************/
93
94 /* create a new tooltipped area */
95 #define TOOLTIP( text )                                                   \
96     /* create an event box to catch some events */                        \
97     item_event_box = gtk_event_box_new();                                 \
98     /* add a tooltip on mouseover */                                      \
99     gtk_tooltips_set_tip( p_intf->p_sys->p_tooltips,                      \
100                           item_event_box, text, "" );                     \
101     gtk_container_set_border_width( GTK_CONTAINER(item_event_box), 4 );
102
103 /* draws a right aligned label in side of a widget */
104 #define LABEL_AND_WIDGET( label_text, widget, tooltip )                   \
105     gtk_table_resize( GTK_TABLE(category_table), ++rows, 2 );             \
106     item_align = gtk_alignment_new( 1, .5, 0, 0 );                        \
107     item_label = gtk_label_new( label_text );                             \
108     gtk_container_add( GTK_CONTAINER(item_align), item_label );           \
109     gtk_table_attach_defaults( GTK_TABLE(category_table), item_align,     \
110                                0, 1, rows - 1, rows );                    \
111     item_align = gtk_alignment_new( 0, .5, .5, 0 );                       \
112     gtk_container_add( GTK_CONTAINER(item_align), widget );               \
113     TOOLTIP(tooltip)                                                      \
114     gtk_container_add( GTK_CONTAINER(item_event_box), item_align );       \
115     gtk_table_attach_defaults( GTK_TABLE(category_table), item_event_box, \
116                                1, 2, rows - 1, rows );
117
118 static void GtkCreateConfigDialog( char *psz_module_name,
119                                    intf_thread_t *p_intf )
120 {
121     module_t *p_module, *p_module_bis;
122     module_config_t *p_item;
123
124     guint rows = 0;
125
126     GHashTable *config_hash_table;
127
128     GtkWidget *item_event_box;
129
130     GtkWidget *config_dialog;
131     GtkWidget *config_dialog_vbox;
132     GtkWidget *config_notebook;
133
134     GtkWidget *category_table = NULL;
135     GtkWidget *category_label = NULL;
136
137 #ifndef MODULE_NAME_IS_gnome
138     GtkWidget *dialog_action_area;
139 #endif
140
141     GtkWidget *ok_button;
142     GtkWidget *apply_button;
143     GtkWidget *save_button;
144     GtkWidget *cancel_button;
145
146     GtkWidget *item_align;
147     GtkWidget *item_frame;
148     GtkWidget *item_hbox;
149     GtkWidget *item_label;
150     GtkWidget *item_vbox;
151     GtkWidget *string_entry;
152     GtkWidget *integer_spinbutton;
153     GtkWidget *float_spinbutton;
154     GtkObject *item_adj;
155     GtkWidget *bool_checkbutton;
156     GtkWidget *module_clist;
157     GtkWidget *module_config_button;
158     GtkWidget *module_select_button;
159
160     gint category_max_height;
161
162     /* Check if the dialog box is already opened, if so this will save us
163      * quite a bit of work. (the interface will be destroyed when you actually
164      * close the dialog window, but remember that it is only hidden if you
165      * clicked on the action buttons). This trick also allows us not to
166      * duplicate identical dialog windows. */
167
168     config_dialog = (GtkWidget *)gtk_object_get_data(
169                     GTK_OBJECT(p_intf->p_sys->p_window), psz_module_name );
170     if( config_dialog )
171     {
172         /* Yeah it was open */
173         gtk_widget_show( config_dialog );
174         gtk_widget_grab_focus( config_dialog );
175         return;
176     }
177
178
179     /* Look for the selected module */
180     for( p_module = p_module_bank->first ; p_module != NULL ;
181          p_module = p_module->next )
182     {
183
184         if( psz_module_name && !strcmp( psz_module_name, p_module->psz_name ) )
185             break;
186     }
187     if( !p_module ) return;
188
189     /* We found it, now we can start building its configuration interface */
190     /* Create the configuration dialog box */
191
192 #ifdef MODULE_NAME_IS_gnome
193     config_dialog = gnome_dialog_new( p_module->psz_longname, NULL );
194     config_dialog_vbox = GNOME_DIALOG(config_dialog)->vbox;
195 #else
196     config_dialog = gtk_dialog_new();
197     gtk_window_set_title( GTK_WINDOW(config_dialog), p_module->psz_longname );
198     config_dialog_vbox = GTK_DIALOG(config_dialog)->vbox;
199 #endif
200
201     category_max_height = config_GetIntVariable( MODULE_STRING "-prefs-maxh" );
202
203     gtk_window_set_policy( GTK_WINDOW(config_dialog), TRUE, TRUE, FALSE );
204     gtk_container_set_border_width( GTK_CONTAINER(config_dialog_vbox), 0 );
205
206     /* Create our config hash table and associate it with the dialog box */
207     config_hash_table = g_hash_table_new( NULL, NULL );
208     gtk_object_set_data_full( GTK_OBJECT(config_dialog),
209                               "config_hash_table", config_hash_table,
210                               (GtkDestroyNotify)GtkFreeHashTable );
211
212     /* Create notebook */
213     config_notebook = gtk_notebook_new();
214     gtk_notebook_set_scrollable( GTK_NOTEBOOK(config_notebook), TRUE );
215     gtk_container_add( GTK_CONTAINER(config_dialog_vbox), config_notebook );
216
217     /* Enumerate config options and add corresponding config boxes */
218     p_item = p_module->p_config;
219     do
220     {
221         switch( p_item->i_type )
222         {
223
224         case MODULE_CONFIG_HINT_CATEGORY:
225         case MODULE_CONFIG_HINT_END:
226
227             /*
228              * Before we start building the interface for the new category, we
229              * must close/finish the previous one we were generating.
230              */
231             if( category_table )
232             {
233                 GtkWidget *_scrolled_window;
234                 GtkWidget *_viewport;
235                 GtkWidget *_vbox;
236                 GtkRequisition _requisition;
237
238                 /* create a vbox to deal with EXPAND/FILL issues in the
239                  * notebook page, and pack it with the previously generated
240                  * category_table */
241                 _vbox = gtk_vbox_new( FALSE, 0 );
242                 gtk_container_set_border_width( GTK_CONTAINER(_vbox), 4 );
243                 gtk_box_pack_start( GTK_BOX(_vbox), category_table,
244                                     FALSE, FALSE, 0 );
245
246                 /* create a new scrolled window that will contain all of the
247                  * above. */
248                 _scrolled_window = gtk_scrolled_window_new( NULL, NULL );
249                 gtk_scrolled_window_set_policy(
250                     GTK_SCROLLED_WINDOW(_scrolled_window), GTK_POLICY_NEVER,
251                     GTK_POLICY_AUTOMATIC );
252                 /* add scrolled window as a notebook page */
253                 gtk_notebook_append_page( GTK_NOTEBOOK(config_notebook),
254                                           _scrolled_window, category_label );
255                 /* pack the vbox into the scrolled window */
256                 _viewport = gtk_viewport_new( NULL, NULL );
257                 gtk_viewport_set_shadow_type( GTK_VIEWPORT(_viewport),
258                                               GTK_SHADOW_NONE );
259                 gtk_container_add( GTK_CONTAINER(_viewport), _vbox );
260                 gtk_container_add( GTK_CONTAINER(_scrolled_window),
261                                    _viewport );
262
263                 /* set the size of the scrolled window to the size of the
264                  * child widget */
265                 gtk_widget_show_all( _vbox );
266                 gtk_widget_size_request( _vbox, &_requisition );
267                 if( _requisition.height > category_max_height )
268                     gtk_widget_set_usize( _scrolled_window, -1,
269                                           category_max_height );
270                 else
271                     gtk_widget_set_usize( _scrolled_window, -1,
272                                           _requisition.height );
273
274             }
275
276             /*
277              * Now we can start taking care of the new category
278              */
279
280             if( p_item->i_type == MODULE_CONFIG_HINT_CATEGORY )
281             {
282                 /* create a new table for right-left alignment of children */
283                 category_table = gtk_table_new( 0, 0, FALSE );
284                 gtk_table_set_col_spacings( GTK_TABLE(category_table), 4 );
285                 rows = 0;
286
287                 /* create a new category label */
288                 category_label = gtk_label_new( p_item->psz_text );
289             }
290
291             break;
292
293         case MODULE_CONFIG_ITEM_MODULE:
294
295             item_frame = gtk_frame_new( p_item->psz_text );
296
297             gtk_table_resize( GTK_TABLE(category_table), ++rows, 2 );
298             gtk_table_attach_defaults( GTK_TABLE(category_table), item_frame,
299                                        0, 2, rows - 1, rows );
300
301             item_vbox = gtk_vbox_new( FALSE, 4 );
302             gtk_container_add( GTK_CONTAINER(item_frame), item_vbox );
303
304             /* create a new clist widget */
305             {
306                 gchar * titles[] = { _("Name"), _("Description") };
307
308                 module_clist =
309                     gtk_clist_new_with_titles( 2, titles );
310             }
311             gtk_clist_column_titles_passive( GTK_CLIST(module_clist) );
312             gtk_clist_set_selection_mode( GTK_CLIST(module_clist),
313                                           GTK_SELECTION_SINGLE);
314             gtk_container_add( GTK_CONTAINER(item_vbox), module_clist );
315
316             /* build a list of available modules */
317             {
318                 gchar * entry[2];
319
320                 for( p_module_bis = p_module_bank->first ;
321                      p_module_bis != NULL ;
322                      p_module_bis = p_module_bis->next )
323                 {
324                     if( p_module_bis->i_capabilities & (1 << p_item->i_value) )
325                     {
326                         entry[0] = p_module_bis->psz_name;
327                         entry[1] = p_module_bis->psz_longname;
328                         gtk_clist_append( GTK_CLIST(module_clist), entry );
329                     }
330                 }
331             }
332
333             gtk_clist_set_column_auto_resize( GTK_CLIST(module_clist),
334                                               0, TRUE );
335             gtk_clist_set_column_auto_resize( GTK_CLIST(module_clist),
336                                               1, TRUE );
337
338             /* connect signals to the modules list */
339             gtk_signal_connect( GTK_OBJECT(module_clist), "select_row",
340                                 GTK_SIGNAL_FUNC(GtkModuleHighlighted),
341                                 NULL );
342
343             /* hbox holding the "select" and "configure" buttons */
344             item_hbox = gtk_hbox_new( FALSE, 4 );
345             gtk_container_add( GTK_CONTAINER(item_vbox), item_hbox);
346
347             /* add configure button */
348             module_config_button =
349                 gtk_button_new_with_label( _("Configure") );
350             gtk_widget_set_sensitive( module_config_button, FALSE );
351             gtk_container_add( GTK_CONTAINER(item_hbox),
352                                module_config_button );
353             gtk_object_set_data( GTK_OBJECT(module_config_button),
354                                  "p_intf", p_intf );
355             gtk_object_set_data( GTK_OBJECT(module_clist),
356                                  "config_button", module_config_button );
357
358             /* add select button */
359             module_select_button =
360                 gtk_button_new_with_label( _("Select") );
361             gtk_container_add( GTK_CONTAINER(item_hbox),
362                                module_select_button );
363             /* add a tooltip on mouseover */
364             gtk_tooltips_set_tip( p_intf->p_sys->p_tooltips,
365                                   module_select_button,
366                                   p_item->psz_longtext, "" );
367
368             /* hbox holding the "selected" label and text input */
369             item_hbox = gtk_hbox_new( FALSE, 4 );
370             gtk_container_add( GTK_CONTAINER(item_vbox), item_hbox);
371             /* add new label */
372             item_label = gtk_label_new( _("Selected:") );
373             gtk_container_add( GTK_CONTAINER(item_hbox), item_label );
374
375             /* add input box with default value */
376             string_entry = gtk_entry_new();
377             gtk_object_set_data( GTK_OBJECT(module_clist),
378                                  "module_entry", string_entry );
379             gtk_container_add( GTK_CONTAINER(item_hbox), string_entry );
380             vlc_mutex_lock( p_item->p_lock );
381             gtk_entry_set_text( GTK_ENTRY(string_entry),
382                                 p_item->psz_value ? p_item->psz_value : "" );
383             vlc_mutex_unlock( p_item->p_lock );
384             /* add a tooltip on mouseover */
385             gtk_tooltips_set_tip( p_intf->p_sys->p_tooltips,
386                                   string_entry, p_item->psz_longtext, "" );
387
388             /* connect signals to the buttons */
389             gtk_signal_connect( GTK_OBJECT(module_config_button), "clicked",
390                                 GTK_SIGNAL_FUNC(GtkModuleConfigure),
391                                 (gpointer)module_clist );
392             gtk_signal_connect( GTK_OBJECT(module_select_button), "clicked",
393                                 GTK_SIGNAL_FUNC(GtkModuleSelected),
394                                 (gpointer)module_clist );
395
396             /* connect signal to track changes in the text box */
397             gtk_object_set_data( GTK_OBJECT(string_entry), "config_option",
398                                  p_item->psz_name );
399             gtk_signal_connect( GTK_OBJECT(string_entry), "changed",
400                                 GTK_SIGNAL_FUNC(GtkStringChanged),
401                                 (gpointer)config_dialog );
402             break;
403
404         case MODULE_CONFIG_ITEM_STRING:
405         case MODULE_CONFIG_ITEM_FILE:
406
407             /* add input box with default value */
408             string_entry = gtk_entry_new();
409             vlc_mutex_lock( p_item->p_lock );
410             gtk_entry_set_text( GTK_ENTRY(string_entry),
411                                 p_item->psz_value ? p_item->psz_value : "" );
412             vlc_mutex_unlock( p_item->p_lock );
413
414             /* connect signal to track changes in the text box */
415             gtk_object_set_data( GTK_OBJECT(string_entry), "config_option",
416                                  p_item->psz_name );
417             gtk_signal_connect( GTK_OBJECT(string_entry), "changed",
418                                 GTK_SIGNAL_FUNC(GtkStringChanged),
419                                 (gpointer)config_dialog );
420
421             LABEL_AND_WIDGET( p_item->psz_text,
422                               string_entry, p_item->psz_longtext );
423             break;
424
425         case MODULE_CONFIG_ITEM_INTEGER:
426
427             /* add input box with default value */
428             item_adj = gtk_adjustment_new( p_item->i_value,
429                                            -1, 99999, 1, 10, 10 );
430             integer_spinbutton = gtk_spin_button_new( GTK_ADJUSTMENT(item_adj),
431                                                       1, 0 );
432
433             /* connect signal to track changes in the spinbutton value */
434             gtk_object_set_data( GTK_OBJECT(integer_spinbutton),
435                                  "config_option", p_item->psz_name );
436             gtk_signal_connect( GTK_OBJECT(integer_spinbutton), "changed",
437                                 GTK_SIGNAL_FUNC(GtkIntChanged),
438                                 (gpointer)config_dialog );
439
440             LABEL_AND_WIDGET( p_item->psz_text,
441                               integer_spinbutton, p_item->psz_longtext );
442             break;
443
444         case MODULE_CONFIG_ITEM_FLOAT:
445
446             /* add input box with default value */
447             item_adj = gtk_adjustment_new( p_item->f_value,
448                                            0, 99999, 0.01, 10, 10 );
449             float_spinbutton = gtk_spin_button_new( GTK_ADJUSTMENT(item_adj),
450                                                     0.01, 2 );
451
452             /* connect signal to track changes in the spinbutton value */
453             gtk_object_set_data( GTK_OBJECT(float_spinbutton),
454                                  "config_option", p_item->psz_name );
455             gtk_signal_connect( GTK_OBJECT(float_spinbutton), "changed",
456                                 GTK_SIGNAL_FUNC(GtkFloatChanged),
457                                 (gpointer)config_dialog );
458
459             LABEL_AND_WIDGET( p_item->psz_text,
460                               float_spinbutton, p_item->psz_longtext );
461             break;
462
463         case MODULE_CONFIG_ITEM_BOOL:
464
465             /* add check button */
466             bool_checkbutton = gtk_check_button_new();
467             gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(bool_checkbutton),
468                                           p_item->i_value );
469
470             /* connect signal to track changes in the button state */
471             gtk_object_set_data( GTK_OBJECT(bool_checkbutton), "config_option",
472                                  p_item->psz_name );
473             gtk_signal_connect( GTK_OBJECT(bool_checkbutton), "toggled",
474                                 GTK_SIGNAL_FUNC(GtkBoolChanged),
475                                 (gpointer)config_dialog );
476
477             LABEL_AND_WIDGET( p_item->psz_text,
478                               bool_checkbutton, p_item->psz_longtext );
479             break;
480
481         }
482
483     }
484     while( p_item->i_type != MODULE_CONFIG_HINT_END && p_item++ );
485
486 #ifndef MODULE_NAME_IS_gnome
487     /* Now let's add the action buttons at the bottom of the page */
488     dialog_action_area = GTK_DIALOG(config_dialog)->action_area;
489     gtk_container_set_border_width( GTK_CONTAINER(dialog_action_area), 4 );
490
491     /* add a new table for the config option */
492     item_hbox = gtk_hbox_new( FALSE, 0 );
493     gtk_box_pack_end( GTK_BOX(dialog_action_area), item_hbox,
494                       TRUE, FALSE, 0 );
495     item_hbox = gtk_hbox_new( FALSE, 0 );
496     gtk_box_pack_end( GTK_BOX(dialog_action_area), item_hbox,
497                       TRUE, FALSE, 0 );
498 #endif
499
500     /* Create the OK button */
501 #ifdef MODULE_NAME_IS_gnome
502     gnome_dialog_append_button( GNOME_DIALOG(config_dialog),
503                                 GNOME_STOCK_BUTTON_OK );
504     ok_button =
505         GTK_WIDGET(g_list_last(GNOME_DIALOG(config_dialog)->buttons)->data);
506
507     gnome_dialog_append_button( GNOME_DIALOG(config_dialog),
508                                 GNOME_STOCK_BUTTON_APPLY );
509     apply_button =
510         GTK_WIDGET(g_list_last(GNOME_DIALOG(config_dialog)->buttons)->data);
511
512     gnome_dialog_append_button_with_pixmap(
513         GNOME_DIALOG(config_dialog), _("Save"), GNOME_STOCK_PIXMAP_SAVE );
514     save_button =
515         GTK_WIDGET(g_list_last(GNOME_DIALOG(config_dialog)->buttons)->data);
516
517     gnome_dialog_append_button( GNOME_DIALOG(config_dialog),
518                                 GNOME_STOCK_BUTTON_CANCEL );
519     cancel_button =
520         GTK_WIDGET(g_list_last(GNOME_DIALOG(config_dialog)->buttons)->data);
521 #else
522     ok_button = gtk_button_new_with_label( _("OK") );
523     gtk_box_pack_start( GTK_BOX(dialog_action_area), ok_button,
524                         TRUE, TRUE, 0 );
525
526     apply_button = gtk_button_new_with_label( _("Apply") );
527     gtk_box_pack_start( GTK_BOX(dialog_action_area), apply_button,
528                         TRUE, TRUE, 0 );
529
530     save_button = gtk_button_new_with_label( _("Save") );
531     gtk_box_pack_start( GTK_BOX(dialog_action_area), save_button,
532                         TRUE, TRUE, 0 );
533
534     cancel_button = gtk_button_new_with_label( _("Cancel") );
535     gtk_box_pack_start( GTK_BOX(dialog_action_area), cancel_button,
536                         TRUE, TRUE, 0 );
537 #endif
538
539     gtk_signal_connect( GTK_OBJECT(ok_button), "clicked",
540                         GTK_SIGNAL_FUNC(GtkConfigOk),
541                         config_dialog );
542     gtk_signal_connect( GTK_OBJECT(apply_button), "clicked",
543                         GTK_SIGNAL_FUNC(GtkConfigApply),
544                         config_dialog );
545     gtk_signal_connect( GTK_OBJECT(save_button), "clicked",
546                         GTK_SIGNAL_FUNC(GtkConfigSave),
547                         config_dialog );
548     gtk_signal_connect( GTK_OBJECT(cancel_button), "clicked",
549                         GTK_SIGNAL_FUNC(GtkConfigCancel),
550                         config_dialog );
551
552
553
554     /* Ok, job done successfully. Let's keep a reference to the dialog box */
555     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
556                          psz_module_name, config_dialog );
557     gtk_object_set_data( GTK_OBJECT(config_dialog), "psz_module_name",
558                          psz_module_name );
559
560     /* we want this ref to be destroyed if the object is destroyed */
561     gtk_signal_connect( GTK_OBJECT(config_dialog), "destroy",
562                        GTK_SIGNAL_FUNC(GtkConfigDialogDestroyed),
563                        (gpointer)p_intf );
564
565     gtk_widget_show_all( config_dialog );
566 }
567
568 #undef LABEL_AND_WIDGET
569 #undef TOOLTIP
570
571 /****************************************************************************
572  * GtkConfigApply: store the changes to the config inside the modules
573  * configuration structure
574  ****************************************************************************/
575 void GtkConfigApply( GtkButton * button, gpointer user_data )
576 {
577     GHashTable *hash_table;
578     hash_table = (GHashTable *)gtk_object_get_data( GTK_OBJECT(user_data),
579                                                     "config_hash_table" );
580     g_hash_table_foreach( hash_table, GtkSaveHashValue, NULL );
581
582 }
583
584 void GtkConfigOk( GtkButton * button, gpointer user_data )
585 {
586     GtkConfigApply( button, user_data );
587     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
588 }
589
590
591 void GtkConfigCancel( GtkButton * button, gpointer user_data )
592 {
593     gtk_widget_hide( gtk_widget_get_toplevel( GTK_WIDGET (button) ) );
594 }
595
596 void GtkConfigSave( GtkButton * button, gpointer user_data )
597 {
598     GtkConfigApply( button, user_data );
599     config_SaveConfigFile( NULL );
600 }
601
602 /****************************************************************************
603  * GtkModuleHighlighted: display module description when an entry is selected
604  *   in the clist, and activate the configure button if necessary.
605  ****************************************************************************/
606 void GtkModuleHighlighted( GtkCList *module_clist, int row, int column,
607                            GdkEventButton *event, gpointer user_data )
608 {
609     GtkWidget *config_button;
610     module_t *p_module;
611     char *psz_name;
612
613     if( gtk_clist_get_text( GTK_CLIST(module_clist), row, 0, &psz_name ) )
614     {
615         /* look for module 'psz_name' */
616         for( p_module = p_module_bank->first ;
617              p_module != NULL ;
618              p_module = p_module->next )
619         {
620           if( !strcmp( p_module->psz_name, psz_name ) )
621           {
622               gtk_object_set_data( GTK_OBJECT(module_clist),
623                                    "module_highlighted", p_module );
624               config_button = gtk_object_get_data( GTK_OBJECT(module_clist),
625                                                    "config_button" );
626               if( p_module->i_config_items )
627                   gtk_widget_set_sensitive( config_button, TRUE );
628               else
629                   gtk_widget_set_sensitive( config_button, FALSE );
630
631               break;
632           }
633         }
634
635     }
636 }
637
638 /****************************************************************************
639  * GtkModuleConfigure: display module configuration dialog box.
640  ****************************************************************************/
641 void GtkModuleConfigure( GtkButton *button, gpointer user_data )
642 {
643     module_t *p_module;
644     intf_thread_t *p_intf;
645
646     p_module = (module_t *)gtk_object_get_data( GTK_OBJECT(user_data),
647                                                 "module_highlighted" );
648
649     if( !p_module ) return;
650     p_intf = (intf_thread_t *)gtk_object_get_data( GTK_OBJECT(button),
651                                                    "p_intf" );
652     GtkCreateConfigDialog( p_module->psz_name, (gpointer)p_intf );
653
654 }
655
656 /****************************************************************************
657  * GtkModuleSelected: select module.
658  ****************************************************************************/
659 void GtkModuleSelected( GtkButton *button, gpointer user_data )
660 {
661     module_t *p_module;
662     GtkWidget *widget;
663
664     p_module = (module_t *)gtk_object_get_data( GTK_OBJECT(user_data),
665                                                 "module_highlighted" );
666     widget = (GtkWidget *)gtk_object_get_data( GTK_OBJECT(user_data),
667                                                "module_entry" );
668     if( !p_module ) return;
669
670     gtk_entry_set_text( GTK_ENTRY(widget), p_module->psz_name );
671
672 }
673
674 /****************************************************************************
675  * GtkStringChanged: signal called when the user changes a string value.
676  ****************************************************************************/
677 static void GtkStringChanged( GtkEditable *editable, gpointer user_data )
678 {
679     module_config_t *p_config;
680
681     GHashTable *hash_table;
682
683     hash_table = (GHashTable *)gtk_object_get_data( GTK_OBJECT(user_data),
684                                                     "config_hash_table" );
685     /* free old p_config */
686     p_config = (module_config_t *)g_hash_table_lookup( hash_table,
687                                                        (gpointer)editable );
688     if( p_config ) GtkFreeHashValue( NULL, (gpointer)p_config, NULL );
689
690     p_config = malloc( sizeof(module_config_t) );
691     p_config->i_type = MODULE_CONFIG_ITEM_STRING;
692     p_config->psz_value = gtk_editable_get_chars( editable, 0, -1 );
693     p_config->psz_name = (char *)gtk_object_get_data( GTK_OBJECT(editable),
694                                                       "config_option" );
695
696     g_hash_table_insert( hash_table, (gpointer)editable,
697                          (gpointer)p_config );
698 }
699
700 /****************************************************************************
701  * GtkIntChanged: signal called when the user changes an integer value.
702  ****************************************************************************/
703 static void GtkIntChanged( GtkEditable *editable, gpointer user_data )
704 {
705     module_config_t *p_config;
706
707     GHashTable *hash_table;
708
709     hash_table = (GHashTable *)gtk_object_get_data( GTK_OBJECT(user_data),
710                                                     "config_hash_table" );
711
712     /* free old p_config */
713     p_config = (module_config_t *)g_hash_table_lookup( hash_table,
714                                                        (gpointer)editable );
715     if( p_config ) GtkFreeHashValue( NULL, (gpointer)p_config, NULL );
716
717     p_config = malloc( sizeof(module_config_t) );
718     p_config->i_type = MODULE_CONFIG_ITEM_INTEGER;
719     p_config->i_value = gtk_spin_button_get_value_as_int(
720                             GTK_SPIN_BUTTON(editable) );
721     p_config->psz_name = (char *)gtk_object_get_data( GTK_OBJECT(editable),
722                                                       "config_option" );
723
724     g_hash_table_insert( hash_table, (gpointer)editable,
725                          (gpointer)p_config );
726 }
727
728 /****************************************************************************
729  * GtkFloatChanged: signal called when the user changes a float value.
730  ****************************************************************************/
731 static void GtkFloatChanged( GtkEditable *editable, gpointer user_data )
732 {
733     module_config_t *p_config;
734
735     GHashTable *hash_table;
736
737     hash_table = (GHashTable *)gtk_object_get_data( GTK_OBJECT(user_data),
738                                                     "config_hash_table" );
739
740     /* free old p_config */
741     p_config = (module_config_t *)g_hash_table_lookup( hash_table,
742                                                        (gpointer)editable );
743     if( p_config ) GtkFreeHashValue( NULL, (gpointer)p_config, NULL );
744
745     p_config = malloc( sizeof(module_config_t) );
746     p_config->i_type = MODULE_CONFIG_ITEM_FLOAT;
747     p_config->f_value = gtk_spin_button_get_value_as_float(
748                            GTK_SPIN_BUTTON(editable) );
749     p_config->psz_name = (char *)gtk_object_get_data( GTK_OBJECT(editable),
750                                                       "config_option" );
751
752     g_hash_table_insert( hash_table, (gpointer)editable,
753                          (gpointer)p_config );
754 }
755
756 /****************************************************************************
757  * GtkStringChanged: signal called when the user changes a bool value.
758  ****************************************************************************/
759 static void GtkBoolChanged( GtkToggleButton *button, gpointer user_data )
760 {
761     module_config_t *p_config;
762
763     GHashTable *hash_table;
764
765     hash_table = (GHashTable *)gtk_object_get_data( GTK_OBJECT(user_data),
766                                                     "config_hash_table" );
767
768     /* free old p_config */
769     p_config = (module_config_t *)g_hash_table_lookup( hash_table,
770                                                        (gpointer)button );
771     if( p_config ) GtkFreeHashValue( NULL, (gpointer)p_config, NULL );
772
773     p_config = malloc( sizeof(module_config_t) );
774     p_config->i_type = MODULE_CONFIG_ITEM_BOOL;
775     p_config->i_value = gtk_toggle_button_get_active( button );
776     p_config->psz_name = (char *)gtk_object_get_data( GTK_OBJECT(button),
777                                                       "config_option" );
778
779     g_hash_table_insert( hash_table, (gpointer)button,
780                          (gpointer)p_config );
781 }
782
783 /****************************************************************************
784  * GtkFreeHashTable: signal called when the config hash table is destroyed.
785  ****************************************************************************/
786 static void GtkFreeHashTable( gpointer user_data )
787 {
788     GHashTable *hash_table = (GHashTable *)user_data;
789
790     g_hash_table_foreach( hash_table, GtkFreeHashValue, NULL );
791     g_hash_table_destroy( hash_table );
792 }
793
794 /****************************************************************************
795  * GtkFreeHashValue: signal called when an element of the config hash table
796  * is destroyed.
797  ****************************************************************************/
798 static void GtkFreeHashValue( gpointer key, gpointer value, gpointer user_data)
799 {
800     module_config_t *p_config = (module_config_t *)value;
801
802     if( p_config->i_type == MODULE_CONFIG_ITEM_STRING )
803         g_free( p_config->psz_value );
804     free( p_config );
805 }
806
807 /****************************************************************************
808  * GtkSaveHashValue: callback used when enumerating the hash table in
809  * GtkConfigApply().
810  ****************************************************************************/
811 static void GtkSaveHashValue( gpointer key, gpointer value, gpointer user_data)
812 {
813     module_config_t *p_config = (module_config_t *)value;
814
815     switch( p_config->i_type )
816     {
817
818     case MODULE_CONFIG_ITEM_STRING:
819     case MODULE_CONFIG_ITEM_FILE:
820     case MODULE_CONFIG_ITEM_MODULE:
821         config_PutPszVariable( p_config->psz_name,
822             *p_config->psz_value? p_config->psz_value : NULL );
823         break;
824     case MODULE_CONFIG_ITEM_INTEGER:
825     case MODULE_CONFIG_ITEM_BOOL:
826         config_PutIntVariable( p_config->psz_name, p_config->i_value );
827         break;
828     case MODULE_CONFIG_ITEM_FLOAT:
829         config_PutFloatVariable( p_config->psz_name, p_config->f_value );
830         break;
831     }
832 }
833
834 /****************************************************************************
835  * GtkConfigDialogDestroyed: callback triggered when the config dialog box is
836  * destroyed.
837  ****************************************************************************/
838 static void GtkConfigDialogDestroyed( GtkObject *object, gpointer user_data )
839 {
840     intf_thread_t *p_intf = (intf_thread_t *)user_data;
841     char *psz_module_name;
842
843     psz_module_name = gtk_object_get_data( object, "psz_module_name" );
844
845     /* remove the ref to the dialog box */
846     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
847                          psz_module_name, NULL );
848
849 }