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

Generated on Wed Dec 30 20:40:09 2009 for OpenTTD by  doxygen 1.5.6