displays.fs 34.4 KB
Newer Older
1 2 3 4 5 6 7
\ Display                                              05aug99py
gadget class displays
public: ptr dpy                 ptr childs
        xresource ptr xrc       ptr nextwin
        cell var xwin           cell var cur-cursor
        cell var rw             cell var rh
        cell var mx             cell var my
bp's avatar
bp committed
8 9
        cell var mb
        cell var !moved         cell var clicks
10 11 12 13 14 15 16 17
        cell var lastclick      cell var lasttime
        cell var tx             cell var ty
        cell var clipregion     cell var counter
        cell var clip-is        cell var clip-should
        cell var clip-r         gadget ptr pointed
        font ptr cur-font       cell var events

\ Display                                              11nov06py
bp's avatar
bp committed
18
[defined]  xrender [IF]
19
        cell var xpict          method ?xpict   [THEN]
bp's avatar
bp committed
20
[defined]  x11 [IF]   sizeof XRectangle var xrect
21
        cell var lastcal        cell var timeoffset    [THEN]
bp's avatar
bp committed
22
[defined]  win32 [IF]   sizeof rect       var xrect
23 24 25 26
        cell var style          cell var owner  [THEN]
        cell var xft-draw

\ Display                                              11nov06py
bp's avatar
bp committed
27
        early drawable
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        method line             method text
        method image            method box
        method mask             method fill
        method stroke           method drawer
        method mouse            method screenpos
        method trans            method trans'
        method transback        method sync
        method set-cursor       method set-rect
        method set-color        method set-font
        method set-hints        method set-linewidth
        method moreclicks       method transclick

\ Display                                              04aug05py
        method clip-rect        method txy!
        method get-event        method handle-event
        method schedule-event   method size-event
        method click            method click?
        method moved?           method moved!
        method get-win          method get-dpy
bp's avatar
bp committed
47
        method click^           method flush-queue
48 49 50 51
        method show-me          method scroll
        method clipx            method clipy
        method geometry         method geometry?
        method >exposed         method child-moved
bp's avatar
bp committed
52
[defined]  win32 [IF]                   method win>o     [THEN]
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        early xS                method mxy!
        method schedule         method invoke
        method cleanup          method do-idle

\ Display                                              06feb00py
how:    : dispose  clicks HandleOff
          xrc dispose  super dispose ;
        : xS ( -- n )  xrc xN @ 2+ 2/ 2/ ;
        : dpy! ( dpy -- )  bind dpy !resized ;
        | Variable schedules-root
        : cleanup ( o -- )  >r events
          BEGIN  dup @ WHILE
                 dup @ cell+ cell+ @ r@ =
                 IF  dup @ dup >r @ over !
                     r> schedules-root DelFix
                 ELSE  @  THEN
          REPEAT  drop rdrop ;
bp's avatar
bp committed
70 71
[defined]  x11 [IF]
        : ?calib ( -- )  lastcal @ 0= XTime lastcal @ - #60000 >
72 73 74 75 76 77 78 79 80 81 82 83 84
          or  IF  xrc calibrate XTime lastcal !  THEN ;  [THEN]

\ Display tasking                                      29aug99py
        : schedule ( xt object time -- )
          schedules-root 4 cells $40 NewFix dup >r cell+ !
          r@ 2 cells + 2!   events
          BEGIN  dup @  WHILE
                 dup @ cell+ @ r@ cell+ @ - 0<  WHILE
                 @  REPEAT  THEN
          dup @ r@ ! r> swap ! ;
        : invoke ( -- ms )  events @
          IF    events @ cell+ @ timer@ - dup 0<
                IF    events @ dup @ events ! dup 2 cells + 2@
bp's avatar
bp committed
85 86 87
		      rot schedules-root DelFix
                      object with send endwith
		ELSE  >us #1000 m/mod nip  THEN
88 89 90 91 92 93
          ELSE  minwait  THEN  -1 max minwait min ;
        : do-idle ( n -- ) dup 0>
          IF  xrc fid swap idle  ELSE  drop sync  THEN ;

\ Display tasking                                      09jul00py

bp's avatar
bp committed
94
      [defined] VFXFORTH [IF]
bp's avatar
bp committed
95
	Variable pass^
bp's avatar
bp committed
96
	Variable events-lock  events-lock off
bp's avatar
bp committed
97
	: .catch-rest ( n -- )  ." Error " . cr ;
bp's avatar
bp committed
98 99
	: handle-events ( -- )  events-lock @ ?EXIT
	    events-lock on
100
	    [: handle-event  invoke drop ;] catch
bp's avatar
bp committed
101
	    events-lock off throw ;
bp's avatar
bp committed
102
        : do-event  pass^ @ op!
103
          up@ TO event-task'
