diff options
author | Alexey Yakovenko <wakeroid@gmail.com> | 2010-01-29 21:12:56 +0100 |
---|---|---|
committer | Alexey Yakovenko <wakeroid@gmail.com> | 2010-01-29 21:12:56 +0100 |
commit | 6be45e99048df4e618f25f26c1b45386fce3e281 (patch) | |
tree | 290d46e35af4f09425e2d765aa20c474e97ed2c8 /plugins/gtkui | |
parent | a9a782b0ca8d93d419b2530cd6bb368244342433 (diff) |
better plugin configuration dialogs
Diffstat (limited to 'plugins/gtkui')
-rw-r--r-- | plugins/gtkui/Makefile.am | 1 | ||||
-rw-r--r-- | plugins/gtkui/callbacks.c | 181 | ||||
-rw-r--r-- | plugins/gtkui/gtkui.h | 25 | ||||
-rw-r--r-- | plugins/gtkui/pluginconf.c | 242 |
4 files changed, 268 insertions, 181 deletions
diff --git a/plugins/gtkui/Makefile.am b/plugins/gtkui/Makefile.am index 504984e0..aaf9c56a 100644 --- a/plugins/gtkui/Makefile.am +++ b/plugins/gtkui/Makefile.am @@ -8,6 +8,7 @@ gtkui_la_SOURCES = gtkui.c gtkui.h\ progress.c progress.h\ search.c search.h\ fileman.c\ + pluginconf.c\ parser.c parser.h gtkui_la_LDFLAGS = -module diff --git a/plugins/gtkui/callbacks.c b/plugins/gtkui/callbacks.c index 25c47408..48fe25c3 100644 --- a/plugins/gtkui/callbacks.c +++ b/plugins/gtkui/callbacks.c @@ -2033,187 +2033,6 @@ on_addlocation_key_press_event (GtkWidget *widget, return FALSE; } - -void -on_prop_entry_changed(GtkEditable *editable, gpointer user_data) { - const char *key = g_object_get_data (G_OBJECT (editable), "key"); - if (key) { - deadbeef->conf_set_str (key, gtk_entry_get_text (GTK_ENTRY (editable))); - deadbeef->sendmessage (M_CONFIGCHANGED, 0, 0, 0); - } -} - -void -on_prop_checkbox_clicked (GtkButton *button, gpointer user_data) { - const char *key = g_object_get_data (G_OBJECT (button), "key"); - if (key) { - deadbeef->conf_set_int (key, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button))); - deadbeef->sendmessage (M_CONFIGCHANGED, 0, 0, 0); - } -} - -void -on_prop_browse_file (GtkButton *button, gpointer user_data) { - GtkWidget *dlg = gtk_file_chooser_dialog_new ("Open file...", GTK_WINDOW (mainwin), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); - - gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dlg), FALSE); - // restore folder - gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dlg), deadbeef->conf_get_str ("filechooser.lastdir", "")); - int response = gtk_dialog_run (GTK_DIALOG (dlg)); - // store folder - gchar *folder = gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (dlg)); - if (folder) { - deadbeef->conf_set_str ("filechooser.lastdir", folder); - g_free (folder); - deadbeef->sendmessage (M_CONFIGCHANGED, 0, 0, 0); - } - if (response == GTK_RESPONSE_OK) { - gchar *file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlg)); - gtk_widget_destroy (dlg); - if (file) { - GtkWidget *entry = GTK_WIDGET (user_data); - gtk_entry_set_text (GTK_ENTRY (entry), file); - g_free (file); - } - } - else { - gtk_widget_destroy (dlg); - } -} - -gboolean -on_plug_prefwin_key_press_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data) { - if (event->keyval == GDK_Escape) { - gtk_widget_destroy (widget); - } - return FALSE; -} - -void -plugin_configure (GtkWidget *parentwin, DB_plugin_t *p) { - // create window - GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL); - gtk_container_set_border_width (GTK_CONTAINER(win), 12); -// gtk_widget_set_events (win, GDK_KEY_PRESS_MASK); - gtk_widget_set_size_request (win, 300, -1); - g_signal_connect ((gpointer) win, "key_press_event", G_CALLBACK (on_plug_prefwin_key_press_event), NULL); - char title[200]; - snprintf (title, sizeof (title), "Setup %s", p->name); - gtk_window_set_title (GTK_WINDOW (win), title); - gtk_window_set_modal (GTK_WINDOW (win), TRUE); - gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parentwin)); - GtkWidget *vbox; - vbox = gtk_vbox_new (FALSE, 8); - gtk_widget_show (vbox); - gtk_container_add (GTK_CONTAINER (win), vbox); -// GtkWidget *tbl; -// tbl = gtk_table_new (1, 2, FALSE); -// gtk_container_set_border_width (GTK_CONTAINER (tbl), 3); -// gtk_table_set_col_spacings (GTK_TABLE (tbl), 3); -// gtk_container_add (GTK_CONTAINER (win), tbl); - - int nrows = 0; - // parse script - char token[MAX_TOKEN]; - const char *script = p->configdialog; - parser_line = 1; - while (script = gettoken (script, token)) { - if (strcmp (token, "property")) { - fprintf (stderr, "invalid token while loading plugin %s config dialog: %s at line %d\n", p->name, token, parser_line); - break; - } - char labeltext[MAX_TOKEN]; - script = gettoken_warn_eof (script, labeltext); - if (!script) { - break; - } - char type[MAX_TOKEN]; - script = gettoken_warn_eof (script, type); - if (!script) { - break; - } - char key[MAX_TOKEN]; - script = gettoken_warn_eof (script, key); - if (!script) { - break; - } - char def[MAX_TOKEN]; - script = gettoken_warn_eof (script, def); - if (!script) { - break; - } - script = gettoken_warn_eof (script, token); - if (!script) { - break; - } - if (strcmp (token, ";")) { - fprintf (stderr, "expected `;' while loading plugin %s config dialog: %s at line %d\n", p->name, token, parser_line); - break; - } - - // add to dialog - nrows++; - //gtk_table_resize (GTK_TABLE (tbl), nrows, 2); - GtkWidget *label = NULL; - GtkWidget *prop = NULL; - if (!strcmp (type, "entry") || !strcmp (type, "password")) { - label = gtk_label_new (labeltext); - prop = gtk_entry_new (); - gtk_entry_set_text (GTK_ENTRY (prop), deadbeef->conf_get_str (key, def)); - g_signal_connect ((gpointer) prop, "changed", - G_CALLBACK (on_prop_entry_changed), - NULL); - } - else if (!strcmp (type, "checkbox")) { - prop = gtk_check_button_new_with_label (labeltext); - int val = deadbeef->conf_get_int (key, atoi (def)); - gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (prop), val); - g_signal_connect ((gpointer) prop, "clicked", - G_CALLBACK (on_prop_checkbox_clicked), - NULL); - } - else if (!strcmp (type, "file")) { - GtkWidget *cont = NULL; - label = gtk_label_new (labeltext); - cont = gtk_hbox_new (FALSE, 2); - prop = gtk_entry_new (); - gtk_editable_set_editable (GTK_EDITABLE (prop), FALSE); - g_signal_connect ((gpointer) prop, "changed", - G_CALLBACK (on_prop_entry_changed), - NULL); - gtk_entry_set_text (GTK_ENTRY (prop), deadbeef->conf_get_str (key, def)); - gtk_box_pack_start (GTK_BOX (cont), prop, TRUE, TRUE, 0); - GtkWidget *btn = gtk_button_new_with_label ("…"); - gtk_box_pack_start (GTK_BOX (cont), btn, FALSE, FALSE, 0); - g_signal_connect (G_OBJECT (btn), "clicked", G_CALLBACK (on_prop_browse_file), prop); - } - if (!strcmp (type, "password")) { - gtk_entry_set_visibility (GTK_ENTRY (prop), FALSE); - } - GtkWidget *cont = NULL; - if (label && prop) { - GtkWidget *hbox = NULL; - hbox = gtk_hbox_new (FALSE, 8); - gtk_widget_show (hbox); - gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); - gtk_box_pack_start (GTK_BOX (hbox), prop, TRUE, TRUE, 0); - cont = hbox; - } - else { - cont = prop; - } - if (prop) { - char *keydup = strdup (key); - g_object_set_data_full (G_OBJECT (prop), "key", keydup, (GDestroyNotify)free); - } - if (cont) { - gtk_box_pack_start (GTK_BOX (vbox), cont, FALSE, FALSE, 0); - } - } - - gtk_widget_show_all (win); -} - void on_configure_plugin_clicked (GtkButton *button, gpointer user_data) diff --git a/plugins/gtkui/gtkui.h b/plugins/gtkui/gtkui.h index ae88ecff..7737c253 100644 --- a/plugins/gtkui/gtkui.h +++ b/plugins/gtkui/gtkui.h @@ -1,3 +1,21 @@ +/* + DeaDBeeF - ultimate music player for GNU/Linux systems with X11 + Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net> + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ #ifndef __GTKUI_H #define __GTKUI_H @@ -5,6 +23,8 @@ extern DB_functions_t *deadbeef; struct _GSList; +// misc utility functions + void gtkui_add_dirs (struct _GSList *lst); @@ -17,4 +37,9 @@ gtkui_open_files (struct _GSList *lst); void gtkui_receive_fm_drop (char *mem, int length, int drop_y); +// plugin configuration dialogs + +void +plugin_configure (GtkWidget *parentwin, DB_plugin_t *p); + #endif diff --git a/plugins/gtkui/pluginconf.c b/plugins/gtkui/pluginconf.c new file mode 100644 index 00000000..057afe18 --- /dev/null +++ b/plugins/gtkui/pluginconf.c @@ -0,0 +1,242 @@ +/* + DeaDBeeF - ultimate music player for GNU/Linux systems with X11 + Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net> + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <gtk/gtk.h> +#include <gdk/gdkkeysyms.h> +#include "../../deadbeef.h" +#include "gtkui.h" +#include "parser.h" +#include "support.h" + +#define trace(...) { fprintf (stderr, __VA_ARGS__); } +//#define trace(fmt,...) + +extern GtkWidget *mainwin; + +void +on_prop_browse_file (GtkButton *button, gpointer user_data) { + GtkWidget *dlg = gtk_file_chooser_dialog_new ("Open file...", GTK_WINDOW (mainwin), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); + + gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dlg), FALSE); + // restore folder + gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dlg), deadbeef->conf_get_str ("filechooser.lastdir", "")); + int response = gtk_dialog_run (GTK_DIALOG (dlg)); + // store folder + gchar *folder = gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (dlg)); + if (folder) { + deadbeef->conf_set_str ("filechooser.lastdir", folder); + g_free (folder); + deadbeef->sendmessage (M_CONFIGCHANGED, 0, 0, 0); + } + if (response == GTK_RESPONSE_OK) { + gchar *file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlg)); + gtk_widget_destroy (dlg); + if (file) { + GtkWidget *entry = GTK_WIDGET (user_data); + gtk_entry_set_text (GTK_ENTRY (entry), file); + g_free (file); + } + } + else { + gtk_widget_destroy (dlg); + } +} + +static void apply_conf (GtkWidget *w, DB_plugin_t *p) { + // parse script + char token[MAX_TOKEN]; + const char *script = p->configdialog; + parser_line = 1; + while (script = gettoken (script, token)) { + if (strcmp (token, "property")) { + fprintf (stderr, "invalid token while loading plugin %s config dialog: %s at line %d\n", p->name, token, parser_line); + break; + } + char labeltext[MAX_TOKEN]; + script = gettoken_warn_eof (script, labeltext); + if (!script) { + break; + } + char type[MAX_TOKEN]; + script = gettoken_warn_eof (script, type); + if (!script) { + break; + } + char key[MAX_TOKEN]; + script = gettoken_warn_eof (script, key); + if (!script) { + break; + } + char def[MAX_TOKEN]; + script = gettoken_warn_eof (script, def); + if (!script) { + break; + } + script = gettoken_warn_eof (script, token); + if (!script) { + break; + } + if (strcmp (token, ";")) { + fprintf (stderr, "expected `;' while loading plugin %s config dialog: %s at line %d\n", p->name, token, parser_line); + break; + } + + // fetch data + GtkWidget *widget = lookup_widget (w, key); + if (widget) { + if (!strcmp (type, "entry") || !strcmp (type, "password") || !strcmp (type, "file")) { + deadbeef->conf_set_str (key, gtk_entry_get_text (GTK_ENTRY (widget))); + } + else if (!strcmp (type, "checkbox")) { + deadbeef->conf_set_int (key, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))); + } + } + } + deadbeef->sendmessage (M_CONFIGCHANGED, 0, 0, 0); +} + +void +plugin_configure (GtkWidget *parentwin, DB_plugin_t *p) { + // create window + char title[200]; + snprintf (title, sizeof (title), "Setup %s", p->name); + GtkWidget *win = gtk_dialog_new_with_buttons (title, GTK_WINDOW (parentwin), GTK_DIALOG_MODAL, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, GTK_STOCK_APPLY, GTK_RESPONSE_APPLY, NULL); + gtk_window_set_type_hint (GTK_WINDOW (win), GDK_WINDOW_TYPE_HINT_DIALOG); + gtk_container_set_border_width (GTK_CONTAINER(win), 12); + + gtk_widget_set_size_request (win, 300, -1); + gtk_window_set_title (GTK_WINDOW (win), title); + gtk_window_set_modal (GTK_WINDOW (win), TRUE); + gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parentwin)); + GtkWidget *vbox; + vbox = GTK_DIALOG (win)->vbox; + GtkWidget *action_area = GTK_DIALOG (win)->action_area; + gtk_widget_show (action_area); + gtk_button_box_set_layout (GTK_BUTTON_BOX (action_area), GTK_BUTTONBOX_END); + + + // parse script + char token[MAX_TOKEN]; + const char *script = p->configdialog; + parser_line = 1; + while (script = gettoken (script, token)) { + if (strcmp (token, "property")) { + fprintf (stderr, "invalid token while loading plugin %s config dialog: %s at line %d\n", p->name, token, parser_line); + break; + } + char labeltext[MAX_TOKEN]; + script = gettoken_warn_eof (script, labeltext); + if (!script) { + break; + } + char type[MAX_TOKEN]; + script = gettoken_warn_eof (script, type); + if (!script) { + break; + } + char key[MAX_TOKEN]; + script = gettoken_warn_eof (script, key); + if (!script) { + break; + } + char def[MAX_TOKEN]; + script = gettoken_warn_eof (script, def); + if (!script) { + break; + } + script = gettoken_warn_eof (script, token); + if (!script) { + break; + } + if (strcmp (token, ";")) { + fprintf (stderr, "expected `;' while loading plugin %s config dialog: %s at line %d\n", p->name, token, parser_line); + break; + } + + // add to dialog + GtkWidget *label = NULL; + GtkWidget *prop = NULL; + GtkWidget *cont = NULL; + if (!strcmp (type, "entry") || !strcmp (type, "password")) { + label = gtk_label_new (labeltext); + gtk_widget_show (label); + prop = gtk_entry_new (); + gtk_widget_show (prop); + gtk_entry_set_text (GTK_ENTRY (prop), deadbeef->conf_get_str (key, def)); + } + else if (!strcmp (type, "checkbox")) { + prop = gtk_check_button_new_with_label (labeltext); + gtk_widget_show (prop); + int val = deadbeef->conf_get_int (key, atoi (def)); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (prop), val); + } + else if (!strcmp (type, "file")) { + label = gtk_label_new (labeltext); + gtk_widget_show (label); + cont = gtk_hbox_new (FALSE, 2); + gtk_widget_show (cont); + prop = gtk_entry_new (); + gtk_widget_show (prop); + gtk_editable_set_editable (GTK_EDITABLE (prop), FALSE); + gtk_entry_set_text (GTK_ENTRY (prop), deadbeef->conf_get_str (key, def)); + gtk_box_pack_start (GTK_BOX (cont), prop, TRUE, TRUE, 0); + GtkWidget *btn = gtk_button_new_with_label ("…"); + gtk_widget_show (btn); + gtk_box_pack_start (GTK_BOX (cont), btn, FALSE, FALSE, 0); + g_signal_connect (G_OBJECT (btn), "clicked", G_CALLBACK (on_prop_browse_file), prop); + } + if (!strcmp (type, "password")) { + gtk_entry_set_visibility (GTK_ENTRY (prop), FALSE); + } + if (label && prop) { + GtkWidget *hbox = NULL; + hbox = gtk_hbox_new (FALSE, 8); + gtk_widget_show (hbox); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (hbox), cont ? cont : prop, TRUE, TRUE, 0); + cont = hbox; + } + else { + cont = prop; + } + if (prop) { + g_object_set_data (G_OBJECT (win), key, prop); + } + if (cont) { + gtk_box_pack_start (GTK_BOX (vbox), cont, FALSE, FALSE, 0); + } + } + + int response; + do { + response = gtk_dialog_run (GTK_DIALOG (win)); + if (response == GTK_RESPONSE_APPLY || response == GTK_RESPONSE_OK) { + apply_conf (win, p); + } + } while (response == GTK_RESPONSE_APPLY); + gtk_widget_destroy (win); +} + + |