Home   Archive   Permalink



What exactly is 'do-events' for?

I have written a few REBOL2 applications using VIEW, with success. I usually make a main window, with buttons that do things including sometimes viewing other windows. It all seems to work fine.
    
When I look at sample programs, on rebol.org for example, I sometimes see programs that display a window and then "do-events." I don't understand what "do-events" does for me since my programs seem to work fine without it.
    
Thank you.

posted by:   Steven White       15-Oct-2013/10:19:13-7:00



It's usually used together with view/new, in cases where you need the GUI layout to exist before showing it, usually to perform some calculation before the event loop begins (before the system starts watching for user events). Do-events just starts the event loop (so you can also use it to restart a GUI that's stopped do to an error).
    
In this example, I wanted the cards to be arranged before the GUI layout is shown. In order to do that, the GUI needs to exist, so I used view/new, then ran the arrange-cards functions, then do-events:
    
R E B O L [title: "Playing Card Framework - Freecell"]
flash "Downloading card images..."
do load-thru http://www.re-bol.com/playing-cards.r
unview
random/seed now
loop 156 [
     pos1: pick cards rnd1: (random 52) * 5
     pos2: pick cards rnd2: (random 52) * 5
     poke cards rnd1 pos2
     poke cards rnd2 pos1
]
movestyle: [
     engage: func [face action event] [
         if action = 'down [
             start-coord: face/offset
             face/data: event/offset
             remove find face/parent-face/pane face
             append face/parent-face/pane face
         ]
         if find [over away] action [
             unrounded-pos: (face/offset + event/offset - face/data)
             snap-to-x: (round/to first unrounded-pos 80) + 20
             snap-to-y: (round/to second unrounded-pos 20) + 20
             face/offset: (as-pair snap-to-x snap-to-y)
         ]
         if action = 'up [
             if any [
                 (find cards face/offset)
                 (face/offset/2 < 20)
             ] [
                 if (face/offset/2 < 398) [face/offset: start-coord]
             ]
             replace cards start-coord face/offset
             arrange-cards
         ]
         show face
     ]
]
positions: does [
     temp: copy []
     foreach item cards [if ((type? item) = pair!) [append temp item]]
     return sort temp
]
arrange-cards: does [
     foreach position positions [
         foreach card system/view/screen-face/pane/1/pane [
             if (card/offset = position) and (position/2 < 398) [
                 remove find system/view/screen-face/pane/1/pane card
                 append system/view/screen-face/pane/1/pane card
             ]
         ]
     ]
     show system/view/screen-face/pane/1/pane
]
gui: [size 670x510 backdrop 0.150.0 across ]
foreach [card label num color pos] cards [
     append gui compose [
         at (pos) image load to-binary decompress (card) feel movestyle
     ]
]
box-pos: 18x398
loop 4 [
     append gui compose [
         at (box-pos) box green 72x2
         at (box-pos) box green 2x97
         at (box-pos + 320x0) box white 72x2
         at (box-pos + 320x0) box white 2x97
     ]
     box-pos: box-pos + 80x0
]
view/new center-face layout gui
arrange-cards
do-events
    
You'll see a similar procedure in this image capture code:
    
avicap32.dll: load/library %avicap32.dll
user32.dll: load/library %user32.dll
find-window-by-class: make routine! [
     ClassName [string!] WindowName [integer!] return: [integer!]
] user32.dll "FindWindowA"
sendmessage: make routine! [
     hWnd [integer!] val1 [integer!] val2 [integer!] val3 [integer!]
     return: [integer!]
] user32.dll "SendMessageA"
sendmessage-file: make routine! [
     hWnd [integer!] val1 [integer!] val2 [integer!] val3 [string!]
     return: [integer!]
] user32.dll "SendMessageA"
cap: make routine! [
     cap [string!] child-val1 [integer!] val2 [integer!] val3 [integer!]
     width [integer!] height [integer!] handle [integer!]
     val4 [integer!] return: [integer!]
] avicap32.dll "capCreateCaptureWindowA"
view/new center-face layout/tight [
     image 320x240
     across
     btn "Take Snapshot" [
         sendmessage cap-result 1085 0 0
         sendmessage-file cap-result 1049 0 "scrshot.bmp"
         browse %scrshot.bmp
     ]
     btn "Exit" [
         sendmessage cap-result 1205 0 0
         sendmessage cap-result 1035 0 0
         free user32.dll quit
     ]
]
hwnd: find-window-by-class "REBOLWind" 0
cap-result: cap "cap" 1342177280 0 0 320 240 hwnd 0
sendmessage cap-result 1034 0 0
sendmessage cap-result 1077 1 0
sendmessage cap-result 1075 1 0
sendmessage cap-result 1074 1 0
sendmessage cap-result 1076 1 0
do-events

posted by:   Nick       16-Oct-2013/4:40:38-7:00