00001
00002
00003
00004
00005
00006
00007
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
00038 WD_IMGBTN_LEFT = 1,
00039 WD_IMGBTN_RIGHT = 2,
00040 WD_IMGBTN_TOP = 1,
00041 WD_IMGBTN_BOTTOM = 2,
00042
00043
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
00053 WD_FRAMERECT_LEFT = 2,
00054 WD_FRAMERECT_RIGHT = 2,
00055 WD_FRAMERECT_TOP = 1,
00056 WD_FRAMERECT_BOTTOM = 1,
00057
00058
00059 WD_TEXTPANEL_TOP = 6,
00060 WD_TEXTPANEL_BOTTOM = 6,
00061
00062
00063 WD_FRAMETEXT_LEFT = 6,
00064 WD_FRAMETEXT_RIGHT = 6,
00065 WD_FRAMETEXT_TOP = 6,
00066 WD_FRAMETEXT_BOTTOM = 6,
00067
00068
00069 WD_MATRIX_LEFT = 2,
00070 WD_MATRIX_RIGHT = 2,
00071 WD_MATRIX_TOP = 3,
00072 WD_MATRIX_BOTTOM = 1,
00073
00074
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
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
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
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
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
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
00123 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
00124
00125
00126 extern Window *_z_front_window;
00127 extern Window *_z_back_window;
00128 extern Window *_focused_window;
00129
00130
00132 enum WindowPosition {
00133 WDP_MANUAL,
00134 WDP_AUTO,
00135 WDP_CENTER,
00136 WDP_ALIGN_TOOLBAR,
00137 };
00138
00139 Point GetToolbarAlignedWindowPosition(int window_width);
00140
00144 struct WindowDesc : ZeroedMemoryAllocator {
00145
00146 WindowDesc(WindowPosition default_pos, int16 def_width, int16 def_height,
00147 WindowClass window_class, WindowClass parent_class, uint32 flags,
00148 const NWidgetPart *nwid_parts, int16 nwid_length);
00149
00150 ~WindowDesc();
00151
00152 WindowPosition default_pos;
00153 int16 default_width;
00154 int16 default_height;
00155 WindowClass cls;
00156 WindowClass parent_cls;
00157 uint32 flags;
00158 const NWidgetPart *nwid_parts;
00159 int16 nwid_length;
00160 };
00161
00165 enum WindowDefaultFlag {
00166 WDF_CONSTRUCTION = 1 << 0,
00167 WDF_UNCLICK_BUTTONS = 1 << 1,
00168 WDF_MODAL = 1 << 2,
00169 WDF_NO_FOCUS = 1 << 3,
00170 };
00171
00175 class Scrollbar {
00176 private:
00177 const bool is_vertical;
00178 uint16 count;
00179 uint16 cap;
00180 uint16 pos;
00181
00182 public:
00183 Scrollbar(bool is_vertical) : is_vertical(is_vertical)
00184 {
00185 }
00186
00191 FORCEINLINE uint16 GetCount() const
00192 {
00193 return this->count;
00194 }
00195
00200 FORCEINLINE uint16 GetCapacity() const
00201 {
00202 return this->cap;
00203 }
00204
00209 FORCEINLINE uint16 GetPosition() const
00210 {
00211 return this->pos;
00212 }
00213
00219 FORCEINLINE bool IsVisible(uint16 item) const
00220 {
00221 return IsInsideBS(item, this->GetPosition(), this->GetCapacity());
00222 }
00223
00229 void SetCount(int num)
00230 {
00231 assert(num >= 0);
00232 assert(num <= MAX_UVALUE(uint16));
00233
00234 this->count = num;
00235 num -= this->cap;
00236 if (num < 0) num = 0;
00237 if (num < this->pos) this->pos = num;
00238 }
00239
00245 void SetCapacity(int capacity)
00246 {
00247 assert(capacity > 0);
00248 assert(capacity <= MAX_UVALUE(uint16));
00249
00250 this->cap = capacity;
00251 if (this->cap + this->pos > this->count) this->pos = max(0, this->count - this->cap);
00252 }
00253
00254 void SetCapacityFromWidget(Window *w, int widget, int padding = 0);
00255
00260 void SetPosition(int position)
00261 {
00262 assert(position >= 0);
00263 assert(this->count <= this->cap ? (position == 0) : (position + this->cap <= this->count));
00264 this->pos = position;
00265 }
00266
00272 void UpdatePosition(int difference)
00273 {
00274 if (difference == 0) return;
00275 this->SetPosition(Clamp(this->pos + difference, 0, max(this->count - this->cap, 0)));
00276 }
00277
00284 void ScrollTowards(int position)
00285 {
00286 if (position < this->GetPosition()) {
00287
00288 this->SetPosition(position);
00289 } else if (position >= this->GetPosition() + this->GetCapacity()) {
00290
00291 this->SetPosition(position - this->GetCapacity() + 1);
00292 }
00293 }
00294 };
00295
00299 struct ResizeInfo {
00300 uint step_width;
00301 uint step_height;
00302 };
00303
00305 enum SortButtonState {
00306 SBS_OFF,
00307 SBS_DOWN,
00308 SBS_UP,
00309 };
00310
00318 struct ViewportData : ViewPort {
00319 VehicleID follow_vehicle;
00320 int32 scrollpos_x;
00321 int32 scrollpos_y;
00322 int32 dest_scrollpos_x;
00323 int32 dest_scrollpos_y;
00324 };
00325
00329 struct Window : ZeroedMemoryAllocator {
00331 enum EventState {
00332 ES_HANDLED,
00333 ES_NOT_HANDLED,
00334 };
00335
00336 protected:
00337 void InitializeData(WindowClass cls, int window_number, uint32 desc_flags);
00338 void InitializePositionSize(int x, int y, int min_width, int min_height);
00339 void FindWindowPlacementAndResize(int def_width, int def_height);
00340
00341 public:
00342 Window();
00343
00344 virtual ~Window();
00345
00352 FORCEINLINE void *operator new[](size_t size)
00353 {
00354 NOT_REACHED();
00355 }
00356
00362 FORCEINLINE void operator delete(void *ptr)
00363 {
00364 }
00365
00366 uint16 flags4;
00367 WindowClass window_class;
00368 WindowNumber window_number;
00369
00370 int left;
00371 int top;
00372 int width;
00373 int height;
00374
00375 Scrollbar hscroll;
00376 Scrollbar vscroll;
00377 Scrollbar vscroll2;
00378 ResizeInfo resize;
00379
00380 Owner owner;
00381
00382 ViewportData *viewport;
00383 uint32 desc_flags;
00384 const NWidgetCore *nested_focus;
00385 NWidgetBase *nested_root;
00386 NWidgetBase **nested_array;
00387 uint nested_array_size;
00388 NWidgetStacked *shade_select;
00389 Dimension unshaded_size;
00390
00391 Window *parent;
00392 Window *z_front;
00393 Window *z_back;
00394
00395 template <class NWID>
00396 inline const NWID *GetWidget(uint widnum) const;
00397 template <class NWID>
00398 inline NWID *GetWidget(uint widnum);
00399
00400
00401 void InitNested(const WindowDesc *desc, WindowNumber number = 0);
00402 void CreateNestedTree(const WindowDesc *desc, bool fill_nested = true);
00403 void FinishInitNested(const WindowDesc *desc, WindowNumber window_number);
00404
00412 inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
00413 {
00414 assert(widget_index < this->nested_array_size);
00415 if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
00416 }
00417
00422 inline void DisableWidget(byte widget_index)
00423 {
00424 SetWidgetDisabledState(widget_index, true);
00425 }
00426
00431 inline void EnableWidget(byte widget_index)
00432 {
00433 SetWidgetDisabledState(widget_index, false);
00434 }
00435
00441 inline bool IsWidgetDisabled(byte widget_index) const
00442 {
00443 assert(widget_index < this->nested_array_size);
00444 return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
00445 }
00446
00452 inline bool IsWidgetFocused(byte widget_index) const
00453 {
00454 return this->nested_focus != NULL && this->nested_focus->index == widget_index;
00455 }
00456
00463 inline bool IsWidgetGloballyFocused(byte widget_index) const
00464 {
00465 return _focused_window == this && IsWidgetFocused(widget_index);
00466 }
00467
00473 inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
00474 {
00475 assert(widget_index < this->nested_array_size);
00476 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
00477 }
00478
00483 inline void ToggleWidgetLoweredState(byte widget_index)
00484 {
00485 assert(widget_index < this->nested_array_size);
00486 bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00487 this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
00488 }
00489
00494 inline void LowerWidget(byte widget_index)
00495 {
00496 SetWidgetLoweredState(widget_index, true);
00497 }
00498
00503 inline void RaiseWidget(byte widget_index)
00504 {
00505 SetWidgetLoweredState(widget_index, false);
00506 }
00507
00513 inline bool IsWidgetLowered(byte widget_index) const
00514 {
00515 assert(widget_index < this->nested_array_size);
00516 return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
00517 }
00518
00519 void UnfocusFocusedWidget();
00520 bool SetFocusedWidget(byte widget_index);
00521
00522 void HandleButtonClick(byte widget);
00523
00524 void RaiseButtons(bool autoraise = false);
00525 void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
00526 void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
00527 void SetWidgetDirty(byte widget_index) const;
00528
00529 void DrawWidgets() const;
00530 void DrawViewport() const;
00531 void DrawSortButtonState(int widget, SortButtonState state) const;
00532
00533 void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
00534
00535 void SetDirty() const;
00536 void ReInit(int rx = 0, int ry = 0);
00537
00539 inline bool IsShaded() const
00540 {
00541 return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
00542 }
00543
00544 void SetShaded(bool make_shaded);
00545
00550 void InvalidateData(int data = 0)
00551 {
00552 this->SetDirty();
00553 this->OnInvalidateData(data);
00554 }
00555
00556
00557
00562 virtual void OnInit() { }
00563
00572 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number);
00573
00578 virtual void OnPaint() {}
00579
00586 virtual void DrawWidget(const Rect &r, int widget) const {}
00587
00600 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
00601
00608 virtual void SetStringParameters(int widget) const {}
00609
00613 virtual void OnFocus() {}
00614
00618 virtual void OnFocusLost() {}
00619
00627 virtual EventState OnKeyPress(uint16 key, uint16 keycode) { return ES_NOT_HANDLED; }
00628
00634 virtual EventState OnCTRLStateChange() { return ES_NOT_HANDLED; }
00635
00636
00643 virtual void OnClick(Point pt, int widget, int click_count) {}
00644
00650 virtual void OnRightClick(Point pt, int widget) {}
00651
00657 virtual void OnDragDrop(Point pt, int widget) {}
00658
00663 virtual void OnScroll(Point delta) {}
00664
00671 virtual void OnMouseOver(Point pt, int widget) {}
00672
00677 virtual void OnMouseWheel(int wheel) {}
00678
00679
00683 virtual void OnMouseLoop() {}
00684
00688 virtual void OnTick() {}
00689
00693 virtual void OnHundredthTick() {}
00694
00698 virtual void OnTimeout() {}
00699
00700
00705 virtual void OnResize() {}
00706
00712 virtual void OnDropdownSelect(int widget, int index) {}
00713
00720 virtual void OnQueryTextFinished(char *str) {}
00721
00726 virtual void OnInvalidateData(int data = 0) {}
00727
00728
00735 virtual void OnPlaceObject(Point pt, TileIndex tile) {}
00736
00740 virtual void OnPlaceObjectAbort() {}
00741
00742
00750 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
00751
00761 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
00762
00770 virtual void OnPlacePresize(Point pt, TileIndex tile) {}
00771
00772
00773 };
00774
00780 template <class NWID>
00781 inline NWID *Window::GetWidget(uint widnum)
00782 {
00783 if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
00784 NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
00785 assert(nwid != NULL);
00786 return nwid;
00787 }
00788
00790 template <>
00791 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
00792 {
00793 if (widnum >= this->nested_array_size) return NULL;
00794 return this->nested_array[widnum];
00795 }
00796
00802 template <class NWID>
00803 inline const NWID *Window::GetWidget(uint widnum) const
00804 {
00805 return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
00806 }
00807
00808
00812 class PickerWindowBase : public Window {
00813
00814 public:
00815 PickerWindowBase(Window *parent) : Window()
00816 {
00817 this->parent = parent;
00818 };
00819
00820 virtual ~PickerWindowBase();
00821 };
00822
00826 enum WindowFlags {
00827 WF_TIMEOUT_TRIGGER = 1,
00828 WF_TIMEOUT_BEGIN = 7,
00829 WF_TIMEOUT_MASK = 7,
00830 WF_DRAGGING = 1 << 3,
00831 WF_SCROLL_UP = 1 << 4,
00832 WF_SCROLL_DOWN = 1 << 5,
00833 WF_SCROLL_MIDDLE = 1 << 6,
00834 WF_SCROLL2 = 1 << 7,
00835 WF_HSCROLL = 1 << 8,
00836 WF_SIZING_RIGHT = 1 << 9,
00837 WF_SIZING_LEFT = 1 << 10,
00838 WF_SIZING = WF_SIZING_RIGHT | WF_SIZING_LEFT,
00839 WF_STICKY = 1 << 11,
00840
00841 WF_DISABLE_VP_SCROLL = 1 << 12,
00842
00843 WF_WHITE_BORDER_ONE = 1 << 13,
00844 WF_WHITE_BORDER_MASK = 1 << 14 | WF_WHITE_BORDER_ONE,
00845 };
00846
00847 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);
00848 Window *FindWindowFromPt(int x, int y);
00849
00856 template <typename Wcls>
00857 Wcls *AllocateWindowDescFront(const WindowDesc *desc, int window_number)
00858 {
00859 if (BringWindowToFrontById(desc->cls, window_number)) return NULL;
00860 return new Wcls(desc, window_number);
00861 }
00862
00863 void RelocateAllWindows(int neww, int newh);
00864
00865
00866 void GuiShowTooltips(StringID str, uint paramcount = 0, const uint64 params[] = NULL, bool use_left_mouse_button = false);
00867
00868
00869 int GetWidgetFromPos(const Window *w, int x, int y);
00870
00872 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
00873 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
00874 #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
00875 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
00876
00877 extern Point _cursorpos_drag_start;
00878
00879 extern int _scrollbar_start_pos;
00880 extern int _scrollbar_size;
00881 extern byte _scroller_click_timeout;
00882
00883 extern bool _scrolling_scrollbar;
00884 extern bool _scrolling_viewport;
00885
00886 extern byte _special_mouse_mode;
00887 enum SpecialMouseMode {
00888 WSM_NONE = 0,
00889 WSM_DRAGDROP = 1,
00890 WSM_SIZING = 2,
00891 WSM_PRESIZE = 3,
00892 };
00893
00894 Window *GetCallbackWnd();
00895
00896 void SetFocusedWindow(Window *w);
00897 bool EditBoxInGlobalFocus();
00898
00899 void ScrollbarClickHandler(Window *w, const NWidgetCore *nw, int x, int y);
00900
00901 #endif