#

## GUI Resources

Component | Button | Frame | Image | InputBox | Label | ListBox | ProgressBar | RadioButton | ScrollBar | Window

### GUI definition files (.xml)

Instead of hardcoding every GUI components by code, you can use a GUI definition file to define the placement, attributes and hierarchy of GUI components. The GUI definition format looks a lot like the ClanLib resource manager format, so if you're familiar with that, you should easily get acquainted with the GUI definition format. GUI files do not have sections. Instead, components can contain child-components within a tag, simply function as "sections". This means that the hierarchical structure of the GUI is directly readable from the GUI definition file. Let's look at a small example;

    <components>
       <window name="my_window" x="10" y="10" width="600" height="400" title="My Window" >
          <components>
             <button name="my_button" x="25" y="25" width="100" height="20" text="My Button" />
             <label name="my_label" x="25" y="50" text="My label" />
          </components>
       </window>
    </components>

A window named my_window is the top-level component here. As this component is at the top of the component hierarchy, the x, y position is directly mapped to the parent GUI item. Since this window has no GUI parent, the coordinates aremapped to your CL_DisplayWindow. Each sub-somponent's x/y values are relative to the current position of their parent. This means that my_button will always be offset (25, 25) pixels from the upper-left corner of the client area of the window, and that both my_button and my_label will be dragged along if my_window is moved. Width and height is not relative to any parents, and is measured in pixels also. However, all sub-components are clipped within the screen area of the parent.

Every component type recognize different sets of values in the .xml file. Some values are mandatory for a component (they have to exist in the .xml-file for the initialization to succeed), and some are optional, and have default values in case they don't exist. All component types require the 'x' and 'y' value, describing the position/offset of the component in question.

Note that all components need to be within a <components> tag. This goes for the root items, and also children of other components.

Example code to load a GUI definition file:

    CL_ResourceManager resources("resources.xml");
    CL_StyleManager_Silver style(&resources);
    CL_GUIManager gui(&style);

    CL_ComponentManager manager("mygui.xml", &gui);

    gui.run();

This code is fairly simple, but a few things need to be explained.

- The "resources.xml" file must contain items that describe the GUI style. The easiest way to do this is to copy the files required from the "Resources" directory of your ClanLib distribution.
- The "mygui.xml" file contains the GUI layout code, as listed above.
- Finally, calling "gui.run();" will block all further execution. This may, or may not be what you want. You can call "gui.show();" to draw the GUI to the screen exactly once. For more information, see CL_GUIManager.

To access individual components from a definition file, you use the CL_ComponentManager::get_component function. Some examples:

    // Hook up the button click signal to a function
    CL_Button *my_button = (CL_Button *) manager.get_component("my_button");
    CL_Slot s = my_button->sig_clicked().connect(this, &App::on_button_clicked);

If you have RTTI enabled in your project, you can also use the following approach:

    CL_Button *my_button;
    manager.get_component("my_button", &my_button);

### Methods for running a GUI and rendering game graphics

If you want to draw directly to the screen and run a GUI at the same time, there are two options available to you.

#### Option 1: Use gui.show()

Instead of calling "gui.run()" in the above example, you can simply call "gui.show()" every time the GUI needs to be drawn to the screen. It's up to you to decide how often to redraw the GUI. Calling "gui.show()" from within your own render loop is very easy to do, but is slow without code to detect when the GUI needs to be redrawn.

#### Option 2: Use the sig_paint event.

A sig_paint signal is emitted every time the GUI is redrawn. You can use this event to draw your own graphics before the GUI is drawn to the screen. Simply connect one of your own methods to the paint event:

    CL_Slot m_slotOnPaint = gui.sig_paint().connect(this, &Game::OnRender);

All you need to do is create the "Game::OnRender" class (obviously replace "Game" with the name of your class).

### Putting GUI definitions inside ClanLib resource files

Now that ClanLib uses XML for both the resources and the gui definitions, you can actually put them both into the same file.

    <resources>
       <!-- Include all resources from the silver theme here too -->
       <surface name="button_up" file="resources/button_up.tga" />

       <components name="gui">
          <button x="10" y="190" surface_up="button_up" />
       </components>
    </resources>

And some code to load it:

    CL_ResourceManager resources("resources.xml");
    CL_StyleManager_Silver style(&resources);
    CL_GUIManager gui(&style);

    CL_ComponentManager manager("gui", &resources, &gui);

    gui.run();

### Component options

The following section describes all the component attributes available for each component.

#### Component

This is the base class all other components inherit. So every other component has these basic attributes.

Tag name: <component>
Class name: CL_Component

- Attribute name: Name of gui component.
  Valid values: String
  Default value: ""
- Attribute x: Position x of component.
  Valid values: Integer
  Default value: 0
- Attribute y: Position y of component.
  Valid values: Integer
  Default value: 0
