window_gui.h

Go to the documentation of this file.
00001 /* $Id: window_gui.h 21803 2011-01-15 15:36:58Z terkhen $ */
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 
00022 enum EventState {
00023   ES_HANDLED,     
00024   ES_NOT_HANDLED, 
00025 };
00026 
00030 enum FrameFlags {
00031   FR_NONE         =  0,
00032   FR_TRANSPARENT  =  1 << 0,  
00033   FR_BORDERONLY   =  1 << 4,  
00034   FR_LOWERED      =  1 << 5,  
00035   FR_DARKENED     =  1 << 6,  
00036 };
00037 
00038 DECLARE_ENUM_AS_BIT_SET(FrameFlags)
00039 
00040 
00041 enum WidgetDrawDistances {
00042   /* WWT_IMGBTN(_2) */
00043   WD_IMGBTN_LEFT    = 1,      
00044   WD_IMGBTN_RIGHT   = 2,      
00045   WD_IMGBTN_TOP     = 1,      
00046   WD_IMGBTN_BOTTOM  = 2,      
00047 
00048   /* WWT_INSET */
00049   WD_INSET_LEFT  = 2,         
00050   WD_INSET_RIGHT = 2,         
00051   WD_INSET_TOP   = 1,         
00052 
00053   WD_VSCROLLBAR_WIDTH  = 12,  
00054 
00055   WD_HSCROLLBAR_HEIGHT = 12,  
00056 
00057   /* FrameRect widgets, all text buttons, panel, editbox */
00058   WD_FRAMERECT_LEFT   = 2,    
00059   WD_FRAMERECT_RIGHT  = 2,    
00060   WD_FRAMERECT_TOP    = 1,    
00061   WD_FRAMERECT_BOTTOM = 1,    
00062 
00063   /* Extra space at top/bottom of text panels */
00064   WD_TEXTPANEL_TOP    = 6,    
00065   WD_TEXTPANEL_BOTTOM = 6,    
00066 
00067   /* WWT_FRAME */
00068   WD_FRAMETEXT_LEFT   = 6,    
00069   WD_FRAMETEXT_RIGHT  = 6,    
00070   WD_FRAMETEXT_TOP    = 6,    
00071   WD_FRAMETEXT_BOTTOM = 6,    
00072 
00073   /* WWT_MATRIX */
00074   WD_MATRIX_LEFT   = 2,       
00075   WD_MATRIX_RIGHT  = 2,       
00076   WD_MATRIX_TOP    = 3,       
00077   WD_MATRIX_BOTTOM = 1,       
00078 
00079   /* WWT_SHADEBOX */
00080   WD_SHADEBOX_WIDTH  = 12,    
00081   WD_SHADEBOX_LEFT   = 2,     
00082   WD_SHADEBOX_RIGHT  = 2,     
00083   WD_SHADEBOX_TOP    = 3,     
00084   WD_SHADEBOX_BOTTOM = 3,     
00085 
00086   /* WWT_STICKYBOX */
00087   WD_STICKYBOX_WIDTH  = 12,   
00088   WD_STICKYBOX_LEFT   = 2,    
00089   WD_STICKYBOX_RIGHT  = 2,    
00090   WD_STICKYBOX_TOP    = 3,    
00091   WD_STICKYBOX_BOTTOM = 3,    
00092 
00093   /* WWT_DEBUGBOX */
00094   WD_DEBUGBOX_WIDTH  = 12,    
00095   WD_DEBUGBOX_LEFT   = 2,     
00096   WD_DEBUGBOX_RIGHT  = 2,     
00097   WD_DEBUGBOX_TOP    = 3,     
00098   WD_DEBUGBOX_BOTTOM = 3,     
00099 
00100   /* WWT_RESIZEBOX */
00101   WD_RESIZEBOX_WIDTH  = 12,   
00102   WD_RESIZEBOX_LEFT   = 3,    
00103   WD_RESIZEBOX_RIGHT  = 2,    
00104   WD_RESIZEBOX_TOP    = 3,    
00105   WD_RESIZEBOX_BOTTOM = 2,    
00106 
00107   /* WWT_CLOSEBOX */
00108   WD_CLOSEBOX_WIDTH  = 11,    
00109   WD_CLOSEBOX_LEFT   = 2,     
00110   WD_CLOSEBOX_RIGHT  = 1,     
00111   WD_CLOSEBOX_TOP    = 2,     
00112   WD_CLOSEBOX_BOTTOM = 2,     
00113 
00114   /* WWT_CAPTION */
00115   WD_CAPTION_HEIGHT     = 14, 
00116   WD_CAPTIONTEXT_LEFT   = 2,  
00117   WD_CAPTIONTEXT_RIGHT  = 2,  
00118   WD_CAPTIONTEXT_TOP    = 2,  
00119   WD_CAPTIONTEXT_BOTTOM = 2,  
00120 
00121   /* Dropdown widget. */
00122   WD_DROPDOWN_HEIGHT     = 12, 
00123   WD_DROPDOWNTEXT_LEFT   = 2,  
00124   WD_DROPDOWNTEXT_RIGHT  = 14, 
00125   WD_DROPDOWNTEXT_TOP    = 1,  
00126   WD_DROPDOWNTEXT_BOTTOM = 1,  
00127 
00128   WD_SORTBUTTON_ARROW_WIDTH = 11, 
00129 
00130   WD_PAR_VSEP_NORMAL = 2,      
00131   WD_PAR_VSEP_WIDE   = 8,      
00132 };
00133 
00134 /* widget.cpp */
00135 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
00136 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
00137 
00138 /* window.cpp */
00139 extern Window *_z_front_window;
00140 extern Window *_z_back_window;
00141 extern Window *_focused_window;
00142 
00143 
00145 enum WindowPosition {
00146   WDP_MANUAL,        
00147   WDP_AUTO,          
00148   WDP_CENTER,        
00149   WDP_ALIGN_TOOLBAR, 
00150 };
00151 
00152 Point GetToolbarAlignedWindowPosition(int window_width);
00153 
00157 struct WindowDesc : ZeroedMemoryAllocator {
00158 
00159   WindowDesc(WindowPosition default_pos, int16 def_width, int16 def_height,
00160       WindowClass window_class, WindowClass parent_class, uint32 flags,
00161       const NWidgetPart *nwid_parts, int16 nwid_length);
00162 
00163   ~WindowDesc();
00164 
00165   WindowPosition default_pos;    
00166   int16 default_width;           
00167   int16 default_height;          
00168   WindowClass cls;               
00169   WindowClass parent_cls;        
00170   uint32 flags;                  
00171   const NWidgetPart *nwid_parts; 
00172   int16 nwid_length;             
00173 };
00174 
00178 enum WindowDefaultFlag {
00179   WDF_CONSTRUCTION    =   1 << 0, 
00180   WDF_UNCLICK_BUTTONS =   1 << 1, 
00181   WDF_MODAL           =   1 << 2, 
00182   WDF_NO_FOCUS        =   1 << 3, 
00183 };
00184 
00188 struct ResizeInfo {
00189   uint step_width;  
00190   uint step_height; 
00191 };
00192 
00194 enum SortButtonState {
00195   SBS_OFF,  
00196   SBS_DOWN, 
00197   SBS_UP,   
00198 };
00199 
00207 struct ViewportData : ViewPort {
00208   VehicleID follow_vehicle; 
00209   int32 scrollpos_x;        
00210   int32 scrollpos_y;        
00211   int32 dest_scrollpos_x;   
00212   int32 dest_scrollpos_y;   
00213 };
00214 
00218 struct Window : ZeroedMemoryAllocator {
00219 protected:
00220   void InitializeData(const WindowDesc *desc, WindowNumber window_number);
00221   void InitializePositionSize(int x, int y, int min_width, int min_height);
00222   void FindWindowPlacementAndResize(int def_width, int def_height);
00223 
00224 public:
00225   Window();
00226 
00227   virtual ~Window();
00228 
00235   FORCEINLINE void *operator new[](size_t size)
00236   {
00237     NOT_REACHED();
00238   }
00239 
00245   FORCEINLINE void operator delete(void *ptr)
00246   {
00247   }
00248 
00249   uint16 flags4;              
00250   WindowClass window_class;   
00251   WindowNumber window_number; 
00252 
00253   int left;   
00254   int top;    
00255   int width;  
00256   int height; 
00257 
00258   ResizeInfo resize;  
00259 
00260   Owner owner;        
00261 
00262   ViewportData *viewport;          
00263   uint32 desc_flags;               
00264   const NWidgetCore *nested_focus; 
00265   NWidgetBase *nested_root;        
00266   NWidgetBase **nested_array;      
00267   uint nested_array_size;          
00268   NWidgetStacked *shade_select;    
00269   Dimension unshaded_size;         
00270 
00271   int scrolling_scrollbar;         
00272 
00273   Window *parent;                  
00274   Window *z_front;                 
00275   Window *z_back;                  
00276 
00277   template <class NWID>
00278   inline const NWID *GetWidget(uint widnum) const;
00279   template <class NWID>
00280   inline NWID *GetWidget(uint widnum);
00281 
00282   const Scrollbar *GetScrollbar(uint widnum) const;
00283   Scrollbar *GetScrollbar(uint widnum);
00284 
00285   void InitNested(const WindowDesc *desc, WindowNumber number = 0);
00286   void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
00287   void FinishInitNested(const WindowDesc *desc, WindowNumber window_number = 0);
00288 
00296   inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
00297   {
00298     assert(widget_index < this->nested_array_size);
00299     if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
00300   }
00301 
00306   inline void DisableWidget(byte widget_index)
00307   {
00308     SetWidgetDisabledState(widget_index, true);
00309   }
00310 
00315   inline void EnableWidget(byte widget_index)
00316   {
00317     SetWidgetDisabledState(widget_index, false);
00318   }
00319 
00325   inline bool IsWidgetDisabled(byte widget_index) const
00326   {
00327     assert(widget_index < this->nested_array_size);
00328     return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
00329   }
00330 
00336   inline bool IsWidgetFocused(byte widget_index) const
00337   {
00338     return this->nested_focus != NULL && this->nested_focus->index == widget_index;
00339   }
00340 
00347   inline bool IsWidgetGloballyFocused(byte widget_index) const
00348   {
00349     return _focused_window == this && IsWidgetFocused(widget_index);
00350   }
00351 
00357   inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
00358   {
00359     assert(widget_index < this->nested_array_size);
00360     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
00361   }
00362 
00367   inline void ToggleWidgetLoweredState(byte widget_index)
00368   {
00369     assert(widget_index < this->nested_array_size);
00370     bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00371     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
00372   }
00373 
00378   inline void LowerWidget(byte widget_index)
00379   {
00380     SetWidgetLoweredState(widget_index, true);
00381   }
00382 
00387   inline void RaiseWidget(byte widget_index)
00388   {
00389     SetWidgetLoweredState(widget_index, false);
00390   }
00391 
00397   inline bool IsWidgetLowered(byte widget_index) const
00398   {
00399     assert(widget_index < this->nested_array_size);
00400     return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00401   }
00402 
00403   void UnfocusFocusedWidget();
00404   bool SetFocusedWidget(byte widget_index);
00405 
00406   void HandleButtonClick(byte widget);
00407   int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
00408 
00409   void RaiseButtons(bool autoraise = false);
00410   void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
00411   void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
00412   void SetWidgetDirty(byte widget_index) const;
00413 
00414   void DrawWidgets() const;
00415   void DrawViewport() const;
00416   void DrawSortButtonState(int widget, SortButtonState state) const;
00417 
00418   void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
00419 
00420   void SetDirty() const;
00421   void ReInit(int rx = 0, int ry = 0);
00422 
00424   inline bool IsShaded() const
00425   {
00426     return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
00427   }
00428 
00429   void SetShaded(bool make_shaded);
00430 
00435   void InvalidateData(int data = 0)
00436   {
00437     this->SetDirty();
00438     this->OnInvalidateData(data);
00439   }
00440 
00441   /*** Event handling ***/
00442 
00447   virtual void OnInit() { }
00448 
00457   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
00458 
00463   virtual void OnPaint()
00464   {
00465     this->DrawWidgets();
00466   }
00467 
00474   virtual void DrawWidget(const Rect &r, int widget) const {}
00475 
00488   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
00489 
00496   virtual void SetStringParameters(int widget) const {}
00497 
00501   virtual void OnFocus() {}
00502 
00506   virtual void OnFocusLost() {}
00507 
00515   virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
00516 
00522   virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
00523 
00524 
00531   virtual void OnClick(Point pt, int widget, int click_count) {}
00532 
00540   virtual bool OnRightClick(Point pt, int widget) { return false; }
00541 
00547   virtual void OnHover(Point pt, int widget) {}
00548 
00554   virtual void OnMouseDrag(Point pt, int widget) {}
00555 
00561   virtual void OnDragDrop(Point pt, int widget) {}
00562 
00567   virtual void OnScroll(Point delta) {}
00568 
00575   virtual void OnMouseOver(Point pt, int widget) {}
00576 
00581   virtual void OnMouseWheel(int wheel) {}
00582 
00583 
00587   virtual void OnMouseLoop() {}
00588 
00592   virtual void OnTick() {}
00593 
00597   virtual void OnHundredthTick() {}
00598 
00602   virtual void OnTimeout() {}
00603 
00604 
00609   virtual void OnResize() {}
00610 
00616   virtual void OnDropdownSelect(int widget, int index) {}
00617 
00624   virtual void OnQueryTextFinished(char *str) {}
00625 
00630   virtual void OnInvalidateData(int data = 0) {}
00631 
00632 
00639   virtual void OnPlaceObject(Point pt, TileIndex tile) {}
00640 
00645   virtual void OnVehicleSelect(const struct Vehicle *v) {}
00646 
00650   virtual void OnPlaceObjectAbort() {}
00651 
00652 
00660   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
00661 
00671   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
00672 
00680   virtual void OnPlacePresize(Point pt, TileIndex tile) {}
00681 
00682   /*** End of the event handling ***/
00683 
00688   virtual bool IsNewGRFInspectable() const { return false; }
00689 
00696   virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
00697 };
00698 
00705 template <class NWID>
00706 inline NWID *Window::GetWidget(uint widnum)
00707 {
00708   if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
00709   NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
00710   assert(nwid != NULL);
00711   return nwid;
00712 }
00713 
00715 template <>
00716 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
00717 {
00718   if (widnum >= this->nested_array_size) return NULL;
00719   return this->nested_array[widnum];
00720 }
00721 
00728 template <class NWID>
00729 inline const NWID *Window::GetWidget(uint widnum) const
00730 {
00731   return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
00732 }
00733 
00734 
00738 class PickerWindowBase : public Window {
00739 
00740 public:
00741   PickerWindowBase(Window *parent) : Window()
00742   {
00743     this->parent = parent;
00744   }
00745 
00746   virtual ~PickerWindowBase();
00747 };
00748 
00752 enum WindowFlags {
00753   WF_TIMEOUT_TRIGGER   = 1,       
00754   WF_TIMEOUT_BEGIN     = 7,       
00755   WF_TIMEOUT_MASK      = 7,       
00756   WF_DRAGGING          = 1 <<  3, 
00757   WF_SIZING_RIGHT      = 1 <<  4, 
00758   WF_SIZING_LEFT       = 1 <<  5, 
00759   WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, 
00760   WF_STICKY            = 1 <<  6, 
00761 
00762   WF_DISABLE_VP_SCROLL = 1 <<  7, 
00763 
00764   WF_WHITE_BORDER_ONE  = 1 <<  8,
00765   WF_WHITE_BORDER_MASK = 1 <<  9 | WF_WHITE_BORDER_ONE,
00766 
00767   WF_CENTERED          = 1 << 10, 
00768 };
00769 
00770 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
00771 Window *FindWindowFromPt(int x, int y);
00772 
00779 template <typename Wcls>
00780 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
00781 {
00782   if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
00783   return new Wcls(desc, window_number);
00784 }
00785 
00786 void RelocateAllWindows(int neww, int newh);
00787 
00788 /* misc_gui.cpp */
00789 enum TooltipCloseCondition {
00790   TCC_RIGHT_CLICK,
00791   TCC_LEFT_CLICK,
00792   TCC_HOVER,
00793 };
00794 
00795 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
00796 
00797 /* widget.cpp */
00798 int GetWidgetFromPos(const Window *w, int x, int y);
00799 
00801 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)  for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
00802 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
00803 #define FOR_ALL_WINDOWS_FROM_BACK(w)  FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
00804 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
00805 
00806 extern Point _cursorpos_drag_start;
00807 
00808 extern int _scrollbar_start_pos;
00809 extern int _scrollbar_size;
00810 extern byte _scroller_click_timeout;
00811 
00812 extern bool _scrolling_viewport;
00813 extern bool _mouse_hovering;
00814 
00816 enum SpecialMouseMode {
00817   WSM_NONE,     
00818   WSM_DRAGDROP, 
00819   WSM_SIZING,   
00820   WSM_PRESIZE,  
00821 };
00822 extern SpecialMouseMode _special_mouse_mode;
00823 
00824 void SetFocusedWindow(Window *w);
00825 
00826 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
00827 
00828 #endif /* WINDOW_GUI_H */

Generated on Fri Feb 4 20:53:51 2011 for OpenTTD by  doxygen 1.6.1