Difference between revisions of "LuaAPI/types/widget"

From The Battle for Wesnoth Wiki
< LuaAPI‎ | types
m (Widget methods: - typo)
 
(23 intermediate revisions by 5 users not shown)
Line 1: Line 1:
the 'widget' userdata offers access to a widget of a gui2 dialog, while there is only one type of widget userdata that covers all widgets including the window itself, The properties of a widget userdataq are different for each widgets. Indexing a widgets userdata can either be used to
+
<div class="tright"> __TOC__ </div>
  
* Access a child widget
+
The '''widget''' userdata offers access to a widget of a GUI2 dialog. While there is only one type of widget userdata that covers all widgets including the window itself, the properties of a widget userdata are different for each type of widget. Indexing a widget's userdata can either be used to access a child widget or to set or get a property of a widget. Some properties are read-only or write-only; the properties depend on the type of the widget.
Example code
+
 
```
+
An example of accessing a child widget:
 +
 
 +
<syntaxhighlight lang=lua>
 
function preshow(dialog)
 
function preshow(dialog)
 
   local okay_button = dialog.okay_button
 
   local okay_button = dialog.okay_button
   -- okay_button is now a handle to the the widgets child with the id 'okay_button'  
+
   -- okay_button is now a handle to the the widget's child with the id 'okay_button'  
 
end
 
end
``` or to
+
</syntaxhighlight>
* Set/Get a property of a widget Some Properties are read-only, the propertied depends on the type of  the widget, the following properties exist:
 
 
 
  
=== widget attributes ===
+
== Widget Attributes ==
  
 +
=== selected ===
  
==== selected ====
+
* ''widget''.'''selected''' &harr; ''boolean''
 +
* Available on: '''[toggle_button]''', '''[toggle_panel]'''
  
* '''w.selected = value'''
+
Whether the item is selected or not. Note that this should only be used for widgets that have only 2 states. In particular, there exist 3-State toggle_buttons (for example in listbox headers). For those, selected_index must be used instead.
* '''boolean'''
 
* '''get/set'''
 
* [toggle_button], [toggle_panel]
 
  
whether the item is selected or not
+
=== selected_index ===
  
 +
* ''widget''.'''selected_index''' &harr; ''index''
 +
* Available on: '''[listbox]''', '''[multi_page]''', '''[stacked_widget]''', '''[menu_button]''', '''[toggle_button]''', '''[toggle_panel]'''
  
==== selected_index ====
+
The selected index of the item. For '''[toggle_button]''' and '''[toggle_panel]''', this is the same as '''selected''' only encoded as a number (1 for false or 2 for true) instead of a boolean.
  
* '''w.selected_index = value'''
+
For a  '''[listbox]'''with '''has_maximum=false''' and more than one item selected, reading '''selected_index''' will return the first selected index.
* '''number'''
 
* '''get/set'''
 
* [listbox], [multi_page], [stacked_widget], selectable_item ([menu_button], [toggle_button], [toggle_panel])
 
  
the selected index of the item
+
For a '''[stacked_widget]''', only the layer specified by '''selected_index''' will be displayed and receive events (callbacks will only be triggered on the selected layer).
 +
A '''selected_index''' of 0 represents all layers being selected, with events only being received by the layer with the highest index.  Note that term ''selected'' for the ''layer'' of a stacked_widget is not the same as the '''selected''' ''widget'' attribute.
  
 +
=== text ===
  
==== text ====
+
* ''widget''.'''text''' &harr; ''text''
 +
* Available on: '''[text_box]'''
  
* '''w.text = value'''
+
The text of the textbox.
* '''string'''
 
* '''get/set'''
 
* [text_box]
 
  
the text of the textbox
+
=== value ===
  
==== value ====
+
* ''widget''.'''value''' &harr; ''position''
 +
* Available on: '''[slider]'''
  
* '''w.value = value'''
+
The current position of the slider.
* '''number'''
 
* '''get/set'''
 
* [slider]
 
  
 +
=== percentage ===
  
==== percentage ====
+
* ''widget''.'''percentage''' &harr; ''position''
 +
