libui_paradise 0.2.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of libui_paradise might be problematic. Click here for more details.

Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +2064 -0
  3. data/doc/README.gen +1923 -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 +207 -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 +1157 -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 +1610 -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 +19 -0
  79. metadata +156 -0
@@ -0,0 +1,207 @@
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
+ def graph_size(area_width, area_height)
37
+ graph_width = area_width - X_OFF_LEFT - X_OFF_RIGHT
38
+ graph_height = area_height - Y_OFF_TOP - Y_OFF_BOTTOM
39
+ [graph_width, graph_height]
40
+ end
41
+
42
+ matrix = UI::FFI::DrawMatrix.malloc
43
+ matrix.to_ptr.free = Fiddle::RUBY_FREE
44
+
45
+ def point_locations(datapoints, width, height)
46
+ xincr = width / 9.0 # 10 - 1 to make the last point be at the end
47
+ yincr = height / 100.0
48
+
49
+ data = []
50
+ datapoints.each_with_index do |dp, i|
51
+ val = 100 - UI.spinbox_value(dp)
52
+ data << [xincr * i, yincr * val]
53
+ i += 1
54
+ end
55
+
56
+ data
57
+ end
58
+
59
+ def construct_graph(datapoints, width, height, should_extend)
60
+ locations = point_locations(datapoints, width, height)
61
+ path = UI.draw_new_path(0) # winding
62
+ first_location = locations[0] # x and y
63
+ UI.draw_path_new_figure(path, first_location[0], first_location[1])
64
+ locations.each do |loc|
65
+ UI.draw_path_line_to(path, loc[0], loc[1])
66
+ end
67
+
68
+ if should_extend
69
+ UI.draw_path_line_to(path, width, height)
70
+ UI.draw_path_line_to(path, 0, height)
71
+ UI.draw_path_close_figure(path)
72
+ end
73
+
74
+ UI.draw_path_end(path)
75
+
76
+ path
77
+ end
78
+
79
+ handler_draw_event = Fiddle::Closure::BlockCaller.new(
80
+ 0, [1, 1, 1]
81
+ ) do |_area_handler, _area, area_draw_params|
82
+ area_draw_params = UI::FFI::AreaDrawParams.new(area_draw_params)
83
+ path = UI.draw_new_path(0) # winding
84
+ UI.draw_path_add_rectangle(path, 0, 0, area_draw_params.AreaWidth, area_draw_params.AreaHeight)
85
+ UI.draw_path_end(path)
86
+ set_solid_brush(brush, 0xFFFFFF, 1.0) # white
87
+ UI.draw_fill(area_draw_params.Context, path, brush.to_ptr)
88
+ UI.draw_free_path(path)
89
+ dsp = UI::FFI::DrawStrokeParams.malloc
90
+ dsp.to_ptr.free = Fiddle::RUBY_FREE
91
+ dsp.Cap = 0 # flat
92
+ dsp.Join = 0 # miter
93
+ dsp.Thickness = 2
94
+ dsp.MiterLimit = 10 # DEFAULT_MITER_LIMIT
95
+ dashes = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE)
96
+ dsp.Dashes = dashes
97
+ dsp.NumDashes = 0
98
+ dsp.DashPhase = 0
99
+
100
+ # draw axes
101
+ set_solid_brush(brush, 0x000000, 1.0) # black
102
+ graph_width, graph_height = *graph_size(area_draw_params.AreaWidth, area_draw_params.AreaHeight)
103
+
104
+ path = UI.draw_new_path(0) # winding
105
+ UI.draw_path_new_figure(path, X_OFF_LEFT, Y_OFF_TOP)
106
+ UI.draw_path_line_to(path, X_OFF_LEFT, Y_OFF_TOP + graph_height)
107
+ UI.draw_path_line_to(path, X_OFF_LEFT + graph_width, Y_OFF_TOP + graph_height)
108
+ UI.draw_path_end(path)
109
+ UI.draw_stroke(area_draw_params.Context, path, brush, dsp)
110
+ UI.draw_free_path(path)
111
+
112
+ # now transform the coordinate space so (0, 0) is the top-left corner of the graph
113
+ UI.draw_matrix_set_identity(matrix)
114
+ UI.draw_matrix_translate(matrix, X_OFF_LEFT, Y_OFF_TOP)
115
+ UI.draw_transform(area_draw_params.Context, matrix)
116
+
117
+ # now get the color for the graph itself and set up the brush
118
+ # uiColorButtonColor(colorButton, &graphR, &graphG, &graphB, &graphA)
119
+ graph_r = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE) # double
120
+ graph_g = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE) # double
121
+ graph_b = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE) # double
122
+ graph_a = Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE) # double
123
+
124
+ UI.color_button_color(color_button, graph_r, graph_g, graph_b, graph_a)
125
+ brush.Type = 0 # solid
126
+ brush.R = graph_r[0, 8].unpack1('d')
127
+ brush.G = graph_g[0, 8].unpack1('d')
128
+ brush.B = graph_b[0, 8].unpack1('d')
129
+
130
+ # now create the fill for the graph below the graph line
131
+ path = construct_graph(datapoints, graph_width, graph_height, true)
132
+ brush.A = graph_a[0, 8].unpack1('d') / 2.0
133
+ UI.draw_fill(area_draw_params.Context, path, brush)
134
+ UI.draw_free_path(path)
135
+
136
+ # now draw the histogram line
137
+ path = construct_graph(datapoints, graph_width, graph_height, false)
138
+ brush.A = graph_a[0, 8].unpack1('d')
139
+ UI.draw_stroke(area_draw_params.Context, path, brush, dsp)
140
+ UI.draw_free_path(path)
141
+ end
142
+
143
+ # Assigning to local variables
144
+ # This is intended to protect Fiddle::Closure from garbage collection.
145
+ # See https://github.com/kojix2/LibUI/issues/8
146
+ do_nothing = Fiddle::Closure::BlockCaller.new(0, [0]) {}
147
+ key_event = Fiddle::Closure::BlockCaller.new(1, [0]) { 0 }
148
+ handler.Draw = handler_draw_event
149
+ handler.MouseEvent = do_nothing
150
+ handler.MouseCrossed = do_nothing
151
+ handler.DragBroken = do_nothing
152
+ handler.KeyEvent = key_event
153
+
154
+ UI.freeInitError(init) unless init.nil?
155
+
156
+ hbox = UI.new_horizontal_box
157
+ UI.box_set_padded(hbox, 1)
158
+
159
+ vbox = UI.new_vertical_box
160
+ UI.box_set_padded(vbox, 1)
161
+ UI.box_append(hbox, vbox, 0)
162
+ UI.box_append(hbox, histogram, 1)
163
+
164
+ datapoints = Array.new(10) do
165
+ UI.new_spinbox(0, 100).tap do |datapoint|
166
+ UI.spinbox_set_value(datapoint, Random.new.rand(90))
167
+ UI.spinbox_on_changed(datapoint) do
168
+ UI.area_queue_redraw_all(histogram)
169
+ end
170
+ UI.box_append(vbox, datapoint, 0)
171
+ end
172
+ end
173
+
174
+ def set_solid_brush(brush, color, alpha)
175
+ brush.Type = 0 # solid
176
+ brush.R = ((color >> 16) & 0xFF) / 255.0
177
+ brush.G = ((color >> 8) & 0xFF) / 255.0
178
+ brush.B = (color & 0xFF) / 255.0
179
+ brush.A = alpha
180
+ brush
181
+ end
182
+
183
+ set_solid_brush(brush, BLUE, 1.0)
184
+ LibUI.color_button_set_color(color_button, brush.R, brush.G, brush.B, brush.A)
185
+
186
+ LibUI.color_button_on_changed(color_button) {
187
+ LibUI.area_queue_redraw_all(histogram)
188
+ }
189
+
190
+ LibUI.box_append(vbox, color_button, 0)
191
+
192
+ MAIN_WINDOW = LibUI.new_window('Histogram example', 800, 600, 1)
193
+ LibUI.window_set_margined(MAIN_WINDOW, 1)
194
+ LibUI.window_set_child(MAIN_WINDOW, hbox)
195
+
196
+ should_quit = proc { |_ptr|
197
+ LibUI.control_destroy(MAIN_WINDOW)
198
+ LibUI.quit
199
+ 0
200
+ }
201
+
202
+ LibUI.window_on_closing(MAIN_WINDOW, should_quit)
203
+ LibUI.on_should_quit(should_quit)
204
+ LibUI.control_show(MAIN_WINDOW)
205
+
206
+ LibUI.main
207
+ 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