iup-ffi 0.12.0

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.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/LICENCE.txt +21 -0
  3. data/README.rdoc +348 -0
  4. data/lib/iup-ffi-plain.rb +9 -0
  5. data/lib/iup-ffi.rb +76 -0
  6. data/lib/plain/iupcdlib.rb +107 -0
  7. data/lib/plain/iupcontrolslib.rb +24 -0
  8. data/lib/plain/iupimglib.rb +14 -0
  9. data/lib/plain/iupimlib.rb +16 -0
  10. data/lib/plain/iuplib.rb +209 -0
  11. data/lib/plain/scintilla-lib.rb +15 -0
  12. data/lib/wrapped/attribute-builders.rb +105 -0
  13. data/lib/wrapped/attribute-reference.rb +27 -0
  14. data/lib/wrapped/background-box.rb +33 -0
  15. data/lib/wrapped/button.rb +108 -0
  16. data/lib/wrapped/callback-setter.rb +78 -0
  17. data/lib/wrapped/canvas.rb +467 -0
  18. data/lib/wrapped/colourbar.rb +94 -0
  19. data/lib/wrapped/colourdialog.rb +63 -0
  20. data/lib/wrapped/common-attributes.rb +64 -0
  21. data/lib/wrapped/constants.rb +953 -0
  22. data/lib/wrapped/dial.rb +87 -0
  23. data/lib/wrapped/dialog.rb +176 -0
  24. data/lib/wrapped/dialogs.rb +106 -0
  25. data/lib/wrapped/drag-drop-attributes.rb +57 -0
  26. data/lib/wrapped/dynamic-fill-methods.rb +27 -0
  27. data/lib/wrapped/expander.rb +65 -0
  28. data/lib/wrapped/filedialog.rb +93 -0
  29. data/lib/wrapped/fill.rb +26 -0
  30. data/lib/wrapped/fontdialog.rb +42 -0
  31. data/lib/wrapped/frame.rb +47 -0
  32. data/lib/wrapped/gridbox.rb +94 -0
  33. data/lib/wrapped/hbox.rb +49 -0
  34. data/lib/wrapped/image-attributes.rb +27 -0
  35. data/lib/wrapped/image.rb +118 -0
  36. data/lib/wrapped/internal-drag-drop-attributes.rb +21 -0
  37. data/lib/wrapped/iup-global.rb +51 -0
  38. data/lib/wrapped/label.rb +98 -0
  39. data/lib/wrapped/link.rb +59 -0
  40. data/lib/wrapped/list.rb +353 -0
  41. data/lib/wrapped/matrix.rb +233 -0
  42. data/lib/wrapped/menu.rb +50 -0
  43. data/lib/wrapped/menuitem.rb +80 -0
  44. data/lib/wrapped/messagedialog.rb +51 -0
  45. data/lib/wrapped/progressbar.rb +48 -0
  46. data/lib/wrapped/progressdialog.rb +111 -0
  47. data/lib/wrapped/radio.rb +43 -0
  48. data/lib/wrapped/scintilla.rb +277 -0
  49. data/lib/wrapped/scrollbar-attributes.rb +141 -0
  50. data/lib/wrapped/scrollbox.rb +147 -0
  51. data/lib/wrapped/separator.rb +11 -0
  52. data/lib/wrapped/splitbox.rb +48 -0
  53. data/lib/wrapped/stretchbox.rb +42 -0
  54. data/lib/wrapped/submenu.rb +34 -0
  55. data/lib/wrapped/tabs.rb +149 -0
  56. data/lib/wrapped/text.rb +225 -0
  57. data/lib/wrapped/timer.rb +42 -0
  58. data/lib/wrapped/toggle.rb +98 -0
  59. data/lib/wrapped/tree.rb +465 -0
  60. data/lib/wrapped/val.rb +97 -0
  61. data/lib/wrapped/vbox.rb +51 -0
  62. data/lib/wrapped/widget.rb +137 -0
  63. data/lib/wrapped/zbox.rb +54 -0
  64. metadata +124 -0
