Hi Errol and Miven,
Thanks for your valuable suggestions.
It took me some time to install gtk3+ and modify the previous code based on the available tutorial files. The code pasted below is intended to perform the following.
1) Opens a main window -> with 1 menuitem "file"-> containing 2 menuitems "Open" and "Run".
2) On clicking "Open"-> Filechooserdialog opens.
Further, the "Filechooserdialog box" has three gdkevent "selection-changed", "current-folder-changed" and "response" with three callbacks namely, "print_selected", "print_current_folder" and response_cb, whose prototype are as follows.
Code:
g_signal_connect (dialog, "selection-changed", G_CALLBACK (print_selected),NULL);
g_signal_connect (dialog, "current-folder-changed", G_CALLBACK (print_current_folder), NULL);
g_signal_connect (dialog, "response", G_CALLBACK (response_cb), NULL);
When I select a file, the callback function to "response" prints the fullpath of the selected file. Till this point, things are fine.
Now I have one minor problem and one major problem. The minor one is, with the following 'response_cb'
callback function, on clicking either the 'open' or 'close' buttons which are present by default in the file selection dialog box, the main window also closes. When I changed the function from 'response_cb' to 'gtk_widget_destroy', this problem was averted. (I'm not sure whether this is the correct way to do it.)
Code:
g_signal_connect (dialog, "response", G_CALLBACK (response_cb), NULL);
But the major one is the passing of the gpointer data to these callback functions. I searched for the standard prototype definitions of the callbacks for the mentioned gdkevents (selection-changed, current-folder-changed, response), I could find none, except for the functions defined in the demo files, which does not pass any gpointer data. i.e.
Code:
static void print_current_folder (GtkFileChooser *chooser);
Code:
static void print_selected (GtkFileChooser *chooser);
Code:
static void response_cb (GtkDialog *dialog, gint response_id);
But what I wanted is to pass a structure similar to the following example; to each of the callbacks and copy and store the filepath as it prints to the terminal.
Code:
typedef struct
{ gchar *filename;
GtkFileChooser *chooser;
} Data_type;
Unfortunately, when I try to initialize and pass it as a data I ran into trouble as 'GtkFileChooser' is an object of a class and allocationg memory is through 'new' function available in c++. I'm not sure how malloc can be used in this instance,
Code:
Data_type data_type, *data_type_ptr;
data_type_ptr=&data_type;
//data_type_ptr->chooser=(GtkFileChooser *) malloc (GtkFileChooser );
data_type_ptr->filename=(gchar *) malloc (MAX_CHAR);
Any help/suggestions on passing the data and printing it to terminal when the 'File->'run' menu is activated (after activation of 'File->open') would be greatly appreciated. Thank you,
The currently working code is as follows,
Code:
/* testmenubars.c -- test different packing directions Copyright (C) 2005 Red Hat, Inc. Author: Matthias Clasen */
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define MAX_CHAR 500
static GtkFileChooserAction action;
static void print_current_folder (GtkFileChooser *chooser)
{
gchar *uri;
uri = gtk_file_chooser_get_current_folder_uri (chooser);
g_print ("Current folder changed :\n %s\n", uri ? uri : "(null)");
g_free (uri);
}
static void print_selected (GtkFileChooser *chooser)
{
GSList *uris = gtk_file_chooser_get_uris (chooser);
GSList *tmp_list;
g_print ("Selection changed :\n");
for (tmp_list = uris; tmp_list; tmp_list = tmp_list->next)
{
gchar *uri = tmp_list->data;
g_print (" %s\n", uri);
g_free (uri);
}
g_print ("\n");
g_slist_free (uris);
}
static void response_cb (GtkDialog *dialog, gint response_id)
{
if (response_id == GTK_RESPONSE_OK)
{
GSList *list;
list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog));
if (list)
{
GSList *l;
g_print ("Selected files:\n");
for (l = list; l; l = l->next)
{
g_print ("%s\n", (char *) l->data);
g_free (l->data);
}
g_slist_free (list);
}
else
g_print ("No selected files\n");
}
else
g_print ("Dialog was closed\n");
gtk_main_quit ();
}
void file_open_dialog()
{
action = GTK_FILE_CHOOSER_ACTION_OPEN;
GtkWidget *dialog;
dialog =gtk_file_chooser_dialog_new("Select a file",NULL,GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
g_signal_connect (dialog, "selection-changed", G_CALLBACK (print_selected),NULL);
g_signal_connect (dialog, "current-folder-changed", G_CALLBACK (print_current_folder), NULL);
g_signal_connect (dialog, "response", G_CALLBACK (response_cb), NULL);
//g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show_all (dialog);
}
int main (int argc, char **argv)
{
static GtkWidget *window = NULL;
GtkWidget *box;
gtk_init (&argc, &argv);
GtkWidget *menubar;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window,"delete-event",G_CALLBACK (gtk_main_quit), NULL);
gtk_window_set_title (GTK_WINDOW (window), "Automex");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
GtkWidget *menuitem,*sub_menuitem;
GtkWidget *menu;
// Creating menubar
menubar = gtk_menu_bar_new ();
gtk_menu_bar_set_pack_direction (GTK_MENU_BAR (menubar), GTK_PACK_DIRECTION_LTR);
gtk_menu_bar_set_child_pack_direction (GTK_MENU_BAR (menubar),GTK_PACK_DIRECTION_LTR);
// Creating menuitem: File
menuitem = gtk_image_menu_item_new_from_stock ("File", NULL);
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
menu = gtk_menu_new ();
// Creating submenuitem: Open
sub_menuitem = gtk_menu_item_new_with_label ("Open");
gtk_menu_shell_append (GTK_MENU_SHELL (menu), sub_menuitem);
g_signal_connect (sub_menuitem,"activate",G_CALLBACK (file_open_dialog), NULL);
gtk_widget_show (sub_menuitem);
// Creating submenuitem: Run
sub_menuitem = gtk_menu_item_new_with_label ("Run");
gtk_menu_shell_append (GTK_MENU_SHELL (menu), sub_menuitem);
gtk_widget_show (sub_menuitem);
// Adding submenuitems to the menuitem
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
gtk_widget_show_all (box);
gtk_widget_show (window);
gtk_main ();
return 0;
}