swappo1
October 7th, 2009, 06:18 PM
Hello,
I am getting a segmentation fault from the open_file function and I can't figure out what the problem is. I put a printf statement in and determined that the seg fault is just above it. Any ideas?
#include "menu.h"
#include <stdio.h>
#define NUM_ENTRIES 11
#define RAD_ENTRIES 3
static GtkActionEntry entries[] =
{
{"File", NULL, "_File", NULL, NULL, NULL},
{"New", GTK_STOCK_NEW, NULL, NULL,
"Create a new file", NULL},
{"Open", GTK_STOCK_OPEN, NULL, NULL,
"Open an existing file", G_CALLBACK(open_file)},
{"Save", GTK_STOCK_SAVE, NULL, NULL,
"Save the current file", NULL},
{"Edit", NULL, "_Edit", NULL, NULL, NULL},
{"Undo", GTK_STOCK_UNDO, NULL, NULL,
"Undo the last change", NULL},
{"Cut", GTK_STOCK_CUT, NULL, NULL,
"Cut the selection to the clipboard", NULL},
{"Copy", GTK_STOCK_COPY, NULL, NULL,
"Copy the selection to the clipboard", NULL},
{"Paste", GTK_STOCK_PASTE, NULL, NULL,
"Paste from the clipboard", NULL},
{"View", NULL, "_View", NULL, NULL, NULL},
{"Highlight Mode", NULL, "_Highlight Mode", NULL, NULL, NULL}
};
/*for radio submenu on highlight mode drop down menu*/
static GtkRadioActionEntry radio_entries[] =
{
{"Plain Text", NULL, "Plain Text", NULL, NULL, 1},
{"C Text", NULL, "C", NULL, NULL, 2},
{"C++ Text", NULL, "C++", NULL, NULL, 3},
};
/*this will create the menu and tool bar*/
void create_menu_bar(MainWin *mw)
{
GtkActionGroup *group;
GtkUIManager *ui_manager;
group = gtk_action_group_new("MainActionGroup");
gtk_action_group_add_actions(group, entries, NUM_ENTRIES, NULL);
/*add a radio submenu onto Highlight Mode*/
gtk_action_group_add_radio_actions(group, radio_entries,
RAD_ENTRIES, 1, NULL, NULL);
ui_manager = gtk_ui_manager_new();
gtk_ui_manager_insert_action_group(ui_manager, group, 0);
/*add the menu from the xml file to the ui*/
gtk_ui_manager_add_ui_from_file(ui_manager, "menu.ui", NULL);
/*return the widget found by path /MenuBar*/
mw->menu_bar = gtk_ui_manager_get_widget(ui_manager, "/MenuBar");
gtk_ui_manager_add_ui_from_file(ui_manager, "toolbar.ui", NULL);
/* return the widget found by the path /ToolBar*/
mw->tool_bar = gtk_ui_manager_get_widget(ui_manager, "/ToolBar");
gtk_window_add_accel_group(GTK_WINDOW(mw->window),
gtk_ui_manager_get_accel_group(ui_manager));
}
GtkWidget *create_find()
{
GtkWidget *entry, *hbox, *button;
entry = gtk_entry_new();
button = gtk_button_new_from_stock(GTK_STOCK_FIND);
hbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 0);
return hbox;
}
static void open_file(GtkButton *open, MainWin *mw)
{
GtkWidget *dialog;
gchar *filename, *content;
gint result;
dialog = gtk_file_chooser_dialog_new("Open File", NULL,
GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_OK,
GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL, NULL);
result = gtk_dialog_run(GTK_DIALOG(dialog));
if(result == GTK_RESPONSE_ACCEPT)
{
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dia log));
if(g_file_get_contents(filename, &content, NULL, NULL))
{
mw->buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(mw->text_view));
printf("ok\n");
gtk_text_buffer_set_text(mw->buffer, content, -1);
g_free(filename);
g_free(content);
}
}
gtk_widget_destroy(dialog);
}
I am getting a segmentation fault from the open_file function and I can't figure out what the problem is. I put a printf statement in and determined that the seg fault is just above it. Any ideas?
#include "menu.h"
#include <stdio.h>
#define NUM_ENTRIES 11
#define RAD_ENTRIES 3
static GtkActionEntry entries[] =
{
{"File", NULL, "_File", NULL, NULL, NULL},
{"New", GTK_STOCK_NEW, NULL, NULL,
"Create a new file", NULL},
{"Open", GTK_STOCK_OPEN, NULL, NULL,
"Open an existing file", G_CALLBACK(open_file)},
{"Save", GTK_STOCK_SAVE, NULL, NULL,
"Save the current file", NULL},
{"Edit", NULL, "_Edit", NULL, NULL, NULL},
{"Undo", GTK_STOCK_UNDO, NULL, NULL,
"Undo the last change", NULL},
{"Cut", GTK_STOCK_CUT, NULL, NULL,
"Cut the selection to the clipboard", NULL},
{"Copy", GTK_STOCK_COPY, NULL, NULL,
"Copy the selection to the clipboard", NULL},
{"Paste", GTK_STOCK_PASTE, NULL, NULL,
"Paste from the clipboard", NULL},
{"View", NULL, "_View", NULL, NULL, NULL},
{"Highlight Mode", NULL, "_Highlight Mode", NULL, NULL, NULL}
};
/*for radio submenu on highlight mode drop down menu*/
static GtkRadioActionEntry radio_entries[] =
{
{"Plain Text", NULL, "Plain Text", NULL, NULL, 1},
{"C Text", NULL, "C", NULL, NULL, 2},
{"C++ Text", NULL, "C++", NULL, NULL, 3},
};
/*this will create the menu and tool bar*/
void create_menu_bar(MainWin *mw)
{
GtkActionGroup *group;
GtkUIManager *ui_manager;
group = gtk_action_group_new("MainActionGroup");
gtk_action_group_add_actions(group, entries, NUM_ENTRIES, NULL);
/*add a radio submenu onto Highlight Mode*/
gtk_action_group_add_radio_actions(group, radio_entries,
RAD_ENTRIES, 1, NULL, NULL);
ui_manager = gtk_ui_manager_new();
gtk_ui_manager_insert_action_group(ui_manager, group, 0);
/*add the menu from the xml file to the ui*/
gtk_ui_manager_add_ui_from_file(ui_manager, "menu.ui", NULL);
/*return the widget found by path /MenuBar*/
mw->menu_bar = gtk_ui_manager_get_widget(ui_manager, "/MenuBar");
gtk_ui_manager_add_ui_from_file(ui_manager, "toolbar.ui", NULL);
/* return the widget found by the path /ToolBar*/
mw->tool_bar = gtk_ui_manager_get_widget(ui_manager, "/ToolBar");
gtk_window_add_accel_group(GTK_WINDOW(mw->window),
gtk_ui_manager_get_accel_group(ui_manager));
}
GtkWidget *create_find()
{
GtkWidget *entry, *hbox, *button;
entry = gtk_entry_new();
button = gtk_button_new_from_stock(GTK_STOCK_FIND);
hbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 0);
return hbox;
}
static void open_file(GtkButton *open, MainWin *mw)
{
GtkWidget *dialog;
gchar *filename, *content;
gint result;
dialog = gtk_file_chooser_dialog_new("Open File", NULL,
GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_OK,
GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL, NULL);
result = gtk_dialog_run(GTK_DIALOG(dialog));
if(result == GTK_RESPONSE_ACCEPT)
{
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dia log));
if(g_file_get_contents(filename, &content, NULL, NULL))
{
mw->buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(mw->text_view));
printf("ok\n");
gtk_text_buffer_set_text(mw->buffer, content, -1);
g_free(filename);
g_free(content);
}
}
gtk_widget_destroy(dialog);
}