GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Apr 19, 2014 10:08 pm

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Open file from menu
PostPosted: Sun Jul 08, 2012 3:20 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
I am trying to create a textview that will show the file opens.
I have managed to make it read the commandline argument as:

Code:
  stat(argv[1], &filestat);
    buffer = (char *) malloc(filestat.st_size * sizeof (char));
    efile = fopen(argv[1], "r");
    fread(buffer, filestat.st_size, 1, efile);
    gtk_text_buffer_set_text(textbuffer, buffer, filestat.st_size);
    free(buffer);


But I am facing 2 problem(2nd problem is more important to solve).
1) putting the above block inside "if (argv[1] != "NULL")" is giving a
segmentation fault:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x000000364066c81e in fread () from /lib64/libc.so.6

and 2) How I can open the file using file menu, instead of commandline
argument? I have defined the menu as follows:
Code:
    file = gtk_menu_item_new_with_mnemonic("_File");
    new = gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW,
accel_group);
    open = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN,
accel_group);
    sep = gtk_separator_menu_item_new();
    quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT,
accel_group);

    gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), new);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), sep);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
    gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 3);

    g_signal_connect(G_OBJECT(new), "activate",
G_CALLBACK(filenew_activated), (gpointer) window);


    g_signal_connect(G_OBJECT(open), "activate",
G_CALLBACK(fileopen_activated), (gpointer) window);

    g_signal_connect_swapped(G_OBJECT(window), "destroy",
            G_CALLBACK(gtk_main_quit), NULL);

    g_signal_connect(G_OBJECT(quit), "activate",
            G_CALLBACK(gtk_main_quit), NULL);

which is opening the file selector, but *NOT* writing it in the
textview. I wish to open the file in textview and save it aswell after I
edit that.

If anyone kindly show the how-to, I will learn to solve problem (1) later.
Please help.


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Mon Jul 09, 2012 8:11 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Hi,

You may want to ask your question in another way. Your code samples are far too short given the questions you are asking.

You also seam to have little understanding of the C programming language. You seam to be not doing any error checking. The value of sizeof(char) will always be 1 as all other data types in size are defined in multiples of the type char.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Tue Jul 10, 2012 8:50 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
errol wrote:
Your code samples are far too short given the questions you are asking.
You also seam to have little understanding of the C programming language.

I agree to both of your blame. I am both a c and gtk novice. I am completly depending on the huge tutorials availavle on net to develop the app.
So, I am pasting below the complete code that I have managed to write so far.
If someone help me with two problem, I ll be very happy.

1) How to open/save any file from filemenu option to the textview widget.
2) How I can extract the data entered in the entry widget to write those data in the textview?

Code:
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>

static GtkWidget *window;
GtkWidget *vbox, *hbox, *combo;
GtkWidget *vbox1, *hbox1, *combo1;
GtkWidget *vbox2, *hbox2, *combo2;
GtkWidget *entry1;
GtkWidget *entryEditor;
GtkWidget *entryAuth, *entryEdition, *entryTitle, *entryPub, *entryVol, *entryIss, *entryPage;
GtkWidget *entryMon;
GtkWidget *entryNote, *entryKey, *entrySer, *entryYear;
GtkWidget *entryAdd;

GtkWidget *menubar, *filemenu, *filemenu_menu, *enew, *open, *save;
GtkWidget *separator_1;
GtkWidget *quit;
GtkWidget *editmenu, *editmenu_menu, *editcut, *editcopy, *editpaste;
GtkWidget *separator_2;
GtkWidget *editselectall, *editdeselect;
GtkWidget *helpmenu, *helpmenu_menu, *helphelp, *helpabout;
GtkWidget *toolbar1;
GtkToolItem *tooleditcut, *tooleditcopy, *tooleditpaste;
GtkToolItem *separator_3;
GtkToolItem *tooleditselectall;
GtkAccelGroup *menuGroup;

GtkTextBuffer *buffer;
GtkTextIter start, end, iter;
GtkWidget *view;
#include "menubar.c"

