summaryrefslogtreecommitdiffstats
path: root/window.inc
blob: 65b294d3c6678e0f3e1cbe815930d8ac3438bd17 (plain)
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%
% window code
%
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


% public words:
%
% window.dialog		( ) ==> ( window )
%   - create a new dialog
%
% window.init		( window ) ==> ( )
%   - initialize window
%
% window.show		( window ) ==> ( )
%   - draw window
%
% window.current	( ) ==> ( window )
%   - the top level window
%
% window.action		( ) ==> ( action )
%   - recent window action
%
% window.input		( key_in ) ==> ( key_out )
%   - handle keyboard input
%
% window.done		( ) ==> ( )
%   - close top level window
%
%
% constants:
%   - window.action
%       actNothing	- do nothing
%       actExit		- leave boot menu
%       actCloseInfo	- close info window
%       actPassword	- password entered
%       actStart	- boot kernel
%       actEject	- eject CD
%       actPowerOff	- turn computer off
%       actReboot	- reboot computer
%       actRedraw	- redraw everything
%       actNoClose	- don't close dialog (it's a flag)
%


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% window related global variables
%

% all open windows are stacked here
/window.list 8 array def
/window.list.index 0 def

% the top level window
% /window.current

% action selected by closing window
/window.action actNothing def

% window field definitions
/widget.size 0 def
/newfield { widget.size def /widget.size widget.size 1 add def } def

/.type           newfield
/.x              newfield
/.y              newfield
/.width          newfield
/.height         newfield
/.width.min      newfield
/.position       newfield
/.color.fg       newfield
/.color.bg       newfield
/.font           newfield
/.saved          newfield
/.saved.areas    newfield
/.title          newfield
/.title.fg       newfield
/.title.bg       newfield
/.title.height   newfield
/.text           newfield
/.text.x         newfield
/.text.y         newfield
/.buttons        newfield
/.button.y       newfield
/.ed             newfield
/.ed.font        newfield
/.ed.list        newfield
/.ed.buffer.list newfield
/.ed.text.list   newfield
/.ed.width       newfield
/.ed.focus       newfield
/.ed.pw_field    newfield
/.xmenu          newfield
/.xmenu.update   newfield

% window types
/t_dialog 100 def
/t_help   101 def
/t_main   102 def
/t_xmenu  103 def