* Available on: '''[progress_bar]'''
  
* '''w.percentage = value'''
+
The current position of the progress bar, between 0 and 100.
* '''number'''
 
* '''get/set'''
 
* [progress_bar]
 
  
 +
=== selected_item_path ===
  
==== selected_item_path ====
+
* ''widget''.'''selected_item_path''' &rarr; ''array of indices''
 +
* Available on: '''[tree_view]'''
  
* '''value = w.selected_item_path'''
+
A table describing the currently selected node. If for example, in the following treeview, Item 9 is selected, the result will be {2,1,3}.
* '''array of integers'''
 
* '''get'''
 
* [slider]
 
  
a table descibing the currently selected node. If for example in this treeview
 
 
  +Section1
 
  +Section1
 
   +Subsection11
 
   +Subsection11
Line 86: Line 79:
 
   *Item11
 
   *Item11
 
   *Item12
 
   *Item12
Item 9 is selected the value will be {2,1,3}
 
 
==== path ====
 
  
* '''value = w.path'''
+
=== path ===
* '''array of integers'''
 
* '''get'''
 
* [tree_view_node]
 
  
See selected_item_path for the syntax.
+
* ''widget''.'''path''' &rarr; ''array of indices''
 +
* Available on: '''[tree_view_node]'''
  
 +
