3

I have a Gtk.Notebook that has a custom popup menu that is displayed when the user right clicks on any of the page buttons.

How can I know which notebook page the user clicked on? I want to add an action in my menu to make it the current page.

notebook.button_press_event.connect( (wid,evt) => {
    if ( evt.button==3 ) {
        // which page button did the user click on?
        notebook.set_current_page( «clicked no tab» );
        // ... make it the current page
    }
}

I tried to find the tab by position:

int numtab = notebook.get_tab_at_pos((int)evt.x, (int)evt.y);

But there doesn't seem to be a get_tab_at_pos or similar method.

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
bul
  • 31
  • 4
  • So you want to call `gtk_notebook_set_current_page ()` when the user right clicks a tab? That's what the left mouse button is normally used for, so why do it with the right button as well? – Jens Mühlenhoff Sep 19 '16 at 11:19
  • because I displays a menu that will do actions on this tab and left mouse is useful to other actions, thank you for your intervention – bul Sep 19 '16 at 11:49
  • left mouse : switch tabs for example – bul Sep 19 '16 at 11:57
  • I'm still not sure that I have correctly understood what you want to do. You should add a full code example and/or a longer description of what you are trying to do and what the problem is you are stuck at. – Jens Mühlenhoff Sep 19 '16 at 12:17
  • difficult for me in English, but I would like to select the tab where I right click, to turn it CURRENT_PAGE. that's all – bul Sep 19 '16 at 12:53
  • you "left click" on a tab, the tab become current_page, I have a menu displayed on a right click that needs to do actions on this tab, so I have put the tab on which you right click in current_page – bul Sep 19 '16 at 13:01
  • tab 1 is current-tab, right click on tab 2, how to tab 2 in current-tab ? – bul Sep 19 '16 at 13:04
  • tab 1 is current-tab, right click on tab 2, I have to work on tab 2, so put in current-tab tab 2 – bul Sep 19 '16 at 13:09
  • Ok, I think I got it now. I will edit your question, so others can understand it. – Jens Mühlenhoff Sep 19 '16 at 14:36
  • What I still don't understand is `notebook.button_press_event` where did you get that from? The default `Gtk.Notebook` class doesn't have such an event?! – Jens Mühlenhoff Sep 19 '16 at 14:50

1 Answers1

2

One solution might be to use a Gtk.EventBox as suggested here (PHP code):

$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// setup notebook
$notebook = new GtkNotebook(); // note 1
$vbox->pack_start($notebook);

// add two tabs of GtkLabel
add_new_tab($notebook, new GtkLabel('Notebook 1'), 'Label #1');
add_new_tab($notebook, new GtkLabel('Notebook 2'), 'Label #2');

// add a thrid tab of GtkTextView
$buffer = new GtkTextBuffer();
$view = new GtkTextView();
$view->set_buffer($buffer);
$view->set_wrap_mode(Gtk::WRAP_WORD);
add_new_tab($notebook, $view, 'TextView');

$window->show_all();
Gtk::main();

// add new tab
function add_new_tab($notebook, $widget, $tab_label) {
    $eventbox = new GtkEventBox();
    $label = new GtkLabel($tab_label);
    $eventbox->add($label); // note 2
    $label->show(); // note 3
    $eventbox->connect('button-press-event', 'on_tab', $tab_label); // note 4
    $notebook->append_page($widget, $eventbox); // note 5
}

// function that is called when user click on tab
function on_tab($widget, $event, $tab_label) {  // note 6
    echo "tab clicked = $tab_label\n";
}
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
  • trying to understand, adapt, and I tell you (perhaps not immediately) thank you – bul Sep 20 '16 at 02:13
  • it works perfectly. it was a little more complicated, the title of the tab = picture + label, and loop in the tabs to find the right one. thanks again. – bul Sep 20 '16 at 08:38