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 Dec 23, 2014 1:35 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: notebook +textview -- - help~~?
PostPosted: Thu Mar 15, 2012 9:52 am 
Offline

Joined: Thu Mar 15, 2012 9:50 am
Posts: 2
When switch notebook letter, it doesn't show textview. This is the code:
Code:
#include <gtk/gtk.h>
GtkWidget *txtview;
GtkWidget *win;
void refresh_textview( GtkWidget *win )
{
   GtkTextBuffer *buffer;
   GtkTextIter iter;
   GtkTextMark *mark;
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (win));
   mark = gtk_text_buffer_get_insert (buffer); //
   gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
   gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW (win),mark);
   gtk_text_buffer_insert (buffer, &iter, "Buffer", -1);
   gtk_widget_draw(GTK_WIDGET(win),NULL);   
}

void clicked_cb(GtkWidget *button, GtkNotebook *nb)
{
   GtkWidget *lbl;//, *txtview;
   gchar str[256];
   static gint n = 0;

   sprintf(str, "Page %d", ++n);
   
   lbl = gtk_label_new(str);
   gtk_widget_show(lbl);
   txtview = gtk_text_view_new();
   refresh_textview(txtview);

   gtk_notebook_append_page(nb, txtview, lbl);  //加入页面

   gtk_widget_show(txtview);
   gtk_widget_show(GTK_WIDGET(nb));
   gtk_widget_queue_draw(GTK_WIDGET(nb));
}

void get_pagenum(GtkWidget *widget,GtkWidget *notebook)
{   
    GtkWidget *dialog,*window;
    gint n;
    n=gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook));
   
    window=gtk_widget_get_toplevel(widget);
    dialog=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_MODAL||GTK_DIALOG_NO_SEPARATOR,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"PageNum:%d",n);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
    g_print("%d\n",n);
}

gboolean refreshwin(gpointer notebook)
{
   int num;
   int pagenum;
   pagenum=gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook));
   num = gtk_notebook_current_page(GTK_NOTEBOOK(notebook));
   g_print("Current Num:%d AllPagenum = %d\n",num,pagenum);
   g_print("Refreshwin\n");
   gtk_widget_draw(notebook,NULL);
   gtk_widget_show(notebook);
   gtk_widget_show(win);
   return TRUE;
}

int main(int argc, char **argv)
{
   GtkWidget *vbox;
   GtkWidget *nb;
   GtkWidget *button;
   
   gtk_init(&argc, &argv);
   
   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_default_size(GTK_WINDOW(win),400,300);
   g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
   
   vbox = gtk_vbox_new(FALSE, 0);
   gtk_container_add(GTK_CONTAINER(win), vbox);
   gtk_widget_show(vbox);
   
   nb = gtk_notebook_new();

   g_signal_connect(G_OBJECT(nb),"switch_page",G_CALLBACK(refreshwin),nb);

   gtk_box_pack_start(GTK_BOX(vbox), nb, TRUE, TRUE, 0);
   gtk_widget_show(nb);
   
   button = gtk_button_new_with_label("Add Page");
   g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(clicked_cb), (gpointer)nb);
   gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);
   
   gtk_widget_show(win);   
   gtk_main();   
   return 0;
}


Top
 Profile  
 
 Post subject: Re: notebook +textview -- - help~~?
PostPosted: Thu Mar 15, 2012 11:13 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
I don't understand your question at all but a run of your code I'll assume you're talking about the values sent to g_print not being what you want as that seems to be the only error. I'm actually very surprised this compiles. I guess gcc is a lot cleverer than I thought. There are a lot of errors. Here is some that I've corrected:

* in refresh_txtview you have win as a local scope variable but have declared it differently as a global variable above.
* You use sprintf but don't include string.h. actually you are better not including this and use the glib functions (g_sprintf etc.)

* in clicked_cb there is no need to redo gtk_widget_show on the notebook or queue a draw, this will happen automatically when control is passed back to gtk_main. Similarly with all the other refreshing you are doing.
* Also here you have listed the second arguement as GtkNotebook* when it should be gpointer and then cast as needed.
* Your refreshwin function is declared incorrectly

