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_VSCROLLBAR_WIDTH  = 12,  
00055 
00056   WD_HSCROLLBAR_HEIGHT = 12,  
00057 
00058   /* FrameRect widgets, all text buttons, panel, editbox */
00059   WD_FRAMERECT_LEFT   = 2,    
00060   WD_FRAMERECT_RIGHT  = 2,    
00061   WD_FRAMERECT_TOP    = 1,    
00062   WD_FRAMERECT_BOTTOM = 1,    
00063 
00064   /* Extra space at top/bottom of text panels */
00065   WD_TEXTPANEL_TOP    = 6,    
00066   WD_TEXTPANEL_BOTTOM = 6,    
00067 
00068   /* WWT_FRAME */
00069   WD_FRAMETEXT_LEFT   = 6,    
00070   WD_FRAMETEXT_RIGHT  = 6,    
00071   WD_FRAMETEXT_TOP    = 6,    
00072   WD_FRAMETEXT_BOTTOM = 6,    
00073 
00074   /* WWT_MATRIX */
00075   WD_MATRIX_LEFT   = 2,       
00076   WD_MATRIX_RIGHT  = 2,       
00077   WD_MATRIX_TOP    = 3,       
00078   WD_MATRIX_BOTTOM = 1,       
00079 
00080   /* WWT_SHADEBOX */
00081   WD_SHADEBOX_WIDTH  = 12,    
00082   WD_SHADEBOX_LEFT   = 2,     
00083   WD_SHADEBOX_RIGHT  = 2,     
00084   WD_SHADEBOX_TOP    = 3,     
00085   WD_SHADEBOX_BOTTOM = 3,     
00086 
00087   /* WWT_STICKYBOX */
00088   WD_STICKYBOX_WIDTH  = 12,   
00089   WD_STICKYBOX_LEFT   = 2,    
00090   WD_STICKYBOX_RIGHT  = 2,    
00091   WD_STICKYBOX_TOP    = 3,    
00092   WD_STICKYBOX_BOTTOM = 3,    
00093 
00094   /* WWT_DEBUGBOX */
00095   WD_DEBUGBOX_WIDTH  = 12,    
00096   WD_DEBUGBOX_LEFT   = 2,     
00097   WD_DEBUGBOX_RIGHT  = 2,     
00098   WD_DEBUGBOX_TOP    = 3,     
00099   WD_DEBUGBOX_BOTTOM = 3,     
00100 
00101   /* WWT_RESIZEBOX */
00102   WD_RESIZEBOX_WIDTH  = 12,   
00103   WD_RESIZEBOX_LEFT   = 3,    
00104   WD_RESIZEBOX_RIGHT  = 2,    
00105   WD_RESIZEBOX_TOP    = 3,    
00106   WD_RESIZEBOX_BOTTOM = 2,    
00107 
00108   /* WWT_CLOSEBOX */
00109   WD_CLOSEBOX_WIDTH  = 11,    
00110   WD_CLOSEBOX_LEFT   = 2,     
00111   WD_CLOSEBOX_RIGHT  = 1,     
00112   WD_CLOSEBOX_TOP    = 2,     
00113   WD_CLOSEBOX_BOTTOM = 2,     
00114 
00115   /* WWT_CAPTION */
00116   WD_CAPTION_HEIGHT     = 14, 
00117   WD_CAPTIONTEXT_LEFT   = 2,  
00118   WD_CAPTIONTEXT_RIGHT  = 2,  
00119   WD_CAPTIONTEXT_TOP    = 2,  
00120   WD_CAPTIONTEXT_BOTTOM = 2,  
00121 
00122   /* Dropdown widget. */
00123   WD_DROPDOWN_HEIGHT     = 12, 
00124   WD_DROPDOWNTEXT_LEFT   = 2,  
00125   WD_DROPDOWNTEXT_RIGHT  = 14, 
00126   WD_DROPDOWNTEXT_TOP    = 1,  
00127   WD_DROPDOWNTEXT_BOTTOM = 1,  
00128 
00129   WD_SORTBUTTON_ARROW_WIDTH = 11, 
00130 
00131   WD_PAR_VSEP_NORMAL = 2,      
00132   WD_PAR_VSEP_WIDE   = 8,      
00133 };
00134 
00135 /* widget.cpp */
00136 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
00137 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
00138 
00139 /* window.cpp */
00140 extern Window *_z_front_window;
00141 extern Window *_z_back_window;
00142 extern Window *_focused_window;
00143 
00144 
00146 enum WindowPosition {
00147   WDP_MANUAL,        
00148   WDP_AUTO,          
00149   WDP_CENTER,        
00150   WDP_ALIGN_TOOLBAR, 
00151 };
00152 
00153 Point GetToolbarAlignedWindowPosition(int window_width);
00154 
00158 struct WindowDesc : ZeroedMemoryAllocator {
00159 
00160   WindowDesc(WindowPosition default_pos, int16 def_width, int16 def_height,
00161       WindowClass window_class, WindowClass parent_class, uint32 flags,
00162       const NWidgetPart *nwid_parts, int16 nwid_length);
00163 
00164   ~WindowDesc();
00165 
00166   WindowPosition default_pos;    
00167   int16 default_width;           
00168   int16 default_height;          
00169   WindowClass cls;               
00170   WindowClass parent_cls;        
00171   uint32 flags;                  
00172   const NWidgetPart *nwid_parts; 
00173   int16 nwid_length;             
00174 };
00175 
00179 enum WindowDefaultFlag {
00180   WDF_CONSTRUCTION    =   1 << 0, 
00181   WDF_UNCLICK_BUTTONS =   1 << 1, 
00182   WDF_MODAL           =   1 << 2, 
00183   WDF_NO_FOCUS        =   1 << 3, 
00184 };
00185 
00189 struct ResizeInfo {
00190   uint step_width;  
00191   uint step_height; 
00192 };
00193 
00195 enum SortButtonState {
00196   SBS_OFF,  
00197   SBS_DOWN, 
00198   SBS_UP,   
00199 };
00200 
00208 struct ViewportData : ViewPort {
00209   VehicleID follow_vehicle; 
00210   int32 scrollpos_x;        
00211   int32 scrollpos_y;        
00212   int32 dest_scrollpos_x;   
00213   int32 dest_scrollpos_y;   
00214 };
00215 
00219 struct Window : ZeroedMemoryAllocator {
00220 protected:
00221   void InitializeData(const WindowDesc *desc, WindowNumber window_number);
00222   void InitializePositionSize(int x, int y, int min_width, int min_height);
00223   void FindWindowPlacementAndResize(int def_width, int def_height);
00224 
00225   SmallVector<int, 4> scheduled_invalidation_data;  
00226 
00227 public:
00228   Window();
00229 
00230   virtual ~Window();
00231 
00238   FORCEINLINE void *operator new[](size_t size)
00239   {
00240     NOT_REACHED();
00241   }
00242 
00248   FORCEINLINE void operator delete(void *ptr)
00249   {
00250   }
00251 
00252   uint16 flags4;              
00253   WindowClass window_class;   
00254   WindowNumber window_number; 
00255 
00256   int left;   
00257   int top;    
00258   int width;  
00259   int height; 
00260 
00261   ResizeInfo resize;  
00262 
00263   Owner owner;        
00264 
00265   ViewportData *viewport;          
00266   uint32 desc_flags;               
00267   const NWidgetCore *nested_focus; 
00268   NWidgetBase *nested_root;        
00269   NWidgetBase **nested_array;      
00270   uint nested_array_size;          
00271   NWidgetStacked *shade_select;    
00272   Dimension unshaded_size;         
00273 
00274   int scrolling_scrollbar;         
00275 
00276   Window *parent;                  
00277   Window *z_front;                 
00278   Window *z_back;                  
00279 
00280   template <class NWID>
00281   inline const NWID *GetWidget(uint widnum) const;
00282   template <class NWID>
00283   inline NWID *GetWidget(uint widnum);
00284 
00285   const Scrollbar *GetScrollbar(uint widnum) const;
00286   Scrollbar *GetScrollbar(uint widnum);
00287 
00288   void InitNested(const WindowDesc *desc, WindowNumber number = 0);
00289   void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
00290   void FinishInitNested(const WindowDesc *desc, WindowNumber window_number = 0);
00291 
00299   inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
00300   {
00301     assert(widget_index < this->nested_array_size);
00302     if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
00303   }
00304 
00309   inline void DisableWidget(byte widget_index)
00310   {
00311     SetWidgetDisabledState(widget_index, true);
00312   }
00313 
00318   inline void EnableWidget(byte widget_index)
00319   {
00320     SetWidgetDisabledState(widget_index, false);
00321   }
00322 
00328   inline bool IsWidgetDisabled(byte widget_index) const
00329   {
00330     assert(widget_index < this->nested_array_size);
00331     return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
00332   }
00333 
00339   inline bool IsWidgetFocused(byte widget_index) const
00340   {
00341     return this->nested_focus != NULL && this->nested_focus->index == widget_index;
00342   }
00343 
00350   inline bool IsWidgetGloballyFocused(byte widget_index) const
00351   {
00352     return _focused_window == this && IsWidgetFocused(widget_index);
00353   }
00354 
00360   inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
00361   {
00362     assert(widget_index < this->nested_array_size);
00363     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
00364   }
00365 
00370   inline void ToggleWidgetLoweredState(byte widget_index)
00371   {
00372     assert(widget_index < this->nested_array_size);
00373     bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00374     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
00375   }
00376 
00381   inline void LowerWidget(byte widget_index)
00382   {
00383     SetWidgetLoweredState(widget_index, true);
00384   }
00385 
00390   inline void RaiseWidget(byte widget_index)
00391   {
00392     SetWidgetLoweredState(widget_index, false);
00393   }
00394 
00400   inline bool IsWidgetLowered(byte widget_index) const
00401   {
00402     assert(widget_index < this->nested_array_size);
00403     return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00404   }
00405 
00406   void UnfocusFocusedWidget();
00407   bool SetFocusedWidget(byte widget_index);
00408 
00409   void HandleButtonClick(byte widget);
00410   int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
00411 
00412   void RaiseButtons(bool autoraise = false);
00413   void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
00414   void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
00415   void SetWidgetDirty(byte widget_index) const;
00416 
00417   void DrawWidgets() const;
00418   void DrawViewport() const;
00419   void DrawSortButtonState(int widget, SortButtonState state) const;
00420 
00421   void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
00422 
00423   void SetDirty() const;
00424   void ReInit(int rx = 0, int ry = 0);
00425 
00427   inline bool IsShaded() const
00428   {
00429     return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
00430   }
00431 
00432   void SetShaded(bool make_shaded);
00433 
00439   void InvalidateData(int data = 0, bool gui_scope = true)
00440   {
00441     this->SetDirty();
00442     if (!gui_scope) {
00443       /* Schedule GUI-scope invalidation for next redraw. */
00444       *this->scheduled_invalidation_data.Append() = data;
00445     }
00446     this->OnInvalidateData(data, gui_scope);
00447   }
00448 
00452   void ProcessScheduledInvalidations()
00453   {
00454     for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
00455       this->OnInvalidateData(*data, true);
00456     }
00457     this->scheduled_invalidation_data.Clear();
00458   }
00459 
00460   /*** Event handling ***/
00461 
00466   virtual void OnInit() { }
00467 
00476   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
00477 
00482   virtual void OnPaint()
00483   {
00484     this->DrawWidgets();
00485   }
00486 
00493   virtual void DrawWidget(const Rect &r, int widget) const {}
00494 
00507   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
00508 
00515   virtual void SetStringParameters(int widget) const {}
00516 
00520   virtual void OnFocus() {}
00521 
00525   virtual void OnFocusLost() {}
00526 
00534   virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
00535 
00541   virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
00542 
00543 
00550   virtual void OnClick(Point pt, int widget, int click_count) {}
00551 
00559   virtual bool OnRightClick(Point pt, int widget) { return false; }
00560 
00566   virtual void OnHover(Point pt, int widget) {}
00567 
00573   virtual void OnMouseDrag(Point pt, int widget) {}
00574 
00580   virtual void OnDragDrop(Point pt, int widget) {}
00581 
00586   virtual void OnScroll(Point delta) {}
00587 
00594   virtual void OnMouseOver(Point pt, int widget) {}
00595 
00600   virtual void OnMouseWheel(int wheel) {}
00601 
00602 
00606   virtual void OnMouseLoop() {}
00607 
00611   virtual void OnTick() {}
00612 
00616   virtual void OnHundredthTick() {}
00617 
00621   virtual void OnTimeout() {}
00622 
00623 
00628   virtual void OnResize() {}
00629 
00635   virtual void OnDropdownSelect(int widget, int index) {}
00636 
00643   virtual void OnQueryTextFinished(char *str) {}
00644 
00650   virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
00651 
00658   virtual void OnPlaceObject(Point pt, TileIndex tile) {}
00659 
00664   virtual void OnVehicleSelect(const struct Vehicle *v) {}
00665 
00669   virtual void OnPlaceObjectAbort() {}
00670 
00671 
00679   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
00680 
00690   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
00691 
00699   virtual void OnPlacePresize(Point pt, TileIndex tile) {}
00700 
00701   /*** End of the event handling ***/
00702 
00707   virtual bool IsNewGRFInspectable() const { return false; }
00708 
00715   virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
00716 };
00717 
00724 template <class NWID>
00725 inline NWID *Window::GetWidget(uint widnum)
00726 {
00727   if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
00728   NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
00729   assert(nwid != NULL);
00730   return nwid;
00731 }
00732 
00734 template <>
00735 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
00736 {
00737   if (widnum >= this->nested_array_size) return NULL;
00738   return this->nested_array[widnum];
00739 }
00740 
00747 template <class NWID>
00748 inline const NWID *Window::GetWidget(uint widnum) const
00749 {
00750   return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
00751 }
00752 
00753 
00757 class PickerWindowBase : public Window {
00758 
00759 public:
00760   PickerWindowBase(Window *parent) : Window()
00761   {
00762     this->parent = parent;
00763   }
00764 
00765   virtual ~PickerWindowBase();
00766 };
00767 
00771 enum WindowFlags {
00772   WF_TIMEOUT_TRIGGER   = 1,       
00773   WF_TIMEOUT_BEGIN     = 7,       
00774   WF_TIMEOUT_MASK      = 7,       
00775   WF_DRAGGING          = 1 <<  3, 
00776   WF_SIZING_RIGHT      = 1 <<  4, 
00777   WF_SIZING_LEFT       = 1 <<  5, 
00778   WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, 
00779   WF_STICKY            = 1 <<  6, 
00780 
00781   WF_DISABLE_VP_SCROLL = 1 <<  7, 
00782 
00783   WF_WHITE_BORDER_ONE  = 1 <<  8,
00784   WF_WHITE_BORDER_MASK = 1 <<  9 | WF_WHITE_BORDER_ONE,
00785 
00786   WF_CENTERED          = 1 << 10, 
00787 };
00788 
00789 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
00790 Window *FindWindowFromPt(int x, int y);
00791 
00798 template <typename Wcls>
00799 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
00800 {
00801   if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
00802   return new Wcls(desc, window_number);
00803 }
00804 
00805 void RelocateAllWindows(int neww, int newh);
00806 
00807 /* misc_gui.cpp */
00808 enum TooltipCloseCondition {
00809   TCC_RIGHT_CLICK,
00810   TCC_LEFT_CLICK,
00811   TCC_HOVER,
00812 };
00813 
00814 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
00815 
00816 /* widget.cpp */
00817 int GetWidgetFromPos(const Window *w, int x, int y);
00818 
00820 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)  for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
00821 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
00822 #define FOR_ALL_WINDOWS_FROM_BACK(w)  FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
00823 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
00824 
00825 extern Point _cursorpos_drag_start;
00826 
00827 extern int _scrollbar_start_pos;
00828 extern int _scrollbar_size;
00829 extern byte _scroller_click_timeout;
00830 
00831 extern bool _scrolling_viewport;
00832 extern bool _mouse_hovering;
00833 
00835 enum SpecialMouseMode {
00836   WSM_NONE,     
00837   WSM_DRAGDROP, 
00838   WSM_SIZING,   
00839   WSM_PRESIZE,  
00840 };
00841 extern SpecialMouseMode _special_mouse_mode;
00842 
00843 void SetFocusedWindow(Window *w);
00844 
00845 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
00846 
00847 #endif /* WINDOW_GUI_H */

Generated on Mon May 9 05:19:04 2011 for OpenTTD by  doxygen 1.6.1