Find the answer to your Linux question:
Results 1 to 6 of 6
I'm writing my own X text editor. I'm stuck with function for saving file. How can I declare the text inside the text widget? It creates a file when I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Oct 2004
    Location
    Serbia&Montenegro
    Posts
    281

    Need some GTK help!


    I'm writing my own X text editor. I'm stuck with function for saving file. How can I declare the text inside the text widget? It creates a file when I try to save it but I can't save the text. Any ideas?
    Linux registered user #358842
    Human knowledge belongs to the world.

  2. #2
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Hello.
    Do you mean you want to access text within GtkTextView widget If so then try using the following combination:
    Code:
     GtkTextBuffer *buf = gtk_text_view_get_buffer(yourEntryWidget);
    GtkTextIter start_iter, end_iter;
    gtk_text_buffer_get_iter_at_offset(buf, &start_iter, 0);
    gtk_text_buffer_get_iter_at_offset(buf, &end_iter, -1);
    gchar *text = gtk_text_buffer_get_text(buf, &start_iter, &end_iter, FALSE);
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  3. #3
    Linux User
    Join Date
    Oct 2004
    Location
    Serbia&Montenegro
    Posts
    281
    Hi kyku. Thanks for your reply. Here's the code I have (it's still 0.1 version):
    Code:
    #include <gtk/gtk.h>
    #include <strings.h>
    #include <stdio.h>
    #include <fcntl.h>
    
    GtkWidget *file_selection_box;
    char *filename;
    FILE *infile;
    int f;
    void open1&#40;&#41;;
    void file&#40;&#41;;
    void store_filename&#40;&#41;;
    void store_filename1&#40;GtkFileSelection *file_selection, gpointer data&#41;;
    void save1&#40;&#41;;
    
    void save&#40;void&#41;
    &#123;
    file_selection_box = gtk_file_selection_new&#40;"Save file"&#41;;
    
    gtk_signal_connect &#40;GTK_OBJECT &#40;GTK_FILE_SELECTION&#40;file_selection_box&#41;->ok_button&#41;, "clicked",
    GTK_SIGNAL_FUNC &#40;store_filename1&#41;, NULL&#41;;
    
    gtk_signal_connect_object &#40;GTK_OBJECT &#40;GTK_FILE_SELECTION&#40;file_selection_box&#41;->ok_button&#41;,
     "clicked", GTK_SIGNAL_FUNC &#40;save1&#41;, &#40;gpointer&#41; file_selection_box&#41;;
    
    gtk_signal_connect_object &#40;GTK_OBJECT &#40;GTK_FILE_SELECTION&#40;file_selection_box&#41;->cancel_button&#41;,
    "clicked", GTK_SIGNAL_FUNC &#40;gtk_widget_destroy&#41;, &#40;gpointer&#41; file_selection_box&#41;;
    
    gtk_widget_show &#40;file_selection_box&#41;;
    gtk_main&#40;&#41;;
    &#125;
    
    void option&#40;&#41;
    &#123;
        GtkWidget *window;
       GtkWidget *frame;
        GtkWidget *button;
        GtkWidget *label;
        gint i;
    
        window = gtk_window_new &#40;GTK_WINDOW_TOPLEVEL&#41;;
        gtk_window_set_title&#40;GTK_WINDOW&#40;window&#41;, "Testing option"&#41;;
    
        gtk_signal_connect &#40;GTK_OBJECT &#40;window&#41;, "destroy",
                            GTK_SIGNAL_FUNC &#40;gtk_main_quit&#41;, NULL&#41;;
    
        gtk_widget_set_usize&#40;window, 300, 300&#41;;
        gtk_container_set_border_width &#40;GTK_CONTAINER &#40;window&#41;, 10&#41;;
    
        frame = gtk_frame_new&#40;NULL&#41;;
        gtk_container_add&#40;GTK_CONTAINER&#40;window&#41;, frame&#41;;
    
        gtk_frame_set_shadow_type&#40; GTK_FRAME&#40;frame&#41;, GTK_SHADOW_ETCHED_OUT&#41;;
        label = gtk_label_new &#40;"This option is still under construction"&#41;;
        gtk_container_add &#40;GTK_CONTAINER &#40;frame&#41;, label&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;frame&#41;, label, FALSE, FALSE, 0&#41;;
        gtk_widget_show&#40;frame&#41;;
        gtk_widget_show&#40;label&#41;;
        gtk_widget_show &#40;window&#41;;
        gtk_main &#40;&#41;;
    &#125;
    
      /* This is the GtkItemFactoryEntry structure used to generate new menus.
         Item 1&#58; The menu path. The letter after the underscore indicates an
                 accelerator key once the menu is open.
         Item 2&#58; The accelerator key for the entry
         Item 3&#58; The callback function.
         Item 4&#58; The callback action.  This changes the parameters with
                 which the function is called.  The default is 0.
         Item 5&#58; The item type, used to define what kind of an item it is.
                    Here are the possible values&#58;
    
                 NULL               -> "<Item>"
                 ""                 -> "<Item>"
                 "<Title>"          -> create a title item
                 "<Item>"           -> create a simple item
                 "<CheckItem>"      -> create a check item
                 "<ToggleItem>"     -> create a toggle item
                 "<RadioItem>"      -> create a radio item
                 <path>             -> path of a radio item to link against
                 "<Separator>"      -> create a separator
                 "<Branch>"         -> create an item to hold sub items &#40;optional&#41;
                 "<LastBranch>"     -> create a right justified branch
      */
    
      static  GtkItemFactoryEntry menu_items&#91;&#93; = &#123;
        &#123; "/_Fajl",         NULL,         NULL, 0, "<Branch>" &#125;,
        &#123; "/Fajl/_New",    "<control>N", option, 0, NULL &#125;,
        &#123; "/Fajl/_Open",  "<control>O", file, 0, NULL &#125;,
        &#123; "/Fajl/_Save", "<control>S", option, 0, NULL &#125;,
        &#123; "/Fajl/Save_As", "<control>K", save, 0, NULL &#125;,
        &#123; "/Fajl/sep1",     NULL,         NULL, 0, "<Separator>" &#125;,
        &#123; "/Fajl/Exit",     "<control>Q", gtk_main_quit, 0, NULL &#125;,
        &#123; "/_Options",      NULL,         NULL, 0, "<Branch>" &#125;,
        &#123; "/Help",         NULL,         NULL, 0, "<Branch>" &#125;,
        &#123; "/_Help/Details",   NULL,         option, 0, NULL &#125;,
      &#125;;
    
    
      int get_main_menu&#40; GtkWidget  *window,
                          GtkWidget **menubar &#41;
      &#123;
        GtkItemFactory *item_factory;
        GtkAccelGroup *accel_group;
        gint nmenu_items = sizeof &#40;menu_items&#41; / sizeof &#40;menu_items&#91;0&#93;&#41;;
        
        accel_group = gtk_accel_group_new &#40;&#41;;
    
        item_factory = gtk_item_factory_new &#40;GTK_TYPE_MENU_BAR, "<main>",
                                             accel_group&#41;;
    
        gtk_item_factory_create_items &#40;item_factory, nmenu_items, menu_items, NULL&#41;;
    
        gtk_window_add_accel_group &#40;GTK_WINDOW &#40;window&#41;, accel_group&#41;;
    
        if &#40;menubar&#41;
          *menubar = gtk_item_factory_get_widget &#40;item_factory, "<main>"&#41;;
      &#125;
    
      int main&#40; int argc,
                char *argv&#91;&#93; &#41;
      &#123;
        GtkWidget *window;
        GtkWidget *main_vbox;
        GtkWidget *menubar;
        GtkWidget *box2;
        GtkWidget *text;
        GtkWidget *table;
        GtkWidget *vscrollbar;
    
        gtk_init &#40;&argc, &argv&#41;;
        create&#40;&#41;;
        gtk_main&#40;&#41;;
        return&#40;0&#41;;
    
      &#125;
    
    
    void open1&#40;int argc, char *argv&#91;&#93;&#41;
    &#123;
                   GtkWidget *text;
        GtkWidget *window;
        GtkWidget *main_vbox;
        GtkWidget *menubar;
        GtkWidget *box2;
        GtkWidget *table;
        GtkWidget *vscrollbar;
    
        gtk_init &#40;&argc, &argv&#41;;
    
        window = gtk_window_new &#40;GTK_WINDOW_TOPLEVEL&#41;;
        gtk_signal_connect &#40;GTK_OBJECT &#40;window&#41;, "destroy",
                            GTK_SIGNAL_FUNC &#40;gtk_main_quit&#41;,
                            "WM destroy"&#41;;
        gtk_window_set_title &#40;GTK_WINDOW&#40;window&#41;, "Tekst editor v0.1"&#41;;
        gtk_widget_set_usize &#40;GTK_WIDGET&#40;window&#41;, 600, 500&#41;;
    
        main_vbox = gtk_vbox_new &#40;FALSE, 1&#41;;
        gtk_container_border_width &#40;GTK_CONTAINER &#40;main_vbox&#41;, 1&#41;;
        gtk_container_add &#40;GTK_CONTAINER &#40;window&#41;, main_vbox&#41;;
        gtk_widget_show &#40;main_vbox&#41;;
    
        get_main_menu &#40;window, &menubar&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;main_vbox&#41;, menubar, FALSE, TRUE, 0&#41;;
        gtk_widget_show &#40;menubar&#41;;
    
        box2 = gtk_vbox_new &#40;FALSE, 10&#41;;
        gtk_container_set_border_width &#40;GTK_CONTAINER &#40;box2&#41;, 10&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;main_vbox&#41;, box2, TRUE, TRUE, 0&#41;;
        gtk_widget_show &#40;box2&#41;;
    
    
        table = gtk_table_new &#40;2, 2, FALSE&#41;;
        gtk_table_set_row_spacing &#40;GTK_TABLE &#40;table&#41;, 0, 2&#41;;
        gtk_table_set_col_spacing &#40;GTK_TABLE &#40;table&#41;, 0, 2&#41;;
          gtk_box_pack_start &#40;GTK_BOX &#40;box2&#41;, table, TRUE, TRUE, 0&#41;;
        gtk_widget_show &#40;table&#41;;
    
        text = gtk_text_new &#40;NULL, NULL&#41;;
        gtk_text_set_editable &#40;GTK_TEXT &#40;text&#41;, TRUE&#41;;
        gtk_table_attach &#40;GTK_TABLE &#40;table&#41;, text, 0, 1, 0, 1,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0&#41;;
        gtk_widget_show &#40;text&#41;;
    
        vscrollbar = gtk_vscrollbar_new &#40;GTK_TEXT &#40;text&#41;->vadj&#41;;
        gtk_table_attach &#40;GTK_TABLE &#40;table&#41;, vscrollbar, 1, 2, 0, 1,
                          GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0&#41;;
        gtk_widget_show &#40;vscrollbar&#41;;
    
        GdkFont *fixed_font;
        fixed_font = gdk_font_load &#40;"-misc-fixed-medium-r-*-*-*-120-*-*-*-*-*-*"&#41;;
        infile = fopen&#40;filename, "r"&#41;;
    
        if &#40;infile&#41; &#123;
          char buffer&#91;1024&#93;;
          int nchars;
    
          while &#40;1&#41;
            &#123;
              nchars = fread&#40;buffer, 1, 1024, infile&#41;;
              gtk_text_insert&#40;GTK_TEXT&#40;text&#41;, fixed_font, NULL,
                               NULL, buffer, nchars&#41;;
    
              if &#40;nchars < 1024&#41;
                break;
            &#125;
    
          fclose &#40;infile&#41;;
    
          
        &#125;
        gtk_widget_show &#40;window&#41;;
        gtk_main &#40;&#41;;
    &#125;
    void store_filename&#40;GtkFileSelection *file_selection, gpointer data&#41;
    &#123;
    filename = gtk_file_selection_get_filename&#40;GTK_FILE_SELECTION&#40;file_selection_box&#41;&#41;;
    
    g_print &#40;"Opened file&#58; %s\n", filename&#41;;
    &#125;
    
    void file&#40;void&#41;
    &#123;
    file_selection_box = gtk_file_selection_new&#40;"Open file"&#41;;
    
    gtk_signal_connect &#40;GTK_OBJECT &#40;GTK_FILE_SELECTION&#40;file_selection_box&#41;->ok_button&#41;, "clicked",
    GTK_SIGNAL_FUNC &#40;store_filename&#41;, NULL&#41;;
    
    gtk_signal_connect_object &#40;GTK_OBJECT &#40;GTK_FILE_SELECTION&#40;file_selection_box&#41;->ok_button&#41;,
     "clicked", GTK_SIGNAL_FUNC &#40;open1&#41;, &#40;gpointer&#41; file_selection_box&#41;;
    
    gtk_signal_connect_object &#40;GTK_OBJECT &#40;GTK_FILE_SELECTION&#40;file_selection_box&#41;->cancel_button&#41;,
    "clicked", GTK_SIGNAL_FUNC &#40;gtk_widget_destroy&#41;, &#40;gpointer&#41; file_selection_box&#41;;
    
    gtk_widget_show &#40;file_selection_box&#41;;
    gtk_main&#40;&#41;;
    &#125;
    gint create&#40;int argc, char *argv&#91;&#93;&#41;
    &#123;
      GtkWidget *window;
        GtkWidget *main_vbox;
        GtkWidget *menubar;
        GtkWidget *box2;
        GtkWidget *text;
          GtkWidget *table;
        GtkWidget *vscrollbar;
    
        gtk_init &#40;&argc, &argv&#41;;
    
        window = gtk_window_new &#40;GTK_WINDOW_TOPLEVEL&#41;;
        gtk_signal_connect &#40;GTK_OBJECT &#40;window&#41;, "destroy",
                            GTK_SIGNAL_FUNC &#40;gtk_main_quit&#41;,
                            NULL&#41;;
        gtk_window_set_title &#40;GTK_WINDOW&#40;window&#41;, "Tekst editor v0.1"&#41;;
        gtk_widget_set_usize &#40;GTK_WIDGET&#40;window&#41;, 600, 500&#41;;
    
        main_vbox = gtk_vbox_new &#40;FALSE, 1&#41;;
        gtk_container_border_width &#40;GTK_CONTAINER &#40;main_vbox&#41;, 1&#41;;
        gtk_container_add &#40;GTK_CONTAINER &#40;window&#41;, main_vbox&#41;;
        gtk_widget_show &#40;main_vbox&#41;;
    
        get_main_menu &#40;window, &menubar&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;main_vbox&#41;, menubar, FALSE, TRUE, 0&#41;;
        gtk_widget_show &#40;menubar&#41;;
    
        box2 = gtk_vbox_new &#40;FALSE, 10&#41;;
        gtk_container_set_border_width &#40;GTK_CONTAINER &#40;box2&#41;, 10&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;main_vbox&#41;, box2, TRUE, TRUE, 0&#41;;
        gtk_widget_show &#40;box2&#41;;
    
    
        table = gtk_table_new &#40;2, 2, FALSE&#41;;
        gtk_table_set_row_spacing &#40;GTK_TABLE &#40;table&#41;, 0, 2&#41;;
        gtk_table_set_col_spacing &#40;GTK_TABLE &#40;table&#41;, 0, 2&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;box2&#41;, table, TRUE, TRUE, 0&#41;;
        gtk_widget_show &#40;table&#41;;
    
        text = gtk_text_new &#40;NULL, NULL&#41;;
        gtk_text_set_editable &#40;GTK_TEXT &#40;text&#41;, TRUE&#41;;
          gtk_table_attach &#40;GTK_TABLE &#40;table&#41;, text, 0, 1, 0, 1,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0&#41;;
        gtk_widget_show &#40;text&#41;;
    
        vscrollbar = gtk_vscrollbar_new &#40;GTK_TEXT &#40;text&#41;->vadj&#41;;
        gtk_table_attach &#40;GTK_TABLE &#40;table&#41;, vscrollbar, 1, 2, 0, 1,
                          GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0&#41;;
        gtk_widget_show &#40;vscrollbar&#41;;
    
    
        gtk_widget_show &#40;window&#41;;
        gtk_main&#40;&#41;;
    
        return&#40;0&#41;;
      &#125;
    void edit &#40;GtkWidget *text&#41;
    
      &#123;
        gtk_text_set_editable&#40;GTK_TEXT&#40;text&#41;, TRUE&#41;;
    
      &#125;
    
      void wrap &#40;GtkWidget *text&#41;
    
      &#123;
        gtk_text_set_word_wrap&#40;GTK_TEXT&#40;text&#41;, TRUE&#41;;
    
      &#125;
    void store_filename1&#40;GtkFileSelection *file_selection, gpointer data&#41;
    &#123;
    filename = gtk_file_selection_get_filename&#40;GTK_FILE_SELECTION&#40;file_selection_box&#41;&#41;;
    
    g_print &#40;"Saved file&#58; %s\n", filename&#41;;
    &#125;
    void save1&#40;int argc, char *argv&#91;&#93;&#41;
    &#123;
        int j;
        GtkWidget *text;
        GtkWidget *window;
        GtkWidget *main_vbox;
        GtkWidget *menubar;
        GtkWidget *box2;
        GtkWidget *table;
        GtkWidget *vscrollbar;
    
        gtk_init &#40;&argc, &argv&#41;;
    
        window = gtk_window_new &#40;GTK_WINDOW_TOPLEVEL&#41;;
        gtk_signal_connect &#40;GTK_OBJECT &#40;window&#41;, "destroy",
                            GTK_SIGNAL_FUNC &#40;gtk_main_quit&#41;,
                            "WM destroy"&#41;;
        gtk_window_set_title &#40;GTK_WINDOW&#40;window&#41;, "Tekst editor v0.1"&#41;;
        gtk_widget_set_usize &#40;GTK_WIDGET&#40;window&#41;, 600, 500&#41;;
    
        main_vbox = gtk_vbox_new &#40;FALSE, 1&#41;;
          gtk_container_border_width &#40;GTK_CONTAINER &#40;main_vbox&#41;, 1&#41;;
        gtk_container_add &#40;GTK_CONTAINER &#40;window&#41;, main_vbox&#41;;
    
        get_main_menu &#40;window, &menubar&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;main_vbox&#41;, menubar, FALSE, TRUE, 0&#41;;
    
        box2 = gtk_vbox_new &#40;FALSE, 10&#41;;
        gtk_container_set_border_width &#40;GTK_CONTAINER &#40;box2&#41;, 10&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;main_vbox&#41;, box2, TRUE, TRUE, 0&#41;;
    
        table = gtk_table_new &#40;2, 2, FALSE&#41;;
        gtk_table_set_row_spacing &#40;GTK_TABLE &#40;table&#41;, 0, 2&#41;;
        gtk_table_set_col_spacing &#40;GTK_TABLE &#40;table&#41;, 0, 2&#41;;
        gtk_box_pack_start &#40;GTK_BOX &#40;box2&#41;, table, TRUE, TRUE, 0&#41;;
         text = gtk_text_new &#40;NULL, NULL&#41;;
        gtk_text_set_editable &#40;GTK_TEXT &#40;text&#41;, TRUE&#41;;
        gtk_table_attach &#40;GTK_TABLE &#40;table&#41;, text, 0, 1, 0, 1,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0&#41;;
    
        vscrollbar = gtk_vscrollbar_new &#40;GTK_TEXT &#40;text&#41;->vadj&#41;;
        gtk_table_attach &#40;GTK_TABLE &#40;table&#41;, vscrollbar, 1, 2, 0, 1,
                          GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0&#41;;
    
        GdkFont *fixed_font;
        fixed_font = gdk_font_load &#40;"-misc-fixed-medium-r-*-*-*-120-*-*-*-*-*-*"&#41;;
        f = open&#40;filename,O_WRONLY|O_CREAT,0644&#41;;
        write&#40;gtk_editable_get_chars&#40;GTK_EDITABLE&#40;text&#41;, 0, -1&#41;,  f&#41;;
        close&#40;f&#41;;
        gtk_main &#40;&#41;;
        g_free&#40;text&#41;;
    &#125;
    I have problems with last few lines of the code.
    Linux registered user #358842
    Human knowledge belongs to the world.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Hello
    It seems that GtkText widget is deprecated in GTK+ 2.0. Try GtkTextView instead or if you insist on using it compile with -DGTK_ENABLE_BROKEN (check on top of gtktext.h). By the way you should compile code with -Wall flag and ensure no warnings appear. Best regards.
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  6. #5
    Linux User
    Join Date
    Oct 2004
    Location
    Serbia&Montenegro
    Posts
    281
    Hi kyku. I'll try GTK 2.0 cause I saw on the net that it's better. Thanks for your help.
    Linux registered user #358842
    Human knowledge belongs to the world.

  7. #6
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Nice to see that I was compiling it with wrong wersion of the library
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •