After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 313273 - Implement gtk_notebook_get_page_at_pos()
Implement gtk_notebook_get_page_at_pos()
Status: RESOLVED OBSOLETE
Product: gtk+
Classification: Platform
Component: Widget: GtkNotebook
2.6.x
Other Linux
: Normal enhancement
: Small API
Assigned To: gtk-bugs
gtk-bugs
Depends on:
Blocks:
 
 
Reported: 2005-08-11 22:42 UTC by gbz
Modified: 2018-02-10 03:37 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
patch (4.54 KB, patch)
2006-10-12 16:18 UTC, Carlos Garnacho
none Details | Review
updated patch, fixes those issues (6.05 KB, patch)
2006-11-15 00:23 UTC, Carlos Garnacho
none Details | Review

Description gbz 2005-08-11 22:42:02 UTC
When a custom popup menu for the tabs is needed, the first that comes to mind
would be to put an eventbox above the label widget of every tab, and check for
the "button-press-event" event.
But that method doesn't catch clicks that are at the tab, but outside the label
widget (the "tab-border" property of GtkNotebook). And also, it would be a waste
to add a lot of event windows, when GtkNotebook already has one.
So if there was a function like:
  gint page_num        gtk_notebook_get_page_at_pos(GtkNotebook *notebook,
                                                    gint x,
                                                    gint y);
- it would be possible to know what tab is clicked on, providing the event->x
and event->y. It should return some negative value if the click is not at a tab.
Comment 1 gbz 2005-08-11 22:53:30 UTC
To clarify a few things:
Instead of connecting the event box above the label to "button-press-event", you
would connect the GtkNotebook instead.
And if there is a negative return value from gtk_notebook_get_page_at_pos(), it
might be considered not to show the popup menu.
Comment 2 Matthias Clasen 2005-08-14 14:28:59 UTC
Should that not be gtk_notebook_get_tab_at_pos() then ?
Comment 3 Matthias Clasen 2006-03-21 16:55:21 UTC
Carlos, doesn't one of your outstanding patches add a (private)
gtk_notebook_get_tab_at_pos() function ? 
Might be worth to export it.
Comment 4 Carlos Garnacho 2006-03-21 17:22:59 UTC
Yeah, it's implemented in patch from bug #332991, once we figure out what to do with it, I'll make a patch to export it
Comment 5 Carlos Garnacho 2006-05-10 18:39:51 UTC
Hmm, while implementing this I've found a small dilemma, using widget coordinates seem to be the most obvious (other *_at_pos() functions already do that), but if you connect to say ::button-press-event and use this function, the event coordinates will be relative to notebook->event_window, used for catching tabs events.

One solution would be adding gtk_notebook_window_to_tabs_coords() and gtk_notebook_tabs_to_window_coords(), but exposing this implementation detail sucks completely.

Another solution would be assuming that the coords will be related to notebook->event_window, given that this function would be mostly used in event handlers, but it would fail "misteriously" if it's used outside one of these with known coordinates.
Comment 6 Matthias Clasen 2006-05-11 13:46:03 UTC
I think documenting that the coordinates are relative to event_window, together
with a small example showing how to use it in an event handler would be acceptable
Comment 7 Owen Taylor 2006-05-11 15:13:45 UTC
If you take that approach, don't you need to provide a getter
for event_window a) for language bindability b) because event_window
isn't documented as a public field?
Comment 8 Owen Taylor 2006-05-11 15:17:11 UTC
Well, I suppose if you just assume that whenever you get an event
on the Notebook it is in event window coordinates, you are OK, but
it seems messy.

I wonder if we could have:

 gtk_widget_get_event_position(GtkWidget *widget, GdkEvent *event, int *x, int *y);

that translated the event coordinates into widget coordinates - 
that *is* possible to do generically, though its somewhat tricky.

(I'd make that suggestion more enthusiastically, if "widget coordinates"
weren't so weirdly defined...)
Comment 9 Owen Taylor 2006-05-11 15:18:39 UTC
Continuing to talk to myself: "assuming that whenever you get an event
it is in event window coordinates" is simply wrong, because of the
inheritance of button press events up the widget heirarchy when not
handled.
Comment 10 Carlos Garnacho 2006-10-12 16:18:38 UTC
Created attachment 74578 [details] [review]
patch

This patch implements gtk_widget_get_event_position() and gtk_notebook_get_tab_at_pos(), with this, all that an event handler should do to know the tab below the pointer is:

gint x, y, position;

gtk_widget_get_event_position (widget, event, &x, y);
position = gtk_notebook_get_tab_at_pos (GTK_NOTEBOOK (widget), x, y);
Comment 11 Matthias Clasen 2006-11-14 16:10:29 UTC
 g_return_val_if_fail (gdk_event_get_coords (event, &event_x, &event_y), FALSE);

You can't rely on side-effects of return-val-if-fail.


+ * Return Value: #FALSE if the widget is not realized, the widget and
+ * the event window do not share a common toplevel, or the event doesn't
+ * deliver event window coordinates.

I don't see any code to return FALSE if the even does not deliver event
window coordinates. The documentation should probably mention for which
types of common events this does work, like mouse events.
Comment 12 Carlos Garnacho 2006-11-15 00:23:38 UTC
Created attachment 76610 [details] [review]
updated patch, fixes those issues
Comment 13 Matthias Clasen 2006-11-15 18:45:15 UTC
Yes, looks better on that count. 

But I have to admit that I'm not really sure that it returns the correct values in all cases, since as Owen said, widget coordinates are wierd.
Comment 14 Carlos Garnacho 2006-11-15 19:34:50 UTC
as far as I can tell, it would always return coordinates relative to the widget allocation, was done following the same semantics than gtk_widget_translate_coordinates(), even uses it if the event which receives the event and the passed widget aren't the same.

Oh, perhaps allocation relative coordinates isn't always equivalent to widget coordinates? AFAICS gtk_widget_translate_coordinates() tries to make clear that it's using the former, should just a doc change making this clear be ok?
Comment 15 Philip Withnall 2007-05-26 16:35:50 UTC
Patch 76610 applies cleanly. Reviewing it would take a little while.

(Working on http://mail.gnome.org/archives/gtk-devel-list/2007-March/msg00148.html)
Comment 16 Murray Cumming 2008-04-01 07:52:56 UTC
Could a maintainer review the patch, please?
Comment 17 Christian Dywan 2008-07-23 01:26:45 UTC
To me the proposed approach seems a little awkward, particularly when looking back at the original use case. What about (a) signal(s) being emitted whenever a mouse button is pressed over a tab label? One argument could be the page index.
Comment 18 Matthias Clasen 2018-02-10 03:37:18 UTC
We're moving to gitlab! As part of this move, we are closing bugs that haven't seen activity in more than 5 years. If this issue is still imporant to you and
still relevant with GTK+ 3.22 or master, please consider creating a gitlab issue
for it.