Author: mordante
Date: Thu Jun 12 08:34:59 2008
New Revision: 27110
URL: http://svn.gna.org/viewcvs/wesnoth?rev=27110&view=rev
Log:
Refactoring the code to add listbox items from the code, with these changes it
will be easier to add multilple widgets to a row.
Modified:
trunk/src/gui/widgets/listbox.cpp
trunk/src/gui/widgets/listbox.hpp
Modified: trunk/src/gui/widgets/listbox.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/listbox.cpp?rev=27110&r1=27109&r2=27110&view=diff
==============================================================================
--- trunk/src/gui/widgets/listbox.cpp (original)
+++ trunk/src/gui/widgets/listbox.cpp Thu Jun 12 08:34:59 2008
@@ -371,11 +371,18 @@
return result;
}
-void tlistbox::add_item(const t_string& label, const std::string& icon)
+void tlistbox::add_item(const titem& item)
+{
+ std::map<std::string, titem> data;
+ data.insert(std::make_pair("", item));
+ add_item(data);
+}
+
+void tlistbox::add_item(const std::map<std::string, titem>& data)
{
assert(list_builder_);
- trow row(*list_builder_, label, icon);
+ trow row(*list_builder_, data);
assert(row.grid());
row.grid()->set_parent(this);
@@ -403,7 +410,7 @@
assert(itor != cell.end());
const std::string& label = itor->second;
- add_item(label, icon);
+ add_item(titem(label, icon));
}
}
@@ -478,17 +485,19 @@
rows_[row].grid()->set_active(active);
}
-tlistbox::trow::trow(const tbuilder_grid& list_builder_,const t_string& label,
const std::string& icon) :
+tlistbox::trow::trow(const tbuilder_grid& list_builder_,
+ const std::map<std::string, titem>& data) :
grid_(dynamic_cast<tgrid*>(list_builder_.build())),
height_(0),
selected_(false)
{
assert(grid_);
- init_in_grid(grid_, label, icon);
-}
-
-void tlistbox::trow::init_in_grid(tgrid* grid, const t_string& label, const
std::string& icon)
-{
+ init_in_grid(grid_, data);
+}
+
+void tlistbox::trow::init_in_grid(tgrid* grid,
+ const std::map<std::string, titem>& data)
+{
for(unsigned row = 0; row < grid->get_rows(); ++row) {
for(unsigned col = 0; col < grid->get_cols(); ++col) {
twidget* widget = grid->widget(row, col);
@@ -500,16 +509,23 @@
if(btn) {
btn->set_callback_mouse_left_click(callback_select_list_item);
- btn->set_label(label);
- btn->set_icon_name(icon);
+ std::map<std::string, titem>::const_iterator
itor = data.find(btn->id());
+ if(itor == data.end()) {
+ itor = data.find("");
+ }
+ if(itor != data.end()) {
+ btn->set_label(itor->second.label);
+ btn->set_icon_name(itor->second.icon);
+ }
} else if(grid) {
- init_in_grid(child_grid, label, icon);
+ init_in_grid(child_grid, data);
} else {
std::cerr << "Widget type " <<
typeid(*widget).name() << ".\n";
assert(false);
}
}
}
+
}
void tlistbox::trow::select(const bool sel)
Modified: trunk/src/gui/widgets/listbox.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/listbox.hpp?rev=27110&r1=27109&r2=27110&view=diff
==============================================================================
--- trunk/src/gui/widgets/listbox.hpp (original)
+++ trunk/src/gui/widgets/listbox.hpp Thu Jun 12 08:34:59 2008
@@ -15,9 +15,8 @@
#ifndef GUI_WIDGETS_LISTBOX_HPP_INCLUDED
#define GUI_WIDGETS_LISTBOX_HPP_INCLUDED
+#include "tstring.hpp"
#include "gui/widgets/container.hpp"
-
-class t_string;
namespace gui2 {
@@ -98,15 +97,47 @@
*/
/**
- * Adds an item to the list, it requires the builder_list to be
defined.
- * NOTE this is for a listbox with one item per row, for multiple items
- * there will be a version with gets a vector with values. Probably
there
- * also will be a version which gets the name of an image next to the
- * label so we can define lists even easier.
- *
- * Probably the hardcoded list will disappear as well at some point
- */
- void add_item(const t_string& label, const std::string& icon = "");
+ * Listbox item definition.
+ *
+ * A row in a listbox can have one or more items (widgets) each of them
can
+ * be defined from the code. Every wiget can have an item 'assigned'
which
+ * means the fields in that widget get the values of the items set after
+ * constructing.
+ */
+ struct titem {
+
+ titem(const t_string& label, const std::string& icon = "") :
+ label(label),
+ icon(icon)
+ {}
+
+ /** The label for the widget. */
+ t_string label;
+
+ /** the filename of the icon for the widget. */
+ std::string icon;
+ };
+
+ /**
+ * Adds an item to the list.
+ *
+ * The widget added gets the item assigned to it.
+ *
+ * @param item The item to assign to the widget.
+ */
+ void add_item(const titem& item);
+
+ /**
+ * Adds an item to the list.
+ *
+ * An item is most of the time a row which contains one or more widgets.
+ * This map contains the items to assign to those widgets. If a widget
with
+ * the 'id' of a map exists then that widget gets that 'item' assigned.
Else if
+ * there is an empty map 'id', that 'item' gets assigned.
+ *
+ * @param data Map with the items to assign to the row
items.
+ */
+ void add_item(const std::map<std::string /*id*/, titem /*item*/>& data);
/**
* Adds one or more items to the listbox.
@@ -204,8 +235,9 @@
class trow {
public:
+
trow(const tbuilder_grid& list_builder_,
- const t_string& label, const std::string& icon);
+ const std::map<std::string, titem>& data);
void select(const bool sel = true);
@@ -230,7 +262,7 @@
bool selected_;
void init_in_grid(tgrid* grid,
- const t_string& label, const std::string& icon);
+ const std::map<std::string, titem>& data);
void select_in_grid(tgrid* grid, const bool sel);
};
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits