/* gmysql -- a graphical frontend to MySQL databases Copyright (C) 1998, 1999 Stephen R. Dodd This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include "createdbdlg.h" static void create_db_dlg_done( GtkWidget *widget, gpointer data ); static void create_db_dlg_cancel( GtkWidget *widget, gpointer data ); static void create_db_dlg_destroyed( GtkWidget *widget, gpointer data ); static gint create_db_dlg_key_pressed( GtkWidget *widget, GdkEventKey *event ); static void populate_host_list( gpointer key, gpointer value, gpointer user_data ); static void host_selected( GtkWidget *widget, gpointer data ); static GSList *group; static gchar *selected_host; static int preselect_index; static int select_index; static GtkWidget *opt_menu; CreateDbDlg *create_db_dlg_new( TableList *owner, gchar *host ) { CreateDbDlg *create_db_dlg; GtkWidget *table, *label, *entry, *button, *dialog, *menu; create_db_dlg = g_new( CreateDbDlg, 1 ); create_db_dlg->owner = owner; dialog = create_db_dlg->dialog = gtk_dialog_new(); gtk_signal_connect( GTK_OBJECT( dialog ), "destroy", GTK_SIGNAL_FUNC( create_db_dlg_destroyed ), create_db_dlg ); gtk_signal_connect( GTK_OBJECT( dialog ), "key_press_event", GTK_SIGNAL_FUNC( create_db_dlg_key_pressed ), NULL ); table = gtk_table_new( 2, 2, FALSE ); label = gtk_label_new( "Host" ); gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_RIGHT ); gtk_misc_set_alignment( GTK_MISC( label ), 1.0, 0.5 ); opt_menu = entry = create_db_dlg->host = gtk_option_menu_new(); menu = gtk_menu_new(); group = NULL; selected_host = host; preselect_index = 0; select_index = 0; g_hash_table_foreach( owner->conns, populate_host_list, menu ); gtk_option_menu_set_menu( GTK_OPTION_MENU( entry ), menu ); gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 0, 1, GTK_FILL, 0, 5, 5 ); gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 0, 1, GTK_FILL|GTK_EXPAND, 0, 5, 5 ); gtk_option_menu_set_history( GTK_OPTION_MENU( entry ), select_index ); gtk_widget_show( label ); gtk_widget_show( entry ); label = gtk_label_new( "Database" ); gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_RIGHT ); gtk_misc_set_alignment( GTK_MISC( label ), 1.0, 0.5 ); entry = create_db_dlg->db = gtk_entry_new(); gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 1, 2, GTK_FILL, 0, 5, 5 ); gtk_table_attach( GTK_TABLE( table ), entry, 1, 2, 1, 2, GTK_FILL|GTK_EXPAND, 0, 5, 5 ); gtk_widget_show( label ); gtk_widget_show( entry ); gtk_box_pack_start( GTK_BOX( GTK_DIALOG( dialog )->vbox ), table, FALSE, TRUE, 0 ); gtk_widget_show( table ); gtk_widget_grab_focus( create_db_dlg->db ); /* must come after table is shown? */ gtk_box_set_homogeneous( GTK_BOX( GTK_DIALOG( dialog )->action_area ), FALSE ); button = gtk_button_new_with_label( "Cancel" ); gtk_signal_connect( GTK_OBJECT( button ), "clicked", GTK_SIGNAL_FUNC( create_db_dlg_cancel ), create_db_dlg ); GTK_WIDGET_SET_FLAGS( button, GTK_CAN_DEFAULT ); gtk_box_pack_end( GTK_BOX( GTK_DIALOG( dialog )->action_area ), button, FALSE, FALSE, 0 ); gtk_widget_show( button ); button = gtk_button_new_with_label( "Create" ); gtk_signal_connect( GTK_OBJECT( button ), "clicked", GTK_SIGNAL_FUNC( create_db_dlg_done ), create_db_dlg ); GTK_WIDGET_SET_FLAGS( button, GTK_CAN_DEFAULT ); gtk_box_pack_end( GTK_BOX( GTK_DIALOG( dialog )->action_area ), button, FALSE, FALSE, 0 ); gtk_widget_grab_default( button ); /* must do after packing but before showing */ #if !(CONFIG_GTK_MAJOR==1 && CONFIG_GTK_MINOR==0) gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE ); #endif /* not GTK 1.0 */ gtk_widget_show( button ); return create_db_dlg; } void create_db_dlg_show( CreateDbDlg *create_db_dlg ) { gtk_widget_show( create_db_dlg->dialog ); } void create_db_dlg_done( GtkWidget *widget, gpointer data ) { CreateDbDlg *create_db_dlg; gchar *db; create_db_dlg = CREATE_DB_DLG(data); g_assert( create_db_dlg ); db = gtk_entry_get_text( GTK_ENTRY( create_db_dlg->db ) ); gtk_widget_hide( create_db_dlg->dialog ); while( gtk_events_pending()) gtk_main_iteration(); table_list_create_db( create_db_dlg->owner, selected_host, db ); gtk_widget_destroy( create_db_dlg->dialog ); } void create_db_dlg_set_host( CreateDbDlg *create_db_dlg, gchar *host ) { /* for( int i = 0 gtk_option_menu_set_history( GTK_OPTION_MENU( create_db_dlg->host ) */ // so I'm lazy - I want an easy way to select a string item in an // option menu... } void create_db_dlg_cancel( GtkWidget *widget, gpointer data ) { CreateDbDlg *create_db_dlg; create_db_dlg = CREATE_DB_DLG(data); gtk_widget_destroy( create_db_dlg->dialog ); } gint create_db_dlg_key_pressed( GtkWidget *widget, GdkEventKey *event ) { if( event->keyval == GDK_Escape ) gtk_widget_destroy( widget ); return FALSE; } void create_db_dlg_destroyed( GtkWidget *widget, gpointer data ) { g_free( data ); } void populate_host_list( gpointer key, gpointer value, gpointer user_data ) { DBConn *db_conn; GtkWidget *menu, *item; db_conn = (DBConn *)value; menu = GTK_WIDGET( user_data ); if( !selected_host ) selected_host = db_conn->host; item = gtk_radio_menu_item_new_with_label( group, db_conn->host ); group = gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( item ) ); gtk_signal_connect( GTK_OBJECT( item ), "activate", GTK_SIGNAL_FUNC( host_selected ), db_conn->host ); gtk_widget_show( item ); gtk_menu_append( GTK_MENU( menu ), item ); if( !strcmp( selected_host, db_conn->host ) ) { selected_host = db_conn->host; select_index = preselect_index; g_print( "Want %u\n", select_index ); } preselect_index++; } void host_selected( GtkWidget *widget, gpointer data ) { selected_host = data; }