# dropdown.cpp

    00001 /* $Id: dropdown.cpp 12121 2008-02-12 12:54:51Z peter1138 $ */
    00002
    00003 #include "../stdafx.h"
    00004 #include "../openttd.h"
    00005 #include "../strings_type.h"
    00006 #include "../window_gui.h"
    00007 #include "../strings_func.h"
    00008 #include "../strings_type.h"
    00009 #include "../gfx_func.h"
    00010 #include "../window_func.h"
    00011 #include "../core/math_func.hpp"
    00012 #include "dropdown_type.h"
    00013 #include "dropdown_func.h"
    00014
    00015 #include "../table/sprites.h"
    00016 #include "table/strings.h"
    00017
    00018 StringID DropDownListItem::String() const
    00019 {
    00020   return STR_NULL;
    00021 }
    00022
    00023 StringID DropDownListStringItem::String() const
    00024 {
    00025   return this->string;
    00026 }
    00027
    00028 StringID DropDownListParamStringItem::String() const
    00029 {
    00030   for (uint i = 0; i < lengthof(this->decode_params); i++) SetDParam(i, this->decode_params[i]);
    00031   return this->string;
    00032 }
    00033
    00038 static void DeleteDropDownList(DropDownList *list)
    00039 {
    00040   for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) {
    00041     DropDownListItem *item = *it;
    00042     delete item;
    00043   }
    00044   delete list;
    00045 }
    00046
    00047 struct dropdown_d {
    00048   WindowClass parent_wnd_class;
    00049   WindowNumber parent_wnd_num;
    00050   byte parent_button;
    00051   DropDownList *list;
    00052   int selected_index;
    00053   byte click_delay;
    00054   bool drag_mode;
    00055   int scrolling;
    00056 };
    00057 assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(dropdown_d));
    00058
    00059 static const Widget _dropdown_menu_widgets[] = {
    00060 {      WWT_PANEL,   RESIZE_NONE,     0,     0, 0,     0, 0, 0x0, STR_NULL},
    00061 {  WWT_SCROLLBAR,   RESIZE_NONE,     0,     0, 0,     0, 0, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
    00062 {   WIDGETS_END},
    00063 };
    00064
    00065 static int GetDropDownItem(const Window *w)
    00066 {
    00067   if (GetWidgetFromPos(w, _cursor.pos.x - w->left, _cursor.pos.y - w->top) < 0) return -1;
    00068
    00069   int y = _cursor.pos.y - w->top - 2 + w->vscroll.pos * 10;
    00070   if (y < 0) return -1;
    00071
    00072   uint selected_row = y / 10;
    00073   const DropDownList *list = WP(w, dropdown_d).list;
    00074
    00075   if (selected_row >= list->size()) return -1;
    00076
    00077   for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it, selected_row--) {
    00078     if (selected_row == 0) {
    00079       const DropDownListItem *item = *it;
    00080       if (item->masked || item->String() == STR_NULL) return -1;
    00081       return item->result;
    00082     }
    00083   }
    00084
    00085   return -1;
    00086 }
    00087
    00088 static void DropDownMenuWndProc(Window *w, WindowEvent *e)
    00089 {
    00090   switch (e->event) {
    00091     case WE_PAINT: {
    00092       DrawWindowWidgets(w);
    00093
    00094       int x = 1;
    00095       int y = 2 - w->vscroll.pos * 10;
    00096
    00097       int sel    = WP(w, dropdown_d).selected_index;
    00098       int width  = w->widget[0].right - 3;
    00099       int height = w->widget[0].bottom - 3;
    00100
    00101       DropDownList *list = WP(w, dropdown_d).list;
    00102
    00103       for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
    00104         if (y >= 0 && y <= height) {
    00105           const DropDownListItem *item = *it;
    00106           if (item->String() != STR_NULL) {
    00107             if (sel == item->result) GfxFillRect(x + 1, y, x + width, y + 9, 0);
    00108
    00109             DrawStringTruncated(x + 2, y, item->String(), sel == item->result ? TC_WHITE : TC_BLACK, x + width);
    00110
    00111             if (item->masked) {
    00112               GfxFillRect(x, y, x + width, y + 9,
    00113                 (1 << PALETTE_MODIFIER_GREYOUT) | _colour_gradient[w->widget[0].color][5]
    00114               );
    00115             }
    00116           } else {
    00117             int c1 = _colour_gradient[w->widget[0].color][3];
    00118             int c2 = _colour_gradient[w->widget[0].color][7];
    00119
    00120             GfxFillRect(x + 1, y + 3, x + w->width - 5, y + 3, c1);
    00121             GfxFillRect(x + 1, y + 4, x + w->width - 5, y + 4, c2);
    00122           }
    00123         }
    00124         y += 10;
    00125       }
    00126     } break;
    00127
    00128     case WE_CLICK: {
    00129       if (e->we.click.widget != 0) break;
    00130       int item = GetDropDownItem(w);
    00131       if (item >= 0) {
    00132         WP(w, dropdown_d).click_delay = 4;
    00133         WP(w, dropdown_d).selected_index = item;
    00134         SetWindowDirty(w);
    00135       }
    00136     } break;
    00137
    00138     case WE_TICK:
    00139       if (WP(w, dropdown_d).scrolling == -1) {
    00140         w->vscroll.pos = max(0, w->vscroll.pos - 1);
    00141         SetWindowDirty(w);
    00142       } else if (WP(w, dropdown_d).scrolling == 1) {
    00143         w->vscroll.pos = min(w->vscroll.count - w->vscroll.cap, w->vscroll.pos + 1);
    00144         SetWindowDirty(w);
    00145       }
    00146       WP(w, dropdown_d).scrolling = 0;
    00147       break;
    00148
    00149     case WE_MOUSELOOP: {
    00150       Window *w2 = FindWindowById(WP(w, dropdown_d).parent_wnd_class, WP(w,dropdown_d).parent_wnd_num);
    00151       if (w2 == NULL) {
    00152         DeleteWindow(w);
    00153         return;
    00154       }
    00155
    00156       if (WP(w, dropdown_d).click_delay != 0 && --WP(w,dropdown_d).click_delay == 0) {
    00157         WindowEvent e;
    00158         e.event = WE_DROPDOWN_SELECT;
    00159         e.we.dropdown.button = WP(w, dropdown_d).parent_button;
    00160         e.we.dropdown.index  = WP(w, dropdown_d).selected_index;
    00161         w2->wndproc(w2, &e);
    00162         DeleteWindow(w);
    00163         return;
    00164       }
    00165
    00166       if (WP(w, dropdown_d).drag_mode) {
    00167         int item = GetDropDownItem(w);
    00168
    00169         if (!_left_button_clicked) {
    00170           WP(w, dropdown_d).drag_mode = false;
    00171           if (item < 0) return;
    00172           WP(w, dropdown_d).click_delay = 2;
    00173         } else {
    00174           if (_cursor.pos.y <= w->top + 2) {
    00175             /* Cursor is above the list, set scroll up */
    00176             WP(w, dropdown_d).scrolling = -1;
    00177             return;
    00178           } else if (_cursor.pos.y >= w->top + w->height - 2) {
    00179             /* Cursor is below list, set scroll down */
    00180             WP(w, dropdown_d).scrolling = 1;
    00181             return;
    00182           }
    00183
    00184           if (item < 0) return;
    00185         }
    00186
    00187         WP(w, dropdown_d).selected_index = item;
    00188         SetWindowDirty(w);
    00189       }
    00190     } break;
    00191
    00192     case WE_DESTROY: {
    00193       Window *w2 = FindWindowById(WP(w, dropdown_d).parent_wnd_class, WP(w,dropdown_d).parent_wnd_num);
    00194       if (w2 != NULL) {
    00195         w2->RaiseWidget(WP(w, dropdown_d).parent_button);
    00196         w2->InvalidateWidget(WP(w, dropdown_d).parent_button);
    00197       }
    00198
    00199       DeleteDropDownList(WP(w, dropdown_d).list);
    00200     } break;
    00201   }
    00202 }
    00203
    00204 void ShowDropDownList(Window *w, DropDownList *list, int selected, int button)
    00205 {
    00206   bool is_dropdown_menu_shown = w->IsWidgetLowered(button);
    00207
    00208   DeleteWindowById(WC_DROPDOWN_MENU, 0);
    00209
    00210   if (is_dropdown_menu_shown) {
    00211     DeleteDropDownList(list);
    00212     return;
    00213   }
    00214
    00215   w->LowerWidget(button);
    00216   w->InvalidateWidget(button);
    00217
    00218   /* Our parent's button widget is used to determine where to place the drop
    00219    * down list window. */
    00220   const Widget *wi = &w->widget[button];
    00221
    00222   /* The preferred position is just below the dropdown calling widget */
    00223   int top = w->top + wi->bottom + 1;
    00224   int height = list->size() * 10 + 4;
    00225
    00226   /* Check if the status bar is visible, as we don't want to draw over it */
    00227   Window *w3 = FindWindowById(WC_STATUS_BAR, 0);
    00228   int screen_bottom = w3 == NULL ? _screen.height : w3->top;
    00229
    00230   bool scroll = false;
    00231
    00232   /* Check if the dropdown will fully fit below the widget */
    00233   if (top + height >= screen_bottom) {
    00234     w3 = FindWindowById(WC_MAIN_TOOLBAR, 0);
    00235     int screen_top = w3 == NULL ? 0 : w3->top + w3->height;
    00236
    00237     /* If not, check if it will fit above the widget */
    00238     if (w->top + wi->top - height > screen_top) {
    00239       top = w->top + wi->top - height;
    00240     } else {
    00241       /* ... and lastly if it won't, enable the scroll bar and fit the
    00242        * list in below the widget */
    00243       int rows = (screen_bottom - 4 - top) / 10;
    00244       height = rows * 10 + 4;
    00245       scroll = true;
    00246     }
    00247   }
    00248
    00249   Window *dw = AllocateWindow(
    00250     w->left + wi->left,
    00251     top,
    00252     wi->right - wi->left + 1,
    00253     height,
    00254     DropDownMenuWndProc,
    00255     WC_DROPDOWN_MENU,
    00256     _dropdown_menu_widgets);
    00257
    00258   dw->widget[0].color = wi->color;
    00259   dw->widget[0].right = wi->right - wi->left;
    00260   dw->widget[0].bottom = height - 1;
    00261
    00262   dw->SetWidgetHiddenState(1, !scroll);
    00263
    00264   if (scroll) {
    00265     /* We're scrolling, so enable the scroll bar and shrink the list by
    00266      * the scrollbar's width */
    00267     dw->widget[1].color  = wi->color;
    00268     dw->widget[1].right  = dw->widget[0].right;
    00269     dw->widget[1].left   = dw->widget[1].right - 11;
    00270     dw->widget[1].bottom = height - 1;
    00271     dw->widget[0].right -= 12;
    00272
    00273     dw->vscroll.cap   = (height - 4) / 10;
    00274     dw->vscroll.count = list->size();
    00275   }
    00276
    00277   dw->desc_flags = WDF_DEF_WIDGET;
    00278   dw->flags4 &= ~WF_WHITE_BORDER_MASK;
    00279
    00280   WP(dw, dropdown_d).parent_wnd_class = w->window_class;
    00281   WP(dw, dropdown_d).parent_wnd_num   = w->window_number;
    00282   WP(dw, dropdown_d).parent_button    = button;
    00283   WP(dw, dropdown_d).list             = list;
    00284   WP(dw, dropdown_d).selected_index   = selected;
    00285   WP(dw, dropdown_d).click_delay      = 0;
    00286   WP(dw, dropdown_d).drag_mode        = true;
    00287 }
    00288
    00289 void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask)
    00290 {
    00291   /* Don't create a new list if we're just closing an existing menu */
    00292   if (w->IsWidgetLowered(button)) {
    00293     DeleteWindowById(WC_DROPDOWN_MENU, 0);
    00294     return;
    00295   }
    00296
    00297   uint result = 0;
    00298   DropDownList *list = new DropDownList();
    00299
    00300   for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
    00301     if (!HasBit(hidden_mask, i)) {
    00302       list->push_back(new DropDownListStringItem(strings[i], result, HasBit(disabled_mask, i)));
    00303     }
    00304     result++;
    00305   }
    00306
    00307   /* No entries in the list? */
    00308   if (list->size() == 0) {
    00309     DeleteDropDownList(list);
    00310     return;
    00311   }
    00312
    00313   ShowDropDownList(w, list, selected, button);
    00314 }
    00315
    00316 void HideDropDownMenu(Window *pw)
    00317 {
    00318   Window **wz;
    00319   FOR_ALL_WINDOWS(wz) {
    00320     if ((*wz)->window_class != WC_DROPDOWN_MENU) continue;
    00321
    00322     if (pw->window_class == WP(*wz, dropdown_d).parent_wnd_class &&
    00323         pw->window_number == WP(*wz, dropdown_d).parent_wnd_num) {
    00324       DeleteWindow(*wz);
    00325       break;
    00326     }
    00327   }
    00328 }
    00329