bp's avatar
bp committed
104 105
[defined]  win32 [IF]    up@ 'event-task !              [THEN]
          BEGIN  depth >r ['] handle-event catch
bp's avatar
bp committed
106
                 ?dup IF  .catch-rest  THEN
bp's avatar
bp committed
107
[defined]  x11 [IF]    (  err-dpy @ IF  .Xerror  THEN ) ?calib   [THEN]
bp's avatar
bp committed
108 109 110
                 ['] invoke catch
                 ?dup IF  .catch-rest  ELSE  do-idle  THEN
                 depth r> <> IF  ~~  clearstack  THEN
bp's avatar
bp committed
111
          AGAIN ;
bp's avatar
bp committed
112 113
\	  task eventtask
\	  : event-task ^ pass^ ! ['] do-event eventtask initiate ;
bp's avatar
bp committed
114
      [ELSE]
bp's avatar
bp committed
115 116 117 118
        : .catch-rest ( n -- )  .  "error @ ?dup
	  IF  count type  THEN  "error off
	  cr ['] .except catch drop
	  cr ['] .back   catch drop ;
bp's avatar
bp committed
119 120 121
	: event-task  $20000 $10000 NewTask activate
	  >tib off $100 newtib
	  Onlyforth dynamic   " event-task" r0 @ cell+ !
122
          up@ TO event-task'
bp's avatar
bp committed
123
[defined]  win32 [IF]    up@ 'event-task !              [THEN]
124
          BEGIN  depth >r ['] handle-event catch
bp's avatar
bp committed
125
	         ?dup IF  .catch-rest  THEN
bp's avatar
bp committed
126
[defined]  x11 [IF]      err-dpy @ IF  .Xerror  THEN  ?calib   [THEN]
bp's avatar
bp committed
127 128 129
                 ['] invoke catch
                 ?dup IF  .catch-rest  ELSE  do-idle  THEN
                 depth r> <> IF  ~~  clearstack  THEN
130
          AGAIN ;
bp's avatar
bp committed
131
      [THEN]
132 133 134 135 136 137
        : set-hints ;

\ Display                                              07jan07py

        : init ( resource -- )  bind xrc  self bind dpy
          'nilscreen bind childs  'nilscreen bind nextwin
bp's avatar
bp committed
138
[defined]  x11 [IF]
139 140 141
          xrc dpy @ dup xrc screen @ RootWindow xwin !
          xrc screen @ 2dup DisplayWidth  w ! DisplayHeight h !
[THEN]
bp's avatar
bp committed
142
[defined]  win32 [IF]
143 144 145 146 147
          GetDesktopWindow xwin !
          0 0 0 0 sp@ xwin @ GetWindowRect drop 2drop w ! h !
[THEN]
          maxclicks 8* cell+ clicks 2dup Handle! @ swap erase
          -1 rw ! -1 rh !  -1 cur-cursor !
bp's avatar
bp committed
148
[defined] VFXFORTH [ 0= ] [IF] event-task [THEN] ;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

\ Display                                              27jun02py

        : append  ( addr -- )  childs self swap  bind childs
          childs bind nextwin  self childs bind parent ;
        : delete  ( addr -- )  link childs  childs delete ;
        : show-me ( x y -- )  2drop ;
        : scroll ( x y -- )  2drop ;
        : clipx ( -- x w ) 0 w @ ;
        : clipy ( -- y h ) 0 h @ ;
        : ALLCHILDS ( .. -- ..' )  childs self
          BEGIN  dup 'nilscreen <>  WHILE
                 r@ swap >o execute nextwin self o>
          REPEAT  drop rdrop ;
        : !resized 0 set-font  xrc !font  ALLCHILDS  !resized ;
        : resized  ALLCHILDS  resized ;
        : draw     ALLCHILDS  draw ;

\ Display                                              05jan07py

bp's avatar
bp committed
169
        : get-win [defined]  win32 [IF]  xrc dc @
170 171 172 173 174 175 176 177
                  [ELSE]  xwin @  [THEN] ;
        : ?clip ( -- )  clip-is @ clip-should @ <>
          IF  0 clip-rect   self >o
              BEGIN   clip-is @  clip-is off  clip-should off
                      dup op!  0= UNTIL  o>
              ( clip-should @ clip-is ! )  THEN ;
        : txy! ( x y -- )  txy 2@ p+ ty ! tx ! ;
        : geometry ( w h -- )
bp's avatar
bp committed
178
          x @ y @ 2swap resize ;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        : geometry? ( -- w h )  w @ h @ ;
        : transback ;    : trans ;   : trans' ;
        : set-rect ( o -- )  bind pointed ;

\ Display                                              18jul99py

        : clip-box ( x y w h -- x' y' w' h' ) >xyxy
          clipy over + >r max r> min >r
          clipx over + >r max r> min >r
          clipy over + >r max r> min >r
          clipx over + >r max r> min r> r> r> >xywh ;

        : drawer ( x y o xt -- )  ?clip
          ^ rot >o swap execute o> ;

\ Xrender extras                                       11nov06py

bp's avatar
bp committed
196
[defined]  xrender [IF]
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        Create pict_attrib sizeof XRenderPictureAttributes allot
        1 pict_attrib XRenderPictureAttributes dither !
        : ?xpict ( -- )  xpict @ ?EXIT  xrc dpy @ xwin @
          over dup dup DefaultScreen DefaultVisual
          XRenderFindVisualFormat
          $800 pict_attrib XRenderCreatePicture xpict ! ;
        Create dummy_rect 0 w, 0 w, $7FFF w, $7FFF w,
        : ?pclip ( -- )  xrc dpy @ xpict @ clip-r @ dup
          IF    XRenderSetPictureClipRegion
          ELSE  drop 0 0 dummy_rect 1
                XRenderSetPictureClipRectangles
          THEN ;
[THEN]

\ Display                                              11nov06py

bp's avatar
bp committed
213
[defined]  x11 [IF]
bp's avatar
bp committed
214
        : drawable ( -- dpy win gc ) xrc dpy @ xwin @ xrc gc @ ;
215
        : set-font ( n -- )  xrc font@ bind cur-font ;
bp's avatar
bp committed
216 217
        : set-color ( color -- )  ?clip $FF and dup $10 u<
          IF  dup cells Pixmaps + @  ELSE  0  THEN ?dup
218 219
          IF    nip  tx @ ty @ rot xrc set-tile
          ELSE  xrc color xrc set-color  THEN ;
bp's avatar
bp committed
220
        : set-linewidth ( n -- ) >r
bp's avatar
bp committed
221
	  drawable nip r> 0 0 1 XSetLineAttributes drop ;
222 223 224
        : set-cursor ( n -- )
          cur-cursor @ over = IF  drop  EXIT  THEN
          dup cur-cursor !
bp's avatar
bp committed
225
	  xrc cursor drawable drop rot XDefineCursor drop
bp's avatar
bp committed
226
      ;
227 228 229 230

\ Display                                              21aug99py

        : line ( x y x y color -- )  set-color
bp's avatar
bp committed
231
          2>r 2>r drawable 2r> 2r> XDrawLine ;
232 233 234
        : text ( addr u x y color -- )  set-color
          self cur-font draw ;
        : box ( x y w h color -- )  set-color \ clip-box
bp's avatar
bp committed
235
          2>r 2>r drawable 2r> 2r> XFillRectangle ;
236
        : fill ( x y array n color -- )  set-color
bp's avatar
bp committed
237 238 239 240 241
	  2swap over $-4000 $4000 within over $-4000 $4000 within and
	  IF  swap 2over drop w!+ w!
	      2>r drawable 2r> Complex CoordModePrevious
	      XFillPolygon
	  ELSE  2drop 2drop  THEN ;
242
        : stroke ( x y array n color -- )  set-color
bp's avatar
bp committed
243 244 245 246
	  2swap over $-4000 $4000 within over $-4000 $4000 within and
	  IF  swap 2over drop w!+ w!
	      2>r drawable 2r> CoordModePrevious XDrawLines
	  ELSE  2drop 2drop  THEN ;
247 248 249

\ Display                                              11nov06py

bp's avatar
bp committed
250
        : clip-mask ( y x w -- )
bp's avatar
bp committed
251 252
	  drawable nip rot XSetClipMask
          drawable nip 2swap swap XSetClipOrigin ;
bp's avatar
bp committed
253
        : mask { xs ys w h x y win1 win2 }  ?clip
254
          win1 0= IF
bp's avatar
bp committed
255
	      drawable win2 -rot xs ys w h x y XCopyArea
bp's avatar
bp committed
256
[defined]  xrender [IF]  ELSE  win1 -1 = IF
257 258 259 260 261 262 263
              ?xpict ?pclip
              xrc dpy @ 3 win2 0 xpict @ xs ys xs ys x y w h
              XRenderComposite  [THEN]

\ Display                                              21mar04py

          ELSE  clip-is @ 0= IF  y x win1 clip-mask
bp's avatar
bp committed
264
	      drawable win2 -rot xs ys w h x y XCopyArea
265 266
              0 0 0 clip-mask
          ELSE  1 xrc set-function  0 xrc set-color
bp's avatar
bp committed
267
	      drawable win1 -rot xs ys w h x y 1 XCopyPlane
268
              6 xrc set-function
bp's avatar
bp committed
269
	      drawable win2 -rot xs ys w h x y XCopyArea
270
              3 xrc set-function
bp's avatar
bp committed
271 272
          THEN THEN [defined]  xrender [IF] THEN [THEN] ;
        : image { xs ys w h x y win }  ?clip
bp's avatar
bp committed
273
	  drawable win -rot xs ys w h x y XCopyArea ;
274 275 276 277
[THEN]

\ Display                                              27jun02py

bp's avatar
bp committed
278
[defined]  win32 [IF]
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
        : drawable ( -- dc ) xrc dc @ ;
        : set-font ( n -- )  xrc font@ bind cur-font ;
        : set-color ( color -- )  ?clip
          dup xrc color drawable SelectObject drop
          dup xrc pen   drawable SelectObject drop
              xrc rgb   drawable SetTextColor drop ;
        : set-cursor ( n -- )
          cur-cursor @ over = IF  drop  EXIT  THEN
          dup cur-cursor !
          xrc cursor dup SetCursor drop
          GCL_HCURSOR xwin @ SetClassLong drop ;
        : set-linewidth drop ;

\ Display                                              27jun02py

        : line ( x y x y color -- )      set-color
          2swap swap 0 -rot drawable MoveToEx ?err
          swap drawable LineTo ?err ;
        : text ( addr u x y color -- )   set-color
          self cur-font draw ;
        : box ( x y w h color -- )
          >r 2dup 1 1 d= IF
               2drop r> xrc rgb -rot swap drawable SetPixel drop
               EXIT  THEN  r>  set-color
          2over p+ swap 2swap swap drawable Rectangle ?err ;

\ Display                                              27jun02py

        : make-path ( x y addr n color -- n polygon )
          set-color
          2over swap (poly'' @ 2!
          (poly'' @ 2 cells + -rot  dup >r  cells bounds
          ?DO  I wx@+ wx@ \ swap dup 0< + dup 0> - swap
               rot >r p+ 2dup swap r> !+ !+
               cell +LOOP  drop 2drop
          r> 1+ (poly'' @ ;
        : fill ( x y array n color -- )  make-path
          drawable Polygon  ?err ;
        : stroke ( x y array n color -- )  make-path
          drawable Polyline ?err ;

\ Display                                              10aug02py

\        : ?gc ( win -- gc )  dup GetDC dup 0=
\          IF  drop  ELSE  nip  THEN ;
bp's avatar
bp committed
324
        : mask { xs ys w h x y win1 win2 } ?clip
bp's avatar
bp committed
325 326 327
          win1 IF  :srcand ys xs win1 \ ?gc
                   h w y x drawable BitBlt ?err
                   :srcor  ELSE  $00CC0020  THEN  ys xs win2 \ ?gc
bp's avatar
bp committed
328 329
          h w y x drawable BitBlt ?err ;
        : image { xs ys w h x y win }      ?clip
330
          $00CC0020 ys xs win \ ?gc
bp's avatar
bp committed
331
          h w y x  drawable BitBlt ?err ;
332 333 334
[THEN]

\ Display       Clipping rectangle x11                 12may02py
bp's avatar
bp committed
335
[defined]  x11 [IF]
336
        : rect>reg ( rect -- r )  XCreateRegion
bp's avatar
bp committed
337
          dup >r dup XUnionRectWithRegion r> ;
338
        : clip-r? ( -- )  clipregion @ clip-r @ <>
bp's avatar
bp committed
339
          clip-r @ and  IF  clip-r @ XDestroyRegion drop  clip-r off  THEN ;
340 341 342

        : clip-rect ( rect -- )  clip-r? clipregion @
          IF    dup IF    rect>reg >r r@ clipregion @ r@
343
                          XIntersectRegion r>
344 345
                    ELSE  drop clipregion @  THEN
          ELSE  dup IF    rect>reg
bp's avatar
bp committed
346
                    ELSE  drop drawable nip None XSetClipMask
bp's avatar
bp committed
347
                          clip-r off EXIT  THEN
bp's avatar
bp committed
348
          THEN  dup clip-r ! drawable nip rot XSetRegion ;
349 350 351
[THEN]

\ Display       Clipping rectangle win32               20oct99py
bp's avatar
bp committed
352
[defined]  win32 [IF]
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        : clip-rect ( rect -- )  clipregion @
          IF    dup IF    w@+ w@+ w@+ w@ 2over p+
                          swap 2swap swap
                          CreateRectRgn >r
                          RGN_AND r@ clipregion @ r@
                          CombineRgn drop
                          r@ xrc dc @ SelectClipRgn drop
                          rdrop \ no need to destroy it?
                    ELSE  drop clipregion @ xrc dc @
                          SelectClipRgn drop  THEN
          ELSE  dup IF    w@+ w@+ w@+ w@ 2over p+
                          swap 2swap swap CreateRectRgn
                    THEN  xrc dc @ SelectClipRgn drop
          THEN ;                                [THEN]

\ Display                                              25jan03py

bp's avatar
bp committed
370
[defined]  x11 [IF]
371 372 373 374 375
        : add-region ( x y w h -- )
\          base push cr hex xwin @ . decimal
\          ." : add region " 4 0 DO I pick . LOOP
          clipregion @ dup 0= IF  drop XCreateRegion  THEN  >r
          swap 2swap swap  xrect  w!+ w!+ w!+ w!
bp's avatar
bp committed
376
          xrect r@ dup  XUnionRectWithRegion
377 378 379 380 381
          clipregion @ clip-r @ = IF  r@ clip-r !  THEN
          r> clipregion ! ;

\ Display                                              12may02py

bp's avatar
bp committed
382
        : do-exposed ( -- )
bp's avatar
bp committed
383 384 385 386 387 388
          flags #exposed bit@ IF  clipregion @
	      IF  clipregion @ >r
		  drawable nip r@ XSetRegion   draw
		  clip-r? clipregion off  clip-r off
		  r> XDestroyRegion drop  0 clip-rect  THEN  THEN ;
        : do-resize ( -- )
bp's avatar
bp committed
389 390 391 392 393 394
          rw @ -1 <> rh @ -1 <> or
          IF  clipregion @ ?dup IF XDestroyRegion drop  THEN
              clip-r? clipregion off  clip-r off  0 clip-rect
              w @ rw @ <>  h @ rh @ <> or
              IF  xywh 2drop rw @ rh @ resize  THEN  draw
              -1 rw !  -1 rh !  THEN ;
bp's avatar
bp committed
395
        : size-event ( -- )
bp's avatar
bp committed
396
	  do-resize  do-exposed
397 398 399 400 401
          nextwin goto size-event ;
[THEN]

\ Display                                              28mar99py

bp's avatar
bp committed
402
[defined]  win32 [IF]
403 404 405 406 407 408 409 410
        : size-event ( -- )
          rw @ -1 <> rh @ -1 <> or
          IF  w @ rw @ dup w ! <>  h @ rh @ dup h !  <> or
              IF  xywh resize draw  THEN
              -1 rw !  -1 rh !  THEN ;
[THEN]

\ Display                                              07jan07py
bp's avatar
bp committed
411
[defined]  x11 [IF]
412
        : get-event ( mask -- ) dup >r
bp's avatar
bp committed
413
          IF  BEGIN  xrc dpy @ r@ event XCheckMaskEvent  WHILE
414
                     event 0 XFilterEvent
415 416
                     0= IF  childs handle-event  THEN  REPEAT
          ELSE  BEGIN  xrc dpy @  XPending  WHILE
bp's avatar
bp committed
417
                       xrc dpy @ event XNextEvent
418
                       event 0 XFilterEvent
419 420 421
                       0= IF  childs handle-event  THEN
                REPEAT  childs size-event  \ drop
          THEN  rdrop ;
bp's avatar
bp committed
422 423
        : sync ( -- )  xrc dpy @ 0 XSync ;
	: mouse ( -- x y b )  0 sp@ >r 0 sp@ >r 0 sp@ >r
bp's avatar
bp committed
424
	  drawable drop dummy dup dup dup r> r> r>
bp's avatar
bp committed
425
          XQueryPointer drop swap rot 8 >> ;    [THEN]
426 427 428

\ Display                                              18oct98py

bp's avatar
bp committed
429
[defined]  win32 [IF]
bp's avatar
bp committed
430 431 432 433 434 435 436 437
        : dispatch-event ( -- )
	  event TranslateMessage drop  maxascii $80 =
	  IF    event DispatchMessageW drop
	  ELSE  event DispatchMessage drop  THEN ;
        : get-event ( event -- )  drop
	  BEGIN  PM_REMOVE 0 0  0 event PeekMessageW  WHILE
		  dispatch-event pause  REPEAT
	  size-event ;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        : sync ( -- ) ;
        : mouse ( -- x y b )  QS_MOUSEMOVE  get-event
          mx @ my @ mb @ ;

        : win>o ( win -- win o )
          0 ALLCHILDS  over xwin @ = IF  drop ^  THEN ;
[THEN]

\ Display                                              07jan07py

        : screenpos ( -- x y )  x @ y @ ;
        : schedule-event ( -- )  flush-queue
          clicks @ @  IF   click clicked  THEN
          moved?  IF  child-moved  THEN
          nextwin goto schedule-event ;
        : handle-event ( -- )
          0 get-event
          childs schedule-event
          ( invoke ) ;
bp's avatar
bp committed
457 458
        : moved!  !moved on ;
        : moved?  ( -- flag )  !moved @ !moved off ;
459
        : get-dpy ( -- addr )  ^ ;
460
        : mxy! ( mx my -- )  my ! mx ! ;
461 462

\ Display                                              04aug05py
bp's avatar
bp committed
463
[defined]  x11 [IF]
bp's avatar
bp committed
464
        [: event XMotionEvent time @ event-time !
465
          event XMotionEvent x @+ @ mxy!
bp's avatar
bp committed
466
          event XMotionEvent state @ 8 >> mb !  moved! ;]
467 468 469 470 471 472 473
        MotionNotify cells Handlers + !
        | 2Variable comp_stat
        | Variable look_key
        | Create look_chars $100 allot

\ Display                                              04jan07py

bp's avatar
bp committed
474
        [: ( -- ) \ cr 'd emit 'o emit
bp's avatar
bp committed
475
          do-exposed
476
          event XKeyEvent time @ event-time !
bp's avatar
bp committed
477
[defined]  has-utf8 [IF]  xrc ic @ ?dup >r [THEN]
bp's avatar
bp committed
478
          event look_chars $FF look_key comp_stat
bp's avatar
bp committed
479
[defined]  has-utf8 [IF]  r>
480
          IF    Xutf8LookupString
bp's avatar
bp committed
481
          ELSE  XLookupString  THEN
482 483
[ELSE]    XLookupString  [THEN]
          ?dup IF  look_chars swap bounds ?DO
bp's avatar
bp committed
484
                   I xc@+ swap >r event XKeyEvent state @ $3F and keyed
485
               r> I - +LOOP  EXIT  THEN
bp's avatar
bp committed
486
          look_key @ event XKeyEvent state @ $3F and $80 or keyed ;]
487 488 489 490
        KeyPress cells Handlers + !

\ Display                                              11sep05py

bp's avatar
bp committed
491 492
        [: \ cr ." mapping notify"
          event XRefreshKeyboardMapping drop ;]
493
        MappingNotify cells Handlers + !
bp's avatar
bp committed
494
        : click^ ( -- event )  clicks @ @+ swap 8* + ;
495
        : transclick ( x y -- x' y' ) ;
bp's avatar
bp committed
496
	: sendclick ( count event -- )  flags #pending +bit  click^ >r >r
497 498 499 500 501
          r@ XButtonEvent state @
          r@ XButtonEvent button @ $80 swap << xor
          r@ XButtonEvent x @
          r> XButtonEvent y @ transclick swap
          r> w!+ w!+ w!+ w!  ;
bp's avatar
bp committed
502
        : !xyclick ( event -- )  click^ >r
503 504 505 506 507 508
          dup XButtonEvent x @ swap
              XButtonEvent y @ transclick swap r> w!+ w! ;

\ Display                                              05aug99py

        : in-time? ( time flag -- flag )
Bernd Paysan's avatar
Bernd Paysan committed
509 510
          lasttime @ rot - negate swap lastclick @ =
          IF  sameclick  ELSE  twoclicks  THEN < ;
bp's avatar
bp committed
511
        : samepos? ( event -- flag )  flags #pending bit@
bp's avatar
bp committed
512
          IF    XButtonEvent x @+ @  click^ w@+ w@ p-
513 514 515 516 517
                dup * swap dup * + samepos <
          ELSE  drop false  THEN ;
        : moreclicks ( -- )
          clicks @ @ maxclicks 1- < negate clicks @ +! ;
        : flush-queue ( -- )  XTime xrc timeoffset @ @ +
bp's avatar
bp committed
518
          lasttime @ - twoclicks > flags #pending bit@ and
bp's avatar
bp committed
519
          IF  click^ 6+ w@ 1 and
bp's avatar
bp committed
520
              IF  moreclicks  THEN  flags #pending -bit  THEN  ;
bp's avatar
bp committed
521
        : +clicks ( -- ) click^ 6+ dup w@ 2+ -2 and swap w! ;
522 523 524 525 526

\ Display                                              09mar99py
        : .button cr base push hex event XButtonEvent window
          @+ @+ @ swap rot xwin @ 9 .r 9 .r 9 .r 9 .r
          space event XButtonEvent x @+ swap . @ . ;
bp's avatar
bp committed
527
        [: ( -- )
bp's avatar
bp committed
528 529
          do-exposed
          event XButtonEvent time @ event-time !
530 531 532 533
          event XButtonEvent time @ dup true in-time?
          swap lasttime !  IF   event samepos?
               IF  lastclick @
                   IF    1 event XButtonEvent button @ 1- <<
bp's avatar
bp committed
534 535
                         click^ 5 + dup c@ rot or swap c!
                   ELSE  click^ 6 + w@ -2 and 1+ event sendclick
536
                         lastclick on
bp's avatar
bp committed
537 538
                   THEN  EXIT  THEN   event !xyclick flags #pending -bit
          THEN  flags #pending bit@  IF  moreclicks  THEN
bp's avatar
bp committed
539
          1 event sendclick lastclick on ;]
540 541 542
          ButtonPress cells Handlers + !

\ Display                                              09mar99py
bp's avatar
bp committed
543
	[: ( -- )
bp's avatar
bp committed
544 545
          do-exposed
	  event XButtonEvent time @ event-time !
546 547 548 549 550
          event XButtonEvent time @ dup 0 in-time?
          swap lasttime !
          IF  event samepos?  IF  lastclick @
              IF    +clicks  lastclick off
                    moreclicks 2 event sendclick
bp's avatar
bp committed
551
              ELSE  click^ 6+ w@ 1 and
552
                    IF  1 event XButtonEvent button @ 1- <<
bp's avatar
bp committed
553 554
                        click^ 5 + dup c@ rot invert and swap c!
                    THEN  THEN  EXIT  THEN  click^ 6+ w@  1 and
555 556
              ELSE true THEN  \ output push display .button
          IF  event !xyclick +clicks moreclicks  THEN
bp's avatar
bp committed
557
          flags #pending bit@ 0= >r
bp's avatar
bp committed
558
	  2 event sendclick  lastclick off
bp's avatar
bp committed
559
	  r> IF  flags #pending -bit  THEN ;]
560 561 562 563 564 565 566 567 568 569 570 571 572 573
          ButtonRelease cells Handlers + !

\ Display                                              28jun98py

        : click?  ( -- n )  clicks @ @ 0=
          IF  0 get-event  THEN  clicks @ @ ;
        : click   ( -- x y b n )
          BEGIN  pause click?  UNTIL
          -1 clicks @ +!  clicks @ cell+ wx@+ wx@+ c@+ c@+ w@
          rot kbshift !
          clicks @ $C + dup 8 - clicks @ @ 8* move ;

\ Display                                              04aug05py

bp's avatar
bp committed
574
        [:
575 576
          event XExposeEvent x @+ @+ @+ @  add-region
\         event XExposeEvent count @ 0= IF ."  draw"  draw  THEN
bp's avatar
bp committed
577
          flags #exposed +bit ;]
578 579
        dup Expose         cells Handlers + !
            GraphicsExpose cells Handlers + !
bp's avatar
bp committed
580 581
        [: do-exposed pointed self
          IF  mx @ my @ pointed moved  THEN ;]
bp's avatar
bp committed
582
        EnterNotify    cells Handlers + !
bp's avatar
bp committed
583 584
        [: do-exposed pointed self
          IF  pointed leave  0 bind pointed  moved? drop  THEN ;]
585 586 587 588 589 590
        LeaveNotify    cells Handlers + !

\ Display                                              23apr06py

        Create xev  here  sizeof XEvent  dup allot erase

bp's avatar
bp committed
591
        [: \ cr  ." Selection Notify "
592 593 594
          event XSelectionRequestEvent time @ event-time !
          event XSelectionEvent property @
          event XSelectionEvent requestor @
bp's avatar
bp committed
595
          xrc dpy @ fetch-property ;]
596 597
        SelectionNotify cells Handlers + !

bp's avatar
bp committed
598 599
        [: \ cr  ." Selection Clear "
          own-selection off ;]
600 601 602 603
        SelectionClear  cells Handlers + !

\ Display                                              16jan05py

bp's avatar
bp committed
604
: rest-request { addr n mode format type }
605 606 607 608 609
    xrc dpy @
    event XSelectionRequestEvent requestor @
    event XSelectionRequestEvent property @ dup >r
    type format mode addr n
    XChangeProperty drop sync
610 611
  r> xev XSelectionEvent property ! ;
: string-request ( -- )
bp's avatar
bp committed
612
  (@select PropModeReplace 8 XA_STRING rest-request ;
613
: string8-request ( -- )
bp's avatar
bp committed
614
  (@select PropModeReplace 8 XA_STRING8 rest-request ;
615 616 617 618 619 620 621
: compound-request ( -- )  string-request ;

\ Display                                              23apr06py

| Create 'string XA_STRING , XA_STRING8 ,
: target-request ( -- )
  XA_STRING8 XA_STRING 'string 2!
bp's avatar
bp committed
622
  'string 2 PropModeReplace #32 4 rest-request ;
623 624

\ Display                                              16jan05py
bp's avatar
bp committed
625
        [: \ cr  ." Selection Request "
626 627 628 629 630 631
          event XSelectionRequestEvent time @ event-time !
          event xev 4 cells move
          event XSelectionRequestEvent requestor
          xev XSelectionEvent requestor 6 cells move
          xev XSelectionEvent property off
          SelectionNotify xev XSelectionEvent type !
bp's avatar
bp committed
632
          xrc dpy @ event XSelectionRequestEvent target @
bp's avatar
bp committed
633
          XGetAtomName >len  1 0 DO \ output push display
634 635 636 637 638
          2dup s" STRING"  str= IF  string8-request LEAVE  THEN
      2dup s" UTF8_STRING" str= IF  string-request  LEAVE  THEN
          2dup s" TARGETS" str= IF  target-request  LEAVE  THEN
          2dup s" COMPOUND_TEXT" str=
                                IF  compound-request LEAVE THEN
bp's avatar
bp committed
639
          LOOP  ( 2dup type cr ) 2drop
640 641 642

\ Display                                              07jan05py

bp's avatar
bp committed
643 644
          xrc dpy @
	  event XSelectionRequestEvent requestor @
bp's avatar
bp committed
645
          0 0 xev XSendEvent drop ;]
646 647 648 649
        SelectionRequest cells Handlers + !

\ Display                                              07jan07py

bp's avatar
bp committed
650 651
        [:  flags #exposed +bit ;] NoExpose cells Handlers + !
        [: ( -- ) \ resize request
652 653 654
           event XConfigureEvent x @ x !
           event XConfigureEvent y @ y !
           event XConfigureEvent width  @ rw !
bp's avatar
bp committed
655
           event XConfigureEvent height @ rh ! ;]
656
        ConfigureNotify cells Handlers + !
bp's avatar
bp committed
657
	[: do-exposed  focus ;]
bp's avatar
bp committed
658
	  FocusIn  cells Handlers + !
bp's avatar
bp committed
659
	[: do-exposed  defocus ;]
bp's avatar
bp committed
660
	  FocusOut cells Handlers + !
bp's avatar
bp committed
661
        : >exposed ( -- )  sync  flags #exposed -bit
662
          BEGIN  ( ExposureMask ) 0 get-event
bp's avatar
bp committed
663
                 pause  flags #exposed bit@  UNTIL ;
664 665

\ Display                                              02aug98py
bp's avatar
bp committed
666 667
        [: ( -- )
          event sizeof XClientMessageEvent dump ;]
668 669 670 671 672
        ClientMessage cells Handlers + !

[THEN]

\ Display                                              19jan00py
bp's avatar
bp committed
673
[defined]  win32 [IF]        Create paint  $40 allot
bp's avatar
bp committed
674
        [: ( lparam wparam msg win -- ret )
675 676 677
          xrc dc @ >r  paint xwin @ BeginPaint xrc dc !
          Xform0 xrc dc @ SetWorldTransform drop
          draw  paint xwin @ EndPaint drop  r> xrc dc !
bp's avatar
bp committed
678 679
          2drop 2drop 0 flags #exposed +bit ;]        WM_PAINT Handler@ !
        [:  3 pick >lohi y ! x ! DefWindowProc ;]
680
                                             WM_MOVE  Handler@ !
bp's avatar
bp committed
681 682
        [:  2drop 2drop close 0 ;]      WM_CLOSE Handler@ !
        [:  3 pick WINDOWPOS flags @ SWP_NOSIZE and
683 684 685
          IF  DefWindowProc  EXIT  THEN  2drop drop
          WINDOWPOS cx 2@ 0. 0. sp@ 0 style @ rot
          AdjustWindowRect drop p- p- rw ! rh !
bp's avatar
bp committed
686
          size-event 0 ;]         WM_WINDOWPOSCHANGED Handler@ !
687 688

\ Display                                              28jul07py
bp's avatar
bp committed
689
        [: 2drop drop { rect |
690 691 692 693 694 695 696 697 698
          vglue >r hglue >r 0 0 sp@ >r 0 style @ r>
          AdjustWindowRect drop p- rect 2@ p+
          dup r> + 2 pick r> + >r >r
          rect 2 cells + 2@ rot max >r max r>
          r> min swap r> min swap
          yinc >r xinc >r 0 0 sp@ >r 0 style @ r>
          AdjustWindowRect drop p- rect 2@ p+
          rot over - r@ 2/ + r@ / r> * + -rot swap
              over - r@ 2/ + r@ / r> * + swap
bp's avatar
bp committed
699 700 701 702 703
          rect 2 cells + 2! 0 } ;]          WM_SIZING Handler@ !
 \        [: ( lparam wparam msg win -- ret )
 \        DefWindowProc ;]         WM_INPUTLANGCHANGE Handler@ !
 \       [: ( lparam wparam msg win -- ret )
 \        DefWindowProc ;]  WM_INPUTLANGCHANGEREQUEST Handler@ !
704 705 706 707 708 709 710

\ Display                                              19jan00py

        : >kshift ( n -- n' )  VK_SHIFT - 1 swap <<
          dup 1 <> IF  2*  THEN ;
        : shift@ ( -- kbshift )  0
          VK_MENU 1+ VK_SHIFT ?DO
bp's avatar
bp committed
711
              I GetKeyState $8000 and IF  I >kshift or  THEN
712 713 714 715 716 717 718 719 720 721 722 723 724 725
          LOOP ;
public: displays ptr grab-key
private:
        : ?keyed  grab-key self
          IF  grab-key keyed  ELSE  keyed  THEN ;
        : ?grab  grab-key self
          IF  r> grab-key self >o execute o>  THEN ;

\ Display                                              29jul07py

        | Create xkeys  $FF55 , $FF56 , $FF57 , $FF50 ,
                        $FF51 , $FF52 , $FF53 , $FF54 ,
                        0 ,     0 ,     0 ,     0 ,
                        $0000 , $007F ,
bp's avatar
bp committed
726
        [: 2drop nip dup $21 $2F within
727 728
          IF    $21 - cells xkeys + @ ?dup
                IF  shift@ ?keyed  THEN
bp's avatar
bp committed
729 730
          ELSE  drop  THEN  0 ;]           WM_KEYDOWN Handler@ !
        [:  2drop nip shift@       ?keyed 0 ;]
731
                                              WM_CHAR Handler@ !
bp's avatar
bp committed
732
 \       [:  2drop nip shift@       ?keyed 0 ;]
733
 \                                        WM_IME_CHAR Handler@ !
bp's avatar
bp committed
734
        [:  2drop nip shift@ ( 8 or )  ?keyed 0 ;]
735 736 737 738
                                           WM_SYSCHAR Handler@ !

\ Display                                              12aug00py

bp's avatar
bp committed
739
        : click^ ( -- event )  clicks @ @+ swap 8* + ;
740 741 742 743 744 745
        : >mshift ( fwkeys -- mstate ) 0
          over $01 and 0<> $100 and or
          over $02 and 0<> $400 and or
          over $10 and 0<> $200 and or
          dup $500 = emulate-3button @ and IF  $700 xor  THEN
          nip shift@ or ;
bp's avatar
bp committed
746
        : !xyclick ( event -- )  click^ >r
bp's avatar
bp committed
747
          MSG lparam lohi@ swap r> w!+ w! ;
748
        : sendclick ( count event -- )
bp's avatar
bp committed
749
          flags #pending +bit  click^ >r
750
          dup MSG wparam @ >mshift swap
bp's avatar
bp committed
751
              MSG lparam lohi@ swap r> w!+ w!+ w!+ w! ;
752 753 754

\ Display                                              19jan00py
        : in-time? ( time flag -- flag )
Bernd Paysan's avatar
Bernd Paysan committed
755
          lasttime @ rot - negate swap lastclick @ =
756
          IF  sameclick  ELSE  twoclicks  THEN  < ;
bp's avatar
bp committed
757
        : samepos? ( event -- flag )  flags #pending bit@
bp's avatar
bp committed
758
          IF    MSG lparam lohi@ ( swap ) click^ w@+ w@ p-
759 760 761 762 763
                dup * swap dup * + samepos <
          ELSE  drop false  THEN ;
        : moreclicks ( -- )
          clicks @ @ maxclicks 1- < negate clicks @ +! ;
        : flush-queue ( -- )  GetTickCount
bp's avatar
bp committed
764
          lasttime @ - twoclicks > flags #pending bit@ and
bp's avatar
bp committed
765
          IF  click^ 6+ w@ 1 and
bp's avatar
bp committed
766
              IF  moreclicks  THEN  flags #pending -bit
767
              ( ReleaseCapture drop )  THEN  ;
bp's avatar
bp committed
768
        : +clicks ( -- ) click^ 6+ dup w@ 2+ -2 and swap w! ;
769 770

\ Display                                              19jan00py
bp's avatar
bp committed
771
        [: ( lparam wparam msg win -- 0 ) ?grab \ add press
772 773 774 775 776
          SetCapture 2drop 2drop
          event MSG time @ dup true in-time?
          swap lasttime !
          IF   event samepos?
               IF  lastclick @
bp's avatar
bp committed
777 778
                   IF   event MSG wparam @ >mshift click^ 4 + w!
                   ELSE  click^ 6 + w@ -2 and 1+ event sendclick
779
                         lastclick on
bp's avatar
bp committed
780 781
                   THEN  0 EXIT  THEN event !xyclick flags #pending -bit
          THEN  flags #pending bit@  IF  moreclicks  THEN
bp's avatar
bp committed
782
          1 event sendclick lastclick on 0 ;]
783 784 785 786 787
                                   dup WM_LBUTTONDOWN Handler@ !
                                   dup WM_RBUTTONDOWN Handler@ !
                                       WM_MBUTTONDOWN Handler@ !

\ Display                                              19jan00py
bp's avatar
bp committed
788
        [:  2drop $13 and 0= IF ReleaseCapture drop THEN
789 790 791 792 793
          ?grab  drop event MSG time @ dup 0 in-time?
          swap lasttime !
          IF  event samepos?  IF  lastclick @
              IF    +clicks  lastclick off
                    moreclicks 2 event sendclick
bp's avatar
bp committed
794 795 796
              ELSE  click^ 6+ w@ 1 and
                    IF  event MSG wparam @ >mshift click^ 4 + w!
                    THEN  THEN 0 EXIT  THEN  click^ 6+ w@  1 and
797 798
              ELSE true THEN  \ output push display .button
          IF  event !xyclick +clicks moreclicks  THEN
bp's avatar
bp committed
799
          flags #pending bit@ 0= >r
bp's avatar
bp committed
800
	  2 event sendclick  lastclick off 0
bp's avatar
bp committed
801
	  r> IF  flags #pending -bit  THEN ;]
802 803 804 805 806 807
                                   dup WM_LBUTTONUP   Handler@ !
        dup WM_RBUTTONUP   Handler@ !  WM_MBUTTONUP   Handler@ !

\ mouse wheel                                          01jun07py
        : >wshift ( fwkeys -- count mstate ) dup >r >mshift
          r@ 0< $1000 and or  r@ 0> $800 and or
bp's avatar
bp committed
808
          r> 16 >> #60 / abs swap ;
bp's avatar
bp committed
809
        : sendwheel ( event -- )  flags #pending +bit
810 811
          dup MSG wparam @ >wshift drop 0 ?DO
             dup   MSG wparam @ >wshift nip I 2+ swap
bp's avatar
bp committed
812
             over2 MSG lparam lohi@ y @ - swap x @ -
bp's avatar
bp committed
813
             click^ w!+ w!+ w!+ w!
814 815
             moreclicks
          2 +LOOP drop ;
bp's avatar
bp committed
816
        [: ( lparam wparam msg win -- ) moved!
bp's avatar
bp committed
817
          flags #pending bit@ IF  moreclicks flags #pending -bit  THEN
818
          event MSG time @  lasttime !
bp's avatar
bp committed
819
          2drop 2drop event sendwheel 0 ;]
820 821 822 823 824 825 826 827 828 829 830 831
                                        WM_MOUSEWHEEL Handler@ !

\ Display                                              12aug00py
        : click?  ( -- n )  clicks @ @ 0=
          IF  0 get-event  THEN  clicks @ @ ;
        : click   ( -- x y b n )
          BEGIN  pause click?  UNTIL
          -1 clicks @ +!  clicks @ cell+ wx@+ wx@+ c@+ c@+ w@
          rot kbshift ! \ kb-shift off
          clicks @ $C + dup 8 - clicks @ @ 8* move
          ( 2over 2over cr . . . . ) ;

bp's avatar
bp committed
832
        [:  2drop drop >r
833 834
          vglue + hglue +
          0. sp@ 0 style @ rot AdjustWindowRect drop p-
bp's avatar
bp committed
835
          r> $8 + 2! 0 ;]            WM_GETMINMAXINFO Handler@ !
836 837

\ Display                                              29jul07py
bp's avatar
bp committed
838 839
        [: ( lparam wparam msg win -- ) ?grab moved!
          2drop >mshift $FF and mb ! >lohi mxy! 0 ;]
840
                                         WM_MOUSEMOVE Handler@ !
bp's avatar
bp committed
841
        [:  pointed self
842
          IF  pointed leave 0 bind pointed  THEN
843
          DefWindowProc ;              WM_NCMOUSEMOVE Handler@ !
844

bp's avatar
bp committed
845 846 847
        [: focus   2drop 2drop 0 ;]  WM_SETFOCUS  Handler@ !
        [: defocus 2drop 2drop 0 ;]  WM_KILLFOCUS Handler@ !
        [: ( lparam wparam msg win -- )
848
          2drop 2drop get-sys-colors xrc free-colors
bp's avatar
bp committed
849
          xrc colors 0 ;]            WM_SYSCOLORCHANGE Handler@ !
850 851 852 853 854 855 856 857
        : >exposed ;
[THEN]
class;

\ Display                                              21oct99py

displays ptr screen

bp's avatar
bp committed
858
[defined]  x11 [IF]
bp's avatar
bp committed
859 860 861
    [:  ( -- )  0 screen get-event ;] IS screen-event
    [:  ( -- )   screen sync ;] IS screen-sync
    [:  ( ic -- ) screen xrc ic ! ;] IS screen-ic!
862 863
[THEN]

bp's avatar
bp committed
864
[defined]  win32 [IF]
bp's avatar
bp committed
865
    [: ( win -- o )  screen win>o nip ;] IS win>o
866 867 868 869 870 871 872 873 874 875
[THEN]

\ font implementation                                  21aug99py

font implements
        : display  >r color @
          r@ displays with  set-color  endwith
          addr @ u @ 2swap r> draw ;
class;