6

I have a collection of GtkEntry widgets, some of which are editable and focusable, and some of which are not. I would like to find out which, if any, of them currently has text selected, in order to implement an Edit->Copy menu item. Is there any way to do this other than iterating over all the widgets until gtk_editable_get_selection_bounds returns true?

I am currently sidestepping the issue by calling gtk_clipboard_get(GDK_SELECTION_PRIMARY) but from what the docs say, that's not portable (and will also pick up text selected anywhere within the current display, not just from within my application).

Martin DeMello
  • 11,876
  • 7
  • 49
  • 64
  • Unfortunately there doesn't seem to be a signal when a widget claims a selection, but you might be able to use the same "selection-get" signal handler for each entry, and then fake a selection conversion so you get called for the entry with the selection (if any). – ergosys May 09 '12 at 19:11
  • ergosys: what do you mean by fake a selection conversion? – Martin DeMello May 09 '12 at 19:40
  • Request a selection conversion without actually intending to receive the data. I.e. call gtk_selection_convert(), but have no (or maybe a stub) "selection-received" signal handler. – ergosys May 09 '12 at 19:54
  • gtk_selection_convert() still needs me to specify which selection i want to receive, which leads to the same problem as gtk_clipboard_get(). as far as i can make out, there is no windows equivalent of GDK_SELECTION_PRIMARY - i'm not sure if windows itself doesn't let you see when something is selected but not copied to the clipboard, or if gtk just doesn't expose that functionality outside of x. – Martin DeMello May 09 '12 at 20:59

1 Answers1

4

Have you tried gtk_window_get_focus ()? Users are frequently interacting with entries, so it may work for you. The documentation says that it "retrieves the current focused widget within the window." You can look it by yourself here. Then, compare if the widget retrieved is one of your entries.

Once you get the focused entry, perhaps you would like to get its text using gtk_entry_get_text () , though, it will get all the text in the entry. If this does not fit your purposes, the solution might be using gtk_editable_copy_clipboard () which copies the contents of the currently selected content in the editable (of course, cast the entry to editable) and puts it on the clipboard. Then if it applies, paste what was copied using gtk_editable_paste_clipboard ().

PALEN
  • 2,784
  • 2
  • 23
  • 24