- Attribute width: Width of component.
  Valid values: Integer
  Default value: 0
- Attribute height: Height of component.
  Valid values: Integer
  Default value: 0
- Attribute visible: Visibility of component.
  Valid values: Boolean
  Default value: true
- Attribute enabled: If the component is enabled or disabled.
  Valid values: Boolean
  Default value: true
- Attribute tab_id: Tab id.
  Valid values: Integer
  Default value: 0

Examples:

    <component name="component1" x="10" y="10" width="100" height="20" />
    <component name="component2" x="10" y="30" width="70" height="20" enabled="false" tab_id="3"/>
    <component name="component3" x="10" y="50" width="80" height="40" visible="false" />

#### Button

Tag name: <button>
Class name: CL_Button

- Attribute text: The text on the button.
  Valid values: String
  Default value: ""
- Attribute toggled: If the button is toggled by default or not.
  Valid values: Boolean
  Default value: false
- Attribute togglemode: The togglemode, if false the button is sticky.
  Valid values: Boolean
  Default value: false

Examples:

    <button name="button1" togglemode="true" x="10" y="10" width="100" height="20" text="Stick me!" />
    <button name="button2" x="10" y="40" width="100" height="20" text="Press me!" />
    <button name="button3" x="10" y="70" text="Auto size" />

#### Frame

Tag name: <frame>
Class name: CL_Frame

- Attribute filled: If the frame should be filled or transparent.
  Valid values: Boolean
  Default value: false

Examples:

    <frame name="frame1" x="10" y="10" width="100" height="20" filled="true" />
    <frame x="10" y="35" width="100" height="20" />

#### Image

Tag name: <image>
Class name: CL_Image

- Attribute :
  Valid values:
  Default value:

Example:

#### InputBox

Tag name: <inputbox>
Class name: CL_InputBox

- Attribute text: Default input of the inputbox.
  Valid values: String
  Default value: ""
- Attribute passwordmode: Password mode sets if the inputbox should display only * when entering text.
  Valid values: Boolean
  Default value: false
- Attribute read_only: If read only, it is not possible to enter text into the inputbox.
  Valid values: Boolean
  Default value: false
- Attribute max_length: Max number of characters allowed.
  Valid values: Integer
  Default value: unlimited

Examples:

    <inputbox name="inputbox1" x="10" y="10" width="100" height="20" text="Default value" />
    <inputbox name="inputbox2" x="10" y="35" width="100" height="20" max_length="4" passwordmode="true" />

#### Label

Tag name: <label>
Class name: CL_Label

- Attribute text: The text of the label.
  Valid values: String
  Default value: ""

Example:

    <label x="10" y="10" width="100" height="20" text="Welcome to ClanGUI!" />

#### ListBox

Tag name: <listbox>
Class name: CL_ListBox

- Child element item: Defines a item in the listbox.
  - Attribute value: Text of the listitem.
    Valid values: String
    Default value: ""

Example:

    <listbox name="listbox1" x="10" y="10" width="100" height="100">
    	<item value="Item 1" />
    	<item value="Item 2" />
    	<item value="Item 3" />
    </listbox>

#### ProgressBar

Tag name: <progressbar>
Class name: CL_ProgressBar

- Attribute steps: Number of steps in the progressbar.
  Valid values: Integer
  Default value: 0

Example:

    <progressbar name="progressbar1" x="10" y="10" width="100" height="20" steps="100" />

#### RadioButton

Tag name: <radiobutton>
Class name: CL_RadioButton

- Attribute :
  Valid values:
  Default value:

Example:

#### ScrollBar

Tag name: <scrollbar>
Class name: CL_ScrollBar

- Attribute orientation: The orientation of the scrollbar, horizontal or vertical.
  Valid values: hor, horz, horizontal, 1 = horizontal orientation - ver, vert, vertical, 0 = vertical orientation
  Default value: vertical
- Attribute min: Minimum value of the scrollbar range.
  Valid values: Integer
  Default value: 0
- Attribute max: Maximum value of the scrollbar range.
  Valid values: Integer
  Default value: value of minimum
- Attribute value: Current value of the scrollbar range.
  Valid values: Integer
  Default value: value of minimum
- Attribute tracking: Enables or disables tracking.
  Valid values: Boolean
  Default value: false

Examples:

    <scrollbar name="scrollbar1" x="160" y="10" width="20" height="220" min="0" orientation="ver" max="100" />
    <scrollbar name="scrollbar2" x="10" y="230" width="150" height="20" min="0" max="10" orientation="hor" />

#### Window

Tag name: <window>
Class name: CL_Window

- Attribute title: Title of the window.
  Valid values: String
  Default value: ""

Example:

    <window name="window1" x="10" y="10" width="400" height="300" title="This is a window" />

Questions or comments, write to the ClanLib mailing list.