 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
paragasu Familiar Face
Joined: 31 Jul 2008 Posts: 44
|
Posted: Mon Oct 06, 2008 2:17 am Post subject: replacing gtk_notebook page |
|
|
how replace a gtk notebook instead of appending a new page?
thank you |
|
| Back to top |
|
 |
dreblen Never Seen the Sunlight
Joined: 14 Jun 2007 Posts: 936 Location: Falun, WI USA
|
|
| Back to top |
|
 |
paragasu Familiar Face
Joined: 31 Jul 2008 Posts: 44
|
Posted: Mon Oct 06, 2008 2:41 am Post subject: |
|
|
well.. thank you.. but still need one more question to ask :D
how to make sure no duplicate page in the notebook.
i have this function, which create a notebook page every time the
user click on a list of selection (treeview). but the problem come when
user click on selection more than one time.
As a result there is exist a more than one page that contain same information. How to find out, the page with that information is already
exists so i do not need to insert anymore page. Or how do i know if the page exist so i can replace it?
thank you |
|
| Back to top |
|
 |
dreblen Never Seen the Sunlight
Joined: 14 Jun 2007 Posts: 936 Location: Falun, WI USA
|
Posted: Mon Oct 06, 2008 3:05 am Post subject: |
|
|
Maybe I'm missing something, but isn't that pretty much the same question?
As an alternative, could you re-create the entire GtkNotebook every time the button is pressed? |
|
| Back to top |
|
 |
paragasu Familiar Face
Joined: 31 Jul 2008 Posts: 44
|
Posted: Mon Oct 06, 2008 3:22 am Post subject: ups |
|
|
actually, not recreate the entire gtk notebook? my mistake..
it just create a new page and append to the existing notebook every time i double click the tree view row. |
|
| Back to top |
|
 |
ramesh GTK+ Guru
Joined: 16 Nov 2006 Posts: 270 Location: INDIA
|
|
| Back to top |
|
 |
tadeboro Never Seen the Sunlight
Joined: 23 Jul 2008 Posts: 2114 Location: Slovenia
|
Posted: Mon Oct 06, 2008 8:45 am Post subject: |
|
|
Hi.
Below is some code which does approximately the same thing as your (I'm using buttons instead of treeview). If you have any questions regarding it, just ask.
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <gtk/gtk.h>
typedef struct _MyWid MyWid;
struct _MyWid
{
GtkWidget *notebook;
gint *content;
int max_size;
};
void
rem_page( GtkWidget *button,
MyWid *widgets )
{
gint cur_page;
cur_page = gtk_notebook_get_current_page( GTK_NOTEBOOK( widgets->notebook ) );
gtk_notebook_remove_page( GTK_NOTEBOOK( widgets->notebook ), cur_page );
*(widgets->content + cur_page) = -1;
}
void
cb_clicked( GtkWidget *button,
MyWid *widgets )
{
const gchar *string;
gint id;
register int i;
GtkWidget *del_button;
GtkWidget *label;
id = GPOINTER_TO_INT( g_object_get_data( G_OBJECT( button ), "id" ) );
for( i = 0; i < widgets->max_size; i++ )
{
if( *(widgets->content + i) == id )
return;
}
string = gtk_button_get_label( GTK_BUTTON( button ) );
label = gtk_label_new( string );
del_button = gtk_button_new_with_label( "Remove this page" );
g_signal_connect( G_OBJECT( del_button ), "clicked",
G_CALLBACK( rem_page ), widgets );
gtk_notebook_append_page( GTK_NOTEBOOK( widgets->notebook ),
del_button, label );
gtk_widget_show_all( widgets->notebook );
*(widgets->content + id) = id;
}
int
main( int argc,
char **argv )
{
GtkWidget *window;
GtkWidget *hbox;
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *notebook;
register int i;
MyWid widgets;
char *labels[] = { "button 1", "button 2", "button 3", "button 4", "button 5" };
gtk_init( &argc, &argv );
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_default_size( GTK_WINDOW( window ), 500, -1 );
g_signal_connect( G_OBJECT( window ), "delete-event",
G_CALLBACK( gtk_main_quit ), NULL );
hbox = gtk_hbox_new( FALSE, 5 );
gtk_container_add( GTK_CONTAINER( window ), hbox );
vbox = gtk_vbox_new( FALSE, 5 );
gtk_box_pack_start( GTK_BOX( hbox ), vbox, FALSE, TRUE, 0 );
for( i = 0; i < 5; i++ )
{
button = gtk_button_new_with_label( labels[i] );
gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, TRUE, 0 );
g_object_set_data( G_OBJECT( button ), "id", GINT_TO_POINTER( i ) );
g_signal_connect( G_OBJECT( button ), "clicked",
G_CALLBACK( cb_clicked ), &widgets );
}
widgets.content = g_malloc( sizeof( gint ) * i );
widgets.max_size = i;
for( i = 0; i < 5; i++ )
widgets.content[i] = -1;
notebook = gtk_notebook_new();
gtk_box_pack_start( GTK_BOX( hbox ), notebook, TRUE, TRUE, 0 );
widgets.notebook = notebook;
gtk_widget_show_all( window );
gtk_main();
return( 0 );
} | |
|
| Back to top |
|
 |
