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 const WindowDesc _dropdown_desc(
00094 WDP_MANUAL, 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) : Window()
00128 {
00129 this->position = position;
00130
00131 this->CreateNestedTree(&_dropdown_desc);
00132
00133 this->vscroll = this->GetScrollbar(WID_DM_SCROLL);
00134
00135 uint items_width = size.width - (scroll ? NWidgetScrollbar::GetVerticalDimension().width : 0);
00136 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_DM_ITEMS);
00137 nwi->SetMinimalSize(items_width, size.height + 4);
00138 nwi->colour = wi_colour;
00139
00140 nwi = this->GetWidget<NWidgetCore>(WID_DM_SCROLL);
00141 nwi->colour = wi_colour;
00142
00143 this->GetWidget<NWidgetStacked>(WID_DM_SHOW_SCROLL)->SetDisplayedPlane(scroll ? 0 : SZSP_NONE);
00144
00145 this->FinishInitNested(&_dropdown_desc, 0);
00146 CLRBITS(this->flags, WF_WHITE_BORDER);
00147
00148
00149 int list_height = 0;
00150 for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
00151 DropDownListItem *item = *it;
00152 list_height += item->Height(items_width);
00153 }
00154
00155
00156 this->vscroll->SetCapacity(size.height * (uint16)list->size() / list_height);
00157 this->vscroll->SetCount((uint16)list->size());
00158
00159 this->parent_wnd_class = parent->window_class;
00160 this->parent_wnd_num = parent->window_number;
00161 this->parent_button = button;
00162 this->list = list;
00163 this->selected_index = selected;
00164 this->click_delay = 0;
00165 this->drag_mode = true;
00166 this->instant_close = instant_close;
00167 }
00168
00169 ~DropdownWindow()
00170 {
00171
00172
00173 this->window_class = WC_INVALID;
00174 this->SetDirty();
00175
00176 Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
00177 if (w2 != NULL) {
00178 Point pt = _cursor.pos;
00179 pt.x -= w2->left;
00180 pt.y -= w2->top;
00181 w2->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close);
00182 }
00183 DeleteDropDownList(this->list);
00184 }
00185
00186 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00187 {
00188 return this->position;
00189 }
00190
00196 bool GetDropDownItem(int &value)
00197 {
00198 if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
00199
00200 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_DM_ITEMS);
00201 int y = _cursor.pos.y - this->top - nwi->pos_y - 2;
00202 int width = nwi->current_x - 4;
00203 int pos = this->vscroll->GetPosition();
00204
00205 const DropDownList *list = this->list;
00206
00207 for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
00208
00209 if (--pos >= 0) continue;
00210
00211 const DropDownListItem *item = *it;
00212 int item_height = item->Height(width);
00213
00214 if (y < item_height) {
00215 if (item->masked || !item->Selectable()) return false;
00216 value = item->result;
00217 return true;
00218 }
00219
00220 y -= item_height;
00221 }
00222
00223 return false;
00224 }
00225
00226 virtual void DrawWidget(const Rect &r, int widget) const
00227 {
00228 if (widget != WID_DM_ITEMS) return;
00229
00230 TextColour colour = (TextColour)this->GetWidget<NWidgetCore>(widget)->colour;
00231
00232 int y = r.top + 2;
00233 int pos = this->vscroll->GetPosition();
00234 for (DropDownList::const_iterator it = this->list->begin(); it != this->list->end(); ++it) {
00235 const DropDownListItem *item = *it;
00236 int item_height = item->Height(r.right - r.left + 1);
00237
00238
00239 if (--pos >= 0) continue;
00240
00241 if (y + item_height < r.bottom) {
00242 bool selected = (this->selected_index == item->result);
00243 if (selected) GfxFillRect(r.left + 2, y, r.right - 1, y + item_height - 1, PC_BLACK);
00244
00245 item->Draw(r.left, r.right, y, y + item_height, selected, colour);
00246
00247 if (item->masked) {
00248 GfxFillRect(r.left + 1, y, r.right - 1, y + item_height - 1, _colour_gradient[colour][5], FILLRECT_CHECKER);
00249 }
00250 }
00251 y += item_height;
00252 }
00253 }
00254
00255 virtual void OnClick(Point pt, int widget, int click_count)
00256 {
00257 if (widget != WID_DM_ITEMS) return;
00258 int item;
00259 if (this->GetDropDownItem(item)) {
00260 this->click_delay = 4;
00261 this->selected_index = item;
00262 this->SetDirty();
00263 }
00264 }
00265
00266 virtual void OnTick()
00267 {
00268 if (this->scrolling != 0) {
00269 int pos = this->vscroll->GetPosition();
00270
00271 this->vscroll->UpdatePosition(this->scrolling);
00272 this->scrolling = 0;
00273
00274 if (pos != this->vscroll->GetPosition()) {
00275 this->SetDirty();
00276 }
00277 }
00278 }
00279
00280 virtual void OnMouseLoop()
00281 {
00282 Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
00283 if (w2 == NULL) {
00284 delete this;
00285 return;
00286 }
00287
00288 if (this->click_delay != 0 && --this->click_delay == 0) {
00289
00290
00291 this->window_class = WC_INVALID;
00292 this->SetDirty();
00293
00294 w2->OnDropdownSelect(this->parent_button, this->selected_index);
00295 delete this;
00296 return;
00297 }
00298
00299 if (this->drag_mode) {
00300 int item;
00301
00302 if (!_left_button_clicked) {
00303 this->drag_mode = false;
00304 if (!this->GetDropDownItem(item)) {
00305 if (this->instant_close) delete this;
00306 return;
00307 }
00308 this->click_delay = 2;
00309 } else {
00310 if (_cursor.pos.y <= this->top + 2) {
00311
00312 this->scrolling = -1;
00313 return;
00314 } else if (_cursor.pos.y >= this->top + this->height - 2) {
00315
00316 this->scrolling = 1;
00317 return;
00318 }
00319
00320 if (!this->GetDropDownItem(item)) return;
00321 }
00322
00323 if (this->selected_index != item) {
00324 this->selected_index = item;
00325 this->SetDirty();
00326 }
00327 }
00328 }
00329 };
00330
00345 void ShowDropDownListAt(Window *w, DropDownList *list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width, bool instant_close)
00346 {
00347 DeleteWindowById(WC_DROPDOWN_MENU, 0);
00348
00349
00350 int top = w->top + wi_rect.bottom + 1;
00351
00352
00353 uint width = wi_rect.right - wi_rect.left + 1;
00354
00355 uint max_item_width = 0;
00356
00357 if (auto_width) {
00358
00359 for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
00360 const DropDownListItem *item = *it;
00361 max_item_width = max(max_item_width, item->Width() + 5);
00362 }
00363 }
00364
00365
00366 int list_height = 0;
00367
00368 for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
00369 DropDownListItem *item = *it;
00370 list_height += item->Height(width);
00371 }
00372
00373
00374 int height = list_height;
00375
00376
00377 int screen_bottom = GetMainViewBottom();
00378 bool scroll = false;
00379
00380
00381 if (top + height + 4 >= screen_bottom) {
00382
00383 if (w->top + wi_rect.top - height > GetMainViewTop()) {
00384 top = w->top + wi_rect.top - height - 4;
00385 } else {
00386
00387
00388 int avg_height = list_height / (int)list->size();
00389 int rows = (screen_bottom - 4 - top) / avg_height;
00390 height = rows * avg_height;
00391 scroll = true;
00392
00393
00394 max_item_width += NWidgetScrollbar::GetVerticalDimension().width;
00395 }
00396 }
00397
00398 if (auto_width) width = max(width, max_item_width);
00399
00400 Point dw_pos = { w->left + (_current_text_dir == TD_RTL ? wi_rect.right + 1 - width : wi_rect.left), top};
00401 Dimension dw_size = {width, height};
00402 new DropdownWindow(w, list, selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll);
00403 }
00404
00418 void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close)
00419 {
00420
00421
00422 Rect wi_rect;
00423 NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
00424 wi_rect.left = nwi->pos_x;
00425 wi_rect.right = nwi->pos_x + nwi->current_x - 1;
00426 wi_rect.top = nwi->pos_y;
00427 wi_rect.bottom = nwi->pos_y + nwi->current_y - 1;
00428 Colours wi_colour = nwi->colour;
00429
00430 if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
00431 nwi->disp_flags |= ND_DROPDOWN_ACTIVE;
00432 } else {
00433 w->LowerWidget(button);
00434 }
00435 w->SetWidgetDirty(button);
00436
00437 if (width != 0) {
00438 if (_current_text_dir == TD_RTL) {
00439 wi_rect.left = wi_rect.right + 1 - width;
00440 } else {
00441 wi_rect.right = wi_rect.left + width - 1;
00442 }
00443 }
00444
00445 ShowDropDownListAt(w, list, selected, button, wi_rect, wi_colour, auto_width, instant_close);
00446 }
00447
00459 void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask, uint width)
00460 {
00461 DropDownList *list = new DropDownList();
00462
00463 for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
00464 if (!HasBit(hidden_mask, i)) {
00465 list->push_back(new DropDownListStringItem(strings[i], i, HasBit(disabled_mask, i)));
00466 }
00467 }
00468
00469
00470 if (list->size() == 0) {
00471 DeleteDropDownList(list);
00472 return;
00473 }
00474
00475 ShowDropDownList(w, list, selected, button, width);
00476 }
00477
00483 int HideDropDownMenu(Window *pw)
00484 {
00485 Window *w;
00486 FOR_ALL_WINDOWS_FROM_BACK(w) {
00487 if (w->window_class != WC_DROPDOWN_MENU) continue;
00488
00489 DropdownWindow *dw = dynamic_cast<DropdownWindow*>(w);
00490 if (pw->window_class == dw->parent_wnd_class &&
00491 pw->window_number == dw->parent_wnd_num) {
00492 int parent_button = dw->parent_button;
00493 delete dw;
00494 return parent_button;
00495 }
00496 }
00497
00498 return -1;
00499 }
00500