int main(int argc,
        char *argv[]) {
    GtkWidget *window, *button, *table, *frame, *notebook, *label, *checkbutton, *widget, *scrolledwindow, *textview;
    GtkTextBuffer *textbuffer;
    GtkWidget *menubar, *filemenu, *file, *new, *open, *quit;
    GtkWidget *sep;
    GtkAccelGroup *accel_group = NULL;
    struct stat filestat;
    char *buffer;
    FILE *efile;
    int i;
    char bufferf[32];
    char bufferl[32];
    //    char bibtype;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "BibMk");
    gtk_container_set_border_width(GTK_CONTAINER(window), 20);
    gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
    g_signal_connect(window, "delete-event",
            G_CALLBACK(gtk_main_quit), NULL);

    vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_container_add(GTK_CONTAINER(window), hbox);
    gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
    frame = gtk_frame_new("Properties");

    menubar = gtk_menu_bar_new();
    gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 2);

    /*
     * FILE MENU
     */
    menubar = gtk_menu_bar_new();
    filemenu = gtk_menu_new();

    accel_group = gtk_accel_group_new();
    gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);

    file = gtk_menu_item_new_with_mnemonic("_File");
    new = gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW, accel_group);
    open = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN, accel_group);
    sep = gtk_separator_menu_item_new();
    quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel_group);

    gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), new);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), sep);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
    gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 3);

    g_signal_connect(G_OBJECT(new), "activate", G_CALLBACK(filenew_activated), (gpointer) window);
    g_signal_connect(G_OBJECT(open), "activate", G_CALLBACK(fileopen_activated), (gpointer) window);
    g_signal_connect(G_OBJECT(quit), "activate", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    /*
     * HELP MENU
     */
/*    helpmenu = gtk_menu_item_new_with_label("Help");
    helpmenu_menu = gtk_menu_new();
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(helpmenu), helpmenu_menu);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), helpmenu);

    helphelp = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, menuGroup);
    gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu_menu), helphelp);
*/

    vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    hbox2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_container_add(GTK_CONTAINER(vbox), hbox2);

    combo = gtk_combo_box_text_new();
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Book");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Article");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Booklet");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Conference");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "inBook");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "inCollection");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "inProceedings");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Manual");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "MastersThesis");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Misc");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "PhdThesis");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Proceedings");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Techreport");
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), "Unpublished");
    gtk_box_pack_start(GTK_BOX(hbox2), combo, TRUE, TRUE, 0);
    gtk_widget_show(combo);

    entry1 = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entry1), "bibKey");
    gtk_container_add(GTK_CONTAINER(hbox2), entry1);
    gtk_widget_show(entry1);

    scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_set_size_request(scrolledwindow, 400, 175);
    gtk_widget_show(scrolledwindow);
    gtk_container_add(GTK_CONTAINER(hbox), scrolledwindow);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow),
            GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

    textview = gtk_text_view_new();
    gtk_widget_show(textview);
    gtk_container_add(GTK_CONTAINER(scrolledwindow), textview);
    gtk_widget_set_size_request(textview, 600, -1);

    textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));

if ( argc > 1 && argv[1] != NULL ) {
    stat(argv[1], &filestat);
    buffer = (char *) malloc(filestat.st_size * sizeof (char));
    efile = fopen(argv[1], "r");
    fread(buffer, filestat.st_size, 1, efile);
    gtk_text_buffer_set_text(textbuffer, buffer, filestat.st_size);
    free(buffer);
}

/*
* CREATE THE NOTEBOOK PAN
*/

    table = gtk_table_new(3, 6, FALSE);

/* Create a new notebook, place the position of the tabs */
    notebook = gtk_notebook_new();
    gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
    gtk_table_attach_defaults(GTK_TABLE(table), notebook, 0, 6, 0, 1);
    gtk_widget_show(notebook);

/*
* CREATE BASIC NOTEBOOK
*/
    frame = gtk_frame_new("Authors");
    gtk_widget_set_size_request(frame, 400, 175);
    vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    hbox1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_container_add(GTK_CONTAINER(frame), vbox1);

/*
* Authors Tab
*/
    entryAuth = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryAuth), "Author");
    gtk_container_add(GTK_CONTAINER(vbox1), entryAuth);
    gtk_widget_show(entryAuth);

    entryEditor = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryEditor), "Editor");
    gtk_container_add(GTK_CONTAINER(vbox1), entryEditor);
    gtk_widget_show(entryEditor);

    entryTitle = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryTitle), "Title");
    gtk_container_add(GTK_CONTAINER(vbox1), entryTitle);
    gtk_widget_show(entryTitle);

    label = gtk_label_new("Authors/Title");
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label);
/*
* Publishers Tab
*/
    frame = gtk_frame_new("Publishers");
    gtk_widget_set_size_request(frame, 400, 175);
    vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    hbox1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_container_add(GTK_CONTAINER(frame), vbox1);

    entryPub = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryPub), "Publishor");
    gtk_container_add(GTK_CONTAINER(vbox1), entryPub);
    gtk_widget_show(entryPub);

    entryVol = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryVol), "Volume");
    gtk_container_add(GTK_CONTAINER(vbox1), entryVol);
    gtk_widget_show(entryVol);

    entryIss = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryIss), "Issue");
    gtk_container_add(GTK_CONTAINER(vbox1), entryIss);
    gtk_widget_show(entryIss);

    entryPage = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryPage), "Page");
    gtk_container_add(GTK_CONTAINER(vbox1), entryPage);
    gtk_widget_show(entryPage);

    entryYear = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryYear), "Year");
    gtk_container_add(GTK_CONTAINER(vbox1), entryYear);
    gtk_widget_show(entryYear);

    label = gtk_label_new("Publishers");
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label);

