GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Oct 21, 2014 4:46 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Create a new tab in GtkNotebook
PostPosted: Sun Oct 23, 2011 11:10 am 
Offline
GTK+ Geek

Joined: Sun Apr 03, 2011 11:12 am
Posts: 63
Hello friends, I am trying to create gedit :P. But I got stuck.
Here is my code

Code:
#include<gtk/gtk.h>
#include<glib.h>
#include<gtksourceview/gtksourceview.h>
#include<gtksourceview/gtksourcelanguage.h>
#include<gtksourceview/gtksourcelanguagemanager.h>
#include<stdio.h>

GtkSourceBuffer *buffer;
GtkWidget  *notebook;
GtkWidget *scrollwin[10];
GtkWidget *txtinput[10];

static void destroy(GtkWidget *);
static gboolean delete_event(GtkWidget *,GdkEvent *);
void open_activated(GtkWidget *, GtkWindow *);
void filenew_activated(GtkWidget *);
void save_activated(GtkWidget *, GtkWindow *);
void cut_activated(GtkWidget *);
void paste_activated(GtkWidget *);
void copy_activated(GtkWidget *);
void undo_activated(GtkWidget *);
void redo_activated(GtkWidget *);
void newtab_activated(GtkWidget *);

int main(int argc, char *argv[])
{
    GtkWidget *window,*menubar, *file, *edit, *filemenu, *editmenu, *label1;
    GtkWidget *filenew, *open, *save, *cut, *copy, *paste,*vbox,*undo,*redo,*newtab;
    GtkAccelGroup *group;
    GtkAdjustment *horizontal, *vertical;
    GtkWidget *viewport;

    int i;
   
    gtk_init(&argc, &argv);
   
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window),"TextEditor");
    gtk_widget_set_size_request(window,600,400);
   
    group = gtk_accel_group_new();
    menubar = gtk_menu_bar_new();
   
    for (i=0;i<10;i++)
    {
        buffer = GTK_SOURCE_BUFFER(gtk_source_buffer_new(NULL));
        txtinput[i] = gtk_source_view_new_with_buffer(buffer);   
        scrollwin[i] = gtk_scrolled_window_new(NULL,NULL);
        }
   
    notebook = gtk_notebook_new();
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin[0]),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
    horizontal = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrollwin[0]));
    vertical = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrollwin[0]));
    viewport = gtk_viewport_new(horizontal,vertical);
    gtk_container_add(GTK_CONTAINER(scrollwin[0]),txtinput[0]);
    label1 = gtk_label_new("New File");
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook),scrollwin[0],label1);
       
    vbox = gtk_vbox_new(FALSE,2);
    gtk_box_pack_start(GTK_BOX(vbox),menubar,FALSE,FALSE,0); /* Expand set to FALSE,Hence Fill also to FALSE*/
    gtk_box_pack_start(GTK_BOX(vbox),notebook,TRUE,TRUE,0); /* Expand and Fill set to FALSE*/
   
    file = gtk_menu_item_new_with_label("File");
    edit = gtk_menu_item_new_with_label("Edit");
   
    filemenu = gtk_menu_new();
    editmenu = gtk_menu_new();
   
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(file),filemenu);
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit),editmenu);
   
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar),file);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar),edit);
   
    /*Create the File menu*/
    filenew = gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW,group);
    open = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN,group);
    save = gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE,group);
    newtab = gtk_menu_item_new_with_label("New Tab");
       
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu),filenew);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu),newtab);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu),open);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu),save);
   
    /*Create the Edit menu*/
    cut = gtk_image_menu_item_new_from_stock(GTK_STOCK_CUT,group);
    copy = gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY,group);
    paste = gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE,group);
    undo = gtk_image_menu_item_new_from_stock(GTK_STOCK_UNDO,group);
    redo = gtk_image_menu_item_new_from_stock(GTK_STOCK_REDO,group);
   
    g_signal_connect(G_OBJECT(undo),"activate",G_CALLBACK(undo_activated),NULL);
    g_signal_connect(G_OBJECT(redo),"activate",G_CALLBACK(redo_activated),NULL);
    g_signal_connect(G_OBJECT(cut),"activate",G_CALLBACK(cut_activated),NULL);
    g_signal_connect(G_OBJECT(paste),"activate",G_CALLBACK(paste_activated),NULL);
    g_signal_connect(G_OBJECT(copy),"activate",G_CALLBACK(copy_activated),NULL);
   
    gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),undo);
    gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),redo);
    gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),cut);
    gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),copy);
    gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),paste);
   
    g_signal_connect(G_OBJECT(open),"activate",G_CALLBACK(open_activated),(gpointer)GTK_WINDOW(window));
    g_signal_connect(G_OBJECT(filenew),"activate",G_CALLBACK(filenew_activated),NULL);
    g_signal_connect(G_OBJECT(save),"activate",G_CALLBACK(save_activated),(gpointer)GTK_WINDOW(window));
    g_signal_connect(G_OBJECT(newtab),"activate",G_CALLBACK(newtab_activated),NULL);
   
    gtk_container_add(GTK_CONTAINER(window),vbox);
    gtk_window_add_accel_group(GTK_WINDOW(window),group);
    g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(destroy),NULL);
    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),NULL);
   
    gtk_widget_show_all(window);
    gtk_main();
    return 0;
    } 

