window_gui.h

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 #ifndef WINDOW_GUI_H
00013 #define WINDOW_GUI_H
00014 
00015 #include "vehicle_type.h"
00016 #include "viewport_type.h"
00017 #include "company_type.h"
00018 #include "tile_type.h"
00019 #include "widget_type.h"
00020 #include "core/smallvec_type.hpp"
00021 
00023 enum EventState {
00024   ES_HANDLED,     
00025   ES_NOT_HANDLED, 
00026 };
00027 
00031 enum FrameFlags {
00032   FR_NONE         =  0,
00033   FR_TRANSPARENT  =  1 << 0,  
00034   FR_BORDERONLY   =  1 << 4,  
00035   FR_LOWERED      =  1 << 5,  
00036   FR_DARKENED     =  1 << 6,  
00037 };
00038 
00039 DECLARE_ENUM_AS_BIT_SET(FrameFlags)
00040 
00041 
00042 enum WidgetDrawDistances {
00043   /* WWT_IMGBTN(_2) */
00044   WD_IMGBTN_LEFT    = 1,      
00045   WD_IMGBTN_RIGHT   = 2,      
00046   WD_IMGBTN_TOP     = 1,      
00047   WD_IMGBTN_BOTTOM  = 2,      
00048 
00049   /* WWT_INSET */
00050   WD_INSET_LEFT  = 2,         
00051   WD_INSET_RIGHT = 2,         
00052   WD_INSET_TOP   = 1,         
00053 
00054   WD_SCROLLBAR_LEFT   = 2,    
00055   WD_SCROLLBAR_RIGHT  = 2,    
00056   WD_SCROLLBAR_TOP    = 2,    
00057   WD_SCROLLBAR_BOTTOM = 2,    
00058 
00059   /* Size of the pure frame bevel without any padding. */
00060   WD_BEVEL_LEFT       = 1,    
00061   WD_BEVEL_RIGHT      = 1,    
00062   WD_BEVEL_TOP        = 1,    
00063   WD_BEVEL_BOTTOM     = 1,    
00064 
00065   /* FrameRect widgets, all text buttons, panel, editbox */
00066   WD_FRAMERECT_LEFT   = 2,    
00067   WD_FRAMERECT_RIGHT  = 2,    
00068   WD_FRAMERECT_TOP    = 1,    
00069   WD_FRAMERECT_BOTTOM = 1,    
00070 
00071   /* Extra space at top/bottom of text panels */
00072   WD_TEXTPANEL_TOP    = 6,    
00073   WD_TEXTPANEL_BOTTOM = 6,    
00074 
00075   /* WWT_FRAME */
00076   WD_FRAMETEXT_LEFT   = 6,    
00077   WD_FRAMETEXT_RIGHT  = 6,    
00078   WD_FRAMETEXT_TOP    = 6,    
00079   WD_FRAMETEXT_BOTTOM = 6,    
00080 
00081   /* WWT_MATRIX */
00082   WD_MATRIX_LEFT   = 2,       
00083   WD_MATRIX_RIGHT  = 2,       
00084   WD_MATRIX_TOP    = 3,       
00085   WD_MATRIX_BOTTOM = 1,       
00086 
00087   /* WWT_SHADEBOX */
00088   WD_SHADEBOX_WIDTH  = 12,    
00089   WD_SHADEBOX_LEFT   = 2,     
00090   WD_SHADEBOX_RIGHT  = 2,     
00091   WD_SHADEBOX_TOP    = 3,     
00092   WD_SHADEBOX_BOTTOM = 3,     
00093 
00094   /* WWT_STICKYBOX */
00095   WD_STICKYBOX_WIDTH  = 12,   
00096   WD_STICKYBOX_LEFT   = 2,    
00097   WD_STICKYBOX_RIGHT  = 2,    
00098   WD_STICKYBOX_TOP    = 3,    
00099   WD_STICKYBOX_BOTTOM = 3,    
00100 
00101   /* WWT_DEBUGBOX */
00102   WD_DEBUGBOX_WIDTH  = 12,    
00103   WD_DEBUGBOX_LEFT   = 2,     
00104   WD_DEBUGBOX_RIGHT  = 2,     
00105   WD_DEBUGBOX_TOP    = 3,     
00106   WD_DEBUGBOX_BOTTOM = 3,     
00107 
00108   /* WWT_RESIZEBOX */
00109   WD_RESIZEBOX_WIDTH  = 12,   
00110   WD_RESIZEBOX_LEFT   = 3,    
00111   WD_RESIZEBOX_RIGHT  = 2,    
00112   WD_RESIZEBOX_TOP    = 3,    
00113   WD_RESIZEBOX_BOTTOM = 2,    
00114 
00115   /* WWT_CLOSEBOX */
00116   WD_CLOSEBOX_WIDTH  = 11,    
00117   WD_CLOSEBOX_LEFT   = 2,     
00118   WD_CLOSEBOX_RIGHT  = 1,     
00119   WD_CLOSEBOX_TOP    = 2,     
00120   WD_CLOSEBOX_BOTTOM = 2,     
00121 
00122   /* WWT_CAPTION */
00123   WD_CAPTION_HEIGHT     = 14, 
00124   WD_CAPTIONTEXT_LEFT   = 2,  
00125   WD_CAPTIONTEXT_RIGHT  = 2,  
00126   WD_CAPTIONTEXT_TOP    = 2,  
00127   WD_CAPTIONTEXT_BOTTOM = 2,  
00128 
00129   /* Dropdown widget. */
00130   WD_DROPDOWN_HEIGHT     = 12, 
00131   WD_DROPDOWNTEXT_LEFT   = 2,  
00132   WD_DROPDOWNTEXT_RIGHT  = 14, 
00133   WD_DROPDOWNTEXT_TOP    = 1,  
00134   WD_DROPDOWNTEXT_BOTTOM = 1,  
00135 
00136   WD_SORTBUTTON_ARROW_WIDTH = 11, 
00137 
00138   WD_PAR_VSEP_NORMAL = 2,      
00139   WD_PAR_VSEP_WIDE   = 8,      
00140 };
00141 
00142 /* widget.cpp */
00143 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
00144 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
00145 
00146 /* window.cpp */
00147 extern Window *_z_front_window;
00148 extern Window *_z_back_window;
00149 extern Window *_focused_window;
00150 
00151 
00153 enum WindowPosition {
00154   WDP_MANUAL,        
00155   WDP_AUTO,          
00156   WDP_CENTER,        
00157   WDP_ALIGN_TOOLBAR, 
00158 };
00159 
00160 Point GetToolbarAlignedWindowPosition(int window_width);
00161 
00165 struct WindowDesc : ZeroedMemoryAllocator {
00166 
00167   WindowDesc(WindowPosition default_pos, int16 def_width, int16 def_height,
00168       WindowClass window_class, WindowClass parent_class, uint32 flags,
00169       const NWidgetPart *nwid_parts, int16 nwid_length);
00170 
00171   ~WindowDesc();
00172 
00173   WindowPosition default_pos;    
00174   int16 default_width;           
00175   int16 default_height;          
00176   WindowClass cls;               
00177   WindowClass parent_cls;        
00178   uint32 flags;                  
00179   const NWidgetPart *nwid_parts; 
00180   int16 nwid_length;             
00181 };
00182 
00186 enum WindowDefaultFlag {
00187   WDF_CONSTRUCTION    =   1 << 0, 
00188   WDF_UNCLICK_BUTTONS =   1 << 1, 
00189   WDF_MODAL           =   1 << 2, 
00190   WDF_NO_FOCUS        =   1 << 3, 
00191 };
00192 
00196 struct ResizeInfo {
00197   uint step_width;  
00198   uint step_height; 
00199 };
00200 
00202 enum SortButtonState {
00203   SBS_OFF,  
00204   SBS_DOWN, 
00205   SBS_UP,   
00206 };
00207 
00215 struct ViewportData : ViewPort {
00216   VehicleID follow_vehicle; 
00217   int32 scrollpos_x;        
00218   int32 scrollpos_y;        
00219   int32 dest_scrollpos_x;   
00220   int32 dest_scrollpos_y;   
00221 };
00222 
00226 struct Window : ZeroedMemoryAllocator {
00227 protected:
00228   void InitializeData(const WindowDesc *desc, WindowNumber window_number);
00229   void InitializePositionSize(int x, int y, int min_width, int min_height);
00230   void FindWindowPlacementAndResize(int def_width, int def_height);
00231 
00232   SmallVector<int, 4> scheduled_invalidation_data;  
00233 
00234 public:
00235   Window();
00236 
00237   virtual ~Window();
00238 
00245   FORCEINLINE void *operator new[](size_t size)
00246   {
00247     NOT_REACHED();
00248   }
00249 
00255   FORCEINLINE void operator delete(void *ptr)
00256   {
00257   }
00258 
00259   uint16 flags4;              
00260   WindowClass window_class;   
00261   WindowNumber window_number; 
00262 
00263   int left;   
00264   int top;    
00265   int width;  
00266   int height; 
00267 
00268   ResizeInfo resize;  
00269 
00270   Owner owner;        
00271 
00272   ViewportData *viewport;          
00273   uint32 desc_flags;               
00274   const NWidgetCore *nested_focus; 
00275   NWidgetBase *nested_root;        
00276   NWidgetBase **nested_array;      
00277   uint nested_array_size;          
00278   NWidgetStacked *shade_select;    
00279   Dimension unshaded_size;         
00280 
00281   int scrolling_scrollbar;         
00282 
00283   Window *parent;                  
00284   Window *z_front;                 
00285   Window *z_back;                  
00286 
00287   template <class NWID>
00288   inline const NWID *GetWidget(uint widnum) const;
00289   template <class NWID>
00290   inline NWID *GetWidget(uint widnum);
00291 
00292   const Scrollbar *GetScrollbar(uint widnum) const;
00293   Scrollbar *GetScrollbar(uint widnum);
00294 
00295   void InitNested(const WindowDesc *desc, WindowNumber number = 0);
00296   void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
00297   void FinishInitNested(const WindowDesc *desc, WindowNumber window_number = 0);
00298 
00306   inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
00307   {
00308     assert(widget_index < this->nested_array_size);
00309     if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
00310   }
00311 
00316   inline void DisableWidget(byte widget_index)
00317   {
00318     SetWidgetDisabledState(widget_index, true);
00319   }
00320 
00325   inline void EnableWidget(byte widget_index)
00326   {
00327     SetWidgetDisabledState(widget_index, false);
00328   }
00329 
00335   inline bool IsWidgetDisabled(byte widget_index) const
00336   {
00337     assert(widget_index < this->nested_array_size);
00338     return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
00339   }
00340 
00346   inline bool IsWidgetFocused(byte widget_index) const
00347   {
00348     return this->nested_focus != NULL && this->nested_focus->index == widget_index;
00349   }
00350 
00357   inline bool IsWidgetGloballyFocused(byte widget_index) const
00358   {
00359     return _focused_window == this && IsWidgetFocused(widget_index);
00360   }
00361 
00367   inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
00368   {
00369     assert(widget_index < this->nested_array_size);
00370     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
00371   }
00372 
00377   inline void ToggleWidgetLoweredState(byte widget_index)
00378   {
00379     assert(widget_index < this->nested_array_size);
00380     bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00381     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
00382   }
00383 
00388   inline void LowerWidget(byte widget_index)
00389   {
00390     SetWidgetLoweredState(widget_index, true);
00391   }
00392 
00397   inline void RaiseWidget(byte widget_index)
00398   {
00399     SetWidgetLoweredState(widget_index, false);
00400   }
00401 
00407   inline bool IsWidgetLowered(byte widget_index) const
00408   {
00409     assert(widget_index < this->nested_array_size);
00410     return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00411   }
00412 
00413   void UnfocusFocusedWidget();
00414   bool SetFocusedWidget(byte widget_index);
00415 
00416   void HandleButtonClick(byte widget);
00417   int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
00418 
00419   void RaiseButtons(bool autoraise = false);
00420   void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
00421   void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
00422   void SetWidgetDirty(byte widget_index) const;
00423 
00424   void DrawWidgets() const;
00425   void DrawViewport() const;
00426   void DrawSortButtonState(int widget, SortButtonState state) const;
00427 
00428   void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
00429 
00430   void SetDirty() const;
00431   void ReInit(int rx = 0, int ry = 0);
00432 
00434   inline bool IsShaded() const
00435   {
00436     return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
00437   }
00438 
00439   void SetShaded(bool make_shaded);
00440 
00446   void InvalidateData(int data = 0, bool gui_scope = true)
00447   {
00448     this->SetDirty();
00449     if (!gui_scope) {
00450       /* Schedule GUI-scope invalidation for next redraw. */
00451       *this->scheduled_invalidation_data.Append() = data;
00452     }
00453     this->OnInvalidateData(data, gui_scope);
00454   }
00455 
00459   void ProcessScheduledInvalidations()
00460   {
00461     for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
00462       this->OnInvalidateData(*data, true);
00463     }
00464     this->scheduled_invalidation_data.Clear();
00465   }
00466 
00467   /*** Event handling ***/
00468 
00473   virtual void OnInit() { }
00474 
00483   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
00484 
00489   virtual void OnPaint()
00490   {
00491     this->DrawWidgets();
00492   }
00493 
00500   virtual void DrawWidget(const Rect &r, int widget) const {}
00501 
00514   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
00515 
00522   virtual void SetStringParameters(int widget) const {}
00523 
00527   virtual void OnFocus() {}
00528 
00532   virtual void OnFocusLost() {}
00533 
00541   virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
00542 
00548   virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
00549 
00550 
00557   virtual void OnClick(Point pt, int widget, int click_count) {}
00558 
00566   virtual bool OnRightClick(Point pt, int widget) { return false; }
00567 
00573   virtual void OnHover(Point pt, int widget) {}
00574 
00580   virtual void OnMouseDrag(Point pt, int widget) {}
00581 
00587   virtual void OnDragDrop(Point pt, int widget) {}
00588 
00593   virtual void OnScroll(Point delta) {}
00594 
00601   virtual void OnMouseOver(Point pt, int widget) {}
00602 
00607   virtual void OnMouseWheel(int wheel) {}
00608 
00609 
00613   virtual void OnMouseLoop() {}
00614 
00618   virtual void OnTick() {}
00619 
00623   virtual void OnHundredthTick() {}
00624 
00628   virtual void OnTimeout() {}
00629 
00630 
00635   virtual void OnResize() {}
00636 
00642   virtual void OnDropdownSelect(int widget, int index) {}
00643 
00650   virtual void OnQueryTextFinished(char *str) {}
00651 
00657   virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
00658 
00665   virtual void OnPlaceObject(Point pt, TileIndex tile) {}
00666 
00671   virtual void OnVehicleSelect(const struct Vehicle *v) {}
00672 
00676   virtual void OnPlaceObjectAbort() {}
00677 
00678 
00686   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
00687 
00697   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
00698 
00706   virtual void OnPlacePresize(Point pt, TileIndex tile) {}
00707 
00708   /*** End of the event handling ***/
00709 
00714   virtual bool IsNewGRFInspectable() const { return false; }
00715 
00722   virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
00723 };
00724 
00731 template <class NWID>
00732 inline NWID *Window::GetWidget(uint widnum)
00733 {
00734   if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
00735   NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
00736   assert(nwid != NULL);
00737   return nwid;
00738 }
00739 
00741 template <>
00742 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
00743 {
00744   if (widnum >= this->nested_array_size) return NULL;
00745   return this->nested_array[widnum];
00746 }
00747 
00754 template <class NWID>
00755 inline const NWID *Window::GetWidget(uint widnum) const
00756 {
00757   return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
00758 }
00759 
00760 
00764 class PickerWindowBase : public Window {
00765 
00766 public:
00767   PickerWindowBase(Window *parent) : Window()
00768   {
00769     this->parent = parent;
00770   }
00771 
00772   virtual ~PickerWindowBase();
00773 };
00774 
00778 enum WindowFlags {
00779   WF_TIMEOUT_TRIGGER   = 1,       
00780   WF_TIMEOUT_BEGIN     = 7,       
00781   WF_TIMEOUT_MASK      = 7,       
00782   WF_DRAGGING          = 1 <<  3, 
00783   WF_SIZING_RIGHT      = 1 <<  4, 
00784   WF_SIZING_LEFT       = 1 <<  5, 
00785   WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, 
00786   WF_STICKY            = 1 <<  6, 
00787 
00788   WF_DISABLE_VP_SCROLL = 1 <<  7, 
00789 
00790   WF_WHITE_BORDER_ONE  = 1 <<  8,
00791   WF_WHITE_BORDER_MASK = 1 <<  9 | WF_WHITE_BORDER_ONE,
00792 
00793   WF_CENTERED          = 1 << 10, 
00794 };
00795 
00796 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
00797 Window *FindWindowFromPt(int x, int y);
00798 
00805 template <typename Wcls>
00806 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
00807 {
00808   if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
00809   return new Wcls(desc, window_number);
00810 }
00811 
00812 void RelocateAllWindows(int neww, int newh);
00813 
00814 /* misc_gui.cpp */
00815 enum TooltipCloseCondition {
00816   TCC_RIGHT_CLICK,
00817   TCC_LEFT_CLICK,
00818   TCC_HOVER,
00819 };
00820 
00821 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
00822 
00823 /* widget.cpp */
00824 int GetWidgetFromPos(const Window *w, int x, int y);
00825 
00827 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)  for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
00828 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
00829 #define FOR_ALL_WINDOWS_FROM_BACK(w)  FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
00830 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
00831 
00832 extern Point _cursorpos_drag_start;
00833 
00834 extern int _scrollbar_start_pos;
00835 extern int _scrollbar_size;
00836 extern byte _scroller_click_timeout;
00837 
00838 extern bool _scrolling_viewport;
00839 extern bool _mouse_hovering;
00840 
00842 enum SpecialMouseMode {
00843   WSM_NONE,     
00844   WSM_DRAGDROP, 
00845   WSM_SIZING,   
00846   WSM_PRESIZE,  
00847 };
00848 extern SpecialMouseMode _special_mouse_mode;
00849 
00850 void SetFocusedWindow(Window *w);
00851 
00852 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
00853 
00854 #endif /* WINDOW_GUI_H */