/*
* Other Tab
*/
    frame = gtk_frame_new("Optional");
    gtk_widget_set_size_request(frame, 400, 175);
    vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    hbox1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_container_add(GTK_CONTAINER(frame), vbox1);

    entryMon = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryMon), "Month");
    gtk_container_add(GTK_CONTAINER(vbox1), entryMon);
    gtk_widget_show(entryMon);

    entryNote = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryNote), "Note");
    gtk_container_add(GTK_CONTAINER(vbox1), entryNote);
    gtk_widget_show(entryNote);

    entryKey = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryKey), "Key");
    gtk_container_add(GTK_CONTAINER(vbox1), entryKey);
    gtk_widget_show(entryKey);

    entryAdd = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryAdd), "Address");
    gtk_container_add(GTK_CONTAINER(vbox1), entryAdd);
    gtk_widget_show(entryAdd);

    entryEdition = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entryEdition), "Edition");
    gtk_container_add(GTK_CONTAINER(vbox1), entryEdition);
    gtk_widget_show(entryEdition);

    label = gtk_label_new("Optional");
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label);

    button = gtk_button_new_with_label("CREATE");
    gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
    gtk_container_add(GTK_CONTAINER(vbox), button);
    gtk_widget_show(button);

    gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
    gtk_widget_show(table);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Tue Jul 10, 2012 5:37 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Just a few more questions before I can give an answer.

What version of GTK+ are you using?