A table describing this node in the overall treeview. See [[#selected_item_path|selected_item_path]] for the meaning of the table..
  
==== unfolded ====
+
=== unfolded ===
  
* '''w.unfolded = value'''
+
* ''widget''.'''unfolded''' &larr; ''boolean''
* '''boolean'''
+
* Available on: '''[tree_view_node]'''
* '''set'''
 
* [tree_view_node]
 
  
 +
Control whether a tree node is currently expanded or not.
  
==== unit ====
+
=== unit ===
  
* '''w.unit = value'''
+
* ''widget''.'''unit''' &larr; ''unit or unit type''
* '''unit''' or '''unit type'''
+
* Available on: '''[unit_preview_pane]'''
* '''set'''
 
* [unit_preview_pane]
 
  
 +
Change the displayed unit or unit type in the preview pane.
  
==== item_count ====
+
=== item_count ===
  
* '''value = w.item_count'''
+
* ''widget''.'''item_count''' &rarr; ''number of items''
* '''number'''
+
* Available on: '''[multi_page]''', '''[listbox]'''
* '''get'''
 
* [multi_page], [listbox]
 
  
==== use_markup ====
+
The number of items in the container widget.
  
* '''w.use_markup = value'''
+
=== use_markup ===
* '''boolean'''
 
* '''set'''
 
* most widgets, in particular [label], [button]
 
  
==== label ====
+
* ''widget''.'''use_markup''' &rarr; ''boolean''
 +
* Available on: Most widgets, in particular '''[label]''', '''[button]'''
  
* '''w.label = value'''
+
Sets whether the widget's label will parse [[Pango formatting]].
* '''string'''
 
* '''set'''
 
* most widgets, in particular [label], [button], [image]
 
  
the label, technically the is a special string used in the widgets wml definition, it usualyl does what one would expect but also sets the image for [image] widgets
+
=== label ===
  
==== marked_up_text ====
+
* ''widget''.'''label''' &larr; ''text''
 +
* Available on: Most widgets, in particular '''[label]''', '''[button]''', '''[image]'''
  
* '''w.marked_up_text = value'''
+
The widget's label. Technically this is a special string used in the widget's wml definition. It usually does what one would expect, but also sets the image for '''image''' widgets.  For '''[text_box]''', use '''text''' for initial values.
* '''string'''
 
* '''set'''
 
* most widgets, in particular [label], [button]
 
  
shortcut for setting label and use_markup=yes
+
=== marked_up_text ===
  
 +
* ''widget''.'''marked_up_text''' &larr; ''text''
 +
* Available on: Most widgets, in particular '''[label]''', '''[button]'''
  
==== enabled ====
+
Shortcut for setting label and use_markup=yes.
  
* '''w.enabled = value'''
+
=== enabled ===
* '''boolean'''
 
* '''set'''
 
* all widgets
 
  
==== tooltip ====
+
* ''widget''.'''enabled''' &larr; ''boolean''
 +
* Available on: Most widgets
  
* '''w.tooltip = value'''
+
=== tooltip ===
* '''string'''
 
* '''set'''
 
* all widgets
 
  
 +
* ''widget''.'''tooltip''' &larr; ''text''
 +
* Available on: Most widgets
  
==== visible ====
+
=== visible ===
  
* '''w.visible = value'''
+
* ''widget''.'''visible''' &larr; ''visibility string''
* '''string'''
+
* Available on: Most widgets
* '''set'''
 
* all widgets
 
 
 
The following visibility statuses are recognized:
 
  
 +
Determines whether the widget is visible onscreen. The following visibility statuses are recognized:
 
{| clasS="wikitable"
 
{| clasS="wikitable"
 
! String value !! Boolean shorthand !! Meaning
 
! String value !! Boolean shorthand !! Meaning
Line 181: Line 155:
 
|}
 
|}
  
 +
=== type ===
 +
 +
* ''widget''.'''type''' &rarr; ''string''
 +
* Available on: All widgets
 +
 +
Returns a string specifying the type of the widget.
 +
 +
== Widget callbacks ==
 +
 +
=== on_modified ===
 +
 +
* ''widget''.'''on_modified''' &larr; '''function'''()
 +
* Available on: Most widgets, in particular '''[slider]''', '''[toggle_button]''', '''[listbox]''', '''[menu_button]''', '''[text_box]'''
 +
 +
Triggers when the user changes the value of the widget.
 +
 +
=== on_left_click ===
  
==== type ====
+
* ''widget''.'''on_left_click''' &larr; '''function'''()
 +
* Available on: All widgets
  
* '''value = w.type'''
+
Triggers when the user clicks on the widget.
* '''string'''
 
* '''get'''
 
* all widgets
 
  
 +
=== on_button_click ===
  
 +
* ''widget''.'''on_button_click''' &larr; '''function'''()
 +
* Available on: '''[button]''', '''[repeating_button]'''
  
=== widget callbacks ===
+
Triggers when the user clicks on the button. This can differ from '''on_left_click''', depending on the type of widget. For example, on a '''[repeating_button]''' it will fire multiple times if the user holds the mouse button down.
  
==== on_modified ====
+
== Widget methods ==
* '''function w.on_modified() .... end
 
* all widgets the allow the user to select a value of any type.
 
  
==== on_left_click ====
+
Any function defined in the [[LuaAPI/gui/widget|gui.widget]] module and taking a widget as its first parameter can be called as a method of a widget. This includes any functions that are added to the module by user code. Note that these methods are available even if the widget itself doesn't support that function, so in some cases it may be necessary to check '''widget.type''' before calling the method.
* '''function w.on_left_click() .... end
 
* all widgets
 
  
==== on_button_click ====
+
[[Category:Lua Reference]]
* '''function w.on_button_click() .... end
 
* button type widgets
 

Latest revision as of 16:24, 5 November 2024

The widget userdata offers access to a widget of a GUI2 dialog. While there is only one type of widget userdata that covers all widgets including the window itself, the properties of a widget userdata are different for each type of widget. Indexing a widget's userdata can either be used to access a child widget or to set or get a property of a widget. Some properties are read-only or write-only; the properties depend on the type of the widget.

An example of accessing a child widget:

function preshow(dialog)
  local okay_button = dialog.okay_button
  -- okay_button is now a handle to the the widget's child with the id 'okay_button' 
end

Widget Attributes

selected

  • widget.selectedboolean
  • Available on: [toggle_button], [toggle_panel]

Whether the item is selected or not. Note that this should only be used for widgets that have only 2 states. In particular, there exist 3-State toggle_buttons (for example in listbox headers). For those, selected_index must be used instead.

selected_index

  • widget.selected_indexindex
  • Available on: [listbox], [multi_page], [stacked_widget], [menu_button], [toggle_button], [toggle_panel]

The selected index of the item. For [toggle_button] and [toggle_panel], this is the same as selected only encoded as a number (1 for false or 2 for true) instead of a boolean.

For a [listbox]with has_maximum=false and more than one item selected, reading selected_index will return the first selected index.

For a [stacked_widget], only the layer specified by selected_index will be displayed and receive events (callbacks will only be triggered on the selected layer). A selected_index of 0 represents all layers being selected, with events only being received by the layer with the highest index. Note that term selected for the layer of a stacked_widget is not the same as the selected widget attribute.

text

  • widget.texttext
  • Available on: [text_box]

The text of the textbox.

value

  • widget.valueposition
  • Available on: [slider]

The current position of the slider.

percentage

  • widget.percentageposition
  • Available on: [progress_bar]

The current position of the progress bar, between 0 and 100.

selected_item_path

  • widget.selected_item_patharray of indices
  • Available on: [tree_view]

A table describing the currently selected node. If for example, in the following treeview, Item 9 is selected, the result will be {2,1,3}.

+Section1
 +Subsection11
  *Item1
  *Item2
  *Item3
 +Subsection12
  *Item4
  *Item5
  *Item6
+Section2
 +Subsection21
  *Item7
  *Item8
  *Item9
 +Subsection22
  *Item10
  *Item11
  *Item12

path

  • widget.patharray of indices
  • Available on: [tree_view_node]

A table describing this node in the overall treeview. See selected_item_path for the meaning of the table..

unfolded

  • widget.unfoldedboolean
  • Available on: [tree_view_node]

Control whether a tree node is currently expanded or not.

unit

  • widget.unitunit or unit type
  • Available on: [unit_preview_pane]

Change the displayed unit or unit type in the preview pane.

item_count

  • widget.item_countnumber of items
  • Available on: [multi_page], [listbox]

The number of items in the container widget.

use_markup

  • widget.use_markupboolean
  • Available on: Most widgets, in particular [label], [button]

Sets whether the widget's label will parse Pango formatting.

label

  • widget.labeltext
  • Available on: Most widgets, in particular [label], [button], [image]

The widget's label. Technically this is a special string used in the widget's wml definition. It usually does what one would expect, but also sets the image for image widgets. For [text_box], use text for initial values.

marked_up_text

  • widget.marked_up_texttext
  • Available on: Most widgets, in particular [label], [button]

Shortcut for setting label and use_markup=yes.

enabled

  • widget.enabledboolean
  • Available on: Most widgets

tooltip

  • widget.tooltiptext
  • Available on: Most widgets

visible

  • widget.visiblevisibility string
  • Available on: Most widgets

Determines whether the widget is visible onscreen. The following visibility statuses are recognized:

String value Boolean shorthand Meaning
visible true The widget is visible and handles events.
hidden The widget is not visible, doesn't handle events, but still takes up space on the dialog grid.
invisible false The widget is not visible, doesn't handle events, and does not take up space on the dialog grid.

type

  • widget.typestring
  • Available on: All widgets

Returns a string specifying the type of the widget.

Widget callbacks

on_modified

  • widget.on_modifiedfunction()
  • Available on: Most widgets, in particular [slider], [toggle_button], [listbox], [menu_button], [text_box]

Triggers when the user changes the value of the widget.

on_left_click

  • widget.on_left_clickfunction()
  • Available on: All widgets

Triggers when the user clicks on the widget.

on_button_click

  • widget.on_button_clickfunction()
  • Available on: [button], [repeating_button]

Triggers when the user clicks on the button. This can differ from on_left_click, depending on the type of widget. For example, on a [repeating_button] it will fire multiple times if the user holds the mouse button down.

Widget methods

Any function defined in the gui.widget module and taking a widget as its first parameter can be called as a method of a widget. This includes any functions that are added to the module by user code. Note that these methods are available even if the widget itself doesn't support that function, so in some cases it may be necessary to check widget.type before calling the method.

This page was last edited on 5 November 2024, at 16:24.