rndk 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -4
- data/TODO +21 -1
- data/demos/appointment.rb +279 -299
- data/demos/clock.rb +13 -8
- data/demos/rss-reader.rb +84 -0
- data/examples/01-hello-world.rb +13 -11
- data/examples/02-colors.rb +14 -21
- data/examples/03-markup.rb +7 -7
- data/examples/04-quick-widgets.rb +2 -2
- data/examples/05-position-widget.rb +50 -31
- data/examples/06-callbacks.rb +77 -0
- data/examples/07-traverse.rb +90 -0
- data/examples/10-all-widgets.rb +165 -0
- data/examples/calendar.rb +20 -32
- data/examples/entry.rb +15 -20
- data/examples/label.rb +11 -11
- data/examples/scroll.rb +16 -60
- data/examples/slider.rb +18 -19
- data/examples/viewer.rb +65 -0
- data/lib/rndk.rb +28 -7
- data/lib/rndk/alphalist.rb +309 -313
- data/lib/rndk/button.rb +239 -157
- data/lib/rndk/buttonbox.rb +136 -103
- data/lib/rndk/calendar.rb +246 -203
- data/lib/rndk/core/color.rb +63 -13
- data/lib/rndk/core/display.rb +1 -1
- data/lib/rndk/core/draw.rb +11 -11
- data/lib/rndk/core/markup.rb +21 -21
- data/lib/rndk/core/quick_widgets.rb +75 -96
- data/lib/rndk/core/screen.rb +145 -102
- data/lib/rndk/core/traverse.rb +150 -136
- data/lib/rndk/core/utils.rb +5 -6
- data/lib/rndk/core/widget.rb +207 -191
- data/lib/rndk/core/widget_bind.rb +108 -0
- data/lib/rndk/dialog.rb +88 -56
- data/lib/rndk/entry.rb +79 -64
- data/lib/rndk/fscale.rb +38 -20
- data/lib/rndk/fslider.rb +38 -23
- data/lib/rndk/graph.rb +92 -53
- data/lib/rndk/itemlist.rb +80 -62
- data/lib/rndk/label.rb +111 -77
- data/lib/rndk/radio.rb +138 -128
- data/lib/rndk/scale.rb +123 -122
- data/lib/rndk/scroll.rb +444 -391
- data/lib/rndk/scroller.rb +21 -21
- data/lib/rndk/slider.rb +149 -140
- data/lib/rndk/template.rb +74 -61
- data/lib/rndk/version.rb +1 -1
- data/lib/rndk/viewer.rb +499 -298
- metadata +8 -14
- data/demos/fileview.rb +0 -141
- data/lib/rndk/dscale.rb +0 -13
- data/lib/rndk/fselect.rb +0 -938
- data/lib/rndk/histogram.rb +0 -412
- data/lib/rndk/marquee.rb +0 -244
- data/lib/rndk/matrix.rb +0 -1189
- data/lib/rndk/mentry.rb +0 -619
- data/lib/rndk/menu.rb +0 -478
- data/lib/rndk/selection.rb +0 -630
- data/lib/rndk/swindow.rb +0 -766
- data/lib/rndk/uscale.rb +0 -14
- data/lib/rndk/uslider.rb +0 -14
data/lib/rndk/button.rb
CHANGED
@@ -1,31 +1,85 @@
|
|
1
1
|
require 'rndk'
|
2
2
|
|
3
3
|
module RNDK
|
4
|
-
|
5
|
-
|
4
|
+
|
5
|
+
class Button < Widget
|
6
|
+
|
7
|
+
# A button with a label attached to a callback.
|
8
|
+
#
|
9
|
+
# ## Settings
|
10
|
+
#
|
11
|
+
# * `x` is the x position - can be an integer or `RNDK::LEFT`,
|
12
|
+
# `RNDK::RIGHT`, `RNDK::CENTER`.
|
13
|
+
# * `y` is the y position - can be an integer or `RNDK::TOP`,
|
14
|
+
# `RNDK::BOTTOM`, `RNDK::CENTER`.
|
15
|
+
# * `label` is the String that will appear on the button.
|
16
|
+
# * `action` is a Proc that will execute when the button is
|
17
|
+
# pressed.
|
18
|
+
# * `box` if the Widget is drawn with a box outside it.
|
19
|
+
# * `shadow` turns on/off the shadow around the Widget.
|
20
|
+
#
|
21
|
+
# ## Usage
|
22
|
+
# ```
|
23
|
+
# b = RNDK::Button.new(screen, {
|
24
|
+
# :x => 50,
|
25
|
+
# :y => 8,
|
26
|
+
# :label => "</77>button",
|
27
|
+
# })
|
28
|
+
#
|
29
|
+
# b.bind_signal(:pressed) do
|
30
|
+
# screen.popup_label "Button pressed"
|
31
|
+
# end
|
32
|
+
# ```
|
33
|
+
#
|
34
|
+
# @note When binding signals, remember that if you return
|
35
|
+
# `false` it stops executing other signals.
|
36
|
+
# So that's a nice idea for a `:before_pressing`
|
37
|
+
# signal that asks the user if he's certain of pressing.
|
38
|
+
def initialize(screen, config={})
|
6
39
|
super()
|
7
|
-
|
8
|
-
|
40
|
+
@widget_type = :button
|
41
|
+
@supported_signals += [:before_pressing, :pressed]
|
42
|
+
|
43
|
+
x = 0
|
44
|
+
y = 0
|
45
|
+
label = "button"
|
46
|
+
action = nil
|
47
|
+
box = true
|
48
|
+
shadow = false
|
49
|
+
|
50
|
+
config.each do |key, val|
|
51
|
+
x = val if key == :x
|
52
|
+
y = val if key == :y
|
53
|
+
label = val if key == :label
|
54
|
+
action = val if key == :action
|
55
|
+
box = val if key == :box
|
56
|
+
shadow = val if key == :shadow
|
57
|
+
end
|
58
|
+
|
59
|
+
parent_width = Ncurses.getmaxx screen.window
|
60
|
+
parent_height = Ncurses.getmaxy screen.window
|
9
61
|
box_width = 0
|
10
|
-
|
11
|
-
|
62
|
+
x = x
|
63
|
+
y = y
|
12
64
|
|
13
|
-
self.set_box
|
65
|
+
self.set_box box
|
14
66
|
box_height = 1 + 2 * @border_size
|
15
67
|
|
16
68
|
# Translate the string to a chtype array.
|
17
69
|
info_len = []
|
18
70
|
info_pos = []
|
19
|
-
@info = RNDK.char2Chtype(
|
71
|
+
@info = RNDK.char2Chtype(label, info_len, info_pos)
|
20
72
|
@info_len = info_len[0]
|
21
73
|
@info_pos = info_pos[0]
|
22
74
|
box_width = [box_width, @info_len].max + 2 * @border_size
|
23
75
|
|
24
76
|
# Create the string alignments.
|
25
77
|
@info_pos = RNDK.justifyString(box_width - 2 * @border_size,
|
26
|
-
|
78
|
+
@info_len,
|
79
|
+
@info_pos)
|
27
80
|
|
28
|
-
# Make sure we didn't extend beyond the dimensions of the
|
81
|
+
# Make sure we didn't extend beyond the dimensions of the
|
82
|
+
# window.
|
29
83
|
box_width = if box_width > parent_width
|
30
84
|
then parent_width
|
31
85
|
else box_width
|
@@ -36,23 +90,22 @@ module RNDK
|
|
36
90
|
end
|
37
91
|
|
38
92
|
# Rejustify the x and y positions if we need to.
|
39
|
-
xtmp = [
|
40
|
-
ytmp = [
|
41
|
-
RNDK.alignxy(
|
42
|
-
|
43
|
-
|
93
|
+
xtmp = [x]
|
94
|
+
ytmp = [y]
|
95
|
+
RNDK.alignxy(screen.window, xtmp, ytmp, box_width, box_height)
|
96
|
+
x = xtmp[0]
|
97
|
+
y = ytmp[0]
|
44
98
|
|
45
99
|
# Create the button.
|
46
|
-
@screen =
|
100
|
+
@screen = screen
|
47
101
|
# ObjOf (button)->fn = &my_funcs;
|
48
|
-
@parent =
|
49
|
-
@win = Ncurses.newwin(box_height, box_width,
|
102
|
+
@parent = screen.window
|
103
|
+
@win = Ncurses.newwin(box_height, box_width, y, x)
|
50
104
|
@shadow_win = nil
|
51
|
-
@
|
52
|
-
@
|
105
|
+
@x = x
|
106
|
+
@y = y
|
53
107
|
@box_width = box_width
|
54
108
|
@box_height = box_height
|
55
|
-
@callback = callback
|
56
109
|
@input_window = @win
|
57
110
|
@accepts_focus = true
|
58
111
|
@shadow = shadow
|
@@ -64,76 +117,73 @@ module RNDK
|
|
64
117
|
|
65
118
|
Ncurses.keypad(@win, true)
|
66
119
|
|
67
|
-
|
68
|
-
if shadow
|
69
|
-
@shadow_win = Ncurses.newwin(box_height, box_width,
|
70
|
-
ypos + 1, xpos + 1)
|
71
|
-
end
|
120
|
+
set_shadow @shadow
|
72
121
|
|
73
122
|
# Register this baby.
|
74
|
-
|
123
|
+
screen.register(@widget_type, self)
|
75
124
|
end
|
76
125
|
|
77
|
-
#
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
actions.each do |x|
|
95
|
-
ret = self.inject(action)
|
96
|
-
if @exit_type == :EARLY_EXIT
|
97
|
-
return ret
|
98
|
-
end
|
99
|
-
end
|
126
|
+
# Sets multiple attributes of the Widget.
|
127
|
+
#
|
128
|
+
# See Button#initialize.
|
129
|
+
#
|
130
|
+
# @note Don't try to change `x`/`y` positions here,
|
131
|
+
# use Button#move
|
132
|
+
# @note Don't try to change `action` here,
|
133
|
+
# use TODO
|
134
|
+
def set(config)
|
135
|
+
label = @label
|
136
|
+
box = @box
|
137
|
+
shadow = @shadow
|
138
|
+
|
139
|
+
config.each do |key, val|
|
140
|
+
label = val if key == :label
|
141
|
+
box = val if key == :box
|
142
|
+
shadow = val if key == :shadow
|
100
143
|
end
|
101
144
|
|
102
|
-
|
103
|
-
self.
|
104
|
-
|
105
|
-
end
|
106
|
-
|
107
|
-
# This sets multiple attributes of the widget.
|
108
|
-
def set(mesg, box)
|
109
|
-
self.set_message(mesg)
|
110
|
-
self.set_box(box)
|
145
|
+
self.set_label(label) if label != @label
|
146
|
+
self.set_box(box) if box != @box
|
147
|
+
self.set_shadow(shadow) if shadow != @shadow
|
111
148
|
end
|
112
149
|
|
113
|
-
#
|
114
|
-
def
|
115
|
-
|
116
|
-
|
117
|
-
@
|
118
|
-
@
|
119
|
-
@
|
120
|
-
|
150
|
+
# Sets the text within the button.
|
151
|
+
def set_label label
|
152
|
+
label_len = []
|
153
|
+
label_pos = []
|
154
|
+
@label = RNDK.char2Chtype(label, label_len, label_pos)
|
155
|
+
@label_len = label_len[0]
|
156
|
+
@label_pos = RNDK.justifyString(@box_width - 2 * @border_size,
|
157
|
+
label_pos[0])
|
121
158
|
|
122
159
|
# Redraw the button widget.
|
123
160
|
self.erase
|
124
161
|
self.draw(box)
|
125
162
|
end
|
126
163
|
|
127
|
-
def
|
164
|
+
def get_label
|
128
165
|
return @info
|
129
166
|
end
|
130
167
|
|
168
|
+
# Turns on/off the shadow around the window
|
169
|
+
def set_shadow option
|
170
|
+
if option and @shadow_win.nil?
|
171
|
+
@shadow_win = Ncurses.newwin(box_height,
|
172
|
+
box_width,
|
173
|
+
y + 1,
|
174
|
+
x + 1)
|
175
|
+
|
176
|
+
elsif (not option) and (not @shadow_win.nil?)
|
177
|
+
RNDK::window_delete @shadow_win
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
131
181
|
# This sets the background attribute of the widget.
|
132
|
-
def
|
182
|
+
def set_bg_color(attrib)
|
133
183
|
Ncurses.wbkgd(@win, attrib)
|
134
184
|
end
|
135
185
|
|
136
|
-
def
|
186
|
+
def draw_label
|
137
187
|
box_width = @box_width
|
138
188
|
|
139
189
|
# Draw in the message.
|
@@ -147,7 +197,7 @@ module RNDK
|
|
147
197
|
end
|
148
198
|
|
149
199
|
if @has_focus
|
150
|
-
c =
|
200
|
+
c = RNDK::Color[:reverse] | c
|
151
201
|
end
|
152
202
|
|
153
203
|
Ncurses.mvwaddch(@win, @border_size, i + @border_size, c)
|
@@ -155,52 +205,51 @@ module RNDK
|
|
155
205
|
end
|
156
206
|
|
157
207
|
# This draws the button widget
|
158
|
-
def draw
|
208
|
+
def draw
|
159
209
|
# Is there a shadow?
|
160
210
|
unless @shadow_win.nil?
|
161
211
|
Draw.drawShadow(@shadow_win)
|
162
212
|
end
|
163
213
|
|
164
214
|
# Box the widget if asked.
|
165
|
-
if @box
|
166
|
-
|
167
|
-
|
168
|
-
self.drawText
|
215
|
+
Draw.drawObjBox(@win, self) if @box
|
216
|
+
|
217
|
+
self.draw_label
|
169
218
|
Ncurses.wrefresh @win
|
170
219
|
end
|
171
220
|
|
172
221
|
# This erases the button widget.
|
173
222
|
def erase
|
174
|
-
if self.
|
223
|
+
if self.valid?
|
175
224
|
RNDK.window_erase(@win)
|
176
225
|
RNDK.window_erase(@shadow_win)
|
177
226
|
end
|
178
227
|
end
|
179
228
|
|
180
229
|
# @see Widget#move
|
181
|
-
def move(
|
230
|
+
def move(x, y, relative, refresh_flag)
|
182
231
|
current_x = Ncurses.getbegx(@win)
|
183
232
|
current_y = Ncurses.getbegy(@win)
|
184
|
-
|
185
|
-
|
233
|
+
x = x
|
234
|
+
y = y
|
186
235
|
|
187
236
|
# If this is a relative move, then we will adjust where we want
|
188
237
|
# to move to.
|
189
238
|
if relative
|
190
|
-
|
191
|
-
|
239
|
+
x = Ncurses.getbegx(@win) + x
|
240
|
+
y = Ncurses.getbegy(@win) + y
|
192
241
|
end
|
193
242
|
|
194
243
|
# Adjust the window if we need to.
|
195
|
-
xtmp = [
|
196
|
-
ytmp = [
|
244
|
+
xtmp = [x]
|
245
|
+
ytmp = [y]
|
197
246
|
RNDK.alignxy(@screen.window, xtmp, ytmp, @box_width, @box_height)
|
198
|
-
|
199
|
-
|
247
|
+
x = xtmp[0]
|
248
|
+
y = ytmp[0]
|
200
249
|
|
201
250
|
# Get the difference
|
202
|
-
xdiff = current_x -
|
203
|
-
ydiff = current_y -
|
251
|
+
xdiff = current_x - x
|
252
|
+
ydiff = current_y - y
|
204
253
|
|
205
254
|
# Move the window to the new location.
|
206
255
|
RNDK.window_move(@win, -xdiff, -ydiff)
|
@@ -211,12 +260,110 @@ module RNDK
|
|
211
260
|
|
212
261
|
# Redraw the window, if they asked for it.
|
213
262
|
if refresh_flag
|
214
|
-
self.draw
|
263
|
+
self.draw
|
215
264
|
end
|
216
265
|
end
|
217
266
|
|
218
|
-
# This
|
219
|
-
|
267
|
+
# This destroys the button widget pointer.
|
268
|
+
def destroy
|
269
|
+
RNDK.window_delete @shadow_win
|
270
|
+
RNDK.window_delete @win
|
271
|
+
|
272
|
+
self.clean_bindings
|
273
|
+
|
274
|
+
@screen.unregister self
|
275
|
+
end
|
276
|
+
|
277
|
+
# Activates the Widget, letting the user interact with it.
|
278
|
+
#
|
279
|
+
# `actions` is an Array of characters. If it's non-null,
|
280
|
+
# will #inject each char on it into the Widget.
|
281
|
+
#
|
282
|
+
# @return `true` if pressed, `false` elsewhere.
|
283
|
+
def activate(actions=[])
|
284
|
+
self.draw
|
285
|
+
ret = false
|
286
|
+
|
287
|
+
if actions.nil? || actions.size == 0
|
288
|
+
while true
|
289
|
+
input = self.getch
|
290
|
+
|
291
|
+
# Inject the character into the widget.
|
292
|
+
ret = self.inject input
|
293
|
+
|
294
|
+
return ret if @exit_type != :EARLY_EXIT
|
295
|
+
end
|
296
|
+
else
|
297
|
+
# Inject each character one at a time.
|
298
|
+
actions.each do |x|
|
299
|
+
ret = self.inject action
|
300
|
+
|
301
|
+
return ret if @exit_type == :EARLY_EXIT
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
# Set the exit type and exit
|
306
|
+
self.set_exit_type(0)
|
307
|
+
return false
|
308
|
+
end
|
309
|
+
|
310
|
+
# This injects a single character into the widget.
|
311
|
+
def inject input
|
312
|
+
ret = false
|
313
|
+
complete = false
|
314
|
+
|
315
|
+
self.set_exit_type(0)
|
316
|
+
|
317
|
+
# Check a predefined binding.
|
318
|
+
if self.is_bound? input
|
319
|
+
self.run_key_binding input
|
320
|
+
#complete = true
|
321
|
+
|
322
|
+
else
|
323
|
+
case input
|
324
|
+
when RNDK::KEY_ESC, Ncurses::ERR
|
325
|
+
self.set_exit_type(input)
|
326
|
+
complete = true
|
327
|
+
|
328
|
+
when ' '.ord, RNDK::KEY_RETURN, Ncurses::KEY_ENTER
|
329
|
+
keep_going = self.run_signal_binding(:before_pressing)
|
330
|
+
|
331
|
+
# RUBY DOESN'T HAVE BREAK INSIDE CASE..WHEN BLOCKS
|
332
|
+
# AGHGW
|
333
|
+
|
334
|
+
if keep_going
|
335
|
+
self.run_signal_binding(:pressed)
|
336
|
+
self.set_exit_type(Ncurses::KEY_ENTER)
|
337
|
+
ret = true
|
338
|
+
complete = true
|
339
|
+
end
|
340
|
+
|
341
|
+
when RNDK::REFRESH
|
342
|
+
@screen.erase
|
343
|
+
@screen.refresh
|
344
|
+
|
345
|
+
else
|
346
|
+
RNDK.beep
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
self.set_exit_type(0) unless complete
|
351
|
+
|
352
|
+
@result_data = ret
|
353
|
+
ret
|
354
|
+
end
|
355
|
+
|
356
|
+
def focus
|
357
|
+
self.draw_label
|
358
|
+
Ncurses.wrefresh @win
|
359
|
+
end
|
360
|
+
|
361
|
+
def unfocus
|
362
|
+
self.draw_label
|
363
|
+
Ncurses.wrefresh @win
|
364
|
+
end
|
365
|
+
|
366
|
+
# @see Widget#position
|
220
367
|
def position
|
221
368
|
# Declare some variables
|
222
369
|
orig_x = Ncurses.getbegx(@win)
|
@@ -225,7 +372,7 @@ module RNDK
|
|
225
372
|
|
226
373
|
# Let them move the widget around until they hit return
|
227
374
|
while key != Ncurses::KEY_ENTER && key != RNDK::KEY_RETURN
|
228
|
-
key = self.getch
|
375
|
+
key = self.getch
|
229
376
|
if key == Ncurses::KEY_UP || key == '8'.ord
|
230
377
|
if Ncurses.getbegy(@win) > 0
|
231
378
|
self.move(0, -1, true, true)
|
@@ -302,71 +449,6 @@ module RNDK
|
|
302
449
|
end
|
303
450
|
end
|
304
451
|
|
305
|
-
# This destroys the button object pointer.
|
306
|
-
def destroy
|
307
|
-
RNDK.window_delete(@shadow_win)
|
308
|
-
RNDK.window_delete(@win)
|
309
|
-
|
310
|
-
self.clean_bindings(:BUTTON)
|
311
|
-
|
312
|
-
RNDK::Screen.unregister(:BUTTON, self)
|
313
|
-
end
|
314
|
-
|
315
|
-
# This injects a single character into the widget.
|
316
|
-
def inject(input)
|
317
|
-
ret = -1
|
318
|
-
complete = false
|
319
|
-
|
320
|
-
self.set_exit_type(0)
|
321
|
-
|
322
|
-
# Check a predefined binding.
|
323
|
-
if self.checkBind(:BUTTON, input)
|
324
|
-
complete = true
|
325
|
-
else
|
326
|
-
case input
|
327
|
-
when RNDK::KEY_ESC
|
328
|
-
self.set_exit_type(input)
|
329
|
-
complete = true
|
330
|
-
when Ncurses::ERR
|
331
|
-
self.set_exit_type(input)
|
332
|
-
complete = true
|
333
|
-
when ' '.ord, RNDK::KEY_RETURN, Ncurses::KEY_ENTER
|
334
|
-
unless @callback.nil?
|
335
|
-
@callback.call(self)
|
336
|
-
end
|
337
|
-
self.set_exit_type(Ncurses::KEY_ENTER)
|
338
|
-
ret = 0
|
339
|
-
complete = true
|
340
|
-
when RNDK::REFRESH
|
341
|
-
@screen.erase
|
342
|
-
@screen.refresh
|
343
|
-
else
|
344
|
-
RNDK.beep
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
unless complete
|
349
|
-
self.set_exit_type(0)
|
350
|
-
end
|
351
|
-
|
352
|
-
@result_data = ret
|
353
|
-
return ret
|
354
|
-
end
|
355
|
-
|
356
|
-
def focus
|
357
|
-
self.drawText
|
358
|
-
Ncurses.wrefresh @win
|
359
|
-
end
|
360
|
-
|
361
|
-
def unfocus
|
362
|
-
self.drawText
|
363
|
-
Ncurses.wrefresh @win
|
364
|
-
end
|
365
|
-
|
366
|
-
def object_type
|
367
|
-
:BUTTON
|
368
|
-
end
|
369
|
-
|
370
452
|
end
|
371
453
|
end
|
372
454
|
|