Dear Errol,
Thanks for your previous reply. I tried to use the function "gtk_entry_buffer_set_text" to load a file in the textview. I was sucessful in viewing the file (by following the discussion
viewtopic.php?f=3&t=178380 ) but actually, i wanted to open the notebook and its associated pages by clicking a new menu (which i wanted to create in future with the name "show_files"). To acheive this, i had to pass the "Gtkwidget *grid" as a member of a struture and display the files selected in the "File->Open" step. If i do so Iam getting segmentation fault even on the first step i.e. after "file selection", when i include a grid as a member. Iam not sure why this happens.
Additionally, if you have any suggestions on dynamically allocating the memory for FILE ** pointers, i would be glad to use it. Any help on the above issues would be greatly appreciated.
I have pasted the working code, but the segmentation causing section of grid usage and its initialization has been commented. Also the section of code, which i wanted to be a call back for "Show_file" menu activation is placed within comment (begin, end). Thank you,
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 <glib/gprintf.h>
#include <glib.h>
#include <glib/gstdio.h>
#define MAXCHAR 300
#define FILE_MAX 100
#define INSTRUCT "info"
static GtkFileChooserAction action;
typedef struct
{
GSList *f_names;
FILE **infile;
//GtkWidget *grid;
}File_ptr;
static void response_cb (GtkDialog *dialog,int response_id, File_ptr * user_data)
{
GSList *list;
*user_data->f_names = *gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER (dialog));
if (user_data->f_names)
{
GSList *l;
g_print ("Selected files:\n");
for (l = user_data->f_names; l; l= l->next)
{ g_print ("Open: %s\n", (gchar *) l->data);}
g_slist_free_full (l,g_free);
}
gtk_widget_destroy (GTK_WIDGET (dialog));
}
void file_name_retrieve (File_ptr * user_data)
{
GSList *l;
int i;
char line[300],c;
gchar file_n[300];
i=0;
for (l = user_data->f_names; l; l = l->next)
{
g_print ("Run: %s\n", (gchar *) l->data);
strcpy(file_n,(gchar *) l->data);
i++;
}
g_slist_free_full (l, g_free);
}
void file_open_dialog(File_ptr *f_names)
{
gboolean multiple=TRUE;
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);
gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog),multiple);
g_signal_connect (dialog, "response", G_CALLBACK (response_cb), f_names);
gtk_widget_show_all (dialog);
}
int main (int argc, char **argv)
{
static GtkWidget *window;
GtkWidget *grid;
GtkWidget *menuitem,*sub_menuitem;
GtkWidget *menu;
GtkWidget *menubar;
GtkWidget *note_book,*page_1,*page_2,*title_1,*title_2,*text_1,*text_2;
gboolean opt_true=TRUE;
File_ptr *f_names=g_slice_new (File_ptr);
gchar *file_name_ptr = NULL;
GtkTextBuffer *buffer=NULL;
GtkTextIter start, end;
GtkTextIter iter;
gint nchars;
gchar line[MAXCHAR];
gchar file_buff[MAXCHAR];
int i;
FILE *infile,*outfile[FILE_MAX];
f_names->infile=&outfile[0];
//f_names->grid = gtk_grid_new ();
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window),500,500);
g_signal_connect (window,"delete-event",G_CALLBACK (gtk_main_quit), NULL);
gtk_window_set_title (GTK_WINDOW (window), "Automex");
grid = gtk_grid_new ();
// Creating menubar
menubar = gtk_menu_bar_new ();
// 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_swapped (sub_menuitem,"activate",G_CALLBACK (file_open_dialog), f_names);
// Creating submenuitem: Run
sub_menuitem = gtk_menu_item_new_with_label ("Run");
gtk_menu_shell_append (GTK_MENU_SHELL (menu), sub_menuitem);
g_signal_connect_swapped (sub_menuitem,"activate",G_CALLBACK (file_name_retrieve), f_names );
// Create a notebook and open two files
// Should go inside a callback function for "show_file" menu
//---- cb for "show_file" begins here ----
note_book=gtk_notebook_new();
page_1 = gtk_text_view_new();
title_1 = gtk_label_new ("File_1");
page_2 = gtk_text_view_new();
title_2 = gtk_label_new ("File_2");
gtk_text_view_set_editable(GTK_TEXT_VIEW (page_1),opt_true);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW (page_1),GTK_WRAP_WORD_CHAR);
gtk_notebook_append_page(GTK_NOTEBOOK(note_book),page_1,title_1);
gtk_notebook_append_page(GTK_NOTEBOOK(note_book),page_2,title_2);
gtk_widget_set_hexpand(note_book,opt_true);
gtk_widget_set_vexpand(note_book,opt_true);
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page_1));
gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
infile = fopen (INSTRUCT, "r");
if (infile)
{
fgets(file_buff,sizeof(file_buff),infile);
while(!(feof(infile)))
{
nchars=strlen(file_buff);
gtk_text_buffer_insert (buffer, &iter, file_buff, nchars);
fgets(file_buff,sizeof(file_buff),infile);
}
}
fclose (infile);
gtk_grid_attach (GTK_GRID (grid),note_book, 0, 1, 200, 200);
// ---- cb for "show_file" ends here ----
// Attaching a menubar
gtk_grid_attach (GTK_GRID (grid), GTK_WIDGET(menubar), 0, 0, 1, 1);
// Adding submenuitems to the menuitem
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), GTK_WIDGET(menu));
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (grid));
gtk_widget_show_all (window);
gtk_main ();
return 0;
}