window_gui.h

Go to the documentation of this file.
00001 /* $Id: window_gui.h 22152 2011-02-26 20:13:14Z 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 "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 
00438   void InvalidateData(int data = 0)
00439   {
00440     this->SetDirty();
00441     this->OnInvalidateData(data);
00442   }
00443 
00449   void ScheduleInvalidateData(int data = 0)
00450   {
00451     this->SetDirty();
00452     *this->scheduled_invalidation_data.Append() = data;
00453   }
00454 
00458   void ProcessScheduledInvalidations()
00459   {
00460     for (int *data = this->scheduled_invalidation_data.Begin(); this->window_class != WC_INVALID && data != this->scheduled_invalidation_data.End(); data++) {
00461       this->OnInvalidateData(*data);
00462     }
00463     this->scheduled_invalidation_data.Clear();
00464   }
00465 
00466   /*** Event handling ***/
00467 
00472   virtual void OnInit() { }
00473 
00482   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
00483 
00488   virtual void OnPaint()
00489   {
00490     this->DrawWidgets();
00491   }
00492 
00499   virtual void DrawWidget(const Rect &r, int widget) const {}
00500 
00513   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
00514 
00521   virtual void SetStringParameters(int widget) const {}
00522 
00526   virtual void OnFocus() {}
00527 
00531   virtual void OnFocusLost() {}
00532 
00540   virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
00541 
00547   virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
00548 
00549 
00556   virtual void OnClick(Point pt, int widget, int click_count) {}
00557 
00565   virtual bool OnRightClick(Point pt, int widget) { return false; }
00566 
00572   virtual void OnHover(Point pt, int widget) {}
00573 
00579   virtual void OnMouseDrag(Point pt, int widget) {}
00580 
00586   virtual void OnDragDrop(Point pt, int widget) {}
00587 
00592   virtual void OnScroll(Point delta) {}
00593 
00600   virtual void OnMouseOver(Point pt, int widget) {}
00601 
00606   virtual void OnMouseWheel(int wheel) {}
00607 
00608 
00612   virtual void OnMouseLoop() {}
00613 
00617   virtual void OnTick() {}
00618 
00622   virtual void OnHundredthTick() {}
00623 
00627   virtual void OnTimeout() {}
00628 
00629 
00634   virtual void OnResize() {}
00635 
00641   virtual void OnDropdownSelect(int widget, int index) {}
00642 
00649   virtual void OnQueryTextFinished(char *str) {}
00650 
00655   virtual void OnInvalidateData(int data = 0) {}
00656 
00657 
00664   virtual void OnPlaceObject(Point pt, TileIndex tile) {}
00665 
00670   virtual void OnVehicleSelect(const struct Vehicle *v) {}
00671 
00675   virtual void OnPlaceObjectAbort() {}
00676 
00677 
00685   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
00686 
00696   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
00697 
00705   virtual void OnPlacePresize(Point pt, TileIndex tile) {}
00706 
00707   /*** End of the event handling ***/
00708 
00713   virtual bool IsNewGRFInspectable() const { return false; }
00714 
00721   virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
00722 };
00723 
00730 template <class NWID>
00731 inline NWID *Window::GetWidget(uint widnum)
00732 {
00733   if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
00734   NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
00735   assert(nwid != NULL);
00736   return nwid;
00737 }
00738 
00740 template <>
00741 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
00742 {
00743   if (widnum >= this->nested_array_size) return NULL;
00744   return this->nested_array[widnum];
00745 }
00746 
00753 template <class NWID>
00754 inline const NWID *Window::GetWidget(uint widnum) const
00755 {
00756   return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
00757 }
00758 
00759 
00763 class PickerWindowBase : public Window {
00764 
00765 public:
00766   PickerWindowBase(Window *parent) : Window()
00767   {
00768     this->parent = parent;
00769   }
00770 
00771   virtual ~PickerWindowBase();
00772 };
00773 
00777 enum WindowFlags {
00778   WF_TIMEOUT_TRIGGER   = 1,       
00779   WF_TIMEOUT_BEGIN     = 7,       
00780   WF_TIMEOUT_MASK      = 7,       
00781   WF_DRAGGING          = 1 <<  3, 
00782   WF_SIZING_RIGHT      = 1 <<  4, 
00783   WF_SIZING_LEFT       = 1 <<  5, 
00784   WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, 
00785   WF_STICKY            = 1 <<  6, 
00786 
00787   WF_DISABLE_VP_SCROLL = 1 <<  7, 
00788 
00789   WF_WHITE_BORDER_ONE  = 1 <<  8,
00790   WF_WHITE_BORDER_MASK = 1 <<  9 | WF_WHITE_BORDER_ONE,
00791 
00792   WF_CENTERED          = 1 << 10, 
00793 };
00794 
00795 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
00796 Window *FindWindowFromPt(int x, int y);
00797 
00804 template <typename Wcls>
00805 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
00806 {
00807   if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
00808   return new Wcls(desc, window_number);
00809 }
00810 
00811 void RelocateAllWindows(int neww, int newh);
00812 
00813 /* misc_gui.cpp */
00814 enum TooltipCloseCondition {
00815   TCC_RIGHT_CLICK,
00816   TCC_LEFT_CLICK,
00817   TCC_HOVER,
00818 };
00819 
00820 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
00821 
00822 /* widget.cpp */
00823 int GetWidgetFromPos(const Window *w, int x, int y);
00824 
00826 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)  for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
00827 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
00828 #define FOR_ALL_WINDOWS_FROM_BACK(w)  FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
00829 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
00830 
00831 extern Point _cursorpos_drag_start;
00832 
00833 extern int _scrollbar_start_pos;
00834 extern int _scrollbar_size;
00835 extern byte _scroller_click_timeout;
00836 
00837 extern bool _scrolling_viewport;
00838 extern bool _mouse_hovering;
00839 
00841 enum SpecialMouseMode {
00842   WSM_NONE,     
00843   WSM_DRAGDROP, 
00844   WSM_SIZING,   
00845   WSM_PRESIZE,  
00846 };
00847 extern SpecialMouseMode _special_mouse_mode;
00848 
00849 void SetFocusedWindow(Window *w);
00850 
00851 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
00852 
00853 #endif /* WINDOW_GUI_H */

Generated on Fri Mar 4 21:37:09 2011 for OpenTTD by  doxygen 1.6.1