# dropdown.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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 /* static */ 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   byte 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     /* Total length of list */
    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     /* Capacity is the average number of items visible */
    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     Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
    00172     if (w2 != NULL) {
    00173       if (w2->nested_array != NULL) {
    00174         NWidgetCore *nwi2 = w2->GetWidget<NWidgetCore>(this->parent_button);
    00175         if (nwi2->type == NWID_BUTTON_DROPDOWN) {
    00176           nwi2->disp_flags &= ~ND_DROPDOWN_ACTIVE;
    00177         } else {
    00178           w2->RaiseWidget(this->parent_button);
    00179         }
    00180       } else {
    00181         w2->RaiseWidget(this->parent_button);
    00182       }
    00183       w2->SetWidgetDirty(this->parent_button);
    00184     }
    00185
    00186     DeleteDropDownList(this->list);
    00187   }
    00188
    00189   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00190   {
    00191     return this->position;
    00192   }
    00193
    00199   bool GetDropDownItem(int &value)
    00200   {
    00201     if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
    00202
    00203     NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_DM_ITEMS);
    00204     int y     = _cursor.pos.y - this->top - nwi->pos_y - 2;
    00205     int width = nwi->current_x - 4;
    00206     int pos   = this->vscroll->GetPosition();
    00207
    00208     const DropDownList *list = this->list;
    00209
    00210     for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
    00211       /* Skip items that are scrolled up */
    00212       if (--pos >= 0) continue;
    00213
    00214       const DropDownListItem *item = *it;
    00215       int item_height = item->Height(width);
    00216
    00217       if (y < item_height) {
    00218         if (item->masked || !item->Selectable()) return false;
    00219         value = item->result;
    00220         return true;
    00221       }
    00222
    00223       y -= item_height;
    00224     }
    00225
    00226     return false;
    00227   }
    00228
    00229   virtual void DrawWidget(const Rect &r, int widget) const
    00230   {
    00231     if (widget != WID_DM_ITEMS) return;
    00232
    00233     TextColour colour = (TextColour)this->GetWidget<NWidgetCore>(widget)->colour;
    00234
    00235     int y = r.top + 2;
    00236     int pos = this->vscroll->GetPosition();
    00237     for (DropDownList::const_iterator it = this->list->begin(); it != this->list->end(); ++it) {
    00238       const DropDownListItem *item = *it;
    00239       int item_height = item->Height(r.right - r.left + 1);
    00240
    00241       /* Skip items that are scrolled up */
    00242       if (--pos >= 0) continue;
    00243
    00244       if (y + item_height < r.bottom) {
    00245         bool selected = (this->selected_index == item->result);
    00246         if (selected) GfxFillRect(r.left + 2, y, r.right - 1, y + item_height - 1, PC_BLACK);
    00247
    00248         item->Draw(r.left, r.right, y, y + item_height, selected, colour);
    00249
    00250         if (item->masked) {
    00251           GfxFillRect(r.left + 1, y, r.right - 1, y + item_height - 1, _colour_gradient[colour][5], FILLRECT_CHECKER);
    00252         }
    00253       }
    00254       y += item_height;
    00255     }
    00256   }
    00257
    00258   virtual void OnClick(Point pt, int widget, int click_count)
    00259   {
    00260     if (widget != WID_DM_ITEMS) return;
    00261     int item;
    00262     if (this->GetDropDownItem(item)) {
    00263       this->click_delay = 4;
    00264       this->selected_index = item;
    00265       this->SetDirty();
    00266     }
    00267   }
    00268
    00269   virtual void OnTick()
    00270   {
    00271     if (this->scrolling != 0) {
    00272       int pos = this->vscroll->GetPosition();
    00273
    00274       this->vscroll->UpdatePosition(this->scrolling);
    00275       this->scrolling = 0;
    00276
    00277       if (pos != this->vscroll->GetPosition()) {
    00278         this->SetDirty();
    00279       }
    00280     }
    00281   }
    00282
    00283   virtual void OnMouseLoop()
    00284   {
    00285     Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
    00286     if (w2 == NULL) {
    00287       delete this;
    00288       return;
    00289     }
    00290
    00291     if (this->click_delay != 0 && --this->click_delay == 0) {
    00292       /* Make the dropdown "invisible", so it doesn't affect new window placement.
    00293        * Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
    00294       this->window_class = WC_INVALID;
    00295       this->SetDirty();
    00296
    00297       w2->OnDropdownSelect(this->parent_button, this->selected_index);
    00298       delete this;
    00299       return;
    00300     }
    00301
    00302     if (this->drag_mode) {
    00303       int item;
    00304
    00305       if (!_left_button_clicked) {
    00306         this->drag_mode = false;
    00307         if (!this->GetDropDownItem(item)) {
    00308           if (this->instant_close) {
    00309             /* Make the dropdown "invisible", so it doesn't affect new window placement.
    00310              * Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
    00311             this->window_class = WC_INVALID;
    00312             this->SetDirty();
    00313
    00314             if (GetWidgetFromPos(w2, _cursor.pos.x - w2->left, _cursor.pos.y - w2->top) == this->parent_button) {
    00315               /* Send event for selected option if we're still
    00316                * on the parent button of the list. */
    00317               w2->OnDropdownSelect(this->parent_button, this->selected_index);
    00318             }
    00319             delete this;
    00320           }
    00321           return;
    00322         }
    00323         this->click_delay = 2;
    00324       } else {
    00325         if (_cursor.pos.y <= this->top + 2) {
    00326           /* Cursor is above the list, set scroll up */
    00327           this->scrolling = -1;
    00328           return;
    00329         } else if (_cursor.pos.y >= this->top + this->height - 2) {
    00330           /* Cursor is below list, set scroll down */
    00331           this->scrolling = 1;
    00332           return;
    00333         }
    00334
    00335         if (!this->GetDropDownItem(item)) return;
    00336       }
    00337
    00338       if (this->selected_index != item) {
    00339         this->selected_index = item;
    00340         this->SetDirty();
    00341       }
    00342     }
    00343   }
    00344 };
    00345
    00346 void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close)
    00347 {
    00348   DeleteWindowById(WC_DROPDOWN_MENU, 0);
    00349
    00350   /* Our parent's button widget is used to determine where to place the drop
    00351    * down list window. */
    00352   Rect wi_rect;
    00353   Colours wi_colour;
    00354   NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
    00355   wi_rect.left   = nwi->pos_x;
    00356   wi_rect.right  = nwi->pos_x + nwi->current_x - 1;
    00357   wi_rect.top    = nwi->pos_y;
    00358   wi_rect.bottom = nwi->pos_y + nwi->current_y - 1;
    00359   wi_colour = nwi->colour;
    00360
    00361   if (nwi->type == NWID_BUTTON_DROPDOWN) {
    00362     nwi->disp_flags |= ND_DROPDOWN_ACTIVE;
    00363   } else {
    00364     w->LowerWidget(button);
    00365   }
    00366   w->SetWidgetDirty(button);
    00367
    00368   /* The preferred position is just below the dropdown calling widget */
    00369   int top = w->top + wi_rect.bottom + 1;
    00370
    00371   if (width == 0) width = wi_rect.right - wi_rect.left + 1;
    00372
    00373   uint max_item_width = 0;
    00374
    00375   if (auto_width) {
    00376     /* Find the longest item in the list */
    00377     for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
    00378       const DropDownListItem *item = *it;
    00379       max_item_width = max(max_item_width, item->Width() + 5);
    00380     }
    00381   }
    00382
    00383   /* Total length of list */
    00384   int list_height = 0;
    00385
    00386   for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
    00387     DropDownListItem *item = *it;
    00388     list_height += item->Height(width);
    00389   }
    00390
    00391   /* Height of window visible */
    00392   int height = list_height;
    00393
    00394   /* Check if the status bar is visible, as we don't want to draw over it */
    00395   int screen_bottom = GetMainViewBottom();
    00396   bool scroll = false;
    00397
    00398   /* Check if the dropdown will fully fit below the widget */
    00399   if (top + height + 4 >= screen_bottom) {
    00400     /* If not, check if it will fit above the widget */
    00401     if (w->top + wi_rect.top - height > GetMainViewTop()) {
    00402       top = w->top + wi_rect.top - height - 4;
    00403     } else {
    00404       /* ... and lastly if it won't, enable the scroll bar and fit the
    00405        * list in below the widget */
    00406       int avg_height = list_height / (int)list->size();
    00407       int rows = (screen_bottom - 4 - top) / avg_height;
    00408       height = rows * avg_height;
    00409       scroll = true;
    00410       /* Add space for the scroll bar if we automatically determined
    00411        * the width of the list. */
    00412       max_item_width += NWidgetScrollbar::GetVerticalDimension().width;
    00413     }
    00414   }
    00415
    00416   if (auto_width) width = max(width, max_item_width);
    00417
    00418   Point dw_pos = { w->left + (_current_text_dir == TD_RTL ? wi_rect.right + 1 - width : wi_rect.left), top};
    00419   Dimension dw_size = {width, height};
    00420   new DropdownWindow(w, list, selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll);
    00421 }
    00422
    00434 void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask, uint width)
    00435 {
    00436   DropDownList *list = new DropDownList();
    00437
    00438   for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
    00439     if (!HasBit(hidden_mask, i)) {
    00440       list->push_back(new DropDownListStringItem(strings[i], i, HasBit(disabled_mask, i)));
    00441     }
    00442   }
    00443
    00444   /* No entries in the list? */
    00445   if (list->size() == 0) {
    00446     DeleteDropDownList(list);
    00447     return;
    00448   }
    00449
    00450   ShowDropDownList(w, list, selected, button, width);
    00451 }
    00452
    00458 int HideDropDownMenu(Window *pw)
    00459 {
    00460   Window *w;
    00461   FOR_ALL_WINDOWS_FROM_BACK(w) {
    00462     if (w->window_class != WC_DROPDOWN_MENU) continue;
    00463
    00464     DropdownWindow *dw = dynamic_cast<DropdownWindow*>(w);
    00465     if (pw->window_class == dw->parent_wnd_class &&
    00466         pw->window_number == dw->parent_wnd_num) {
    00467       int parent_button = dw->parent_button;
    00468       delete dw;
    00469       return parent_button;
    00470     }
    00471   }
    00472
    00473   return -1;
    00474 }
    00475