only for RuBoard - do not distribute or recompile | |
GtkDialog, GtkFileSelection, GtkColorSelectionDialog, and GtkFontSelectionDialog
These are comparable to the Windows Common Dialog Boxes. The GtkDialog widget is analogous to the start of a MessageBox form, and the rest are self-explanatory. The GtkFileSelection can also be used to select a directory. Figure 3.4 shows Listing 3.4 compiled and running. When you click the various buttons in that program, you access the dialog boxes represented in Figures 3.5, 3.6, and 3.7. The dialog box in Figure 3.5 is instantiated by the File Selection button in Figure 3.4, Figure 3.6 is instantiated by the Color Selection button, and Figure 3.7 is instantiated by the Change My Font button. Figures 3.5, 3.6, and 3.7 are placed in Listing 3.4 near the relevant code.
Figure 3.4. Selection widgets demo.
Figure 3.5. The File Selection Dialog widget.
Figure 3.6. The Color Selection widget.
Figure 3.7. The Font Selection widget.
Listing 3.4 Demo of the Dialog Widgets: GtkDialog, GtkFileSelection, GtkColorSelectionDialog, and GtkFontSelectionDialog
#include <gtk/gtk.h> GtkWidget *frm_dialogs; GtkWidget *MsgBox; GtkWidget *frm_quit; GtkWidget *frm_file_sel; GtkWidget *entry_filename; GtkWidget *draw_area; GtkWidget *frm_color_sel; GtkWidget *frm_font_sel; GtkWidget *cmd_font_sel; GtkWidget *lbl_font_sel; static GdkFont *font_sel; void destroy_main(); void cmd_msgbox_clicked(); void cmd_advanced_msgbox_clicked(); void cmd_OK_clicked(); void cmd_save_clicked(); void cmd_quit_without_saving_clicked(); void cmd_file_sel_clicked(); void frm_file_sel_ok_clicked(); void frm_file_sel_cancel_clicked(); void cmd_color_sel_clicked(); void frm_color_sel_ok_clicked(); void frm_color_sel_cancel_clicked(); void cmd_font_sel_clicked(); void frm_font_sel_ok_clicked(); void frm_font_sel_cancel_clicked(); gint main(gint argc, gchar *argv[]) { GtkWidget *vbox_main; GtkWidget *cmd_msgbox; GtkWidget *cmd_advanced_msgbox; GtkWidget *cmd_file_sel; GtkWidget *lbl_file_sel; GtkWidget *cmd_color_sel; gtk_init(&argc, &argv); frm_dialogs = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(frm_dialogs), "Dialogs Demo"); gtk_signal_connect(GTK_OBJECT(frm_dialogs), "destroy", GTK_SIGNAL_FUNC(destroy_main), NULL); vbox_main = gtk_vbox_new(TRUE, 2); cmd_msgbox = gtk_button_new_with_label("MsgBox Emulation"); gtk_signal_connect(GTK_OBJECT(cmd_msgbox), "clicked", GTK_SIGNAL_FUNC(cmd_msgbox_clicked), NULL); cmd_advanced_msgbox = gtk_button_new_with_label("Advanced MsgBox"); gtk_signal_connect(GTK_OBJECT(cmd_advanced_msgbox), "clicked", GTK_SIGNAL_FUNC(cmd_advanced_msgbox_clicked), NULL); cmd_file_sel = gtk_button_new_with_label("File Selection"); gtk_signal_connect(GTK_OBJECT(cmd_file_sel), "clicked", GTK_SIGNAL_FUNC(cmd_file_sel_clicked), NULL); lbl_file_sel = gtk_label_new("File Selected is:"); entry_filename = gtk_entry_new(); cmd_color_sel = gtk_button_new_with_label("Color Selection"); gtk_signal_connect(GTK_OBJECT(cmd_color_sel), "clicked", GTK_SIGNAL_FUNC(cmd_color_sel_clicked), NULL); draw_area = gtk_drawing_area_new(); /* In the following command button, you will change the * font of the button. If the label of the command * button is a separate object, you can poke the style * directly into the label instead of going * through the button widget. */ lbl_font_sel = gtk_label_new("Change My Font"); cmd_font_sel = gtk_button_new(); gtk_container_add(GTK_CONTAINER(cmd_font_sel), lbl_font_sel); gtk_signal_connect(GTK_OBJECT(cmd_font_sel), "clicked", GTK_SIGNAL_FUNC(cmd_font_sel_clicked), NULL); gtk_box_pack_start(GTK_BOX(vbox_main), cmd_msgbox, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox_main), cmd_advanced_msgbox, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox_main), cmd_file_sel, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox_main), lbl_file_sel, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox_main), entry_filename, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox_main), cmd_color_sel, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox_main), draw_area, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox_main), cmd_font_sel, TRUE, TRUE, 0); gtk_container_add(GTK_CONTAINER(frm_dialogs), vbox_main); gtk_widget_show_all (frm_dialogs); gtk_main (); return 0; } void destroy_main() { gtk_main_quit(); } void cmd_msgbox_clicked() { GtkWidget *lbl_message; GtkWidget *cmd_OK; MsgBox = gtk_dialog_new(); gtk_window_set_title(GTK_WINDOW(MsgBox), "Message Box"); lbl_message = gtk_label_new(" This is the error message."); cmd_OK = gtk_button_new_with_label("OK"); gtk_signal_connect(GTK_OBJECT(cmd_OK), "clicked", GTK_SIGNAL_FUNC(cmd_OK_clicked), NULL); gtk_box_pack_start(GTK_BOX(GTK_DIALOG(MsgBox)->vbox), lbl_message, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(GTK_DIALOG(MsgBox)->action_area), cmd_OK, TRUE, TRUE, 0); gtk_widget_show_all(MsgBox); } void cmd_advanced_msgbox_clicked() { GtkWidget *lbl_do_you_want_to; GtkWidget *cmd_save, *cmd_quit_without_saving; GtkWidget *chk_save_as_XML; GtkWidget *vbox; frm_quit = gtk_dialog_new(); gtk_window_set_title(GTK_WINDOW(frm_quit), "Changes Not Saved"); vbox = gtk_vbox_new(TRUE, 0); lbl_do_you_want_to = gtk_label_new(" Do you want to..."); cmd_save = gtk_button_new_with_label("Save Changes?"); gtk_signal_connect(GTK_OBJECT(cmd_save), "clicked", GTK_SIGNAL_FUNC(cmd_save_clicked), NULL); cmd_quit_without_saving = gtk_button_new_with_label("Quit without\nSaving Changes?"); gtk_signal_connect(GTK_OBJECT(cmd_quit_without_saving), "clicked", GTK_SIGNAL_FUNC(cmd_quit_without_saving_clicked), NULL); chk_save_as_XML = gtk_check_button_new_with_label("Save as XML"); gtk_box_pack_start(GTK_BOX(GTK_DIALOG(frm_quit)->vbox), lbl_do_you_want_to, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), cmd_save, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), chk_save_as_XML, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), cmd_quit_without_saving, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(GTK_DIALOG(frm_quit)->action_area), vbox, TRUE, TRUE, 0); gtk_widget_show_all(frm_quit); } void cmd_OK_clicked() { gtk_widget_destroy(GTK_WIDGET(MsgBox)); } void cmd_save_clicked() { gtk_widget_destroy(GTK_WIDGET(frm_quit)); } void cmd_quit_without_saving_clicked() { gtk_widget_destroy(GTK_WIDGET(frm_quit)); } /* Figure 3.5 is inserted here because the code that is * relevant immediately follows. However, please keep in mind * that the code following the figure is a continuation of the * previous code listing.*/ void cmd_file_sel_clicked() { frm_file_sel = gtk_file_selection_new("Demo File Selection"); gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(frm_file_sel)->ok_button), "clicked", GTK_SIGNAL_FUNC(frm_file_sel_ok_clicked), NULL); gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION (frm_file_sel)->cancel_button), "clicked", GTK_SIGNAL_FUNC(frm_file_sel_cancel_clicked), NULL); gtk_widget_show(frm_file_sel); } void frm_file_sel_ok_clicked() { gtk_entry_set_text(GTK_ENTRY(entry_filename), gtk_file_selection_get_filename(GTK_FILE_SELECTION(frm_file_sel))); gtk_widget_destroy(frm_file_sel); } void frm_file_sel_cancel_clicked() { gtk_widget_destroy(frm_file_sel); /* If you just wanted to hide the widget instead of destroying it, * you would use gtk_widget_hide(frm_file_sel); * and the next time you showed the widget, it would be as you * last left it. */ } /* Again, Figure 3.6 is placed here because it is relevant to the * code that follows; this is still Listing 3.4 .*/ void cmd_color_sel_clicked() { frm_color_sel = gtk_color_selection_dialog_new("Demo Color Selection"); gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG (frm_color_sel)->ok_button), "clicked", GTK_SIGNAL_FUNC(frm_color_sel_ok_clicked), NULL); gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG (frm_color_sel)->cancel_button), "clicked", GTK_SIGNAL_FUNC(frm_color_sel_cancel_clicked), NULL); gtk_widget_show(frm_color_sel); } void frm_color_sel_ok_clicked() { gdouble rgb[3]; GdkColor gdk_color; GdkColormap *map; /* Note that there is a difference between GTK_COLOR_SELECTION and * GTK_COLOR_SELECTION_DIALOG . The former inherits from the GtkVBox * widget and is an orphan widget. The latter is the normal way to * create a color selection dialog box and inherits from the GtkDialog * widget. The ok_button, cancel_button, and help_button are only * available from GTK_COLOR_SELECTION_DIALOG . */ gtk_color_selection_get_color(GTK_COLOR_SELECTION( GTK_COLOR_SELECTION_DIALOG (frm_color_sel)->colorsel), rgb); /* Initialize the colormap structure. */ map = gdk_window_get_colormap(draw_area->window); gdk_color.red = (guint16)(rgb[0]*65535.0); gdk_color.green = (guint16)(rgb[1]*65535.0); gdk_color.blue = (guint16)(rgb[2]*65535.0); gdk_color_alloc (map, &gdk_color); gdk_window_set_background(draw_area->window, &gdk_color); /* The following command acts as a "refresh". */ gdk_window_clear(draw_area->window); gtk_widget_destroy(frm_color_sel); } void frm_color_sel_cancel_clicked() { gtk_widget_destroy(frm_color_sel); } /* This is the last figure that goes with Listing 3.4 . Figure 3.7 * is called by the "Change My Font" button at the bottom of the * vertical packing box (as shown in Figure 3.4 ).*/ void cmd_font_sel_clicked() { frm_font_sel = gtk_font_selection_dialog_new("Demo Font Selection"); gtk_signal_connect(GTK_OBJECT(GTK_FONT_SELECTION_DIALOG (frm_font_sel)->ok_button), "clicked", GTK_SIGNAL_FUNC(frm_font_sel_ok_clicked), NULL); gtk_signal_connect(GTK_OBJECT(GTK_FONT_SELECTION_DIALOG (frm_font_sel)->cancel_button), "clicked", GTK_SIGNAL_FUNC(frm_font_sel_cancel_clicked), NULL); gtk_widget_show(frm_font_sel); } void frm_font_sel_ok_clicked() { GtkStyle *style_sel; font_sel = gdk_font_load(gtk_font_selection_dialog_get_font_name( GTK_FONT_SELECTION_DIALOG(frm_font_sel))); style_sel = gtk_style_copy(gtk_widget_get_style(GTK_WIDGET(cmd_font_sel))); style_sel->font = font_sel; /* In the following call, the font of the lbl_font_sel label * widget is set, which is the label for cmd_font_sel . For the following * call to work, it has to be sent a label; sending cmd_font_sel * in place of lbl_font_sel will not work. */ gtk_widget_set_style(GTK_WIDGET(lbl_font_sel), style_sel); gtk_widget_destroy(frm_font_sel); } void frm_font_sel_cancel_clicked() { gtk_widget_destroy(frm_font_sel); }
only for RuBoard - do not distribute or recompile | |