Search code examples
haskellwxhaskell

How do I focus a widget?


When I start my application I want to set the focus on a certain button. Currently I need to hit tab once to have the button focused.

The wxwidgets documents mention a SetFocus method (link) which doesn't seem to be available in wxhaskell?

Then I found MoveBeforeInTabOrder (link) but again, I didn't find it in wxhaskell.

A maintainer of wxhaskell mentioned it is a 'fairly complete GUI binding' in 2009 so am I just missing something here or is it bad luck?

Here is my minimal example:

module Main where

import Graphics.UI.WX
import Graphics.UI.WXCore

main :: IO ()
main = start hello

hello :: IO ()
hello = do
  f <- frame []
  test <- button f [ text := "button" ]
  set f [ layout := widget test ]

Solution

  • The wx library has a focusOn function that allows you to focus on a control.

    It is a re-export of wxcore's windowSetFocus. The type is a bit misleading: it says Window a, but it works for buttons because they are windows too.

    The following works (note I only added the last line):

    module Main where
    
    import Graphics.UI.WX
    import Graphics.UI.WXCore
    
    main :: IO ()
    main = start hello
    
    hello :: IO ()
    hello = do
      f <- frame []
      test <- button f [ text := "button" ]
      set f [ layout := widget test ]
      focusOn test                          -- Here!