The textview displays exactly how it should so I don't know why you mention it in your question. Did you want the g_print to go to the textview? I'm not sure whether you really want to use g_print, it send info to stdout which on most machines is the terminal, i.e. it won't be seen unless you run the program from the terminal. You would likely be much better setting up a statusbar and pushing the text there each time instead.

The /n you are putting on the end of the strings. You would be better off setting up a macro with conditional compilation to capture both the unix and Windows versions of line endings.

Here is a correction of the code (still using g_print: run from the terminal to see its output)

Code:
#include <gtk/gtk.h>
void clicked_cb(GtkWidget *widget, gpointer data)
{
gchar str[16];
gint n;
GtkTextBuffer *buffer;
GtkTextIter iter;
GtkTextMark *mark;
GtkWidget *lbl, *txtview;

n=gtk_notebook_get_n_pages(GTK_NOTEBOOK(data));
g_snprintf(str, 15, "Page %d", ++n);
lbl = gtk_label_new(str);
gtk_widget_show(lbl);
txtview = gtk_text_view_new();
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtview));
mark = gtk_text_buffer_get_insert(buffer);
gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(txtview),mark);
gtk_text_buffer_insert(buffer, &iter, "Buffer", -1);
gtk_widget_show(txtview);
gtk_notebook_append_page(GTK_NOTEBOOK(data), txtview, lbl); //加入页面
}

void refreshwin(GtkWidget* widget, gpointer data)
{
gint num;
gint pagenum;
pagenum=gtk_notebook_get_n_pages(GTK_NOTEBOOK(widget));
num = gtk_notebook_current_page(GTK_NOTEBOOK(widget));
g_print("Current Page Number: %d, Total Number of Pages = %d\n", ++num, pagenum);
}

int main(int argc, char **argv)
{
GtkWidget *win;
GtkWidget *vbox;
GtkWidget *nb;
GtkWidget *button;

gtk_init(&argc, &argv);
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(win),400,300);
g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(win), vbox);
gtk_widget_show(vbox);
nb = gtk_notebook_new();
g_signal_connect_after(G_OBJECT(nb),"switch_page",G_CALLBACK(refreshwin),NULL);
gtk_box_pack_start(GTK_BOX(vbox), nb, TRUE, TRUE, 0);
gtk_widget_show(nb);
button = gtk_button_new_with_label("Add Page");
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(clicked_cb), (gpointer)nb);
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
gtk_widget_show(button);
gtk_widget_show(win);
gtk_main();
return 0;
}


Top
 Profile  
 
 Post subject: Re: notebook +textview -- - help~~?
PostPosted: Fri Mar 16, 2012 12:57 am 
Offline

Joined: Thu Mar 15, 2012 9:50 am
Posts: 2
Code:
#include <gtk/gtk.h>
void insert_textview( GtkWidget *win )
{
   GtkTextBuffer *buffer;
//   GtkTextIter iter;
   GtkTextMark *mark;
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (win));
   mark = gtk_text_buffer_get_insert (buffer); //
//   gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
//   gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW (txtview),mark);
//  gtk_text_buffer_insert (buffer, &iter, "Buffer", -1);
//   gtk_text_view_set_buffer(GTK_TEXT_VIEW(txtview),buffer);
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (win));   
   gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1);
   gtk_widget_draw(GTK_WIDGET(win),NULL);   
}

void clicked_cb(GtkWidget *button, GtkNotebook *nb)
{
   GtkWidget *lbl;//, *txtview;
   gchar str[256];
   static gint n = 0;
   GtkWidget *frame;
   GtkWidget *txtview;
   sprintf(str, "Page %d", ++n);
   
   lbl = gtk_label_new(str);
   gtk_widget_show(lbl);
   txtview = gtk_text_view_new();
   insert_textview(txtview); // insert text
   
   frame = gtk_frame_new(NULL);
   gtk_container_add(GTK_CONTAINER(frame),txtview);   
   gtk_widget_show(frame);

   gtk_notebook_append_page(nb, frame, lbl);  //add the page

   gtk_widget_show(txtview);
   gtk_widget_show(GTK_WIDGET(nb));
   gtk_widget_queue_draw(GTK_WIDGET(nb));
}

