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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
module GUI2HTML (
GuiPort,GUI2HTML.Widget(..),Button,ConfigButton,
TextEditScroll,ListBoxScroll,CanvasScroll,EntryScroll,
ConfItem(..),ReconfigureItem(..),
GUI2HTML.Cmd,GUI2HTML.Command,GUI2HTML.Event(..),ConfCollection(..),
MenuItem(..),
GUI2HTML.CanvasItem(..),
WidgetRef, GUI2HTML.Style(..), GUI2HTML.Color(..),
col,row,matrix,
runGUI,runInitGUI,
exitGUI,getValue,setValue,updateValue,appendValue,
seeText,
focusInput,addCanvas,setConfig,
getSaveFile
) where
import UI2HTML
import qualified UI
import FunctionInversion(invf1)
import Maybe
import qualified HTML
import IOExts
import Read
import Char(isDigit,isSpace)
data Widget =
PlainButton [ConfItem]
| Canvas [ConfItem]
| CheckButton [ConfItem]
| Entry [ConfItem]
| Label [ConfItem]
| ListBox [ConfItem]
| Message [ConfItem]
| [ConfItem]
| Scale Int Int [ConfItem]
| ScrollH WidgetRef [ConfItem]
| ScrollV WidgetRef [ConfItem]
| TextEdit [ConfItem]
| Row [ConfCollection] [Widget]
| Col [ConfCollection] [Widget]
| Matrix [ConfCollection] [[Widget]]
type UIRef = UI.Ref HTML.CgiRef
data ConfItem =
Active Bool
| Anchor String
| Background String
| Foreground String
| Handler Event (Command)
| Height Int
| CheckInit String
| CanvasItems [CanvasItem]
| List [String]
| [MenuItem]
| WRef UIRef
| Text String
| Width Int
| Fill | FillX | FillY
| TclOption String
type GuiPort = UI2HTML.UIEnv
type WidgetRef = UI.Ref HTML.CgiRef
type Command = GuiPort -> IO [ReconfigureItem]
data ReconfigureItem =
WidgetConf WidgetRef ConfItem
data Event =
DefaultEvent
| MouseButton1
| MouseButton2
| MouseButton3
| KeyPress
| Return
gui2uievent :: Event -> UI.Event
gui2uievent event = case event of
MouseButton1 -> UI.MouseButton1
MouseButton2 -> UI.MouseButton2
MouseButton3 -> UI.MouseButton3
KeyPress -> UI.KeyPress
Return -> UI.Return
_ -> UI.DefaultEvent
data =
MButton (GuiPort -> IO [ReconfigureItem]) String
| MSeparator
| String [MenuItem]
data ConfCollection =
CenterAlign | LeftAlign | RightAlign | TopAlign | BottomAlign
data CanvasItem =
CLine [(Int,Int)] String
| CPolygon [(Int,Int)] String
| CRectangle (Int,Int) (Int,Int) String
| COval (Int,Int) (Int,Int) String
| CText (Int,Int) String String
Button :: (UIEnv -> IO _) -> [ConfItem] -> Widget
Button cmnd confs = PlainButton (Cmd cmnd : confs)
TextEditScroll :: [ConfItem] -> Widget
TextEditScroll = TextEdit
EntryScroll :: [ConfItem] -> Widget
EntryScroll = Entry
CanvasScroll :: [ConfItem] -> Widget
CanvasScroll = Canvas
ConfigButton :: (UIEnv -> IO ([ReconfigureItem])) -> [ConfItem] -> Widget
ConfigButton cmnd = Button cmd2
where cmd2 x = do wconfs <- cmnd x
widgetconf2cmd wconfs x
Cmd :: (UIEnv -> IO _) -> ConfItem
Cmd cmnd = Handler DefaultEvent (\env -> cmnd env >> return [])
Command :: (UIEnv -> IO ([ReconfigureItem])) -> ConfItem
Command cmnd = Handler DefaultEvent cmnd
button = Button
command = Cmd
cmd = Command
data Style = Bold | Italic | Underline | Fg Color | Bg Color
data Color
= Black | Blue | Brown | Cyan | Gold | Gray | Green | Magenta | Navy | Orange
| Pink | Purple | Red | Tomato| Turquoise | Violet | White | Yellow
gui2ui :: Widget -> UIWidget
gui2ui widget = case widget of
PlainButton confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
in UI.Widget UI.Button
mbtext mbref hs (c2s confitems) []
CheckButton confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
checked = (getCheckInit confitems == 1)
in UI.Widget (UI.CheckButton checked)
mbtext mbref hs (c2s confitems) []
Entry confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
in UI.Widget UI.Entry
mbtext mbref hs (c2s confitems) []
Label confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
in UI.Widget UI.Label
mbtext mbref hs (c2s confitems) []
Message confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
in UI.Widget UI.Label
mbtext mbref hs (c2s confitems) []
Canvas confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
in UI.Widget (UI.Canvas w h)
mbtext mbref hs (c2s nconfs) []
where
h = fromMaybe 150 (getWidth confitems)
w = fromMaybe 300 (getHeight confitems)
nconfs = rmWidthHeight confitems
TextEdit confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
in UI.Widget (UI.TextEdit r c)
mbtext mbref hs (c2s nconfs) []
where
c = fromMaybe 50 (getWidth confitems)
r = fromMaybe 10 (getHeight confitems)
nconfs = rmWidthHeight confitems
Scale min max confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
in UI.Widget (UI.Scale min max)
mbtext mbref hs (c2s confitems) []
ListBox confitems -> let (mbtext,mbref,hs) = (c2lrh confitems)
in UI.Widget
(UI.ListBox size xs (getCheckInit confitems))
mbtext mbref hs (c2s confitems) []
where
xs = getList confitems
size = fromMaybe 1 (getHeight confitems)
MenuButton confitems ->
UI.menuBar [(UI.menu (fromJust (getText confitems))
(map menu2ui (fromJust $ getMenu confitems)))]
where
menuitem = case menuitem of
MButton (cmnd) label -> UI.menuItem cmd2 label
where
cmd2 x = do wconfs <- cmnd x
widgetconf2cmd wconfs x
MMenuButton label items -> UI.menu label (map menu2ui items)
MSeparator -> UI.menuSeparator
ScrollH _ _ ->
UI.label "GUI2HTML.gui2ui, ScrollH not implemented"
ScrollV _ _ ->
UI.label "GUI2HTML.gui2ui, ScrollV not implemented"
Row confs widgets -> UI.rowS (collconfs2s confs) (map gui2ui widgets)
Col confs widgets -> UI.colS (collconfs2s confs) (map gui2ui widgets)
Matrix confs widgetss -> UI.matrixS c (map (map gui2ui) widgetss)
where c = collconfs2s confs
where
c2lrh = confitems2lrh
c2s confitems =
let h = confitems2styleclass confitems in
if null h then [] else [UI.Class h]
collconfs2s confs =
let h = collectionconf2styleclass confs in
if null h then [] else [UI.Class h]
confitems2lrh confitems = c2lrh' (Nothing,Nothing,[]) confitems
where
foo ref | ref =:= UI.Ref cref = ref
where cref free
c2lrh' lrh [] = lrh
c2lrh' (mblabel,mbref,handlers) (conf:confs) = case conf of
Text label -> c2lrh' ((Just label),mbref,handlers) confs
WRef ref -> c2lrh' (mblabel,(Just (foo ref)),handlers) confs
Handler event cmnd -> c2lrh'
(mblabel,mbref,
((UI.Handler
(gui2uievent event) (UI.Cmd cmd2)):handlers))
confs
where cmd2 x = do wconfs <- cmnd x
widgetconf2cmd wconfs x
_ -> c2lrh' (mblabel,mbref,handlers) confs
confitems2styleclass confitems = c2s' confitems
where
c2s' [] = []
c2s' (conf:confs) = case conf of
Active b -> UI.Active b:c2s' confs
Background color -> UI.Bg (string2Color color):c2s' confs
Foreground color -> UI.Fg (string2Color color):c2s' confs
Fill -> UI.Fill UI.Both:c2s' confs
FillX -> UI.Fill UI.X:c2s' confs
FillY -> UI.Fill UI.Y:c2s' confs
Height n -> UI.Height n:c2s' confs
Width n -> UI.Width n:c2s' confs
_ -> c2s' confs
collectionconf2styleclass collconfs = c2s' collconfs
where
c2s' [] = []
c2s' (conf:confs) = case conf of
CenterAlign -> UI.Align UI.Center:c2s' confs
LeftAlign -> UI.Align UI.Left:c2s' confs
RightAlign -> UI.Align UI.Right:c2s' confs
TopAlign -> UI.Align UI.Top:c2s' confs
BottomAlign -> UI.Align UI.Bottom:c2s' confs
_ -> c2s' confs
canvasgui2canvasui :: [CanvasItem] -> [UI.CanvasItem]
canvasgui2canvasui canvasitems = map conv canvasitems
where
conv (CLine points str) = UI.CLine points str
conv (CPolygon points str) = UI.CPolygon points str
conv (CRectangle x y str) = UI.CRectangle x y str
conv (COval x y str) = UI.COval x y str
conv (CText x str1 str2) = UI.CText x str1 str2
getText :: [ConfItem] -> Maybe (String)
getText [] = Nothing
getText (x:xs) = case x of
Text str -> Just str
_ -> getText xs
getWidth :: [ConfItem] -> Maybe Int
getWidth [] = Nothing
getWidth (x:xs) = case x of
Width w -> Just w
_ -> getWidth xs
getHeight :: [ConfItem] -> Maybe Int
getHeight [] = Nothing
getHeight (x:xs) = case x of
Height w -> Just w
_ -> getHeight xs
getMenu :: [ConfItem] -> Maybe ([MenuItem])
[] = Nothing
getMenu (x:xs) = case x of
Menu items -> Just items
_ -> getMenu xs
getList :: [ConfItem] -> [String]
getList [] = []
getList (x:xs) = case x of
List items -> items
_ -> getList xs
getCheckInit :: [ConfItem] -> Int
getCheckInit [] = -1
getCheckInit (x:xs) = case x of
CheckInit str -> readInt str
_ -> getCheckInit xs
rmWidthHeight :: [ConfItem] -> [ConfItem]
rmWidthHeight [] = []
rmWidthHeight (x:xs) = case x of
Height _ -> rmWidthHeight xs
Width _ -> rmWidthHeight xs
_ -> x:rmWidthHeight xs
widgetconf2cmd :: [ReconfigureItem] -> UIEnv -> IO ()
widgetconf2cmd [] _ = done
widgetconf2cmd ((WidgetConf ref confItem):items) x = do
case confItem of
Active b -> setDisabled ref (not b) x
Background str -> changeStyles ref [UI.Class [UI.Bg (string2Color str)]] x
Foreground str -> changeStyles ref [UI.Class [UI.Fg (string2Color str)]] x
Handler event cmnd -> setHandler ref (gui2uievent event) cmd2 x
where
cmd2 env' = do
wconfs <- cmnd env'
widgetconf2cmd wconfs env'
done
Height n -> changeStyles ref [UI.Class [UI.Height n]] x
List l -> UI2HTML.setConfig ref (UI2HTML.List l) x
Text text -> setValue ref text x
Fill -> changeStyles ref [UI.Class [UI.Fill Both]] x
FillX -> changeStyles ref [UI.Class [UI.Fill X]] x
FillY -> changeStyles ref [UI.Class [UI.Fill Y]] x
_ -> done
widgetconf2cmd items x
string2Color :: String -> UI.Color
string2Color = invf1 UI.showColor
ListBoxScroll :: [ConfItem] -> Widget
ListBoxScroll = ListBox
setConfig :: UIRef -> ConfItem -> UIEnv -> IO ()
setConfig cref value env = case value of
List l -> UI2HTML.setConfig cref (UI2HTML.List l) env
_ -> done
seeText :: UIRef -> (Int,Int) -> UIEnv -> IO ()
seeText = UI2HTML.seeText
getSaveFile :: IO ([_])
getSaveFile = return []
focusInput :: _ -> UIEnv -> IO ()
focusInput _ env =
showMessage "GUI2HTML.focusInput is not implemented" env
chooseColor :: IO (String)
chooseColor = return "red"
col :: [Widget] -> Widget
col = Col []
row :: [Widget] -> Widget
row = Row []
matrix :: [[Widget]] -> Widget
matrix = Matrix []
getValue :: UIRef -> UIEnv -> IO (String)
getValue = UI2HTML.getValue
setValue :: UIRef -> String -> UIEnv -> IO ()
setValue = UI2HTML.setValue
updateValue :: (String -> String) -> UIRef -> UIEnv -> IO ()
updateValue = UI2HTML.updateValue
appendValue :: UIRef -> String -> UIEnv -> IO ()
appendValue = UI2HTML.appendValue
exitGUI :: UIEnv -> IO ()
exitGUI = UI2HTML.exitUI
runGUI :: String -> Widget -> IO HTML.HtmlForm
runGUI title gui = UI2HTML.runUI title (gui2ui gui)
runInitGUI :: String -> Widget -> _ -> IO HTML.HtmlForm
runInitGUI title gui _ = UI2HTML.runUI title (gui2ui gui)
addCanvas :: UIRef -> [CanvasItem] -> UIEnv -> IO ()
addCanvas ref items env = UI2HTML.addCanvas ref (canvasgui2canvasui items) env
readMaybeInt :: String -> Maybe Int
readMaybeInt "" = Nothing
readMaybeInt (v:s) | v=='-' = maybe Nothing (\i->Just (-i)) (acc 0 s)
| isDigit v = acc 0 (v:s)
| otherwise = Nothing
where
acc n "" = Just n
acc n (c:cs) | isDigit c = acc (10*n + ord c - ord '0') cs
| otherwise = Nothing
|