The call back functions are not included with your source and they will give me more of a clue to what you may want. Also you are including a C source file. Although this can be done it is considered bad practice.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Tue Jul 10, 2012 7:02 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
its gtk+3.
can be compiled using
gcc bibmk.c -o cbib `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
here is the callback function.
Sorry that i missed it.
Code:
/*
// prog menubar.c
* Functions defined in menubar
*/
static gboolean delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )
{
    gtk_main_quit ();
    return FALSE;
}

void  MessageBox (GtkWindow *parentWindow, char *messageValue)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new (parentWindow, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, messageValue);
    gtk_window_set_title (GTK_WINDOW (dialog), "Information");
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}


/*static void filenew_activated (GtkMenuItem *filenew, GtkWindow *parentWindow)
{
    MessageBox(parentWindow, "Menu filenew was clicked.");
}*/


static void filenew_activated ( GtkWidget *widget, gpointer data)
{
GtkWidget *dialog;
dialog =gtk_file_chooser_dialog_new("OPEN FILE",
                                     GTK_WINDOW(window),
                                     GTK_FILE_CHOOSER_ACTION_OPEN,
                                     GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,
                                     GTK_STOCK_NEW,GTK_RESPONSE_ACCEPT,
                                     NULL);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  {
    char *filename;

    filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
//    open_file (filename);
    g_free (filename);
  }

gtk_widget_destroy (dialog);
}


static void fileopen_activated ( GtkWidget *widget, gpointer data)
{
GtkWidget *dialog;
dialog =gtk_file_chooser_dialog_new("OPEN FILE",
                                     GTK_WINDOW(window),
                                     GTK_FILE_CHOOSER_ACTION_OPEN,
                                     GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,
                                     GTK_STOCK_NEW,GTK_RESPONSE_ACCEPT,
                                     NULL);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  {
    char *filename;

    filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
//    open_file (filename);
    g_free (filename);
  }

gtk_widget_destroy (dialog);
}


one side question, if putting c sourcecode is a bad practice (evidently, as i cannot attach c code as file), how should i show my problem?


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Thu Jul 12, 2012 3:58 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Any help please?


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Thu Jul 12, 2012 11:55 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Please do not demand help. Everyone here is doing this for free in their spare time. So unless you are paying people for a service please be patient.

Note it takes time to sift though peoples code, work out that they are attempting to do and then work out a solution

_________________
E.


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Sat Jul 14, 2012 8:35 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Your original code contains many errors and uses many deprecated API usage. I will concentrate on the main areas.

First you usage of deprecated code you use the widgets GtkBox, GtkTable both of which are deprecated and you should use GtkGrid instead.

You should not use
Code:
#include "menubar.c"
header files should be for definitions of function, structures, data types and macros. And not for including sections of C code. When you want to separate sections of code compile them individually to an object file and then link them together.

You do not need to use gtk_widget_show() on each widget and then use gtk_widget_show_all() on the window. To make your code cleaner do not use gtk_widget_show() and once you have created all of your widgets and placed them in the window you can then use gtk_widget_show_all(). This tells GTK+ to walk through all the widgets showing them. This will shorten your code considerably.

When organising your code you may want to first define your user interface then connect your signals for the user interface. This way you keep the code a bit more organised and also signals will not start being called before you have finished building the interface.

For related widgets such as that for a window or a menu bar you will probably want to keep the data in a structure to help organise your application and to keep track of the meaning of each variable. After a while knowing what the meaning of the variable "vbox1", "vbox2", and probably "vbox768" become impossible. Also only keep hold of variables that you need long term, so layout widgets probably do not need to be held onto long term.

Going back to your original problem I have rewritten your original call back function. Here the user data pass is a pointer to a GtkTextBuffer as there is no other access to this data unless a global variable is used. If you application was more structured you could have passed a pointer to a structure which could contain much more information including a point to the GtkTextBuffer. Note that I have used a GLib function to read the file. Note prefer GLib functions if you are using GTK+ as you know that there will always be there, then use system calls if there is nothing there that can do what you need.
Code:
static void fileopen_activated( GtkMenuItem *widget, gpointer data)
{
    GtkWidget *dialog = gtk_file_chooser_dialog_new("OPEN FILE",
                                     GTK_WINDOW(window),
                                     GTK_FILE_CHOOSER_ACTION_OPEN,
                                     GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,
                                     GTK_STOCK_NEW,GTK_RESPONSE_ACCEPT,
                                     NULL);

    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
        gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
        char *buffer;
        gsize len;
        GtkTextBuffer *textbuffer = GTK_TEXT_BUFFER(data);

        if (g_file_get_contents(filename, &buffer, &len, NULL)) {
            gtk_text_buffer_set_text(textbuffer, buffer, len);
            g_free(buffer);
        }
        g_free (filename);
    }

    gtk_widget_destroy (dialog);
}

And this is how the signal is connected to the call back. The user data being passed is the text buffer.
Code:
    g_signal_connect(G_OBJECT(open), "activate", G_CALLBACK(fileopen_activated), (gpointer) textbuffer);

_________________
E.


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Sat Jul 14, 2012 2:39 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Errol,
A huge thanks for the modified code.
But, after changing the callback function code and the g_signal_connect, It still failed to read the file and open it in textview.
Am I missing something?
when the file is opened from command line, I found there is function to open and read it as

Code:
   buffer = (char *) malloc(filestat.st_size * sizeof (char));
    efile = fopen(argv[1], "r");
    fread(buffer, filestat.st_size, 1, efile);
    gtk_text_buffer_set_text(textbuffer, buffer, filestat.st_size);


but there is no fread in the callback function. may be this is what I am missing? I am also trying to rewrite the code according to your suggestion. I will be grateful if you kindly take one more view on openning the file.
Regards


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Sat Jul 14, 2012 5:52 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Yes, your original code is flawed, it has many errors in it including memory leaks, not closing an opened file, lack of error checking, many unused variables, poor coding style and very inefficient code. When I compiled it with full warnings set the number of warnings I got was huge.

So to say checking your code was difficult is a bit of an understatement.

If you read my comments and looked at the code you would see that I have used the GLib API to read a file which has full error checking. You could transfer that code to read in the file via the command line to replace your poor code else where.

What we are getting into is very basic C language problems and has very little to do with GTK+. You may want to read a book on C programming and learn a bit more. The main book is "The C Programming Language" by Brian Kernighan and Dennis Ritchie.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Open file from menu
PostPosted: Sat Jul 14, 2012 6:28 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Errol,
Yes, I understand this. I am not a C programmer. I have gatherd the elements from many different tutorials. So, those problems are there. I am trying to rewrite the code according to your suggestion(Thanks for your time and pointing the problems).
Once I am done I will repost the code for suggestions from the forum.
Hope, I will get some kind help from you then as you did now.
Regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group