[TT# 1592][t] Improve test for open opcode delegation. All tests in the file pass...
[parrot.git] / examples / nci / xlibtest.rb
blob150ab978bfe1f28b843a23fb61d671bee06516d7
1 # xlibtest.rb - A test of Xlib.pir usage from cardinal
2
3 # To run this file, execute the following command from the
4 # current directory:
5
6 # ../../parrot ../../languages/cardinal/cardinal.pbc  xlibtest.rb
7
8 # (You must have the cardinal pbc already builded).
9
10 # Press Esc key to exit the program.
11
12 # Parsing is very slow.  Give it a minute to start up.
13 require 'Xlib'
15 puts 'Hello'
17 puts 'Display: ' + Xlib::DisplayName()
19 display = Xlib::OpenDisplay('')
21 puts 'Default screen: ' + display.DefaultScreen()
23 display.hello()
25 white  = display.WhitePixel(0)
26 root   = display.RootWindow()
27 window = display.CreateSimpleWindow(root, 0, 0, 600, 400, 0, 0, white)
28 window.StoreName("Hello, ruby")
29 window.SelectInput(163919)
30 window.Map()
32 # Get Escape keycode
33 keysym = Xlib::StringToKeysym('Escape')
34 code_escape = display.KeysymToKeycode(keysym)
36 event = Xlib::newEvent()
38 type = 0
40 lastx = 0
41 lasty = 0
42 pressed = 0
43 listline = Array.new()
44 line = Array.new()
46 while type != 17
47     display.NextEvent(event)
48     type = event.type()
49     if type == 4
50         x = event.x()
51         y = event.y()
52         window.DrawPoint(x, y)
53         lastx = x
54         lasty = y
55         pressed = 1
56     end
57     if type == 5
58         newline = line
59         listline.push(newline)
60         line = Array.new()
61         pressed = 0
62     end
63     if type == 6 && pressed
64         x = event.x()
65         y = event.y()
66         if x != lastx || y != lasty
67             window.DrawLine(lastx, lasty, x, y)
68             lastx = x
69             lasty = y
70             line.push(x, y)
71         end
72     end
73     if type == 12
74         #puts 'Exposed. Lines: ', +@listline
75         listline.each do |l|
76             #puts 'Points ', l.elems
77             if l.elems  > 0
78                 lx = l[0]
79                 ly = l[1]
80                 #puts lx, ' ', ly
81                 window.DrawPoint(lx, ly)
83                 i = 2
84                 while i < l.elems
85                     x = l[i]
86                     y = l[i+1]
87                     window.DrawLine(lx, ly, x, y)
88                     lx = x
89                     ly = y
90                     #puts lx, ' ', ly
91                     i += 2
92                 end
93             end
94         end
95     end
96     if type == 2
97         code = event.keycode()
98         if code == code_escape
99             window.Unmap()
100             window.Destroy()
101         end
102     end
103     if type == 33
104         window.Unmap()
105         window.Destroy()
106     end
109 display.Close()
111 puts 'Bye'