00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "precomp.h"
00010 #include "API/Core/Input/input.h"
00011 #include "API/Core/Font/font.h"
00012 #include "API/GUI/gui_manager.h"
00013 #include "API/GUI/scrollbar.h"
00014 #include "selection_list.h"
00015
00016 #define SELECTIONLIST_DEFAULT_MAX_HEIGHT 5
00017
00018 CL_SelectionList::CL_SelectionList(
00019 const CL_ComponentOptions &options,
00020 CL_Component *parent,
00021 CL_StyleManager_Default *style,
00022 CL_Font *_font,
00023 const std::list<std::string> &_selection_list) : CL_Component(style,parent)
00024 {
00025 this->style = style;
00026 selection_list = _selection_list;
00027 font = _font;
00028 highlighted_option = -1;
00029
00030 int x, y, width, height;
00031 x = options.get_value_as_int("x");
00032 y = options.get_value_as_int("y");
00033 width = options.get_value_as_int("width");
00034
00035 max_height = SELECTIONLIST_DEFAULT_MAX_HEIGHT;
00036 if (options.exists("height"))
00037 max_height = options.get_value_as_int("height");
00038 if (max_height > selection_list.size())
00039 max_height = selection_list.size();
00040 if (options.exists("value"))
00041 highlighted_option = options.get_value_as_int("value");
00042
00043 height = font->get_height()*max_height;
00044
00045 set_position(CL_Rect(x, y, x + width, y + height));
00046
00047 slots.add_slot(sig_key_down().connect(CL_CreateSlot(this, &CL_SelectionList::on_key_down)));
00048 slots.add_slot(sig_mouse_moved().connect(CL_CreateSlot(this, &CL_SelectionList::on_mouse_move)));
00049 slots.add_slot(sig_paint().connect(CL_CreateSlot(this, &CL_SelectionList::on_paint)));
00050 slots.add_slot(sig_lost_focus().connect(CL_CreateSlot(this, &CL_SelectionList::cancel)));
00051
00052 scrollbar = NULL;
00053 has_scrollbar = false;
00054 scroll_offset = 0;
00055 if (selection_list.size() > max_height)
00056 {
00057 has_scrollbar = true;
00058 CL_ComponentOptions s_options;
00059
00060 #ifdef BORLAND
00061 s_options.options.insert(std::make_pair(std::string("orientation"), std::string("ver")));
00062 s_options.options.insert(std::make_pair(std::string("x"), std::string(CL_String(get_width()-17))));
00063 s_options.options.insert(std::make_pair(std::string("y"), std::string("1")));
00064 s_options.options.insert(std::make_pair(std::string("height"), std::string(CL_String(get_height()-2))));
00065 s_options.options.insert(std::make_pair(std::string("range"), std::string(CL_String(int(selection_list.size()-max_height)))));
00066 s_options.options.insert(std::make_pair(std::string("scroller_size"), std::string("16")));
00067 if (highlighted_option != -1)
00068 {
00069 s_options.options.insert(std::make_pair(std::string("value"), std::string(CL_String(highlighted_option))));
00070 }
00071 #else
00072 s_options.options.insert(std::make_pair<std::string const, std::string>("orientation", "ver"));
00073 s_options.options.insert(std::make_pair<std::string const, std::string>("x", CL_String(get_width()-17)));
00074 s_options.options.insert(std::make_pair<std::string const, std::string>("y", "1"));
00075 s_options.options.insert(std::make_pair<std::string const, std::string>("height", CL_String(get_height()-2)));
00076 s_options.options.insert(std::make_pair<std::string const, std::string>("range", CL_String(int(selection_list.size()-max_height))));
00077 s_options.options.insert(std::make_pair<std::string const, std::string>("scroller_size", "16"));
00078 if (highlighted_option != -1)
00079 {
00080 s_options.options.insert(std::make_pair<std::string const, std::string>("value", CL_String(highlighted_option)));
00081 }
00082 #endif
00083
00084 scrollbar = CL_ScrollBar::create(s_options, this, style);
00085 slots.add_slot(scrollbar->sig_scrolled.connect(CL_CreateSlot(this, &CL_SelectionList::on_scroll_change)));
00086 add_child(scrollbar, true);
00087
00088 scroll_offset = scrollbar->get_cur_position();
00089 }
00090 }
00091
00092 void CL_SelectionList::on_paint()
00093 {
00094
00095
00096 style->draw_line(0, 0, get_width()-1, 0, GUICOL_DARK_OUTLINE);
00097 style->draw_line(0, get_height()-1, get_width()-1, get_height()-1, GUICOL_DARK_OUTLINE);
00098 style->draw_line(0, 0, 0, get_height()-1, GUICOL_DARK_OUTLINE);
00099 style->draw_line(get_width()-1, 0, get_width()-1, get_height()-1, GUICOL_DARK_OUTLINE);
00100 style->fill_rect(1, 1, get_width()-1, get_height()-1, GUICOL_TEXT_BG);
00101
00102 int pos = 0;
00103 std::list<std::string>::iterator it = selection_list.begin();
00104 for (;it!=selection_list.end();it++)
00105 {
00106 if (pos < scroll_offset)
00107 {
00108 pos++;
00109 continue;
00110 }
00111 if (pos == highlighted_option)
00112 {
00113 style->fill_rect(
00114 1,
00115 1+(pos-scroll_offset)*font->get_height(),
00116 get_width()-1,
00117 1+(pos+1-scroll_offset)*font->get_height(),
00118 GUICOL_SELECTION);
00119 }
00120 font->print_left(4, 1+(pos-scroll_offset)*font->get_height(), (*it).c_str());
00121 pos++;
00122 }
00123 }
00124
00125 void CL_SelectionList::cancel()
00126 {
00127 sig_cancelled();
00128 close();
00129
00130 }
00131
00132 void CL_SelectionList::on_key_down(CL_Component *comp, CL_InputDevice *device, CL_Key key)
00133 {
00134 if (device != CL_Input::pointers[0] || key.id != 0) return;
00135 if (has_scrollbar && key.x >= get_width()-scrollbar->get_width()) return;
00136
00137 if(has_focus())
00138 {
00139 sig_activated(highlighted_option);
00140 close();
00141
00142 }
00143 else
00144 {
00145
00146 cancel();
00147 }
00148 }
00149
00150 void CL_SelectionList::on_mouse_move(CL_Component *comp, CL_InputDevice *device, int x, int y)
00151 {
00152 if (has_scrollbar && x >= get_width()-scrollbar->get_width())
00153 return;
00154
00155 highlighted_option = y / font->get_height()+scroll_offset;
00156 }
00157
00158 void CL_SelectionList::on_scroll_change(int new_offset)
00159 {
00160 scroll_offset = new_offset;
00161 }