Gtk4: How to construct a traditional window top-level "Edit" menu?

,

I have the menu setup just fine, and it emits win.copy, win.cut, etc. actions, and I have action handlers that they invoke properly, but I’m not sure how to map these to the proper built-in actions in such a way that it works for whatever widget(s) is focused . (For example, in FF, if I highlight any text on this page, and choose “Copy” from the window’s top-level “Edit” menu, the selected text is copied to the clipboard.)

This tutorial comes close: Gtk4-tutorial/sec17.md at 20b4a16175ae3f510151702c8a377b63547b13d8 · ToshioCP/Gtk4-tutorial · GitHub but the handlers are just stubs: Gtk4-tutorial/menu3.c at 20b4a16175ae3f510151702c8a377b63547b13d8 · ToshioCP/Gtk4-tutorial · GitHub

Is there an example or tutorial you can point me towards?

Hello, the built-in actions are on the individual widgets and are prefixed with clipboard. In your action handler, you will usually want to chain to that action. A very simple example might look like this:

gtk_widget_activate_action (my_text_widget, "clipboard.copy". NULL);

You can also try to use gtk_window_get_focus to track the focus of the current window, but this may be unnecessary. Usually document-based applications will always activate on the main widget for that window.

Doesn’t work, unfortunately. I can’t see how to make gtk_widget_activate_action() work without giving it a widget, and the widget returned by gtk_window_get_focus()is the currently focused menu item button widget, not the entry (or other text) widget being edited.

1 Like

Right, if you want a more complex handling you will have to monitor the focus-widget property on the window and cache the most recent text widget.

Ok, that’s more or less doable. I thought there might be some easier way, but apparently not.

Also, I just found this relevant discussion: FR: cut/copy/paste interface for GTK

To anyone reading this: the solution is a bit more complex. You have to track focus, and look for a sequence of GtkPopoverMenuBarItem/GtkModelButton (possibly also a GtkScrolledWindow, and null focus) focuses just after the focus of a textual widget, then copy/paste/whatever to it, and then refocus that text widget. But this is made difficult since there are no GTK_ type check macros exposed for GtkPopoverMenuBarItem/GtkModelButton.

Just tracking the last text widget focused would cause incorrect behavior if focuses changes in a way that should invalidate it, e.g., switching the tab of a notebook.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.