0

I'm building a small Haskell GUI application as an exercise for myself using the gtk2hs library, but I'm stuck at a certain point. So I want to show a FileChooserDialog where a user can select multiple images, and I made a function that does all of this, but now I cannot think of a way to return these values to the main function so that these can work with the selected files. This is my main function that creates main window:

main :: IO ()
main = do
    initGUI
    window <- windowNew
    set window [windowDefaultWidth := 300, windowDefaultHeight := 150, containerBorderWidth := 10]
    table <- tableNew 3 4 True
    containerAdd window table
    startButton <- buttonNewWithLabel "Start"
    tableAttachDefaults table startButton 0 4 2 3
    selectButton <- buttonNewWithLabel "Select images"
    tableAttachDefaults table selectButton 0 2 0 1
    selectedLabel <- labelNew (Just "0 images selected")
    tableAttachDefaults table selectedLabel 2 4 0 1
    onClicked selectButton (selectImages selectedLabel window)
    shiftLabel <- labelNew (Just "Days to shift: ")
    -- Left align text in label
    miscSetAlignment shiftLabel 0.0 0.5
    tableAttachDefaults table shiftLabel 0 3 1 2
    adjustment <- adjustmentNew 0.0 (-5000.0) 5000.0 1.0 10.0 0.0
    spinner <- spinButtonNew adjustment 0.5 0
    tableAttachDefaults table spinner 3 4 1 2
    widgetShowAll window
    mainGUI

The function that shows the FileChooserDialog is the selectImages function and is an onClick function for the selectButton. This function works like this:

selectImages :: LabelClass self => self -> Window -> IO ()
selectImages resultLabel parent = do
    chooser <- fileChooserDialogNew (Just "Select images") (Just parent) FileChooserActionOpen [("Cancel", ResponseCancel), ("Open", ResponseAccept)]
    fileChooserSetSelectMultiple chooser True
    widgetShow chooser
    response <- dialogRun chooser
    case response of
        ResponseCancel -> putStrLn "Cancelled"
        ResponseAccept -> do
            files <- fileChooserGetFilenames chooser
            labelSetText resultLabel $ (show (length files)) ++ " images selected"
    widgetDestroy chooser

Now what I want to do, is to be able to return the files array from the selectImages function to the main function for further processing. How can I do this? Should I somehow use the StateT transformer? Or something else?

  • change the signature to `IO Files` (don't know the type of your Files so look for it), move `widgetDestroy ...` into both `case`s and then use `return files` right below them both – Random Dev Jul 18 '16 at 18:54
  • @Carsten: thank you! But how can I access these from the onClicked function in the main function? –  Jul 18 '16 at 19:01
  • Possible duplicate of [How to deal with application state in Gtk2Hs](http://stackoverflow.com/q/12002814/791604). Does the answer there address your concerns? If not, why not? – Daniel Wagner Jul 18 '16 at 19:11
  • @Daniel Wagner I think the MVar's might work! Thank you! –  Jul 18 '16 at 19:15

0 Answers0