void get_pagenum(GtkWidget *widget,GtkWidget *notebook)
{   
    GtkWidget *dialog,*window;
    gint n;
    n=gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook));
   
    window=gtk_widget_get_toplevel(widget);
    dialog=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_MODAL||GTK_DIALOG_NO_SEPARATOR,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"PageNum:%d",n);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
    g_print("%d\n",n);
}

void switch_notebook(GtkNotebook *notebook,gpointer page,guint page_num,gpointer user_data)
{   
//   gtk_signal_connect(GTK_OBJECT(nb),"expose_event ", GTK_SIGNAL_FUNC(refresh), nb);
   printf("on_notebook1_switch_page()page_num=%d\n",page_num);
   printf("on_notebook1_switch_page()page=%d\n",page);
//   gtk_notebook_set_current_page(GTK_NOTEBOOK(user_data), page);
   
}

static void change_screen_type(GtkWidget *widget, gpointer user_data)
{
   GtkWidget *nb;//
    GtkWidget *w;
    gint page_num;
   g_print("Change_screen_type.\n");
    w = GTK_WIDGET(user_data);
    page_num = gtk_notebook_page_num(GTK_NOTEBOOK(nb), w);
    gtk_notebook_set_current_page(GTK_NOTEBOOK(nb), page_num);
}
int main(int argc, char **argv)
{
   GtkWidget *vbox, *win,  *button, *nb;

   gtk_init(&argc, &argv);
   
   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_default_size(GTK_WINDOW(win),400,300);
   g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
   
   vbox = gtk_vbox_new(FALSE, 0);
   gtk_container_add(GTK_CONTAINER(win), vbox);
   gtk_widget_show(vbox);
   
   nb = gtk_notebook_new();

   g_signal_connect(G_OBJECT(nb),"switch_page",G_CALLBACK(switch_notebook),nb);

   gtk_box_pack_start(GTK_BOX(vbox), nb, TRUE, TRUE, 0);
   gtk_widget_show(nb);
   
   button = gtk_button_new_with_label("Add Page");
   g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(clicked_cb), (gpointer)nb);
   gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);
   
   gtk_widget_show(win);   
   gtk_main();   
   return 0;
}

    txtview = gtk_text_view_new();
   insert_textview(txtview); // insert text
   frame = gtk_frame_new(NULL);
   gtk_container_add(GTK_CONTAINER(frame),txtview);   
   gtk_widget_show(frame);
   gtk_notebook_append_page(nb, frame, lbl);  //add the page

I add a textview to notebook's page, every textview can show from notebook, but if I switch the page of notebook, it will not show the textview in the old notebook's page. Thanks


Top
 Profile  
 
 Post subject: Re: notebook +textview -- - help~~?
PostPosted: Fri Mar 16, 2012 7:29 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Hello,

It is very unclear what it is you want and how we can help you. First I assume that you are using GTK+ 2 due to the types of functions you are using and signals.

First you are using API calls that have been deprecated since GTK+ 2 was introduced (the transition from 1 to 2) the main one being gtk_widget_draw() which in GTK+ 3 has been totally changed and has a different calling arguments. Also gtk_widget_draw() and gtk_widget_queue_draw() are not needed and just add in-efficiencies.

Your basic C programming has many errors. Such as passing a pointer to a function when it is expecting an int.

I would first suggest that you compile you code using all the error checking your compiler can give you so that you can find and correct you coding errors or us one of the many tools out there which can check code for you.

_________________
E.


Top
 Profile  
 
 Post subject: Re: notebook +textview -- - help~~?
PostPosted: Fri Mar 16, 2012 9:41 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
It doesn't work because you have changed back a number of corrections that I have fixed (and given reasons for why they don't work) as well as introducing new ones.
As long as you are continuing to make fundamental errors, having people on this forum look at your code will be a incredibly tedious process.
Please take the time to read and understand the advice given.

1) Ensure you are using the correct function prototypes. Virtually every callback should be of the form void func_name(GtkWidget*, gpointer)
2) Brush up on your C programming and understand the difference between local and global variables, and especially understand the issue of variable scope.


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

All times are UTC


Who is online

Users browsing this forum: No registered users 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