diff options
author | Jan Lipovský <janlipovsky@gmail.com> | 2010-12-29 17:56:04 +0100 |
---|---|---|
committer | Jan Lipovský <janlipovsky@gmail.com> | 2010-12-29 17:56:04 +0100 |
commit | 385f6377ca4f0be4cf4a5b493e8d13995534b5be (patch) | |
tree | d0155c60b8e10f8161b84398496d3fac25e5d47c /gncpmount.c | |
download | gncpmount-385f6377ca4f0be4cf4a5b493e8d13995534b5be.tar.gz gncpmount-385f6377ca4f0be4cf4a5b493e8d13995534b5be.tar.xz gncpmount-385f6377ca4f0be4cf4a5b493e8d13995534b5be.zip |
GUI with Options dialog
Diffstat (limited to 'gncpmount.c')
-rw-r--r-- | gncpmount.c | 312 |
1 files changed, 312 insertions, 0 deletions
diff --git a/gncpmount.c b/gncpmount.c new file mode 100644 index 0000000..2a61dae --- /dev/null +++ b/gncpmount.c @@ -0,0 +1,312 @@ +#include <stdlib.h> +#include <gtk/gtk.h> + +#include "dialogs.h" +#include "globals.h" + +#define WIDTH 400 +#define HEIGHT -1 + + + + +/** +* Show info, error or warning message +*/ +void +show_message (GtkMessageType type, const gchar *format, gchar *msgtxt, const gchar *format_sec, gchar *msgtxt_sec) +{ + GtkWidget *dialog; + + dialog = gtk_message_dialog_new ( GTK_WINDOW (gui.win), + GTK_DIALOG_DESTROY_WITH_PARENT, + type, + GTK_BUTTONS_OK, + format, + msgtxt); + + gtk_message_dialog_format_secondary_text ( GTK_MESSAGE_DIALOG (dialog), + format_sec, + msgtxt_sec); + gtk_dialog_run ( GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); +} + + +/** +* Create Open folder dialog and let user select folder +* for mount point +*/ +static void +open_folder (GtkWidget *wid, GtkWidget *win) +{ + GtkWidget *dialog; + dialog = gtk_file_chooser_dialog_new ("Select folder - mount point", + GTK_WINDOW (gui.win), + GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, + NULL); + if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) + { + char *folder; + folder = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); + gtk_entry_set_text (GTK_ENTRY (gui.entry_mount_point), folder); + g_free (folder); + } + gtk_widget_destroy (dialog); + +} + + + +static void +passwd_usage (GtkWidget *wid, GtkWidget *win) +{ + cmd_params.n = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gui.check_n)); + + gtk_entry_set_editable (GTK_ENTRY(gui.entry_password), !cmd_params.n); + + if(cmd_params.n) + { + gtk_entry_set_visibility (GTK_ENTRY(gui.entry_password), TRUE); + set_parameter_text (gui.entry_password, &(cmd_params.password)); + gtk_entry_set_text (GTK_ENTRY(gui.entry_password), "no password will be used"); + } + else + { + gtk_entry_set_visibility (GTK_ENTRY(gui.entry_password), FALSE); + if(cmd_params.password != NULL) + gtk_entry_set_text (GTK_ENTRY(gui.entry_password), cmd_params.password); + } +} + + + +static void +run_options_dialog ( GtkWidget *w, gpointer data ) +{ + options_dialog(&gui); +} + + +static void +set_gui_from_cmdparams () +{ + + if(cmd_params.username != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_username), cmd_params.username); + + if(cmd_params.password != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_password), cmd_params.password); + + if(cmd_params.server != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_server), cmd_params.server); + + if(cmd_params.dns_name != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_dns_server), cmd_params.dns_name); + + if(cmd_params.mount_point != NULL) + gtk_entry_set_text(GTK_ENTRY(gui.entry_mount_point), cmd_params.mount_point); + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gui.check_n), cmd_params.n); +} + + + + +/* Menu, an array of GtkItemFactoryEntry structures that defines each menu item */ +static GtkItemFactoryEntry menu_items[] = { + { "/_File", NULL, NULL, 0, "<Branch>" }, + { "/File/_New", "<control>N", NULL, 0, "<StockItem>", GTK_STOCK_NEW }, + { "/File/_Load", "<control>L", NULL, 0, "<StockItem>", GTK_STOCK_OPEN }, + { "/File/_Save", "<control>S", NULL, 0, "<StockItem>", GTK_STOCK_SAVE }, + { "/File/sep1", NULL, NULL, 0, "<Separator>" }, + { "/File/_Quit", "<CTRL>Q", gtk_main_quit, 0, "<StockItem>", GTK_STOCK_QUIT }, + { "/Tools", NULL, NULL, 0, "<Branch>" }, + { "/_Tools/Options", "<control>O", run_options_dialog, 0, "<Item>" }, + { "/_Help", NULL, NULL, 0, "<Branch>" }, + { "/_Help/About", NULL, NULL, 0, "<Item>" }, +}; + + +static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]); + + +/* Returns a menubar widget made from the above menu */ +static GtkWidget * +get_menubar_menu( GtkWidget *window ) +{ + GtkItemFactory *item_factory; + GtkAccelGroup *accel_group; + + /* Make an accelerator group (shortcut keys) */ + accel_group = gtk_accel_group_new (); + + /* Make an ItemFactory (that makes a menubar) */ + item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", + accel_group); + + /* This function generates the menu items. Pass the item factory, + the number of items in the array, the array itself, and any + callback data for the the menu items. */ + gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL); + + /* Attach the new accelerator group to the window. */ + gtk_window_add_accel_group (GTK_WINDOW (window), accel_group); + + /* Finally, return the actual menu bar created by the item factory. */ + return gtk_item_factory_get_widget (item_factory, "<main>"); +} + + + +int main (int argc, char *argv[]) +{ + GtkWidget *label = NULL; + GtkWidget *tmp = NULL; + GtkWidget *hbox = NULL; + + GtkWidget *halign = NULL; + + GtkWidget *logo = NULL; + + /* Initialize GTK+ */ + g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL); + gtk_init (&argc, &argv); + g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL); + + /* Create the main window */ + gui.win = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (gui.win), GUI_TITLE ); + gtk_window_set_position (GTK_WINDOW (gui.win), GTK_WIN_POS_CENTER); + gtk_window_set_default_size (GTK_WINDOW (gui.win), WIDTH, HEIGHT); + gtk_window_set_resizable (GTK_WINDOW (gui.win), FALSE); + + gtk_widget_realize (gui.win); + g_signal_connect (gui.win, "destroy", gtk_main_quit, NULL); + + + tmp = gtk_vbox_new (FALSE, 1); + gtk_container_add (GTK_CONTAINER (gui.win), tmp); + + gui.menu = get_menubar_menu (gui.win); + gtk_box_pack_start (GTK_BOX (tmp), gui.menu, FALSE, TRUE, 0); + + /* Create a vertical box */ + gui.vbox = gtk_vbox_new (FALSE, 6); + gtk_box_pack_start (GTK_BOX (tmp), gui.vbox, FALSE, TRUE, 0); + + gtk_container_set_border_width (GTK_CONTAINER (gui.vbox), 10); + + logo = gtk_image_new_from_file("gncpmount.png"); + gtk_box_pack_start (GTK_BOX (gui.vbox), logo, TRUE, TRUE, 0); + + /* Create a notebook */ + gui.notebook = gtk_notebook_new (); + gtk_notebook_set_tab_pos (GTK_NOTEBOOK (gui.notebook), GTK_POS_TOP); + gtk_box_pack_start (GTK_BOX (gui.vbox), gui.notebook, TRUE, TRUE, 0); + gtk_widget_set_size_request(gui.notebook, WIDTH,-1); + + + /* Page Login */ + + /* Create table */ + gui.table = gtk_table_new (3, 2, FALSE); + label = gtk_label_new ("Login"); + gtk_notebook_prepend_page (GTK_NOTEBOOK (gui.notebook), gui.table, label); + + /* Labels */ + halign = gtk_alignment_new(0, 0, 0, 1); + + label = gtk_label_new ("Username [-U]:"); + gtk_container_add(GTK_CONTAINER(halign), label); + gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 0, 1, GTK_FILL , GTK_FILL | GTK_EXPAND, 4, 0); + + halign = gtk_alignment_new(0, 0, 0, 1); + label = gtk_label_new ("Password [-P]:"); + gtk_container_add(GTK_CONTAINER(halign), label); + gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND, 4, 0); + + /* Chcekbox */ + gui.check_n = gtk_check_button_new_with_label ("Do not use any password [-n]"); + g_signal_connect (G_OBJECT (gui.check_n), "toggled", G_CALLBACK (passwd_usage), NULL); + gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.check_n, 1, 2, 2, 3); + + + /* Entry */ + gui.entry_username = gtk_entry_new (); + gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.entry_username, 1, 2, 0, 1); + + gui.entry_password = gtk_entry_new (); + gtk_entry_set_visibility (GTK_ENTRY(gui.entry_password), FALSE); + gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.entry_password, 1, 2, 1, 2); + + + + /* Page SERVER */ + gui.table = gtk_table_new (4, 2, FALSE); + + label = gtk_label_new ("Server"); + gtk_notebook_append_page (GTK_NOTEBOOK (gui.notebook), gui.table, label); + + halign = gtk_alignment_new(0, 0, 0, 1); + label = gtk_label_new ("Mount point:"); + gtk_container_add(GTK_CONTAINER(halign), label); + gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 0, 1, GTK_FILL , GTK_FILL | GTK_EXPAND, 6, 0); + + hbox = gtk_hbox_new (FALSE, 2); + gtk_table_attach_defaults (GTK_TABLE (gui.table), hbox, 1, 2, 0, 1); + gui.entry_mount_point = gtk_entry_new (); + gtk_container_add(GTK_CONTAINER(hbox), gui.entry_mount_point); + + gui.btn_browse = gtk_button_new_with_label ("Browse"); + g_signal_connect (gui.btn_browse, "clicked", G_CALLBACK (open_folder), NULL); + gtk_container_add(GTK_CONTAINER(hbox), gui.btn_browse); + + tmp = gtk_hseparator_new(); + gtk_table_attach (GTK_TABLE(gui.table), tmp, 0, 2, 1, 2, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 6, 6); + + + halign = gtk_alignment_new(0, 0, 0, 1); + label = gtk_label_new ("Server [-S]:"); + gtk_container_add(GTK_CONTAINER(halign), label); + gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 2, 3, GTK_FILL, GTK_FILL | GTK_EXPAND, 6, 0); + + gui.entry_server = gtk_entry_new (); + gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.entry_server, 1, 2, 2, 3); + + + halign = gtk_alignment_new(0, 0, 0, 1); + label = gtk_label_new ("DNS name [-A]:"); + gtk_container_add(GTK_CONTAINER(halign), label); + gtk_table_attach(GTK_TABLE(gui.table), halign, 0, 1, 3, 4, GTK_FILL, GTK_FILL | GTK_EXPAND, 6, 0); + + gui.entry_dns_server = gtk_entry_new (); + gtk_table_attach_defaults (GTK_TABLE (gui.table), gui.entry_dns_server, 1, 2, 3, 4); + + + + /* Button box */ + gui.btnbox = gtk_hbutton_box_new(); + gtk_hbutton_box_set_layout_default(GTK_BUTTONBOX_END); + gtk_box_pack_start (GTK_BOX (gui.vbox), gui.btnbox, TRUE, TRUE, 0); + + /* Buttons */ + gui.btn_mount = gtk_button_new_with_label ("Mount"); + g_signal_connect (gui.btn_mount, "clicked", gtk_main_quit, NULL); + gtk_box_pack_start (GTK_BOX (gui.btnbox), gui.btn_mount, TRUE, TRUE, 0); + + gui.btn_close = gtk_button_new_from_stock (GTK_STOCK_CLOSE); + g_signal_connect (gui.btn_close, "clicked", gtk_main_quit, NULL); + gtk_box_pack_start (GTK_BOX (gui.btnbox), gui.btn_close, TRUE, TRUE, 0); + + + /* Fill in informations from cmd_params to wiidgets */ + set_gui_from_cmdparams(); + + /* Enter the main loop */ + gtk_widget_show_all (gui.win); + gtk_main (); + return 0; +} |