Linux Headquarters

I've installed Linux… now what???

GtkFileSelection – File Selection Dialog Box

Introduction
The GtkFileSelection widget allows for GTK+ programs that utilize it to have a consistent file dialog window. The standard GtkFileSelection widget can be extended and customized, but for the purpose of this example, we are using the standard file dialog box.
Once you have compiled and run the program, select File -> Open from the menu. The Open GtkMenuItem is assigned to a gtk_signal_connect that calls the create_file_selection function, which launches the file dialog box. Select a file and click OK. The OK button is assigned to a gtk_signal_connect that calls the store_filename function. This function grabs the file name from the file dialog box and writes it to the command line from which the program was launched from. In a real world program, there would also be another function to open the file and draw the contents to the text area, but this example does not actually open the file.

Source Code

/*
*File name: filedialog.c
*/

#include
#include

/*– Declare gloabal variables for the file dialog box and the filename –*/
GtkWidget *file_selection_box;
gchar *filename;

/*– This function allows the program to exit properly when the window is closed –*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
g_print (“Quitting…\n”);
gtk_main_quit();
return (FALSE);
}

void store_filename(GtkFileSelection *file_selection, gpointer data)
{
/*– Extract filename from the file selection dialog –*/
filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(file_selection_box));

/*– For the example, print the filename to the command line –*/
g_print (filename);
}

void create_file_selection(void)
{
/*– Create the selector widget –*/
file_selection_box = gtk_file_selection_new(“Please select a file for editing.”);

/*– Link the ok button to the store_filename function –*/
gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->ok_button),
“clicked”, GTK_SIGNAL_FUNC (store_filename), NULL);

/*– Destroy the dialog box when the user clicks the ok_button –*/
gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->ok_button),
“clicked”, GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) file_selection_box);

/*– Destroy the dialog box when the user clicks the cancel_button –*/
gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->cancel_button),
“clicked”, GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) file_selection_box);

/* Display the file dialog box */
gtk_widget_show (file_selection_box);
}

/*– This function allows the program to exit properly when the window is closed –*/
gint ClosingAppWindow (GtkWidget *widget, gpointer gdata)
{
g_print (“\nQuitting…\n”);
gtk_main_quit();
return (FALSE);
}

int main (int argc, char *argv[])
{
/*– Declare the GTK Widgets used in the program –*/
GtkWidget *window;
GtkWidget *menuFile;
GtkWidget *menuEdit;
GtkWidget *menuHelp;
GtkWidget *menubar;
GtkWidget *menu;
GtkWidget *menuitem;
GtkWidget *vbox;
GtkWidget *handlebox;
GtkWidget *text;

gchar *buffer = “Go to the file menu and select Open to launch the file selection dialog.”;

/*– Initialize GTK –*/
gtk_init (&argc, &argv);

/*– Create the new window –*/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

/*– Create the vbox –*/
vbox = gtk_vbox_new(FALSE, 0);

/*– Create a text area –*/
text = gtk_text_new(NULL, NULL);

/*– Create the handlebox –*/
handlebox = gtk_handle_box_new();

/*– Create the menu bar –*/
menubar = gtk_menu_bar_new();

/*– Set text area to be editable –*/
gtk_text_set_editable(GTK_TEXT (text), TRUE);

/*– Connect the window to the destroyapp function –*/
gtk_signal_connect(GTK_OBJECT(window), “delete_event”, GTK_SIGNAL_FUNC(destroyapp), NULL);

/*—————- Create File menu items ——————*/

menuFile = gtk_menu_item_new_with_label (“File”);
gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuFile);
gtk_widget_show(menuFile);

/*– Create File submenu –*/
menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuFile), menu);

/*– Create New menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“New”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);

/*– Create Open menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“Open”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_signal_connect(GTK_OBJECT (menuitem), “activate”, GTK_SIGNAL_FUNC (create_file_selection), NULL);
gtk_widget_show (menuitem);

/*– Create Exit menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“Exit”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_signal_connect(GTK_OBJECT (menuitem), “activate”, GTK_SIGNAL_FUNC (ClosingAppWindow), NULL);
gtk_widget_show (menuitem);
/*—————- End File menu declarations —————-*/

/*—————- Create Edit menu items ——————–*/

menuEdit = gtk_menu_item_new_with_label (“Edit”);
gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuEdit);
gtk_widget_show(menuEdit);

/*– Create File submenu –*/
menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuEdit), menu);

/*– Create Undo menu item under Edit submenu –*/
menuitem = gtk_menu_item_new_with_label (“Undo”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);

/*– Create Copy menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“Copy”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);

/*– Create Cut menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“Cut”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);
/*—————- End Edit menu declarations —————-*/

/*—————- Start Help menu declarations —————-*/
menuHelp = gtk_menu_item_new_with_label (“Help”);
gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuHelp);
gtk_widget_show(menuHelp);

/*– Create Help submenu –*/
menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuHelp), menu);

/*– Create About menu item under Help submenu –*/
menuitem = gtk_menu_item_new_with_label (“About”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);
/*—————- End Help menu declarations —————-*/

/*– Add the menubar to the handlebox –*/
gtk_container_add(GTK_CONTAINER(handlebox), menubar);

/*– Pack the handlebox into the top of the vbox.
*– You must use gtk_box_pack_start here instead of gtk_container_add
*– or the menu will get larger as the window is enlarged
*/
gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, TRUE, 0);

/*– Add the text area to the window –*/
gtk_container_add(GTK_CONTAINER(vbox), text);

/*– Add the vbox to the main window –*/
gtk_container_add(GTK_CONTAINER(window), vbox);

/*– Add some text to the window –*/
gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buffer, strlen(buffer));

/*– Set window border to zero so that text area takes up the whole window –*/
gtk_container_border_width (GTK_CONTAINER (window), 0);

/*– Set the window to be 640 x 480 pixels –*/
gtk_window_set_default_size (GTK_WINDOW(window), 640, 200);

/*– Set the window title –*/
gtk_window_set_title(GTK_WINDOW (window), “File Dialog”);

/*– Display the widgets –*/
gtk_widget_show(handlebox);
gtk_widget_show(vbox);
gtk_widget_show(text);
gtk_widget_show(menuitem);
gtk_widget_show(menubar);
gtk_widget_show(window);

/*– Start the GTK event loop –*/
gtk_main();

/*– Return 0 if exit is successful –*/
return 0;
}

Compile the Source Code
gcc -Wall -g filedialog.c -o filedialog `gtk-config –cflags` `gtk-config –libs`

Execute the Program
./filedialog

What is Related

Comments are closed.

Subscribe to email feed

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Dedicated Linux Serv

Linux is the popular system nowadays, offering all the benefits ...

StarOffice 5.1

Introduction StarOffice 5.1 is a complete office suite with a word ...

AxY FTP

Introduction AxY FTP, formerly known as wxFTP, is a graphical FTP ...

Adobe Acrobat PDF Re

Introduction Many of you are probably familar with Adobe Acrobat Reader ...

Macromedia Shockwave

Introduction Macromedia has developed a version of its popular Shockwave plugin ...

Twitter updates

No public Twitter messages.