11 import qualified Graphics
.UI
.Gtk
as G
15 configs
:: [(String, Config
)]
16 configs
= [("Beginner", beginner
),
17 ("Intermediate", intermediate
),
20 selectConfig
:: G
.RadioMenuItem
-> Config
-> Globals
-> IO ()
21 selectConfig
item cfg g
= do
22 active
<- G
.checkMenuItemGetActive
item
24 modifyIORef
(gState g
) $ \s
-> s
{ sPreviousConfigItem
= Just
item }
26 s
<- readIORef
(gState g
)
27 when (sConfig s
/= cfg
) $ do
28 writeIORef
(gState g
) s
{ sConfig
= cfg
}
30 G
.widgetQueueDraw
(gBoard g
)
31 modifyIORef
(gState g
) $ \s
-> s
{ sPreviousConfigItem
= Nothing
}
33 customConfig
:: G
.RadioMenuItem
-> Globals
-> IO ()
34 customConfig
item g
= do
35 active
<- G
.checkMenuItemGetActive
item
36 when active
$ customConfig
' item g
38 customConfig
' :: G
.RadioMenuItem
-> Globals
-> IO ()
39 customConfig
' item g
= do
40 s
<- readIORef
(gState g
)
41 let Config
{ cSize
= Pos sx sy
, cMines
= m
} = sConfig s
44 G
.dialogAddButton dia G
.stockCancel G
.ResponseCancel
45 okButton
<- G
.dialogAddButton dia G
.stockOk G
.ResponseOk
46 G
.dialogSetDefaultResponse dia G
.ResponseCancel
47 G
.windowSetTitle dia
"Custom config"
49 upper
<- G
.dialogGetUpper dia
50 table
<- G
.tableNew
2 3 False
51 upper `G
.containerAdd` table
52 let fs
= [("width", 2, sx
, pX maxSize
),
53 ("height", 2, sy
, pY maxSize
),
55 fields
<- forM
(zip [0..] fs
) $ \(c
, (n
, l
, v
, h
)) -> do
56 label
<- G
.labelNew
(Just n
)
57 G
.tableAttach table label
0 1 c
(c
+1) [G
.Fill
] [] 5 2
58 G
.miscSetAlignment label
0 0.5
59 adj
<- G
.adjustmentNew
(fromIntegral v
) l
(fromIntegral h
) 1 10 10
60 button
<- G
.spinButtonNew adj
0.5 0
61 G
.tableAttach table button
1 2 c
(c
+1) [G
.Expand
, G
.Fill
] [] 5 2
65 res
<- G
.dialogRun dia
66 [width
, height
, mines
] <- forM fields
$ \f -> do
67 round `
liftM` G
.spinButtonGetValue f
68 let cfg
' = Config
{ cSize
= Pos width height
,
69 cMines
= min (width
* height
- 1) mines
}
72 G
.ResponseOk
-> selectConfig
item cfg
' g
73 _
-> maybe (return ()) (`G
.checkMenuItemSetActive`
True)
74 (sPreviousConfigItem s
)
76 selectStrategy
:: Strategy
-> Globals
-> IO ()
77 selectStrategy strat g
= do
78 modifyIORef
(gState g
) $ \s
-> s
{ sStrategy
= strat
}
80 G
.widgetQueueDraw
(gBoard g
)
82 reset
:: Globals
-> IO ()
84 s
<- readIORef
(gState g
)
85 writeIORef
(gState g
) s
{ sBoard
= Nothing
, sStop
= Nothing
}
86 maybe (return ()) id (sStop s
)