window_gui.h

Go to the documentation of this file.
00001 /* $Id: window_gui.h 23640 2011-12-20 17:57:56Z truebrain $ */
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 
00211 enum WindowFlags {
00212   WF_TIMEOUT           = 1 <<  0, 
00213 
00214   WF_DRAGGING          = 1 <<  3, 
00215   WF_SIZING_RIGHT      = 1 <<  4, 
00216   WF_SIZING_LEFT       = 1 <<  5, 
00217   WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, 
00218   WF_STICKY            = 1 <<  6, 
00219   WF_DISABLE_VP_SCROLL = 1 <<  7, 
00220   WF_WHITE_BORDER      = 1 <<  8, 
00221   WF_HIGHLIGHTED       = 1 <<  9, 
00222   WF_CENTERED          = 1 << 10, 
00223 };
00224 DECLARE_ENUM_AS_BIT_SET(WindowFlags)
00225 
00226 static const int TIMEOUT_DURATION = 7; 
00227 static const int WHITE_BORDER_DURATION = 3; 
00228 
00236 struct ViewportData : ViewPort {
00237   VehicleID follow_vehicle; 
00238   int32 scrollpos_x;        
00239   int32 scrollpos_y;        
00240   int32 dest_scrollpos_x;   
00241   int32 dest_scrollpos_y;   
00242 };
00243 
00247 struct Window : ZeroedMemoryAllocator {
00248 protected:
00249   void InitializeData(const WindowDesc *desc, WindowNumber window_number);
00250   void InitializePositionSize(int x, int y, int min_width, int min_height);
00251   void FindWindowPlacementAndResize(int def_width, int def_height);
00252 
00253   SmallVector<int, 4> scheduled_invalidation_data;  
00254 
00255 public:
00256   Window();
00257 
00258   virtual ~Window();
00259 
00266   inline void *operator new[](size_t size)
00267   {
00268     NOT_REACHED();
00269   }
00270 
00276   inline void operator delete(void *ptr)
00277   {
00278   }
00279 
00280   WindowFlags flags;          
00281   WindowClass window_class;   
00282   WindowNumber window_number; 
00283 
00284   uint8 timeout_timer;      
00285   uint8 white_border_timer; 
00286 
00287   int left;   
00288   int top;    
00289   int width;  
00290   int height; 
00291 
00292   ResizeInfo resize;  
00293 
00294   Owner owner;        
00295 
00296   ViewportData *viewport;          
00297   uint32 desc_flags;               
00298   const NWidgetCore *nested_focus; 
00299   NWidgetBase *nested_root;        
00300   NWidgetBase **nested_array;      
00301   uint nested_array_size;          
00302   NWidgetStacked *shade_select;    
00303   Dimension unshaded_size;         
00304 
00305   int scrolling_scrollbar;         
00306 
00307   Window *parent;                  
00308   Window *z_front;                 
00309   Window *z_back;                  
00310 
00311   template <class NWID>
00312   inline const NWID *GetWidget(uint widnum) const;
00313   template <class NWID>
00314   inline NWID *GetWidget(uint widnum);
00315 
00316   const Scrollbar *GetScrollbar(uint widnum) const;
00317   Scrollbar *GetScrollbar(uint widnum);
00318 
00319   void InitNested(const WindowDesc *desc, WindowNumber number = 0);
00320   void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
00321   void FinishInitNested(const WindowDesc *desc, WindowNumber window_number = 0);
00322 
00326   inline void SetTimeout()
00327   {
00328     this->flags |= WF_TIMEOUT;
00329     this->timeout_timer = TIMEOUT_DURATION;
00330   }
00331 
00335   inline void SetWhiteBorder()
00336   {
00337     this->flags |= WF_WHITE_BORDER;
00338     this->white_border_timer = WHITE_BORDER_DURATION;
00339   }
00340 
00341   void DisableAllWidgetHighlight();
00342   void SetWidgetHighlight(byte widget_index, TextColour highlighted_colour);
00343   bool IsWidgetHighlighted(byte widget_index) const;
00344 
00352   inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
00353   {
00354     assert(widget_index < this->nested_array_size);
00355     if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
00356   }
00357 
00362   inline void DisableWidget(byte widget_index)
00363   {
00364     SetWidgetDisabledState(widget_index, true);
00365   }
00366 
00371   inline void EnableWidget(byte widget_index)
00372   {
00373     SetWidgetDisabledState(widget_index, false);
00374   }
00375 
00381   inline bool IsWidgetDisabled(byte widget_index) const
00382   {
00383     assert(widget_index < this->nested_array_size);
00384     return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
00385   }
00386 
00392   inline bool IsWidgetFocused(byte widget_index) const
00393   {
00394     return this->nested_focus != NULL && this->nested_focus->index == widget_index;
00395   }
00396 
00403   inline bool IsWidgetGloballyFocused(byte widget_index) const
00404   {
00405     return _focused_window == this && IsWidgetFocused(widget_index);
00406   }
00407 
00413   inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
00414   {
00415     assert(widget_index < this->nested_array_size);
00416     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
00417   }
00418 
00423   inline void ToggleWidgetLoweredState(byte widget_index)
00424   {
00425     assert(widget_index < this->nested_array_size);
00426     bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00427     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
00428   }
00429 
00434   inline void LowerWidget(byte widget_index)
00435   {
00436     SetWidgetLoweredState(widget_index, true);
00437   }
00438 
00443   inline void RaiseWidget(byte widget_index)
00444   {
00445     SetWidgetLoweredState(widget_index, false);
00446   }
00447 
00453   inline bool IsWidgetLowered(byte widget_index) const
00454   {
00455     assert(widget_index < this->nested_array_size);
00456     return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00457   }
00458 
00459   void UnfocusFocusedWidget();
00460   bool SetFocusedWidget(byte widget_index);
00461 
00462   void HandleButtonClick(byte widget);
00463   int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
00464 
00465   void RaiseButtons(bool autoraise = false);
00466   void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
00467   void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
00468   void SetWidgetDirty(byte widget_index) const;
00469 
00470   void DrawWidgets() const;
00471   void DrawViewport() const;
00472   void DrawSortButtonState(int widget, SortButtonState state) const;
00473 
00474   void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
00475 
00476   void SetDirty() const;
00477   void ReInit(int rx = 0, int ry = 0);
00478 
00480   inline bool IsShaded() const
00481   {
00482     return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
00483   }
00484 
00485   void SetShaded(bool make_shaded);
00486 
00487   void InvalidateData(int data = 0, bool gui_scope = true);
00488   void ProcessScheduledInvalidations();
00489   void ProcessHighlightedInvalidations();
00490 
00491   /*** Event handling ***/
00492 
00497   virtual void OnInit() { }
00498 
00507   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
00508 
00513   virtual void OnPaint()
00514   {
00515     this->DrawWidgets();
00516   }
00517 
00524   virtual void DrawWidget(const Rect &r, int widget) const {}
00525 
00538   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
00539 
00546   virtual void SetStringParameters(int widget) const {}
00547 
00551   virtual void OnFocus() {}
00552 
00556   virtual void OnFocusLost() {}
00557 
00565   virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
00566 
00572   virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
00573 
00574 
00581   virtual void OnClick(Point pt, int widget, int click_count) {}
00582 
00590   virtual bool OnRightClick(Point pt, int widget) { return false; }
00591 
00597   virtual void OnHover(Point pt, int widget) {}
00598 
00604   virtual void OnMouseDrag(Point pt, int widget) {}
00605 
00611   virtual void OnDragDrop(Point pt, int widget) {}
00612 
00617   virtual void OnScroll(Point delta) {}
00618 
00625   virtual void OnMouseOver(Point pt, int widget) {}
00626 
00631   virtual void OnMouseWheel(int wheel) {}
00632 
00633 
00637   virtual void OnMouseLoop() {}
00638 
00642   virtual void OnTick() {}
00643 
00647   virtual void OnHundredthTick() {}
00648 
00652   virtual void OnTimeout() {}
00653 
00654 
00659   virtual void OnResize() {}
00660 
00666   virtual void OnDropdownSelect(int widget, int index) {}
00667 
00674   virtual void OnQueryTextFinished(char *str) {}
00675 
00681   virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
00682 
00689   virtual void OnPlaceObject(Point pt, TileIndex tile) {}
00690 
00696   virtual bool OnVehicleSelect(const struct Vehicle *v) { return false; }
00697 
00701   virtual void OnPlaceObjectAbort() {}
00702 
00703 
00711   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
00712 
00722   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
00723 
00731   virtual void OnPlacePresize(Point pt, TileIndex tile) {}
00732 
00733   /*** End of the event handling ***/
00734 
00739   virtual bool IsNewGRFInspectable() const { return false; }
00740 
00747   virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
00748 };
00749 
00756 template <class NWID>
00757 inline NWID *Window::GetWidget(uint widnum)
00758 {
00759   if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
00760   NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
00761   assert(nwid != NULL);
00762   return nwid;
00763 }
00764 
00766 template <>
00767 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
00768 {
00769   if (widnum >= this->nested_array_size) return NULL;
00770   return this->nested_array[widnum];
00771 }
00772 
00779 template <class NWID>
00780 inline const NWID *Window::GetWidget(uint widnum) const
00781 {
00782   return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
00783 }
00784 
00785 
00789 class PickerWindowBase : public Window {
00790 
00791 public:
00792   PickerWindowBase(Window *parent) : Window()
00793   {
00794     this->parent = parent;
00795   }
00796 
00797   virtual ~PickerWindowBase();
00798 };
00799 
00800 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
00801 Window *FindWindowFromPt(int x, int y);
00802 
00809 template <typename Wcls>
00810 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
00811 {
00812   if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
00813   return new Wcls(desc, window_number);
00814 }
00815 
00816 void RelocateAllWindows(int neww, int newh);
00817 
00818 /* misc_gui.cpp */
00819 enum TooltipCloseCondition {
00820   TCC_RIGHT_CLICK,
00821   TCC_LEFT_CLICK,
00822   TCC_HOVER,
00823 };
00824 
00825 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
00826 
00827 /* widget.cpp */
00828 int GetWidgetFromPos(const Window *w, int x, int y);
00829 
00831 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)  for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
00832 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
00833 #define FOR_ALL_WINDOWS_FROM_BACK(w)  FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
00834 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
00835 
00836 extern Point _cursorpos_drag_start;
00837 
00838 extern int _scrollbar_start_pos;
00839 extern int _scrollbar_size;
00840 extern byte _scroller_click_timeout;
00841 
00842 extern bool _scrolling_viewport;
00843 extern bool _mouse_hovering;
00844 
00846 enum SpecialMouseMode {
00847   WSM_NONE,     
00848   WSM_DRAGDROP, 
00849   WSM_SIZING,   
00850   WSM_PRESIZE,  
00851 };
00852 extern SpecialMouseMode _special_mouse_mode;
00853 
00854 void SetFocusedWindow(Window *w);
00855 
00856 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
00857 
00858 #endif /* WINDOW_GUI_H */