@@ -0,0 +1,47 @@
1
+ module Iup
2
+
3
+ # A frame contains a child widget and displays it with a border.
4
+ # Optionally, the frame can have a text title.
5
+ #
6
+ # == Attributes
7
+ #
8
+ # clientoffset:: read-only, returns current offset of frame in its client
9
+ # as "widthxheight".
10
+ # clientsize:: read-only, returns current size of frame as "widthxheight".
11
+ # expand:: Allows frame to fill available space in indicated direction.
12
+ # Values 'no' / 'horizontal' / 'vertical' / 'yes'.
13
+ # position:: <b>read-only</b> returns position in pixels within client window
14
+ # as "x,y".
15
+ # rastersize:: Size of the frame, in pixels, value as "widthxheight".
16
+ # screenposition:: <b>read-only</b> returns position in pixels on screen
17
+ # as "x,y".
18
+ # sunken:: For frame with no title, gives a sunken appearance if set:
19
+ # values as 'yes' / 'no'.
20
+ # title:: Text displayed as frame title.
21
+ #
22
+ class Frame < Widget
23
+
24
+ # Creates an instance of the frame.
25
+ # widget:: the child widget to contain
26
+ # block:: optional block to set up the box's attributes.
27
+ def initialize widget, &block
28
+ @handle = IupLib.IupFrame widget.handle
29
+
30
+ # run any provided block on instance, to set up further attributes
31
+ self.instance_eval &block if block_given?
32
+ end
33
+
34
+ # -- attributes
35
+
36
+ define_readonly :clientoffset
37
+ define_readonly :clientsize
38
+ define_attribute :expand
39
+ define_readonly :position
40
+ define_attribute :rastersize
41
+ define_readonly :screenposition
42
+ define_attribute :sunken
43
+ define_attribute :title
44
+
45
+ end
46
+ end
47
+
@@ -0,0 +1,94 @@
1
+ module Iup
2
+
3
+ # A container which arranges its widgets in a left-to-right, top-to-bottom order.
4
+ #
5
+ # == Attributes
6
+ #
7
+ # alignmentcol:: gets/sets horizontal alignment of columns.
8
+ # * alignmentcol() -> reads value of 'ALIGNMENTCOL'
9
+ # * alignmentcol(val) -> sets 'ALIGNMENTCOL' to +val+, which is 'ALEFT' / 'ACENTER' / 'ARIGHT'
10
+ # * alignmentcol(n, val) -> sets alignment of column +n+ to +val+.
11
+ # alignmentlin:: vertical alignment within line, as 'atop' / 'abottom' / 'acenter'
12
+ # cgapcol:: n, horizontal space in characters between columns.
13
+ # cgaplin:: n, vertical space in characters between lines.
14
+ # clientoffset:: <b>read-only</b>, returns current offset of box in its client
15
+ # as "widthxheight".
16
+ # clientsize:: <b>read-only</b>, returns current size of box as "widthxheight".
17
+ # cmargin:: Margin in x and y directions in characters, value as "mxn".
18
+ # expand:: Allows container to fill available space in indicated direction.
19
+ # Values 'no' / 'horizontal' / 'vertical' / 'yes'.
20
+ # expandchildren:: Set to allow children to expand fully,
21
+ # values as 'yes' / 'no' / 'horizontal' / 'vertical'.
22
+ # fittochildren:: 'column' / 'line', n -> force column/line n to fit largest element in that column/line.
23
+ # gapcol:: n, horizontal space in pixels between columns.
24
+ # gaplin:: n, vertical space in pixels between lines.
25
+ # homogeneouscol:: Forces all columns to have same horizontal space,
26
+ # values as 'yes' / 'no'.
27
+ # homogeneouslin:: Forces all lines to have same vertical space.
28
+ # values as 'yes' / 'no'.
29
+ # margin:: Margin in x and y directions in pixels, value as "mxn".
30
+ # normalsize:: Set to make natural size of children same,
31
+ # values as 'yes' / 'no' / 'horizontal' / 'vertical'.
32
+ # numcol:: <b>read-only</b> returns number of columns.
33
+ # numdiv:: 'auto' / n, controls number of divisions in direction as
34
+ # determined by +orientation+.
35
+ # numlin:: <b>read-only</b> returns number of lines.
36
+ # orientation:: Controls distribution of children, in lines or columns.
37
+ # Value as 'horizontal' / 'vertical'.
38
+ # position:: <b>read-only</b> returns position in pixels within client window
39
+ # as "x,y".
40
+ # rastersize:: Size of the container, in pixels, value as "widthxheight".
41
+ # sizecol:: Index of column to use for calculating height of lines.
42
+ # sizelin:: Index of line to use for calculating width of columns.
43
+ #
44
+ #
45
+ class GridBox < Widget
46
+ include DynamicFillMethods
47
+
48
+ # Creates an instance of the hbox.
49
+ # *widgets:: one or more child widgets
50
+ # block:: optional block to set up the box's attributes.
51
+ def initialize *widgets, &block
52
+ @handle = IupLib.IupGridBox *widget_list(widgets)
53
+
54
+ # run any provided block on instance, to set up further attributes
55
+ self.instance_eval &block if block_given?
56
+ end
57
+
58
+ # -- attributes
59
+
60
+ def alignmentcol val=nil, val2=nil # :nodoc:
61
+ if val.nil?
62
+ IupLib.IupGetAttribute(@handle, 'ALIGNMENTCOL').first
63
+ elsif val2.nil?
64
+ IupLib.IupSetAttribute @handle, 'ALIGNMENTCOL', val.to_s
65
+ else
66
+ IupLib.IupSetAttribute @handle, "ALIGNMENTCOL#{val}", val2.to_s
67
+ end
68
+ end
69
+
70
+ define_attribute :alignmentlin
71
+ define_attribute :cgapcol
72
+ define_attribute :cgaplin
73
+ define_readonly :clientoffset
74
+ define_readonly :clientsize
75
+ define_attribute :cmargin
76
+ define_attribute :expand
77
+ define_attribute :expandchildren
78
+ define_attribute :fittochildren
79
+ define_attribute :gapcol
80
+ define_attribute :gaplin
81
+ define_attribute :homogeneouscol
82
+ define_attribute :homogeneouslin
83
+ define_attribute :margin
84
+ define_attribute :normalizesize
85
+ define_readonly :numcol
86
+ define_attribute :numdiv
87
+ define_readonly :numlin
88
+ define_attribute :orientation
89
+ define_readonly :position
90
+ define_attribute :rastersize
91
+ define_attribute :sizecol
92
+ define_attribute :sizelin
93
+ end
94
+ end
@@ -0,0 +1,49 @@
1
+ module Iup
2
+
3
+ # A container for one or more child widgets, arranged in a horizontal row.
4
+ #
5
+ # == Attributes
6
+ #
7
+ # alignment:: horizontal alignment of children, 'atop' / 'acenter' / 'abottom'
8
+ # clientoffset:: <b>read-only</b>, returns current offset of box in its client
9
+ # as "widthxheight".
10
+ # clientsize:: <b>read-only</b>, returns current size of box as "widthxheight".
11
+ # expand:: Allows container to fill available space in indicated direction.
12
+ # Values 'no' / 'horizontal' / 'vertical' / 'yes'.
13
+ # expandchildren:: Set to allow children to expand fully, values as 'yes' / 'no'.
14
+ # gap:: Number of pixels between children, default value of 0.
15
+ # homogeneous:: Set to force all children to get equal size, values as 'yes' / 'no'.
16
+ # margin:: Margin in x and y directions, value as "mxn".
17
+ # normalsize:: Set to make natural size of children same, values as 'yes' / 'no'.
18
+ # position:: <b>read-only</b> returns position in pixels within client window
19
+ # as "x,y".
20
+ # rastersize:: Size of the container, in pixels, value as "widthxheight".
21
+ #
22
+ class HBox < Widget
23
+ include DynamicFillMethods
24
+
25
+ # Creates an instance of the hbox.
26
+ # *widgets:: one or more child widgets
27
+ # block:: optional block to set up the box's attributes.
28
+ def initialize *widgets, &block
29
+ @handle = IupLib.IupHbox *widget_list(widgets)
30
+
31
+ # run any provided block on instance, to set up further attributes
32
+ self.instance_eval &block if block_given?
33
+ end
34
+
35
+ # -- attributes
36
+
37
+ define_attribute :alignment
38
+ define_readonly :clientoffset
39
+ define_readonly :clientsize
40
+ define_attribute :expand
41
+ define_attribute :expandchildren
42
+ define_attribute :gap
43
+ define_attribute :homogeneous
44
+ define_attribute :margin
45
+ define_attribute :normalizesize
46
+ define_readonly :position
47
+ define_attribute :rastersize
48
+ end
49
+ end
@@ -0,0 +1,27 @@
1
+ module Iup
2
+
3
+ # Defines attributes for widgets which contain images.
4
+ #
5
+ module ImageAttributes
6
+ include AttributeReference
7
+
8
+ # Sets the image to display, based on an image or image name.
9
+ # This can use an actual image object, or the name of an image
10
+ # from +IupImageLib+.
11
+ def image val=nil
12
+ attribute_reference 'IMAGE', ImageWidget, val
13
+ end
14
+
15
+ # Sets the image to display when inactive, based on an image or image name.
16
+ def iminactive val=nil
17
+ attribute_reference 'IMINACTION', ImageWidget, val
18
+ end
19
+
20
+ # Sets the image to display when pressed, based on an image or image name.
21
+ def impress val=nil
22
+ attribute_reference 'IMPRESS', ImageWidget, val
23
+ end
24
+ end
25
+
26
+ end
27
+
@@ -0,0 +1,118 @@
1
+ module Iup
2
+
3
+ # Loads an image from file and returns an appropriate ruby class
4
+ def Iup.LoadImage filename
5
+ handle = ImLib.IupLoadImage filename
6
+ image = ImageWidget.new
7
+ image.instance_eval do
8
+ @handle = handle
9
+ end
10
+ return image
11
+ end
12
+
13
+ # Parent class of images provides common attributes.
14
+ #
15
+ # == Attributes
16
+ #
17
+ # assign_handle:: <b>write-only</b> sets the name for referring to image.
18
+ # bpp:: <b>read-only</b> Returns the number of bits per pixel.
19
+ # channels:: <b>read-only</b> Returns the number of channels.
20
+ # color:: index, "r g b" -> gets/sets colour index. (Used only in 8-bit images created using +Image+)
21
+ # colour:: index, "r g b" -> gets/sets colour index. (Used only in 8-bit images created using +Image+)
22
+ # height:: <b>read-only</b> Height of the image, in pixels.
23
+ # hotspot:: "x:y", coordinates of spot in image used for mouse click. Default is "0:0".
24
+ # rastersize:: <b>read-only</b> Size of the image, in pixels, value as "widthxheight".
25
+ # wid:: <b>read-only</b> Native widget identifier.
26
+ # width:: <b>read-only</b> Width of the image, in pixels.
27
+ #
28
+ class ImageWidget < Widget
29
+
30
+ define_readonly :bpp
31
+ define_readonly :channels
32
+
33
+ def colour index, value #:nodoc:
34
+ IupLib.IupSetAttribute @handle, index.to_s, value
35
+ end
36
+
37
+ def color index, value # :nodoc:
38
+ colour index, value
39
+ end
40
+
41
+ define_readonly :height
42
+ define_attribute :hotspot
43
+ define_readonly :rastersize
44
+ define_readonly :wid
45
+ define_readonly :width
46
+
47
+ # Saves image to given filename.
48
+ # Optional format is one of: "BMP", "JPEG", "GIF", "TIFF", "PNG", "PNM", "PCX", "ICO", etc.
49
+ def save filename, format = 'png'
50
+ err = IupIm.IupSaveImage @handle, filename, format
51
+ if err.zero?
52
+ raise Exception, 'Could not save image as file'
53
+ end
54
+ end
55
+ end
56
+
57
+ # Image made from greyscale values. For example:
58
+ #
59
+ # pixmap_x = [
60
+ # 1,2,3,3,3,3,3,3,3,2,1,
61
+ # 2,1,2,3,3,3,3,3,2,1,2,
62
+ # 3,2,1,2,3,3,3,2,1,2,3,
63
+ # 3,3,2,1,2,3,2,1,2,3,3,
64
+ # 3,3,3,2,1,2,1,2,3,3,3,
65
+ # 3,3,3,3,2,1,2,3,3,3,3,
66
+ # 3,3,3,2,1,2,1,2,3,3,3,
67
+ # 3,3,2,1,2,3,2,1,2,3,3,
68
+ # 3,2,1,2,3,3,3,2,1,2,3,
69
+ # 2,1,2,3,3,3,3,3,2,1,2,
70
+ # 1,2,3,3,3,3,3,3,3,2,1
71
+ # ]
72
+ #
73
+ # img = Image.new 11, 11, pixmap_x
74
+ #
75
+ class Image < ImageWidget
76
+ # Constructor creates an image from raw pixels, using 1-value per pixel.
77
+ # width:: width of image in pixels
78
+ # height:: height of image in pixels
79
+ # pixels:: an array of pixel values
80
+ # block:: an optional block to set attributes
81
+ def initialize width, height, pixels, &block
82
+ @handle = IupLib.IupImage width, height, IupLib.pointer_from_chars(pixels)
83
+
84
+ # run any provided block on instance, to set up further attributes
85
+ self.instance_eval &block if block_given?
86
+ end
87
+ end
88
+
89
+ # Image made from RGB values.
90
+ class ImageRGB < ImageWidget
91
+ # Constructor creates an image from raw pixels, using 3-values per pixel.
92
+ # width:: width of image in pixels
93
+ # height:: height of image in pixels
94
+ # pixels:: an array of pixel values
95
+ # block:: an optional block to set attributes
96
+ def initialize width, height, pixels, &block
97
+ @handle = IupLib.IupImageRGB width, height, IupLib.pointer_from_chars(pixels)
98
+
99
+ # run any provided block on instance, to set up further attributes
100
+ self.instance_eval &block if block_given?
101
+ end
102
+ end
103
+
104
+ # Image made from RGBA values.
105
+ class ImageRGBA < ImageWidget
106
+ # Constructor creates an image from raw pixels, using 4-values per pixel.
107
+ # width:: width of image in pixels
108
+ # height:: height of image in pixels
109
+ # pixels:: an array of pixel values
110
+ # block:: an optional block to set attributes
111
+ def initialize width, height, pixels, &block
112
+ @handle = IupLib.IupImageRGBA width, height, IupLib.pointer_from_chars(pixels)
113
+
114
+ # run any provided block on instance, to set up further attributes
115
+ self.instance_eval &block if block_given?
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,21 @@
1
+ module Iup
2
+
3
+ # defines attributes and methods used for internal drag and drop
4
+ #
5
+ module InternalDragDropAttributes
6
+ extend AttributeBuilders
7
+
8
+ # attributes
9
+
10
+ define_attribute :dragdroplist
11
+ define_attribute :dropfilestarget
12
+ define_attribute :showdragdrop
13
+
14
+ # callbacks
15
+
16
+ def dragdrop_cb callback
17
+ define_callback callback, 'DRAGDROP_CB', :iiii_i
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,51 @@
1
+ module Iup
2
+
3
+ # All IUP GUI code must be called via a block passed to this method, as
4
+ # in this minimal example.
5
+ #
6
+ # mainloop do
7
+ #
8
+ # label = Label.new("Hello World!")
9
+ #
10
+ # Dialog.new label do
11
+ # title ' ... from IUP'
12
+ # size '150x50'
13
+ # end.show
14
+ # end
15
+ #
16
+ def mainloop
17
+ begin
18
+ IupLib.IupOpen 0, nil
19
+ ImgLib::IupImageLibOpen()
20
+ yield
21
+ IupLib.IupMainLoop
22
+ ensure
23
+ IupLib.IupClose
24
+ end
25
+ end
26
+
27
+ # Accesses the global variable 'name'.
28
+ # Provide a value for 'val' to set the variable,
29
+ # or leave as nil to get the variable.
30
+ def global name, val=nil
31
+ if val.nil?
32
+ IupLib.IupGetGlobal(name).first
33
+ else
34
+ IupLib.IupSetGlobal name, val
35
+ end
36
+ end
37
+
38
+ # Sets the idle_action
39
+ def idle callback
40
+ unless callback.nil? or callback.arity.zero?
41
+ raise ArgumentError 'callback to idle must take no arguments'
42
+ end
43
+ IupLib.IupSetFunction 'IDLE_ACTION', callback
44
+ end
45
+
46
+ # Make all of Iup module available at class level
47
+ # so Iup::mainloop etc can be used within 'include'
48
+ class << self
49
+ include Iup
50
+ end
51
+ end
@@ -0,0 +1,98 @@
1
+ module Iup
2
+
3
+ # A Label is a a static control.
4
+ # It can display some text or an image, or act as a separator.
5
+ #
6
+ # The following example (see "examples/label.rb") displays three labels:
7
+ #
8
+ # 1. a label with some text given in blue, a large font, and center aligned.
9
+ # 2. a label as a horizontal separator.
10
+ # 3. a label using an image.
11
+ #
12
+ # labeltext = <<-END
13
+ # This label has the following attributes set:
14
+ # BGCOLOR = 255 255 0
15
+ # FGCOLOR = 0 0 255
16
+ # FONT = 'Courier, Normal 14'
17
+ # ALIGNMENT = ACENTER
18
+ # END
19
+ #
20
+ # lbl = Label.new labeltext do # <1>
21
+ # bgcolor '255 255 0'
22
+ # fgcolor '0 0 255'
23
+ # font 'Courier, Normal 14'
24
+ # alignment 'ACENTER'
25
+ # end
26
+ #
27
+ # lbl_explain = Label.new 'The label on the right has the image of a star'
28
+ # lbl_star = Label.new do # <2>
29
+ # image img_star # reference to an image
30
+ # end
31
+ #
32
+ # separator = Label.new do # <3>
33
+ # separator 'HORIZONTAL'
34
+ # end
35
+ #
36
+ # Dialog.new(VBox.new(lbl, separator, HBox.new(lbl_explain, lbl_star))) do
37
+ # title 'IupLabel Example'
38
+ # end.show
39
+ #
40
+ # 1. A label with text, modifying the attributes of the text.
41
+ # 2. A label with an image.
42
+ # 3. A label which acts as an horizontal separator.
43
+ #
44
+ # == Attributes
45
+ #
46
+ # alignment:: Sets the horizontal and vertical alignment.
47
+ # The value is a string "horizontal:vertical", with
48
+ # options ALEFT, ACENTER, ARIGHT or none.
49
+ # ellipsis:: If set, adds "..." to the text if there is inadequate space,
50
+ # values 'yes' / 'no'.
51
+ # expand:: Allows label to fill available space in indicated direction.
52
+ # Values 'no' / 'horizontal' / 'vertical' / 'yes'.
53
+ # image:: Sets the image to display.
54
+ # This can be an actual image object, or the name of an image.
55
+ # iminactive:: Sets the image to display wihen inactive.
56
+ # padding:: Margin in x and y directions, value as "mxn".
57
+ # position:: <b>read-only</b> returns position in pixels within client window
58
+ # as "x,y".
59
+ # rastersize:: Size of the label, in pixels, value as "widthxheight".
60
+ # separator:: 'horizontal' / 'vertical', makes line into a line separator.
61
+ # screenposition:: <b>read-only</b> returns position in pixels on screen
62
+ # as "x,y".
63
+ # spacing:: Space between image and text, value as a number.
64
+ # tip:: Tooltip string.
65
+ # title:: text to display (unless label has an image or is a separator).
66
+ #
67
+ class Label < Widget
68
+ include ImageAttributes
69
+ include DragDropAttributes
70
+
71
+ # Creates an instance of a label.
72
+ # text:: optional text to use for label.
73
+ # block:: optional block to set label's attributes.
74
+ def initialize text=nil, &block
75
+ @handle = IupLib.IupLabel text
76
+
77
+ # run any provided block on instance, to set up further attributes
78
+ self.instance_eval &block if block_given?
79
+ end
80
+
81
+ # -- attributes
82
+
83
+ define_attribute :alignment
84
+ define_attribute :ellipsis
85
+ define_attribute :expand
86
+ define_attribute :padding
87
+ define_readonly :position
88
+ define_attribute :rastersize
89
+ define_readonly :screenposition
90
+ define_attribute :separator
91
+ define_attribute :tip
92
+ define_attribute :title
93
+
94
+ # -- callbacks
95
+
96
+ include ButtonCallback
97
+ end
98
+ end
@@ -0,0 +1,59 @@
1
+ module Iup
2
+
3
+ # A Link is a static control, a kind of Label, which displays an underlined
4
+ # clickable text. When clicked, the Link can open a url.
5
+ #
6
+ # The control can show the actual link, or an alternative piece of text.
7
+ # A custom +action+ can be provided, which must return +DEFAULT+ to also
8
+ # open the URL, or +IGNORE+ if the URL should not be opened.
9
+ #
10
+ # For example:
11
+ #
12
+ # link1 = Link.new 'https://ruby-lang.org' # <1>
13
+ #
14
+ # link2 = Link.new 'https://ruby-lang.org', 'Source' do # <2>
15
+ # tip 'Link is https://ruby-lang.org'
16
+ # end
17
+ #
18
+ # link3 = Link.new 'https://ruby-lang.org', 'Source' do # <3>
19
+ # action ->(url) {
20
+ # puts "you clicked on #{title} with URL: #{url}"
21
+ # IGNORE # return DEFAULT if you also want link to be opened
22
+ # }
23
+ # end
24
+ #
25
+ # 1. Simply displays the URL, and opens it when clicked.
26
+ # 2. Displays the word "Source" but opens URL when clicked.
27
+ # 3. Displays the word "Source" but overrides +action+ so the URL is not opened when clicked.
28
+ #
29
+ class Link < Label
30
+
31
+ # Creates an instance of Link.
32
+ # url:: the URL to display / follow.
33
+ # text:: optional text to display in place of URL
34
+ # block:: optional block to set up link's parameters
35
+ def initialize url, text=nil, &block
36
+ @handle = IupLib.IupLink url, text
37
+
38
+ # run any provided block on instance, to set up further attributes
39
+ self.instance_eval &block if block_given?
40
+ end
41
+
42
+ # -- callbacks
43
+
44
+ # action callback is called when the link is clicked.
45
+ # The action callback takes a single argument, the
46
+ # url of the link.
47
+ # Return IUP_CLOSE to process link.
48
+ # If returns IUP_DEFAULT, or link is not defined, the IupHelp function will be called.
49
+ def action callback
50
+ unless callback.arity == 1
51
+ raise ArgumentError, 'action callback must take 1 argument: the url'
52
+ end
53
+ cb = Proc.new do |ih, url|
54
+ callback.call url
55
+ end
56
+ define_callback cb, 'ACTION', :s_i
57
+ end
58
+ end
59
+ end