Widgets
From Guichan
Contents |
Widget
Widget is the base class for all widgets. It contains common functionality for all widgets, such as setting if a widget should be visible or not.
gcn::Widget* widget;
void foo()
{
widget->setEnabled(true);
wigdet->setVisible(false); // A non visible widget is also a disabled widget.
widget->setBaseColor(gcn::Color(0xff0000));
widget->setBackgroundColor(gcn::Color(0x00ff00));
widget->setForegroundColor(gcn::Color(0x0000ff));
widget->setSelectionColor(gcn::Color(0xff00ff));
}
Widget also contains functions for adding and removing listeners to a widget, such as adding and removing action listeners and mouse listeners, see Events.
gcn::Widget* widget;
MyActionListener* actionListener;
MyMouseListener* mouseListener;
void foo()
{
widget->addActionListener(actionListener);
widget->removeMouseListener(mouseListener);
}
BasicContainer
BasicContainer is the base class for all containers. It contains common functionality for a container, such as adding and removing widgets. BasicContainer is an abstract class and is implemented by a couple of Widgets, such as Container and Window.
Button
Button is a normal button with a caption. A Button can be automatically adjusted to the size of the caption and the font with the Button::adjustSize function.
gcn::Button* button;
void foo()
{
button = new gcn::Button("Caption");
button->adjustSize();
}
A Button sends an action event to the action listener's of the Button as soon as it has been pressed.
gcn::Button* button;
class ButtonActionListener: public gcn::ActionListener
{
void action(const ActionEvent& actionEvent)
{
if (actionEvent.getSource() == button)
{
// The Button has been pressed.
}
}
};
void foo()
{
gcn::ActionListener* actionListener = new ButtonActionListener();
button->addActionListener(actionListener);
}
CheckBox
CheckBox is a normal check box with a caption. A CheckBox can be automatically adjusted to the size of the caption and the font with the CheckBox::adjustSize function.
gcn::CheckBox* checkBox;
void foo()
{
checkBox = new gcn::CheckBox("Caption");
checkBox->adjustSize();
}
A CheckBox can be in one of two states, it can be marked or not marked. To check whether a CheckBox is marked or not is done with the CheckBox::isMarked function.
gcn::CheckBox* checkBox;
void foo()
{
if (checkBox->isMarked())
{
// The CheckBox is marked.
}
}
A CheckBox sends an action event to the action listener's of the CheckBox as soon as it's state changes.
gcn::CheckBox* checkBox;
class CheckBoxActionListener: public gcn::ActionListener
{
void action(const ActionEvent& actionEvent)
{
if (actionEvent.getSource() == checkBox)
{
// The state of the CheckBox has changed.
}
}
};
void foo()
{
gcn::ActionListener* actionListener = new CheckBoxActionListener();
checkBox->addActionListener(actionListener);
}
Container
A Container is a widget that can contain other widgets. Widgets are added to a Container with the Container::add function.
gcn::Container* container;
gcn::Label* label;
gcn::Button* button;
void foo()
{
container = new gcn::Container();
container->add(label); // Default position will be used (i.e. Labels x and y)
container->add(button, 100, 150); // Adds a button at coordinate (100, 150)
}
An added widget can be removed with the Container::remove function.
gcn::Container* container;
gcn::Button* button;
void foo()
{
container->remove(button);
}
A container can be cleared from all widgets with the Container::clear function.
gcn::Container* container;
void foo()
{
container->clear();
}
The order by which widgets are drawn in a Container is determined by the order of widgets added to a Container. The order can be changed by moving a widget to the top of a Container or the bottom of a Container with the Container::moveToTop and Container::moveToBottom functions. The order by which widgets receive focus is also determined by the order of widgets added to a Container. The order of receiving focus can also be changed by the Container::moveToTop and Container::moveToBottom functions.
gcn::Container* container;
gcn::Label* label;
gcn::Button* button;
void foo()
{
container->moveToTop(button);
container->moveToBottom(label);
}
DropDown
DropDown is a widget where a list can be folded down and an item in the list can be selected. If the list in the DropDown gets bigger than the DropDown, scroll bars will be shown. To display a list a DropDown needs a list model. A list model is a class that implements the ListModel interface. Here is an example of a list model that contains three elements:
class MyListModel: public gcn::ListModel
{
public:
std::string getElementAt(int i)
{
if (i == 0)
{
return "Element 1";
}
else if (i == 1)
{
return "Element 2";
}
else if (i == 2)
{
return "Element 3";
}
}
int getNumberOfElements()
{
return 3;
}
};
A ListModel is passed to DropDowns's constructor.
ListModel* listModel;
gcn::DropDown* dropDown:
void foo()
{
listModel = new MyListModel();
dropDown = new gcn::DropDown(listModel);
}
As DropDown uses a ListBox to display it's list and a ScrollArea to make the list scrollable, a costume ListBox and costume ScrollArea can be passed to the DropDown with the DropDown's constructor.
gcn::ListModel* listModel;
gcn::ScrollArea* scrollArea;
gcn::ListBox* listBox;
gcn::DropDown* dropDown;
void foo()
{
listModel = new MyListModel();
scrollArea = new gcn::ScrollArea();
listBox = new gcn::ListBox();
dropDown = new gcn::DropDown(listModel, scrollArea, listBox);
}
The index of selected element in the list of the DropDown can be retrieved with the DropDown::getSelected function.
gcn::DropDown* dropDown;
void foo()
{
dropDown->getSelected();
}
The selected element index can be set with the DropDown::setSelected function.
gcn::DropDown* dropDown;
void foo()
{
dropDown->setSelected(1);
}
A DropDown sends an action event to the action listener's of the DropDown as soon as an element in the list of the DropDown gets selected.
gcn::DropDown * dropDown;
class DropDownActionListener: public gcn::ActionListener
{
void action(const ActionEvent& actionEvent)
{
if (actionEvent.getSource() == dropDown)
{
// An element in the list of the drop down has been selected.
}
}
};
void foo()
{
gcn::ActionListener* actionListener = new DropDownActionListener();
dropDown->addActionListener(actionListener);
}
Icon
Icon is a basic widget for displaying an image. An Icon needs an Image to display which is passed to the Icon's constructor.
gcn::Icon* icon;
gcn::Image* image;
void foo()
{
image = gcn::Image::load("image.bmp");
icon = new gcn::Icon(image);
}
Label
Label is a normal label displaying a caption consisting of a text with one row. A Label can be automatically adjusted to the size of the caption and the font with the Label::adjustSize function.
gcn::Label* label;
void foo()
{
label = new gcn::Label("Caption");
label->adjustSize();
}
ListBox
A ListBox is a widget that displays a list in which a list element is selectable. To display a list the ListBox needs a list model. A list model is a class that implements the ListModel interface. Here is an example of a list model that contains three elements:
class MyListModel: public gcn::ListModel
{
public:
std::string getElementAt(int i)
{
if (i == 0)
{
return "Element 1";
}
else if (i == 1)
{
return "Element 2";
}
else if (i == 2)
{
return "Element 3";
}
}
int getNumberOfElements()
{
return 3;
}
};
A ListModel is passed to ListBox's constructor or with the ListBox::setListModel function.
gcn::ListModel* listModel;
gcn::ListBox* listBox1;
gcn::ListBox* listBox2;
void foo()
{
listModel = new MyListModel();
listBox1 = new gcn::ListBox(listModel);
listBox2 = new gcn::ListBox();
listBox2->setListModel(listModel);
}
The index of selected element in the ListBox can be retrieved with the ListBox ::getSelected function.
gcn::ListBox * listBox;
void foo()
{
listBox->getSelected();
}
The selected element index can be set with the ListBox::setSelected function.
gcn::ListBox * listBox;
void foo()
{
listBox->setSelected(1);
}
A ListBox sends an action event to the action listener's of the ListBox as soon as an element in the ListBox gets selected.
gcn::ListBox* listBox;
class ListBoxActionListener: public gcn::ActionListener
{
void action(const ActionEvent& actionEvent)
{
if (actionEvent.getSource() == listBox)
{
// An element in the list box has been selected.
}
}
};
void foo()
{
gcn::ActionListener* actionListener = new ListBoxActionListener();
listBox->addActionListener(actionListener);
}
RadioButton
RadioButton is a normal radio button with a caption. A RadioButton can belong to a radio button group. Only one of the RadioButtons in a radio button group can be marked.
gcn::RadioButton* radioButton1;
gcn::RadioButton* radioButton2;
gcn::RadioButton* radioButton3;
void foo()
{
radioButton1 = new gcn::RadioButton("Caption", "Radio button group 1");
radioButton2 = new gcn::RadioButton("Caption", "Radio button group 1");
radioButton3 = new gcn::RadioButton("Caption", "Another radio button group");
}
A RadioButton can be in one of two states, it can be marked or not marked. To check whether a RadioButton is marked or not is done with the RadioBuitton::isMarked function.
gcn::RadioButton* radioButton;
void foo()
{
if (radioBitton->isMarked())
{
// The RadioBitton is marked.
}
}
A RadioButton sends an action event to the action listener's of the RadioButton as soon as it's state changes.
gcn::RadioButton* radioButton;
class RadioButtonActionListener: public gcn::ActionListener
{
void action(const ActionEvent& actionEvent)
{
if (actionEvent.getSource() == radioButton)
{
// The state of the RadioButton has changed.
}
}
};
void foo()
{
gcn::ActionListener* actionListener = new RadioButtonActionListener();
radioButton->addActionListener(actionListener);
}
ScrollArea
ScrollArea is an area that adds the ability to scroll a larger widget than the ScrollArea using horizontal and vertical scroll bars. The widget to scroll is passed to the ScrollArea using it's constructor or with the ScrollArea::setContent function.
gcn::Container* container1;
gcn::Container* container2;
gcn::ScrollArea* scrollArea1;
gcn::ScrollArea* scrollArea2;
void foo()
{
scrollArea1 = new gcn::ScrollArea(container1);
scrollArea2 = new gcn::ScrollArea();
scrollArea2->setContent(container2);
}
A ScrollArea has two scroll bars, a horizontal scroll bar and a vertical scroll bar. A ScrollArea can have different policies regarding when the scroll bars should be visible. Each scroll bar have it's own policy. The policies are to always show the scroll bar, to never show the scroll bar and to automatically show the scroll bar when the content widget is bigger than the ScrollArea. A policy is set to the horizontal scroll bar with the ScrollArea::setHorizontalScrollPolicy and to the vertical scroll bar with the ScrollArea::setVerticalScrollPolicy.
gcn::ScrollArea* scrollArea2;
gcn::ScrollArea* scrollArea1;
void foo()
{
scrollArea1->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS);
scrollArea1->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_AUTO);
scrollArea2->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
scrollArea2->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS);
}
Slider
A Slider is a normal slider with which the user can slide between a start value and an end value.
gcn::Slider* slider1;
gcn::Slider* slider2;
void foo()
{
slider1 = new gcn::Slider(100.0); // Start value is 0 and end value is 100.
slider2 = new gcn::Slider(-1.0, 1.0); // Start value is -1 and end value is 1.
}
A Slider can have on of two different orientations, horizontal orientation (the Slider is drawn horizontally) or vertical orientation (the Slider is drawn vertically). An orientation is set with the Slider::setOrientation function.
gcn::Slider* slider1;
gcn::Slider* slider2;
void foo()
{
slider1->setOrientation(gcn::Slider::HORIZONTAL);
slider2->setOrientation(gcn::Slider::VERTICAL);
}
A Sliders value is retrieved with the Slider::getValue function.
gcn::Slider* slider;
void foo()
{
double value = slider->getValue();
}
A Slider sends an action event to the action listener's of the Sdlier as soon as it's value changes.
gcn::Slider* slider;
class SliderActionListener: public gcn::ActionListener
{
void action(const ActionEvent& actionEvent)
{
if (actionEvent.getSource() == slider)
{
// The value of the Slider has changed.
}
}
};
void foo()
{
gcn::ActionListener* actionListener = new SliderActionListener();
sliderBox->addActionListener(actionListener);
}
TextBox
A TextBox is an area that can contain text with an arbitrary number of rows.
gcn::TextBox* textBox;
void foo()
{
textBox= new gcn::TextBox("Default text"):
}
A TextBox can either be set to editable or none editable with the TextBox::setEditable function. An editable TextBox grows in size as text is entered and shrinks in size as text is deleted.
gcn::TextBox* textBox;
void foo()
{
textBox->setEditable(false):
}
The text entered in a TextBox can be retrieved with the TextBox::getText function.
gcn::TextBox* textBox;
void foo()
{
textBox->getText();
}
As a TextBox grows in size when text is entered it's common to add a TextBox to a ScrollArea so the TextBox will be scrollable.
gcn::TextBox* textBox;
gcn::ScrollArea* scrollArea;
void foo()
{
scrollArea = new gcn::ScrollArea(textBox);
}
TextField
A TextField is a normal text field where one row of text can be entered.
gcn::TextField* textField;
void foo()
{
textField = new gcn::TextField("Default text"):
}
The text entered in a TextField can be retrieved with the TextField::getText function.
gcn::TextField* textField;
void foo()
{
textField->getText();
}
A TextField sends an action event to the action listener's of the TextField as soon as a user hits enter in the TextField.
gcn::TextField* textField;
class TextFieldActionListener: public gcn::ActionListener
{
void action(const ActionEvent& actionEvent)
{
if (actionEvent.getSource() == textField)
{
// The user has hit enter in the TextField
}
}
};
void foo()
{
gcn::ActionListener* actionListener = new TextFieldActionListener();
textField->addActionListener(actionListener);
}
Window
A Window is just like a container with the exception that a Window has a caption and has the ability to be moved.
gcn::Window* window;
gcn::Label* label;
gcn::Button* button;
void foo()
{
window = new gcn::Window("Caption");
window->add(label); // Default position will be used (i.e. Labels x and y)
window->add(button, 100, 150); // Adds a button at coordinate (100, 150)
}
A Window can be set to movable or not movable with the Window::setMovable function.
gcn::Window* window;
void foo()
{
window->setMovable(false);
}
A Window can be resized to fit it's content with the Window::resizeToContent function.
gcn::Window* window;
void foo()
{
window->resizeToContent();
}
A portable C++ GUI library designed for games using Allegro, HGE, OpenGL, OpenLayer and/or SDL.