static void destroy(GtkWidget *window)
{
    gtk_main_quit();
    }
static gboolean delete_event(GtkWidget *window,GdkEvent *event)
{
    return FALSE;
    }

void newtab_activated(GtkWidget *widget)
{
    int pages = 0;
    GtkWidget *label1;
    label1 = gtk_label_new("New File");
    pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook));
    gtk_container_add(GTK_CONTAINER(scrollwin[pages]),txtinput[pages]);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook),scrollwin[pages],label1);
    }
   
void undo_activated(GtkWidget *widget)
{
    GtkSourceBuffer *buffer;
    buffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))])));
    gtk_source_buffer_undo(buffer);
    }

void redo_activated(GtkWidget *widget)
{
    GtkSourceBuffer *buffer;
    buffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))])));
    gtk_source_buffer_redo(buffer);
    }

void copy_activated(GtkWidget *widget)
{
    GtkClipboard *clipboard;
    GtkTextBuffer *buffer;
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
    clipboard = gtk_clipboard_get(GDK_NONE);
    gtk_text_buffer_copy_clipboard(buffer,clipboard);
    }

void cut_activated(GtkWidget *widget)
{
    GtkClipboard *clipboard;
    GtkTextBuffer *buffer;
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
    clipboard = gtk_clipboard_get(GDK_NONE);
    gtk_text_buffer_cut_clipboard(buffer,clipboard,TRUE);
    }

void paste_activated(GtkWidget *widget)
{
    GtkClipboard *clipboard;
    GtkTextBuffer *buffer;
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
    clipboard = gtk_clipboard_get(GDK_NONE);
    gtk_text_buffer_paste_clipboard(buffer,clipboard,NULL,TRUE);
    }
                 
void save_activated(GtkWidget *widget, GtkWindow *parent)
{
    GtkWidget *dialog;
    GtkTextIter start_iter, end_iter;
    GtkTextBuffer *buffer;
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
    dialog = gtk_file_chooser_dialog_new("Save File", parent, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_SAVE,GTK_RESPONSE_ACCEPT,NULL);
    if (gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT)
    {
        char *filename,*text;
        gssize length = -1;
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        gtk_text_buffer_get_end_iter(buffer,&end_iter);
        gtk_text_buffer_get_start_iter(buffer,&start_iter);
        text = gtk_text_buffer_get_text(buffer,&start_iter,&end_iter,FALSE);
        g_file_set_contents(filename,text,length,NULL);
        g_free(filename);
        g_free(text);
        }
    gtk_widget_destroy(dialog);
    }
   
void open_activated(GtkWidget *widget, GtkWindow *parent)
{
    GtkWidget *dialog;
    dialog = gtk_file_chooser_dialog_new("Open File", parent, GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);
    GtkTextBuffer *buffer;
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
    if(gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT)
    {
        char *filename,*string;
        gsize length = -1;
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        g_file_get_contents(filename,&string,&length,NULL);       
        gtk_text_buffer_set_text(buffer,string,-1);
        g_free(filename);
        g_free(string);
        }
    gtk_widget_destroy(dialog);
    }

void filenew_activated(GtkWidget *widget)
{
    GtkTextBuffer *buffer;
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
    gtk_text_buffer_set_text(buffer,"",-1);
    }




Here you can see the function newtab_activated. It is connected to newtab menu item. But whenever I clicked on New Tab in File Menu, nothing happens. Also please forgive me now for all those bad programming practices, I have followed till now, I will correct it later on. Now Please solve the problem!!
Thanks in advance


Top
 Profile  
 
 Post subject: Re: Create a new tab in GtkNotebook
PostPosted: Sun Oct 23, 2011 1:57 pm 
Offline
GTK+ Geek

Joined: Sun Apr 03, 2011 11:12 am
Posts: 63
problem solved, just added gtk_show_all(window);


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 4 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