OpenTTD
window_gui.h
Go to the documentation of this file.
1 /* $Id: window_gui.h 27147 2015-02-13 21:25:48Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef WINDOW_GUI_H
13 #define WINDOW_GUI_H
14 
15 #include "vehicle_type.h"
16 #include "viewport_type.h"
17 #include "company_type.h"
18 #include "tile_type.h"
19 #include "widget_type.h"
20 #include "core/smallvec_type.hpp"
21 #include "core/smallmap_type.hpp"
22 #include "string_type.h"
23 
27 enum FrameFlags {
28  FR_NONE = 0,
29  FR_TRANSPARENT = 1 << 0,
30  FR_BORDERONLY = 1 << 4,
31  FR_LOWERED = 1 << 5,
32  FR_DARKENED = 1 << 6,
33 };
34 
36 
37 
39  /* WWT_IMGBTN(_2) */
44 
45  /* WWT_INSET */
49 
54 
55  /* Size of the pure frame bevel without any padding. */
60 
61  /* FrameRect widgets, all text buttons, panel, editbox */
66 
67  /* Extra space at top/bottom of text panels */
70 
71  /* WWT_FRAME */
76 
77  /* WWT_MATRIX */
82 
83  /* WWT_SHADEBOX */
89 
90  /* WWT_STICKYBOX */
96 
97  /* WWT_DEBUGBOX */
103 
104  /* WWT_DEFSIZEBOX */
110 
111  /* WWT_RESIZEBOX */
117 
118  /* WWT_CLOSEBOX */
124 
125  /* WWT_CAPTION */
131 
132  /* Dropdown widget. */
138 
141 };
142 
143 /* widget.cpp */
144 void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
145 void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
146 
147 /* window.cpp */
148 extern Window *_z_front_window;
149 extern Window *_z_back_window;
150 extern Window *_focused_window;
151 
152 
159 };
160 
161 Point GetToolbarAlignedWindowPosition(int window_width);
162 
163 struct HotkeyList;
164 
169 
170  WindowDesc(WindowPosition default_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
171  WindowClass window_class, WindowClass parent_class, uint32 flags,
172  const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys = NULL);
173 
174  ~WindowDesc();
175 
179  const char *ini_key;
180  uint32 flags;
182  int16 nwid_length;
184 
185  bool pref_sticky;
186  int16 pref_width;
187  int16 pref_height;
188 
189  int16 GetDefaultWidth() const;
190  int16 GetDefaultHeight() const;
191 
192  static void LoadFromConfig();
193  static void SaveToConfig();
194 
195 private:
198 
203  WindowDesc(const WindowDesc &other);
204 };
205 
210  WDF_CONSTRUCTION = 1 << 0,
211  WDF_MODAL = 1 << 1,
212  WDF_NO_FOCUS = 1 << 2,
213 };
214 
218 struct ResizeInfo {
219  uint step_width;
220  uint step_height;
221 };
222 
228 };
229 
234  WF_TIMEOUT = 1 << 0,
235 
236  WF_DRAGGING = 1 << 3,
237  WF_SIZING_RIGHT = 1 << 4,
238  WF_SIZING_LEFT = 1 << 5,
240  WF_STICKY = 1 << 6,
242  WF_WHITE_BORDER = 1 << 8,
243  WF_HIGHLIGHTED = 1 << 9,
244  WF_CENTERED = 1 << 10,
245 };
247 
248 static const int TIMEOUT_DURATION = 7;
249 static const int WHITE_BORDER_DURATION = 3;
250 
260  int32 scrollpos_x;
261  int32 scrollpos_y;
264 };
265 
266 struct QueryString;
267 
272 protected:
274  void InitializePositionSize(int x, int y, int min_width, int min_height);
275  virtual void FindWindowPlacementAndResize(int def_width, int def_height);
276 
278 
279 public:
280  Window(WindowDesc *desc);
281 
282  virtual ~Window();
283 
290  inline void *operator new[](size_t size)
291  {
292  NOT_REACHED();
293  }
294 
300  inline void operator delete(void *ptr)
301  {
302  }
303 
308 
311 
312  int left;
313  int top;
314  int width;
315  int height;
316 
318 
320 
329 
331 
335 
336  template <class NWID>
337  inline const NWID *GetWidget(uint widnum) const;
338  template <class NWID>
339  inline NWID *GetWidget(uint widnum);
340 
341  const Scrollbar *GetScrollbar(uint widnum) const;
342  Scrollbar *GetScrollbar(uint widnum);
343 
344  const QueryString *GetQueryString(uint widnum) const;
345  QueryString *GetQueryString(uint widnum);
346 
347  virtual const char *GetFocusedText() const;
348  virtual const char *GetCaret() const;
349  virtual const char *GetMarkedText(size_t *length) const;
350  virtual Point GetCaretPosition() const;
351  virtual Rect GetTextBoundingRect(const char *from, const char *to) const;
352  virtual const char *GetTextCharacterAtPosition(const Point &pt) const;
353 
354  void InitNested(WindowNumber number = 0);
355  void CreateNestedTree(bool fill_nested = true);
356  void FinishInitNested(WindowNumber window_number = 0);
357 
361  inline void SetTimeout()
362  {
363  this->flags |= WF_TIMEOUT;
365  }
366 
370  inline void SetWhiteBorder()
371  {
372  this->flags |= WF_WHITE_BORDER;
374  }
375 
377  void SetWidgetHighlight(byte widget_index, TextColour highlighted_colour);
378  bool IsWidgetHighlighted(byte widget_index) const;
379 
387  inline void SetWidgetDisabledState(byte widget_index, bool disab_stat)
388  {
389  assert(widget_index < this->nested_array_size);
390  if (this->nested_array[widget_index] != NULL) this->GetWidget<NWidgetCore>(widget_index)->SetDisabled(disab_stat);
391  }
392 
397  inline void DisableWidget(byte widget_index)
398  {
399  SetWidgetDisabledState(widget_index, true);
400  }
401 
406  inline void EnableWidget(byte widget_index)
407  {
408  SetWidgetDisabledState(widget_index, false);
409  }
410 
416  inline bool IsWidgetDisabled(byte widget_index) const
417  {
418  assert(widget_index < this->nested_array_size);
419  return this->GetWidget<NWidgetCore>(widget_index)->IsDisabled();
420  }
421 
427  inline bool IsWidgetFocused(byte widget_index) const
428  {
429  return this->nested_focus != NULL && this->nested_focus->index == widget_index;
430  }
431 
438  inline bool IsWidgetGloballyFocused(byte widget_index) const
439  {
440  return _focused_window == this && IsWidgetFocused(widget_index);
441  }
442 
448  inline void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
449  {
450  assert(widget_index < this->nested_array_size);
451  this->GetWidget<NWidgetCore>(widget_index)->SetLowered(lowered_stat);
452  }
453 
458  inline void ToggleWidgetLoweredState(byte widget_index)
459  {
460  assert(widget_index < this->nested_array_size);
461  bool lowered_state = this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
462  this->GetWidget<NWidgetCore>(widget_index)->SetLowered(!lowered_state);
463  }
464 
469  inline void LowerWidget(byte widget_index)
470  {
471  SetWidgetLoweredState(widget_index, true);
472  }
473 
478  inline void RaiseWidget(byte widget_index)
479  {
480  SetWidgetLoweredState(widget_index, false);
481  }
482 
488  inline bool IsWidgetLowered(byte widget_index) const
489  {
490  assert(widget_index < this->nested_array_size);
491  return this->GetWidget<NWidgetCore>(widget_index)->IsLowered();
492  }
493 
494  void UnfocusFocusedWidget();
495  bool SetFocusedWidget(int widget_index);
496 
497  EventState HandleEditBoxKey(int wid, WChar key, uint16 keycode);
498  virtual void InsertTextString(int wid, const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end);
499 
500  void HandleButtonClick(byte widget);
501  int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
502 
503  void RaiseButtons(bool autoraise = false);
504  void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);
505  void CDECL SetWidgetsLoweredState(bool lowered_stat, int widgets, ...);
506  void SetWidgetDirty(byte widget_index) const;
507 
508  void DrawWidgets() const;
509  void DrawViewport() const;
510  void DrawSortButtonState(int widget, SortButtonState state) const;
511  static int SortButtonWidth();
512 
513  void DeleteChildWindows(WindowClass wc = WC_INVALID) const;
514 
515  void SetDirty() const;
516  void ReInit(int rx = 0, int ry = 0);
517 
519  inline bool IsShaded() const
520  {
521  return this->shade_select != NULL && this->shade_select->shown_plane == SZSP_HORIZONTAL;
522  }
523 
524  void SetShaded(bool make_shaded);
525 
526  void InvalidateData(int data = 0, bool gui_scope = true);
529 
530  /*** Event handling ***/
531 
536  virtual void OnInit() { }
537 
538  virtual void ApplyDefaults();
539 
547  virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number);
548 
553  virtual void OnPaint()
554  {
555  this->DrawWidgets();
556  }
557 
564  virtual void DrawWidget(const Rect &r, int widget) const {}
565 
578  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) {}
579 
586  virtual void SetStringParameters(int widget) const {}
587 
591  virtual void OnFocus() {}
592 
593  virtual void OnFocusLost();
594 
602  virtual EventState OnKeyPress(WChar key, uint16 keycode) { return ES_NOT_HANDLED; }
603 
604  virtual EventState OnHotkey(int hotkey);
605 
612 
613 
620  virtual void OnClick(Point pt, int widget, int click_count) {}
621 
629  virtual bool OnRightClick(Point pt, int widget) { return false; }
630 
636  virtual void OnHover(Point pt, int widget) {}
637 
643  virtual void OnMouseDrag(Point pt, int widget) {}
644 
650  virtual void OnDragDrop(Point pt, int widget) {}
651 
656  virtual void OnScroll(Point delta) {}
657 
664  virtual void OnMouseOver(Point pt, int widget) {}
665 
670  virtual void OnMouseWheel(int wheel) {}
671 
672 
676  virtual void OnMouseLoop() {}
677 
681  virtual void OnTick() {}
682 
686  virtual void OnHundredthTick() {}
687 
691  virtual void OnTimeout() {}
692 
693 
698  virtual void OnResize() {}
699 
705  virtual void OnDropdownSelect(int widget, int index) {}
706 
707  virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close);
708 
713  virtual void OnEditboxChanged(int widget) {}
714 
721  virtual void OnQueryTextFinished(char *str) {}
722 
728  virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {}
729 
736  virtual void OnPlaceObject(Point pt, TileIndex tile) {}
737 
743  virtual bool OnVehicleSelect(const struct Vehicle *v) { return false; }
744 
748  virtual void OnPlaceObjectAbort() {}
749 
750 
758  virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) {}
759 
769  virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) {}
770 
778  virtual void OnPlacePresize(Point pt, TileIndex tile) {}
779 
780  /*** End of the event handling ***/
781 
786  virtual bool IsNewGRFInspectable() const { return false; }
787 
794  virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
795 };
796 
803 template <class NWID>
804 inline NWID *Window::GetWidget(uint widnum)
805 {
806  if (widnum >= this->nested_array_size || this->nested_array[widnum] == NULL) return NULL;
807  NWID *nwid = dynamic_cast<NWID *>(this->nested_array[widnum]);
808  assert(nwid != NULL);
809  return nwid;
810 }
811 
813 template <>
814 inline const NWidgetBase *Window::GetWidget<NWidgetBase>(uint widnum) const
815 {
816  if (widnum >= this->nested_array_size) return NULL;
817  return this->nested_array[widnum];
818 }
819 
826 template <class NWID>
827 inline const NWID *Window::GetWidget(uint widnum) const
828 {
829  return const_cast<Window *>(this)->GetWidget<NWID>(widnum);
830 }
831 
832 
836 class PickerWindowBase : public Window {
837 
838 public:
840  {
841  this->parent = parent;
842  }
843 
844  virtual ~PickerWindowBase();
845 };
846 
848 Window *FindWindowFromPt(int x, int y);
849 
858 template <typename Wcls>
859 Wcls *AllocateWindowDescFront(WindowDesc *desc, int window_number, bool return_existing = false)
860 {
861  Wcls *w = static_cast<Wcls *>(BringWindowToFrontById(desc->cls, window_number));
862  if (w != NULL) return return_existing ? w : NULL;
863  return new Wcls(desc, window_number);
864 }
865 
866 void RelocateAllWindows(int neww, int newh);
867 
868 /* misc_gui.cpp */
869 enum TooltipCloseCondition {
870  TCC_RIGHT_CLICK,
871  TCC_LEFT_CLICK,
872  TCC_HOVER,
873 };
874 
875 void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const uint64 params[] = NULL, TooltipCloseCondition close_tooltip = TCC_HOVER);
876 
877 /* widget.cpp */
878 int GetWidgetFromPos(const Window *w, int x, int y);
879 
881 #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID)
882 #define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != NULL; w = w->z_back) if (w->window_class != WC_INVALID)
883 #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
884 #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
885 
886 extern Point _cursorpos_drag_start;
887 
888 extern int _scrollbar_start_pos;
889 extern int _scrollbar_size;
890 extern byte _scroller_click_timeout;
891 
892 extern bool _scrolling_viewport;
893 extern bool _mouse_hovering;
894 
901 };
903 
904 void SetFocusedWindow(Window *w);
905 
906 void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y);
907 
908 #endif /* WINDOW_GUI_H */