% actions
/actNothing           0 def
/actExit              1 def
/actCloseInfo         2 def
/actPassword          3 def
/actStart             4 def
/actEject             5 def
/actPowerOff          6 def
/actRedraw            7 def
/actRedrawPanel       8 def
/actInstallOK         9 def
/actInstallCancel    10 def
/actReboot           11 def
/actNoClose       0x100 def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Create new dialog window.
%
% ( ) ==> ( window )
%
/window.dialog {
  widget.size array
  dup .type t_dialog put
  dup .position 8 put		% centered at 8/10 of screen height
  dup .x 0 put
  dup .y 0 put
  dup .width.min 0 put
  dup .color.fg black put
  dup .color.bg lightgray put
  dup .font font.normal put
  dup .title.fg white put
  dup .title.bg title.bg put
  dup .title.height help.title.height put
  dup .text.x 12 put
  dup .text.y help.title.height 10 add put
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Handle keyboard input.
%
% ( key_in ) ==> ( key_out )
%
/window.input {
  window.current .undef ne {
    window.current .type get
    dup t_dialog eq { exch dialog.input exch } if
    dup t_help eq { exch help.input exch } if
    dup t_main eq { exch main.input exch } if
    dup t_xmenu eq { exch xmenu.input exch } if
    pop

    % maybe there is an editable input field
    dup 0 ne {
      window.current .ed get .undef ne {
        window.current .ed.font get setfont
        window.current .color.fg get setcolor
        window.current .ed get exch edit.input
        0
      } if
    } if

    % only top level window gets input
    pop 0

  } if
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Initialize window.
%
% ( window ) ==> ( )
%
/window.init {
  dup .type get
  dup t_dialog eq { pop dialog.init return } if
  dup t_help eq { pop help.init return } if
  dup t_main eq { pop main.init return } if
  dup t_xmenu eq { pop xmenu.init return } if
  pop
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Show window.
%
% ( window ) ==> ( )
%
/window.show {
  dup .type get
  dup t_dialog eq { pop dialog.show return } if
  dup t_help eq { pop help.show return } if
  dup t_main eq { pop main.show return } if
  dup t_xmenu eq { pop xmenu.show return } if
  pop
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Add window to list.
%
% ( window ) ==> ( )
%
/window.push {
  window.list.index window.list length ge { pop return } if
  /window.current over def
  window.list window.list.index rot put
  /window.list.index window.list.index 1 add def
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Remove window from list.
%
% ( ) ==> ( window )
%
/window.pop {
  window.list.index 0 eq { .undef return } if
  /window.list.index window.list.index 1 sub def
  window.list window.list.index get
  window.list window.list.index .undef put
  /window.current
    window.list.index 0 eq { .undef } { window.list window.list.index 1 sub get } ifelse
  def
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Close top level window.
%
% ( ) ==> ( )
%
/window.done {
  window.current .undef ne {
    window.current dup .type get
    dup t_help eq { help.done } if
    dup t_xmenu eq { xmenu.done } if
    pop
    % restore saved background and free bg image
    dup .saved get .undef ne {
      dup .x get 1 sub over .y get 1 sub moveto
      dup .saved get dup restorescreen free
      dup .saved .undef put
    } if
    % restore & free saved background areas
    dup .saved.areas get .undef ne {
      % list of [ x y screen ]
      dup .saved.areas get {
        dup { } forall
        3 1 roll moveto dup restorescreen free
        free
      } forall
      dup .saved.areas get free
      dup .saved.areas .undef put
    } if
    % free input field memory
    dup .ed get .undef ne {
      dup .ed get 2 get free		% background
      dup .ed get free
      dup .ed .undef put
    } if
    dup .ed.text.list get free
    dup .ed.list get dup {
      { dup 2 get free free } forall
    } { pop } ifelse
    dup .ed.list get free
    pop
    % remove it from window list
    window.pop
    % free buttons & button list
    dup .buttons get
      dup .undef ne {
        dup length 0 gt {
          dup length 1 sub 0 1 rot {
            over exch get free
          } for
        } if
      } if
      free
    % free window
    free
  } if
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Handle keyboard input.
%
% ( key_in ) ==> ( key_out )
%
/dialog.input {
  dup 0 eq { return } if

  /window.buttons window.current .buttons get def

  window.buttons .undef ne { window.buttons length {

    dup keyEnter eq {
      window.buttons window.findselected
      over over get 7 get /window.action exch def get
      dup button.press 100000 usleep
      window.action actNoClose and {
        window.action dialog.specialaction { button.show } if
      } {
        pop window.done
      } ifelse
      pop 0
    } if

    window.current .ed.list get {
      dup keyDown eq over keyTab eq or {
        window.current .ed.focus get
        window.current .ed.list get over get edit.hidecursor

        1 add window.current .ed.list get length mod
        window.current .ed.focus 2 index put
        window.current .ed.list get exch get edit.showcursor
        pop 0
      } if

      dup keyUp eq over keyShiftTab eq or {
        window.current .ed.focus get
        window.current .ed.list get over get edit.hidecursor

        1 sub window.current .ed.list get length exch over add exch mod
        window.current .ed.focus 2 index put
        window.current .ed.list get exch get edit.showcursor
        pop 0
      } if

%      dup keyTab eq {
%        window.findselected 1 add window.buttons length mod
%        window.selectbutton
%        pop 0
%      } if

    } {
      dup keyTab eq over keyRight eq or over keyDown eq or {
        window.findselected 1 add window.buttons length mod
        window.selectbutton
        pop 0
      } if

      dup keyLeft eq over keyUp eq or {
        window.findselected window.buttons length 1 sub add window.buttons length mod
        window.selectbutton
        pop 0
      } if
    } ifelse

    dup window.findkey dup 0 ge {
      window.buttons exch
      over over get 7 get /window.action exch def get
      dup button.press 100000 usleep
      window.action actNoClose and {
        window.action dialog.specialaction { button.show } if
      } {
        pop window.done
      } ifelse
      pop 0
    } {
      pop
    } ifelse

  } if } if


  % maybe there are input fields
  dup 0 ne {
    window.current .ed.list get dup {
      window.current .ed.font get
        window.current .ed.focus get window.current .ed.pw_field get eq { pwmode } if
        setfont
      window.current .color.fg get setcolor

      window.current .ed.focus get get exch edit.input
      0
    } { pop } ifelse
  } if

} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Find selected button.
%
% ( ) ==> ( button_idx )
%
/window.findselected {
  0
  0 1 window.buttons length 1 sub {
    dup
    window.buttons exch get 5 get
    {
      exch pop exit
    } {
      pop
    } ifelse
  } for
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Find button for key.
%
% ( key ) ==> ( button_idx )
%
% button_idx = -1 if not found
%
/window.findkey {
  /window.key exch def
  -1
  window.key 0 eq { return } if
  0 1 window.buttons length 1 sub {
    dup
    window.buttons exch get 6 get window.key eq
    {
      exch pop exit
    } {
      pop
    } ifelse
  } for
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Select button.
%
% ( button_idx ) ==> ( )
%
/window.selectbutton {
  window.findselected
  over over eq {
    pop pop
  } {
    window.buttons exch get button.notdefault button.show
    window.buttons exch get button.default button.show
  } ifelse
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Initialize dialog window.
%
% ( window ) ==> ( )
%
/dialog.init {
  /dialog.tmp exch def

  dialog.tmp .font get setfont

  dialog.tmp .text get dup "" ne { strsize } { pop 0 0 } ifelse
  /dialog.height exch dialog.tmp .text.y get 10 add add def
  /dialog.width exch dialog.tmp .text.x get 1 add 2 mul add dialog.tmp .width.min get max def

  /dialog.width dialog.tmp .title get strsize pop dialog.tmp .text.x get 1 add 2 mul add dialog.width max def

  dialog.tmp .ed.text.list get dup {
    /dialog.width
      dialog.tmp .ed.width get dialog.tmp .text.x get 1 add 2 mul add 8 add dialog.width max
    def
    {
      strsize
      /dialog.height exch dialog.height add def
      /dialog.width exch dialog.tmp .text.x get 1 add 2 mul add dialog.width max def
    } forall
  } { pop } ifelse

  dialog.tmp .ed.buffer.list get dup {
    length lineheight 20 add mul /dialog.height exch dialog.height add def
  } { pop } ifelse

  /window.buttons dialog.tmp .buttons get def

  /dialog.button.y dialog.height 5 add def

  window.buttons .undef ne { window.buttons length {
    /dialog.height window.buttons 0 get 3 get dialog.button.y add 8 add def
  } if } if

  /dialog.y screen.size exch pop dialog.tmp .position get mul 10 div dialog.height sub 2 div 10 max def

  window.buttons .undef ne { window.buttons length {
    10
    0 1 window.buttons length 1 sub {
      window.buttons exch get
      dup 1 dialog.button.y dialog.y add put
      2 get 10 add add
    } for

    dialog.width max /dialog.width exch def
  } if } if

  % adjust to window size
  dialog.tmp .ed.width get .undef ne {
    dialog.tmp .ed.width over over get dialog.width 30 sub max put
  } if

  /dialog.x screen.size pop dialog.width sub 2 div def

  window.buttons .undef ne { window.buttons length {
    % calculate button x positions
    dialog.width 0
    0 1 window.buttons length 1 sub {
      window.buttons exch get 2 get add
    } for
    sub window.buttons length 1 add div
    dialog.x over add
    0 1 window.buttons length 1 sub {
      window.buttons exch get
      over over 0 rot put
      2 get add over add
    } for
    pop pop
  } if } if

  % store values

  dialog.tmp
  dup .x dialog.x put
  dup .y dialog.y put
  dup .width dialog.width put
  dup .height dialog.height put
      .button.y dialog.button.y put

  /dialog.tmp .undef def
} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Draw dialog window.
%
% ( window ) ==> ( )
%
/dialog.show {
  /dialog.tmp exch def

  % put into list early, so drawing functions can access it there
  dialog.tmp window.push

  % now start drawing

  dialog.tmp .x get 1 sub dialog.tmp .y get 1 sub moveto
  white black
  dialog.tmp .width get 2 add dialog.tmp .height get 2 add
  over over
  savescreen
  dialog.tmp .saved rot put
  drawborder

  dialog.tmp .color.bg get setcolor
  dialog.tmp .x get dialog.tmp .y get moveto
  dialog.tmp .width get dialog.tmp .height get fillrect

  dialog.tmp .title.bg get setcolor
  dialog.tmp .x get 1 add dialog.tmp .y get 1 add moveto
  dialog.tmp .width get 2 sub dialog.tmp .title.height get 1 sub fillrect

  dialog.tmp .x get 10 add dialog.tmp .y get 3 add moveto
  dialog.tmp .title.fg get setcolor
  dialog.tmp .title get show

  dialog.tmp .color.fg get setcolor
  dialog.tmp .x get dialog.tmp .y get moveto
  dialog.tmp .text.x get dialog.tmp .text.y get rmoveto
  dialog.tmp .text get show

  dialog.tmp .ed.text.list get dup {
    /dialog.tmp.idx 0 def
    {
      show
      dialog.tmp .ed.buffer.list get dialog.tmp.idx get

      dup {

        3 7 rmoveto

        /dialog.tmp.buf [
          currentpoint
          dialog.tmp .ed.width get fontheight 2 add savescreen
          0 0
          0 0 0
        ] def

        dialog.tmp.buf 3 2 index put
        dialog.tmp.buf 4 rot cvp length put

        dialog.tmp .ed.list get dialog.tmp.idx dialog.tmp.buf put

        currentcolor
        currentpoint over 1 sub over 2 sub moveto
        black white dialog.tmp .ed.width get 2 add fontheight 5 add drawborder
        moveto -3 lineheight 20 add 7 sub rmoveto
        setcolor

        currentfont
        % hide text in password fields
        dialog.tmp.idx dialog.tmp .ed.pw_field get eq {
          dialog.tmp .ed.font get pwmode setfont
        } if
        dialog.tmp.buf dup 3 get edit.init
        setfont

        dialog.tmp .ed.focus get dialog.tmp.idx ne { dialog.tmp.buf edit.hidecursor } if

      } { pop } ifelse

      /dialog.tmp.idx inc
    } forall
  } { pop } ifelse

  dialog.tmp .buttons get .undef ne { dialog.tmp .buttons get length {
    0 1 dialog.tmp .buttons get length 1 sub {
      dialog.tmp .buttons get exch get button.show
    } for
  } if } if

  /dialog.tmp .undef def

} def


% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Do something without closing the window.
%
% ( action ) ==> ( true|false )
%
% Returns whether the window still exists.
%
/dialog.specialaction {
  actNoClose not and

  true exch

  dup actEject eq {
    bootdrive eject pop
  } if

  dup actPowerOff eq {
    poweroff
  } if

  dup actReboot eq {
    reboot
  } if

  dup actInstallOK eq {
    install.ok
    exch not exch
  } if

%  dup actInstallCancel eq {
%    install.cancel
%    exch not exch
%  } if

  pop
} def