libui_paradise 0.2.49

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +2072 -0
  3. data/doc/README.gen +1931 -0
  4. data/doc/SNIPPETS.md +94 -0
  5. data/doc/todo/todo.md +16 -0
  6. data/lib/libui_paradise/autoinclude.rb +9 -0
  7. data/lib/libui_paradise/base/base.rb +154 -0
  8. data/lib/libui_paradise/colours/colours.rb +14 -0
  9. data/lib/libui_paradise/domain_specific_language/README.md +5 -0
  10. data/lib/libui_paradise/domain_specific_language/button.yml +7 -0
  11. data/lib/libui_paradise/examples/complex/001_hbox_example.rb +17 -0
  12. data/lib/libui_paradise/examples/complex/002_tabs_example.rb +25 -0
  13. data/lib/libui_paradise/examples/complex/003_open_file_button_example.rb +38 -0
  14. data/lib/libui_paradise/examples/complex/004_font_button.rb +41 -0
  15. data/lib/libui_paradise/examples/complex/005_search_entry_example.rb +18 -0
  16. data/lib/libui_paradise/examples/complex/006_coloured_boxes_example.rb +22 -0
  17. data/lib/libui_paradise/examples/complex/007_slider_example.rb +31 -0
  18. data/lib/libui_paradise/examples/complex/008_radio_button_example.rb +23 -0
  19. data/lib/libui_paradise/examples/complex/009_separator_example.rb +25 -0
  20. data/lib/libui_paradise/examples/complex/010_table_example.rb +55 -0
  21. data/lib/libui_paradise/examples/complex/011_two_buttons_showing_how_to_enable_and_disable_them.rb +32 -0
  22. data/lib/libui_paradise/examples/complex/012_password_entry_example.rb +29 -0
  23. data/lib/libui_paradise/examples/complex/013_form_example.rb +38 -0
  24. data/lib/libui_paradise/examples/complex/014_text_example.rb +18 -0
  25. data/lib/libui_paradise/examples/complex/015_text_view_example.rb +16 -0
  26. data/lib/libui_paradise/examples/complex/016_grid_example.rb +31 -0
  27. data/lib/libui_paradise/examples/complex/017_unicode_text_example.rb +30 -0
  28. data/lib/libui_paradise/examples/complex/018_spinbutton_example.rb +33 -0
  29. data/lib/libui_paradise/examples/complex/019_combo_box_example.rb +50 -0
  30. data/lib/libui_paradise/examples/complex/020_checkbox_example.rb +81 -0
  31. data/lib/libui_paradise/examples/complex/021_font_example.rb +115 -0
  32. data/lib/libui_paradise/examples/complex/022_simple_notepad_example.rb +25 -0
  33. data/lib/libui_paradise/examples/complex/023_msg_box_error.rb +27 -0
  34. data/lib/libui_paradise/examples/complex/024_parse_config_file_example.config +6 -0
  35. data/lib/libui_paradise/examples/complex/024_parse_config_file_example.rb +15 -0
  36. data/lib/libui_paradise/examples/complex/025_colour_button.rb +53 -0
  37. data/lib/libui_paradise/examples/complex/026_basic_table_image.rb +96 -0
  38. data/lib/libui_paradise/examples/complex/027_basic_button_example.rb +19 -0
  39. data/lib/libui_paradise/examples/complex/028_try_for_automatic_button_press_event_after_a_delay.rb +47 -0
  40. data/lib/libui_paradise/examples/complex/029_progressbar_example.rb +116 -0
  41. data/lib/libui_paradise/examples/complex/030_entry_responds_to_comment_as_synonymous_to_the_enter_key_pressed_example.rb +140 -0
  42. data/lib/libui_paradise/examples/complex/031_notification_functionality_example.rb +96 -0
  43. data/lib/libui_paradise/examples/complex/032_simple_window_example.rb +69 -0
  44. data/lib/libui_paradise/examples/complex/033_daemonize_and_exit_after_delay_example.rb +91 -0
  45. data/lib/libui_paradise/examples/simple/001_open_file_example.rb +35 -0
  46. data/lib/libui_paradise/examples/simple/002_histogram_example.rb +213 -0
  47. data/lib/libui_paradise/examples/simple/003_fancy_text_example.rb +220 -0
  48. data/lib/libui_paradise/examples/simple/004_date_time_picker.rb +52 -0
  49. data/lib/libui_paradise/examples/simple/005_text_drawing_example.rb +185 -0
  50. data/lib/libui_paradise/examples/simple/006_midi_player.rb +96 -0
  51. data/lib/libui_paradise/examples/simple/007_control_gallery.rb +191 -0
  52. data/lib/libui_paradise/examples/simple/008_basic_area_example.rb +56 -0
  53. data/lib/libui_paradise/examples/simple/009_spectrum.rb +107 -0
  54. data/lib/libui_paradise/examples/simple/README.md +7 -0
  55. data/lib/libui_paradise/experimental/dsl.rb +17 -0
  56. data/lib/libui_paradise/extensions/counters.rb +58 -0
  57. data/lib/libui_paradise/extensions/extensions.rb +29 -0
  58. data/lib/libui_paradise/extensions/hash_fiddle_pointer_widgets.rb +150 -0
  59. data/lib/libui_paradise/extensions/misc.rb +754 -0
  60. data/lib/libui_paradise/fiddle/pointer.rb +1158 -0
  61. data/lib/libui_paradise/generic_window/README.md +9 -0
  62. data/lib/libui_paradise/generic_window/generic_window.rb +79 -0
  63. data/lib/libui_paradise/images/LIBUI_PARADISE_LOGO.png +0 -0
  64. data/lib/libui_paradise/images/README.md +2 -0
  65. data/lib/libui_paradise/images/form_example.png +0 -0
  66. data/lib/libui_paradise/libui_classes/box.rb +156 -0
  67. data/lib/libui_paradise/libui_classes/grid.rb +55 -0
  68. data/lib/libui_paradise/libui_classes/libui_classes.rb +1611 -0
  69. data/lib/libui_paradise/project/project.rb +28 -0
  70. data/lib/libui_paradise/prototype/README.md +3 -0
  71. data/lib/libui_paradise/prototype/prototype.rb +107 -0
  72. data/lib/libui_paradise/requires/require_the_libui_classes.rb +29 -0
  73. data/lib/libui_paradise/requires/require_the_libui_paradise_project.rb +12 -0
  74. data/lib/libui_paradise/toplevel_methods/misc.rb +13 -0
  75. data/lib/libui_paradise/version/version.rb +17 -0
  76. data/lib/libui_paradise.rb +1 -0
  77. data/libui_paradise.gemspec +49 -0
  78. data/test/testing_generic_window.rb +20 -0
  79. metadata +156 -0
