# window_gui.h

Go to the documentation of this file.

    00001 /* $Id: window_gui.h 18809 2010-01-15 16:41:15Z 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 #ifndef WINDOW_GUI_H
    00013 #define WINDOW_GUI_H
    00014
    00015 #include "core/math_func.hpp"
    00016 #include "vehicle_type.h"
    00017 #include "viewport_type.h"
    00018 #include "company_type.h"
    00019 #include "tile_type.h"
    00020 #include "widget_type.h"
    00021
    00025 enum FrameFlags {
    00026   FR_NONE         =  0,
    00027   FR_TRANSPARENT  =  1 << 0,
    00028   FR_BORDERONLY   =  1 << 4,
    00029   FR_LOWERED      =  1 << 5,
    00030   FR_DARKENED     =  1 << 6,
    00031 };
    00032
    00033 DECLARE_ENUM_AS_BIT_SET(FrameFlags);
    00034
    00036 enum WidgetDrawDistances {
    00037   /* WWT_IMGBTN(_2) */
    00038   WD_IMGBTN_LEFT    = 1,
    00039   WD_IMGBTN_RIGHT   = 2,
    00040   WD_IMGBTN_TOP     = 1,
    00041   WD_IMGBTN_BOTTOM  = 2,
    00042
    00043   /* WWT_INSET */
    00044   WD_INSET_LEFT  = 2,
    00045   WD_INSET_RIGHT = 2,
    00046   WD_INSET_TOP   = 1,
    00047
    00048   WD_VSCROLLBAR_WIDTH  = 12,
    00049
    00050   WD_HSCROLLBAR_HEIGHT = 12,
    00051
    00052   /* FrameRect widgets, all text buttons, panel, editbox */
    00053   WD_FRAMERECT_LEFT   = 2,
    00054   WD_FRAMERECT_RIGHT  = 2,
    00055   WD_FRAMERECT_TOP    = 1,
    00056   WD_FRAMERECT_BOTTOM = 1,
    00057
    00058   /* Extra space at top/bottom of text panels */
    00059   WD_TEXTPANEL_TOP    = 6,
    00060   WD_TEXTPANEL_BOTTOM = 6,
    00061
    00062   /* WWT_FRAME */
    00063   WD_FRAMETEXT_LEFT   = 6,
    00064   WD_FRAMETEXT_RIGHT  = 6,
    00065   WD_FRAMETEXT_TOP    = 6,
    00066   WD_FRAMETEXT_BOTTOM = 6,
    00067
    00068   /* WWT_MATRIX */
    00069   WD_MATRIX_LEFT   = 2,
    00070   WD_MATRIX_RIGHT  = 2,
    00071   WD_MATRIX_TOP    = 3,
    00072   WD_MATRIX_BOTTOM = 1,
    00073
    00074   /* WWT_SHADEBOX */
    00075   WD_SHADEBOX_WIDTH  = 12,
    00076   WD_SHADEBOX_LEFT   = 2,
    00077   WD_SHADEBOX_RIGHT  = 2,
    00078   WD_SHADEBOX_TOP    = 3,
    00079   WD_SHADEBOX_BOTTOM = 3,
    00080
    00081   /* WWT_STICKYBOX */
    00082   WD_STICKYBOX_WIDTH  = 12,
    00083   WD_STICKYBOX_LEFT   = 2,
    00084   WD_STICKYBOX_RIGHT  = 2,
    00085   WD_STICKYBOX_TOP    = 3,
    00086   WD_STICKYBOX_BOTTOM = 3,
    00087
    00088   /* WWT_RESIZEBOX */
    00089   WD_RESIZEBOX_WIDTH  = 12,
    00090   WD_RESIZEBOX_LEFT   = 3,
    00091   WD_RESIZEBOX_RIGHT  = 2,
    00092   WD_RESIZEBOX_TOP    = 3,
    00093   WD_RESIZEBOX_BOTTOM = 2,
    00094
    00095   /* WWT_CLOSEBOX */
    00096   WD_CLOSEBOX_WIDTH  = 11,
    00097   WD_CLOSEBOX_LEFT   = 2,
    00098   WD_CLOSEBOX_RIGHT  = 1,
    00099   WD_CLOSEBOX_TOP    = 2,
    00100   WD_CLOSEBOX_BOTTOM = 2,
    00101
    00102   /* WWT_CAPTION */
    00103   WD_CAPTION_HEIGHT     = 14,
    00104   WD_CAPTIONTEXT_LEFT   = 2,
    00105   WD_CAPTIONTEXT_RIGHT  = 2,
    00106   WD_CAPTIONTEXT_TOP    = 2,
    00107   WD_CAPTIONTEXT_BOTTOM = 2,
    00108
    00109   /* Dropdown widget. */
    00110   WD_DROPDOWN_HEIGHT     = 12,
    00111   WD_DROPDOWNTEXT_LEFT   = 2,
    00112   WD_DROPDOWNTEXT_RIGHT  = 14,
    00113   WD_DROPDOWNTEXT_TOP    = 1,
    00114   WD_DROPDOWNTEXT_BOTTOM = 1,
    00115
    00116   WD_SORTBUTTON_ARROW_WIDTH = 11,
    00117
    00118   WD_PAR_VSEP_NORMAL = 2,
    00119   WD_PAR_VSEP_WIDE   = 8,
    00120 };
    00121
    00122 /* wiget.cpp */
    00123 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
    00124
    00125 /* window.cpp */
    00126 extern Window *_z_front_window;
    00127 extern Window *_z_back_window;
    00128 extern Window *_focused_window;
    00129
    00130
    00132 enum WindowPosition {
    00133   WDP_MANUAL,
    00134   WDP_AUTO,
    00135   WDP_CENTER,
    00136   WDP_ALIGN_TOOLBAR,
    00137 };
    00138
    00139 Point GetToolbarAlignedWindowPosition(int window_width);
    00140
    00144 struct WindowDesc : ZeroedMemoryAllocator {
    00145
    00146   WindowDesc(WindowPosition default_pos, int16 def_width, int16 def_height,
    00147       WindowClass window_class, WindowClass parent_class, uint32 flags,
    00148       const NWidgetPart *nwid_parts, int16 nwid_length);
    00149
    00150   ~WindowDesc();
    00151
    00152   WindowPosition default_pos;
    00153   int16 default_width;
    00154   int16 default_height;
    00155   WindowClass cls;
    00156   WindowClass parent_cls;
    00157   uint32 flags;
    00158   const NWidgetPart *nwid_parts;
    00159   int16 nwid_length;
    00160 };
    00161
    00165 enum WindowDefaultFlag {
    00166   WDF_CONSTRUCTION    =   1 << 0,
    00167   WDF_UNCLICK_BUTTONS =   1 << 1,
    00168   WDF_MODAL           =   1 << 2,
    00169   WDF_NO_FOCUS        =   1 << 3,
    00170 };
    00171
    00175 class Scrollbar {
    00176 private:
    00177   const bool is_vertical;
    00178   uint16 count;
    00179   uint16 cap;
    00180   uint16 pos;
    00181
    00182 public:
    00183   Scrollbar(bool is_vertical) : is_vertical(is_vertical)
    00184   {
    00185   }
    00186
    00191   FORCEINLINE uint16 GetCount() const
    00192   {
    00193     return this->count;
    00194   }
    00195
    00200   FORCEINLINE uint16 GetCapacity() const
    00201   {
    00202     return this->cap;
    00203   }
    00204
    00209   FORCEINLINE uint16 GetPosition() const
    00210   {
    00211     return this->pos;
    00212   }
    00213
    00219   FORCEINLINE bool IsVisible(uint16 item) const
    00220   {
    00221     return IsInsideBS(item, this->GetPosition(), this->GetCapacity());
    00222   }
    00223
    00229   void SetCount(int num)
    00230   {
    00231     assert(num >= 0);
    00232     assert(num <= MAX_UVALUE(uint16));
    00233
    00234     this->count = num;
    00235     num -= this->cap;
    00236     if (num < 0) num = 0;
    00237     if (num < this->pos) this->pos = num;
    00238   }
    00239
    00245   void SetCapacity(int capacity)
    00246   {
    00247     assert(capacity > 0);
    00248     assert(capacity <= MAX_UVALUE(uint16));
    00249
    00250     this->cap = capacity;
    00251     if (this->cap + this->pos > this->count) this->pos = max(0, this->count - this->cap);
    00252   }
    00253
    00254   void SetCapacityFromWidget(Window *w, int widget, int padding = 0);
    00255
    00260   void SetPosition(int position)
    00261   {
    00262     assert(position >= 0);
    00263     assert(this->count <= this->cap ? (position == 0) : (position + this->cap <= this->count));
    00264     this->pos = position;
    00265   }
    00266
    00272   void UpdatePosition(int difference)
    00273   {
    00274     if (difference == 0) return;
    00275     this->SetPosition(Clamp(this->pos + difference, 0, max(this->count - this->cap, 0)));
    00276   }
    00277
    00284   void ScrollTowards(int position)
    00285   {
    00286     if (position < this->GetPosition()) {
    00287       /* scroll up to the item */
    00288       this->SetPosition(position);
    00289     } else if (position >= this->GetPosition() + this->GetCapacity()) {
    00290       /* scroll down so that the item is at the bottom */
    00291       this->SetPosition(position - this->GetCapacity() + 1);
    00292     }
    00293   }
    00294 };
    00295
    00299 struct ResizeInfo {
    00300   uint step_width;
    00301   uint step_height;
    00302 };
    00303
    00305 enum SortButtonState {
    00306   SBS_OFF,
    00307   SBS_DOWN,
    00308   SBS_UP,
    00309 };
    00310
    00318 struct ViewportData : ViewPort {
    00319   VehicleID follow_vehicle;
    00320   int32 scrollpos_x;
    00321   int32 scrollpos_y;
    00322   int32 dest_scrollpos_x;
    00323   int32 dest_scrollpos_y;
    00324 };
    00325
    00329 struct Window : ZeroedMemoryAllocator {
    00331   enum EventState {
    00332     ES_HANDLED,
    00333     ES_NOT_HANDLED,
    00334   };
    00335
    00336 protected:
    00337   void InitializeData(WindowClass cls, int window_number, uint32 desc_flags);
    00338   void InitializePositionSize(int x, int y, int min_width, int min_height);
    00339   void FindWindowPlacementAndResize(int def_width, int def_height);
    00340
    00341 public:
    00342   Window();
    00343
    00344   virtual ~Window();
    00345
    00352   FORCEINLINE void *operator new[](size_t size)
    00353   {
    00354     NOT_REACHED();
    00355   }
    00356
    00362   FORCEINLINE void operator delete(void *ptr)
    00363   {
    00364   }
    00365
    00366   uint16 flags4;
    00367   WindowClass window_class;
    00368   WindowNumber window_number;
    00369
    00370   int left;
    00371   int top;
    00372   int width;
    00373   int height;
    00374
    00375   Scrollbar hscroll;
    00376   Scrollbar vscroll;
    00377   Scrollbar vscroll2;
    00378   ResizeInfo resize;
    00379
    00380   Owner owner;
    00381
    00382   ViewportData *viewport;
    00383   uint32 desc_flags;
    00384   const NWidgetCore *nested_focus;
    00385   NWidgetBase *nested_root;
    00386   NWidgetBase **nested_array;
    00387   uint nested_array_size;
    00388   NWidgetStacked *shade_select;
    00389   Dimension unshaded_size;
    00390
    00391   Window *parent;
    00392   Window *z_front;
    00393   Window *z_back;
    00394
    00395   template <class NWID>
    00396   inline const NWID *GetWidget(uint widnum) const;
    00397   template <class NWID>
    00398   inline NWID *GetWidget(uint widnum);
    00399
    00400
    00401   void InitNested(const WindowDesc *desc, WindowNumber number = 0);
    00402   void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
    00403   void FinishInitNested(const WindowDesc *desc, WindowNumber window_number);
    00404
    00412   inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
    00413   {
    00414     assert(widget_index < this->nested_array_size);
    00415     if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
    00416   }
    00417
    00422   inline void DisableWidget(byte widget_index)
    00423   {
    00424     SetWidgetDisabledState(widget_index, true);
    00425   }
    00426
    00431   inline void EnableWidget(byte widget_index)
    00432   {
    00433     SetWidgetDisabledState(widget_index, false);
    00434   }
    00435
    00441   inline bool IsWidgetDisabled(byte widget_index) const
    00442   {
    00443     assert(widget_index < this->nested_array_size);
    00444     return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
    00445   }
    00446
    00452   inline bool IsWidgetFocused(byte widget_index) const
    00453   {
    00454     return this->nested_focus != NULL && this->nested_focus->index == widget_index;
    00455   }
    00456
    00463   inline bool IsWidgetGloballyFocused(byte widget_index) const
    00464   {
    00465     return _focused_window == this && IsWidgetFocused(widget_index);
    00466   }
    00467
    00473   inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
    00474   {
    00475     assert(widget_index < this->nested_array_size);
    00476     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
    00477   }
    00478
    00483   inline void ToggleWidgetLoweredState(byte widget_index)
    00484   {
    00485     assert(widget_index < this->nested_array_size);
    00486     bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
    00487     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
    00488   }
    00489
    00494   inline void LowerWidget(byte widget_index)
    00495   {
    00496     SetWidgetLoweredState(widget_index, true);
    00497   }
    00498
    00503   inline void RaiseWidget(byte widget_index)
    00504   {
    00505     SetWidgetLoweredState(widget_index, false);
    00506   }
    00507
    00513   inline bool IsWidgetLowered(byte widget_index) const
    00514   {
    00515     assert(widget_index < this->nested_array_size);
    00516     return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
    00517   }
    00518
    00519   void UnfocusFocusedWidget();
    00520   bool SetFocusedWidget(byte widget_index);
    00521
    00522   void HandleButtonClick(byte widget);
    00523
    00524   void RaiseButtons(bool autoraise = false);
    00525   void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
    00526   void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
    00527   void SetWidgetDirty(byte widget_index) const;
    00528
    00529   void DrawWidgets() const;
    00530   void DrawViewport() const;
    00531   void DrawSortButtonState(int widget, SortButtonState state) const;
    00532
    00533   void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
    00534
    00535   void SetDirty() const;
    00536   void ReInit(int rx = 0, int ry = 0);
    00537
    00539   inline bool IsShaded() const
    00540   {
    00541     return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
    00542   }
    00543
    00544   void SetShaded(bool make_shaded);
    00545
    00550   void InvalidateData(int data = 0)
    00551   {
    00552     this->SetDirty();
    00553     this->OnInvalidateData(data);
    00554   }
    00555
    00556   /*** Event handling ***/
    00557
    00562   virtual void OnInit() { }
    00563
    00572   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
    00573
    00578   virtual void OnPaint() {}
    00579
    00586   virtual void DrawWidget(const Rect &r, int widget) const {}
    00587
    00600   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
    00601
    00608   virtual void SetStringParameters(int widget) const {}
    00609
    00613   virtual void OnFocus() {}
    00614
    00618   virtual void OnFocusLost() {}
    00619
    00627   virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
    00628
    00634   virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
    00635
    00636
    00642   virtual void OnClick(Point pt, int widget) {}
    00643
    00649   virtual void OnDoubleClick(Point pt, int widget) {}
    00650
    00656   virtual void OnRightClick(Point pt, int widget) {}
    00657
    00663   virtual void OnDragDrop(Point pt, int widget) {}
    00664
    00669   virtual void OnScroll(Point delta) {}
    00670
    00677   virtual void OnMouseOver(Point pt, int widget) {}
    00678
    00683   virtual void OnMouseWheel(int wheel) {}
    00684
    00685
    00689   virtual void OnMouseLoop() {}
    00690
    00694   virtual void OnTick() {}
    00695
    00699   virtual void OnHundredthTick() {}
    00700
    00704   virtual void OnTimeout() {}
    00705
    00706
    00711   virtual void OnResize() {}
    00712
    00718   virtual void OnDropdownSelect(int widget, int index) {}
    00719
    00725   virtual void OnQueryTextFinished(char *str) {}
    00726
    00731   virtual void OnInvalidateData(int data = 0) {}
    00732
    00733
    00740   virtual void OnPlaceObject(Point pt, TileIndex tile) {}
    00741
    00745   virtual void OnPlaceObjectAbort() {}
    00746
    00747
    00755   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
    00756
    00766   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
    00767
    00775   virtual void OnPlacePresize(Point pt, TileIndex tile) {}
    00776
    00777   /*** End of the event handling ***/
    00778 };
    00779
    00785 template <class NWID>
    00786 inline NWID *Window::GetWidget(uint widnum)
    00787 {
    00788   if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
    00789   NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
    00790   assert(nwid != NULL);
    00791   return nwid;
    00792 }
    00793
    00795 template <>
    00796 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
    00797 {
    00798   if (widnum >= this->nested_array_size) return NULL;
    00799   return this->nested_array[widnum];
    00800 }
    00801
    00807 template <class NWID>
    00808 inline const NWID *Window::GetWidget(uint widnum) const
    00809 {
    00810   return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
    00811 }
    00812
    00813
    00817 class PickerWindowBase : public Window {
    00818
    00819 public:
    00820   PickerWindowBase(Window *parent) : Window()
    00821   {
    00822     this->parent = parent;
    00823   };
    00824
    00825   virtual ~PickerWindowBase();
    00826 };
    00827
    00831 enum WindowFlags {
    00832   WF_TIMEOUT_TRIGGER   = 1,
    00833   WF_TIMEOUT_BEGIN     = 7,
    00834   WF_TIMEOUT_MASK      = 7,
    00835   WF_DRAGGING          = 1 <<  3,
    00836   WF_SCROLL_UP         = 1 <<  4,
    00837   WF_SCROLL_DOWN       = 1 <<  5,
    00838   WF_SCROLL_MIDDLE     = 1 <<  6,
    00839   WF_SCROLL2           = 1 <<  7,
    00840   WF_HSCROLL           = 1 <<  8,
    00841   WF_SIZING_RIGHT      = 1 <<  9,
    00842   WF_SIZING_LEFT       = 1 << 10,
    00843   WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT,
    00844   WF_STICKY            = 1 << 11,
    00845
    00846   WF_DISABLE_VP_SCROLL = 1 << 12,
    00847
    00848   WF_WHITE_BORDER_ONE  = 1 << 13,
    00849   WF_WHITE_BORDER_MASK = 1 << 14 | WF_WHITE_BORDER_ONE,
    00850 };
    00851
    00852 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
    00853 Window *FindWindowFromPt(int x, int y);
    00854
    00861 template <typename Wcls>
    00862 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
    00863 {
    00864   if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
    00865   return new Wcls(desc, window_number);
    00866 }
    00867
    00868 void RelocateAllWindows(int neww, int newh);
    00869
    00870 /* misc_gui.cpp */
    00871 void GuiShowTooltips(StringID str, uint paramcount = 0, const uint64 params[] = NULL, bool use_left_mouse_button = false);
    00872
    00873 /* widget.cpp */
    00874 int GetWidgetFromPos(const Window *w, int x, int y);
    00875
    00877 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)  for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
    00878 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
    00879 #define FOR_ALL_WINDOWS_FROM_BACK(w)  FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
    00880 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
    00881
    00882 extern Point _cursorpos_drag_start;
    00883
    00884 extern int _scrollbar_start_pos;
    00885 extern int _scrollbar_size;
    00886 extern byte _scroller_click_timeout;
    00887
    00888 extern bool _scrolling_scrollbar;
    00889 extern bool _scrolling_viewport;
    00890
    00891 extern byte _special_mouse_mode;
    00892 enum SpecialMouseMode {
    00893   WSM_NONE     = 0,
    00894   WSM_DRAGDROP = 1,
    00895   WSM_SIZING   = 2,
    00896   WSM_PRESIZE  = 3,
    00897 };
    00898
    00899 Window *GetCallbackWnd();
    00900
    00901 void SetFocusedWindow(Window *w);
    00902 bool EditBoxInGlobalFocus();
    00903
    00904 void ScrollbarClickHandler(Window *w, const NWidgetCore *nw, int x, int y);
    00905
    00906 #endif /* WINDOW_GUI_H */