tron@2186: /* $Id$ */ tron@2186: rubidium@10455: /** @file window.cpp Windowing system, widgets and events */ belugas@6443: truelight@0: #include "stdafx.h" glx@4755: #include Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" rubidium@9724: #include "player_func.h" rubidium@9723: #include "gfx_func.h" rubidium@10715: #include "console_func.h" rubidium@10715: #include "console_gui.h" rubidium@9723: #include "viewport_func.h" tron@2159: #include "variables.h" truelight@4300: #include "genworld.h" rubidium@9628: #include "blitter/factory.hpp" rubidium@9723: #include "window_gui.h" rubidium@9723: #include "zoom_func.h" rubidium@9723: #include "core/alloc_func.hpp" rubidium@9723: #include "map_func.h" rubidium@9723: #include "vehicle_base.h" rubidium@9724: #include "settings_type.h" rubidium@10249: #include "cheat_func.h" rubidium@10455: #include "window_func.h" rubidium@10455: #include "tilehighlight_func.h" rubidium@9724: rubidium@9724: #include "table/sprites.h" truelight@0: rubidium@10181: static Point _drag_delta; ///< delta between mouse cursor and upper left corner of dragged window rubidium@10181: static Window *_mouseover_last_w = NULL; ///< Window of the last MOUSEOVER event rubidium@10142: rubidium@10142: /** rubidium@10142: * List of windows opened at the screen. rubidium@10142: * Uppermost window is at _z_windows[_last_z_window - 1], rubidium@10142: * bottom window is at _z_windows[0] rubidium@10142: */ rubidium@10181: Window *_z_windows[MAX_NUMBER_OF_WINDOWS]; Darkvater@5126: Window **_last_z_window; ///< always points to the next free space in the z-array Darkvater@5124: glx@10776: byte _no_scroll; rubidium@9724: Point _cursorpos_drag_start; rubidium@9724: rubidium@9724: int _scrollbar_start_pos; rubidium@9724: int _scrollbar_size; rubidium@9724: byte _scroller_click_timeout; rubidium@9724: rubidium@9724: bool _scrolling_scrollbar; rubidium@9724: bool _scrolling_viewport; rubidium@9724: rubidium@9724: byte _special_mouse_mode; rubidium@9724: rubidium@9724: rubidium@9723: void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...) glx@4755: { glx@4755: va_list wdg_list; glx@4755: glx@4755: va_start(wdg_list, widgets); glx@4755: glx@4755: while (widgets != WIDGET_LIST_END) { rubidium@9723: SetWidgetDisabledState(widgets, disab_stat); glx@4755: widgets = va_arg(wdg_list, int); glx@4755: } glx@4755: glx@4755: va_end(wdg_list); glx@4755: } glx@4755: rubidium@9723: void CDECL Window::SetWidgetsHiddenState(bool hidden_stat, int widgets, ...) glx@4755: { glx@4755: va_list wdg_list; glx@4755: glx@4755: va_start(wdg_list, widgets); glx@4755: glx@4755: while (widgets != WIDGET_LIST_END) { rubidium@9723: SetWidgetHiddenState(widgets, hidden_stat); glx@4755: widgets = va_arg(wdg_list, int); glx@4755: } glx@4755: glx@4755: va_end(wdg_list); glx@4755: } glx@4755: rubidium@9723: void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...) rubidium@9723: { rubidium@9723: va_list wdg_list; rubidium@9723: rubidium@9723: va_start(wdg_list, widgets); rubidium@9723: rubidium@9723: while (widgets != WIDGET_LIST_END) { rubidium@9723: SetWidgetLoweredState(widgets, lowered_stat); rubidium@9723: widgets = va_arg(wdg_list, int); rubidium@9723: } rubidium@9723: rubidium@9723: va_end(wdg_list); rubidium@9723: } rubidium@9723: rubidium@9723: void Window::RaiseButtons() belugas@4719: { rubidium@10455: for (uint i = 0; i < this->widget_count; i++) { rubidium@9723: if (this->IsWidgetLowered(i)) { rubidium@9723: this->RaiseWidget(i); rubidium@9723: this->InvalidateWidget(i); belugas@4719: } belugas@4719: } belugas@4719: } belugas@4719: rubidium@9723: void Window::InvalidateWidget(byte widget_index) const truelight@0: { rubidium@9723: const Widget *wi = &this->widget[widget_index]; rubidium@9723: rubidium@9723: /* Don't redraw the window if the widget is invisible or of no-type */ rubidium@9723: if (wi->type == WWT_EMPTY || IsWidgetHidden(widget_index)) return; rubidium@9723: rubidium@9723: SetDirtyBlocks(this->left + wi->left, this->top + wi->top, this->left + wi->right + 1, this->top + wi->bottom + 1); truelight@0: } truelight@0: rubidium@9723: void Window::HandleButtonClick(byte widget) rubidium@9723: { rubidium@9723: this->LowerWidget(widget); rubidium@9723: this->flags4 |= 5 << WF_TIMEOUT_SHL; rubidium@9723: this->InvalidateWidget(widget); rubidium@9723: } tron@2817: Darkvater@5124: static void StartWindowDrag(Window *w); Darkvater@5124: static void StartWindowSizing(Window *w); tron@2817: rubidium@10142: /** rubidium@10142: * Dispatch left mouse-button (possibly double) click in window. rubidium@10142: * @param w Window to dispatch event in rubidium@10142: * @param x X coordinate of the click rubidium@10142: * @param y Y coordinate of the click rubidium@10142: * @param double_click Was it a double click? rubidium@10142: */ glx@9629: static void DispatchLeftClickEvent(Window *w, int x, int y, bool double_click) tron@2596: { rubidium@10513: int widget = 0; truelight@0: if (w->desc_flags & WDF_DEF_WIDGET) { rubidium@10513: widget = GetWidgetFromPos(w, x, y); rubidium@10513: if (widget < 0) return; // exit if clicked outside of widgets truelight@158: rubidium@5236: /* don't allow any interaction if the button has been disabled */ rubidium@10513: if (w->IsWidgetDisabled(widget)) return; darkvater@222: rubidium@10513: const Widget *wi = &w->widget[widget]; truelight@0: Darkvater@4938: if (wi->type & WWB_MASK) { darkvater@211: /* special widget handling for buttons*/ tron@2952: switch (wi->type) { Darkvater@4938: case WWT_PANEL | WWB_PUSHBUTTON: /* WWT_PUSHBTN */ Darkvater@4938: case WWT_IMGBTN | WWB_PUSHBUTTON: /* WWT_PUSHIMGBTN */ Darkvater@4938: case WWT_TEXTBTN | WWB_PUSHBUTTON: /* WWT_PUSHTXTBTN */ rubidium@10513: w->HandleButtonClick(widget); Darkvater@4938: break; truelight@0: } bjarni@842: } else if (wi->type == WWT_SCROLLBAR || wi->type == WWT_SCROLL2BAR || wi->type == WWT_HSCROLLBAR) { rubidium@10513: ScrollbarClickHandler(w, wi, x, y); truelight@0: } truelight@0: truelight@0: if (w->desc_flags & WDF_STD_BTN) { rubidium@10513: if (widget == 0) { /* 'X' */ rubidium@10455: delete w; celestar@984: return; tron@1109: } tron@1109: rubidium@10513: if (widget == 1) { /* 'Title bar' */ Darkvater@5124: StartWindowDrag(w); darkvater@1112: return; darkvater@1112: } truelight@0: } truelight@867: darkvater@1112: if (w->desc_flags & WDF_RESIZABLE && wi->type == WWT_RESIZEBOX) { Darkvater@5124: StartWindowSizing(w); rubidium@10513: w->InvalidateWidget(widget); darkvater@1112: return; darkvater@1112: } truelight@867: truelight@867: if (w->desc_flags & WDF_STICKY_BUTTON && wi->type == WWT_STICKYBOX) { truelight@867: w->flags4 ^= WF_STICKY; rubidium@10513: w->InvalidateWidget(widget); darkvater@1112: return; darkvater@682: } truelight@0: } darkvater@1038: rubidium@10513: Point pt = { x, y }; rubidium@10513: rubidium@10513: if (double_click) { rubidium@10513: w->OnDoubleClick(pt, widget); rubidium@10513: } else { rubidium@10513: w->OnClick(pt, widget); rubidium@10513: } truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Dispatch right mouse-button click in window. rubidium@10142: * @param w Window to dispatch event in rubidium@10142: * @param x X coordinate of the click rubidium@10142: * @param y Y coordinate of the click rubidium@10142: */ belugas@4171: static void DispatchRightClickEvent(Window *w, int x, int y) tron@2596: { rubidium@10513: int widget = 0; truelight@0: truelight@0: /* default tooltips handler? */ truelight@0: if (w->desc_flags & WDF_STD_TOOLTIPS) { rubidium@10513: widget = GetWidgetFromPos(w, x, y); rubidium@10513: if (widget < 0) return; // exit if clicked outside of widgets truelight@0: rubidium@10513: if (w->widget[widget].tooltips != 0) { rubidium@10513: GuiShowTooltips(w->widget[widget].tooltips); truelight@0: return; truelight@0: } truelight@0: } truelight@0: rubidium@10513: Point pt = { x, y }; rubidium@10513: w->OnRightClick(pt, widget); truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Dispatch the mousewheel-action to the window. rubidium@10142: * The window will scroll any compatible scrollbars if the mouse is pointed over the bar or its contents rubidium@10142: * @param w Window Darkvater@2021: * @param widget the widget where the scrollwheel was used Darkvater@2021: * @param wheel scroll up or down Darkvater@2021: */ belugas@4171: static void DispatchMouseWheelEvent(Window *w, int widget, int wheel) truelight@0: { Darkvater@2021: if (widget < 0) return; Darkvater@2021: rubidium@10455: const Widget *wi1 = &w->widget[widget]; rubidium@10455: const Widget *wi2 = &w->widget[widget + 1]; Darkvater@2021: darkvater@982: /* The listbox can only scroll if scrolling was done on the scrollbar itself, tron@1019: * or on the listbox (and the next item is (must be) the scrollbar) darkvater@982: * XXX - should be rewritten as a widget-dependent scroller but that's darkvater@982: * not happening until someone rewrites the whole widget-code */ rubidium@10455: Scrollbar *sb; tron@1019: if ((sb = &w->vscroll, wi1->type == WWT_SCROLLBAR) || (sb = &w->vscroll2, wi1->type == WWT_SCROLL2BAR) || darkvater@982: (sb = &w->vscroll2, wi2->type == WWT_SCROLL2BAR) || (sb = &w->vscroll, wi2->type == WWT_SCROLLBAR) ) { darkvater@982: darkvater@982: if (sb->count > sb->cap) { rubidium@9722: int pos = Clamp(sb->pos + wheel, 0, sb->count - sb->cap); darkvater@982: if (pos != sb->pos) { darkvater@982: sb->pos = pos; rubidium@10455: w->SetDirty(); darkvater@982: } truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Generate repaint events for the visible part of window *wz within the rectangle. rubidium@10142: * rubidium@10142: * The function goes recursively upwards in the window stack, and splits the rectangle rubidium@10142: * into multiple pieces at the window edges, so obscured parts are not redrawn. rubidium@10142: * rubidium@10142: * @param wz Pointer into window stack, pointing at the window that needs to be repainted rubidium@10142: * @param left Left edge of the rectangle that should be repainted rubidium@10142: * @param top Top edge of the rectangle that should be repainted rubidium@10142: * @param right Right edge of the rectangle that should be repainted rubidium@10142: * @param bottom Bottom edge of the rectangle that should be repainted rubidium@10142: */ Darkvater@5124: static void DrawOverlappedWindow(Window* const *wz, int left, int top, int right, int bottom) truelight@0: { Darkvater@5137: Window* const *vz = wz; truelight@0: Darkvater@5124: while (++vz != _last_z_window) { Darkvater@5124: const Window *v = *vz; Darkvater@5124: truelight@0: if (right > v->left && tron@2026: bottom > v->top && truelight@0: left < v->left + v->width && truelight@0: top < v->top + v->height) { rubidium@10142: /* v and rectangle intersect with eeach other */ rubidium@10142: int x; rubidium@10142: rubidium@9601: if (left < (x = v->left)) { Darkvater@5124: DrawOverlappedWindow(wz, left, top, x, bottom); Darkvater@5124: DrawOverlappedWindow(wz, x, top, right, bottom); truelight@0: return; truelight@0: } truelight@0: rubidium@9601: if (right > (x = v->left + v->width)) { Darkvater@5124: DrawOverlappedWindow(wz, left, top, x, bottom); Darkvater@5124: DrawOverlappedWindow(wz, x, top, right, bottom); truelight@0: return; truelight@0: } truelight@0: rubidium@9601: if (top < (x = v->top)) { Darkvater@5124: DrawOverlappedWindow(wz, left, top, right, x); Darkvater@5124: DrawOverlappedWindow(wz, left, x, right, bottom); truelight@0: return; truelight@0: } truelight@0: rubidium@9601: if (bottom > (x = v->top + v->height)) { Darkvater@5124: DrawOverlappedWindow(wz, left, top, right, x); Darkvater@5124: DrawOverlappedWindow(wz, left, x, right, bottom); truelight@0: return; truelight@0: } truelight@0: truelight@0: return; truelight@0: } truelight@0: } truelight@0: glx@10294: /* Setup blitter, and dispatch a repaint event to window *wz */ rubidium@10181: DrawPixelInfo *dp = _cur_dpi; rubidium@10181: dp->width = right - left; rubidium@10181: dp->height = bottom - top; rubidium@10181: dp->left = left - (*wz)->left; rubidium@10181: dp->top = top - (*wz)->top; rubidium@10181: dp->pitch = _screen.pitch; rubidium@10181: dp->dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top); rubidium@10181: dp->zoom = ZOOM_LVL_NORMAL; rubidium@10513: (*wz)->OnPaint(); rubidium@10181: } rubidium@10181: rubidium@10181: /** rubidium@10181: * From a rectangle that needs redrawing, find the windows that intersect with the rectangle. rubidium@10181: * These windows should be re-painted. rubidium@10181: * @param left Left edge of the rectangle that should be repainted rubidium@10181: * @param top Top edge of the rectangle that should be repainted rubidium@10181: * @param right Right edge of the rectangle that should be repainted rubidium@10181: * @param bottom Bottom edge of the rectangle that should be repainted rubidium@10181: */ rubidium@10181: void DrawOverlappedWindowForAll(int left, int top, int right, int bottom) rubidium@10181: { rubidium@10181: Window* const *wz; rubidium@10181: DrawPixelInfo bk; rubidium@10181: _cur_dpi = &bk; rubidium@10181: rubidium@10181: FOR_ALL_WINDOWS(wz) { rubidium@10181: const Window *w = *wz; rubidium@10181: if (right > w->left && rubidium@10181: bottom > w->top && rubidium@10181: left < w->left + w->width && rubidium@10181: top < w->top + w->height) { glx@10294: /* Window w intersects with the rectangle => needs repaint */ rubidium@10181: DrawOverlappedWindow(wz, left, top, right, bottom); rubidium@10181: } truelight@0: } truelight@0: } truelight@0: rubidium@10142: /** rubidium@10455: * Mark entire window as dirty (in need of re-paint) rubidium@10455: * @param w Window to redraw rubidium@10455: * @ingroup dirty rubidium@10455: */ rubidium@10455: void Window::SetDirty() const rubidium@10455: { rubidium@10455: SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height); truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Mark entire window as dirty (in need of re-paint) rubidium@10142: * @param w Window to redraw rubidium@10142: * @ingroup dirty rubidium@10142: */ belugas@4171: void SetWindowDirty(const Window *w) truelight@0: { rubidium@10455: if (w != NULL) w->SetDirty(); truelight@0: } truelight@0: Darkvater@5666: /** Find the Window whose parent pointer points to this window rubidium@9599: * @param w parent Window to find child of rubidium@9599: * @return a Window pointer that is the child of w, or NULL otherwise */ Darkvater@5666: static Window *FindChildWindow(const Window *w) Darkvater@5666: { Darkvater@5666: Window* const *wz; Darkvater@5666: Darkvater@5666: FOR_ALL_WINDOWS(wz) { Darkvater@5666: Window *v = *wz; Darkvater@5666: if (v->parent == w) return v; Darkvater@5666: } Darkvater@5666: Darkvater@5666: return NULL; Darkvater@5666: } Darkvater@5666: Darkvater@5124: /** Find the z-value of a window. A window must already be open rubidium@9599: * or the behaviour is undefined but function should never fail rubidium@9599: * @param w window to query Z Position rubidium@10142: * @return Pointer into the window-list at the position of \a w rubidium@10142: */ Darkvater@5124: Window **FindWindowZPosition(const Window *w) Darkvater@5124: { Darkvater@5124: Window **wz; Darkvater@5124: rubidium@10142: FOR_ALL_WINDOWS(wz) { Darkvater@5124: if (*wz == w) return wz; Darkvater@5124: } Darkvater@5680: rubidium@5838: DEBUG(misc, 3, "Window (cls %d, number %d) is not open, probably removed by recursive calls", Darkvater@5680: w->window_class, w->window_number); Darkvater@5680: return NULL; Darkvater@5124: } Darkvater@5124: rubidium@10142: /** glx@10294: * Remove window and all its child windows from the window stack. rubidium@10142: */ rubidium@10455: Window::~Window() truelight@0: { tron@4077: if (_thd.place_mode != VHM_NONE && rubidium@10455: _thd.window_class == this->window_class && rubidium@10455: _thd.window_number == this->window_number) { truelight@0: ResetObjectToPlace(); truelight@0: } truelight@0: rubidium@10181: /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */ rubidium@10455: if (_mouseover_last_w == this) _mouseover_last_w = NULL; rubidium@10181: Darkvater@5124: /* Find the window in the z-array, and effectively remove it rubidium@10455: * by moving all windows after it one to the left. This must be rubidium@10455: * done before removing the child so we cannot cause recursion rubidium@10455: * between the deletion of the parent and the child. */ rubidium@10455: Window **wz = FindWindowZPosition(this); Darkvater@5680: if (wz == NULL) return; Darkvater@5124: memmove(wz, wz + 1, (byte*)_last_z_window - (byte*)wz); Darkvater@5124: _last_z_window--; rubidium@10181: glx@10645: /* Delete all children a window might have in a head-recursive manner */ glx@10645: Window *child = FindChildWindow(this); glx@10645: while (child != NULL) { glx@10645: delete child; glx@10645: child = FindChildWindow(this); glx@10645: } rubidium@10455: rubidium@10455: if (this->viewport != NULL) DeleteWindowViewport(this); rubidium@10455: rubidium@10455: this->SetDirty(); rubidium@10455: free(this->widget); truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Find a window by its class and window number rubidium@10142: * @param cls Window class rubidium@10142: * @param number Number of the window within the window class rubidium@10142: * @return Pointer to the found window, or \c NULL if not available rubidium@10142: */ truelight@0: Window *FindWindowById(WindowClass cls, WindowNumber number) truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; tron@2639: if (w->window_class == cls && w->window_number == number) return w; truelight@158: } truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Delete a window by its class and window number (if it is open). rubidium@10142: * @param cls Window class rubidium@10142: * @param number Number of the window within the window class rubidium@10142: */ truelight@0: void DeleteWindowById(WindowClass cls, WindowNumber number) truelight@0: { rubidium@10455: delete FindWindowById(cls, number); truelight@158: } truelight@0: rubidium@10142: /** rubidium@10142: * Delete all windows of a given class rubidium@10142: * @param cls Window class of windows to delete rubidium@10142: */ darkvater@999: void DeleteWindowByClass(WindowClass cls) darkvater@999: { Darkvater@5124: Window* const *wz; tron@2639: Darkvater@5121: restart_search: Darkvater@5121: /* When we find the window to delete, we need to restart the search Darkvater@5124: * as deleting this window could cascade in deleting (many) others Darkvater@5124: * anywhere in the z-array */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; darkvater@999: if (w->window_class == cls) { rubidium@10455: delete w; Darkvater@5121: goto restart_search; tron@2639: } darkvater@999: } darkvater@999: } darkvater@999: Darkvater@5120: /** Delete all windows of a player. We identify windows of a player Darkvater@5120: * by looking at the caption colour. If it is equal to the player ID Darkvater@5120: * then we say the window belongs to the player and should be deleted Darkvater@5120: * @param id PlayerID player identifier */ Darkvater@5120: void DeletePlayerWindows(PlayerID id) bjarni@5077: { Darkvater@5124: Window* const *wz; bjarni@5077: Darkvater@5121: restart_search: Darkvater@5121: /* When we find the window to delete, we need to restart the search Darkvater@5124: * as deleting this window could cascade in deleting (many) others Darkvater@5124: * anywhere in the z-array */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; Darkvater@5120: if (w->caption_color == id) { rubidium@10455: delete w; Darkvater@5121: goto restart_search; bjarni@5077: } bjarni@5077: } bjarni@5077: Darkvater@5120: /* Also delete the player specific windows, that don't have a player-colour */ Darkvater@5120: DeleteWindowById(WC_BUY_COMPANY, id); bjarni@5077: } bjarni@5077: Darkvater@5120: /** Change the owner of all the windows one player can take over from another Darkvater@5120: * player in the case of a company merger. Do not change ownership of windows Darkvater@5120: * that need to be deleted once takeover is complete Darkvater@5120: * @param old_player PlayerID of original owner of the window Darkvater@5120: * @param new_player PlayerID of the new owner of the window */ bjarni@5077: void ChangeWindowOwner(PlayerID old_player, PlayerID new_player) bjarni@5077: { Darkvater@5124: Window* const *wz; bjarni@5077: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; Darkvater@5124: rubidium@10455: if (w->caption_color != old_player) continue; Darkvater@5120: rubidium@10455: switch (w->window_class) { rubidium@10455: case WC_PLAYER_COLOR: rubidium@10455: case WC_FINANCES: rubidium@10455: case WC_STATION_LIST: rubidium@10455: case WC_TRAINS_LIST: rubidium@10455: case WC_ROADVEH_LIST: rubidium@10455: case WC_SHIPS_LIST: rubidium@10455: case WC_AIRCRAFT_LIST: rubidium@10455: case WC_BUY_COMPANY: rubidium@10455: case WC_COMPANY: rubidium@10455: continue; rubidium@10455: rubidium@10455: default: rubidium@10455: w->caption_color = new_player; rubidium@10455: break; rubidium@10455: } bjarni@5077: } bjarni@5077: } bjarni@5077: Darkvater@5124: static void BringWindowToFront(const Window *w); tron@2817: Darkvater@5124: /** Find a window and make it the top-window on the screen. The window rubidium@9599: * gets a white border for a brief period of time to visualize its "activation" rubidium@9599: * @param cls WindowClass of the window to activate rubidium@9599: * @param number WindowNumber of the window to activate Darkvater@5124: * @return a pointer to the window thus activated */ truelight@0: Window *BringWindowToFrontById(WindowClass cls, WindowNumber number) truelight@0: { truelight@0: Window *w = FindWindowById(cls, number); truelight@0: truelight@0: if (w != NULL) { truelight@0: w->flags4 |= WF_WHITE_BORDER_MASK; Darkvater@5124: BringWindowToFront(w); rubidium@10455: w->SetDirty(); truelight@0: } truelight@0: truelight@0: return w; truelight@0: } truelight@0: darkvater@1648: static inline bool IsVitalWindow(const Window *w) darkvater@1648: { rubidium@10455: switch (w->window_class) { rubidium@10455: case WC_MAIN_TOOLBAR: rubidium@10455: case WC_STATUS_BAR: rubidium@10455: case WC_NEWS_WINDOW: rubidium@10455: case WC_SEND_NETWORK_MSG: rubidium@10455: return true; rubidium@10455: rubidium@10455: default: rubidium@10455: return false; rubidium@10455: } darkvater@1648: } darkvater@1648: darkvater@1648: /** On clicking on a window, make it the frontmost window of all. However darkvater@1648: * there are certain windows that always need to be on-top; these include darkvater@1648: * - Toolbar, Statusbar (always on) darkvater@1648: * - New window, Chatbar (only if open) Darkvater@5120: * The window is marked dirty for a repaint if the window is actually moved darkvater@1648: * @param w window that is put into the foreground Darkvater@5124: * @return pointer to the window, the same as the input pointer darkvater@1648: */ Darkvater@5124: static void BringWindowToFront(const Window *w) truelight@0: { Darkvater@5124: Window **wz = FindWindowZPosition(w); Darkvater@5124: Window **vz = _last_z_window; truelight@0: Darkvater@5124: /* Bring the window just below the vital windows */ truelight@0: do { Darkvater@5124: if (--vz < _z_windows) return; Darkvater@5124: } while (IsVitalWindow(*vz)); truelight@0: Darkvater@5124: if (wz == vz) return; // window is already in the right position Darkvater@5124: assert(wz < vz); truelight@0: rubidium@10455: Window *tempz = *wz; Darkvater@5124: memmove(wz, wz + 1, (byte*)vz - (byte*)wz); Darkvater@5124: *vz = tempz; truelight@0: rubidium@10455: w->SetDirty(); truelight@0: } truelight@0: darkvater@1648: /** We have run out of windows, so find a suitable candidate for replacement. darkvater@1648: * Keep all important windows intact. These are darkvater@1648: * - Main window (gamefield), Toolbar, Statusbar (always on) darkvater@1648: * - News window, Chatbar (when on) darkvater@1648: * - Any sticked windows since we wanted to keep these darkvater@1648: * @return w pointer to the window that is going to be deleted darkvater@1648: */ rubidium@6573: static Window *FindDeletableWindow() darkvater@763: { Darkvater@5124: Window* const *wz; tron@2639: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; tron@2639: if (w->window_class != WC_MAIN_WINDOW && !IsVitalWindow(w) && !(w->flags4 & WF_STICKY)) { tron@2639: return w; tron@2639: } darkvater@763: } darkvater@763: return NULL; darkvater@763: } darkvater@763: darkvater@1648: /** A window must be freed, and all are marked as important windows. Ease the darkvater@1648: * restriction a bit by allowing to delete sticky windows. Keep important/vital darkvater@1648: * windows intact (Main window, Toolbar, Statusbar, News Window, Chatbar) Darkvater@5124: * Start finding an appropiate candidate from the lowest z-values (bottom) darkvater@1648: * @see FindDeletableWindow() darkvater@1648: * @return w Pointer to the window that is being deleted darkvater@1648: */ rubidium@6573: static Window *ForceFindDeletableWindow() darkvater@763: { Darkvater@5124: Window* const *wz; tron@2639: glx@10294: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; tron@2639: if (w->window_class != WC_MAIN_WINDOW && !IsVitalWindow(w)) return w; darkvater@763: } glx@10294: NOT_REACHED(); darkvater@763: } darkvater@763: rubidium@10513: /** rubidium@10513: * Assign widgets to a new window by initialising its widget pointers, and by rubidium@10513: * copying the widget array \a widget to \c w->widget to allow for resizable rubidium@10513: * windows. rubidium@9599: * @param w Window on which to attach the widget array rubidium@10513: * @param widget pointer of widget array to fill the window with rubidium@10513: * rubidium@10513: * @post \c w->widget points to allocated memory and contains the copied widget array except for the terminating widget, rubidium@10513: * \c w->widget_count contains number of widgets in the allocated memory. rubidium@10513: */ rubidium@10513: static void AssignWidgetToWindow(Window *w, const Widget *widget) truelight@867: { truelight@867: if (widget != NULL) { truelight@867: uint index = 1; truelight@867: rubidium@10455: for (const Widget *wi = widget; wi->type != WWT_LAST; wi++) index++; tron@2639: rubidium@10513: w->widget = MallocT(index); tron@2639: memcpy(w->widget, widget, sizeof(*w->widget) * index); rubidium@5232: w->widget_count = index - 1; tron@2639: } else { truelight@867: w->widget = NULL; rubidium@5232: w->widget_count = 0; tron@2639: } truelight@867: } truelight@867: rubidium@10513: /** rubidium@10513: * Initializes a new Window. rubidium@10513: * This function is called the constructors. bjarni@4520: * See descriptions for those functions for usage bjarni@4520: * Only addition here is window_number, which is the window_number being assigned to the new window rubidium@9599: * @param x offset in pixels from the left of the screen rubidium@9599: * @param y offset in pixels from the top of the screen rubidium@9694: * @param min_width minimum width in pixels of the window rubidium@9694: * @param min_height minimum height in pixels of the window rubidium@9599: * @param cls see WindowClass class of the window, used for identification and grouping rubidium@9599: * @param *widget see Widget pointer to the window layout and various elements rubidium@9599: * @param window_number number being assigned to the new window glx@10645: * @return Window pointer of the newly created window glx@10645: */ rubidium@10513: void Window::Initialize(int x, int y, int min_width, int min_height, glx@10645: WindowClass cls, const Widget *widget, int window_number) truelight@0: { Darkvater@5124: /* We have run out of windows, close one and use that as the place for our new one */ rubidium@10181: if (_last_z_window == endof(_z_windows)) { rubidium@10513: Window *w = FindDeletableWindow(); Darkvater@5124: if (w == NULL) w = ForceFindDeletableWindow(); rubidium@10455: delete w; truelight@0: } truelight@0: glx@9574: /* Set up window properties */ rubidium@10513: this->window_class = cls; rubidium@10513: this->flags4 = WF_WHITE_BORDER_MASK; // just opened windows have a white border rubidium@10513: this->caption_color = 0xFF; rubidium@10513: this->left = x; rubidium@10513: this->top = y; rubidium@10513: this->width = min_width; rubidium@10513: this->height = min_height; rubidium@10513: AssignWidgetToWindow(this, widget); rubidium@10513: this->resize.width = min_width; rubidium@10513: this->resize.height = min_height; rubidium@10513: this->resize.step_width = 1; rubidium@10513: this->resize.step_height = 1; rubidium@10513: this->window_number = window_number; Darkvater@5124: rubidium@10513: /* Hacky way of specifying always-on-top windows. These windows are rubidium@10513: * always above other windows because they are moved below them. rubidium@10513: * status-bar is above news-window because it has been created earlier. rubidium@10513: * Also, as the chat-window is excluded from this, it will always be rubidium@10513: * the last window, thus always on top. rubidium@10513: * XXX - Yes, ugly, probably needs something like w->always_on_top flag rubidium@10513: * to implement correctly, but even then you need some kind of distinction rubidium@10513: * between on-top of chat/news and status windows, because these conflict */ rubidium@10513: Window **wz = _last_z_window; rubidium@10513: if (wz != _z_windows && this->window_class != WC_SEND_NETWORK_MSG && this->window_class != WC_HIGHSCORE && this->window_class != WC_ENDSCREEN) { rubidium@10513: if (FindWindowById(WC_MAIN_TOOLBAR, 0) != NULL) wz--; rubidium@10513: if (FindWindowById(WC_STATUS_BAR, 0) != NULL) wz--; rubidium@10513: if (FindWindowById(WC_NEWS_WINDOW, 0) != NULL) wz--; rubidium@10513: if (FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL) wz--; Darkvater@5124: rubidium@10513: assert(wz >= _z_windows); rubidium@10513: if (wz != _last_z_window) memmove(wz + 1, wz, (byte*)_last_z_window - (byte*)wz); rubidium@10513: } Darkvater@5124: rubidium@10513: *wz = this; rubidium@10513: _last_z_window++; rubidium@10513: } rubidium@9694: rubidium@10513: /** glx@10829: * Resize window towards the default size. glx@10829: * Prior to construction, a position for the new window (for its default size) glx@10829: * has been found with LocalGetWindowPlacement(). Initially, the window is glx@10829: * constructed with minimal size. Resizing the window to its default size is glx@10829: * done here. rubidium@10513: * @param def_width default width in pixels of the window rubidium@10513: * @param def_height default height in pixels of the window glx@10829: * @see Window::Window(), Window::Initialize() rubidium@10513: */ rubidium@10513: void Window::FindWindowPlacementAndResize(int def_width, int def_height) rubidium@10513: { rubidium@9694: /* Try to make windows smaller when our window is too small. rubidium@9694: * w->(width|height) is normally the same as min_(width|height), rubidium@9694: * but this way the GUIs can be made a little more dynamic; rubidium@9694: * one can use the same spec for multiple windows and those rubidium@9694: * can then determine the real minimum size of the window. */ rubidium@10513: if (this->width != def_width || this->height != def_height) { rubidium@9694: /* Think about the overlapping toolbars when determining the minimum window size */ rubidium@9694: int free_height = _screen.height; rubidium@9694: const Window *wt = FindWindowById(WC_STATUS_BAR, 0); rubidium@9694: if (wt != NULL) free_height -= wt->height; rubidium@9694: wt = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@9694: if (wt != NULL) free_height -= wt->height; rubidium@9694: rubidium@10513: int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0); rubidium@10513: int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0); rubidium@9694: rubidium@9694: /* X and Y has to go by step.. calculate it. rubidium@9694: * The cast to int is necessary else x/y are implicitly casted to rubidium@9694: * unsigned int, which won't work. */ rubidium@10513: if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width; rubidium@10513: if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height; rubidium@9694: rubidium@10513: ResizeWindow(this, enlarge_x, enlarge_y); rubidium@10513: rubidium@10513: Point size; rubidium@10513: Point diff; rubidium@10513: size.x = this->width; rubidium@10513: size.y = this->height; rubidium@10513: diff.x = enlarge_x; rubidium@10513: diff.y = enlarge_y; rubidium@10513: this->OnResize(size, diff); rubidium@9694: } rubidium@9694: rubidium@10513: int nx = this->left; rubidium@10513: int ny = this->top; rubidium@9701: rubidium@10513: if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width); rubidium@9694: rubidium@9694: const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@10513: ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height); rubidium@9701: nx = max(nx, 0); rubidium@9701: rubidium@10513: if (this->viewport != NULL) { rubidium@10513: this->viewport->left += nx - this->left; rubidium@10513: this->viewport->top += ny - this->top; rubidium@9701: } rubidium@10513: this->left = nx; rubidium@10513: this->top = ny; rubidium@9694: rubidium@10513: this->SetDirty(); rubidium@10513: } truelight@0: rubidium@10513: void Window::FindWindowPlacementAndResize(const WindowDesc *desc) rubidium@10513: { rubidium@10513: this->FindWindowPlacementAndResize(desc->default_width, desc->default_height); truelight@0: } truelight@0: bjarni@4520: /** bjarni@4520: * Open a new window. If there is no space for a new window, close an open bjarni@4520: * window. Try to avoid stickied windows, but if there is no else, close one of bjarni@4520: * those as well. Then make sure all created windows are below some always-on-top bjarni@4520: * ones. Finally set all variables and call the WE_CREATE event bjarni@4520: * @param x offset in pixels from the left of the screen bjarni@4520: * @param y offset in pixels from the top of the screen bjarni@4520: * @param width width in pixels of the window bjarni@4520: * @param height height in pixels of the window rubidium@9599: * @param cls see WindowClass class of the window, used for identification and grouping rubidium@9599: * @param *widget see Widget pointer to the window layout and various elements rubidium@10142: * @return Window pointer of the newly created window rubidium@10142: */ glx@10645: Window::Window(int x, int y, int width, int height, WindowClass cls, const Widget *widget) bjarni@4520: { glx@10645: this->Initialize(x, y, width, height, cls, widget, 0); bjarni@4520: } truelight@0: glx@10829: /** glx@10829: * Decide whether a given rectangle is a good place to open a completely visible new window. glx@10829: * The new window should be within screen borders, and not overlap with another already glx@10829: * existing window (except for the main window in the background). glx@10829: * @param left Left edge of the rectangle glx@10829: * @param top Top edge of the rectangle glx@10829: * @param width Width of the rectangle glx@10829: * @param height Height of the rectangle glx@10829: * @param pos If rectangle is good, use this parameter to return the top-left corner of the new window glx@10829: * @return Boolean indication that the rectangle is a good place for the new window glx@10829: */ rubidium@9620: static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos) truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: rubidium@9620: int right = width + left; rubidium@9620: int bottom = height + top; truelight@0: rubidium@10455: if (left < 0 || top < 22 || right > _screen.width || bottom > _screen.height) return false; truelight@0: glx@9574: /* Make sure it is not obscured by any window. */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; tron@2639: if (w->window_class == WC_MAIN_WINDOW) continue; truelight@0: truelight@158: if (right > w->left && tron@2639: w->left + w->width > left && truelight@0: bottom > w->top && tron@2639: w->top + w->height > top) { tron@2639: return false; tron@2639: } truelight@158: } truelight@0: rubidium@9620: pos.x = left; rubidium@9620: pos.y = top; truelight@0: return true; truelight@0: } truelight@0: glx@10829: /** glx@10829: * Decide whether a given rectangle is a good place to open a mostly visible new window. glx@10829: * The new window should be mostly within screen borders, and not overlap with another already glx@10829: * existing window (except for the main window in the background). glx@10829: * @param left Left edge of the rectangle glx@10829: * @param top Top edge of the rectangle glx@10829: * @param width Width of the rectangle glx@10829: * @param height Height of the rectangle glx@10829: * @param pos If rectangle is good, use this parameter to return the top-left corner of the new window glx@10829: * @return Boolean indication that the rectangle is a good place for the new window glx@10829: */ rubidium@9620: static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos) truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: glx@10829: /* Left part of the rectangle may be at most 1/4 off-screen, glx@10829: * right part of the rectangle may be at most 1/2 off-screen glx@10829: */ Darkvater@5120: if (left < -(width>>2) || left > _screen.width - (width>>1)) return false; glx@10829: /* Bottom part of the rectangle may be at most 1/4 off-screen */ Darkvater@5120: if (top < 22 || top > _screen.height - (height>>2)) return false; truelight@0: glx@9574: /* Make sure it is not obscured by any window. */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; tron@2639: if (w->window_class == WC_MAIN_WINDOW) continue; truelight@0: truelight@158: if (left + width > w->left && tron@2639: w->left + w->width > left && truelight@0: top + height > w->top && tron@2639: w->top + w->height > top) { tron@2639: return false; tron@2639: } truelight@158: } truelight@0: rubidium@9620: pos.x = left; rubidium@9620: pos.y = top; truelight@0: return true; truelight@0: } truelight@0: glx@10829: /** glx@10829: * Find a good place for opening a new window of a given width and height. glx@10829: * @param width Width of the new window glx@10829: * @param height Height of the new window glx@10829: * @return Top-left coordinate of the new window glx@10829: */ tron@1095: static Point GetAutoPlacePosition(int width, int height) tron@1095: { Darkvater@5124: Window* const *wz; truelight@0: Point pt; truelight@0: glx@10829: /* First attempt, try top-left of the screen */ rubidium@9620: if (IsGoodAutoPlace1(0, 24, width, height, pt)) return pt; truelight@0: glx@10829: /* Second attempt, try around all existing windows with a distance of 2 pixels. glx@10829: * The new window must be entirely on-screen, and not overlap with an existing window. glx@10829: * Eight starting points are tried, two at each corner. glx@10829: */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; tron@2639: if (w->window_class == WC_MAIN_WINDOW) continue; truelight@0: rubidium@9620: if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace1(w->left - width - 2, w->top, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace1(w->left, w->top - height - 2, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace1(w->left - width - 2, w->top + w->height - height, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2, width, height, pt)) return pt; truelight@158: } truelight@158: glx@10829: /* Third attempt, try around all existing windows with a distance of 2 pixels. glx@10829: * The new window may be partly off-screen, and must not overlap with an existing window. glx@10829: * Only four starting points are tried. glx@10829: */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; tron@2639: if (w->window_class == WC_MAIN_WINDOW) continue; truelight@0: rubidium@9620: if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace2(w->left - width - 2, w->top, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt; rubidium@9620: if (IsGoodAutoPlace2(w->left, w->top - height - 2, width, height, pt)) return pt; truelight@0: } truelight@0: glx@10829: /* Fourth and final attempt, put window at diagonal starting from (0, 24), try multiples glx@10829: * of (+5, +5) glx@10829: */ truelight@0: { rubidium@9620: int left = 0, top = 24; truelight@158: rubidium@9620: restart: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; Darkvater@5124: truelight@0: if (w->left == left && w->top == top) { truelight@0: left += 5; truelight@0: top += 5; truelight@0: goto restart; truelight@0: } truelight@0: } truelight@158: truelight@0: pt.x = left; truelight@0: pt.y = top; truelight@0: return pt; truelight@0: } truelight@0: } truelight@0: truelight@9718: /** rubidium@10455: * Compute the position of the top-left corner of a new window that is opened. rubidium@10455: * rubidium@10455: * By default position a child window at an offset of 10/10 of its parent. rubidium@10455: * With the exception of WC_BUILD_TOOLBAR (build railway/roads/ship docks/airports) rubidium@10455: * and WC_SCEN_LAND_GEN (landscaping). Whose child window has an offset of 0/36 of rubidium@10455: * its parent. So it's exactly under the parent toolbar and no buttons will be covered. rubidium@10455: * However if it falls too extremely outside window positions, reposition rubidium@10455: * it to an automatic place. truelight@9718: * truelight@9718: * @param *desc The pointer to the WindowDesc to be created truelight@9718: * @param window_number the window number of the new window truelight@9718: * rubidium@10455: * @return Coordinate of the top-left corner of the new window truelight@9718: */ rubidium@10455: static Point LocalGetWindowPlacement(const WindowDesc *desc, int window_number) truelight@0: { truelight@0: Point pt; truelight@0: Window *w; truelight@0: Darkvater@5071: if (desc->parent_cls != 0 /* WC_MAIN_WINDOW */ && Darkvater@5071: (w = FindWindowById(desc->parent_cls, window_number)) != NULL && Darkvater@5071: w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) { Darkvater@5071: truelight@9718: pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10); rubidium@9694: if (pt.x > _screen.width + 10 - desc->default_width) { rubidium@9694: pt.x = (_screen.width + 10 - desc->default_width) - 20; Darkvater@5071: } truelight@9718: pt.y = w->top + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 36 : 10); truelight@0: } else { Darkvater@5072: switch (desc->left) { rubidium@10455: case WDP_ALIGN_TBR: // Align the right side with the top toolbar Darkvater@5072: w = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@9694: pt.x = (w->left + w->width) - desc->default_width; rubidium@10455: break; rubidium@10455: rubidium@10455: case WDP_ALIGN_TBL: // Align the left side with the top toolbar Darkvater@5072: pt.x = FindWindowById(WC_MAIN_TOOLBAR, 0)->left; Darkvater@5072: break; rubidium@10455: rubidium@10455: case WDP_AUTO: // Find a good automatic position for the window rubidium@10455: return GetAutoPlacePosition(desc->default_width, desc->default_height); rubidium@10455: rubidium@10455: case WDP_CENTER: // Centre the window horizontally rubidium@9694: pt.x = (_screen.width - desc->default_width) / 2; Darkvater@5072: break; rubidium@10455: Darkvater@5072: default: Darkvater@5072: pt.x = desc->left; Darkvater@5072: if (pt.x < 0) pt.x += _screen.width; // negative is from right of the screen Darkvater@5072: } Darkvater@5072: Darkvater@5072: switch (desc->top) { rubidium@10455: case WDP_CENTER: // Centre the window vertically rubidium@9694: pt.y = (_screen.height - desc->default_height) / 2; Darkvater@5072: break; rubidium@10455: Darkvater@5072: /* WDP_AUTO sets the position at once and is controlled by desc->left. Darkvater@5072: * Both left and top must be set to WDP_AUTO */ Darkvater@5072: case WDP_AUTO: Darkvater@5072: NOT_REACHED(); Darkvater@5072: assert(desc->left == WDP_AUTO && desc->top != WDP_AUTO); Darkvater@5072: /* fallthrough */ rubidium@10455: Darkvater@5072: default: Darkvater@5072: pt.y = desc->top; Darkvater@5072: if (pt.y < 0) pt.y += _screen.height; // negative is from bottom of the screen Darkvater@5072: break; truelight@0: } truelight@0: } truelight@0: rubidium@10455: return pt; rubidium@10455: } rubidium@10455: rubidium@10455: /** rubidium@10455: * Set the positions of a new window from a WindowDesc and open it. rubidium@10455: * rubidium@10455: * @param *desc The pointer to the WindowDesc to be created rubidium@10455: * @param window_number the window number of the new window rubidium@10455: * rubidium@10455: * @return Window pointer of the newly created window rubidium@10455: */ glx@10645: Window::Window(const WindowDesc *desc, WindowNumber window_number) rubidium@10455: { rubidium@10455: Point pt = LocalGetWindowPlacement(desc, window_number); glx@10645: this->Initialize(pt.x, pt.y, desc->minimum_width, desc->minimum_height, desc->cls, desc->widgets, window_number); rubidium@10513: this->desc_flags = desc->flags; bjarni@4520: } bjarni@4520: Darkvater@5120: /** Do a search for a window at specific coordinates. For this we start Darkvater@5120: * at the topmost window, obviously and work our way down to the bottom rubidium@9599: * @param x position x to query rubidium@9599: * @param y position y to query Darkvater@5120: * @return a pointer to the found window if any, NULL otherwise */ truelight@0: Window *FindWindowFromPt(int x, int y) truelight@0: { glx@10294: for (Window * const *wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: Window *w = *--wz; rubidium@9723: if (IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) { tron@2639: return w; tron@2639: } truelight@0: } truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: rubidium@10181: /** rubidium@10181: * (re)initialize the windowing system rubidium@10181: */ rubidium@6573: void InitWindowSystem() darkvater@152: { dominik@136: IConsoleClose(); Darkvater@1474: Darkvater@5124: _last_z_window = _z_windows; glx@10294: _mouseover_last_w = NULL; Darkvater@1397: _no_scroll = 0; glx@10776: _scrolling_viewport = 0; truelight@0: } truelight@0: rubidium@10181: /** rubidium@10181: * Close down the windowing system rubidium@10181: */ rubidium@6573: void UnInitWindowSystem() Darkvater@1474: { rubidium@10455: while (_last_z_window != _z_windows) delete _z_windows[0]; Darkvater@1474: } Darkvater@1474: rubidium@10181: /** rubidium@10181: * Reset the windowing system, by means of shutting it down followed by re-initialization rubidium@10181: */ rubidium@6573: void ResetWindowSystem() Darkvater@1474: { Darkvater@1474: UnInitWindowSystem(); Darkvater@1474: InitWindowSystem(); Darkvater@1744: _thd.pos.x = 0; Darkvater@1744: _thd.pos.y = 0; Darkvater@2877: _thd.new_pos.x = 0; Darkvater@2877: _thd.new_pos.y = 0; Darkvater@1474: } Darkvater@1474: rubidium@6573: static void DecreaseWindowCounters() truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { rubidium@10455: Window *w = *--wz; glx@9574: /* Unclick scrollbar buttons if they are pressed. */ truelight@0: if (w->flags4 & (WF_SCROLL_DOWN | WF_SCROLL_UP)) { truelight@0: w->flags4 &= ~(WF_SCROLL_DOWN | WF_SCROLL_UP); rubidium@10455: w->SetDirty(); truelight@0: } rubidium@10513: w->OnMouseLoop(); truelight@0: } truelight@0: Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { rubidium@10455: Window *w = *--wz; truelight@158: rubidium@10455: if (w->flags4 & WF_TIMEOUT_MASK && !(--w->flags4 & WF_TIMEOUT_MASK)) { rubidium@10513: w->OnTimeout(); rubidium@9723: if (w->desc_flags & WDF_UNCLICK_BUTTONS) w->RaiseButtons(); truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@6573: Window *GetCallbackWnd() truelight@0: { truelight@0: return FindWindowById(_thd.window_class, _thd.window_number); truelight@0: } truelight@0: rubidium@6573: static void HandlePlacePresize() truelight@0: { tron@2639: if (_special_mouse_mode != WSM_PRESIZE) return; truelight@0: rubidium@10455: Window *w = GetCallbackWnd(); tron@2639: if (w == NULL) return; truelight@0: rubidium@10513: Point pt = GetTileBelowCursor(); rubidium@10513: if (pt.x == -1) { truelight@0: _thd.selend.x = -1; truelight@0: return; truelight@0: } rubidium@10513: rubidium@10513: w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y)); truelight@0: } truelight@0: rubidium@6573: static bool HandleDragDrop() truelight@0: { tron@2639: if (_special_mouse_mode != WSM_DRAGDROP) return true; tron@2639: if (_left_button_down) return false; truelight@158: rubidium@10455: Window *w = GetCallbackWnd(); truelight@0: tron@2639: if (w != NULL) { glx@9574: /* send an event in client coordinates. */ rubidium@10513: Point pt; rubidium@10513: pt.x = _cursor.pos.x - w->left; rubidium@10513: pt.y = _cursor.pos.y - w->top; rubidium@10513: w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y)); truelight@0: } glx@9732: glx@9732: ResetObjectToPlace(); glx@9732: truelight@0: return false; truelight@0: } truelight@0: rubidium@6573: static bool HandleMouseOver() truelight@543: { rubidium@10181: Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y); truelight@543: glx@9574: /* We changed window, put a MOUSEOVER event to the last window */ rubidium@10181: if (_mouseover_last_w != NULL && _mouseover_last_w != w) { rubidium@10181: /* Reset mouse-over coordinates of previous window */ rubidium@10513: Point pt = { -1, -1 }; rubidium@10513: _mouseover_last_w->OnMouseOver(pt, 0); truelight@543: } rubidium@10181: rubidium@10181: /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */ rubidium@10181: _mouseover_last_w = w; truelight@543: tron@2639: if (w != NULL) { glx@9574: /* send an event in client coordinates. */ rubidium@10513: Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top }; rubidium@10513: int widget = 0; truelight@543: if (w->widget != NULL) { rubidium@10513: widget = GetWidgetFromPos(w, pt.x, pt.y); truelight@543: } rubidium@10513: w->OnMouseOver(pt, widget); truelight@543: } truelight@543: glx@9574: /* Mouseover never stops execution */ truelight@543: return true; truelight@543: } truelight@543: rubidium@10142: /** rubidium@10142: * Resize the window. rubidium@10142: * Update all the widgets of a window based on their resize flags Darkvater@5268: * Both the areas of the old window and the new sized window are set dirty Darkvater@5268: * ensuring proper redrawal. Darkvater@5268: * @param w Window to resize rubidium@9601: * @param x delta x-size of changed window (positive if larger, etc.) rubidium@10142: * @param y delta y-size of changed window rubidium@10142: */ Darkvater@5268: void ResizeWindow(Window *w, int x, int y) Darkvater@5268: { Darkvater@5268: bool resize_height = false; Darkvater@5268: bool resize_width = false; Darkvater@5268: Darkvater@5268: if (x == 0 && y == 0) return; Darkvater@5268: rubidium@10455: w->SetDirty(); rubidium@10455: for (Widget *wi = w->widget; wi->type != WWT_LAST; wi++) { Darkvater@5268: /* Isolate the resizing flags */ Darkvater@5268: byte rsizeflag = GB(wi->display_flags, 0, 4); Darkvater@5268: Darkvater@5268: if (rsizeflag == RESIZE_NONE) continue; Darkvater@5268: Darkvater@5268: /* Resize the widget based on its resize-flag */ Darkvater@5268: if (rsizeflag & RESIZE_LEFT) { Darkvater@5268: wi->left += x; Darkvater@5268: resize_width = true; Darkvater@5268: } Darkvater@5268: Darkvater@5268: if (rsizeflag & RESIZE_RIGHT) { Darkvater@5268: wi->right += x; Darkvater@5268: resize_width = true; Darkvater@5268: } Darkvater@5268: Darkvater@5268: if (rsizeflag & RESIZE_TOP) { Darkvater@5268: wi->top += y; Darkvater@5268: resize_height = true; Darkvater@5268: } Darkvater@5268: Darkvater@5268: if (rsizeflag & RESIZE_BOTTOM) { Darkvater@5268: wi->bottom += y; Darkvater@5268: resize_height = true; Darkvater@5268: } Darkvater@5268: } Darkvater@5268: Darkvater@5268: /* We resized at least 1 widget, so let's resize the window totally */ Darkvater@5268: if (resize_width) w->width += x; Darkvater@5268: if (resize_height) w->height += y; Darkvater@5268: rubidium@10455: w->SetDirty(); Darkvater@5268: } tron@2596: tron@2596: static bool _dragging_window; tron@2596: rubidium@6573: static bool HandleWindowDragging() truelight@0: { Darkvater@5124: Window* const *wz; glx@9574: /* Get out immediately if no window is being dragged at all. */ tron@2639: if (!_dragging_window) return true; truelight@0: glx@9574: /* Otherwise find the window... */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; Darkvater@5124: tron@350: if (w->flags4 & WF_DRAGGING) { tron@370: const Widget *t = &w->widget[1]; // the title bar ... ugh truelight@158: glx@9574: /* Stop the dragging if the left mouse button was released */ truelight@0: if (!_left_button_down) { tron@350: w->flags4 &= ~WF_DRAGGING; truelight@0: break; truelight@0: } truelight@0: rubidium@10455: w->SetDirty(); truelight@0: rubidium@10455: int x = _cursor.pos.x + _drag_delta.x; rubidium@10455: int y = _cursor.pos.y + _drag_delta.y; rubidium@10455: int nx = x; rubidium@10455: int ny = y; tron@350: glx@10776: if (_settings_client.gui.window_snap_radius != 0) { Darkvater@5137: Window* const *vz; Darkvater@5124: glx@10776: int hsnap = _settings_client.gui.window_snap_radius; glx@10776: int vsnap = _settings_client.gui.window_snap_radius; tron@353: int delta; tron@350: Darkvater@5124: FOR_ALL_WINDOWS(vz) { Darkvater@5124: const Window *v = *vz; Darkvater@5124: tron@350: if (v == w) continue; // Don't snap at yourself tron@350: tron@350: if (y + w->height > v->top && y < v->top + v->height) { glx@9574: /* Your left border <-> other right border */ tron@350: delta = abs(v->left + v->width - x); tron@350: if (delta <= hsnap) { tron@350: nx = v->left + v->width; tron@350: hsnap = delta; tron@350: } tron@350: glx@9574: /* Your right border <-> other left border */ tron@350: delta = abs(v->left - x - w->width); tron@350: if (delta <= hsnap) { tron@350: nx = v->left - w->width; tron@350: hsnap = delta; tron@350: } tron@350: } tron@350: tron@353: if (w->top + w->height >= v->top && w->top <= v->top + v->height) { glx@9574: /* Your left border <-> other left border */ tron@353: delta = abs(v->left - x); tron@353: if (delta <= hsnap) { tron@353: nx = v->left; tron@353: hsnap = delta; tron@353: } tron@353: glx@9574: /* Your right border <-> other right border */ tron@353: delta = abs(v->left + v->width - x - w->width); tron@353: if (delta <= hsnap) { tron@353: nx = v->left + v->width - w->width; tron@353: hsnap = delta; tron@353: } tron@353: } tron@353: tron@350: if (x + w->width > v->left && x < v->left + v->width) { glx@9574: /* Your top border <-> other bottom border */ tron@350: delta = abs(v->top + v->height - y); tron@350: if (delta <= vsnap) { tron@350: ny = v->top + v->height; tron@350: vsnap = delta; tron@350: } tron@350: glx@9574: /* Your bottom border <-> other top border */ tron@350: delta = abs(v->top - y - w->height); tron@350: if (delta <= vsnap) { tron@350: ny = v->top - w->height; tron@350: vsnap = delta; tron@350: } tron@350: } tron@353: tron@353: if (w->left + w->width >= v->left && w->left <= v->left + v->width) { glx@9574: /* Your top border <-> other top border */ tron@353: delta = abs(v->top - y); tron@353: if (delta <= vsnap) { tron@353: ny = v->top; tron@353: vsnap = delta; tron@353: } tron@353: glx@9574: /* Your bottom border <-> other bottom border */ tron@353: delta = abs(v->top + v->height - y - w->height); tron@353: if (delta <= vsnap) { tron@353: ny = v->top + v->height - w->height; tron@353: vsnap = delta; tron@353: } tron@353: } truelight@0: } truelight@0: } truelight@158: glx@9574: /* Make sure the window doesn't leave the screen glx@9574: * 13 is the height of the title bar */ rubidium@9722: nx = Clamp(nx, 13 - t->right, _screen.width - 13 - t->left); rubidium@9722: ny = Clamp(ny, 0, _screen.height - 13); tron@350: glx@9574: /* Make sure the title bar isn't hidden by behind the main tool bar */ rubidium@10455: Window *v = FindWindowById(WC_MAIN_TOOLBAR, 0); tron@370: if (v != NULL) { tron@370: int v_bottom = v->top + v->height; tron@370: int v_right = v->left + v->width; tron@370: if (ny + t->top >= v->top && ny + t->top < v_bottom) { tron@371: if ((v->left < 13 && nx + t->left < v->left) || tron@371: (v_right > _screen.width - 13 && nx + t->right > v_right)) { tron@370: ny = v_bottom; tron@370: } else { tron@370: if (nx + t->left > v->left - 13 && tron@370: nx + t->right < v_right + 13) { tron@2639: if (w->top >= v_bottom) { tron@370: ny = v_bottom; tron@2639: } else if (w->left < nx) { tron@370: nx = v->left - 13 - t->left; tron@2639: } else { tron@370: nx = v_right + 13 - t->right; tron@2639: } tron@370: } tron@370: } tron@370: } tron@370: } tron@370: tron@350: if (w->viewport != NULL) { tron@350: w->viewport->left += nx - w->left; tron@350: w->viewport->top += ny - w->top; tron@350: } tron@350: w->left = nx; tron@350: w->top = ny; tron@350: rubidium@10455: w->SetDirty(); truelight@0: return false; truelight@867: } else if (w->flags4 & WF_SIZING) { truelight@867: int x, y; truelight@867: truelight@867: /* Stop the sizing if the left mouse button was released */ truelight@867: if (!_left_button_down) { truelight@867: w->flags4 &= ~WF_SIZING; rubidium@10455: w->SetDirty(); truelight@867: break; truelight@867: } truelight@867: truelight@867: x = _cursor.pos.x - _drag_delta.x; truelight@867: y = _cursor.pos.y - _drag_delta.y; truelight@867: peter1138@2675: /* X and Y has to go by step.. calculate it. peter1138@2675: * The cast to int is necessary else x/y are implicitly casted to peter1138@2675: * unsigned int, which won't work. */ peter1138@2675: if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width; truelight@867: peter1138@2675: if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height; truelight@867: truelight@867: /* Check if we don't go below the minimum set size */ truelight@867: if ((int)w->width + x < (int)w->resize.width) truelight@867: x = w->resize.width - w->width; truelight@867: if ((int)w->height + y < (int)w->resize.height) truelight@867: y = w->resize.height - w->height; truelight@867: truelight@867: /* Window already on size */ tron@2639: if (x == 0 && y == 0) return false; truelight@867: truelight@867: /* Now find the new cursor pos.. this is NOT _cursor, because truelight@867: we move in steps. */ truelight@867: _drag_delta.x += x; truelight@867: _drag_delta.y += y; truelight@867: Darkvater@5268: /* ResizeWindow sets both pre- and after-size to dirty for redrawal */ Darkvater@5268: ResizeWindow(w, x, y); truelight@867: rubidium@10513: Point size; rubidium@10513: Point diff; rubidium@10513: size.x = x + w->width; rubidium@10513: size.y = y + w->height; rubidium@10513: diff.x = x; rubidium@10513: diff.y = y; rubidium@10513: w->OnResize(size, diff); truelight@867: return false; truelight@0: } truelight@0: } truelight@0: truelight@0: _dragging_window = false; truelight@0: return false; truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Start window dragging rubidium@10142: * @param w Window to start dragging rubidium@10142: */ Darkvater@5124: static void StartWindowDrag(Window *w) truelight@0: { truelight@0: w->flags4 |= WF_DRAGGING; truelight@0: _dragging_window = true; truelight@867: tron@350: _drag_delta.x = w->left - _cursor.pos.x; tron@350: _drag_delta.y = w->top - _cursor.pos.y; truelight@867: Darkvater@5124: BringWindowToFront(w); truelight@0: DeleteWindowById(WC_DROPDOWN_MENU, 0); truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Start resizing a window rubidium@10142: * @param w Window to start resizing rubidium@10142: */ Darkvater@5124: static void StartWindowSizing(Window *w) truelight@0: { truelight@0: w->flags4 |= WF_SIZING; truelight@0: _dragging_window = true; truelight@867: truelight@867: _drag_delta.x = _cursor.pos.x; truelight@867: _drag_delta.y = _cursor.pos.y; truelight@867: Darkvater@5124: BringWindowToFront(w); truelight@0: DeleteWindowById(WC_DROPDOWN_MENU, 0); truelight@0: } truelight@0: truelight@0: rubidium@6573: static bool HandleScrollbarScrolling() truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: glx@9574: /* Get out quickly if no item is being scrolled */ tron@2639: if (!_scrolling_scrollbar) return true; truelight@0: glx@9574: /* Find the scrolling window */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; Darkvater@5124: truelight@0: if (w->flags4 & WF_SCROLL_MIDDLE) { glx@9574: /* Abort if no button is clicked any more. */ truelight@0: if (!_left_button_down) { truelight@0: w->flags4 &= ~WF_SCROLL_MIDDLE; rubidium@10455: w->SetDirty(); truelight@0: break; truelight@158: } truelight@0: rubidium@10455: int i; rubidium@10455: Scrollbar *sb; rubidium@10455: truelight@0: if (w->flags4 & WF_HSCROLL) { truelight@0: sb = &w->hscroll; truelight@0: i = _cursor.pos.x - _cursorpos_drag_start.x; bjarni@842: } else if (w->flags4 & WF_SCROLL2){ bjarni@842: sb = &w->vscroll2; bjarni@842: i = _cursor.pos.y - _cursorpos_drag_start.y; truelight@0: } else { truelight@0: sb = &w->vscroll; truelight@0: i = _cursor.pos.y - _cursorpos_drag_start.y; truelight@0: } truelight@0: glx@9574: /* Find the item we want to move to and make sure it's inside bounds. */ rubidium@10455: int pos = min(max(0, i + _scrollbar_start_pos) * sb->count / _scrollbar_size, max(0, sb->count - sb->cap)); truelight@0: if (pos != sb->pos) { truelight@0: sb->pos = pos; rubidium@10455: w->SetDirty(); truelight@0: } truelight@0: return false; truelight@158: } truelight@0: } truelight@158: truelight@0: _scrolling_scrollbar = false; truelight@0: return false; truelight@0: } truelight@0: rubidium@6573: static bool HandleViewportScroll() truelight@0: { glx@10776: bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0); bjarni@6615: tron@2639: if (!_scrolling_viewport) return true; truelight@0: rubidium@10455: Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y); truelight@4335: rubidium@9722: if (!(_right_button_down || scrollwheel_scrolling) || w == NULL) { rubidium@9722: _cursor.fix_at = false; rubidium@9722: _scrolling_viewport = false; rubidium@9722: return true; rubidium@9722: } rubidium@9722: rubidium@10513: if (w == FindWindowById(WC_MAIN_WINDOW, 0) && w->viewport->follow_vehicle != INVALID_VEHICLE) { truelight@9718: /* If the main window is following a vehicle, then first let go of it! */ rubidium@10513: const Vehicle *veh = GetVehicle(w->viewport->follow_vehicle); truelight@9718: ScrollMainWindowTo(veh->x_pos, veh->y_pos, true); /* This also resets follow_vehicle */ truelight@9718: return true; truelight@9718: } truelight@9718: rubidium@10513: Point delta; glx@10776: if (_settings_client.gui.reverse_scroll) { rubidium@10513: delta.x = -_cursor.delta.x; rubidium@10513: delta.y = -_cursor.delta.y; tron@2680: } else { rubidium@10513: delta.x = _cursor.delta.x; rubidium@10513: delta.y = _cursor.delta.y; tron@2680: } tron@2680: bjarni@6615: if (scrollwheel_scrolling) { bjarni@6615: /* We are using scrollwheels for scrolling */ rubidium@10513: delta.x = _cursor.h_wheel; rubidium@10513: delta.y = _cursor.v_wheel; bjarni@6615: _cursor.v_wheel = 0; bjarni@6615: _cursor.h_wheel = 0; bjarni@6615: } bjarni@6615: truelight@4335: /* Create a scroll-event and send it to the window */ rubidium@10513: w->OnScroll(delta); truelight@0: tron@2989: _cursor.delta.x = 0; tron@2989: _cursor.delta.y = 0; tron@2989: return false; truelight@0: } truelight@0: Darkvater@5667: /** Check if a window can be made top-most window, and if so do Darkvater@5667: * it. If a window does not obscure any other windows, it will not Darkvater@5667: * be brought to the foreground. Also if the only obscuring windows Darkvater@5667: * are so-called system-windows, the window will not be moved. Darkvater@5667: * The function will return false when a child window of this window is a Darkvater@5667: * modal-popup; function returns a false and child window gets a white border Darkvater@5667: * @param w Window to bring on-top Darkvater@5667: * @return false if the window has an active modal child, true otherwise */ Darkvater@5667: static bool MaybeBringWindowToFront(const Window *w) truelight@0: { Darkvater@5667: bool bring_to_front = false; truelight@0: tron@2639: if (w->window_class == WC_MAIN_WINDOW || tron@2639: IsVitalWindow(w) || tron@2639: w->window_class == WC_TOOLTIPS || tron@2639: w->window_class == WC_DROPDOWN_MENU) { Darkvater@5667: return true; tron@2639: } truelight@0: rubidium@10455: Window * const *wz = FindWindowZPosition(w); glx@10294: for (Window * const *uz = wz; ++uz != _last_z_window;) { Darkvater@5667: Window *u = *uz; Darkvater@5667: Darkvater@5667: /* A modal child will prevent the activation of the parent window */ Darkvater@5667: if (u->parent == w && (u->desc_flags & WDF_MODAL)) { Darkvater@5667: u->flags4 |= WF_WHITE_BORDER_MASK; rubidium@10455: u->SetDirty(); Darkvater@5667: return false; Darkvater@5667: } Darkvater@5124: tron@2639: if (u->window_class == WC_MAIN_WINDOW || tron@2639: IsVitalWindow(u) || tron@2639: u->window_class == WC_TOOLTIPS || tron@2639: u->window_class == WC_DROPDOWN_MENU) { tron@2639: continue; tron@2639: } truelight@0: Darkvater@5667: /* Window sizes don't interfere, leave z-order alone */ truelight@0: if (w->left + w->width <= u->left || truelight@0: u->left + u->width <= w->left || truelight@0: w->top + w->height <= u->top || tron@2639: u->top + u->height <= w->top) { tron@2639: continue; tron@2639: } truelight@158: Darkvater@5667: bring_to_front = true; truelight@0: } Darkvater@5667: Darkvater@5667: if (bring_to_front) BringWindowToFront(w); Darkvater@5667: return true; truelight@0: } truelight@0: rubidium@10513: /** Handle keyboard input. rubidium@10513: * @param raw_key Lower 8 bits contain the ASCII character, the higher 16 bits the keycode darkvater@1648: */ rubidium@10513: void HandleKeypress(uint32 raw_key) darkvater@1648: { rubidium@4549: /* Stores if a window with a textfield for typing is open rubidium@4549: * If this is the case, keypress events are only passed to windows with text fields and rubidium@4549: * to thein this main toolbar. */ dominik@651: bool query_open = false; truelight@158: Darkvater@5086: /* Darkvater@5086: * During the generation of the world, there might be Darkvater@5086: * another thread that is currently building for example Darkvater@5086: * a road. To not interfere with those tasks, we should Darkvater@5086: * NOT change the _current_player here. Darkvater@5086: * Darkvater@5086: * This is not necessary either, as the only events that Darkvater@5086: * can be handled are the 'close application' events Darkvater@5086: */ Darkvater@5086: if (!IsGeneratingWorld()) _current_player = _local_player; Darkvater@5086: glx@9574: /* Setup event */ rubidium@10513: uint16 key = GB(raw_key, 0, 16); rubidium@10513: uint16 keycode = GB(raw_key, 16, 16); truelight@0: rubidium@9723: /* rubidium@9723: * The Unicode standard defines an area called the private use area. Code points in this rubidium@9723: * area are reserved for private use and thus not portable between systems. For instance, rubidium@9723: * Apple defines code points for the arrow keys in this area, but these are only printable rubidium@9723: * on a system running OS X. We don't want these keys to show up in text fields and such, rubidium@9723: * and thus we have to clear the unicode character when we encounter such a key. rubidium@9723: */ rubidium@10513: if (key >= 0xE000 && key <= 0xF8FF) key = 0; rubidium@9723: rubidium@9723: /* rubidium@9723: * If both key and keycode is zero, we don't bother to process the event. rubidium@9723: */ rubidium@10513: if (key == 0 && keycode == 0) return; rubidium@9723: glx@9574: /* check if we have a query string window open before allowing hotkeys */ rubidium@9723: if (FindWindowById(WC_QUERY_STRING, 0) != NULL || rubidium@9723: FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL || rubidium@9723: FindWindowById(WC_GENERATE_LANDSCAPE, 0) != NULL || rubidium@9723: FindWindowById(WC_CONSOLE, 0) != NULL || rubidium@9723: FindWindowById(WC_SAVELOAD, 0) != NULL || rubidium@9723: FindWindowById(WC_COMPANY_PASSWORD_WINDOW, 0) != NULL) { dominik@651: query_open = true; tron@2639: } dominik@651: glx@9574: /* Call the event, start with the uppermost window. */ rubidium@10142: for (Window* const *wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: Window *w = *--wz; Darkvater@5124: glx@9574: /* if a query window is open, only call the event for certain window types */ tron@2639: if (query_open && tron@2639: w->window_class != WC_QUERY_STRING && tron@2639: w->window_class != WC_SEND_NETWORK_MSG && truelight@4300: w->window_class != WC_GENERATE_LANDSCAPE && tron@2639: w->window_class != WC_CONSOLE && rubidium@9723: w->window_class != WC_SAVELOAD && rubidium@9723: w->window_class != WC_COMPANY_PASSWORD_WINDOW) { dominik@651: continue; tron@2639: } glx@10645: if (w->OnKeyPress(key, keycode) == Window::ES_HANDLED) return; truelight@0: } Darkvater@1637: rubidium@10513: Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@10513: /* When there is no toolbar w is null, check for that */ rubidium@10513: if (w != NULL) w->OnKeyPress(key, keycode); truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * State of CONTROL key has changed rubidium@10142: */ glx@9732: void HandleCtrlChanged() glx@9732: { glx@9732: /* Call the event, start with the uppermost window. */ glx@9732: for (Window* const *wz = _last_z_window; wz != _z_windows;) { glx@9732: Window *w = *--wz; glx@10645: if (w->OnCTRLStateChange() == Window::ES_HANDLED) return; glx@9732: } glx@9732: } glx@9732: rubidium@10142: /** rubidium@10142: * Local counter that is incremented each time an mouse input event is detected. rubidium@10142: * The counter is used to stop auto-scrolling. rubidium@10142: * @see HandleAutoscroll() rubidium@10142: * @see HandleMouseEvents() rubidium@10142: */ Darkvater@5090: static int _input_events_this_tick = 0; Darkvater@5090: rubidium@10142: /** rubidium@10142: * If needed and switched on, perform auto scrolling (automatically rubidium@10142: * moving window contents when mouse is near edge of the window). rubidium@10142: */ rubidium@6573: static void HandleAutoscroll() Darkvater@5090: { Darkvater@5090: if (_input_events_this_tick != 0) { Darkvater@5090: /* HandleAutoscroll is called only once per GameLoop() - so we can clear the counter here */ Darkvater@5090: _input_events_this_tick = 0; Darkvater@5090: /* there were some inputs this tick, don't scroll ??? */ Darkvater@5090: return; Darkvater@5090: } Darkvater@5090: glx@10776: if (_settings_client.gui.autoscroll && _game_mode != GM_MENU && !IsGeneratingWorld()) { rubidium@10455: int x = _cursor.pos.x; rubidium@10455: int y = _cursor.pos.y; rubidium@10455: Window *w = FindWindowFromPt(x, y); Darkvater@5090: if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL) return; rubidium@10455: ViewPort *vp = IsPtInWindowViewport(w, x, y); Darkvater@5090: if (vp != NULL) { Darkvater@5090: x -= vp->left; Darkvater@5090: y -= vp->top; rubidium@10455: glx@9574: /* here allows scrolling in both x and y axis */ Darkvater@5090: #define scrollspeed 3 Darkvater@5090: if (x - 15 < 0) { rubidium@10513: w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom); Darkvater@5090: } else if (15 - (vp->width - x) > 0) { rubidium@10513: w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom); Darkvater@5090: } Darkvater@5090: if (y - 15 < 0) { rubidium@10513: w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom); Darkvater@5090: } else if (15 - (vp->height - y) > 0) { rubidium@10513: w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom); Darkvater@5090: } Darkvater@5090: #undef scrollspeed Darkvater@5090: } Darkvater@5090: } Darkvater@5090: } Darkvater@5090: glx@9629: enum MouseClick { glx@9629: MC_NONE = 0, glx@9629: MC_LEFT, glx@9629: MC_RIGHT, glx@9629: MC_DOUBLE_LEFT, glx@9629: glx@9629: MAX_OFFSET_DOUBLE_CLICK = 5, ///< How much the mouse is allowed to move to call it a double click glx@9629: TIME_BETWEEN_DOUBLE_CLICK = 500, ///< Time between 2 left clicks before it becoming a double click, in ms glx@9629: }; glx@9629: glx@10294: extern void UpdateTileSelection(); glx@10294: extern bool VpHandlePlaceSizingDrag(); glx@10294: glx@10776: static void ScrollMainViewport(int x, int y) glx@10776: { glx@10776: if (_game_mode != GM_MENU) { glx@10776: Window *w = FindWindowById(WC_MAIN_WINDOW, 0); glx@10776: assert(w); glx@10776: glx@10776: w->viewport->dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom); glx@10776: w->viewport->dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom); glx@10776: } glx@10776: } glx@10776: glx@10776: /** glx@10776: * Describes all the different arrow key combinations the game allows glx@10776: * when it is in scrolling mode. glx@10776: * The real arrow keys are bitwise numbered as glx@10776: * 1 = left glx@10776: * 2 = up glx@10776: * 4 = right glx@10776: * 8 = down glx@10776: */ glx@10776: static const int8 scrollamt[16][2] = { glx@10776: { 0, 0}, ///< no key specified glx@10776: {-2, 0}, ///< 1 : left glx@10776: { 0, -2}, ///< 2 : up glx@10776: {-2, -1}, ///< 3 : left + up glx@10776: { 2, 0}, ///< 4 : right glx@10776: { 0, 0}, ///< 5 : left + right = nothing glx@10776: { 2, -1}, ///< 6 : right + up glx@10776: { 0, -2}, ///< 7 : right + left + up = up glx@10776: { 0 ,2}, ///< 8 : down glx@10776: {-2 ,1}, ///< 9 : down + left glx@10776: { 0, 0}, ///< 10 : down + up = nothing glx@10776: {-2, 0}, ///< 11 : left + up + down = left glx@10776: { 2, 1}, ///< 12 : down + right glx@10776: { 0, 2}, ///< 13 : left + right + down = down glx@10776: { 2, 0}, ///< 14 : right + up + down = right glx@10776: { 0, 0}, ///< 15 : left + up + right + down = nothing glx@10776: }; glx@10776: glx@10829: static void HandleKeyScrolling() glx@10776: { glx@10776: if (_dirkeys && !_no_scroll) { glx@10776: int factor = _shift_pressed ? 50 : 10; glx@10776: ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor); glx@10776: } glx@10776: } glx@10776: glx@9629: void MouseLoop(MouseClick click, int mousewheel) truelight@0: { truelight@0: DecreaseWindowCounters(); truelight@0: HandlePlacePresize(); truelight@0: UpdateTileSelection(); glx@10829: tron@2639: if (!VpHandlePlaceSizingDrag()) return; tron@2639: if (!HandleDragDrop()) return; tron@2639: if (!HandleWindowDragging()) return; tron@2639: if (!HandleScrollbarScrolling()) return; tron@2639: if (!HandleViewportScroll()) return; tron@2639: if (!HandleMouseOver()) return; truelight@543: glx@10776: bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0); glx@9629: if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return; truelight@0: rubidium@10455: int x = _cursor.pos.x; rubidium@10455: int y = _cursor.pos.y; rubidium@10455: Window *w = FindWindowFromPt(x, y); tron@2639: if (w == NULL) return; rubidium@10455: Darkvater@5667: if (!MaybeBringWindowToFront(w)) return; rubidium@10455: ViewPort *vp = IsPtInWindowViewport(w, x, y); truelight@0: truelight@4337: /* Don't allow any action in a viewport if either in menu of in generating world */ truelight@4337: if (vp != NULL && (_game_mode == GM_MENU || IsGeneratingWorld())) return; truelight@158: truelight@4337: if (mousewheel != 0) { glx@10776: if (_settings_client.gui.scrollwheel_scrolling == 0) { glx@10645: /* Send mousewheel event to window */ rubidium@10513: w->OnMouseWheel(mousewheel); bjarni@6622: } truelight@4337: truelight@4337: /* Dispatch a MouseWheelEvent for widgets if it is not a viewport */ truelight@4337: if (vp == NULL) DispatchMouseWheelEvent(w, GetWidgetFromPos(w, x - w->left, y - w->top), mousewheel); truelight@4337: } truelight@4337: truelight@4337: if (vp != NULL) { glx@9629: if (scrollwheel_scrolling) click = MC_RIGHT; // we are using the scrollwheel in a viewport, so we emulate right mouse button truelight@4337: switch (click) { glx@9629: case MC_DOUBLE_LEFT: glx@9629: case MC_LEFT: Darkvater@5568: DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite); rubidium@9722: if (_thd.place_mode != VHM_NONE && glx@9574: /* query button and place sign button work in pause mode */ truelight@4337: _cursor.sprite != SPR_CURSOR_QUERY && truelight@4337: _cursor.sprite != SPR_CURSOR_SIGN && truelight@6557: _pause_game != 0 && truelight@4337: !_cheats.build_in_pause.value) { truelight@4337: return; truelight@4337: } truelight@4337: rubidium@9722: if (_thd.place_mode == VHM_NONE) { truelight@4337: HandleViewportClicked(vp, x, y); truelight@4337: } else { truelight@4337: PlaceObject(); truelight@4337: } truelight@4337: break; truelight@4337: glx@9629: case MC_RIGHT: truelight@4337: if (!(w->flags4 & WF_DISABLE_VP_SCROLL)) { truelight@4337: _scrolling_viewport = true; truelight@4337: _cursor.fix_at = true; truelight@4337: } truelight@4337: break; glx@9629: glx@9629: default: glx@9629: break; truelight@0: } truelight@0: } else { tron@2631: switch (click) { rubidium@10513: case MC_DOUBLE_LEFT: rubidium@10513: DispatchLeftClickEvent(w, x - w->left, y - w->top, true); rubidium@10513: if (_mouseover_last_w == NULL) break; // The window got removed. rubidium@10513: /* fallthough, and also give a single-click for backwards compatibility */ rubidium@10513: case MC_LEFT: rubidium@10513: DispatchLeftClickEvent(w, x - w->left, y - w->top, false); rubidium@10513: break; rubidium@10513: bjarni@6616: default: bjarni@6616: if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break; bjarni@6616: /* We try to use the scrollwheel to scroll since we didn't touch any of the buttons. bjarni@6616: * Simulate a right button click so we can get started. */ bjarni@6616: /* fallthough */ glx@9629: case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break; tron@2631: } truelight@0: } truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Handle a mouse event from the video driver rubidium@10142: */ rubidium@6573: void HandleMouseEvents() pasky@1570: { glx@9629: static int double_click_time = 0; glx@9629: static int double_click_x = 0; glx@9629: static int double_click_y = 0; pasky@1570: truelight@4300: /* truelight@4300: * During the generation of the world, there might be truelight@4300: * another thread that is currently building for example truelight@4300: * a road. To not interfere with those tasks, we should truelight@4300: * NOT change the _current_player here. truelight@4300: * truelight@4300: * This is not necessary either, as the only events that truelight@4300: * can be handled are the 'close application' events truelight@4300: */ truelight@4300: if (!IsGeneratingWorld()) _current_player = _local_player; pasky@1570: glx@9574: /* Mouse event? */ rubidium@10455: MouseClick click = MC_NONE; pasky@1570: if (_left_button_down && !_left_button_clicked) { glx@9629: click = MC_LEFT; glx@9629: if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK && glx@9629: double_click_x != 0 && abs(_cursor.pos.x - double_click_x) < MAX_OFFSET_DOUBLE_CLICK && glx@9629: double_click_y != 0 && abs(_cursor.pos.y - double_click_y) < MAX_OFFSET_DOUBLE_CLICK) { glx@9629: click = MC_DOUBLE_LEFT; glx@9629: } glx@9629: double_click_time = _realtime_tick; glx@9629: double_click_x = _cursor.pos.x; glx@9629: double_click_y = _cursor.pos.y; pasky@1570: _left_button_clicked = true; Darkvater@5090: _input_events_this_tick++; pasky@1570: } else if (_right_button_clicked) { pasky@1570: _right_button_clicked = false; glx@9629: click = MC_RIGHT; Darkvater@5090: _input_events_this_tick++; pasky@1570: } pasky@1570: rubidium@10455: int mousewheel = 0; pasky@1570: if (_cursor.wheel) { pasky@1570: mousewheel = _cursor.wheel; pasky@1570: _cursor.wheel = 0; Darkvater@5090: _input_events_this_tick++; pasky@1570: } pasky@1570: pasky@1570: MouseLoop(click, mousewheel); pasky@1570: } pasky@1570: rubidium@10142: /** rubidium@10142: * Regular call from the global game loop rubidium@10142: */ rubidium@6573: void InputLoop() Darkvater@5090: { glx@10955: HandleKeyScrolling(); Darkvater@5090: HandleMouseEvents(); Darkvater@5090: HandleAutoscroll(); Darkvater@5090: } Darkvater@5090: rubidium@10142: /** rubidium@10142: * Update the continuously changing contents of the windows, such as the viewports rubidium@10142: */ rubidium@6573: void UpdateWindows() truelight@0: { Darkvater@5124: Window* const *wz; Darkvater@5089: static int we4_timer = 0; Darkvater@5089: int t = we4_timer + 1; truelight@0: tron@2639: if (t >= 100) { Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { rubidium@10513: (*--wz)->OnHundredthTick(); truelight@0: } truelight@0: t = 0; truelight@0: } Darkvater@5089: we4_timer = t; truelight@0: Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: Window *w = *--wz; truelight@0: if (w->flags4 & WF_WHITE_BORDER_MASK) { truelight@0: w->flags4 -= WF_WHITE_BORDER_ONE; Darkvater@5120: rubidium@10455: if (!(w->flags4 & WF_WHITE_BORDER_MASK)) w->SetDirty(); truelight@0: } truelight@0: } truelight@0: truelight@0: DrawDirtyBlocks(); truelight@0: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: if ((*wz)->viewport != NULL) UpdateViewportPosition(*wz); truelight@0: } rubidium@9701: DrawChatMessage(); glx@9574: /* Redraw mouse cursor in case it was hidden */ truelight@0: DrawMouseCursor(); truelight@158: } truelight@0: rubidium@10142: /** glx@10294: * Mark window as dirty (in need of repainting) glx@10294: * @param cls Window class glx@10294: * @param number Window number in that class rubidium@10142: */ tron@2788: void InvalidateWindow(WindowClass cls, WindowNumber number) truelight@0: { Darkvater@5137: Window* const *wz; truelight@0: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; rubidium@10455: if (w->window_class == cls && w->window_number == number) w->SetDirty(); truelight@0: } truelight@0: } truelight@0: glx@10294: /** rubidium@10142: * Mark a particular widget in a particular window as dirty (in need of repainting) rubidium@10142: * @param cls Window class rubidium@10142: * @param number Window number in that class rubidium@10142: * @param widget_index Index number of the widget that needs repainting rubidium@10142: */ tron@2788: void InvalidateWindowWidget(WindowClass cls, WindowNumber number, byte widget_index) truelight@0: { Darkvater@5137: Window* const *wz; truelight@0: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; tron@2639: if (w->window_class == cls && w->window_number == number) { rubidium@9723: w->InvalidateWidget(widget_index); truelight@0: } truelight@0: } truelight@0: } truelight@0: glx@10294: /** rubidium@10142: * Mark all windows of a particular class as dirty (in need of repainting) rubidium@10142: * @param cls Window class rubidium@10142: */ tron@2788: void InvalidateWindowClasses(WindowClass cls) truelight@0: { Darkvater@5137: Window* const *wz; tron@2639: Darkvater@5124: FOR_ALL_WINDOWS(wz) { rubidium@10455: if ((*wz)->window_class == cls) (*wz)->SetDirty(); truelight@0: } truelight@0: } truelight@0: rubidium@10142: /** rubidium@10142: * Mark window data as invalid (in need of re-computing) rubidium@10142: * @param w Window with invalid data rubidium@10142: */ rubidium@10513: void InvalidateThisWindowData(Window *w, int data) bjarni@4766: { rubidium@10513: w->OnInvalidateData(data); rubidium@10455: w->SetDirty(); bjarni@4766: } bjarni@4766: rubidium@10142: /** glx@10294: * Mark window data of the window of a given class and specific window number as invalid (in need of re-computing) rubidium@10142: * @param cls Window class rubidium@10142: * @param number Window number within the class rubidium@10142: */ rubidium@10513: void InvalidateWindowData(WindowClass cls, WindowNumber number, int data) bjarni@4739: { Darkvater@5124: Window* const *wz; bjarni@4739: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; rubidium@10513: if (w->window_class == cls && w->window_number == number) InvalidateThisWindowData(w, data); bjarni@4766: } bjarni@4766: } bjarni@4766: rubidium@10142: /** rubidium@10142: * Mark window data of all windows of a given class as invalid (in need of re-computing) rubidium@10142: * @param cls Window class rubidium@10142: */ rubidium@10513: void InvalidateWindowClassesData(WindowClass cls, int data) bjarni@4766: { Darkvater@5124: Window* const *wz; bjarni@4766: Darkvater@5124: FOR_ALL_WINDOWS(wz) { rubidium@10513: if ((*wz)->window_class == cls) InvalidateThisWindowData(*wz, data); bjarni@4739: } bjarni@4739: } truelight@0: rubidium@10142: /** rubidium@10142: * Dispatch WE_TICK event over all windows rubidium@10142: */ rubidium@6573: void CallWindowTickEvent() truelight@0: { glx@10776: if (_scroller_click_timeout > 3) { glx@10776: _scroller_click_timeout -= 3; glx@10776: } else { glx@10776: _scroller_click_timeout = 0; glx@10776: } glx@10776: glx@10294: for (Window * const *wz = _last_z_window; wz != _z_windows;) { rubidium@10513: (*--wz)->OnTick(); truelight@0: } truelight@0: } truelight@0: glx@10294: /** glx@10294: * Try to delete a non-vital window. glx@10294: * Non-vital windows are windows other than the game selection, main toolbar, glx@10294: * status bar, toolbar menu, and tooltip windows. Stickied windows are also glx@10294: * considered vital. glx@10294: */ rubidium@6573: void DeleteNonVitalWindows() truelight@0: { Darkvater@5124: Window* const *wz; tron@2639: Darkvater@5121: restart_search: Darkvater@5121: /* When we find the window to delete, we need to restart the search Darkvater@5124: * as deleting this window could cascade in deleting (many) others Darkvater@5124: * anywhere in the z-array */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; truelight@0: if (w->window_class != WC_MAIN_WINDOW && truelight@0: w->window_class != WC_SELECT_GAME && truelight@0: w->window_class != WC_MAIN_TOOLBAR && truelight@0: w->window_class != WC_STATUS_BAR && truelight@0: w->window_class != WC_TOOLBAR_MENU && darkvater@682: w->window_class != WC_TOOLTIPS && darkvater@682: (w->flags4 & WF_STICKY) == 0) { // do not delete windows which are 'pinned' Darkvater@5124: rubidium@10455: delete w; Darkvater@5121: goto restart_search; truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@9599: /** It is possible that a stickied window gets to a position where the darkvater@763: * 'close' button is outside the gaming area. You cannot close it then; except darkvater@763: * with this function. It closes all windows calling the standard function, darkvater@763: * then, does a little hacked loop of closing all stickied windows. Note darkvater@763: * that standard windows (status bar, etc.) are not stickied, so these aren't affected */ rubidium@6573: void DeleteAllNonVitalWindows() darkvater@763: { Darkvater@5124: Window* const *wz; tron@2639: Darkvater@5121: /* Delete every window except for stickied ones, then sticky ones as well */ darkvater@763: DeleteNonVitalWindows(); Darkvater@5124: Darkvater@5121: restart_search: Darkvater@5121: /* When we find the window to delete, we need to restart the search Darkvater@5124: * as deleting this window could cascade in deleting (many) others Darkvater@5124: * anywhere in the z-array */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: if ((*wz)->flags4 & WF_STICKY) { rubidium@10455: delete *wz; Darkvater@5121: goto restart_search; tron@4077: } darkvater@763: } darkvater@763: } darkvater@763: rubidium@9599: /** Delete all always on-top windows to get an empty screen */ rubidium@6573: void HideVitalWindows() darkvater@983: { rubidium@6158: DeleteWindowById(WC_TOOLBAR_MENU, 0); darkvater@983: DeleteWindowById(WC_MAIN_TOOLBAR, 0); darkvater@983: DeleteWindowById(WC_STATUS_BAR, 0); darkvater@983: } darkvater@983: rubidium@10142: /** rubidium@10142: * (Re)position main toolbar window at the screen rubidium@10142: * @param w Window structure of the main toolbar window, may also be \c NULL rubidium@10142: * @return X coordinate of left edge of the repositioned toolbar window rubidium@10142: */ truelight@158: int PositionMainToolbar(Window *w) darkvater@68: { Darkvater@5568: DEBUG(misc, 5, "Repositioning Main Toolbar..."); darkvater@68: tron@4077: if (w == NULL || w->window_class != WC_MAIN_TOOLBAR) { darkvater@68: w = FindWindowById(WC_MAIN_TOOLBAR, 0); tron@4077: } darkvater@68: glx@10776: switch (_settings_client.gui.toolbar_pos) { Darkvater@5071: case 1: w->left = (_screen.width - w->width) / 2; break; tron@2026: case 2: w->left = _screen.width - w->width; break; tron@2026: default: w->left = 0; darkvater@68: } darkvater@68: SetDirtyBlocks(0, 0, _screen.width, w->height); // invalidate the whole top part darkvater@68: return w->left; darkvater@68: } darkvater@68: glx@10776: void SetVScrollCount(Window *w, int num) glx@10776: { glx@10776: w->vscroll.count = num; glx@10776: num -= w->vscroll.cap; glx@10776: if (num < 0) num = 0; glx@10776: if (num < w->vscroll.pos) w->vscroll.pos = num; glx@10776: } glx@10776: glx@10776: void SetVScroll2Count(Window *w, int num) glx@10776: { glx@10776: w->vscroll2.count = num; glx@10776: num -= w->vscroll2.cap; glx@10776: if (num < 0) num = 0; glx@10776: if (num < w->vscroll2.pos) w->vscroll2.pos = num; glx@10776: } glx@10776: glx@10776: void SetHScrollCount(Window *w, int num) glx@10776: { glx@10776: w->hscroll.count = num; glx@10776: num -= w->hscroll.cap; glx@10776: if (num < 0) num = 0; glx@10776: if (num < w->hscroll.pos) w->hscroll.pos = num; glx@10776: } glx@10776: rubidium@10142: /** rubidium@10142: * Relocate all windows to fit the new size of the game application screen rubidium@10142: * @param neww New width of the game application screen rubidium@10142: * @param newh New height of the game appliction screen rubidium@10142: */ truelight@0: void RelocateAllWindows(int neww, int newh) truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; truelight@0: int left, top; truelight@158: truelight@0: if (w->window_class == WC_MAIN_WINDOW) { truelight@0: ViewPort *vp = w->viewport; truelight@0: vp->width = w->width = neww; truelight@0: vp->height = w->height = newh; glx@9624: vp->virtual_width = ScaleByZoom(neww, vp->zoom); glx@9624: vp->virtual_height = ScaleByZoom(newh, vp->zoom); truelight@0: continue; // don't modify top,left darkvater@152: } darkvater@152: Darkvater@5126: /* XXX - this probably needs something more sane. For example specying Darkvater@5126: * in a 'backup'-desc that the window should always be centred. */ tron@2989: switch (w->window_class) { tron@2989: case WC_MAIN_TOOLBAR: rubidium@9694: if (neww - w->width != 0) { rubidium@9694: ResizeWindow(w, min(neww, 640) - w->width, 0); rubidium@9694: rubidium@10513: Point size; rubidium@10513: Point delta; rubidium@10513: size.x = w->width; rubidium@10513: size.y = w->height; rubidium@10513: delta.x = neww - w->width; rubidium@10513: delta.y = 0; rubidium@10513: w->OnResize(size, delta); rubidium@9694: } rubidium@9694: tron@2989: top = w->top; tron@2989: left = PositionMainToolbar(w); // changes toolbar orientation tron@2989: break; tron@2989: tron@2989: case WC_SELECT_GAME: tron@2989: case WC_GAME_OPTIONS: tron@2989: case WC_NETWORK_WINDOW: tron@2989: top = (newh - w->height) >> 1; tron@2989: left = (neww - w->width) >> 1; tron@2989: break; tron@2989: tron@2989: case WC_NEWS_WINDOW: tron@2989: top = newh - w->height; tron@2989: left = (neww - w->width) >> 1; tron@2989: break; tron@2989: tron@2989: case WC_STATUS_BAR: rubidium@9722: ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0); tron@2989: top = newh - w->height; tron@2989: left = (neww - w->width) >> 1; tron@2989: break; tron@2989: tron@2989: case WC_SEND_NETWORK_MSG: rubidium@9722: ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0); tron@2989: top = (newh - 26); // 26 = height of status bar + height of chat bar tron@2989: left = (neww - w->width) >> 1; tron@2989: break; tron@2989: Darkvater@5126: case WC_CONSOLE: Darkvater@5143: IConsoleResize(w); Darkvater@5137: continue; Darkvater@5126: rubidium@9826: default: { tron@2989: left = w->left; tron@2989: if (left + (w->width >> 1) >= neww) left = neww - w->width; rubidium@9723: if (left < 0) left = 0; rubidium@9723: tron@2989: top = w->top; tron@2989: if (top + (w->height >> 1) >= newh) top = newh - w->height; rubidium@9826: rubidium@9826: const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@9826: if (wt != NULL) { rubidium@10249: if (top < wt->height && wt->left < (w->left + w->width) && (wt->left + wt->width) > w->left) top = wt->height; rubidium@9826: if (top >= newh) top = newh - 1; rubidium@9826: } else { rubidium@9826: if (top < 0) top = 0; rubidium@9826: } rubidium@9826: } break; truelight@0: } truelight@0: tron@2639: if (w->viewport != NULL) { truelight@0: w->viewport->left += left - w->left; truelight@0: w->viewport->top += top - w->top; truelight@0: } truelight@0: truelight@0: w->left = left; truelight@0: w->top = top; truelight@0: } truelight@0: } glx@10645: glx@10645: /** Destructor of the base class PickerWindowBase glx@10645: * Main utility is to stop the base Window destructor from triggering glx@10645: * a free while the child will already be free, in this case by the ResetObjectToPlace(). glx@10645: */ glx@10645: PickerWindowBase::~PickerWindowBase() glx@10645: { glx@10645: this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child glx@10645: ResetObjectToPlace(); glx@10645: }