paragasu Familiar Face
Joined: 31 Jul 2008 Posts: 44
|
Posted: Mon Oct 06, 2008 12:11 pm Post subject: question |
|
|
| Code: (C) | 1 2 3
|
*(widgets->content + cur_page) = -1;
|
what does the code do? |
|
| Back to top |
|
 |
tadeboro Never Seen the Sunlight
Joined: 23 Jul 2008 Posts: 2114 Location: Slovenia
|
Posted: Mon Oct 06, 2008 5:08 pm Post subject: Re: question |
|
|
| paragasu wrote: | | Code: (C) | 1 2 3
|
*(widgets->content + cur_page) = -1;
|
what does the code do? | There is some pointer-arithmetic magic inside this piece of code. The above code is equivalent of:
| Code: (C) | 1
| widgets->content[cur_page] = -1 | As for the "-1", I used this value as a marker that a particular notebook page is not shown.
I also noticed that I made some thins for no reason. If you wish I can streamline my code a bit. |
|
| Back to top |
|
 |
paragasu Familiar Face
Joined: 31 Jul 2008 Posts: 44
|
Posted: Tue Oct 07, 2008 12:46 am Post subject: yes please |
|
|
| Quote: | | also noticed that I made some thins for no reason. If you wish I can streamline my code a bit. |
yes please. i am glad if you can post a simpler code. thanks |
|
| Back to top |
|
 |
tadeboro Never Seen the Sunlight
Joined: 23 Jul 2008 Posts: 2114 Location: Slovenia
|
Posted: Tue Oct 07, 2008 7:05 am Post subject: |
|
|
Hi,
below is the simplest code I'm able to write and still achieves what is desired. I also added some comments, so it is easier to understand (or at least that was my intention;).
And the code: | Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #include <gtk/gtk.h>
typedef struct _MyWid MyWid;
struct _MyWid
{
GtkWidget *notebook;
gboolean *content;
int max_size;
};
void
rem_page( GtkWidget *button,
MyWid *widgets )
{
gint cur_page;
/* delete current page */
cur_page = gtk_notebook_get_current_page( GTK_NOTEBOOK( widgets->notebook ) );
gtk_notebook_remove_page( GTK_NOTEBOOK( widgets->notebook ), cur_page );
/* set contents indicator to FALSE, so the corresponding button
* can again create this page.
*/
widgets->content[cur_page] = FALSE;
}
void
cb_clicked( GtkWidget *button,
MyWid *widgets )
{
const gchar *string;
gint id;
register int i;
GtkWidget *del_button;
GtkWidget *label;
/* retrieve ID number from button, calling the function */
id = GPOINTER_TO_INT( g_object_get_data( G_OBJECT( button ), "id" ) );
/* if this button has already created a page, simply return (do
* not add another page).
*/
if( widgets->content[id] == TRUE )
return;
/* Get label from button and create notebook page */
string = gtk_button_get_label( GTK_BUTTON( button ) );
label = gtk_label_new( string );
del_button = gtk_button_new_with_label( "Remove this page" );
g_signal_connect( G_OBJECT( del_button ), "clicked",
G_CALLBACK( rem_page ), widgets );
gtk_notebook_append_page( GTK_NOTEBOOK( widgets->notebook ),
del_button, label );
gtk_widget_show_all( widgets->notebook );
/* Set the content flag to TRUE (this button has already added
* it's page).
*/
widgets->content[id] = TRUE;
}
int
main( int argc,
char **argv )
{
GtkWidget *window;
GtkWidget *hbox;
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *notebook;
register int i;
MyWid widgets;
/* labels for my buttons and notebook pages */
char *labels[] = { "button 1", "button 2", "button 3", "button 4", "button 5" };
/* init gtk and create GUI elements */
gtk_init( &argc, &argv );
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_default_size( GTK_WINDOW( window ), 500, -1 );
g_signal_connect( G_OBJECT( window ), "delete-event",
G_CALLBACK( gtk_main_quit ), NULL );
hbox = gtk_hbox_new( FALSE, 5 );
gtk_container_add( GTK_CONTAINER( window ), hbox );
vbox = gtk_vbox_new( FALSE, 5 );
gtk_box_pack_start( GTK_BOX( hbox ), vbox, FALSE, TRUE, 0 );
for( i = 0; i < 5; i++ )
{
button = gtk_button_new_with_label( labels[i] );
gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, TRUE, 0 );
/* Assign ID number to each button, so we can distinguish them.
* This ID also tells us, where in content array to look for
* a indicator flag. First button gets ID = 0, second ID = 1 ...
*/
g_object_set_data( G_OBJECT( button ), "id", GINT_TO_POINTER( i ) );
g_signal_connect( G_OBJECT( button ), "clicked",
G_CALLBACK( cb_clicked ), &widgets );
}
/* allocate space for my tracking array */
widgets.content = g_malloc( sizeof( gboolean ) * i );
widgets.max_size = i;
/* set initial values in array to FALSE (no pages are show) */
for( i = 0; i < 5; i++ )
widgets.content[i] = FALSE;
notebook = gtk_notebook_new();
gtk_box_pack_start( GTK_BOX( hbox ), notebook, TRUE, TRUE, 0 );
widgets.notebook = notebook;
gtk_widget_show_all( window );
gtk_main();
return( 0 );
} | If you have any qusetions, just ask. |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|