# dropdown.cpp

Go to the documentation of this file.

    00001 /* $Id: dropdown.cpp 19927 2010-06-04 21:10:18Z rubidium $ */
    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 "../strings_func.h"
    00015 #include "../window_func.h"
    00016 #include "dropdown_type.h"
    00017
    00018
    00019 void DropDownListItem::Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
    00020 {
    00021   int c1 = _colour_gradient[bg_colour][3];
    00022   int c2 = _colour_gradient[bg_colour][7];
    00023
    00024   int mid = top + this->Height(0) / 2;
    00025   GfxFillRect(left + 1, mid - 2, right - 1, mid - 2, c1);
    00026   GfxFillRect(left + 1, mid - 1, right - 1, mid - 1, c2);
    00027 }
    00028
    00029 uint DropDownListStringItem::Width() const
    00030 {
    00031   char buffer[512];
    00032   GetString(buffer, this->String(), lastof(buffer));
    00033   return GetStringBoundingBox(buffer).width;
    00034 }
    00035
    00036 void DropDownListStringItem::Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
    00037 {
    00038   DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, this->String(), sel ? TC_WHITE : TC_BLACK);
    00039 }
    00040
    00041 StringID DropDownListParamStringItem::String() const
    00042 {
    00043   for (uint i = 0; i < lengthof(this->decode_params); i++) SetDParam(i, this->decode_params[i]);
    00044   return this->string;
    00045 }
    00046
    00047 uint DropDownListCharStringItem::Width() const
    00048 {
    00049   return GetStringBoundingBox(this->string).width;
    00050 }
    00051
    00052 void DropDownListCharStringItem::Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
    00053 {
    00054   DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, this->string, sel ? TC_WHITE : TC_BLACK);
    00055 }
    00056
    00061 static void DeleteDropDownList(DropDownList *list)
    00062 {
    00063   for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) {
    00064     DropDownListItem *item = *it;
    00065     delete item;
    00066   }
    00067   delete list;
    00068 }
    00069
    00071 enum DropdownMenuWidgets {
    00072   DDM_ITEMS,
    00073   DDM_SCROLL,
    00074 };
    00075
    00076 static const NWidgetPart _nested_dropdown_menu_widgets[] = {
    00077   NWidget(NWID_HORIZONTAL),
    00078     NWidget(WWT_PANEL, COLOUR_END, DDM_ITEMS), SetMinimalSize(1, 1), EndContainer(),
    00079     NWidget(WWT_SCROLLBAR, COLOUR_END, DDM_SCROLL),
    00080   EndContainer(),
    00081 };
    00082
    00083 const WindowDesc _dropdown_desc(
    00084   WDP_MANUAL, 0, 0,
    00085   WC_DROPDOWN_MENU, WC_NONE,
    00086   0,
    00087   _nested_dropdown_menu_widgets, lengthof(_nested_dropdown_menu_widgets)
    00088 );
    00089
    00091 struct DropdownWindow : Window {
    00092   WindowClass parent_wnd_class;
    00093   WindowNumber parent_wnd_num;
    00094   byte parent_button;
    00095   DropDownList *list;
    00096   int selected_index;
    00097   byte click_delay;
    00098   bool drag_mode;
    00099   bool instant_close;
    00100   int scrolling;
    00101   Point position;
    00102
    00115   DropdownWindow(Window *parent, DropDownList *list, int selected, int button, bool instant_close, const Point &position, const Dimension &size, Colours wi_colour, bool scroll) : Window()
    00116   {
    00117     this->position = position;
    00118
    00119     this->CreateNestedTree(&_dropdown_desc);
    00120
    00121     uint items_width = size.width - (scroll ? WD_VSCROLLBAR_WIDTH : 0);
    00122     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(DDM_ITEMS);
    00123     nwi->SetMinimalSize(items_width, size.height + 4);
    00124     nwi->colour = wi_colour;
    00125
    00126     nwi = this->GetWidget<NWidgetCore>(DDM_SCROLL);
    00127     if (scroll) {
    00128       nwi->colour = wi_colour;
    00129     } else {
    00130       nwi->min_x = 0; // Make scrollbar invisible.
    00131     }
    00132
    00133     this->FinishInitNested(&_dropdown_desc, 0);
    00134     this->flags4 &= ~WF_WHITE_BORDER_MASK;
    00135
    00136     /* Total length of list */
    00137     int list_height = 0;
    00138     for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
    00139       DropDownListItem *item = *it;
    00140       list_height += item->Height(items_width);
    00141     }
    00142
    00143     /* Capacity is the average number of items visible */
    00144     this->vscroll.SetCapacity(size.height * (uint16)list->size() / list_height);
    00145     this->vscroll.SetCount((uint16)list->size());
    00146
    00147     this->parent_wnd_class = parent->window_class;
    00148     this->parent_wnd_num   = parent->window_number;
    00149     this->parent_button    = button;
    00150     this->list             = list;
    00151     this->selected_index   = selected;
    00152     this->click_delay      = 0;
    00153     this->drag_mode        = true;
    00154     this->instant_close    = instant_close;
    00155   }
    00156
    00157   ~DropdownWindow()
    00158   {
    00159     Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
    00160     if (w2 != NULL) {
    00161       if (w2->nested_array != NULL) {
    00162         NWidgetCore *nwi2 = w2->GetWidget<NWidgetCore>(this->parent_button);
    00163         if (nwi2->type == NWID_BUTTON_DROPDOWN) {
    00164           nwi2->disp_flags &= ~ND_DROPDOWN_ACTIVE;
    00165         } else {
    00166           w2->RaiseWidget(this->parent_button);
    00167         }
    00168       } else {
    00169         w2->RaiseWidget(this->parent_button);
    00170       }
    00171       w2->SetWidgetDirty(this->parent_button);
    00172     }
    00173
    00174     DeleteDropDownList(this->list);
    00175   }
    00176
    00177   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00178   {
    00179     return this->position;
    00180   }
    00181
    00186   bool GetDropDownItem(int &value)
    00187   {
    00188     if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
    00189
    00190     NWidgetBase *nwi = this->GetWidget<NWidgetBase>(DDM_ITEMS);
    00191     int y     = _cursor.pos.y - this->top - nwi->pos_y - 2;
    00192     int width = nwi->current_x - 4;
    00193     int pos   = this->vscroll.GetPosition();
    00194
    00195     const DropDownList *list = this->list;
    00196
    00197     for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
    00198       /* Skip items that are scrolled up */
    00199       if (--pos >= 0) continue;
    00200
    00201       const DropDownListItem *item = *it;
    00202       int item_height = item->Height(width);
    00203
    00204       if (y < item_height) {
    00205         if (item->masked || !item->Selectable()) return false;
    00206         value = item->result;
    00207         return true;
    00208       }
    00209
    00210       y -= item_height;
    00211     }
    00212
    00213     return false;
    00214   }
    00215
    00216   virtual void OnPaint()
    00217   {
    00218     this->DrawWidgets();
    00219   }
    00220
    00221   virtual void DrawWidget(const Rect &r, int widget) const
    00222   {
    00223     if (widget != DDM_ITEMS) return;
    00224
    00225     TextColour colour = (TextColour)this->GetWidget<NWidgetCore>(widget)->colour;
    00226
    00227     int y = r.top + 2;
    00228     int pos = this->vscroll.GetPosition();
    00229     for (DropDownList::const_iterator it = this->list->begin(); it != this->list->end(); ++it) {
    00230       const DropDownListItem *item = *it;
    00231       int item_height = item->Height(r.right - r.left + 1);
    00232
    00233       /* Skip items that are scrolled up */
    00234       if (--pos >= 0) continue;
    00235
    00236       if (y + item_height < r.bottom) {
    00237         bool selected = (this->selected_index == item->result);
    00238         if (selected) GfxFillRect(r.left + 2, y, r.right - 1, y + item_height - 1, 0);
    00239
    00240         item->Draw(r.left, r.right, y, r.bottom, selected, colour);
    00241
    00242         if (item->masked) {
    00243           GfxFillRect(r.left + 1, y, r.right - 1, y + item_height - 1, _colour_gradient[colour][5], FILLRECT_CHECKER);
    00244         }
    00245       }
    00246       y += item_height;
    00247     }
    00248   }
    00249
    00250   virtual void OnClick(Point pt, int widget, int click_count)
    00251   {
    00252     if (widget != DDM_ITEMS) return;
    00253     int item;
    00254     if (this->GetDropDownItem(item)) {
    00255       this->click_delay = 4;
    00256       this->selected_index = item;
    00257       this->SetDirty();
    00258     }
    00259   }
    00260
    00261   virtual void OnTick()
    00262   {
    00263     if (this->scrolling != 0) {
    00264       int pos = this->vscroll.GetPosition();
    00265
    00266       this->vscroll.UpdatePosition(this->scrolling);
    00267       this->scrolling = 0;
    00268
    00269       if (pos != this->vscroll.GetPosition()) {
    00270         this->SetDirty();
    00271       }
    00272     }
    00273   }
    00274
    00275   virtual void OnMouseLoop()
    00276   {
    00277     Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
    00278     if (w2 == NULL) {
    00279       delete this;
    00280       return;
    00281     }
    00282
    00283     if (this->click_delay != 0 && --this->click_delay == 0) {
    00284       /* Make the dropdown "invisible", so it doesn't affect new window placement. */
    00285       this->window_class = WC_INVALID;
    00286
    00287       w2->OnDropdownSelect(this->parent_button, this->selected_index);
    00288       delete this;
    00289       return;
    00290     }
    00291
    00292     if (this->drag_mode) {
    00293       int item;
    00294
    00295       if (!_left_button_clicked) {
    00296         this->drag_mode = false;
    00297         if (!this->GetDropDownItem(item)) {
    00298           if (this->instant_close) {
    00299             /* Make the dropdown "invisible", so it doesn't affect new window placement. */
    00300             this->window_class = WC_INVALID;
    00301
    00302             if (GetWidgetFromPos(w2, _cursor.pos.x - w2->left, _cursor.pos.y - w2->top) == this->parent_button) {
    00303               /* Send event for selected option if we're still
    00304                * on the parent button of the list. */
    00305               w2->OnDropdownSelect(this->parent_button, this->selected_index);
    00306             }
    00307             delete this;
    00308           }
    00309           return;
    00310         }
    00311         this->click_delay = 2;
    00312       } else {
    00313         if (_cursor.pos.y <= this->top + 2) {
    00314           /* Cursor is above the list, set scroll up */
    00315           this->scrolling = -1;
    00316           return;
    00317         } else if (_cursor.pos.y >= this->top + this->height - 2) {
    00318           /* Cursor is below list, set scroll down */
    00319           this->scrolling = 1;
    00320           return;
    00321         }
    00322
    00323         if (!this->GetDropDownItem(item)) return;
    00324       }
    00325
    00326       if (this->selected_index != item) {
    00327         this->selected_index = item;
    00328         this->SetDirty();
    00329       }
    00330     }
    00331   }
    00332 };
    00333
    00334 void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close)
    00335 {
    00336   DeleteWindowById(WC_DROPDOWN_MENU, 0);
    00337
    00338   /* Our parent's button widget is used to determine where to place the drop
    00339    * down list window. */
    00340   Rect wi_rect;
    00341   Colours wi_colour;
    00342   NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
    00343   wi_rect.left   = nwi->pos_x;
    00344   wi_rect.right  = nwi->pos_x + nwi->current_x - 1;
    00345   wi_rect.top    = nwi->pos_y;
    00346   wi_rect.bottom = nwi->pos_y + nwi->current_y - 1;
    00347   wi_colour = nwi->colour;
    00348
    00349   if (nwi->type == NWID_BUTTON_DROPDOWN) {
    00350     nwi->disp_flags |= ND_DROPDOWN_ACTIVE;
    00351   } else {
    00352     w->LowerWidget(button);
    00353   }
    00354   w->SetWidgetDirty(button);
    00355
    00356   /* The preferred position is just below the dropdown calling widget */
    00357   int top = w->top + wi_rect.bottom + 1;
    00358
    00359   if (width == 0) width = wi_rect.right - wi_rect.left + 1;
    00360
    00361   uint max_item_width = 0;
    00362
    00363   if (auto_width) {
    00364     /* Find the longest item in the list */
    00365     for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
    00366       const DropDownListItem *item = *it;
    00367       max_item_width = max(max_item_width, item->Width() + 5);
    00368     }
    00369   }
    00370
    00371   /* Total length of list */
    00372   int list_height = 0;
    00373
    00374   for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
    00375     DropDownListItem *item = *it;
    00376     list_height += item->Height(width);
    00377   }
    00378
    00379   /* Height of window visible */
    00380   int height = list_height;
    00381
    00382   /* Check if the status bar is visible, as we don't want to draw over it */
    00383   int screen_bottom = GetMainViewBottom();
    00384   bool scroll = false;
    00385
    00386   /* Check if the dropdown will fully fit below the widget */
    00387   if (top + height + 4 >= screen_bottom) {
    00388     /* If not, check if it will fit above the widget */
    00389     if (w->top + wi_rect.top - height > GetMainViewTop()) {
    00390       top = w->top + wi_rect.top - height - 4;
    00391     } else {
    00392       /* ... and lastly if it won't, enable the scroll bar and fit the
    00393        * list in below the widget */
    00394       int avg_height = list_height / (int)list->size();
    00395       int rows = (screen_bottom - 4 - top) / avg_height;
    00396       height = rows * avg_height;
    00397       scroll = true;
    00398       /* Add space for the scroll bar if we automatically determined
    00399        * the width of the list. */
    00400       max_item_width += WD_VSCROLLBAR_WIDTH;
    00401     }
    00402   }
    00403
    00404   if (auto_width) width = max(width, max_item_width);
    00405
    00406   Point dw_pos = { w->left + (_dynlang.text_dir == TD_RTL ? wi_rect.right + 1 - width : wi_rect.left), top};
    00407   Dimension dw_size = {width, height};
    00408   new DropdownWindow(w, list, selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll);
    00409 }
    00410
    00421 void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask, uint width)
    00422 {
    00423   DropDownList *list = new DropDownList();
    00424
    00425   for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
    00426     if (!HasBit(hidden_mask, i)) {
    00427       list->push_back(new DropDownListStringItem(strings[i], i, HasBit(disabled_mask, i)));
    00428     }
    00429   }
    00430
    00431   /* No entries in the list? */
    00432   if (list->size() == 0) {
    00433     DeleteDropDownList(list);
    00434     return;
    00435   }
    00436
    00437   ShowDropDownList(w, list, selected, button, width);
    00438 }
    00439
    00445 int HideDropDownMenu(Window *pw)
    00446 {
    00447   Window *w;
    00448   FOR_ALL_WINDOWS_FROM_BACK(w) {
    00449     if (w->window_class != WC_DROPDOWN_MENU) continue;
    00450
    00451     DropdownWindow *dw = dynamic_cast<DropdownWindow*>(w);
    00452     if (pw->window_class == dw->parent_wnd_class &&
    00453         pw->window_number == dw->parent_wnd_num) {
    00454       int parent_button = dw->parent_button;
    00455       delete dw;
    00456       return parent_button;
    00457     }
    00458   }
    00459
    00460   return -1;
    00461 }
    00462