2

I need entry controls to automatically select-all text when they receive focus. If you run the code and press tab to switch focus between the two controls, all of the text in the control is automatically selected. I need the same behavior when using the mouse to select the other control. My question is: what GTK event is signaled when an entry control is clicked with the mouse?

import Graphics.UI.Gtk

main :: IO ()
main = do
  initGUI

  vbox   <- vBoxNew False 4
  window <- windowNew
  set window [ containerBorderWidth := 8,
               containerChild := vbox ]

  mkEntry "Entry 1" vbox
  mkEntry "Entry 2" vbox

  onDestroy window mainQuit
  widgetShowAll window
  mainGUI


mkEntry :: String -> VBox -> IO Entry
mkEntry txt vbox = do
  entry <- entryNew
  entrySetText entry txt
  boxPackStart vbox entry PackNatural 0
  -- selects all text when tabbing into the control
  entry `on` entryActivate $ do editableSelectRegion entry 0 (-1)
  return entry
jaket
  • 9,140
  • 2
  • 25
  • 44
  • 1
    [Does this help?](http://stackoverflow.com/questions/10173190/gtk-2-0-signal-clicked-on-gtkentry) – huon Nov 22 '12 at 13:54
  • 2
    This question is not really haskell related, as it is about the gtk library itself. You might get more answers by not posing the question in a Haskell-specific way. – Joachim Breitner Nov 22 '12 at 18:10

1 Answers1

1

The main problem is that the click itself causes the entry selection to be changed by GTK+ itself. I got this working by

  1. Using focusInEvent as the trigger
  2. Changing the selection in an idle callback, once everything is sufficiently "calmed down"

Put together:

-- selects all text when tabbing into the control
on entry focusInEvent $ do
    liftIO $ flip idleAdd priorityDefaultIdle $ do
        editableSelectRegion entry 0 (-1)
        return False
    return True
Cactus
  • 27,075
  • 9
  • 69
  • 149