window_gui.h

Go to the documentation of this file.
00001 /* $Id: window_gui.h 20091 2010-07-08 19:39:20Z 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 "core/math_func.hpp"
00016 #include "vehicle_type.h"
00017 #include "viewport_type.h"
00018 #include "company_type.h"
00019 #include "tile_type.h"
00020 #include "widget_type.h"
00021 
00025 enum FrameFlags {
00026   FR_NONE         =  0,
00027   FR_TRANSPARENT  =  1 << 0,  
00028   FR_BORDERONLY   =  1 << 4,  
00029   FR_LOWERED      =  1 << 5,  
00030   FR_DARKENED     =  1 << 6,  
00031 };
00032 
00033 DECLARE_ENUM_AS_BIT_SET(FrameFlags);
00034 
00036 enum WidgetDrawDistances {
00037   /* WWT_IMGBTN(_2) */
00038   WD_IMGBTN_LEFT    = 1,      
00039   WD_IMGBTN_RIGHT   = 2,      
00040   WD_IMGBTN_TOP     = 1,      
00041   WD_IMGBTN_BOTTOM  = 2,      
00042 
00043   /* WWT_INSET */
00044   WD_INSET_LEFT  = 2,         
00045   WD_INSET_RIGHT = 2,         
00046   WD_INSET_TOP   = 1,         
00047 
00048   WD_VSCROLLBAR_WIDTH  = 12,  
00049 
00050   WD_HSCROLLBAR_HEIGHT = 12,  
00051 
00052   /* FrameRect widgets, all text buttons, panel, editbox */
00053   WD_FRAMERECT_LEFT   = 2,    
00054   WD_FRAMERECT_RIGHT  = 2,    
00055   WD_FRAMERECT_TOP    = 1,    
00056   WD_FRAMERECT_BOTTOM = 1,    
00057 
00058   /* Extra space at top/bottom of text panels */
00059   WD_TEXTPANEL_TOP    = 6,    
00060   WD_TEXTPANEL_BOTTOM = 6,    
00061 
00062   /* WWT_FRAME */
00063   WD_FRAMETEXT_LEFT   = 6,    
00064   WD_FRAMETEXT_RIGHT  = 6,    
00065   WD_FRAMETEXT_TOP    = 6,    
00066   WD_FRAMETEXT_BOTTOM = 6,    
00067 
00068   /* WWT_MATRIX */
00069   WD_MATRIX_LEFT   = 2,       
00070   WD_MATRIX_RIGHT  = 2,       
00071   WD_MATRIX_TOP    = 3,       
00072   WD_MATRIX_BOTTOM = 1,       
00073 
00074   /* WWT_SHADEBOX */
00075   WD_SHADEBOX_WIDTH  = 12,    
00076   WD_SHADEBOX_LEFT   = 2,     
00077   WD_SHADEBOX_RIGHT  = 2,     
00078   WD_SHADEBOX_TOP    = 3,     
00079   WD_SHADEBOX_BOTTOM = 3,     
00080 
00081   /* WWT_STICKYBOX */
00082   WD_STICKYBOX_WIDTH  = 12,   
00083   WD_STICKYBOX_LEFT   = 2,    
00084   WD_STICKYBOX_RIGHT  = 2,    
00085   WD_STICKYBOX_TOP    = 3,    
00086   WD_STICKYBOX_BOTTOM = 3,    
00087 
00088   /* WWT_RESIZEBOX */
00089   WD_RESIZEBOX_WIDTH  = 12,   
00090   WD_RESIZEBOX_LEFT   = 3,    
00091   WD_RESIZEBOX_RIGHT  = 2,    
00092   WD_RESIZEBOX_TOP    = 3,    
00093   WD_RESIZEBOX_BOTTOM = 2,    
00094 
00095   /* WWT_CLOSEBOX */
00096   WD_CLOSEBOX_WIDTH  = 11,    
00097   WD_CLOSEBOX_LEFT   = 2,     
00098   WD_CLOSEBOX_RIGHT  = 1,     
00099   WD_CLOSEBOX_TOP    = 2,     
00100   WD_CLOSEBOX_BOTTOM = 2,     
00101 
00102   /* WWT_CAPTION */
00103   WD_CAPTION_HEIGHT     = 14, 
00104   WD_CAPTIONTEXT_LEFT   = 2,  
00105   WD_CAPTIONTEXT_RIGHT  = 2,  
00106   WD_CAPTIONTEXT_TOP    = 2,  
00107   WD_CAPTIONTEXT_BOTTOM = 2,  
00108 
00109   /* Dropdown widget. */
00110   WD_DROPDOWN_HEIGHT     = 12, 
00111   WD_DROPDOWNTEXT_LEFT   = 2,  
00112   WD_DROPDOWNTEXT_RIGHT  = 14, 
00113   WD_DROPDOWNTEXT_TOP    = 1,  
00114   WD_DROPDOWNTEXT_BOTTOM = 1,  
00115 
00116   WD_SORTBUTTON_ARROW_WIDTH = 11, 
00117 
00118   WD_PAR_VSEP_NORMAL = 2,      
00119   WD_PAR_VSEP_WIDE   = 8,      
00120 };
00121 
00122 /* widget.cpp */
00123 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
00124 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
00125 
00126 /* window.cpp */
00127 extern Window *_z_front_window;
00128 extern Window *_z_back_window;
00129 extern Window *_focused_window;
00130 
00131 
00133 enum WindowPosition {
00134   WDP_MANUAL,        
00135   WDP_AUTO,          
00136   WDP_CENTER,        
00137   WDP_ALIGN_TOOLBAR, 
00138 };
00139 
00140 Point GetToolbarAlignedWindowPosition(int window_width);
00141 
00145 struct WindowDesc : ZeroedMemoryAllocator {
00146 
00147   WindowDesc(WindowPosition default_pos, int16 def_width, int16 def_height,
00148       WindowClass window_class, WindowClass parent_class, uint32 flags,
00149       const NWidgetPart *nwid_parts, int16 nwid_length);
00150 
00151   ~WindowDesc();
00152 
00153   WindowPosition default_pos;    
00154   int16 default_width;           
00155   int16 default_height;          
00156   WindowClass cls;               
00157   WindowClass parent_cls;        
00158   uint32 flags;                  
00159   const NWidgetPart *nwid_parts; 
00160   int16 nwid_length;             
00161 };
00162 
00166 enum WindowDefaultFlag {
00167   WDF_CONSTRUCTION    =   1 << 0, 
00168   WDF_UNCLICK_BUTTONS =   1 << 1, 
00169   WDF_MODAL           =   1 << 2, 
00170   WDF_NO_FOCUS        =   1 << 3, 
00171 };
00172 
00176 class Scrollbar {
00177 private:
00178   const bool is_vertical; 
00179   uint16 count;           
00180   uint16 cap;             
00181   uint16 pos;             
00182 
00183 public:
00184   Scrollbar(bool is_vertical) : is_vertical(is_vertical)
00185   {
00186   }
00187 
00192   FORCEINLINE uint16 GetCount() const
00193   {
00194     return this->count;
00195   }
00196 
00201   FORCEINLINE uint16 GetCapacity() const
00202   {
00203     return this->cap;
00204   }
00205 
00210   FORCEINLINE uint16 GetPosition() const
00211   {
00212     return this->pos;
00213   }
00214 
00220   FORCEINLINE bool IsVisible(uint16 item) const
00221   {
00222     return IsInsideBS(item, this->GetPosition(), this->GetCapacity());
00223   }
00224 
00230   void SetCount(int num)
00231   {
00232     assert(num >= 0);
00233     assert(num <= MAX_UVALUE(uint16));
00234 
00235     this->count = num;
00236     num -= this->cap;
00237     if (num < 0) num = 0;
00238     if (num < this->pos) this->pos = num;
00239   }
00240 
00246   void SetCapacity(int capacity)
00247   {
00248     assert(capacity > 0);
00249     assert(capacity <= MAX_UVALUE(uint16));
00250 
00251     this->cap = capacity;
00252     if (this->cap + this->pos > this->count) this->pos = max(0, this->count - this->cap);
00253   }
00254 
00255   void SetCapacityFromWidget(Window *w, int widget, int padding = 0);
00256 
00261   void SetPosition(int position)
00262   {
00263     assert(position >= 0);
00264     assert(this->count <= this->cap ? (position == 0) : (position + this->cap <= this->count));
00265     this->pos = position;
00266   }
00267 
00273   void UpdatePosition(int difference)
00274   {
00275     if (difference == 0) return;
00276     this->SetPosition(Clamp(this->pos + difference, 0, max(this->count - this->cap, 0)));
00277   }
00278 
00285   void ScrollTowards(int position)
00286   {
00287     if (position < this->GetPosition()) {
00288       /* scroll up to the item */
00289       this->SetPosition(position);
00290     } else if (position >= this->GetPosition() + this->GetCapacity()) {
00291       /* scroll down so that the item is at the bottom */
00292       this->SetPosition(position - this->GetCapacity() + 1);
00293     }
00294   }
00295 };
00296 
00300 struct ResizeInfo {
00301   uint step_width;  
00302   uint step_height; 
00303 };
00304 
00306 enum SortButtonState {
00307   SBS_OFF,  
00308   SBS_DOWN, 
00309   SBS_UP,   
00310 };
00311 
00319 struct ViewportData : ViewPort {
00320   VehicleID follow_vehicle; 
00321   int32 scrollpos_x;        
00322   int32 scrollpos_y;        
00323   int32 dest_scrollpos_x;   
00324   int32 dest_scrollpos_y;   
00325 };
00326 
00330 struct Window : ZeroedMemoryAllocator {
00332   enum EventState {
00333     ES_HANDLED,     
00334     ES_NOT_HANDLED, 
00335   };
00336 
00337 protected:
00338   void InitializeData(const WindowDesc *desc, WindowNumber window_number);
00339   void InitializePositionSize(int x, int y, int min_width, int min_height);
00340   void FindWindowPlacementAndResize(int def_width, int def_height);
00341 
00342 public:
00343   Window();
00344 
00345   virtual ~Window();
00346 
00353   FORCEINLINE void *operator new[](size_t size)
00354   {
00355     NOT_REACHED();
00356   }
00357 
00363   FORCEINLINE void operator delete(void *ptr)
00364   {
00365   }
00366 
00367   uint16 flags4;              
00368   WindowClass window_class;   
00369   WindowNumber window_number; 
00370 
00371   int left;   
00372   int top;    
00373   int width;  
00374   int height; 
00375 
00376   Scrollbar hscroll;  
00377   Scrollbar vscroll;  
00378   Scrollbar vscroll2; 
00379   ResizeInfo resize;  
00380 
00381   Owner owner;        
00382 
00383   ViewportData *viewport;          
00384   uint32 desc_flags;               
00385   const NWidgetCore *nested_focus; 
00386   NWidgetBase *nested_root;        
00387   NWidgetBase **nested_array;      
00388   uint nested_array_size;          
00389   NWidgetStacked *shade_select;    
00390   Dimension unshaded_size;         
00391 
00392   Window *parent;                  
00393   Window *z_front;                 
00394   Window *z_back;                  
00395 
00396   template <class NWID>
00397   inline const NWID *GetWidget(uint widnum) const;
00398   template <class NWID>
00399   inline NWID *GetWidget(uint widnum);
00400 
00401 
00402   void InitNested(const WindowDesc *desc, WindowNumber number = 0);
00403   void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
00404   void FinishInitNested(const WindowDesc *desc, WindowNumber window_number);
00405 
00413   inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
00414   {
00415     assert(widget_index < this->nested_array_size);
00416     if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
00417   }
00418 
00423   inline void DisableWidget(byte widget_index)
00424   {
00425     SetWidgetDisabledState(widget_index, true);
00426   }
00427 
00432   inline void EnableWidget(byte widget_index)
00433   {
00434     SetWidgetDisabledState(widget_index, false);
00435   }
00436 
00442   inline bool IsWidgetDisabled(byte widget_index) const
00443   {
00444     assert(widget_index < this->nested_array_size);
00445     return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
00446   }
00447 
00453   inline bool IsWidgetFocused(byte widget_index) const
00454   {
00455     return this->nested_focus != NULL && this->nested_focus->index == widget_index;
00456   }
00457 
00464   inline bool IsWidgetGloballyFocused(byte widget_index) const
00465   {
00466     return _focused_window == this && IsWidgetFocused(widget_index);
00467   }
00468 
00474   inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
00475   {
00476     assert(widget_index < this->nested_array_size);
00477     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
00478   }
00479 
00484   inline void ToggleWidgetLoweredState(byte widget_index)
00485   {
00486     assert(widget_index < this->nested_array_size);
00487     bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00488     this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
00489   }
00490 
00495   inline void LowerWidget(byte widget_index)
00496   {
00497     SetWidgetLoweredState(widget_index, true);
00498   }
00499 
00504   inline void RaiseWidget(byte widget_index)
00505   {
00506     SetWidgetLoweredState(widget_index, false);
00507   }
00508 
00514   inline bool IsWidgetLowered(byte widget_index) const
00515   {
00516     assert(widget_index < this->nested_array_size);
00517     return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00518   }
00519 
00520   void UnfocusFocusedWidget();
00521   bool SetFocusedWidget(byte widget_index);
00522 
00523   void HandleButtonClick(byte widget);
00524 
00525   void RaiseButtons(bool autoraise = false);
00526   void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
00527   void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
00528   void SetWidgetDirty(byte widget_index) const;
00529 
00530   void DrawWidgets() const;
00531   void DrawViewport() const;
00532   void DrawSortButtonState(int widget, SortButtonState state) const;
00533 
00534   void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
00535 
00536   void SetDirty() const;
00537   void ReInit(int rx = 0, int ry = 0);
00538 
00540   inline bool IsShaded() const
00541   {
00542     return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
00543   }
00544 
00545   void SetShaded(bool make_shaded);
00546 
00551   void InvalidateData(int data = 0)
00552   {
00553     this->SetDirty();
00554     this->OnInvalidateData(data);
00555   }
00556 
00557   /*** Event handling ***/
00558 
00563   virtual void OnInit() { }
00564 
00573   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
00574 
00579   virtual void OnPaint() {}
00580 
00587   virtual void DrawWidget(const Rect &r, int widget) const {}
00588 
00601   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
00602 
00609   virtual void SetStringParameters(int widget) const {}
00610 
00614   virtual void OnFocus() {}
00615 
00619   virtual void OnFocusLost() {}
00620 
00628   virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
00629 
00635   virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
00636 
00637 
00644   virtual void OnClick(Point pt, int widget, int click_count) {}
00645 
00651   virtual void OnRightClick(Point pt, int widget) {}
00652 
00658   virtual void OnDragDrop(Point pt, int widget) {}
00659 
00664   virtual void OnScroll(Point delta) {}
00665 
00672   virtual void OnMouseOver(Point pt, int widget) {}
00673 
00678   virtual void OnMouseWheel(int wheel) {}
00679 
00680 
00684   virtual void OnMouseLoop() {}
00685 
00689   virtual void OnTick() {}
00690 
00694   virtual void OnHundredthTick() {}
00695 
00699   virtual void OnTimeout() {}
00700 
00701 
00706   virtual void OnResize() {}
00707 
00713   virtual void OnDropdownSelect(int widget, int index) {}
00714 
00721   virtual void OnQueryTextFinished(char *str) {}
00722 
00727   virtual void OnInvalidateData(int data = 0) {}
00728 
00729 
00736   virtual void OnPlaceObject(Point pt, TileIndex tile) {}
00737 
00741   virtual void OnPlaceObjectAbort() {}
00742 
00743 
00751   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
00752 
00762   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
00763 
00771   virtual void OnPlacePresize(Point pt, TileIndex tile) {}
00772 
00773   /*** End of the event handling ***/
00774 };
00775 
00781 template <class NWID>
00782 inline NWID *Window::GetWidget(uint widnum)
00783 {
00784   if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
00785   NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
00786   assert(nwid != NULL);
00787   return nwid;
00788 }
00789 
00791 template <>
00792 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
00793 {
00794   if (widnum >= this->nested_array_size) return NULL;
00795   return this->nested_array[widnum];
00796 }
00797 
00803 template <class NWID>
00804 inline const NWID *Window::GetWidget(uint widnum) const
00805 {
00806   return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
00807 }
00808 
00809 
00813 class PickerWindowBase : public Window {
00814 
00815 public:
00816   PickerWindowBase(Window *parent) : Window()
00817   {
00818     this->parent = parent;
00819   };
00820 
00821   virtual ~PickerWindowBase();
00822 };
00823 
00827 enum WindowFlags {
00828   WF_TIMEOUT_TRIGGER   = 1,       
00829   WF_TIMEOUT_BEGIN     = 7,       
00830   WF_TIMEOUT_MASK      = 7,       
00831   WF_DRAGGING          = 1 <<  3, 
00832   WF_SCROLL_UP         = 1 <<  4, 
00833   WF_SCROLL_DOWN       = 1 <<  5, 
00834   WF_SCROLL_MIDDLE     = 1 <<  6, 
00835   WF_SCROLL2           = 1 <<  7,
00836   WF_HSCROLL           = 1 <<  8,
00837   WF_SIZING_RIGHT      = 1 <<  9, 
00838   WF_SIZING_LEFT       = 1 << 10, 
00839   WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, 
00840   WF_STICKY            = 1 << 11, 
00841 
00842   WF_DISABLE_VP_SCROLL = 1 << 12, 
00843 
00844   WF_WHITE_BORDER_ONE  = 1 << 13,
00845   WF_WHITE_BORDER_MASK = 1 << 14 | WF_WHITE_BORDER_ONE,
00846 
00847   WF_CENTERED          = 1 << 15, 
00848 };
00849 
00850 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
00851 Window *FindWindowFromPt(int x, int y);
00852 
00859 template <typename Wcls>
00860 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
00861 {
00862   if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
00863   return new Wcls(desc, window_number);
00864 }
00865 
00866 void RelocateAllWindows(int neww, int newh);
00867 
00868 /* misc_gui.cpp */
00869 void GuiShowTooltips(StringID str, uint paramcount = 0, const uint64 params[] = NULL, bool use_left_mouse_button = false);
00870 
00871 /* widget.cpp */
00872 int GetWidgetFromPos(const Window *w, int x, int y);
00873 
00875 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)  for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
00876 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
00877 #define FOR_ALL_WINDOWS_FROM_BACK(w)  FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
00878 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
00879 
00880 extern Point _cursorpos_drag_start;
00881 
00882 extern int _scrollbar_start_pos;
00883 extern int _scrollbar_size;
00884 extern byte _scroller_click_timeout;
00885 
00886 extern bool _scrolling_scrollbar;
00887 extern bool _scrolling_viewport;
00888 
00889 extern byte _special_mouse_mode;
00890 enum SpecialMouseMode {
00891   WSM_NONE     = 0,
00892   WSM_DRAGDROP = 1,
00893   WSM_SIZING   = 2,
00894   WSM_PRESIZE  = 3,
00895 };
00896 
00897 Window *GetCallbackWnd();
00898 
00899 void SetFocusedWindow(Window *w);
00900 bool EditBoxInGlobalFocus();
00901 
00902 void ScrollbarClickHandler(Window *w, const NWidgetCore *nw, int x, int y);
00903 
00904 #endif /* WINDOW_GUI_H */

Generated on Sun Nov 14 14:42:00 2010 for OpenTTD by  doxygen 1.6.1