iup_list_dialog.e 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. class IUP_LIST_DIALOG
  2. -- Shows a modal dialog to select items from a simple or multiple selection
  3. -- list.
  4. --
  5. -- The dialog uses a global attribute called "PARENTDIALOG" as the parent
  6. -- dialog if it is defined. It also uses a global attribute called "ICON" as
  7. -- the dialog icon if it is defined.
  8. inherit
  9. IUP_WIDGET
  10. create {ANY}
  11. list_dialog_simple,
  12. list_dialog_multiple
  13. feature {ANY}
  14. list_dialog_simple (title: STRING; list: ARRAY[STRING];
  15. op, max_col, max_lin: INTEGER)
  16. -- Create a dialog for simple selection.
  17. --
  18. -- title: Text for the dialog’s title.
  19. -- list: List of options.
  20. -- op: Initial selected item. Starts at 1 (note that this index is
  21. -- different from the return value, kept for compatibility reasons).
  22. -- max_col: number of visible columns in the list.
  23. -- max_lin: number of visible lines in the list.
  24. do
  25. t := 1
  26. ttl := title
  27. l := list
  28. o := op
  29. mc := max_col
  30. ml := max_lin
  31. convert_to_native(list)
  32. end
  33. list_dialog_multiple (title: STRING; list: ARRAY[STRING];
  34. max_col, max_lin: INTEGER)
  35. -- Create a dialog for multiple selection.
  36. --
  37. -- title: Text for the dialog’s title.
  38. -- list: List of options.
  39. -- max_col: number of visible columns in the list.
  40. -- max_lin: number of visible lines in the list.
  41. do
  42. t := 2
  43. ttl := title
  44. l := list
  45. o := 0
  46. mc := max_col
  47. ml := max_lin
  48. convert_to_native(list)
  49. end
  50. launch: TUPLE[INTEGER, STRING]
  51. -- Show the dialog. If is a dialog for simple selection, the tuple
  52. -- return an integer with the index of the selected item (starts
  53. -- at 0), or -1 if the user cancels the operation. Also return the
  54. -- item itself, or an empty string if the user cancel the operation.
  55. -- If is a dialog of multiple selection, the function returns -1 when
  56. -- the user cancels the operation and an empty string. If the user
  57. -- does not cancel the operation the function returns 1 and the string
  58. -- parameter will have value 1 for the options selected by the user
  59. -- and value 0 for non-selected options (for example: "0010011100").
  60. local
  61. i, x: INTEGER
  62. so: STRING
  63. marks: NATIVE_ARRAY[INTEGER]
  64. do
  65. marks := marks.calloc(l.count)
  66. marks.set_all_with(0, l.count - 1)
  67. i := int_list_dialog (t, ttl.to_external,
  68. l.count, arg.to_external, o, mc, ml,
  69. marks.to_external)
  70. if i.is_equal(-1) then
  71. Result := [i, ""]
  72. else
  73. if t.is_equal(1) then
  74. Result := [i, l.item(l.lower + i)]
  75. else
  76. so := ""
  77. from
  78. x := 0
  79. until
  80. x.is_equal(l.count)
  81. loop
  82. so.append_string(marks.item(x).to_string)
  83. x := x + 1
  84. end
  85. Result := [i, so]
  86. end
  87. end
  88. end
  89. feature {}
  90. t, o, mc, ml: INTEGER
  91. ttl: STRING
  92. l: ARRAY[STRING]
  93. arg: NATIVE_ARRAY[POINTER]
  94. -- Internals
  95. int_list_dialog (type: INTEGER; title: POINTER; size: INTEGER; list: POINTER; op, max_col, max_lin: INTEGER; marks: POINTER): INTEGER
  96. external "plug_in"
  97. alias "{
  98. location: "${sys}/plugins"
  99. module_name: "iup"
  100. feature_name: "IupListDialog"
  101. }"
  102. end
  103. convert_to_native (col: ARRAY[STRING])
  104. local
  105. iterator: ITERATOR[STRING]; i: INTEGER; s: STRING
  106. do
  107. i := col.count
  108. arg := arg.calloc(i)
  109. iterator := col.new_iterator
  110. i := 0
  111. from
  112. iterator.start
  113. until
  114. iterator.is_off
  115. loop
  116. s := iterator.item
  117. arg.put(s.to_external, i)
  118. iterator.next
  119. i := i + 1
  120. end
  121. end
  122. end -- class IUP_LIST_DIALOG
  123. -- The MIT License (MIT)
  124. -- Copyright (c) 2016, 2017 by German A. Arias
  125. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  126. -- of this software and associated documentation files (the "Software"), to deal
  127. -- in the Software without restriction, including without limitation the rights
  128. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  129. -- copies of the Software, and to permit persons to whom the Software is
  130. -- furnished to do so, subject to the following conditions:
  131. --
  132. -- The above copyright notice and this permission notice shall be included in
  133. -- all copies or substantial portions of the Software.
  134. --
  135. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  136. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  137. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  138. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  139. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  140. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  141. -- SOFTWARE.