00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../window_gui.h"
00014 #include "../string_func.h"
00015 #include "../strings_func.h"
00016 #include "../window_func.h"
00017 #include "dropdown_type.h"
00018
00019 #include "dropdown_widget.h"
00020
00021
00022 void DropDownListItem::Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
00023 {
00024 int c1 = _colour_gradient[bg_colour][3];
00025 int c2 = _colour_gradient[bg_colour][7];
00026
00027 int mid = top + this->Height(0) / 2;
00028 GfxFillRect(left + 1, mid - 2, right - 1, mid - 2, c1);
00029 GfxFillRect(left + 1, mid - 1, right - 1, mid - 1, c2);
00030 }
00031
00032 uint DropDownListStringItem::Width() const
00033 {
00034 char buffer[512];
00035 GetString(buffer, this->String(), lastof(buffer));
00036 return GetStringBoundingBox(buffer).width;
00037 }
00038
00039 void DropDownListStringItem::Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
00040 {
00041 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, this->String(), sel ? TC_WHITE : TC_BLACK);
00042 }
00043
00051 bool DropDownListStringItem::NatSortFunc(const DropDownListItem *first, const DropDownListItem *second)
00052 {
00053 char buffer1[512], buffer2[512];
00054 GetString(buffer1, static_cast<const DropDownListStringItem*>(first)->String(), lastof(buffer1));
00055 GetString(buffer2, static_cast<const DropDownListStringItem*>(second)->String(), lastof(buffer2));
00056 return strnatcmp(buffer1, buffer2) < 0;
00057 }
00058
00059 StringID DropDownListParamStringItem::String() const
00060 {
00061 for (uint i = 0; i < lengthof(this->decode_params); i++) SetDParam(i, this->decode_params[i]);
00062 return this->string;
00063 }
00064
00065 StringID DropDownListCharStringItem::String() const
00066 {
00067 SetDParamStr(0, this->raw_string);
00068 return this->string;
00069 }
00070
00075 static void DeleteDropDownList(DropDownList *list)
00076 {
00077 for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) {
00078 DropDownListItem *item = *it;
00079 delete item;
00080 }
00081 delete list;
00082 }
00083
00084 static const NWidgetPart _nested_dropdown_menu_widgets[] = {
00085 NWidget(NWID_HORIZONTAL),
00086 NWidget(WWT_PANEL, COLOUR_END, WID_DM_ITEMS), SetMinimalSize(1, 1), SetScrollbar(WID_DM_SCROLL), EndContainer(),
00087 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_DM_SHOW_SCROLL),
00088 NWidget(NWID_VSCROLLBAR, COLOUR_END, WID_DM_SCROLL),
00089 EndContainer(),
00090 EndContainer(),
00091 };
00092
00093 static WindowDesc _dropdown_desc(
00094 WDP_MANUAL, NULL, 0, 0,
00095 WC_DROPDOWN_MENU, WC_NONE,
00096 0,
00097 _nested_dropdown_menu_widgets, lengthof(_nested_dropdown_menu_widgets)
00098 );
00099
00101 struct DropdownWindow : Window {
00102 WindowClass parent_wnd_class;
00103 WindowNumber parent_wnd_num;
00104 int parent_button;
00105 DropDownList *list;
00106 int selected_index;
00107 byte click_delay;
00108 bool drag_mode;
00109 bool instant_close;
00110 int scrolling;
00111 Point position;
00112 Scrollbar *vscroll;
00113
00127 DropdownWindow(Window *parent, DropDownList *list, int selected, int button, bool instant_close, const Point &position, const Dimension &size, Colours wi_colour, bool scroll)
00128 : Window(&_dropdown_desc)
00129 {
00130 this->position = position;
00131
00132 this->CreateNestedTree();
00133
00134 this->vscroll = this->GetScrollbar(WID_DM_SCROLL);
00135
00136 uint items_width = size.width - (scroll ? NWidgetScrollbar::GetVerticalDimension().width : 0);
00137 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_DM_ITEMS);
00138 nwi->SetMinimalSize(items_width, size.height + 4);
00139 nwi->colour = wi_colour;
00140
00141 nwi = this->GetWidget<NWidgetCore>(WID_DM_SCROLL);
00142 nwi->colour = wi_colour;
00143
00144 this->GetWidget<NWidgetStacked>(WID_DM_SHOW_SCROLL)->SetDisplayedPlane(scroll ? 0 : SZSP_NONE);
00145
00146 this->FinishInitNested(0);
00147 CLRBITS(this->flags, WF_WHITE_BORDER);
00148
00149
00150 int list_height = 0;
00151 for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
00152 DropDownListItem *item = *it;
00153 list_height += item->Height(items_width);
00154 }
00155
00156
00157 this->vscroll->SetCapacity(size.height * (uint16)list->size() / list_height);
00158 this->vscroll->SetCount((uint16)list->size());
00159
00160 this->parent_wnd_class = parent->window_class;
00161 this->parent_wnd_num = parent->window_number;
00162 this->parent_button = button;
00163 this->list = list;
00164 this->selected_index = selected;
00165 this->click_delay = 0;
00166 this->drag_mode = true;
00167 this->instant_close = instant_close;
00168 }
00169
00170 ~DropdownWindow()
00171 {
00172
00173
00174 this->window_class = WC_INVALID;
00175 this->SetDirty();
00176
00177 Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
00178 if (w2 != NULL) {
00179 Point pt = _cursor.pos;
00180 pt.x -= w2->left;
00181 pt.y -= w2->top;
00182 w2->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close);
00183 }
00184 DeleteDropDownList(this->list);
00185 }
00186
00187 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
00188 {
00189 return this->position;
00190 }
00191
00197 bool GetDropDownItem(int &value)
00198 {
00199 if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
00200
00201 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_DM_ITEMS);
00202 int y = _cursor.pos.y - this->top - nwi->pos_y - 2;
00203 int width = nwi->current_x - 4;
00204 int pos = this->vscroll->GetPosition();
00205
00206 const DropDownList *list = this->list;
00207
00208 for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
00209
00210 if (--pos >= 0) continue;
00211
00212 const DropDownListItem *item = *it;
00213 int item_height = item->Height(width);
00214
00215 if (y < item_height) {
00216 if (item->masked || !item->Selectable()) return false;
00217 value = item->result;
00218 return true;
00219 }
00220
00221 y -= item_height;
00222 }
00223
00224 return false;
00225 }
00226
00227 virtual void DrawWidget(const Rect &r, int widget) const
00228 {
00229 if (widget != WID_DM_ITEMS) return;
00230
00231 TextColour colour = (TextColour)this->GetWidget<NWidgetCore>(widget)->colour;
00232
00233 int y = r.top + 2;
00234 int pos = this->vscroll->GetPosition();
00235 for (DropDownList::const_iterator it = this->list->begin(); it != this->list->end(); ++it) {
00236 const DropDownListItem *item = *it;
00237 int item_height = item->Height(r.right - r.left + 1);
00238
00239
00240 if (--pos >= 0) continue;
00241
00242 if (y + item_height < r.bottom) {
00243 bool selected = (this->selected_index == item->result);
00244 if (selected) GfxFillRect(r.left + 2, y, r.right - 1, y + item_height - 1, PC_BLACK);
00245
00246 item->Draw(r.left, r.right, y, y + item_height, selected, colour);
00247
00248 if (item->masked) {
00249 GfxFillRect(r.left + 1, y, r.right - 1, y + item_height - 1, _colour_gradient[colour][5], FILLRECT_CHECKER);
00250 }
00251 }
00252 y += item_height;
00253 }
00254 }
00255
00256 virtual void OnClick(Point pt, int widget, int click_count)
00257 {
00258 if (widget != WID_DM_ITEMS) return;
00259 int item;
00260 if (this->GetDropDownItem(item)) {
00261 this->click_delay = 4;
00262 this->selected_index = item;
00263 this->SetDirty();
00264 }
00265 }
00266
00267 virtual void OnTick()
00268 {
00269 if (this->scrolling != 0) {
00270 int pos = this->vscroll->GetPosition();
00271
00272 this->vscroll->UpdatePosition(this->scrolling);
00273 this->scrolling = 0;
00274
00275 if (pos != this->vscroll->GetPosition()) {
00276 this->SetDirty();
00277 }
00278 }
00279 }
00280
00281 virtual void OnMouseLoop()
00282 {
00283 Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
00284 if (w2 == NULL) {
00285 delete this;
00286 return;
00287 }
00288
00289 if (this->click_delay != 0 && --this->click_delay == 0) {
00290
00291
00292 this->window_class = WC_INVALID;
00293 this->SetDirty();
00294
00295 w2->OnDropdownSelect(this->parent_button, this->selected_index);
00296 delete this;
00297 return;
00298 }
00299
00300 if (this->drag_mode) {
00301 int item;
00302
00303 if (!_left_button_clicked) {
00304 this->drag_mode = false;
00305 if (!this->GetDropDownItem(item)) {
00306 if (this->instant_close) delete this;
00307 return;
00308 }
00309 this->click_delay = 2;
00310 } else {
00311 if (_cursor.pos.y <= this->top + 2) {
00312
00313 this->scrolling = -1;
00314 return;
00315 } else if (_cursor.pos.y >= this->top + this->height - 2) {
00316
00317 this->scrolling = 1;
00318 return;
00319 }
00320
00321 if (!this->GetDropDownItem(item)) return;
00322 }
00323
00324 if (this->selected_index != item) {
00325 this->selected_index = item;
00326 this->SetDirty();
00327 }
00328 }
00329 }
00330 };
00331
00346 void ShowDropDownListAt(Window *w, DropDownList *list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width, bool instant_close)
00347 {
00348 DeleteWindowById(WC_DROPDOWN_MENU, 0);
00349
00350
00351 int top = w->top + wi_rect.bottom + 1;
00352
00353
00354 uint width = wi_rect.right - wi_rect.left + 1;
00355
00356 uint max_item_width = 0;
00357
00358 if (auto_width) {
00359
00360 for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
00361 const DropDownListItem *item = *it;
00362 max_item_width = max(max_item_width, item->Width() + 5);
00363 }
00364 }
00365
00366
00367 int list_height = 0;
00368
00369 for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
00370 DropDownListItem *item = *it;
00371 list_height += item->Height(width);
00372 }
00373
00374
00375 int height = list_height;
00376
00377
00378 int screen_bottom = GetMainViewBottom();
00379 bool scroll = false;
00380
00381
00382 if (top + height + 4 >= screen_bottom) {
00383
00384 if (w->top + wi_rect.top - height > GetMainViewTop()) {
00385 top = w->top + wi_rect.top - height - 4;
00386 } else {
00387
00388
00389 int avg_height = list_height / (int)list->size();
00390 int rows = (screen_bottom - 4 - top) / avg_height;
00391 height = rows * avg_height;
00392 scroll = true;
00393
00394
00395 max_item_width += NWidgetScrollbar::GetVerticalDimension().width;
00396 }
00397 }
00398
00399 if (auto_width) width = max(width, max_item_width);
00400
00401 Point dw_pos = { w->left + (_current_text_dir == TD_RTL ? wi_rect.right + 1 - width : wi_rect.left), top};
00402 Dimension dw_size = {width, height};
00403 new DropdownWindow(w, list, selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll);
00404 }
00405
00419 void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close)
00420 {
00421
00422
00423 Rect wi_rect;
00424 NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
00425 wi_rect.left = nwi->pos_x;
00426 wi_rect.right = nwi->pos_x + nwi->current_x - 1;
00427 wi_rect.top = nwi->pos_y;
00428 wi_rect.bottom = nwi->pos_y + nwi->current_y - 1;
00429 Colours wi_colour = nwi->colour;
00430
00431 if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
00432 nwi->disp_flags |= ND_DROPDOWN_ACTIVE;
00433 } else {
00434 w->LowerWidget(button);
00435 }
00436 w->SetWidgetDirty(button);
00437
00438 if (width != 0) {
00439 if (_current_text_dir == TD_RTL) {
00440 wi_rect.left = wi_rect.right + 1 - width;
00441 } else {
00442 wi_rect.right = wi_rect.left + width - 1;
00443 }
00444 }
00445
00446 ShowDropDownListAt(w, list, selected, button, wi_rect, wi_colour, auto_width, instant_close);
00447 }
00448
00460 void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask, uint width)
00461 {
00462 DropDownList *list = new DropDownList();
00463
00464 for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
00465 if (!HasBit(hidden_mask, i)) {
00466 list->push_back(new DropDownListStringItem(strings[i], i, HasBit(disabled_mask, i)));
00467 }
00468 }
00469
00470
00471 if (list->size() == 0) {
00472 DeleteDropDownList(list);
00473 return;
00474 }
00475
00476 ShowDropDownList(w, list, selected, button, width);
00477 }
00478
00484 int HideDropDownMenu(Window *pw)
00485 {
00486 Window *w;
00487 FOR_ALL_WINDOWS_FROM_BACK(w) {
00488 if (w->window_class != WC_DROPDOWN_MENU) continue;
00489
00490 DropdownWindow *dw = dynamic_cast<DropdownWindow*>(w);
00491 if (pw->window_class == dw->parent_wnd_class &&
00492 pw->window_number == dw->parent_wnd_num) {
00493 int parent_button = dw->parent_button;
00494 delete dw;
00495 return parent_button;
00496 }
00497 }
00498
00499 return -1;
00500 }
00501