tags:

views:

168

answers:

2

I have created one notebook GTK+ widget, and I am trying to set callbacks on some events as follows:

m_notebook = gtk_notebook_new();
g_signal_connect( GTK_OBJECT(m_notebook), "move-focus-out", G_CALLBACK( on_notebook_move_focus_out ), NULL );
g_signal_connect( GTK_OBJECT(m_notebook), "focus-tab", G_CALLBACK( on_notebook_focus_tab ), NULL );
g_signal_connect( GTK_OBJECT(m_notebook), "change-current-page", G_CALLBACK( on_notebook_change_current_page ), NULL );
gtk_container_add(GTK_CONTAINER (m_window), m_notebook);

And these are the prototypes for callback functions:

G_MODULE_EXPORT gboolean on_notebook_move_focus_out( GtkNotebook *notebook, GtkNotebookTab arg1, gpointer user_data );
G_MODULE_EXPORT gboolean on_notebook_focus_tab( GtkNotebook *notebook, GtkNotebookTab arg1, gpointer user_data );
G_MODULE_EXPORT gboolean on_notebook_change_current_page( GtkNotebook *notebook, gint arg1, gpointer user_data );

I am appending pages to note book at run time using this function call:

gtk_notebook_append_page( GTK_NOTEBOOK(m_notebook), msg_vbox, label );

But I am not receiving any of the above events. What am I doing wrong?

Relevant code is as follow:

    /*
 * Compile me with:

 gcc -o notebook notebook.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
*/

#include <gtk/gtk.h>

static gboolean on_m_notebook_move_focus_out( GtkNotebook *notebook, GtkDirectionType arg1, gpointer user_data );
static gboolean on_m_notebook_focus_tab( GtkNotebook *notebook, GtkNotebookTab arg1, gpointer user_data );
static gboolean on_m_notebook_change_current_page( GtkNotebook *notebook, gint arg1, gpointer user_data );

int main (int argc, char *argv[])
{
    GtkWidget *m_window;
    GtkWidget *m_notebook;

    gtk_init (&argc, &argv);

    m_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(m_window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(m_window), 350, 500);

    m_notebook = gtk_notebook_new();
    g_signal_connect( GTK_OBJECT(m_notebook), "move-focus-out", G_CALLBACK( on_m_notebook_move_focus_out ), NULL );
    g_signal_connect( GTK_OBJECT(m_notebook), "focus-tab", G_CALLBACK( on_m_notebook_focus_tab ), NULL );
    g_signal_connect( GTK_OBJECT(m_notebook), "change-current-page", G_CALLBACK( on_m_notebook_change_current_page ), NULL );

    gtk_container_add(GTK_CONTAINER (m_window), m_notebook);

    GtkWidget *button;

    button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
    gtk_notebook_append_page(GTK_NOTEBOOK(m_notebook), button, NULL);

    button = gtk_button_new_from_stock (GTK_STOCK_ADD);
    gtk_notebook_append_page(GTK_NOTEBOOK(m_notebook), button, NULL);

    button = gtk_button_new_from_stock (GTK_STOCK_DELETE);
    gtk_notebook_append_page(GTK_NOTEBOOK(m_notebook), button, NULL);

    button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
    gtk_notebook_append_page(GTK_NOTEBOOK(m_notebook), button, NULL);

    gtk_widget_show_all( m_window );

    gtk_main ();
    return 0;
}

gboolean on_m_notebook_move_focus_out( GtkNotebook *notebook, GtkDirectionType arg1, gpointer user_data )
{
    printf("on_m_notebook_move_focus_out\n");
    return FALSE;
}

gboolean on_m_notebook_focus_tab( GtkNotebook *notebook, GtkNotebookTab arg1, gpointer user_data )
{
    printf("on_m_notebook_focus_tab []\n");
    return FALSE;
}

gboolean on_m_notebook_change_current_page( GtkNotebook *notebook, gint arg1, gpointer user_data )
{
    printf("on_m_notebook_change_current_page\n");
    return FALSE;
}
+1  A: 

Typically pages are invisible after being added, which might be why the notebook doesn't react. Try also calling gtk_widget_show_all(GTK_WIDGET(msg_vbox)) to make it visible, and see if that triggers any events.

UPDATE: When you add new pages, if the notebook's current focus doesn't actually change so that the new page is displayed, I wouldn't expect it to emit the signals you mentioned. You can of course force it to show the newly added page by calling gtk_notebook_set_current_page(). The page number is returned by gtk_notebook_append_page() and friends.

UPDATE 2: This is a minor one, but you should cast your objects using G_OBJECT(), not GTK_OBJECT(), since the g_signal_connect() function is a glib function that acts on GObjects.

unwind
Here pages are visible after appended. Added gtk_widget_show_all(GTK_WIDGET(msg_vbox)) function call, It doesn't help to trigger events.
kbalar
I didn't get any warnings by changing signal names. Till I am not able to trigger the events.
kbalar
The dashes and underscores don't matter, as long as you don't mix them within one signal name.
ptomato
@ptomato: Thanks, I never knew. I'll remove that part of my answer then, since it shouldn't matter.
unwind
Casting to G_OBJECT() doesn't help. I am able to trigger "switch-page" signal.
kbalar
+1  A: 

You're connecting to the wrong signals. The ones you're using are keybinding signals. They are emitted when you press keys on the keyboard that move the focus out, focus a tab, or change the current page --- such as Page Up, Page Down, etc. Unfortunately there's really no way you could have known that, because these signals are not documented in the GTK docs.

Instead of change-current-page, use switch-page. And instead of the focus signals, connect to the various focus signals of GtkWidget.

ptomato
Yes, I am able to trigger "switch-page"signal.
kbalar