glimmer-dsl-libui 0.5.18 → 0.5.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2604 @@
1
+ # Glimmer DSL for LibUI Basic Examples
2
+
3
+ - [Glimmer DSL for LibUI Basic Examples](#glimmer-dsl-for-libui-basic-examples)
4
+ - [Basic Window](#basic-window)
5
+ - [Basic Child Window](#basic-child-window)
6
+ - [Basic Button](#basic-button)
7
+ - [Basic Entry](#basic-entry)
8
+ - [Simple Notepad](#simple-notepad)
9
+ - [Font Button](#font-button)
10
+ - [Color Button](#color-button)
11
+ - [Date Time Picker](#date-time-picker)
12
+ - [Form](#form)
13
+ - [Basic Table](#basic-table)
14
+ - [Basic Table Image](#basic-table-image)
15
+ - [Basic Table Image Text](#basic-table-image-text)
16
+ - [Basic Table Button](#basic-table-button)
17
+ - [Basic Table Checkbox](#basic-table-checkbox)
18
+ - [Basic Table Checkbox Text](#basic-table-checkbox-text)
19
+ - [Basic Table Progress Bar](#basic-table-progress-bar)
20
+ - [Basic Table Color](#basic-table-color)
21
+ - [Basic Area](#basic-area)
22
+ - [Basic Scrolling Area](#basic-scrolling-area)
23
+ - [Basic Image](#basic-image)
24
+ - [Basic Transform](#basic-transform)
25
+ - [Basic Draw Text](#basic-draw-text)
26
+ - [Basic Code Area](#basic-code-area)
27
+
28
+ ## Basic Window
29
+
30
+ [examples/basic_window.rb](/examples/basic_window.rb)
31
+
32
+ Run with this command from the root of the project if you cloned the project:
33
+
34
+ ```
35
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_window.rb
36
+ ```
37
+
38
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
39
+
40
+ ```
41
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_window'"
42
+ ```
43
+
44
+ Mac | Windows | Linux
45
+ ----|---------|------
46
+ ![glimmer-dsl-libui-mac-basic-window.png](/images/glimmer-dsl-libui-mac-basic-window.png) | ![glimmer-dsl-libui-windows-basic-window.png](/images/glimmer-dsl-libui-windows-basic-window.png) | ![glimmer-dsl-libui-linux-basic-window.png](/images/glimmer-dsl-libui-linux-basic-window.png)
47
+
48
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
49
+
50
+ ```ruby
51
+ require 'libui'
52
+
53
+ UI = LibUI
54
+
55
+ UI.init
56
+
57
+ main_window = UI.new_window('hello world', 300, 200, 1)
58
+
59
+ UI.control_show(main_window)
60
+
61
+ UI.window_on_closing(main_window) do
62
+ puts 'Bye Bye'
63
+ UI.control_destroy(main_window)
64
+ UI.quit
65
+ 0
66
+ end
67
+
68
+ UI.main
69
+ UI.quit
70
+ ```
71
+
72
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
73
+
74
+ ```ruby
75
+ require 'glimmer-dsl-libui'
76
+
77
+ include Glimmer
78
+
79
+ window('hello world', 300, 200, true) {
80
+ on_closing do
81
+ puts 'Bye Bye'
82
+ end
83
+ }.show
84
+ ```
85
+
86
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (setting `window` properties instead of arguments):
87
+
88
+ ```ruby
89
+ require 'glimmer-dsl-libui'
90
+
91
+ include Glimmer
92
+
93
+ window { # first 3 args can be set via properties with 4th arg has_menubar=true by default
94
+ title 'hello world'
95
+ content_size 300, 200
96
+
97
+ on_closing do
98
+ puts 'Bye Bye'
99
+ end
100
+ }.show
101
+ ```
102
+
103
+ ## Basic Child Window
104
+
105
+ [examples/basic_child_window.rb](/examples/basic_child_window.rb)
106
+
107
+ Run with this command from the root of the project if you cloned the project:
108
+
109
+ ```
110
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_child_window.rb
111
+ ```
112
+
113
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
114
+
115
+ ```
116
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_child_window.rb'"
117
+ ```
118
+
119
+ Mac | Windows | Linux
120
+ ----|---------|------
121
+ ![glimmer-dsl-libui-mac-basic-child-window.png](/images/glimmer-dsl-libui-mac-basic-child-window.png) ![glimmer-dsl-libui-mac-basic-child-window-open.png](/images/glimmer-dsl-libui-mac-basic-child-window-open.png) | ![glimmer-dsl-libui-windows-basic-child-window.png](/images/glimmer-dsl-libui-windows-basic-child-window.png) ![glimmer-dsl-libui-windows-basic-child-window-open.png](/images/glimmer-dsl-libui-windows-basic-child-window-open.png) | ![glimmer-dsl-libui-linux-basic-child-window.png](/images/glimmer-dsl-libui-linux-basic-child-window.png) ![glimmer-dsl-libui-linux-basic-child-window-open.png](/images/glimmer-dsl-libui-linux-basic-child-window-open.png)
122
+
123
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
124
+
125
+ ```ruby
126
+ require 'glimmer-dsl-libui'
127
+
128
+ include Glimmer
129
+
130
+ window('Main Window') {
131
+ button('Spawn Child Window') {
132
+ on_clicked do
133
+ window('Child Window') {
134
+ on_closing do
135
+ puts 'Child window is closing'
136
+ end
137
+ }.show
138
+ end
139
+ }
140
+
141
+ on_closing do
142
+ puts 'Main window is closing'
143
+ end
144
+ }.show
145
+ ```
146
+
147
+ ## Basic Button
148
+
149
+ [examples/basic_button.rb](/examples/basic_button.rb)
150
+
151
+ Run with this command from the root of the project if you cloned the project:
152
+
153
+ ```
154
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_button.rb
155
+ ```
156
+
157
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
158
+
159
+ ```
160
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_button'"
161
+ ```
162
+
163
+ Mac | Windows | Linux
164
+ ----|---------|------
165
+ ![glimmer-dsl-libui-mac-basic-button.png](/images/glimmer-dsl-libui-mac-basic-button.png) ![glimmer-dsl-libui-mac-basic-button-msg-box.png](/images/glimmer-dsl-libui-mac-basic-button-msg-box.png) | ![glimmer-dsl-libui-windows-basic-button.png](/images/glimmer-dsl-libui-windows-basic-button.png) ![glimmer-dsl-libui-windows-basic-button-msg-box.png](/images/glimmer-dsl-libui-windows-basic-button-msg-box.png) | ![glimmer-dsl-libui-linux-basic-button.png](/images/glimmer-dsl-libui-linux-basic-button.png) ![glimmer-dsl-libui-linux-basic-button-msg-box.png](/images/glimmer-dsl-libui-linux-basic-button-msg-box.png)
166
+
167
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
168
+
169
+ ```ruby
170
+ require 'libui'
171
+
172
+ UI = LibUI
173
+
174
+ UI.init
175
+
176
+ main_window = UI.new_window('hello world', 300, 200, 1)
177
+
178
+ button = UI.new_button('Button')
179
+
180
+ UI.button_on_clicked(button) do
181
+ UI.msg_box(main_window, 'Information', 'You clicked the button')
182
+ end
183
+
184
+ UI.window_on_closing(main_window) do
185
+ puts 'Bye Bye'
186
+ UI.control_destroy(main_window)
187
+ UI.quit
188
+ 0
189
+ end
190
+
191
+ UI.window_set_child(main_window, button)
192
+ UI.control_show(main_window)
193
+
194
+ UI.main
195
+ UI.quit
196
+ ```
197
+
198
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
199
+
200
+ ```ruby
201
+ require 'glimmer-dsl-libui'
202
+
203
+ include Glimmer
204
+
205
+ window('hello world', 300, 200) {
206
+ button('Button') {
207
+ on_clicked do
208
+ msg_box('Information', 'You clicked the button')
209
+ end
210
+ }
211
+
212
+ on_closing do
213
+ puts 'Bye Bye'
214
+ end
215
+ }.show
216
+ ```
217
+
218
+ ## Basic Entry
219
+
220
+ [examples/basic_entry.rb](/examples/basic_entry.rb)
221
+
222
+ Run with this command from the root of the project if you cloned the project:
223
+
224
+ ```
225
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_entry.rb
226
+ ```
227
+
228
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
229
+
230
+ ```
231
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_entry'"
232
+ ```
233
+
234
+ Mac | Windows | Linux
235
+ ----|---------|------
236
+ ![glimmer-dsl-libui-mac-basic-entry.png](/images/glimmer-dsl-libui-mac-basic-entry.png) ![glimmer-dsl-libui-mac-basic-entry-msg-box.png](/images/glimmer-dsl-libui-mac-basic-entry-msg-box.png) | ![glimmer-dsl-libui-windows-basic-entry.png](/images/glimmer-dsl-libui-windows-basic-entry.png) ![glimmer-dsl-libui-windows-basic-entry-msg-box.png](/images/glimmer-dsl-libui-windows-basic-entry-msg-box.png) | ![glimmer-dsl-libui-linux-basic-entry.png](/images/glimmer-dsl-libui-linux-basic-entry.png) ![glimmer-dsl-libui-linux-basic-entry-msg-box.png](/images/glimmer-dsl-libui-linux-basic-entry-msg-box.png)
237
+
238
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
239
+
240
+ ```ruby
241
+ require 'libui'
242
+
243
+ UI = LibUI
244
+
245
+ UI.init
246
+
247
+ main_window = UI.new_window('Basic Entry', 300, 50, 1)
248
+ UI.window_on_closing(main_window) do
249
+ puts 'Bye Bye'
250
+ UI.control_destroy(main_window)
251
+ UI.quit
252
+ 0
253
+ end
254
+
255
+ hbox = UI.new_horizontal_box
256
+ UI.window_set_child(main_window, hbox)
257
+
258
+ entry = UI.new_entry
259
+ UI.entry_on_changed(entry) do
260
+ puts UI.entry_text(entry).to_s
261
+ $stdout.flush # For Windows
262
+ end
263
+ UI.box_append(hbox, entry, 1)
264
+
265
+ button = UI.new_button('Button')
266
+ UI.button_on_clicked(button) do
267
+ text = UI.entry_text(entry).to_s
268
+ UI.msg_box(main_window, 'You entered', text)
269
+ 0
270
+ end
271
+
272
+ UI.box_append(hbox, button, 0)
273
+
274
+ UI.control_show(main_window)
275
+ UI.main
276
+ UI.quit
277
+ ```
278
+
279
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (with [data-binding](#data-binding)):
280
+
281
+ ```ruby
282
+ require 'glimmer-dsl-libui'
283
+
284
+ class BasicEntry
285
+ include Glimmer
286
+
287
+ attr_accessor :entry_text
288
+
289
+ def launch
290
+ window('Basic Entry', 300, 50) {
291
+ horizontal_box {
292
+ entry {
293
+ # stretchy true # Smart default option for appending to horizontal_box
294
+ text <=> [self, :entry_text, after_write: ->(text) {puts text; $stdout.flush}] # bidirectional data-binding between text property and entry_text attribute, printing after write to model.
295
+ }
296
+
297
+ button('Button') {
298
+ stretchy false # stretchy property is available when control is nested under horizontal_box
299
+
300
+ on_clicked do
301
+ msg_box('You entered', entry_text)
302
+ end
303
+ }
304
+ }
305
+
306
+ on_closing do
307
+ puts 'Bye Bye'
308
+ end
309
+ }.show
310
+ end
311
+ end
312
+
313
+ BasicEntry.new.launch
314
+ ```
315
+
316
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (without [data-binding](#data-binding)):
317
+
318
+ ```ruby
319
+ require 'glimmer-dsl-libui'
320
+
321
+ include Glimmer
322
+
323
+ window('Basic Entry', 300, 50) {
324
+ horizontal_box {
325
+ e = entry {
326
+ # stretchy true # Smart default option for appending to horizontal_box
327
+
328
+ on_changed do
329
+ puts e.text
330
+ $stdout.flush # For Windows
331
+ end
332
+ }
333
+
334
+ button('Button') {
335
+ stretchy false # stretchy property is available when control is nested under horizontal_box
336
+
337
+ on_clicked do
338
+ text = e.text
339
+ msg_box('You entered', text)
340
+ end
341
+ }
342
+ }
343
+
344
+ on_closing do
345
+ puts 'Bye Bye'
346
+ end
347
+ }.show
348
+ ```
349
+
350
+ ## Simple Notepad
351
+
352
+ [examples/simple_notepad.rb](/examples/simple_notepad.rb)
353
+
354
+ Run with this command from the root of the project if you cloned the project:
355
+
356
+ ```
357
+ ruby -r './lib/glimmer-dsl-libui' examples/simple_notepad.rb
358
+ ```
359
+
360
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
361
+
362
+ ```
363
+ ruby -r glimmer-dsl-libui -e "require 'examples/simple_notepad'"
364
+ ```
365
+
366
+ Mac | Windows | Linux
367
+ ----|---------|------
368
+ ![glimmer-dsl-libui-mac-simple-notepad.png](/images/glimmer-dsl-libui-mac-simple-notepad.png) | ![glimmer-dsl-libui-windows-simple-notepad.png](/images/glimmer-dsl-libui-windows-simple-notepad.png) | ![glimmer-dsl-libui-linux-simple-notepad.png](/images/glimmer-dsl-libui-linux-simple-notepad.png)
369
+
370
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
371
+
372
+ ```ruby
373
+ require 'libui'
374
+
375
+ UI = LibUI
376
+
377
+ UI.init
378
+
379
+ main_window = UI.new_window('Notepad', 500, 300, 1)
380
+ UI.window_on_closing(main_window) do
381
+ puts 'Bye Bye'
382
+ UI.control_destroy(main_window)
383
+ UI.quit
384
+ 0
385
+ end
386
+
387
+ vbox = UI.new_vertical_box
388
+ UI.window_set_child(main_window, vbox)
389
+
390
+ entry = UI.new_non_wrapping_multiline_entry
391
+ UI.box_append(vbox, entry, 1)
392
+
393
+ UI.control_show(main_window)
394
+ UI.main
395
+ UI.quit
396
+ ```
397
+
398
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
399
+
400
+ ```ruby
401
+ require 'glimmer-dsl-libui'
402
+
403
+ include Glimmer
404
+
405
+ window('Notepad', 500, 300) {
406
+ on_closing do
407
+ puts 'Bye Bye'
408
+ end
409
+
410
+ vertical_box {
411
+ non_wrapping_multiline_entry
412
+ }
413
+ }.show
414
+ ```
415
+
416
+ ## Font Button
417
+
418
+ [examples/font_button.rb](/examples/font_button.rb)
419
+
420
+ Run with this command from the root of the project if you cloned the project:
421
+
422
+ ```
423
+ ruby -r './lib/glimmer-dsl-libui' examples/font_button.rb
424
+ ```
425
+
426
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
427
+
428
+ ```
429
+ ruby -r glimmer-dsl-libui -e "require 'examples/font_button'"
430
+ ```
431
+
432
+ Mac | Windows | Linux
433
+ ----|---------|------
434
+ ![glimmer-dsl-libui-mac-font-button.png](/images/glimmer-dsl-libui-mac-font-button.png) ![glimmer-dsl-libui-mac-font-button-selection.png](/images/glimmer-dsl-libui-mac-font-button-selection.png) | ![glimmer-dsl-libui-windows-font-button.png](/images/glimmer-dsl-libui-windows-font-button.png) ![glimmer-dsl-libui-windows-font-button-selection.png](/images/glimmer-dsl-libui-windows-font-button-selection.png) | ![glimmer-dsl-libui-linux-font-button.png](/images/glimmer-dsl-libui-linux-font-button.png) ![glimmer-dsl-libui-linux-font-button-selection.png](/images/glimmer-dsl-libui-linux-font-button-selection.png)
435
+
436
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
437
+
438
+ ```ruby
439
+ require 'libui'
440
+
441
+ UI = LibUI
442
+
443
+ UI.init
444
+
445
+ main_window = UI.new_window('hello world', 300, 200, 1)
446
+
447
+ font_button = UI.new_font_button
448
+ font_descriptor = UI::FFI::FontDescriptor.malloc
449
+ font_descriptor.to_ptr.free = Fiddle::RUBY_FREE
450
+ UI.font_button_on_changed(font_button) do
451
+ UI.font_button_font(font_button, font_descriptor)
452
+ p family: font_descriptor.Family.to_s,
453
+ size: font_descriptor.Size,
454
+ weight: font_descriptor.Weight,
455
+ italic: font_descriptor.Italic,
456
+ stretch: font_descriptor.Stretch
457
+ end
458
+
459
+ UI.window_on_closing(main_window) do
460
+ puts 'Bye Bye'
461
+ UI.control_destroy(main_window)
462
+ UI.quit
463
+ 0
464
+ end
465
+
466
+ UI.window_set_child(main_window, font_button)
467
+ UI.control_show(main_window)
468
+
469
+ UI.main
470
+ UI.quit
471
+
472
+ ```
473
+
474
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (with [data-binding](#data-binding)):
475
+
476
+ ```ruby
477
+ require 'glimmer-dsl-libui'
478
+
479
+ class FontButton
480
+ include Glimmer
481
+
482
+ attr_accessor :font_descriptor
483
+
484
+ def launch
485
+ window('hello world', 300, 200) {
486
+ font_button {
487
+ font <=> [self, :font_descriptor, after_write: -> { p font_descriptor }]
488
+ }
489
+
490
+ on_closing do
491
+ puts 'Bye Bye'
492
+ end
493
+ }.show
494
+ end
495
+ end
496
+
497
+ FontButton.new.launch
498
+ ```
499
+
500
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (without [data-binding](#data-binding)):
501
+
502
+ ```ruby
503
+ require 'glimmer-dsl-libui'
504
+
505
+ include Glimmer
506
+
507
+ window('hello world', 300, 200) {
508
+ font_button { |fb|
509
+ on_changed do
510
+ font_descriptor = fb.font
511
+ p font_descriptor
512
+ end
513
+ }
514
+
515
+ on_closing do
516
+ puts 'Bye Bye'
517
+ end
518
+ }.show
519
+ ```
520
+
521
+ ## Color Button
522
+
523
+ [examples/color_button.rb](/examples/color_button.rb)
524
+
525
+ Run with this command from the root of the project if you cloned the project:
526
+
527
+ ```
528
+ ruby -r './lib/glimmer-dsl-libui' examples/color_button.rb
529
+ ```
530
+
531
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
532
+
533
+ ```
534
+ ruby -r glimmer-dsl-libui -e "require 'examples/color_button'"
535
+ ```
536
+
537
+ Mac | Windows | Linux
538
+ ----|---------|------
539
+ ![glimmer-dsl-libui-mac-color-button.png](/images/glimmer-dsl-libui-mac-color-button.png) ![glimmer-dsl-libui-mac-color-button-selection.png](/images/glimmer-dsl-libui-mac-color-button-selection.png) | ![glimmer-dsl-libui-windows-color-button.png](/images/glimmer-dsl-libui-windows-color-button.png) ![glimmer-dsl-libui-windows-color-button-selection.png](/images/glimmer-dsl-libui-windows-color-button-selection.png) | ![glimmer-dsl-libui-linux-color-button.png](/images/glimmer-dsl-libui-linux-color-button.png) ![glimmer-dsl-libui-linux-color-button-selection.png](/images/glimmer-dsl-libui-linux-color-button-selection.png)
540
+
541
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (with [data-binding](#data-binding)):
542
+
543
+ ```ruby
544
+ require 'glimmer-dsl-libui'
545
+
546
+ class ColorButton
547
+ include Glimmer
548
+
549
+ attr_accessor :selected_color
550
+
551
+ def initialize
552
+ @selected_color = :blue
553
+ end
554
+
555
+ def launch
556
+ window('color button', 240) {
557
+ color_button {
558
+ color <=> [self, :selected_color, after_write: ->(color) {p color}]
559
+ }
560
+ }.show
561
+ end
562
+ end
563
+
564
+ ColorButton.new.launch
565
+ ```
566
+
567
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (without [data-binding](#data-binding)):
568
+
569
+ ```ruby
570
+ require 'glimmer-dsl-libui'
571
+
572
+ include Glimmer
573
+
574
+ window('color button', 240) {
575
+ color_button { |cb|
576
+ color :blue
577
+
578
+ on_changed do
579
+ rgba = cb.color
580
+ p rgba
581
+ end
582
+ }
583
+ }.show
584
+ ```
585
+
586
+ ## Date Time Picker
587
+
588
+ [examples/date_time_picker.rb](/examples/date_time_picker.rb)
589
+
590
+ Run with this command from the root of the project if you cloned the project:
591
+
592
+ ```
593
+ ruby -r './lib/glimmer-dsl-libui' examples/date_time_picker.rb
594
+ ```
595
+
596
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
597
+
598
+ ```
599
+ ruby -r glimmer-dsl-libui -e "require 'examples/date_time_picker'"
600
+ ```
601
+
602
+ Mac | Windows | Linux
603
+ ----|---------|------
604
+ ![glimmer-dsl-libui-mac-date-time-picker.png](/images/glimmer-dsl-libui-mac-date-time-picker.png) | ![glimmer-dsl-libui-windows-date-time-picker.png](/images/glimmer-dsl-libui-windows-date-time-picker.png) | ![glimmer-dsl-libui-linux-date-time-picker.png](/images/glimmer-dsl-libui-linux-date-time-picker.png)
605
+
606
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
607
+
608
+ ```ruby
609
+ require 'libui'
610
+
611
+ UI = LibUI
612
+
613
+ UI.init
614
+
615
+ vbox = UI.new_vertical_box
616
+
617
+ date_time_picker = UI.new_date_time_picker
618
+
619
+ time = UI::FFI::TM.malloc
620
+
621
+ UI.date_time_picker_on_changed(date_time_picker) do
622
+ UI.date_time_picker_time(date_time_picker, time)
623
+ p sec: time.tm_sec,
624
+ min: time.tm_min,
625
+ hour: time.tm_hour,
626
+ mday: time.tm_mday,
627
+ mon: time.tm_mon,
628
+ year: time.tm_year,
629
+ wday: time.tm_wday,
630
+ yday: time.tm_yday,
631
+ isdst: time.tm_isdst
632
+ end
633
+ UI.box_append(vbox, date_time_picker, 1)
634
+
635
+ main_window = UI.new_window('Date Time Pickers', 300, 200, 1)
636
+ UI.window_on_closing(main_window) do
637
+ puts 'Bye Bye'
638
+ UI.control_destroy(main_window)
639
+ UI.quit
640
+ 0
641
+ end
642
+ UI.window_set_child(main_window, vbox)
643
+ UI.control_show(main_window)
644
+
645
+ UI.main
646
+ UI.quit
647
+ ```
648
+
649
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (with [data-binding](#data-binding)):
650
+
651
+ ```ruby
652
+ require 'glimmer-dsl-libui'
653
+
654
+ class DateTimePicker
655
+ include Glimmer
656
+
657
+ attr_accessor :picked_time
658
+
659
+ def launch
660
+ window('Date Time Pickers', 300, 200) {
661
+ vertical_box {
662
+ date_time_picker {
663
+ time <=> [self, :picked_time, after_write: ->(time) { p time }]
664
+ }
665
+ }
666
+
667
+ on_closing do
668
+ puts 'Bye Bye'
669
+ end
670
+ }.show
671
+ end
672
+ end
673
+
674
+ DateTimePicker.new.launch
675
+ ```
676
+
677
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (without [data-binding](#data-binding)):
678
+
679
+ ```ruby
680
+ require 'glimmer-dsl-libui'
681
+
682
+ include Glimmer
683
+
684
+ window('Date Time Pickers', 300, 200) {
685
+ vertical_box {
686
+ date_time_picker { |dtp|
687
+ on_changed do
688
+ time = dtp.time
689
+ p time
690
+ end
691
+ }
692
+ }
693
+
694
+ on_closing do
695
+ puts 'Bye Bye'
696
+ end
697
+ }.show
698
+ ```
699
+
700
+ ## Form
701
+
702
+ [examples/form.rb](/examples/form.rb)
703
+
704
+ Run with this command from the root of the project if you cloned the project:
705
+
706
+ ```
707
+ ruby -r './lib/glimmer-dsl-libui' examples/form.rb
708
+ ```
709
+
710
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
711
+
712
+ ```
713
+ ruby -r glimmer-dsl-libui -e "require 'examples/form'"
714
+ ```
715
+
716
+ Mac | Windows | Linux
717
+ ----|---------|------
718
+ ![glimmer-dsl-libui-mac-form.png](/images/glimmer-dsl-libui-mac-form.png) ![glimmer-dsl-libui-mac-form-msg-box.png](/images/glimmer-dsl-libui-mac-form-msg-box.png) | ![glimmer-dsl-libui-windows-form.png](/images/glimmer-dsl-libui-windows-form.png) ![glimmer-dsl-libui-windows-form-msg-box.png](/images/glimmer-dsl-libui-windows-form-msg-box.png) | ![glimmer-dsl-libui-linux-form.png](/images/glimmer-dsl-libui-linux-form.png) ![glimmer-dsl-libui-linux-form-msg-box.png](/images/glimmer-dsl-libui-linux-form-msg-box.png)
719
+
720
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (with [data-binding](#data-binding)):
721
+
722
+ ```ruby
723
+ require 'glimmer-dsl-libui'
724
+
725
+ class Form
726
+ include Glimmer
727
+
728
+ attr_accessor :first_name, :last_name, :phone, :email
729
+
730
+ def launch
731
+ window('Form') {
732
+ margined true
733
+
734
+ vertical_box {
735
+ form {
736
+ entry {
737
+ label 'First Name' # label property is available when control is nested under form
738
+ text <=> [self, :first_name] # bidirectional data-binding of entry text property to self first_name attribute
739
+ }
740
+
741
+ entry {
742
+ label 'Last Name' # label property is available when control is nested under form
743
+ text <=> [self, :last_name]
744
+ }
745
+
746
+ entry {
747
+ label 'Phone' # label property is available when control is nested under form
748
+ text <=> [self, :phone]
749
+ }
750
+
751
+ entry {
752
+ label 'Email' # label property is available when control is nested under form
753
+ text <=> [self, :email]
754
+ }
755
+ }
756
+
757
+ button('Display Info') {
758
+ stretchy false
759
+
760
+ on_clicked do
761
+ msg_box('Info', "#{first_name} #{last_name} has phone #{phone} and email #{email}")
762
+ end
763
+ }
764
+ }
765
+ }.show
766
+ end
767
+ end
768
+
769
+ Form.new.launch
770
+ ```
771
+
772
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (without [data-binding](#data-binding)):
773
+
774
+ ```ruby
775
+ require 'glimmer-dsl-libui'
776
+
777
+ include Glimmer
778
+
779
+ window('Form') {
780
+ margined true
781
+
782
+ vertical_box {
783
+ form {
784
+ @first_name_entry = entry {
785
+ label 'First Name' # label property is available when control is nested under form
786
+ }
787
+
788
+ @last_name_entry = entry {
789
+ label 'Last Name' # label property is available when control is nested under form
790
+ }
791
+
792
+ @phone_entry = entry {
793
+ label 'Phone' # label property is available when control is nested under form
794
+ }
795
+
796
+ @email_entry = entry {
797
+ label 'Email' # label property is available when control is nested under form
798
+ }
799
+ }
800
+
801
+ button('Display Info') {
802
+ stretchy false
803
+
804
+ on_clicked do
805
+ msg_box('Info', "#{@first_name_entry.text} #{@last_name_entry.text} has phone #{@phone_entry.text} and email #{@email_entry.text}")
806
+ end
807
+ }
808
+ }
809
+ }.show
810
+ ```
811
+
812
+ ## Basic Table
813
+
814
+ [examples/basic_table.rb](/examples/basic_table.rb)
815
+
816
+ Run with this command from the root of the project if you cloned the project:
817
+
818
+ ```
819
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_table.rb
820
+ ```
821
+
822
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
823
+
824
+ ```
825
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_table'"
826
+ ```
827
+
828
+ Mac | Windows | Linux
829
+ ----|---------|------
830
+ ![glimmer-dsl-libui-mac-basic-table.png](/images/glimmer-dsl-libui-mac-basic-table.png) | ![glimmer-dsl-libui-windows-basic-table.png](/images/glimmer-dsl-libui-windows-basic-table.png) | ![glimmer-dsl-libui-linux-basic-table.png](/images/glimmer-dsl-libui-linux-basic-table.png)
831
+
832
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
833
+
834
+ ```ruby
835
+ require 'libui'
836
+
837
+ UI = LibUI
838
+
839
+ UI.init
840
+
841
+ main_window = UI.new_window('Animal sounds', 300, 200, 1)
842
+
843
+ hbox = UI.new_horizontal_box
844
+ UI.window_set_child(main_window, hbox)
845
+
846
+ data = [
847
+ %w[cat meow],
848
+ %w[dog woof],
849
+ %w[checken cock-a-doodle-doo],
850
+ %w[horse neigh],
851
+ %w[cow moo]
852
+ ]
853
+
854
+ # Protects BlockCaller objects from garbage collection.
855
+ @blockcaller = []
856
+ def rbcallback(*args, &block)
857
+ args << [0] if args.size == 1 # Argument types are ommited
858
+ blockcaller = Fiddle::Closure::BlockCaller.new(*args, &block)
859
+ @blockcaller << blockcaller
860
+ blockcaller
861
+ end
862
+
863
+ model_handler = UI::FFI::TableModelHandler.malloc
864
+ model_handler.NumColumns = rbcallback(4) { 2 }
865
+ model_handler.ColumnType = rbcallback(4) { 0 }
866
+ model_handler.NumRows = rbcallback(4) { 5 }
867
+ model_handler.CellValue = rbcallback(1, [1, 1, 4, 4]) do |_, _, row, column|
868
+ UI.new_table_value_string(data[row][column])
869
+ end
870
+ model_handler.SetCellValue = rbcallback(0, [0]) {}
871
+
872
+ model = UI.new_table_model(model_handler)
873
+
874
+ table_params = UI::FFI::TableParams.malloc
875
+ table_params.Model = model
876
+ table_params.RowBackgroundColorModelColumn = -1
877
+
878
+ table = UI.new_table(table_params)
879
+ UI.table_append_text_column(table, 'Animal', 0, -1)
880
+ UI.table_append_text_column(table, 'Description', 1, -1)
881
+
882
+ UI.box_append(hbox, table, 1)
883
+ UI.control_show(main_window)
884
+
885
+ UI.window_on_closing(main_window) do
886
+ puts 'Bye Bye'
887
+ UI.control_destroy(main_window)
888
+ UI.free_table_model(model)
889
+ UI.quit
890
+ 0
891
+ end
892
+
893
+ UI.main
894
+ UI.quit
895
+ ```
896
+
897
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
898
+
899
+ ```ruby
900
+ require 'glimmer-dsl-libui'
901
+
902
+ include Glimmer
903
+
904
+ data = [
905
+ %w[cat meow],
906
+ %w[dog woof],
907
+ %w[chicken cock-a-doodle-doo],
908
+ %w[horse neigh],
909
+ %w[cow moo]
910
+ ]
911
+
912
+ window('Animal sounds', 300, 200) {
913
+ horizontal_box {
914
+ table {
915
+ text_column('Animal')
916
+ text_column('Description')
917
+
918
+ cell_rows data
919
+ }
920
+ }
921
+
922
+ on_closing do
923
+ puts 'Bye Bye'
924
+ end
925
+ }.show
926
+ ```
927
+
928
+ ## Basic Table Image
929
+
930
+ Note that behavior varies per platform (i.e. how `table` chooses to size images by default).
931
+
932
+ [examples/basic_table_image.rb](/examples/basic_table_image.rb)
933
+
934
+ Run with this command from the root of the project if you cloned the project:
935
+
936
+ ```
937
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_table_image.rb
938
+ ```
939
+
940
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
941
+
942
+ ```
943
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_table_image'"
944
+ ```
945
+
946
+ Mac | Windows | Linux
947
+ ----|---------|------
948
+ ![glimmer-dsl-libui-mac-basic-table-image.png](/images/glimmer-dsl-libui-mac-basic-table-image.png) | ![glimmer-dsl-libui-windows-basic-table-image.png](/images/glimmer-dsl-libui-windows-basic-table-image.png) | ![glimmer-dsl-libui-linux-basic-table-image.png](/images/glimmer-dsl-libui-linux-basic-table-image.png)
949
+
950
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
951
+
952
+ ```ruby
953
+ # NOTE:
954
+ # This example displays images that can be freely downloaded from the Studio Ghibli website.
955
+
956
+ require 'libui'
957
+ require 'chunky_png'
958
+ require 'open-uri'
959
+
960
+ UI = LibUI
961
+
962
+ UI.init
963
+
964
+ main_window = UI.new_window('The Red Turtle', 310, 350, 0)
965
+
966
+ hbox = UI.new_horizontal_box
967
+ UI.window_set_child(main_window, hbox)
968
+
969
+ IMAGES = []
970
+
971
+ 50.times do |i|
972
+ url = format('https://www.ghibli.jp/gallery/thumb-redturtle%03d.png', (i + 1))
973
+ puts "Processing Image: #{url}"
974
+ f = URI.open(url)
975
+ canvas = ChunkyPNG::Canvas.from_io(f)
976
+ f.close
977
+ data = canvas.to_rgba_stream
978
+ width = canvas.width
979
+ height = canvas.height
980
+ image = UI.new_image(width, height)
981
+ UI.image_append(image, data, width, height, width * 4)
982
+ IMAGES << image
983
+ rescue StandardError => e
984
+ warn url, e.message
985
+ end
986
+
987
+ # Protects BlockCaller objects from garbage collection.
988
+ @blockcaller = []
989
+ def rbcallback(*args, &block)
990
+ args << [0] if args.size == 1 # Argument types are ommited
991
+ blockcaller = Fiddle::Closure::BlockCaller.new(*args, &block)
992
+ @blockcaller << blockcaller
993
+ blockcaller
994
+ end
995
+
996
+ model_handler = UI::FFI::TableModelHandler.malloc
997
+ model_handler.NumColumns = rbcallback(4) { 1 }
998
+ model_handler.ColumnType = rbcallback(4) { 1 } # Image
999
+ model_handler.NumRows = rbcallback(4) { IMAGES.size }
1000
+ model_handler.CellValue = rbcallback(1, [1, 1, 4, 4]) do |_, _, row, _column|
1001
+ UI.new_table_value_image(/images[row])
1002
+ end
1003
+ model_handler.SetCellValue = rbcallback(0, [0]) {}
1004
+
1005
+ model = UI.new_table_model(model_handler)
1006
+
1007
+ table_params = UI::FFI::TableParams.malloc
1008
+ table_params.Model = model
1009
+ table_params.RowBackgroundColorModelColumn = -1
1010
+
1011
+ table = UI.new_table(table_params)
1012
+ UI.table_append_image_column(table, 'www.ghibli.jp/works/red-turtle', 0)
1013
+
1014
+ UI.box_append(hbox, table, 1)
1015
+ UI.control_show(main_window)
1016
+
1017
+ UI.window_on_closing(main_window) do
1018
+ puts 'Bye Bye'
1019
+ UI.control_destroy(main_window)
1020
+ UI.free_table_model(model)
1021
+ IMAGES.each { |i| UI.free_image(i) }
1022
+ UI.quit
1023
+ 0
1024
+ end
1025
+
1026
+ UI.main
1027
+ UI.quit
1028
+ ```
1029
+
1030
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (passing file url as image):
1031
+
1032
+ ```ruby
1033
+ # frozen_string_literal: true
1034
+
1035
+ # NOTE:
1036
+ # This example displays images that can be freely downloaded from the Studio Ghibli website.
1037
+
1038
+ require 'glimmer-dsl-libui'
1039
+
1040
+ include Glimmer
1041
+
1042
+ IMAGE_ROWS = []
1043
+
1044
+ 50.times do |i|
1045
+ url = format('https://www.ghibli.jp/gallery/thumb-redturtle%03d.png', (i + 1))
1046
+ puts "Processing Image: #{url}"; $stdout.flush # for Windows
1047
+ IMAGE_ROWS << [url] # array of one column cell
1048
+ rescue StandardError => e
1049
+ warn url, e.message
1050
+ end
1051
+
1052
+ window('The Red Turtle', 310, 350, false) {
1053
+ horizontal_box {
1054
+ table {
1055
+ image_column('www.ghibli.jp/works/red-turtle')
1056
+
1057
+ cell_rows IMAGE_ROWS
1058
+ }
1059
+ }
1060
+
1061
+ on_closing do
1062
+ puts 'Bye Bye'
1063
+ end
1064
+ }.show
1065
+ ```
1066
+
1067
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (automatic construction of `image`):
1068
+
1069
+ ```ruby
1070
+ # NOTE:
1071
+ # This example displays images that can be freely downloaded from the Studio Ghibli website.
1072
+
1073
+ require 'glimmer-dsl-libui'
1074
+
1075
+ include Glimmer
1076
+
1077
+ IMAGE_ROWS = []
1078
+
1079
+ 50.times do |i|
1080
+ url = format('https://www.ghibli.jp/gallery/thumb-redturtle%03d.png', (i + 1))
1081
+ puts "Processing Image: #{url}"; $stdout.flush # for Windows
1082
+ IMAGE_ROWS << [image(url)] # array of one column cell
1083
+ rescue StandardError => e
1084
+ warn url, e.message
1085
+ end
1086
+
1087
+ window('The Red Turtle', 310, 350, false) {
1088
+ horizontal_box {
1089
+ table {
1090
+ image_column('www.ghibli.jp/works/red-turtle')
1091
+
1092
+ cell_rows IMAGE_ROWS
1093
+ }
1094
+ }
1095
+
1096
+ on_closing do
1097
+ puts 'Bye Bye'
1098
+ end
1099
+ }.show
1100
+ ```
1101
+
1102
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 3 (manual construction of `image` from `image_part`):
1103
+
1104
+ ```ruby
1105
+ # NOTE:
1106
+ # This example displays images that can be freely downloaded from the Studio Ghibli website.
1107
+
1108
+ require 'glimmer-dsl-libui'
1109
+ require 'chunky_png'
1110
+ require 'open-uri'
1111
+
1112
+ include Glimmer
1113
+
1114
+ IMAGE_ROWS = []
1115
+
1116
+ 50.times do |i|
1117
+ url = format('https://www.ghibli.jp/gallery/thumb-redturtle%03d.png', (i + 1))
1118
+ puts "Processing Image: #{url}"
1119
+ f = URI.open(url)
1120
+ canvas = ChunkyPNG::Canvas.from_io(f)
1121
+ f.close
1122
+ data = canvas.to_rgba_stream
1123
+ width = canvas.width
1124
+ height = canvas.height
1125
+ img = image {
1126
+ image_part(data, width, height, width * 4)
1127
+ }
1128
+ IMAGE_ROWS << [img] # array of one column cell
1129
+ rescue StandardError => e
1130
+ warn url, e.message
1131
+ end
1132
+
1133
+ window('The Red Turtle', 310, 350, false) {
1134
+ horizontal_box {
1135
+ table {
1136
+ image_column('www.ghibli.jp/works/red-turtle', 0)
1137
+
1138
+ cell_rows IMAGE_ROWS
1139
+ }
1140
+ }
1141
+
1142
+ on_closing do
1143
+ puts 'Bye Bye'
1144
+ end
1145
+ }.show
1146
+ ```
1147
+
1148
+ ## Basic Table Image Text
1149
+
1150
+ Note that behavior varies per platform (i.e. how `table` chooses to size images by default).
1151
+
1152
+ [examples/basic_table_image_text.rb](/examples/basic_table_image_text.rb)
1153
+
1154
+ Run with this command from the root of the project if you cloned the project:
1155
+
1156
+ ```
1157
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_table_image_text.rb
1158
+ ```
1159
+
1160
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
1161
+
1162
+ ```
1163
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_table_image_text'"
1164
+ ```
1165
+
1166
+ Mac | Windows | Linux
1167
+ ----|---------|------
1168
+ ![glimmer-dsl-libui-mac-basic-table-image-text.png](/images/glimmer-dsl-libui-mac-basic-table-image-text.png) | ![glimmer-dsl-libui-windows-basic-table-image-text.png](/images/glimmer-dsl-libui-windows-basic-table-image-text.png) | ![glimmer-dsl-libui-linux-basic-table-image-text.png](/images/glimmer-dsl-libui-linux-basic-table-image-text.png)
1169
+
1170
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (passing file url as image):
1171
+
1172
+ ```ruby
1173
+ # frozen_string_literal: true
1174
+
1175
+ # NOTE:
1176
+ # This example displays images that can be freely downloaded from the Studio Ghibli website.
1177
+
1178
+ require 'glimmer-dsl-libui'
1179
+
1180
+ include Glimmer
1181
+
1182
+ IMAGE_ROWS = []
1183
+
1184
+ 5.times do |i|
1185
+ url = format('https://www.ghibli.jp/gallery/thumb-redturtle%03d.png', (i + 1))
1186
+ puts "Processing Image: #{url}"; $stdout.flush # for Windows
1187
+ text = url.sub('https://www.ghibli.jp/gallery/thumb-redturtle', '').sub('.png', '')
1188
+ IMAGE_ROWS << [[url, text], [url, text]] # cell values are dual-element arrays
1189
+ rescue StandardError => e
1190
+ warn url, e.message
1191
+ end
1192
+
1193
+ window('The Red Turtle', 670, 350) {
1194
+ horizontal_box {
1195
+ table {
1196
+ image_text_column('image/number')
1197
+ image_text_column('image/number (editable)') {
1198
+ editable true
1199
+ }
1200
+
1201
+ cell_rows IMAGE_ROWS
1202
+ }
1203
+ }
1204
+ }.show
1205
+ ```
1206
+
1207
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (automatic construction of `image`):
1208
+
1209
+ ```ruby
1210
+ # NOTE:
1211
+ # This example displays images that can be freely downloaded from the Studio Ghibli website.
1212
+
1213
+ require 'glimmer-dsl-libui'
1214
+
1215
+ include Glimmer
1216
+
1217
+ IMAGE_ROWS = []
1218
+
1219
+ 5.times do |i|
1220
+ url = format('https://www.ghibli.jp/gallery/thumb-redturtle%03d.png', (i + 1))
1221
+ puts "Processing Image: #{url}"; $stdout.flush # for Windows
1222
+ text = url.sub('https://www.ghibli.jp/gallery/thumb-redturtle', '').sub('.png', '')
1223
+ img = image(url)
1224
+ IMAGE_ROWS << [[img, text], [img, text]] # cell values are dual-element arrays
1225
+ rescue StandardError => e
1226
+ warn url, e.message
1227
+ end
1228
+
1229
+ window('The Red Turtle', 670, 350) {
1230
+ horizontal_box {
1231
+ table {
1232
+ image_text_column('image/number')
1233
+ image_text_column('image/number (editable)') {
1234
+ editable true
1235
+ }
1236
+
1237
+ cell_rows IMAGE_ROWS
1238
+ }
1239
+ }
1240
+ }.show
1241
+ ```
1242
+
1243
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (manual construction of `image` from `image_part`):
1244
+
1245
+ ```ruby
1246
+ # NOTE:
1247
+ # This example displays images that can be freely downloaded from the Studio Ghibli website.
1248
+
1249
+ require 'glimmer-dsl-libui'
1250
+ require 'chunky_png'
1251
+ require 'open-uri'
1252
+
1253
+ include Glimmer
1254
+
1255
+ IMAGE_ROWS = []
1256
+
1257
+ 5.times do |i|
1258
+ url = format('https://www.ghibli.jp/gallery/thumb-redturtle%03d.png', (i + 1))
1259
+ puts "Processing Image: #{url}"
1260
+ f = URI.open(url)
1261
+ canvas = ChunkyPNG::Canvas.from_io(f)
1262
+ f.close
1263
+ data = canvas.to_rgba_stream
1264
+ width = canvas.width
1265
+ height = canvas.height
1266
+ img = image {
1267
+ image_part(data, width, height, width * 4)
1268
+ }
1269
+ text = url.sub('https://www.ghibli.jp/gallery/thumb-redturtle', '').sub('.png', '')
1270
+ IMAGE_ROWS << [[img, text], [img, text]] # cell values are dual-element arrays
1271
+ rescue StandardError => e
1272
+ warn url, e.message
1273
+ end
1274
+
1275
+ window('The Red Turtle', 670, 350) {
1276
+ horizontal_box {
1277
+ table {
1278
+ image_text_column('image/number')
1279
+ image_text_column('image/number (editable)') {
1280
+ editable true
1281
+ }
1282
+
1283
+ cell_rows IMAGE_ROWS
1284
+ }
1285
+ }
1286
+ }.show
1287
+ ```
1288
+
1289
+ ## Basic Table Button
1290
+
1291
+ [examples/basic_table_button.rb](/examples/basic_table_button.rb)
1292
+
1293
+ Run with this command from the root of the project if you cloned the project:
1294
+
1295
+ ```
1296
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_table_button.rb
1297
+ ```
1298
+
1299
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
1300
+
1301
+ ```
1302
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_table_button'"
1303
+ ```
1304
+
1305
+ Mac | Windows | Linux
1306
+ ----|---------|------
1307
+ ![glimmer-dsl-libui-mac-basic-table-button.png](/images/glimmer-dsl-libui-mac-basic-table-button.png) ![glimmer-dsl-libui-mac-basic-table-button-deleted.png](/images/glimmer-dsl-libui-mac-basic-table-button-deleted.png) | ![glimmer-dsl-libui-windows-basic-table-button.png](/images/glimmer-dsl-libui-windows-basic-table-button.png) ![glimmer-dsl-libui-windows-basic-table-button-deleted.png](/images/glimmer-dsl-libui-windows-basic-table-button-deleted.png) | ![glimmer-dsl-libui-linux-basic-table-button.png](/images/glimmer-dsl-libui-linux-basic-table-button.png) ![glimmer-dsl-libui-linux-basic-table-button-deleted.png](/images/glimmer-dsl-libui-linux-basic-table-button-deleted.png)
1308
+
1309
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (with explicit [data-binding](#data-binding)):
1310
+
1311
+ ```ruby
1312
+ require 'glimmer-dsl-libui'
1313
+
1314
+ class BasicTableButton
1315
+ BasicAnimal = Struct.new(:name, :sound)
1316
+
1317
+ class Animal < BasicAnimal
1318
+ def action
1319
+ 'delete'
1320
+ end
1321
+ end
1322
+
1323
+ include Glimmer
1324
+
1325
+ attr_accessor :animals
1326
+
1327
+ def initialize
1328
+ @animals = [
1329
+ Animal.new('cat', 'meow'),
1330
+ Animal.new('dog', 'woof'),
1331
+ Animal.new('chicken', 'cock-a-doodle-doo'),
1332
+ Animal.new('horse', 'neigh'),
1333
+ Animal.new('cow', 'moo'),
1334
+ ]
1335
+ end
1336
+
1337
+ def launch
1338
+ window('Animal sounds', 400, 200) {
1339
+ horizontal_box {
1340
+ table {
1341
+ text_column('Animal')
1342
+ text_column('Description')
1343
+ button_column('Action') {
1344
+ on_clicked do |row|
1345
+ # Option 1: direct data deletion is the simpler solution
1346
+ # @animals.delete_at(row) # automatically deletes actual table row due to explicit data-binding
1347
+
1348
+ # Option 2: cloning only to demonstrate table row deletion upon explicit setting of animals attribute (cloning is not recommended beyond demonstrating this point)
1349
+ new_animals = @animals.clone
1350
+ new_animals.delete_at(row)
1351
+ self.animals = new_animals # automatically loses deleted table row due to explicit data-binding
1352
+ end
1353
+ }
1354
+
1355
+ cell_rows <= [self, :animals, column_attributes: {'Animal' => :name, 'Description' => :sound}]
1356
+
1357
+ # explicit unidirectional data-binding of table cell_rows to self.animals
1358
+ on_changed do |row, type, row_data|
1359
+ puts "Row #{row} #{type}: #{row_data}"
1360
+ $stdout.flush
1361
+ end
1362
+ }
1363
+ }
1364
+ }.show
1365
+ end
1366
+ end
1367
+
1368
+ BasicTableButton.new.launch
1369
+ ```
1370
+
1371
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (with implicit [data-binding](#data-binding)):
1372
+
1373
+ ```ruby
1374
+ require 'glimmer-dsl-libui'
1375
+
1376
+ include Glimmer
1377
+
1378
+ data = [
1379
+ %w[cat meow delete],
1380
+ %w[dog woof delete],
1381
+ %w[chicken cock-a-doodle-doo delete],
1382
+ %w[horse neigh delete],
1383
+ %w[cow moo delete]
1384
+ ]
1385
+
1386
+ window('Animal sounds', 300, 200) {
1387
+ horizontal_box {
1388
+ table {
1389
+ text_column('Animal')
1390
+ text_column('Description')
1391
+ button_column('Action') {
1392
+ on_clicked do |row|
1393
+ data.delete_at(row) # automatically deletes actual table row due to implicit data-binding
1394
+ end
1395
+ }
1396
+
1397
+ cell_rows data # implicit data-binding
1398
+
1399
+ on_changed do |row, type, row_data|
1400
+ puts "Row #{row} #{type}: #{row_data}"
1401
+ end
1402
+ }
1403
+ }
1404
+ }.show
1405
+ ```
1406
+
1407
+ ## Basic Table Checkbox
1408
+
1409
+ [examples/basic_table_checkbox.rb](/examples/basic_table_checkbox.rb)
1410
+
1411
+ Run with this command from the root of the project if you cloned the project:
1412
+
1413
+ ```
1414
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_table_checkbox.rb
1415
+ ```
1416
+
1417
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
1418
+
1419
+ ```
1420
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_table_checkbox'"
1421
+ ```
1422
+
1423
+ Mac | Windows | Linux
1424
+ ----|---------|------
1425
+ ![glimmer-dsl-libui-mac-basic-table-checkbox.png](/images/glimmer-dsl-libui-mac-basic-table-checkbox.png) | ![glimmer-dsl-libui-windows-basic-table-checkbox.png](/images/glimmer-dsl-libui-windows-basic-table-checkbox.png) | ![glimmer-dsl-libui-linux-basic-table-checkbox.png](/images/glimmer-dsl-libui-linux-basic-table-checkbox.png)
1426
+
1427
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
1428
+
1429
+ ```ruby
1430
+ require 'glimmer-dsl-libui'
1431
+
1432
+ include Glimmer
1433
+
1434
+ data = [
1435
+ ['cat', 'meow', true],
1436
+ ['dog', 'woof', true],
1437
+ ['chicken', 'cock-a-doodle-doo', false],
1438
+ ['horse', 'neigh', true],
1439
+ ['cow', 'moo', true]
1440
+ ]
1441
+
1442
+ window('Animal sounds', 300, 200) {
1443
+ horizontal_box {
1444
+ table {
1445
+ text_column('Animal')
1446
+ text_column('Description')
1447
+ checkbox_column('Mammal')
1448
+
1449
+ cell_rows data
1450
+ }
1451
+ }
1452
+ }.show
1453
+ ```
1454
+
1455
+ ## Basic Table Checkbox Text
1456
+
1457
+ [examples/basic_table_checkbox_text.rb](/examples/basic_table_checkbox_text.rb)
1458
+
1459
+ Run with this command from the root of the project if you cloned the project:
1460
+
1461
+ ```
1462
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_table_checkbox_text.rb
1463
+ ```
1464
+
1465
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
1466
+
1467
+ ```
1468
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_table_checkbox_text'"
1469
+ ```
1470
+
1471
+ Mac | Windows | Linux
1472
+ ----|---------|------
1473
+ ![glimmer-dsl-libui-mac-basic-table-checkbox-text.png](/images/glimmer-dsl-libui-mac-basic-table-checkbox-text.png) | ![glimmer-dsl-libui-windows-basic-table-checkbox-text.png](/images/glimmer-dsl-libui-windows-basic-table-checkbox-text.png) | ![glimmer-dsl-libui-linux-basic-table-checkbox-text.png](/images/glimmer-dsl-libui-linux-basic-table-checkbox-text.png)
1474
+
1475
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
1476
+
1477
+ ```ruby
1478
+ require 'glimmer-dsl-libui'
1479
+
1480
+ include Glimmer
1481
+
1482
+ data = [
1483
+ ['cat', 'meow', [true, 'mammal']],
1484
+ ['dog', 'woof', [true, 'mammal']],
1485
+ ['chicken', 'cock-a-doodle-doo', [false, 'mammal']],
1486
+ ['horse', 'neigh', [true, 'mammal']],
1487
+ ['cow', 'moo', [true, 'mammal']]
1488
+ ]
1489
+
1490
+ window('Animal sounds', 400, 200) {
1491
+ horizontal_box {
1492
+ table {
1493
+ text_column('Animal')
1494
+ text_column('Sound')
1495
+ checkbox_text_column('Description')
1496
+
1497
+ cell_rows data
1498
+ }
1499
+ }
1500
+ }.show
1501
+ ```
1502
+
1503
+ ## Basic Table Progress Bar
1504
+
1505
+ [examples/basic_table_progress_bar.rb](/examples/basic_table_progress_bar.rb)
1506
+
1507
+ Run with this command from the root of the project if you cloned the project:
1508
+
1509
+ ```
1510
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_table_progress_bar.rb
1511
+ ```
1512
+
1513
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
1514
+
1515
+ ```
1516
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_table_progress_bar'"
1517
+ ```
1518
+
1519
+ Mac | Windows | Linux
1520
+ ----|---------|------
1521
+ ![glimmer-dsl-libui-mac-basic-table-progress-bar.png](/images/glimmer-dsl-libui-mac-basic-table-progress-bar.png) | ![glimmer-dsl-libui-windows-basic-table-progress-bar.png](/images/glimmer-dsl-libui-windows-basic-table-progress-bar.png) | ![glimmer-dsl-libui-linux-basic-table-progress-bar.png](/images/glimmer-dsl-libui-linux-basic-table-progress-bar.png)
1522
+
1523
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
1524
+
1525
+ ```ruby
1526
+ require 'glimmer-dsl-libui'
1527
+
1528
+ include Glimmer
1529
+
1530
+ data = [
1531
+ ['task 1', 0],
1532
+ ['task 2', 15],
1533
+ ['task 3', 100],
1534
+ ['task 4', 75],
1535
+ ['task 5', -1],
1536
+ ]
1537
+
1538
+ window('Task Progress', 300, 200) {
1539
+ vertical_box {
1540
+ table {
1541
+ text_column('Task')
1542
+ progress_bar_column('Progress')
1543
+
1544
+ cell_rows data # implicit data-binding
1545
+ }
1546
+
1547
+ button('Mark All As Done') {
1548
+ stretchy false
1549
+
1550
+ on_clicked do
1551
+ data.each_with_index do |row_data, row|
1552
+ data[row][1] = 100 # automatically updates table due to implicit data-binding
1553
+ end
1554
+ end
1555
+ }
1556
+ }
1557
+ }.show
1558
+ ```
1559
+
1560
+ ## Basic Table Color
1561
+
1562
+ [examples/basic_table_color.rb](/examples/basic_table_color.rb)
1563
+
1564
+ Run with this command from the root of the project if you cloned the project:
1565
+
1566
+ ```
1567
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_table_color.rb
1568
+ ```
1569
+
1570
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
1571
+
1572
+ ```
1573
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_table_color'"
1574
+ ```
1575
+
1576
+ Mac | Windows | Linux
1577
+ ----|---------|------
1578
+ ![glimmer-dsl-libui-mac-basic-table-color.png](/images/glimmer-dsl-libui-mac-basic-table-color.png) | ![glimmer-dsl-libui-windows-basic-table-color.png](/images/glimmer-dsl-libui-windows-basic-table-color.png) | ![glimmer-dsl-libui-linux-basic-table-color.png](/images/glimmer-dsl-libui-linux-basic-table-color.png)
1579
+
1580
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version (with explicit [data-binding](#data-binding) to model rows using a presenter):
1581
+
1582
+ ```ruby
1583
+ require 'glimmer-dsl-libui'
1584
+
1585
+ class BasicTableColor
1586
+ Animal = Struct.new(:name, :sound, :mammal)
1587
+
1588
+ class AnimalPresenter < Animal
1589
+ def name_color
1590
+ color = case name
1591
+ when 'cat'
1592
+ :red
1593
+ when 'dog'
1594
+ :yellow
1595
+ when 'chicken'
1596
+ :beige
1597
+ when 'horse'
1598
+ :purple
1599
+ when 'cow'
1600
+ :gray
1601
+ end
1602
+ [name, color]
1603
+ end
1604
+
1605
+ def sound_color
1606
+ color = case name
1607
+ when 'cat', 'chicken', 'cow'
1608
+ :blue
1609
+ when 'dog', 'horse'
1610
+ {r: 240, g: 32, b: 32}
1611
+ end
1612
+ [sound, color]
1613
+ end
1614
+
1615
+ def mammal_description_color
1616
+ color = case name
1617
+ when 'cat', 'dog', 'horse', 'cow'
1618
+ :green
1619
+ when 'chicken'
1620
+ :red
1621
+ end
1622
+ [mammal, 'mammal', color]
1623
+ end
1624
+
1625
+ def image_description_color
1626
+ color = case name
1627
+ when 'cat', 'dog', 'horse'
1628
+ :dark_blue
1629
+ when 'chicken'
1630
+ :beige
1631
+ when 'cow'
1632
+ :brown
1633
+ end
1634
+ [img, 'Glimmer', color]
1635
+ end
1636
+
1637
+ def img
1638
+ # scale image to 24x24 (can be passed as file path String only instead of Array to avoid scaling)
1639
+ [File.expand_path('../icons/glimmer.png', __dir__), 24, 24]
1640
+ end
1641
+
1642
+ def background_color
1643
+ case name
1644
+ when 'cat'
1645
+ {r: 255, g: 120, b: 0, a: 0.5}
1646
+ when 'dog'
1647
+ :skyblue
1648
+ when 'chicken'
1649
+ {r: 5, g: 120, b: 110}
1650
+ when 'horse'
1651
+ '#13a1fb'
1652
+ when 'cow'
1653
+ 0x12ff02
1654
+ end
1655
+ end
1656
+ end
1657
+
1658
+ include Glimmer
1659
+
1660
+ attr_accessor :animals
1661
+
1662
+ def initialize
1663
+ @animals = [
1664
+ AnimalPresenter.new('cat', 'meow', true),
1665
+ AnimalPresenter.new('dog', 'woof', true),
1666
+ AnimalPresenter.new('chicken', 'cock-a-doodle-doo', false),
1667
+ AnimalPresenter.new('horse', 'neigh', true),
1668
+ AnimalPresenter.new('cow', 'moo', true),
1669
+ ]
1670
+ end
1671
+
1672
+ def launch
1673
+ window('Animals', 500, 200) {
1674
+ horizontal_box {
1675
+ table {
1676
+ text_color_column('Animal')
1677
+ text_color_column('Sound')
1678
+ checkbox_text_color_column('Description')
1679
+ image_text_color_column('GUI')
1680
+ background_color_column # must always be the last column and always expects data-binding model attribute `background_color` when binding to Array of models
1681
+
1682
+ cell_rows <= [self, :animals, column_attributes: {'Animal' => :name_color, 'Sound' => :sound_color, 'Description' => :mammal_description_color, 'GUI' => :image_description_color}]
1683
+ }
1684
+ }
1685
+ }.show
1686
+ end
1687
+ end
1688
+
1689
+ BasicTableColor.new.launch
1690
+ ```
1691
+
1692
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (with implicit [data-binding](#data-binding) to raw data rows):
1693
+
1694
+ ```ruby
1695
+ require 'glimmer-dsl-libui'
1696
+
1697
+ include Glimmer
1698
+
1699
+ img = [File.expand_path('../icons/glimmer.png', __dir__), 24, 24] # scales image to 24x24 (can be passed as file path String only instead of Array to avoid scaling)
1700
+
1701
+ data = [
1702
+ [['cat', :red] , ['meow', :blue] , [true, 'mammal', :green], [img, 'Glimmer', :dark_blue], {r: 255, g: 120, b: 0, a: 0.5}],
1703
+ [['dog', :yellow] , ['woof', {r: 240, g: 32, b: 32}] , [true, 'mammal', :green], [img, 'Glimmer', :dark_blue], :skyblue],
1704
+ [['chicken', :beige], ['cock-a-doodle-doo', :blue] , [false, 'mammal', :red] , [img, 'Glimmer', :beige], {r: 5, g: 120, b: 110}],
1705
+ [['horse', :purple] , ['neigh', {r: 240, g: 32, b: 32}], [true, 'mammal', :green], [img, 'Glimmer', :dark_blue], '13a1fb'],
1706
+ [['cow', :gray] , ['moo', :blue] , [true, 'mammal', :green], [img, 'Glimmer', :brown], 0x12ff02]
1707
+ ]
1708
+
1709
+ window('Animals', 500, 200) {
1710
+ horizontal_box {
1711
+ table {
1712
+ text_color_column('Animal')
1713
+ text_color_column('Sound')
1714
+ checkbox_text_color_column('Description')
1715
+ image_text_color_column('GUI')
1716
+ background_color_column # must be the last column
1717
+
1718
+ cell_rows data
1719
+ }
1720
+ }
1721
+ }.show
1722
+ ```
1723
+
1724
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 3 (with implicit [data-binding](#data-binding) to raw data rows and manual construction of [libui](https://github.com/andlabs/libui) `image` from `image_part`):
1725
+
1726
+ ```ruby
1727
+ require 'glimmer-dsl-libui'
1728
+ require 'chunky_png'
1729
+
1730
+ include Glimmer
1731
+
1732
+ f = File.open(File.expand_path('../icons/glimmer.png', __dir__))
1733
+ canvas = ChunkyPNG::Canvas.from_io(f)
1734
+ f.close
1735
+ canvas.resample_nearest_neighbor!(24, 24)
1736
+ data = canvas.to_rgba_stream
1737
+ width = canvas.width
1738
+ height = canvas.height
1739
+ img = image {
1740
+ image_part(data, width, height, width * 4)
1741
+ }
1742
+
1743
+ data = [
1744
+ [['cat', :red] , ['meow', :blue] , [true, 'mammal', :green], [img, 'Glimmer', :dark_blue], {r: 255, g: 120, b: 0, a: 0.5}],
1745
+ [['dog', :yellow] , ['woof', {r: 240, g: 32, b: 32}] , [true, 'mammal', :green], [img, 'Glimmer', :dark_blue], :skyblue],
1746
+ [['chicken', :beige], ['cock-a-doodle-doo', :blue] , [false, 'mammal', :red] , [img, 'Glimmer', :beige], {r: 5, g: 120, b: 110}],
1747
+ [['horse', :purple] , ['neigh', {r: 240, g: 32, b: 32}], [true, 'mammal', :green], [img, 'Glimmer', :dark_blue], '13a1fb'],
1748
+ [['cow', :gray] , ['moo', :blue] , [true, 'mammal', :green], [img, 'Glimmer', :brown], 0x12ff02]
1749
+ ]
1750
+
1751
+ window('Animals', 500, 200) {
1752
+ horizontal_box {
1753
+ table {
1754
+ text_color_column('Animal')
1755
+ text_color_column('Sound')
1756
+ checkbox_text_color_column('Description')
1757
+ image_text_color_column('GUI')
1758
+ background_color_column
1759
+
1760
+ cell_rows data
1761
+ }
1762
+ }
1763
+ }.show
1764
+ ```
1765
+
1766
+ ## Basic Area
1767
+
1768
+ [examples/basic_area.rb](/examples/basic_area.rb)
1769
+
1770
+ Run with this command from the root of the project if you cloned the project:
1771
+
1772
+ ```
1773
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_area.rb
1774
+ ```
1775
+
1776
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
1777
+
1778
+ ```
1779
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_area'"
1780
+ ```
1781
+
1782
+ Mac | Windows | Linux
1783
+ ----|---------|------
1784
+ ![glimmer-dsl-libui-mac-basic-area.png](/images/glimmer-dsl-libui-mac-basic-area.png) | ![glimmer-dsl-libui-windows-basic-area.png](/images/glimmer-dsl-libui-windows-basic-area.png) | ![glimmer-dsl-libui-linux-basic-area.png](/images/glimmer-dsl-libui-linux-basic-area.png)
1785
+
1786
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
1787
+
1788
+ ```ruby
1789
+ require 'libui'
1790
+
1791
+ UI = LibUI
1792
+
1793
+ UI.init
1794
+
1795
+ handler = UI::FFI::AreaHandler.malloc
1796
+ area = UI.new_area(handler)
1797
+ brush = UI::FFI::DrawBrush.malloc
1798
+
1799
+ handler_draw_event = Fiddle::Closure::BlockCaller.new(0, [1, 1, 1]) do |_, _, area_draw_params|
1800
+ path = UI.draw_new_path(0)
1801
+ UI.draw_path_add_rectangle(path, 0, 0, 400, 400)
1802
+ UI.draw_path_end(path)
1803
+ brush.Type = 0
1804
+ brush.R = 0.4
1805
+ brush.G = 0.4
1806
+ brush.B = 0.8
1807
+ brush.A = 1.0
1808
+ area_draw_params = UI::FFI::AreaDrawParams.new(area_draw_params)
1809
+ UI.draw_fill(area_draw_params.Context, path, brush.to_ptr)
1810
+ UI.draw_free_path(path)
1811
+ end
1812
+
1813
+ handler.Draw = handler_draw_event
1814
+ handler.MouseEvent = Fiddle::Closure::BlockCaller.new(0, [0]) {}
1815
+ handler.MouseCrossed = Fiddle::Closure::BlockCaller.new(0, [0]) {}
1816
+ handler.DragBroken = Fiddle::Closure::BlockCaller.new(0, [0]) {}
1817
+ handler.KeyEvent = Fiddle::Closure::BlockCaller.new(0, [0]) {}
1818
+
1819
+ box = UI.new_vertical_box
1820
+ UI.box_set_padded(box, 1)
1821
+ UI.box_append(box, area, 1)
1822
+
1823
+ main_window = UI.new_window('Basic Area', 400, 400, 1)
1824
+ UI.window_set_margined(main_window, 1)
1825
+ UI.window_set_child(main_window, box)
1826
+
1827
+ UI.window_on_closing(main_window) do
1828
+ UI.control_destroy(main_window)
1829
+ UI.quit
1830
+ 0
1831
+ end
1832
+ UI.control_show(main_window)
1833
+
1834
+ UI.main
1835
+ UI.quit
1836
+ ```
1837
+
1838
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
1839
+
1840
+ ```ruby
1841
+ require 'glimmer-dsl-libui'
1842
+
1843
+ include Glimmer
1844
+
1845
+ window('Basic Area', 400, 400) {
1846
+ margined true
1847
+
1848
+ vertical_box {
1849
+ area {
1850
+ path { # a stable path is added declaratively
1851
+ rectangle(0, 0, 400, 400)
1852
+
1853
+ fill r: 102, g: 102, b: 204, a: 1.0
1854
+ }
1855
+ }
1856
+ }
1857
+ }.show
1858
+ ```
1859
+
1860
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (semi-declarative `on_draw` dynamic `path` approach):
1861
+
1862
+ ```ruby
1863
+ require 'glimmer-dsl-libui'
1864
+
1865
+ include Glimmer
1866
+
1867
+ window('Basic Area', 400, 400) {
1868
+ margined true
1869
+
1870
+ vertical_box {
1871
+ area {
1872
+ on_draw do |area_draw_params|
1873
+ path { # a dynamic path is added semi-declaratively inside on_draw block
1874
+ rectangle(0, 0, 400, 400)
1875
+
1876
+ fill r: 102, g: 102, b: 204, a: 1.0
1877
+ }
1878
+ end
1879
+ }
1880
+ }
1881
+ }.show
1882
+ ```
1883
+
1884
+ ## Basic Scrolling Area
1885
+
1886
+ [examples/basic_scrolling_area.rb](/examples/basic_scrolling_area.rb)
1887
+
1888
+ Run with this command from the root of the project if you cloned the project:
1889
+
1890
+ ```
1891
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_scrolling_area.rb
1892
+ ```
1893
+
1894
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
1895
+
1896
+ ```
1897
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_scrolling_area'"
1898
+ ```
1899
+
1900
+ Mac | Windows | Linux
1901
+ ----|---------|------
1902
+ ![glimmer-dsl-libui-mac-dynamic-area.png](/images/glimmer-dsl-libui-mac-basic-scrolling-area.png) ![glimmer-dsl-libui-mac-dynamic-area-updated.png](/images/glimmer-dsl-libui-mac-basic-scrolling-area-scrolled.png) | ![glimmer-dsl-libui-windows-dynamic-area.png](/images/glimmer-dsl-libui-windows-basic-scrolling-area.png) ![glimmer-dsl-libui-windows-dynamic-area-updated.png](/images/glimmer-dsl-libui-windows-basic-scrolling-area-scrolled.png) | ![glimmer-dsl-libui-linux-dynamic-area.png](/images/glimmer-dsl-libui-linux-basic-scrolling-area.png) ![glimmer-dsl-libui-linux-dynamic-area-updated.png](/images/glimmer-dsl-libui-linux-basic-scrolling-area-scrolled.png)
1903
+
1904
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
1905
+
1906
+ ```ruby
1907
+ require 'glimmer-dsl-libui'
1908
+
1909
+ class BasicScrollingArea
1910
+ include Glimmer
1911
+
1912
+ SCROLLING_AREA_WIDTH = 800
1913
+ SCROLLING_AREA_HEIGHT = 400
1914
+ SCROLLING_AREA_PADDING_X = 20
1915
+ SCROLLING_AREA_PADDING_Y = 20
1916
+
1917
+ def initialize
1918
+ @x = SCROLLING_AREA_PADDING_X
1919
+ @y = SCROLLING_AREA_HEIGHT - SCROLLING_AREA_PADDING_Y
1920
+ create_gui
1921
+ Glimmer::LibUI.timer(0.01) do
1922
+ @x += SCROLLING_AREA_PADDING_X
1923
+ @y = [[@y + rand(SCROLLING_AREA_PADDING_Y*4)*(rand(2) == 0 ? -1 : 1), SCROLLING_AREA_PADDING_Y].max, SCROLLING_AREA_HEIGHT - SCROLLING_AREA_PADDING_Y].min
1924
+ @graph.content { # re-open @graph's content and add a line
1925
+ line(@x, @y)
1926
+ }
1927
+ # if there is a need to enlarge scrolling area, call `@scrolling_area.set_size(new_width, new_height)`
1928
+ @scrolling_area.scroll_to(@x - (SCROLLING_AREA_WIDTH/2), @y) # 3rd and 4th arguments for width and height are assumed as those of main window by default if not supplied
1929
+ # return false to stop timer once @x exceeds scrolling area width - padding
1930
+ false if @x >= (SCROLLING_AREA_WIDTH - SCROLLING_AREA_PADDING_X*2)
1931
+ end
1932
+ end
1933
+
1934
+ def launch
1935
+ @main_window.show
1936
+ end
1937
+
1938
+ def x_axis
1939
+ polyline(SCROLLING_AREA_PADDING_X, SCROLLING_AREA_HEIGHT - SCROLLING_AREA_PADDING_Y, SCROLLING_AREA_WIDTH - SCROLLING_AREA_PADDING_X*2, SCROLLING_AREA_HEIGHT - SCROLLING_AREA_PADDING_Y) {
1940
+ stroke :black, thickness: 3
1941
+ }
1942
+
1943
+ ((SCROLLING_AREA_WIDTH - SCROLLING_AREA_PADDING_X*4) / SCROLLING_AREA_PADDING_X).times do |x_multiplier|
1944
+ x = x_multiplier*SCROLLING_AREA_PADDING_X + SCROLLING_AREA_PADDING_X*2
1945
+ y = SCROLLING_AREA_HEIGHT - SCROLLING_AREA_PADDING_Y
1946
+
1947
+ polyline(x, y, x, y + SCROLLING_AREA_PADDING_Y/2) {
1948
+ stroke :black, thickness: 2
1949
+ }
1950
+ end
1951
+ end
1952
+
1953
+ def y_axis
1954
+ polyline(SCROLLING_AREA_PADDING_X, SCROLLING_AREA_PADDING_Y, SCROLLING_AREA_PADDING_X, SCROLLING_AREA_HEIGHT - SCROLLING_AREA_PADDING_Y) {
1955
+ stroke :black, thickness: 3
1956
+ }
1957
+
1958
+ ((SCROLLING_AREA_HEIGHT - SCROLLING_AREA_PADDING_Y*3) / SCROLLING_AREA_PADDING_Y).times do |y_multiplier|
1959
+ x = SCROLLING_AREA_PADDING_X
1960
+ y = y_multiplier*SCROLLING_AREA_PADDING_Y + SCROLLING_AREA_PADDING_Y*2
1961
+
1962
+ polyline(x, y, x - SCROLLING_AREA_PADDING_X/2, y) {
1963
+ stroke :black, thickness: 2
1964
+ }
1965
+ end
1966
+ end
1967
+
1968
+ def create_gui
1969
+ @main_window = window('Basic Scrolling Area', SCROLLING_AREA_WIDTH / 2, SCROLLING_AREA_HEIGHT) {
1970
+ resizable false
1971
+
1972
+ @scrolling_area = scrolling_area(SCROLLING_AREA_WIDTH, SCROLLING_AREA_HEIGHT) {
1973
+ x_axis
1974
+ y_axis
1975
+
1976
+ @graph = figure(SCROLLING_AREA_PADDING_X, SCROLLING_AREA_HEIGHT - SCROLLING_AREA_PADDING_Y) {
1977
+ stroke :blue, thickness: 2
1978
+ }
1979
+ }
1980
+ }
1981
+ end
1982
+ end
1983
+
1984
+ BasicScrollingArea.new.launch
1985
+ ```
1986
+
1987
+ ## Basic Image
1988
+
1989
+ Please note the caveats of [Area Image](#area-image) **(Alpha Feature)** with regards to this example.
1990
+
1991
+ [examples/basic_image.rb](/examples/basic_image.rb)
1992
+
1993
+ Run with this command from the root of the project if you cloned the project:
1994
+
1995
+ ```
1996
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_image.rb
1997
+ ```
1998
+
1999
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
2000
+
2001
+ ```
2002
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_image'"
2003
+ ```
2004
+
2005
+ Mac | Windows | Linux
2006
+ ----|---------|------
2007
+ ![glimmer-dsl-libui-mac-basic-image.png](/images/glimmer-dsl-libui-mac-basic-image.png) | ![glimmer-dsl-libui-windows-basic-image.png](/images/glimmer-dsl-libui-windows-basic-image.png) | ![glimmer-dsl-libui-linux-basic-image.png](/images/glimmer-dsl-libui-linux-basic-image.png)
2008
+
2009
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
2010
+
2011
+ ```ruby
2012
+ require 'glimmer-dsl-libui'
2013
+
2014
+ include Glimmer
2015
+
2016
+ window('Basic Image', 96, 96) {
2017
+ area {
2018
+ # image is not a real LibUI control. It is built in Glimmer as a custom control that renders
2019
+ # tiny pixels/lines as rectangle paths. As such, it does not have good performance, but can
2020
+ # be used in exceptional circumstances where an image control is really needed.
2021
+ #
2022
+ # Furthermore, adding image directly under area is even slower due to taking up more memory for every
2023
+ # image pixel rendered. Check basic_image2.rb for a faster alternative using on_draw manually.
2024
+ #
2025
+ # It is recommended to pass width/height args to shrink image and achieve faster performance.
2026
+ image(File.expand_path('../icons/glimmer.png', __dir__), height: 96) # width is automatically calculated from height while preserving original aspect ratio
2027
+ # image(File.expand_path('../icons/glimmer.png', __dir__), width: 96, height: 96) # you can specify both width, height options as alternative
2028
+ # image(File.expand_path('../icons/glimmer.png', __dir__), 96, 96) # you can specify width, height args as alternative
2029
+ # image(File.expand_path('../icons/glimmer.png', __dir__), 0, 0, 96, 96) # you can specify x, y, width, height args as alternative
2030
+ # image(File.expand_path('../icons/glimmer.png', __dir__), x: 0, y: 0, width: 96, height: 96) # you can specify x, y, width, height options as alternative
2031
+ }
2032
+ }.show
2033
+ ```
2034
+
2035
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2 (better performance via `on_draw`):
2036
+
2037
+ ```ruby
2038
+ require 'glimmer-dsl-libui'
2039
+
2040
+ include Glimmer
2041
+
2042
+ window('Basic Image', 96, 96) {
2043
+ area {
2044
+ on_draw do |area_draw_params|
2045
+ image(File.expand_path('../icons/glimmer.png', __dir__), height: 96)
2046
+ end
2047
+ }
2048
+ }.show
2049
+ ```
2050
+
2051
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 3 (explicit properties):
2052
+
2053
+ ```ruby
2054
+ require 'glimmer-dsl-libui'
2055
+
2056
+ include Glimmer
2057
+
2058
+ window('Basic Image', 96, 96) {
2059
+ area {
2060
+ # image is not a real LibUI control. It is built in Glimmer as a custom control that renders
2061
+ # tiny pixels/lines as rectangle paths. As such, it does not have good performance, but can
2062
+ # be used in exceptional circumstances where an image control is really needed.
2063
+ #
2064
+ # Furthermore, adding image directly under area is even slower due to taking up more memory for every
2065
+ # image pixel rendered. Check basic_image4.rb for a faster alternative using on_draw manually.
2066
+ #
2067
+ # It is recommended to pass width/height args to shrink image and achieve faster performance.
2068
+ image {
2069
+ file File.expand_path('../icons/glimmer.png', __dir__)
2070
+ # x 0 # default
2071
+ # y 0 # default
2072
+ # width 96 # gets calculated from height while preserving original aspect ratio of 512x512
2073
+ height 96
2074
+ }
2075
+ }
2076
+ }.show
2077
+ ```
2078
+
2079
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 4 (better performance with `on_draw` when setting explicit properties):
2080
+
2081
+ ```ruby
2082
+ require 'glimmer-dsl-libui'
2083
+
2084
+ include Glimmer
2085
+
2086
+ window('Basic Image', 96, 96) {
2087
+ area {
2088
+ on_draw do |area_draw_params|
2089
+ image {
2090
+ file File.expand_path('../icons/glimmer.png', __dir__)
2091
+ height 96
2092
+ }
2093
+ end
2094
+ }
2095
+ }.show
2096
+ ```
2097
+
2098
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 5 (fully manual pixel-by-pixel rendering):
2099
+
2100
+ ```ruby
2101
+ # frozen_string_literal: true
2102
+
2103
+ # This is the manual way of rendering an image unto an area control.
2104
+ # It could come in handy in special situations.
2105
+ # Otherwise, it is recommended to simply utilize the `image` control that
2106
+ # can be nested under area or area on_draw listener to automate all this work.
2107
+
2108
+ require 'glimmer-dsl-libui'
2109
+ require 'chunky_png'
2110
+
2111
+ include Glimmer
2112
+
2113
+ puts 'Parsing image...'; $stdout.flush
2114
+
2115
+ f = File.open(File.expand_path('../icons/glimmer.png', __dir__))
2116
+ canvas = ChunkyPNG::Canvas.from_io(f)
2117
+ f.close
2118
+ canvas.resample_nearest_neighbor!(96, 96)
2119
+ data = canvas.to_rgba_stream
2120
+ width = canvas.width
2121
+ height = canvas.height
2122
+ puts "Image width: #{width}"
2123
+ puts "Image height: #{height}"
2124
+
2125
+ puts 'Parsing colors...'; $stdout.flush
2126
+
2127
+ color_maps = height.times.map do |y|
2128
+ width.times.map do |x|
2129
+ r = data[(y*width + x)*4].ord
2130
+ g = data[(y*width + x)*4 + 1].ord
2131
+ b = data[(y*width + x)*4 + 2].ord
2132
+ a = data[(y*width + x)*4 + 3].ord
2133
+ {x: x, y: y, color: {r: r, g: g, b: b, a: a}}
2134
+ end
2135
+ end.flatten
2136
+ puts "#{color_maps.size} pixels to render..."; $stdout.flush
2137
+
2138
+ puts 'Parsing shapes...'; $stdout.flush
2139
+
2140
+ shape_maps = []
2141
+ original_color_maps = color_maps.dup
2142
+ indexed_original_color_maps = Hash[original_color_maps.each_with_index.to_a]
2143
+ color_maps.each do |color_map|
2144
+ index = indexed_original_color_maps[color_map]
2145
+ @rectangle_start_x ||= color_map[:x]
2146
+ @rectangle_width ||= 1
2147
+ if color_map[:x] < width - 1 && color_map[:color] == original_color_maps[index + 1][:color]
2148
+ @rectangle_width += 1
2149
+ else
2150
+ if color_map[:x] > 0 && color_map[:color] == original_color_maps[index - 1][:color]
2151
+ shape_maps << {x: @rectangle_start_x, y: color_map[:y], width: @rectangle_width, height: 1, color: color_map[:color]}
2152
+ else
2153
+ shape_maps << {x: color_map[:x], y: color_map[:y], width: 1, height: 1, color: color_map[:color]}
2154
+ end
2155
+ @rectangle_width = 1
2156
+ @rectangle_start_x = color_map[:x] == width - 1 ? 0 : color_map[:x] + 1
2157
+ end
2158
+ end
2159
+ puts "#{shape_maps.size} shapes to render..."; $stdout.flush
2160
+
2161
+ puts 'Rendering image...'; $stdout.flush
2162
+
2163
+ window('Basic Image', 96, 96) {
2164
+ area {
2165
+ on_draw do |area_draw_params|
2166
+ shape_maps.each do |shape_map|
2167
+ path {
2168
+ rectangle(shape_map[:x], shape_map[:y], shape_map[:width], shape_map[:height])
2169
+
2170
+ fill shape_map[:color]
2171
+ }
2172
+ end
2173
+ end
2174
+ }
2175
+ }.show
2176
+ ```
2177
+
2178
+ ## Basic Transform
2179
+
2180
+ [examples/basic_transform.rb](/examples/basic_transform.rb)
2181
+
2182
+ Run with this command from the root of the project if you cloned the project:
2183
+
2184
+ ```
2185
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_transform.rb
2186
+ ```
2187
+
2188
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
2189
+
2190
+ ```
2191
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_transform'"
2192
+ ```
2193
+
2194
+ Mac | Windows | Linux
2195
+ ----|---------|------
2196
+ ![glimmer-dsl-libui-mac-basic-transform.png](/images/glimmer-dsl-libui-mac-basic-transform.png) | ![glimmer-dsl-libui-windows-basic-transform.png](/images/glimmer-dsl-libui-windows-basic-transform.png) | ![glimmer-dsl-libui-linux-basic-transform.png](/images/glimmer-dsl-libui-linux-basic-transform.png)
2197
+
2198
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
2199
+
2200
+ ```ruby
2201
+ require 'glimmer-dsl-libui'
2202
+
2203
+ include Glimmer
2204
+
2205
+ window('Basic Transform', 350, 350) {
2206
+ area {
2207
+ square(0, 0, 350) {
2208
+ fill r: 255, g: 255, b: 0
2209
+ }
2210
+ 40.times do |n|
2211
+ square(0, 0, 100) {
2212
+ fill r: [255 - n*5, 0].max, g: [n*5, 255].min, b: 0, a: 0.5
2213
+ stroke :black, thickness: 2
2214
+
2215
+ transform {
2216
+ unless OS.windows?
2217
+ skew 0.15, 0.15
2218
+ translate 50, 50
2219
+ end
2220
+ rotate 100, 100, -9 * n
2221
+ scale 1.1, 1.1
2222
+ if OS.windows?
2223
+ skew 0.15, 0.15
2224
+ translate 50, 50
2225
+ end
2226
+ }
2227
+ }
2228
+ end
2229
+ }
2230
+ }.show
2231
+ ```
2232
+
2233
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2:
2234
+
2235
+ ```ruby
2236
+ require 'glimmer-dsl-libui'
2237
+
2238
+ include Glimmer
2239
+
2240
+ window('Basic Transform', 350, 350) {
2241
+ area {
2242
+ path {
2243
+ square(0, 0, 350)
2244
+
2245
+ fill r: 255, g: 255, b: 0
2246
+ }
2247
+ 40.times do |n|
2248
+ path {
2249
+ square(0, 0, 100)
2250
+
2251
+ fill r: [255 - n*5, 0].max, g: [n*5, 255].min, b: 0, a: 0.5
2252
+ stroke :black, thickness: 2
2253
+
2254
+ transform {
2255
+ unless OS.windows?
2256
+ skew 0.15, 0.15
2257
+ translate 50, 50
2258
+ end
2259
+ rotate 100, 100, -9 * n
2260
+ scale 1.1, 1.1
2261
+ if OS.windows?
2262
+ skew 0.15, 0.15
2263
+ translate 50, 50
2264
+ end
2265
+ }
2266
+ }
2267
+ end
2268
+ }
2269
+ }.show
2270
+ ```
2271
+
2272
+ ## Basic Draw Text
2273
+
2274
+ [examples/basic_draw_text.rb](/examples/basic_draw_text.rb)
2275
+
2276
+ Run with this command from the root of the project if you cloned the project:
2277
+
2278
+ ```
2279
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_draw_text.rb
2280
+ ```
2281
+
2282
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
2283
+
2284
+ ```
2285
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_draw_text'"
2286
+ ```
2287
+
2288
+ Mac | Windows | Linux
2289
+ ----|---------|------
2290
+ ![glimmer-dsl-libui-mac-basic-draw-text.png](/images/glimmer-dsl-libui-mac-basic-draw-text.png) | ![glimmer-dsl-libui-windows-basic-draw-text.png](/images/glimmer-dsl-libui-windows-basic-draw-text.png) | ![glimmer-dsl-libui-linux-basic-draw-text.png](/images/glimmer-dsl-libui-linux-basic-draw-text.png)
2291
+
2292
+ [LibUI](https://github.com/kojix2/LibUI) Original Version:
2293
+
2294
+ ```ruby
2295
+ require 'libui'
2296
+
2297
+ UI = LibUI
2298
+
2299
+ UI.init
2300
+
2301
+ handler = UI::FFI::AreaHandler.malloc
2302
+ area = UI.new_area(handler)
2303
+
2304
+ # Michael Ende (1929-1995)
2305
+ # The Neverending Story is a fantasy novel by German writer Michael Ende,
2306
+ # The English version, translated by Ralph Manheim, was published in 1983.
2307
+
2308
+ TITLE = 'Michael Ende (1929-1995) The Neverending Story'
2309
+
2310
+ str1 = \
2311
+ ' At last Ygramul sensed that something was coming toward ' \
2312
+ 'her. With the speed of lightning, she turned about, confronting ' \
2313
+ 'Atreyu with an enormous steel-blue face. Her single eye had a ' \
2314
+ 'vertical pupil, which stared at Atreyu with inconceivable malignancy. '
2315
+
2316
+ str2 = \
2317
+ ' A cry of fear escaped Bastian. '
2318
+
2319
+ str3 = \
2320
+ ' A cry of terror passed through the ravine and echoed from ' \
2321
+ 'side to side. Ygramul turned her eye to left and right, to see if ' \
2322
+ 'someone else had arrived, for that sound could not have been ' \
2323
+ 'made by the boy who stood there as though paralyzed with ' \
2324
+ 'horror. '
2325
+
2326
+ str4 = \
2327
+ ' Could she have heard my cry? Bastion wondered in alarm. ' \
2328
+ "But that's not possible. "
2329
+
2330
+ str5 = \
2331
+ ' And then Atreyu heard Ygramuls voice. It was very high ' \
2332
+ 'and slightly hoarse, not at all the right kind of voice for that ' \
2333
+ 'enormous face. Her lips did not move as she spoke. It was the ' \
2334
+ 'buzzing of a great swarm of hornets that shaped itself into ' \
2335
+ 'words. '
2336
+
2337
+ str = ''
2338
+ attr_str = UI.new_attributed_string(str)
2339
+
2340
+ def attr_str.append(what, color)
2341
+ case color
2342
+ when :red
2343
+ color_attribute = UI.new_color_attribute(0.0, 0.5, 0.0, 0.7)
2344
+ when :green
2345
+ color_attribute = UI.new_color_attribute(0.5, 0.0, 0.25, 0.7)
2346
+ end
2347
+ start = UI.attributed_string_len(self)
2348
+ UI.attributed_string_append_unattributed(self, what)
2349
+ UI.attributed_string_set_attribute(self, color_attribute, start, start + what.size)
2350
+ UI.attributed_string_append_unattributed(self, "\n\n")
2351
+ end
2352
+
2353
+ attr_str.append(str1, :green)
2354
+ attr_str.append(str2, :red)
2355
+ attr_str.append(str3, :green)
2356
+ attr_str.append(str4, :red)
2357
+ attr_str.append(str5, :green)
2358
+
2359
+ Georgia = 'Georgia'
2360
+
2361
+ handler_draw_event = Fiddle::Closure::BlockCaller.new(0, [1, 1, 1]) do |_, _, adp|
2362
+ area_draw_params = UI::FFI::AreaDrawParams.new(adp)
2363
+ default_font = UI::FFI::FontDescriptor.malloc
2364
+ default_font.Family = Georgia
2365
+ default_font.Size = 13
2366
+ default_font.Weight = 500
2367
+ default_font.Italic = 0
2368
+ default_font.Stretch = 4
2369
+ params = UI::FFI::DrawTextLayoutParams.malloc
2370
+
2371
+ # UI.font_button_font(font_button, default_font)
2372
+ params.String = attr_str
2373
+ params.DefaultFont = default_font
2374
+ params.Width = area_draw_params.AreaWidth
2375
+ params.Align = 0
2376
+ text_layout = UI.draw_new_text_layout(params)
2377
+ UI.draw_text(area_draw_params.Context, text_layout, 0, 0)
2378
+ UI.draw_free_text_layout(text_layout)
2379
+ end
2380
+
2381
+ handler.Draw = handler_draw_event
2382
+ # Assigning to local variables
2383
+ # This is intended to protect Fiddle::Closure from garbage collection.
2384
+ handler.MouseEvent = (c1 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
2385
+ handler.MouseCrossed = (c2 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
2386
+ handler.DragBroken = (c3 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
2387
+ handler.KeyEvent = (c4 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
2388
+
2389
+ box = UI.new_vertical_box
2390
+ UI.box_set_padded(box, 1)
2391
+ UI.box_append(box, area, 1)
2392
+
2393
+ main_window = UI.new_window(TITLE, 600, 400, 1)
2394
+ UI.window_set_margined(main_window, 1)
2395
+ UI.window_set_child(main_window, box)
2396
+
2397
+ UI.window_on_closing(main_window) do
2398
+ UI.control_destroy(main_window)
2399
+ UI.quit
2400
+ 0
2401
+ end
2402
+ UI.control_show(main_window)
2403
+
2404
+ UI.main
2405
+ UI.quit
2406
+ ```
2407
+
2408
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
2409
+
2410
+ ```ruby
2411
+ require 'glimmer-dsl-libui'
2412
+
2413
+ # Michael Ende (1929-1995)
2414
+ # The Neverending Story is a fantasy novel by German writer Michael Ende,
2415
+ # The English version, translated by Ralph Manheim, was published in 1983.
2416
+ class BasicDrawText
2417
+ include Glimmer
2418
+
2419
+ def alternating_color_string(initial: false, &block)
2420
+ @index = 0 if initial
2421
+ @index += 1
2422
+ string {
2423
+ if @index.odd?
2424
+ color r: 0.5, g: 0, b: 0.25, a: 0.7
2425
+ else
2426
+ color r: 0, g: 0.5, b: 0, a: 0.7
2427
+ end
2428
+
2429
+ block.call + "\n\n"
2430
+ }
2431
+ end
2432
+
2433
+ def launch
2434
+ window('Michael Ende (1929-1995) The Neverending Story', 600, 400) {
2435
+ margined true
2436
+
2437
+ area {
2438
+ text { # default arguments for x, y, and width are (0, 0, area_draw_params[:area_width])
2439
+ # align :left # default alignment
2440
+ default_font family: 'Georgia', size: 13, weight: :medium, italic: :normal, stretch: :normal
2441
+
2442
+ alternating_color_string(initial: true) {
2443
+ ' At last Ygramul sensed that something was coming toward ' \
2444
+ 'her. With the speed of lightning, she turned about, confronting ' \
2445
+ 'Atreyu with an enormous steel-blue face. Her single eye had a ' \
2446
+ 'vertical pupil, which stared at Atreyu with inconceivable malignancy. '
2447
+ }
2448
+ alternating_color_string {
2449
+ ' A cry of fear escaped Bastian. '
2450
+ }
2451
+ alternating_color_string {
2452
+ ' A cry of terror passed through the ravine and echoed from ' \
2453
+ 'side to side. Ygramul turned her eye to left and right, to see if ' \
2454
+ 'someone else had arrived, for that sound could not have been ' \
2455
+ 'made by the boy who stood there as though paralyzed with ' \
2456
+ 'horror. '
2457
+ }
2458
+ alternating_color_string {
2459
+ ' Could she have heard my cry? Bastion wondered in alarm. ' \
2460
+ "But that's not possible. "
2461
+ }
2462
+ alternating_color_string {
2463
+ ' And then Atreyu heard Ygramuls voice. It was very high ' \
2464
+ 'and slightly hoarse, not at all the right kind of voice for that ' \
2465
+ 'enormous face. Her lips did not move as she spoke. It was the ' \
2466
+ 'buzzing of a great swarm of hornets that shaped itself into ' \
2467
+ 'words. '
2468
+ }
2469
+ }
2470
+ }
2471
+ }.show
2472
+ end
2473
+ end
2474
+
2475
+ BasicDrawText.new.launch
2476
+ ```
2477
+
2478
+ [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version 2:
2479
+
2480
+ ```ruby
2481
+ require 'glimmer-dsl-libui'
2482
+
2483
+ # Michael Ende (1929-1995)
2484
+ # The Neverending Story is a fantasy novel by German writer Michael Ende,
2485
+ # The English version, translated by Ralph Manheim, was published in 1983.
2486
+ class BasicDrawText
2487
+ include Glimmer
2488
+
2489
+ def alternating_color_string(initial: false, &block)
2490
+ @index = 0 if initial
2491
+ @index += 1
2492
+ string {
2493
+ if @index.odd?
2494
+ color r: 0.5, g: 0, b: 0.25, a: 0.7
2495
+ else
2496
+ color r: 0, g: 0.5, b: 0, a: 0.7
2497
+ end
2498
+
2499
+ block.call + "\n\n"
2500
+ }
2501
+ end
2502
+
2503
+ def launch
2504
+ window('Michael Ende (1929-1995) The Neverending Story', 600, 400) {
2505
+ margined true
2506
+
2507
+ area {
2508
+ on_draw do |area_draw_params|
2509
+ text { # default arguments for x, y, and width are (0, 0, area_draw_params[:area_width])
2510
+ # align :left # default alignment
2511
+ default_font family: 'Georgia', size: 13, weight: :medium, italic: :normal, stretch: :normal
2512
+
2513
+ alternating_color_string(initial: true) {
2514
+ ' At last Ygramul sensed that something was coming toward ' \
2515
+ 'her. With the speed of lightning, she turned about, confronting ' \
2516
+ 'Atreyu with an enormous steel-blue face. Her single eye had a ' \
2517
+ 'vertical pupil, which stared at Atreyu with inconceivable malignancy. '
2518
+ }
2519
+ alternating_color_string {
2520
+ ' A cry of fear escaped Bastian. '
2521
+ }
2522
+ alternating_color_string {
2523
+ ' A cry of terror passed through the ravine and echoed from ' \
2524
+ 'side to side. Ygramul turned her eye to left and right, to see if ' \
2525
+ 'someone else had arrived, for that sound could not have been ' \
2526
+ 'made by the boy who stood there as though paralyzed with ' \
2527
+ 'horror. '
2528
+ }
2529
+ alternating_color_string {
2530
+ ' Could she have heard my cry? Bastion wondered in alarm. ' \
2531
+ "But that's not possible. "
2532
+ }
2533
+ alternating_color_string {
2534
+ ' And then Atreyu heard Ygramuls voice. It was very high ' \
2535
+ 'and slightly hoarse, not at all the right kind of voice for that ' \
2536
+ 'enormous face. Her lips did not move as she spoke. It was the ' \
2537
+ 'buzzing of a great swarm of hornets that shaped itself into ' \
2538
+ 'words. '
2539
+ }
2540
+ }
2541
+ end
2542
+ }
2543
+ }.show
2544
+ end
2545
+ end
2546
+
2547
+ BasicDrawText.new.launch
2548
+ ```
2549
+
2550
+ ## Basic Code Area
2551
+
2552
+ [examples/basic_code_area.rb](/examples/basic_code_area.rb)
2553
+
2554
+ Run with this command from the root of the project if you cloned the project:
2555
+
2556
+ ```
2557
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_code_area.rb
2558
+ ```
2559
+
2560
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
2561
+
2562
+ ```
2563
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_code_area'"
2564
+ ```
2565
+
2566
+ Mac | Windows | Linux
2567
+ ----|---------|------
2568
+ ![glimmer-dsl-libui-mac-basic-code-area.png](/images/glimmer-dsl-libui-mac-basic-code-area.png) | ![glimmer-dsl-libui-windows-basic-code-area.png](/images/glimmer-dsl-libui-windows-basic-code-area.png) | ![glimmer-dsl-libui-linux-basic-code-area.png](/images/glimmer-dsl-libui-linux-basic-code-area.png)
2569
+
2570
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
2571
+
2572
+ ```ruby
2573
+ require 'glimmer-dsl-libui'
2574
+
2575
+ class BasicCodeArea
2576
+ include Glimmer::LibUI::Application
2577
+
2578
+ before_body do
2579
+ @code = <<~CODE
2580
+ # Greets target with greeting
2581
+ def greet(greeting: 'Hello', target: 'World')
2582
+
2583
+ puts "\#{greeting}, \#{target}!"
2584
+ end
2585
+
2586
+ greet
2587
+ greet(target: 'Robert')
2588
+ greet(greeting: 'Aloha')
2589
+ greet(greeting: 'Aloha', target: 'Nancy')
2590
+ greet(greeting: 'Howdy', target: 'Doodle')
2591
+ CODE
2592
+ end
2593
+
2594
+ body {
2595
+ window('Basic Code Area', 400, 300) {
2596
+ margined true
2597
+
2598
+ code_area(language: 'ruby', code: @code)
2599
+ }
2600
+ }
2601
+ end
2602
+
2603
+ BasicCodeArea.launch
2604
+ ```