window_gui.h

Go to the documentation of this file.
00001 /* $Id: window_gui.h 26024 2013-11-17 13:35:48Z 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 #include "core/smallmap_type.hpp"
00022 #include "string_type.h"
00023 
00025 enum EventState {
00026   ES_HANDLED,     
00027   ES_NOT_HANDLED, 
00028 };
00029 
00033 enum FrameFlags {
00034   FR_NONE         =  0,
00035   FR_TRANSPARENT  =  1 << 0,  
00036   FR_BORDERONLY   =  1 << 4,  
00037   FR_LOWERED      =  1 << 5,  
00038   FR_DARKENED     =  1 << 6,  
00039 };
00040 
00041 DECLARE_ENUM_AS_BIT_SET(FrameFlags)
00042 
00043 
00044 enum WidgetDrawDistances {
00045   /* WWT_IMGBTN(_2) */
00046   WD_IMGBTN_LEFT    = 1,      
00047   WD_IMGBTN_RIGHT   = 2,      
00048   WD_IMGBTN_TOP     = 1,      
00049   WD_IMGBTN_BOTTOM  = 2,      
00050 
00051   /* WWT_INSET */
00052   WD_INSET_LEFT  = 2,         
00053   WD_INSET_RIGHT = 2,         
00054   WD_INSET_TOP   = 1,         
00055 
00056   WD_SCROLLBAR_LEFT   = 2,    
00057   WD_SCROLLBAR_RIGHT  = 2,    
00058   WD_SCROLLBAR_TOP    = 2,    
00059   WD_SCROLLBAR_BOTTOM = 2,    
00060 
00061   /* Size of the pure frame bevel without any padding. */
00062   WD_BEVEL_LEFT       = 1,    
00063   WD_BEVEL_RIGHT      = 1,    
00064   WD_BEVEL_TOP        = 1,    
00065   WD_BEVEL_BOTTOM     = 1,    
00066 
00067   /* FrameRect widgets, all text buttons, panel, editbox */
00068   WD_FRAMERECT_LEFT   = 2,    
00069   WD_FRAMERECT_RIGHT  = 2,    
00070   WD_FRAMERECT_TOP    = 1,    
00071   WD_FRAMERECT_BOTTOM = 1,    
00072 
00073   /* Extra space at top/bottom of text panels */
00074   WD_TEXTPANEL_TOP    = 6,    
00075   WD_TEXTPANEL_BOTTOM = 6,    
00076 
00077   /* WWT_FRAME */
00078   WD_FRAMETEXT_LEFT   = 6,    
00079   WD_FRAMETEXT_RIGHT  = 6,    
00080   WD_FRAMETEXT_TOP    = 6,    
00081   WD_FRAMETEXT_BOTTOM = 6,    
00082 
00083   /* WWT_MATRIX */
00084   WD_MATRIX_LEFT   = 2,       
00085   WD_MATRIX_RIGHT  = 2,       
00086   WD_MATRIX_TOP    = 3,       
00087   WD_MATRIX_BOTTOM = 1,       
00088 
00089   /* WWT_SHADEBOX */
00090   WD_SHADEBOX_WIDTH  = 12,    
00091   WD_SHADEBOX_LEFT   = 2,     
00092   WD_SHADEBOX_RIGHT  = 2,     
00093   WD_SHADEBOX_TOP    = 3,     
00094   WD_SHADEBOX_BOTTOM = 3,     
00095 
00096   /* WWT_STICKYBOX */
00097   WD_STICKYBOX_WIDTH  = 12,   
00098   WD_STICKYBOX_LEFT   = 2,    
00099   WD_STICKYBOX_RIGHT  = 2,    
00100   WD_STICKYBOX_TOP    = 3,    
00101   WD_STICKYBOX_BOTTOM = 3,    
00102 
00103   /* WWT_DEBUGBOX */
00104   WD_DEBUGBOX_WIDTH  = 12,    
00105   WD_DEBUGBOX_LEFT   = 2,     
00106   WD_DEBUGBOX_RIGHT  = 2,     
00107   WD_DEBUGBOX_TOP    = 3,     
00108   WD_DEBUGBOX_BOTTOM = 3,     
00109 
00110   /* WWT_RESIZEBOX */
00111   WD_RESIZEBOX_WIDTH  = 12,   
00112   WD_RESIZEBOX_LEFT   = 3,    
00113   WD_RESIZEBOX_RIGHT  = 2,    
00114   WD_RESIZEBOX_TOP    = 3,    
00115   WD_RESIZEBOX_BOTTOM = 2,    
00116 
00117   /* WWT_CLOSEBOX */
00118   WD_CLOSEBOX_WIDTH  = 11,    
00119   WD_CLOSEBOX_LEFT   = 2,     
00120   WD_CLOSEBOX_RIGHT  = 1,     
00121   WD_CLOSEBOX_TOP    = 2,     
00122   WD_CLOSEBOX_BOTTOM = 2,     
00123 
00124   /* WWT_CAPTION */
00125   WD_CAPTION_HEIGHT     = 14, 
00126   WD_CAPTIONTEXT_LEFT   = 2,  
00127   WD_CAPTIONTEXT_RIGHT  = 2,  
00128   WD_CAPTIONTEXT_TOP    = 2,  
00129   WD_CAPTIONTEXT_BOTTOM = 2,  
00130 
00131   /* Dropdown widget. */
00132   WD_DROPDOWN_HEIGHT     = 12, 
00133   WD_DROPDOWNTEXT_LEFT   = 2,  
00134   WD_DROPDOWNTEXT_RIGHT  = 2,  
00135   WD_DROPDOWNTEXT_TOP    = 1,  
00136   WD_DROPDOWNTEXT_BOTTOM = 1,  
00137 
00138   WD_SORTBUTTON_ARROW_WIDTH = 11, 
00139 
00140   WD_PAR_VSEP_NORMAL = 2,      
00141   WD_PAR_VSEP_WIDE   = 8,      
00142 };
00143 
00144 /* widget.cpp */
00145 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
00146 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
00147 
00148 /* window.cpp */
00149 extern Window *_z_front_window;
00150 extern Window *_z_back_window;
00151 extern Window *_focused_window;
00152 
00153 
00155 enum WindowPosition {
00156   WDP_MANUAL,        
00157   WDP_AUTO,          
00158   WDP_CENTER,        
00159   WDP_ALIGN_TOOLBAR, 
00160 };
00161 
00162 Point GetToolbarAlignedWindowPosition(int window_width);
00163 
00167 struct WindowDesc : ZeroedMemoryAllocator {
00168 
00169   WindowDesc(WindowPosition default_pos, int16 def_width, int16 def_height,
00170       WindowClass window_class, WindowClass parent_class, uint32 flags,
00171       const NWidgetPart *nwid_parts, int16 nwid_length);
00172 
00173   ~WindowDesc();
00174 
00175   WindowPosition default_pos;    
00176   int16 default_width;           
00177   int16 default_height;          
00178   WindowClass cls;               
00179   WindowClass parent_cls;        
00180   uint32 flags;                  
00181   const NWidgetPart *nwid_parts; 
00182   int16 nwid_length;             
00183 };
00184 
00188 enum WindowDefaultFlag {
00189   WDF_CONSTRUCTION    =   1 << 0, 
00190   WDF_MODAL           =   1 << 1, 
00191   WDF_NO_FOCUS        =   1 << 2, 
00192 };
00193 
00197 struct ResizeInfo {
00198   uint step_width;  
00199   uint step_height; 
00200 };
00201 
00203 enum SortButtonState {
00204   SBS_OFF,  
00205   SBS_DOWN, 
00206   SBS_UP,   
00207 };
00208 
00212 enum WindowFlags {
00213   WF_TIMEOUT           = 1 <<  0, 
00214 
00215   WF_DRAGGING          = 1 <<  3, 
00216   WF_SIZING_RIGHT      = 1 <<  4, 
00217   WF_SIZING_LEFT       = 1 <<  5, 
00218   WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, 
00219   WF_STICKY            = 1 <<  6, 
00220   WF_DISABLE_VP_SCROLL = 1 <<  7, 
00221   WF_WHITE_BORDER      = 1 <<  8, 
00222   WF_HIGHLIGHTED       = 1 <<  9, 
00223   WF_CENTERED          = 1 << 10, 
00224 };
00225 DECLARE_ENUM_AS_BIT_SET(WindowFlags)
00226 
00227 static const int TIMEOUT_DURATION = 7; 
00228 static const int WHITE_BORDER_DURATION = 3; 
00229 
00237 struct ViewportData : ViewPort {
00238   VehicleID follow_vehicle; 
00239   int32 scrollpos_x;        
00240   int32 scrollpos_y;        
00241   int32 dest_scrollpos_x;   
00242   int32 dest_scrollpos_y;   
00243 };
00244 
00245 struct QueryString;
00246 
00250 struct Window : ZeroedMemoryAllocator {
00251 protected:
00252   void InitializeData(const WindowDesc *desc, WindowNumber window_number);
00253   void InitializePositionSize(int x, int y, int min_width, int min_height);
00254   void FindWindowPlacementAndResize(int def_width, int def_height);
00255 
00256   SmallVector<int, 4> scheduled_invalidation_data;  
00257 
00258 public:
00259   Window();
00260 
00261   virtual ~Window();
00262 
00269   inline void *operator new[](size_t size)
00270   {
00271     NOT_REACHED();
00272   }
00273 
00279   inline void operator delete(void *ptr)
00280   {
00281   }
00282 
00283   WindowFlags flags;          
00284   WindowClass window_class;   
00285   WindowNumber window_number; 
00286 
00287   uint8 timeout_timer;      
00288   uint8 white_border_timer; 
00289 
00290   int left;   
00291   int top;    
00292   int width;  
00293   int height; 
00294 
00295   ResizeInfo resize;  
00296 
00297   Owner owner;        
00298 
00299   ViewportData *viewport;          
00300   uint32 desc_flags;               
00301   const NWidgetCore *nested_focus; 
00302   SmallMap<int, QueryString*> querystrings; 
00303   NWidgetBase *nested_root;        
00304   NWidgetBase **nested_array;      
00305   uint nested_array_size;          
00306   NWidgetStacked *shade_select;    
00307   Dimension unshaded_size;         
00308 
00309   int scrolling_scrollbar;         
00310 
00311   Window *parent;                  
00312   Window *z_front;                 
00313   Window *z_back;                  
00314 
00315   template <class NWID>
00316   inline const NWID *GetWidget(uint widnum) const;
00317   template <class NWID>
00318   inline NWID *GetWidget(uint widnum);
00319 
00320   const Scrollbar *GetScrollbar(uint widnum) const;
00321   Scrollbar *GetScrollbar(uint widnum);
00322 
00323   const QueryString *GetQueryString(uint widnum) const;
00324   QueryString *GetQueryString(uint widnum);
00325 
00326   virtual const char *GetFocusedText() const;
00327   virtual const char *GetCaret() const;
00328   virtual const char *GetMarkedText(size_t *length) const;
00329   virtual Point GetCaretPosition() const;
00330   virtual Rect GetTextBoundingRect(const char *from, const char *to) const;
00331   virtual const char *GetTextCharacterAtPosition(const Point &pt) const;
00332 
00333   void InitNested(const WindowDesc *desc, WindowNumber number = 0);
00334   void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
00335   void FinishInitNested(const WindowDesc *desc, WindowNumber window_number = 0);
00336 
00340   inline void SetTimeout()
00341   {
00342     this->flags |= WF_TIMEOUT;
00343     this->timeout_timer = TIMEOUT_DURATION;
00344   }
00345 
00349   inline void SetWhiteBorder()
00350   {
00351     this->flags |= WF_WHITE_BORDER;
00352     this->white_border_timer = WHITE_BORDER_DURATION;
00353   }
00354 
00355   void DisableAllWidgetHighlight();
00356   void SetWidgetHighlight(byte widget_index, TextColour highlighted_colour);
00357   bool IsWidgetHighlighted(byte widget_index) const;
00358 
00366   inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
00367   {
00368     assert(widget_index < this->nested_array_size);
00369     if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
00370   }
00371 
00376   inline void DisableWidget(byte widget_index)
00377   {
00378     SetWidgetDisabledState(widget_index, true);
00379   }
00380 
00385   inline void EnableWidget(byte widget_index)
00386   {
00387     SetWidgetDisabledState(widget_index, false);
00388   }
00389 
00395   inline bool IsWidgetDisabled(byte widget_index) const
00396   {
00397     assert(widget_index < this->nested_array_size);
00398     return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
00399   }
00400 
00406   inline bool IsWidgetFocused(byte widget_index) const
00407   {
00408     return this->nested_focus != NULL && this->nested_focus->index == widget_index;
00409   }
00410 
00417   inline bool IsWidgetGloballyFocused(byte widget_index) const
00418   {
00419     return _focused_window == this && IsWidgetFocused(widget_index);
00420   }
00421 
00427   inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
00428   {
00429     assert(widget_index < this->nested_array_size);
00430     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
00431   }
00432 
00437   inline void ToggleWidgetLoweredState(byte widget_index)
00438   {
00439     assert(widget_index < this->nested_array_size);
00440     bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00441     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
00442   }
00443 
00448   inline void LowerWidget(byte widget_index)
00449   {
00450     SetWidgetLoweredState(widget_index, true);
00451   }
00452 
00457   inline void RaiseWidget(byte widget_index)
00458   {
00459     SetWidgetLoweredState(widget_index, false);
00460   }
00461 
00467   inline bool IsWidgetLowered(byte widget_index) const
00468   {
00469     assert(widget_index < this->nested_array_size);
00470     return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00471   }
00472 
00473   void UnfocusFocusedWidget();
00474   bool SetFocusedWidget(int widget_index);
00475 
00476   EventState HandleEditBoxKey(int wid, WChar key, uint16 keycode);
00477   virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end);
00478 
00479   void HandleButtonClick(byte widget);
00480   int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
00481 
00482   void RaiseButtons(bool autoraise = false);
00483   void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
00484   void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
00485   void SetWidgetDirty(byte widget_index) const;
00486 
00487   void DrawWidgets() const;
00488   void DrawViewport() const;
00489   void DrawSortButtonState(int widget, SortButtonState state) const;
00490 
00491   void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
00492 
00493   void SetDirty() const;
00494   void ReInit(int rx = 0, int ry = 0);
00495 
00497   inline bool IsShaded() const
00498   {
00499     return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
00500   }
00501 
00502   void SetShaded(bool make_shaded);
00503 
00504   void InvalidateData(int data = 0, bool gui_scope = true);
00505   void ProcessScheduledInvalidations();
00506   void ProcessHighlightedInvalidations();
00507 
00508   /*** Event handling ***/
00509 
00514   virtual void OnInit() { }
00515 
00524   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
00525 
00530   virtual void OnPaint()
00531   {
00532     this->DrawWidgets();
00533   }
00534 
00541   virtual void DrawWidget(const Rect &r, int widget) const {}
00542 
00555   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
00556 
00563   virtual void SetStringParameters(int widget) const {}
00564 
00568   virtual void OnFocus() {}
00569 
00570   virtual void OnFocusLost();
00571 
00579   virtual EventState OnKeyPress(WChar key, uint16 keycode) { return ES_NOT_HANDLED; }
00580 
00586   virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
00587 
00588 
00595   virtual void OnClick(Point pt, int widget, int click_count) {}
00596 
00604   virtual bool OnRightClick(Point pt, int widget) { return false; }
00605 
00611   virtual void OnHover(Point pt, int widget) {}
00612 
00618   virtual void OnMouseDrag(Point pt, int widget) {}
00619 
00625   virtual void OnDragDrop(Point pt, int widget) {}
00626 
00631   virtual void OnScroll(Point delta) {}
00632 
00639   virtual void OnMouseOver(Point pt, int widget) {}
00640 
00645   virtual void OnMouseWheel(int wheel) {}
00646 
00647 
00651   virtual void OnMouseLoop() {}
00652 
00656   virtual void OnTick() {}
00657 
00661   virtual void OnHundredthTick() {}
00662 
00666   virtual void OnTimeout() {}
00667 
00668 
00673   virtual void OnResize() {}
00674 
00680   virtual void OnDropdownSelect(int widget, int index) {}
00681 
00682   virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close);
00683 
00688   virtual void OnEditboxChanged(int widget) {}
00689 
00696   virtual void OnQueryTextFinished(char *str) {}
00697 
00703   virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
00704 
00711   virtual void OnPlaceObject(Point pt, TileIndex tile) {}
00712 
00718   virtual bool OnVehicleSelect(const struct Vehicle *v) { return false; }
00719 
00723   virtual void OnPlaceObjectAbort() {}
00724 
00725 
00733   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
00734 
00744   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
00745 
00753   virtual void OnPlacePresize(Point pt, TileIndex tile) {}
00754 
00755   /*** End of the event handling ***/
00756 
00761   virtual bool IsNewGRFInspectable() const { return false; }
00762 
00769   virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
00770 };
00771 
00778 template <class NWID>
00779 inline NWID *Window::GetWidget(uint widnum)
00780 {
00781   if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
00782   NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
00783   assert(nwid != NULL);
00784   return nwid;
00785 }
00786 
00788 template <>
00789 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
00790 {
00791   if (widnum >= this->nested_array_size) return NULL;
00792   return this->nested_array[widnum];
00793 }
00794 
00801 template <class NWID>
00802 inline const NWID *Window::GetWidget(uint widnum) const
00803 {
00804   return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
00805 }
00806 
00807 
00811 class PickerWindowBase : public Window {
00812 
00813 public:
00814   PickerWindowBase(Window *parent) : Window()
00815   {
00816     this->parent = parent;
00817   }
00818 
00819   virtual ~PickerWindowBase();
00820 };
00821 
00822 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
00823 Window *FindWindowFromPt(int x, int y);
00824 
00831 template <typename Wcls>
00832 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
00833 {
00834   if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
00835   return new Wcls(desc, window_number);
00836 }
00837 
00838 void RelocateAllWindows(int neww, int newh);
00839 
00840 /* misc_gui.cpp */
00841 enum TooltipCloseCondition {
00842   TCC_RIGHT_CLICK,
00843   TCC_LEFT_CLICK,
00844   TCC_HOVER,
00845 };
00846 
00847 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
00848 
00849 /* widget.cpp */
00850 int GetWidgetFromPos(const Window *w, int x, int y);
00851 
00853 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)  for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
00854 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
00855 #define FOR_ALL_WINDOWS_FROM_BACK(w)  FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
00856 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
00857 
00858 extern Point _cursorpos_drag_start;
00859 
00860 extern int _scrollbar_start_pos;
00861 extern int _scrollbar_size;
00862 extern byte _scroller_click_timeout;
00863 
00864 extern bool _scrolling_viewport;
00865 extern bool _mouse_hovering;
00866 
00868 enum SpecialMouseMode {
00869   WSM_NONE,     
00870   WSM_DRAGDROP, 
00871   WSM_SIZING,   
00872   WSM_PRESIZE,  
00873 };
00874 extern SpecialMouseMode _special_mouse_mode;
00875 
00876 void SetFocusedWindow(Window *w);
00877 
00878 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
00879 
00880 #endif /* WINDOW_GUI_H */