1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
module UI2GUI (
UIWidget, UIRef,
Command(..),Ref,Handler(..),
Widget(Widget),
Event(..),WidgetKind(..),
CanvasItem(..),
StyleClass(..),Position(..),Direction(..),
Style(..),BorderStyle(..),FontStyle(..),Color(..),
addStyle,addStyles,setStyles,
addHandler,addHandlers,setHandlers,
setRef,getRef,
UI2GUI.runUI,UI2GUI.exitUI,
UI2GUI.getValue,UI2GUI.setValue,UI2GUI.updateValue,UI2GUI.appendValue,
UI2GUI.changeStyles,UI2GUI.setHandler,UI2GUI.setDisabled,
UI2GUI.setVisible,
UI2GUI.addCanvas,
UI2GUI.showPopup,UI2GUI.showMessage,
colS,col,rowS,row,matrixS,matrix,
entry,entryS,label,labelS,
UI.button,buttonS,simpleButton,simpleButtonS,
checkButton,checkButtonS,simpleCheckButton,simpleCheckButtonS,
canvas,canvasS,
textEdit,textEditS,
scale,scaleS,message,messageS,
menuBar,menuBarS,menu,menuS,menuSeparator,menuSeparatorS,
menuItem,menuItemS,
listBox,listBoxS,
UI.selection,UI.selectionInitial,UI.selectionInitialS,
UI.radio_main,UI.radio_other,
showBorderStyle,showColor,showPos,
UI2GUI.UIEnv,
setErrorBg
) where
import Maybe
import qualified GUI
import UI
import IOExts
data State = State (GUI.GuiPort,[GUI.ReconfigureItem])
data UIEnv = UIEnv (IORef State)
type UIRef = UI.Ref GUI.WidgetRef
type UIWidget = UI.Widget GUI.WidgetRef (UIEnv -> IO()) ()
ui2guievent :: Event -> GUI.Event
ui2guievent event = case event of
MouseButton1 -> GUI.MouseButton1
MouseButton2 -> GUI.MouseButton2
MouseButton3 -> GUI.MouseButton3
KeyPress -> GUI.KeyPress
Return -> GUI.Return
_ -> GUI.DefaultEvent
ui2guicmd cmd gp = do stateref <- newIORef (State (gp,[]))
cmd (UIEnv stateref)
State (_,reconfigs) <- readIORef stateref
return reconfigs
lrh2confitems :: Maybe String -> Maybe (UIRef)
-> [Handler (UIEnv -> IO ()) _] -> [GUI.ConfItem]
lrh2confitems mblabel mbref handlers =
text ++ wref ++ guihandlers
where
text = map (\x -> GUI.Text x) (maybeToList mblabel)
wref = case mbref of
Just ref -> [GUI.WRef (foo ref)]
where
foo r | r =:= UI.Ref cr = cr
where cr free
Nothing -> []
guihandlers = foo handlers
where
foo [] = []
foo ((UI.Handler event (UI.Cmd cmd)):handlers1) =
GUI.Handler (ui2guievent event) (ui2guicmd cmd): foo handlers1
widgetUI2GUI :: UIWidget -> GUI.Widget
widgetUI2GUI (UI.Widget name mblabel mbref handlers styleclasses widgets) =
case name of
UI.Col -> GUI.ColC collconfs confitems (map widgetUI2GUI widgets)
UI.Row -> GUI.RowC collconfs confitems (map widgetUI2GUI widgets)
UI.Matrix wss -> GUI.Matrix collconfs (map (map widgetUI2GUI) wss)
UI.Label -> GUI.Label confitems
UI.Button -> GUI.PlainButton confitems
UI.Entry -> GUI.Entry confitems
UI.MenuBar ->
GUI.MenuButton [GUI.Text label, GUI.Menu (map menu2menu widgets2)]
where
(label,widgets2) = head (foo widgets)
where
foo (UI.Widget name1 mblabel1 _ _ _ widgets1:_) =
case name1 of
UI.Menu -> [(fromJust mblabel1,widgets1)]
_ -> [("UI.Menu expected in UI2GUI.widgetUI2GUI",[])]
UI.Canvas w h -> GUI.Canvas ([GUI.Width w, GUI.Height h] ++ confitems)
UI.CheckButton checked -> GUI.CheckButton ((GUI.CheckInit init):confitems)
where init = if checked then "1" else "0"
UI.ListBox h items sel ->
GUI.ListBoxScroll
([GUI.Height h,GUI.Width 10,GUI.List items,GUI.CheckInit (show sel)] ++
confitems)
UI.Scale min max -> GUI.Scale min max confitems
UI.TextEdit rows cols ->
GUI.TextEdit ([GUI.Height rows, GUI.Width cols] ++ confitems)
x -> GUI.Label
[GUI.Text (show x ++ " not implemented in UI2GUI.widgetUI2GUI")]
where
collconfs = classes2guicollconfs styleclasses
confitems = lrh2confitems mblabel mbref handlers
++ classes2guiconfitems styleclasses
menu2menu :: UIWidget -> GUI.MenuItem
(UI.Widget name mblabel _ handlers _ widgets) =
case name of
UI.Menu -> GUI.MMenuButton (fromJust mblabel) (map menu2menu widgets)
UI.MenuSeparator -> GUI.MSeparator
UI.MenuItem -> GUI.MButton guihandler (fromJust mblabel)
where
guihandler = head (foo handlers)
where
foo [] = []
foo ((UI.Handler _ (UI.Cmd cmd)):handlers1) =
(ui2guicmd cmd):(foo handlers1)
getdisplayconf :: UIWidget -> [(GUI.WidgetRef,GUI.ConfItem)]
getdisplayconf (UI.Widget name _ mbref _ styleclasses widgets) =
case name of
UI.Matrix wss -> (concatMap (concatMap getdisplayconf) wss)
_ -> (bar styleclasses) ++ (concatMap getdisplayconf widgets)
where
bar [] = []
bar (UI.Class styles:cs) = (foo styles) ++ bar cs
where
foo [] = []
foo (style:styles') = case style of
(UI.Display b) -> case mbref of
Just (UI.Ref ref) -> [(ref,GUI.Display b)] ++ foo styles'
_ -> foo styles'
_ -> foo styles'
runUI :: String -> UIWidget -> IO ()
runUI title widget =
GUI.runInitGUI title (widgetUI2GUI widget) initcmd
where
d = getdisplayconf widget
initcmd gp =
mapIO_ (\ (ref,confitem) -> GUI.setConfig ref confitem gp) d
classes2guiconfitems :: [StyleClass] -> [GUI.ConfItem]
classes2guiconfitems [] = []
classes2guiconfitems (UI.Class styles:cs) =
(styles2confitems styles) ++ classes2guiconfitems cs
where
styles2confitems [] = []
styles2confitems (style:styles') = case style of
UI.Fg color -> [GUI.Foreground $ UI.showColor color]
UI.TextColor color -> [GUI.Foreground $ UI.showColor color]
UI.Bg color ->
case color of
Default -> [GUI.TclOption "-background $defaultBgColor"]
_ -> [GUI.Background $ UI.showColor color]
UI.Display b -> [GUI.Display b]
_ -> []
++ styles2confitems styles'
classes2guicollconfs :: [StyleClass] -> [GUI.ConfCollection]
classes2guicollconfs [] = []
classes2guicollconfs (UI.Class styles:cs) =
(styles2confs styles) ++ classes2guicollconfs cs
where
styles2confs [] = []
styles2confs (style:styles') =
case style of
UI.Align pos -> case pos of
UI.Center -> GUI.CenterAlign:styles2confs styles
UI.Left -> GUI.LeftAlign:styles2confs styles
UI.Right -> GUI.RightAlign:styles2confs styles
UI.Top -> GUI.TopAlign:styles2confs styles
UI.Bottom -> GUI.BottomAlign:styles2confs styles
_ -> styles2confs styles
++ styles2confs styles'
canvasui2canvasgui :: [CanvasItem] -> [GUI.CanvasItem]
canvasui2canvasgui canvasitems = map conv canvasitems
where
conv (CLine points str) = GUI.CLine points str
conv (CPolygon points str) = GUI.CPolygon points str
conv (CRectangle x y str) = GUI.CRectangle x y str
conv (COval x y str) = GUI.COval x y str
conv (CText x str1 str2) = GUI.CText x str1 str2
setValue :: UIRef -> String -> UIEnv -> IO ()
setValue (UI.Ref ref) val (UIEnv p) = do State (gp,_) <- readIORef p
GUI.setValue ref val gp
getValue :: UIRef -> UIEnv -> IO (String)
getValue (UI.Ref ref) (UIEnv p) = do State (gp,_) <- readIORef p
GUI.getValue ref gp
updateValue :: (String -> String) -> UIRef -> UIEnv -> IO ()
updateValue upd (UI.Ref ref) (UIEnv p) = do State (gp,_) <- readIORef p
GUI.updateValue upd ref gp
appendValue :: UIRef -> String -> UIEnv -> IO ()
appendValue (UI.Ref ref) val (UIEnv p) = do State (gp,_) <- readIORef p
GUI.appendValue ref val gp
addCanvas :: UIRef -> [CanvasItem] -> UIEnv -> IO ()
addCanvas (UI.Ref ref) items (UIEnv p) = do
State (gp,_) <- readIORef p
GUI.addCanvas ref (canvasui2canvasgui items) gp
showPopup :: String -> UIWidget -> _ -> IO ()
title ui _ = runUI title ui
showMessage :: String -> UIEnv -> IO ()
showMessage message = showPopup "Info" (label message)
exitUI :: UIEnv -> IO ()
exitUI (UIEnv p) = do State (gp,_) <- readIORef p
GUI.exitGUI gp
setHandler :: UIRef -> Event -> (UIEnv -> IO _) -> UIEnv -> IO ()
setHandler (UI.Ref ref) event cmd (UIEnv p) = do
State (gp,reconfigs) <- readIORef p
writeIORef p
(State (gp,reconfigs ++
[GUI.WidgetConf ref (GUI.Handler (ui2guievent event) (ui2guicmd cmd))]))
setDisabled :: UIRef -> Bool -> UIEnv -> IO ()
setDisabled (UI.Ref ref) value (UIEnv p) = do
State (gp,_) <- readIORef p
GUI.setConfig ref (GUI.Active (not value)) gp
setVisible :: UIRef -> Bool -> UIEnv -> IO ()
setVisible (UI.Ref ref) value (UIEnv p) = do
State (gp,_) <- readIORef p
GUI.setConfig ref (GUI.Display value) gp
changeStyles :: UIRef -> [StyleClass] -> UIEnv -> IO ()
changeStyles (UI.Ref ref) styles (UIEnv env) = do
State (gp,_) <- readIORef env
mapIO_ (\ x -> GUI.setConfig ref x gp) (classes2guiconfitems styles)
setErrorBg ref b env = do
if b then changeStyles ref [Class [Bg Yellow]] env
else changeStyles ref [Class [Bg Default]] env
|