@@ -0,0 +1,213 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # https://github.com/kojix2/LibUI/blob/main/examples/histogram.rb
6
+ #
7
+ # That was initially inspired by:
8
+ #
9
+ # https://github.com/jamescook/libui-ruby/blob/master/example/histogram.rb
10
+ #
11
+ # =========================================================================== #
12
+ require 'libui'
13
+
14
+ UI = LibUI
15
+
16
+ X_OFF_LEFT = 20
17
+ Y_OFF_TOP = 20
18
+ X_OFF_RIGHT = 20
19
+ Y_OFF_BOTTOM = 20
20
+ POINT_RADIUS = 5
21
+
22
+ # =========================================================================== #
23
+ # === BLUE
24
+ # =========================================================================== #
25
+ BLUE = 0x1E90FF
26
+
27
+ init = LibUI.init
28
+ handler = LibUI::FFI::AreaHandler.malloc
29
+ handler.to_ptr.free = Fiddle::RUBY_FREE
30
+ histogram = LibUI.new_area(handler)
31
+ brush = LibUI::FFI::DrawBrush.malloc
32
+ brush.to_ptr.free = Fiddle::RUBY_FREE
33
+ color_button = LibUI.new_color_button
34
+ datapoints = []
35
+
36
+ # =========================================================================== #
37
+ # === graph_size
38
+ # =========================================================================== #
39
+ def graph_size(area_width, area_height)
40
+ graph_width = area_width - X_OFF_LEFT - X_OFF_RIGHT
41
+ graph_height = area_height - Y_OFF_TOP - Y_OFF_BOTTOM
42
+ [graph_width, graph_height]
43
+ end
44
+
45
+ matrix = UI::FFI::DrawMatrix.malloc
46
+ matrix.to_ptr.free = Fiddle::RUBY_FREE
47
+
48
+ def point_locations(datapoints, width, height)
49
+ xincr = width / 9.0 # 10 - 1 to make the last point be at the end
50
+ yincr = height / 100.0
51
+
52
+ data = []
53
+ datapoints.each_with_index do |dp, i|
54
+ val = 100 - UI.spinbox_value(dp)
55
+ data << [xincr * i, yincr * val]
56
+ i += 1
57
+ end
58
+
59
+ data
60
+ end
61
+
62
+ # === construct_graph
63
+ def construct_graph(datapoints, width, height, should_extend)
64
+ locations = point_locations(datapoints, width, height)
65
+ path = LibUI.draw_new_path(0) # winding
66
+ first_location = locations[0] # x and y
67
+ UI.draw_path_new_figure(path, first_location[0], first_location[1])
68
+ locations.each { |loc|
69
+ UI.draw_path_line_to(path, loc[0], loc[1])
70
+ }
71
+
72
+ if should_extend
73
+ LibUI.draw_path_line_to(path, width, height)
74
+ LibUI.draw_path_line_to(path, 0, height)
75
+ LibUI.draw_path_close_figure(path)
76
+ end
77
+
78
+ LibUI.draw_path_end(path)
79
+
80
+ return path
81
+ end
82
+
83
+ handler_draw_event = Fiddle::Closure::BlockCaller.new(
84
+ 0, [1, 1, 1]
85
+ ) do |_area_handler, _area, area_draw_params|
86
+ area_draw_params = UI::FFI::AreaDrawParams.new(area_draw_params)
87
+ path = UI.draw_new_path(0) # winding
88
+ UI.draw_path_add_rectangle(path, 0, 0, area_draw_params.AreaWidth, area_draw_params.AreaHeight)
89
+ UI.draw_path_end(path)
90
+ set_solid_brush(brush, 0xFFFFFF, 1.0) # white
91
+ UI.draw_fill(area_draw_params.Context, path, brush.to_ptr)
92
+ UI.draw_free_path(path)
93
+ dsp = UI::FFI::DrawStrokeParams.malloc
94
+ dsp.to_ptr.free = Fiddle::RUBY_FREE
95
+ dsp.Cap = 0 # flat
96
+ dsp.Join = 0 # miter
97
+ dsp.Thickness = 2
98
+ dsp.MiterLimit = 10 # DEFAULT_MITER_LIMIT
99
+ dashes = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE)
100
+ dsp.Dashes = dashes
101
+ dsp.NumDashes = 0
102
+ dsp.DashPhase = 0
103
+
104
+ # draw axes
105
+ set_solid_brush(brush, 0x000000, 1.0) # black
106
+ graph_width, graph_height = *graph_size(area_draw_params.AreaWidth, area_draw_params.AreaHeight)
107
+
108
+ path = UI.draw_new_path(0) # winding
109
+ UI.draw_path_new_figure(path, X_OFF_LEFT, Y_OFF_TOP)
110
+ UI.draw_path_line_to(path, X_OFF_LEFT, Y_OFF_TOP + graph_height)
111
+ UI.draw_path_line_to(path, X_OFF_LEFT + graph_width, Y_OFF_TOP + graph_height)
112
+ UI.draw_path_end(path)
113
+ UI.draw_stroke(area_draw_params.Context, path, brush, dsp)
114
+ UI.draw_free_path(path)
115
+
116
+ # now transform the coordinate space so (0, 0) is the top-left corner of the graph
117
+ UI.draw_matrix_set_identity(matrix)
118
+ UI.draw_matrix_translate(matrix, X_OFF_LEFT, Y_OFF_TOP)
119
+ UI.draw_transform(area_draw_params.Context, matrix)
120
+
121
+ # now get the color for the graph itself and set up the brush
122
+ # uiColorButtonColor(colorButton, &graphR, &graphG, &graphB, &graphA)
123
+ graph_r = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE) # double
124
+ graph_g = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE) # double
125
+ graph_b = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE) # double
126
+ graph_a = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE) # double
127
+
128
+ UI.color_button_color(color_button, graph_r, graph_g, graph_b, graph_a)
129
+ brush.Type = 0 # solid
130
+ brush.R = graph_r[0, 8].unpack1('d')
131
+ brush.G = graph_g[0, 8].unpack1('d')
132
+ brush.B = graph_b[0, 8].unpack1('d')
133
+
134
+ # now create the fill for the graph below the graph line
135
+ path = construct_graph(datapoints, graph_width, graph_height, true)
136
+ brush.A = graph_a[0, 8].unpack1('d') / 2.0
137
+ UI.draw_fill(area_draw_params.Context, path, brush)
138
+ UI.draw_free_path(path)
139
+
140
+ # now draw the histogram line
141
+ path = construct_graph(datapoints, graph_width, graph_height, false)
142
+ brush.A = graph_a[0, 8].unpack1('d')
143
+ UI.draw_stroke(area_draw_params.Context, path, brush, dsp)
144
+ UI.draw_free_path(path)
145
+ end
146
+
147
+ # Assigning to local variables
148
+ # This is intended to protect Fiddle::Closure from garbage collection.
149
+ # See https://github.com/kojix2/LibUI/issues/8
150
+ do_nothing = Fiddle::Closure::BlockCaller.new(0, [0]) {}
151
+ key_event = Fiddle::Closure::BlockCaller.new(1, [0]) { 0 }
152
+ handler.Draw = handler_draw_event
153
+ handler.MouseEvent = do_nothing
154
+ handler.MouseCrossed = do_nothing
155
+ handler.DragBroken = do_nothing
156
+ handler.KeyEvent = key_event
157
+
158
+ UI.freeInitError(init) unless init.nil?
159
+
160
+ hbox = UI.new_horizontal_box
161
+ UI.box_set_padded(hbox, 1)
162
+
163
+ vbox = UI.new_vertical_box
164
+ UI.box_set_padded(vbox, 1)
165
+ UI.box_append(hbox, vbox, 0)
166
+ UI.box_append(hbox, histogram, 1)
167
+
168
+ datapoints = Array.new(10) do
169
+ UI.new_spinbox(0, 100).tap do |datapoint|
170
+ UI.spinbox_set_value(datapoint, Random.new.rand(90))
171
+ UI.spinbox_on_changed(datapoint) {
172
+ # Redraw the drawing area next:
173
+ UI.area_queue_redraw_all(histogram)
174
+ }
175
+ UI.box_append(vbox, datapoint, 0)
176
+ end
177
+ end
178
+
179
+ # === set_solid_brush
180
+ def set_solid_brush(brush, color, alpha)
181
+ brush.Type = 0 # solid
182
+ brush.R = ((color >> 16) & 0xFF) / 255.0
183
+ brush.G = ((color >> 8) & 0xFF) / 255.0
184
+ brush.B = (color & 0xFF) / 255.0
185
+ brush.A = alpha
186
+ brush
187
+ end
188
+
189
+ set_solid_brush(brush, BLUE, 1.0)
190
+ LibUI.color_button_set_color(color_button, brush.R, brush.G, brush.B, brush.A)
191
+
192
+ LibUI.color_button_on_changed(color_button) {
193
+ LibUI.area_queue_redraw_all(histogram)
194
+ }
195
+
196
+ LibUI.box_append(vbox, color_button, 0)
197
+
198
+ MAIN_WINDOW = LibUI.new_window('Histogram example', 800, 600, 1)
199
+ LibUI.window_set_margined(MAIN_WINDOW, 1)
200
+ LibUI.window_set_child(MAIN_WINDOW, hbox)
201
+
202
+ should_quit = proc { |_ptr|
203
+ LibUI.control_destroy(MAIN_WINDOW)
204
+ LibUI.quit
205
+ 0
206
+ }
207
+
208
+ LibUI.window_on_closing(MAIN_WINDOW, should_quit)
209
+ LibUI.on_should_quit(should_quit)
210
+ LibUI.control_show(MAIN_WINDOW)
211
+
212
+ LibUI.main
213
+ LibUI.quit
@@ -0,0 +1,220 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require '003_fancy_text_example.rb'
6
+ # =========================================================================== #
7
+ require 'libui'
8
+
9
+ UI = LibUI
10
+ UI.init
11
+
12
+ # =========================================================================== #
13
+ # === WIDTH
14
+ # =========================================================================== #
15
+ WIDTH = 1000
16
+
17
+ # =========================================================================== #
18
+ # === USE_THIS_FONT
19
+ # =========================================================================== #
20
+ USE_THIS_FONT = 'Hack' # Or 'Courier New'.
21
+
22
+ # =========================================================================== #
23
+ # === append_with_attribute
24
+ # =========================================================================== #
25
+ def append_with_attribute(attr_str, what, attr1, attr2)
26
+ start_pos = LibUI.attributed_string_len(attr_str)
27
+ end_pos = start_pos + what.length
28
+ LibUI.attributed_string_append_unattributed(attr_str, what)
29
+ LibUI.attributed_string_set_attribute(attr_str, attr1, start_pos, end_pos)
30
+ LibUI.attributed_string_set_attribute(attr_str, attr2, start_pos, end_pos) if attr2
31
+ end
32
+
33
+ # =========================================================================== #
34
+ # === make_attribute_string
35
+ #
36
+ # Create a new attributed string.
37
+ # =========================================================================== #
38
+ def make_attribute_string
39
+ attr_str = LibUI.new_attributed_string(
40
+ "Drawing strings with libui is done with the "\
41
+ "uiAttributedString and uiDrawTextLayout objects.\n" \
42
+ 'uiAttributedString lets you have a variety of attributes: '
43
+ )
44
+
45
+ attr1 = LibUI.new_family_attribute(USE_THIS_FONT)
46
+ append_with_attribute(attr_str, 'font family', attr1, nil)
47
+ LibUI.attributed_string_append_unattributed(attr_str, ', ')
48
+
49
+ # ========================================================================= #
50
+ # === Use a bigger font next
51
+ # ========================================================================= #
52
+ bigger_font = UI.new_size_attribute(45)
53
+ append_with_attribute(attr_str, 'font size', bigger_font, nil)
54
+ UI.attributed_string_append_unattributed(attr_str, ', ')
55
+
56
+ # ========================================================================= #
57
+ # === Make bold text next (bold tag)
58
+ # ========================================================================= #
59
+ attr1 = LibUI.new_weight_attribute(LibUI::TextWeightBold)
60
+ append_with_attribute(attr_str, 'font weight', attr1, nil)
61
+ LibUI.attributed_string_append_unattributed(attr_str, ', ')
62
+
63
+ attr1 = LibUI.new_italic_attribute(UI::TextItalicItalic)
64
+ append_with_attribute(attr_str, 'font italicness', attr1, nil)
65
+ UI.attributed_string_append_unattributed(attr_str, ', ')
66
+
67
+ attr1 = LibUI.new_stretch_attribute(UI::TextStretchCondensed)
68
+ append_with_attribute(attr_str, 'font stretch', attr1, nil)
69
+ UI.attributed_string_append_unattributed(attr_str, ', ')
70
+
71
+ # ========================================================================= #
72
+ # Influence the text-colour via RGB values.
73
+ # ========================================================================= #
74
+ attr1 = LibUI.new_color_attribute(0.75, 0.25, 0.5, 0.75)
75
+ append_with_attribute(attr_str, 'text color', attr1, nil)
76
+ UI.attributed_string_append_unattributed(attr_str, ', ')
77
+
78
+ attr1 = LibUI.new_background_attribute(0.5, 0.5, 0.25, 0.5)
79
+ append_with_attribute(attr_str, 'text background color', attr1, nil)
80
+ UI.attributed_string_append_unattributed(attr_str, ', ')
81
+
82
+ attr1 = UI.new_underline_attribute(UI::UnderlineSingle)
83
+ append_with_attribute(attr_str, 'underline style', attr1, nil)
84
+ UI.attributed_string_append_unattributed(attr_str, ', ')
85
+
86
+ UI.attributed_string_append_unattributed(attr_str, 'and ')
87
+ attr1 = LibUI.new_underline_attribute(UI::UnderlineDouble)
88
+ attr2 = LibUI.new_underline_color_attribute(UI::UnderlineColorCustom, 1.0, 0.0, 0.5, 1.0)
89
+ append_with_attribute(attr_str, 'underline color', attr1, attr2)
90
+ UI.attributed_string_append_unattributed(attr_str, '. ')
91
+
92
+ UI.attributed_string_append_unattributed(attr_str, 'Furthermore, there are attributes allowing for ')
93
+ attr1 = LibUI.new_underline_attribute(UI::UnderlineSuggestion)
94
+ attr2 = LibUI.new_underline_color_attribute(UI::UnderlineColorSpelling, 0, 0, 0, 0)
95
+ append_with_attribute(attr_str, 'special underlines for indicating spelling errors', attr1, attr2)
96
+ UI.attributed_string_append_unattributed(attr_str, ' (and other types of errors) ')
97
+
98
+ UI.attributed_string_append_unattributed(attr_str,
99
+ 'and control over OpenType features such as ligatures (for instance, ')
100
+ otf = UI.new_open_type_features
101
+ UI.open_type_features_add(otf, 'l', 'i', 'g', 'a', 0)
102
+ attr1 = UI.new_features_attribute(otf)
103
+ append_with_attribute(attr_str, 'afford', attr1, nil)
104
+ UI.attributed_string_append_unattributed(attr_str, ' vs. ')
105
+ UI.open_type_features_add(otf, 'l', 'i', 'g', 'a', 1)
106
+ attr1 = UI.new_features_attribute(otf)
107
+ append_with_attribute(attr_str, 'afford', attr1, nil)
108
+ UI.free_open_type_features(otf)
109
+ UI.attributed_string_append_unattributed(attr_str, ").\n")
110
+
111
+ UI.attributed_string_append_unattributed(attr_str,
112
+ 'Use the controls '\
113
+ 'opposite to the text '\
114
+ 'to control properties of the text.')
115
+ attr_str
116
+ end
117
+
118
+ # =========================================================================== #
119
+ # === on_font_changed
120
+ # =========================================================================== #
121
+ def on_font_changed(area)
122
+ UI.area_queue_redraw_all(area)
123
+ end
124
+
125
+ # =========================================================================== #
126
+ # === on_combobox_selected
127
+ # =========================================================================== #
128
+ def on_combobox_selected(area)
129
+ UI.area_queue_redraw_all(area)
130
+ end
131
+
132
+ # === draw_event
133
+ def draw_event(adp, attr_str, font_button, alignment)
134
+ area_draw_params = UI::FFI::AreaDrawParams.new(adp)
135
+ default_font = UI::FFI::FontDescriptor.malloc
136
+ default_font.to_ptr.free = Fiddle::RUBY_FREE
137
+ default_font = UI::FFI::FontDescriptor.malloc
138
+ default_font.to_ptr.free = Fiddle::RUBY_FREE
139
+ params = UI::FFI::DrawTextLayoutParams.malloc
140
+ params.to_ptr.free = Fiddle::RUBY_FREE
141
+
142
+ params.String = attr_str
143
+ UI.font_button_font(font_button, default_font)
144
+ params.DefaultFont = default_font
145
+ params.Width = area_draw_params.AreaWidth
146
+ params.Align = UI.combobox_selected(alignment)
147
+ text_layout = UI.draw_new_text_layout(params)
148
+ UI.draw_text(area_draw_params.Context, text_layout, 0, 0)
149
+ UI.draw_free_text_layout(text_layout)
150
+ UI.free_font_button_font(default_font)
151
+ end
152
+
153
+ UI.init
154
+
155
+ handler = UI::FFI::AreaHandler.malloc
156
+ handler.to_ptr.free = Fiddle::RUBY_FREE
157
+ handler_draw_event = Fiddle::Closure::BlockCaller.new(0, [1, 1, 1]) do |_, _area, adp|
158
+ draw_event(adp, @attr_str, @font_button, @alignment)
159
+ end
160
+
161
+ handler.Draw = handler_draw_event
162
+
163
+ do_nothing = Fiddle::Closure::BlockCaller.new(0, [0]) {}
164
+ key_event = Fiddle::Closure::BlockCaller.new(1, [0]) { 0 }
165
+ handler.MouseEvent = do_nothing
166
+ handler.MouseCrossed = do_nothing
167
+ handler.DragBroken = do_nothing
168
+ handler.KeyEvent = key_event
169
+
170
+ LibUI.on_should_quit {
171
+ LibUI.control_destroy(main_window)
172
+ }
173
+
174
+ # =========================================================================== #
175
+ # Assign to the main attributed string next.
176
+ # =========================================================================== #
177
+ @attr_str = make_attribute_string
178
+
179
+ main_window = LibUI.new_window('Text-Drawing Example', WIDTH, 480, 1)
180
+ LibUI.window_set_margined(main_window, 1)
181
+ LibUI.window_on_closing(main_window) {
182
+ LibUI.control_destroy(main_window)
183
+ LibUI.quit
184
+ 0
185
+ }
186
+
187
+ hbox = LibUI.new_horizontal_box
188
+ LibUI.box_set_padded(hbox, 1)
189
+ LibUI.window_set_child(main_window, hbox)
190
+
191
+ vbox = LibUI.new_vertical_box
192
+ LibUI.box_set_padded(vbox, 1)
193
+ LibUI.box_append(hbox, vbox, 0)
194
+
195
+ @font_button = LibUI.new_font_button
196
+ LibUI.font_button_on_changed(@font_button) { on_font_changed(@area) }
197
+ LibUI.box_append(vbox, @font_button, 0)
198
+
199
+ form = LibUI.new_form
200
+ LibUI.form_set_padded(form, 1)
201
+ LibUI.box_append(vbox, form, 0)
202
+
203
+ # Add a combobox entry next.
204
+ # @alignment = ui_combobox(%w( Left Center Right ))
205
+ @alignment = LibUI.new_combobox
206
+ LibUI.combobox_append(@alignment, 'Left')
207
+ LibUI.combobox_append(@alignment, 'Center')
208
+ LibUI.combobox_append(@alignment, 'Right')
209
+ LibUI.combobox_set_selected(@alignment, 0)
210
+ LibUI.combobox_on_selected(@alignment) { on_combobox_selected(@area) }
211
+ LibUI.form_append(form, 'Alignment', @alignment, 0)
212
+
213
+ @area = LibUI.new_area(handler)
214
+ LibUI.box_append(hbox, @area, 1)
215
+
216
+ LibUI.control_show(main_window)
217
+ LibUI.main
218
+
219
+ LibUI.free_attributed_string(@attr_str)
220
+ LibUI.uninit
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'libui_paradise/examples/simple/004_date_time_picker.rb'
6
+ # =========================================================================== #
7
+ require 'libui'
8
+
9
+ alias e puts
10
+
11
+ # =========================================================================== #
12
+ # === TITLE
13
+ # =========================================================================== #
14
+ TITLE = 'Date Time Pickers'
15
+
16
+ LibUI.init
17
+
18
+ vbox = LibUI.new_vertical_box
19
+
20
+ date_time_picker = LibUI.new_date_time_picker
21
+
22
+ time = LibUI::FFI::TM.malloc
23
+
24
+ LibUI.date_time_picker_on_changed(date_time_picker) {
25
+ LibUI.date_time_picker_time(date_time_picker, time)
26
+ p sec: time.tm_sec,
27
+ min: time.tm_min,
28
+ hour: time.tm_hour,
29
+ mday: time.tm_mday,
30
+ mon: time.tm_mon,
31
+ year: time.tm_year,
32
+ wday: time.tm_wday,
33
+ yday: time.tm_yday,
34
+ isdst: time.tm_isdst
35
+ }
36
+
37
+ LibUI.box_set_padded(vbox, 1)
38
+ LibUI.box_append(vbox, date_time_picker, 1)
39
+
40
+ main_window = LibUI.new_window(TITLE, 300, 200, 1)
41
+ LibUI.window_set_margined(main_window, 1)
42
+
43
+ LibUI.window_on_closing(main_window) {
44
+ LibUI.control_destroy(main_window)
45
+ LibUI.quit
46
+ 0
47
+ }
48
+
49
+ LibUI.window_set_child(main_window, vbox)
50
+ LibUI.control_show(main_window)
51
+ LibUI.main
52
+ LibUI.quit
@@ -0,0 +1,185 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # 005_text_drawing_example.rb
6
+ # =========================================================================== #
7
+ require 'libui'
8
+
9
+ UI = LibUI
10
+
11
+ # =========================================================================== #
12
+ # === append_with_attribute
13
+ # =========================================================================== #
14
+ def append_with_attribute(attr_str, what, attr1, attr2)
15
+ start_pos = LibUI.attributed_string_len(attr_str)
16
+ end_pos = start_pos + what.length
17
+ LibUI.attributed_string_append_unattributed(attr_str, what)
18
+ LibUI.attributed_string_set_attribute(attr_str, attr1, start_pos, end_pos)
19
+ LibUI.attributed_string_set_attribute(attr_str, attr2, start_pos, end_pos) if attr2
20
+ end
21
+
22
+ # =========================================================================== #
23
+ # === make_attribute_string
24
+ # =========================================================================== #
25
+ def make_attribute_string
26
+ attr_str = LibUI.new_attributed_string(
27
+ "Drawing strings with libui is done with the uiAttributedString and uiDrawTextLayout objects.\n" \
28
+ 'uiAttributedString lets you have a variety of attributes: '
29
+ )
30
+
31
+ attr1 = LibUI.new_family_attribute('Courier New')
32
+ append_with_attribute(attr_str, 'font family', attr1, nil)
33
+ UI.attributed_string_append_unattributed(attr_str, ', ')
34
+
35
+ attr1 = LibUI.new_size_attribute(18)
36
+ append_with_attribute(attr_str, 'font size', attr1, nil)
37
+ UI.attributed_string_append_unattributed(attr_str, ', ')
38
+
39
+ attr1 = UI.new_weight_attribute(UI::TextWeightBold)
40
+ append_with_attribute(attr_str, 'font weight', attr1, nil)
41
+ UI.attributed_string_append_unattributed(attr_str, ', ')
42
+
43
+ attr1 = UI.new_italic_attribute(UI::TextItalicItalic)
44
+ append_with_attribute(attr_str, 'font italicness', attr1, nil)
45
+ UI.attributed_string_append_unattributed(attr_str, ', ')
46
+
47
+ attr1 = UI.new_stretch_attribute(UI::TextStretchCondensed)
48
+ append_with_attribute(attr_str, 'font stretch', attr1, nil)
49
+ UI.attributed_string_append_unattributed(attr_str, ', ')
50
+
51
+ attr1 = UI.new_color_attribute(0.75, 0.25, 0.5, 0.75)
52
+ append_with_attribute(attr_str, 'text color', attr1, nil)
53
+ UI.attributed_string_append_unattributed(attr_str, ', ')
54
+
55
+ attr1 = UI.new_background_attribute(0.5, 0.5, 0.25, 0.5)
56
+ append_with_attribute(attr_str, 'text background color', attr1, nil)
57
+ UI.attributed_string_append_unattributed(attr_str, ', ')
58
+
59
+ attr1 = UI.new_underline_attribute(UI::UnderlineSingle)
60
+ append_with_attribute(attr_str, 'underline style', attr1, nil)
61
+ UI.attributed_string_append_unattributed(attr_str, ', ')
62
+
63
+ UI.attributed_string_append_unattributed(attr_str, 'and ')
64
+ attr1 = UI.new_underline_attribute(UI::UnderlineDouble)
65
+ attr2 = UI.new_underline_color_attribute(UI::UnderlineColorCustom, 1.0, 0.0, 0.5, 1.0)
66
+ append_with_attribute(attr_str, 'underline color', attr1, attr2)
67
+ UI.attributed_string_append_unattributed(attr_str, '. ')
68
+
69
+ UI.attributed_string_append_unattributed(attr_str, 'Furthermore, there are attributes allowing for ')
70
+ attr1 = UI.new_underline_attribute(UI::UnderlineSuggestion)
71
+ attr2 = UI.new_underline_color_attribute(UI::UnderlineColorSpelling, 0, 0, 0, 0)
72
+ append_with_attribute(attr_str, 'special underlines for indicating spelling errors', attr1, attr2)
73
+ UI.attributed_string_append_unattributed(attr_str, ' (and other types of errors) ')
74
+
75
+ UI.attributed_string_append_unattributed(attr_str,
76
+ 'and control over OpenType features such as ligatures (for instance, ')
77
+ otf = LibUI.new_open_type_features
78
+ LibUI.open_type_features_add(otf, 'l', 'i', 'g', 'a', 0)
79
+ attr1 = UI.new_features_attribute(otf)
80
+ append_with_attribute(attr_str, 'afford', attr1, nil)
81
+ LibUI.attributed_string_append_unattributed(attr_str, ' vs. ')
82
+ LibUI.open_type_features_add(otf, 'l', 'i', 'g', 'a', 1)
83
+ attr1 = UI.new_features_attribute(otf)
84
+ append_with_attribute(attr_str, 'afford', attr1, nil)
85
+ LibUI.free_open_type_features(otf)
86
+ LibUI.attributed_string_append_unattributed(attr_str, ").\n")
87
+
88
+ LibUI.attributed_string_append_unattributed(attr_str,
89
+ "\nUse the controls opposite to the text to control "\
90
+ "properties of the text.")
91
+ attr_str
92
+ end
93
+
94
+ def on_font_changed(area)
95
+ LibUI.area_queue_redraw_all(area)
96
+ end
97
+
98
+ def on_combobox_selected(area)
99
+ LibUI.area_queue_redraw_all(area)
100
+ end
101
+
102
+ def draw_event(adp, attr_str, font_button, alignment)
103
+ area_draw_params = UI::FFI::AreaDrawParams.new(adp)
104
+ default_font = UI::FFI::FontDescriptor.malloc
105
+ default_font.to_ptr.free = Fiddle::RUBY_FREE
106
+ default_font = UI::FFI::FontDescriptor.malloc
107
+ default_font.to_ptr.free = Fiddle::RUBY_FREE
108
+ params = UI::FFI::DrawTextLayoutParams.malloc
109
+ params.to_ptr.free = Fiddle::RUBY_FREE
110
+
111
+ params.String = attr_str
112
+ UI.font_button_font(font_button, default_font)
113
+ params.DefaultFont = default_font
114
+ params.Width = area_draw_params.AreaWidth
115
+ params.Align = UI.combobox_selected(alignment)
116
+ text_layout = UI.draw_new_text_layout(params)
117
+ UI.draw_text(area_draw_params.Context, text_layout, 0, 0)
118
+ UI.draw_free_text_layout(text_layout)
119
+ UI.free_font_button_font(default_font)
120
+ end
121
+
122
+ LibUI.init
123
+
124
+ handler = UI::FFI::AreaHandler.malloc
125
+ handler.to_ptr.free = Fiddle::RUBY_FREE
126
+
127
+ handler_draw_event = Fiddle::Closure::BlockCaller.new(0, [1, 1, 1]) do |_, _area, adp|
128
+ draw_event(adp, @attr_str, @font_button, @alignment)
129
+ end
130
+
131
+ handler.Draw = handler_draw_event
132
+
133
+ do_nothing = Fiddle::Closure::BlockCaller.new(0, [0]) {}
134
+ key_event = Fiddle::Closure::BlockCaller.new(1, [0]) { 0 }
135
+ handler.MouseEvent = do_nothing
136
+ handler.MouseCrossed = do_nothing
137
+ handler.DragBroken = do_nothing
138
+ handler.KeyEvent = key_event
139
+
140
+ UI.on_should_quit do
141
+ UI.control_destroy(main_window)
142
+ end
143
+
144
+ @attr_str = make_attribute_string
145
+
146
+ main_window = UI.new_window('Text-Drawing Example', 640, 480, 1)
147
+ UI.window_set_margined(main_window, 1)
148
+ UI.window_on_closing(main_window) do
149
+ UI.control_destroy(main_window)
150
+ UI.quit
151
+ 0
152
+ end
153
+
154
+ hbox = UI.new_horizontal_box
155
+ UI.box_set_padded(hbox, 1)
156
+ UI.window_set_child(main_window, hbox)
157
+
158
+ vbox = UI.new_vertical_box
159
+ UI.box_set_padded(vbox, 1)
160
+ UI.box_append(hbox, vbox, 0)
161
+
162
+ @font_button = UI.new_font_button
163
+ UI.font_button_on_changed(@font_button) { on_font_changed(@area) }
164
+ UI.box_append(vbox, @font_button, 0)
165
+
166
+ form = UI.new_form
167
+ UI.form_set_padded(form, 1)
168
+ UI.box_append(vbox, form, 0)
169
+
170
+ @alignment = UI.new_combobox
171
+ UI.combobox_append(@alignment, 'Left')
172
+ UI.combobox_append(@alignment, 'Center')
173
+ UI.combobox_append(@alignment, 'Right')
174
+ UI.combobox_set_selected(@alignment, 0)
175
+ UI.combobox_on_selected(@alignment) { on_combobox_selected(@area) }
176
+ UI.form_append(form, 'Alignment', @alignment, 0)
177
+
178
+ @area = LibUI.new_area(handler)
179
+ LibUI.box_append(hbox, @area, 1)
180
+
181
+ LibUI.control_show(main_window)
182
+ LibUI.main
183
+
184
+ LibUI.free_attributed_string(@attr_str)
185
+ LibUI.uninit