tron@2186: /* $Id$ */ tron@2186: rubidium@10429: /** @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@8750: #include "player_func.h" rubidium@8720: #include "gfx_func.h" rubidium@10684: #include "console_func.h" rubidium@10684: #include "console_gui.h" rubidium@8720: #include "viewport_func.h" tron@2159: #include "variables.h" truelight@4300: #include "genworld.h" truelight@7433: #include "blitter/factory.hpp" rubidium@8602: #include "window_gui.h" rubidium@8619: #include "zoom_func.h" rubidium@8626: #include "core/alloc_func.hpp" rubidium@8635: #include "map_func.h" rubidium@8640: #include "vehicle_base.h" rubidium@8766: #include "settings_type.h" rubidium@10225: #include "cheat_func.h" rubidium@10435: #include "window_func.h" rubidium@10445: #include "tilehighlight_func.h" truelight@0: rubidium@8760: #include "table/sprites.h" rubidium@8760: rubidium@10163: static Point _drag_delta; ///< delta between mouse cursor and upper left corner of dragged window rubidium@10163: static Window *_mouseover_last_w = NULL; ///< Window of the last MOUSEOVER event tron@350: rubidium@10083: /** rubidium@10083: * List of windows opened at the screen. rubidium@10083: * Uppermost window is at _z_windows[_last_z_window - 1], rubidium@10083: * bottom window is at _z_windows[0] rubidium@10083: */ rubidium@10164: 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: rubidium@10772: byte _no_scroll; rubidium@8764: Point _cursorpos_drag_start; rubidium@8764: rubidium@8764: int _scrollbar_start_pos; rubidium@8764: int _scrollbar_size; rubidium@8764: byte _scroller_click_timeout; rubidium@8764: rubidium@8764: bool _scrolling_scrollbar; rubidium@8764: bool _scrolling_viewport; rubidium@8764: rubidium@8764: byte _special_mouse_mode; rubidium@8764: rubidium@8764: belugas@8489: void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...) belugas@8489: { belugas@8489: va_list wdg_list; belugas@8489: belugas@8489: va_start(wdg_list, widgets); belugas@8489: belugas@8489: while (widgets != WIDGET_LIST_END) { belugas@8489: SetWidgetDisabledState(widgets, disab_stat); belugas@8489: widgets = va_arg(wdg_list, int); belugas@8489: } belugas@8489: belugas@8489: va_end(wdg_list); belugas@8489: } belugas@8489: belugas@8489: void CDECL Window::SetWidgetsHiddenState(bool hidden_stat, int widgets, ...) belugas@8489: { belugas@8489: va_list wdg_list; belugas@8489: belugas@8489: va_start(wdg_list, widgets); belugas@8489: belugas@8489: while (widgets != WIDGET_LIST_END) { belugas@8489: SetWidgetHiddenState(widgets, hidden_stat); belugas@8489: widgets = va_arg(wdg_list, int); belugas@8489: } belugas@8489: belugas@8489: va_end(wdg_list); belugas@8489: } belugas@8489: belugas@8489: void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...) belugas@8489: { belugas@8489: va_list wdg_list; belugas@8489: belugas@8489: va_start(wdg_list, widgets); belugas@8489: belugas@8489: while (widgets != WIDGET_LIST_END) { belugas@8489: SetWidgetLoweredState(widgets, lowered_stat); belugas@8489: widgets = va_arg(wdg_list, int); belugas@8489: } belugas@8489: belugas@8489: va_end(wdg_list); belugas@8489: } belugas@8489: belugas@8489: void Window::RaiseButtons() belugas@8489: { rubidium@10442: for (uint i = 0; i < this->widget_count; i++) { belugas@8528: if (this->IsWidgetLowered(i)) { belugas@8528: this->RaiseWidget(i); belugas@8528: this->InvalidateWidget(i); belugas@8489: } belugas@8489: } belugas@8489: } belugas@8489: glx@8522: void Window::InvalidateWidget(byte widget_index) const belugas@8489: { belugas@8489: const Widget *wi = &this->widget[widget_index]; belugas@8489: belugas@8489: /* Don't redraw the window if the widget is invisible or of no-type */ belugas@8489: if (wi->type == WWT_EMPTY || IsWidgetHidden(widget_index)) return; belugas@8489: belugas@8489: SetDirtyBlocks(this->left + wi->left, this->top + wi->top, this->left + wi->right + 1, this->top + wi->bottom + 1); belugas@8489: } belugas@8489: belugas@8531: void Window::HandleButtonClick(byte widget) belugas@8531: { belugas@8531: this->LowerWidget(widget); belugas@8531: this->flags4 |= 5 << WF_TIMEOUT_SHL; belugas@8531: this->InvalidateWidget(widget); belugas@8531: } belugas@8531: Darkvater@5124: static void StartWindowDrag(Window *w); Darkvater@5124: static void StartWindowSizing(Window *w); tron@2817: rubidium@10083: /** rubidium@10083: * Dispatch left mouse-button (possibly double) click in window. rubidium@10083: * @param w Window to dispatch event in rubidium@10083: * @param x X coordinate of the click rubidium@10083: * @param y Y coordinate of the click rubidium@10083: * @param double_click Was it a double click? rubidium@10083: */ truelight@7505: static void DispatchLeftClickEvent(Window *w, int x, int y, bool double_click) tron@2596: { rubidium@10486: int widget = 0; truelight@0: if (w->desc_flags & WDF_DEF_WIDGET) { rubidium@10486: widget = GetWidgetFromPos(w, x, y); rubidium@10486: 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@10486: if (w->IsWidgetDisabled(widget)) return; darkvater@222: rubidium@10486: 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@10486: 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@10486: ScrollbarClickHandler(w, wi, x, y); truelight@0: } truelight@0: truelight@0: if (w->desc_flags & WDF_STD_BTN) { rubidium@10486: if (widget == 0) { /* 'X' */ rubidium@10433: delete w; celestar@984: return; tron@1109: } tron@1109: rubidium@10486: 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@10486: 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@10486: w->InvalidateWidget(widget); darkvater@1112: return; darkvater@682: } truelight@0: } darkvater@1038: rubidium@10486: Point pt = { x, y }; rubidium@10486: rubidium@10486: if (double_click) { rubidium@10486: w->OnDoubleClick(pt, widget); rubidium@10486: } else { rubidium@10486: w->OnClick(pt, widget); rubidium@10486: } truelight@0: } truelight@0: rubidium@10083: /** rubidium@10083: * Dispatch right mouse-button click in window. rubidium@10083: * @param w Window to dispatch event in rubidium@10083: * @param x X coordinate of the click rubidium@10083: * @param y Y coordinate of the click rubidium@10083: */ belugas@4171: static void DispatchRightClickEvent(Window *w, int x, int y) tron@2596: { rubidium@10486: int widget = 0; truelight@0: truelight@0: /* default tooltips handler? */ truelight@0: if (w->desc_flags & WDF_STD_TOOLTIPS) { rubidium@10486: widget = GetWidgetFromPos(w, x, y); rubidium@10486: if (widget < 0) return; // exit if clicked outside of widgets truelight@0: rubidium@10486: if (w->widget[widget].tooltips != 0) { rubidium@10486: GuiShowTooltips(w->widget[widget].tooltips); truelight@0: return; truelight@0: } truelight@0: } truelight@0: rubidium@10486: Point pt = { x, y }; rubidium@10486: w->OnRightClick(pt, widget); truelight@0: } truelight@0: rubidium@10083: /** rubidium@10083: * Dispatch the mousewheel-action to the window. rubidium@10083: * The window will scroll any compatible scrollbars if the mouse is pointed over the bar or its contents rubidium@10083: * @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@10442: const Widget *wi1 = &w->widget[widget]; rubidium@10442: 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@10442: 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) { skidd13@8418: int pos = Clamp(sb->pos + wheel, 0, sb->count - sb->cap); darkvater@982: if (pos != sb->pos) { darkvater@982: sb->pos = pos; rubidium@10434: w->SetDirty(); darkvater@982: } truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@10083: /** rubidium@10083: * Generate repaint events for the visible part of window *wz within the rectangle. rubidium@10083: * rubidium@10083: * The function goes recursively upwards in the window stack, and splits the rectangle rubidium@10083: * into multiple pieces at the window edges, so obscured parts are not redrawn. rubidium@10083: * rubidium@10083: * @param wz Pointer into window stack, pointing at the window that needs to be repainted rubidium@10083: * @param left Left edge of the rectangle that should be repainted rubidium@10083: * @param top Top edge of the rectangle that should be repainted rubidium@10083: * @param right Right edge of the rectangle that should be repainted rubidium@10083: * @param bottom Bottom edge of the rectangle that should be repainted rubidium@10083: */ 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@10083: /* v and rectangle intersect with eeach other */ rubidium@10083: int x; rubidium@10083: rubidium@6987: 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@6987: 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@6987: 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@6987: 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: rubidium@10255: /* Setup blitter, and dispatch a repaint event to window *wz */ rubidium@10161: DrawPixelInfo *dp = _cur_dpi; rubidium@10161: dp->width = right - left; rubidium@10161: dp->height = bottom - top; rubidium@10161: dp->left = left - (*wz)->left; rubidium@10161: dp->top = top - (*wz)->top; rubidium@10161: dp->pitch = _screen.pitch; rubidium@10161: dp->dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top); rubidium@10161: dp->zoom = ZOOM_LVL_NORMAL; rubidium@10486: (*wz)->OnPaint(); rubidium@10161: } rubidium@10161: rubidium@10161: /** rubidium@10161: * From a rectangle that needs redrawing, find the windows that intersect with the rectangle. rubidium@10161: * These windows should be re-painted. rubidium@10161: * @param left Left edge of the rectangle that should be repainted rubidium@10161: * @param top Top edge of the rectangle that should be repainted rubidium@10161: * @param right Right edge of the rectangle that should be repainted rubidium@10161: * @param bottom Bottom edge of the rectangle that should be repainted rubidium@10161: */ rubidium@10161: void DrawOverlappedWindowForAll(int left, int top, int right, int bottom) rubidium@10161: { rubidium@10161: Window* const *wz; rubidium@10161: DrawPixelInfo bk; rubidium@10161: _cur_dpi = &bk; rubidium@10161: rubidium@10161: FOR_ALL_WINDOWS(wz) { rubidium@10161: const Window *w = *wz; rubidium@10161: if (right > w->left && rubidium@10161: bottom > w->top && rubidium@10161: left < w->left + w->width && rubidium@10161: top < w->top + w->height) { rubidium@10255: /* Window w intersects with the rectangle => needs repaint */ rubidium@10161: DrawOverlappedWindow(wz, left, top, right, bottom); rubidium@10161: } truelight@0: } truelight@0: } truelight@0: rubidium@10083: /** rubidium@10083: * Mark entire window as dirty (in need of re-paint) rubidium@10083: * @param w Window to redraw rubidium@10083: * @ingroup dirty rubidium@10083: */ rubidium@10433: void Window::SetDirty() const rubidium@10433: { rubidium@10433: SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height); rubidium@10433: } rubidium@10433: rubidium@10433: /** rubidium@10433: * Mark entire window as dirty (in need of re-paint) rubidium@10433: * @param w Window to redraw rubidium@10433: * @ingroup dirty rubidium@10433: */ belugas@4171: void SetWindowDirty(const Window *w) truelight@0: { rubidium@10433: if (w != NULL) w->SetDirty(); truelight@0: } truelight@0: Darkvater@5666: /** Find the Window whose parent pointer points to this window belugas@6939: * @param w parent Window to find child of belugas@6939: * @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 belugas@6939: * or the behaviour is undefined but function should never fail belugas@6939: * @param w window to query Z Position rubidium@10083: * @return Pointer into the window-list at the position of \a w rubidium@10083: */ Darkvater@5124: Window **FindWindowZPosition(const Window *w) Darkvater@5124: { Darkvater@5124: Window **wz; Darkvater@5124: rubidium@10083: 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@10083: /** rubidium@10255: * Remove window and all its child windows from the window stack. rubidium@10083: */ rubidium@10433: Window::~Window() truelight@0: { tron@4077: if (_thd.place_mode != VHM_NONE && rubidium@10433: _thd.window_class == this->window_class && rubidium@10433: _thd.window_number == this->window_number) { truelight@0: ResetObjectToPlace(); truelight@0: } truelight@0: rubidium@10400: /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */ rubidium@10433: if (_mouseover_last_w == this) _mouseover_last_w = NULL; rubidium@10400: rubidium@10400: /* Find the window in the z-array, and effectively remove it rubidium@10400: * by moving all windows after it one to the left. This must be rubidium@10400: * done before removing the child so we cannot cause recursion rubidium@10400: * between the deletion of the parent and the child. */ rubidium@10433: Window **wz = FindWindowZPosition(this); rubidium@10400: if (wz == NULL) return; rubidium@10400: memmove(wz, wz + 1, (byte*)_last_z_window - (byte*)wz); rubidium@10400: _last_z_window--; rubidium@10400: rubidium@10555: /* Delete all children a window might have in a head-recursive manner */ rubidium@10555: Window *child = FindChildWindow(this); rubidium@10555: while (child != NULL) { rubidium@10555: delete child; rubidium@10555: child = FindChildWindow(this); rubidium@10555: } truelight@0: rubidium@10433: if (this->viewport != NULL) DeleteWindowViewport(this); truelight@867: rubidium@10433: this->SetDirty(); rubidium@10433: free(this->widget); truelight@0: } truelight@0: rubidium@10083: /** rubidium@10083: * Find a window by its class and window number rubidium@10083: * @param cls Window class rubidium@10083: * @param number Number of the window within the window class rubidium@10083: * @return Pointer to the found window, or \c NULL if not available rubidium@10083: */ 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@10083: /** rubidium@10083: * Delete a window by its class and window number (if it is open). rubidium@10083: * @param cls Window class rubidium@10083: * @param number Number of the window within the window class rubidium@10083: */ truelight@0: void DeleteWindowById(WindowClass cls, WindowNumber number) truelight@0: { rubidium@10433: delete FindWindowById(cls, number); truelight@158: } truelight@0: rubidium@10083: /** rubidium@10083: * Delete all windows of a given class rubidium@10083: * @param cls Window class of windows to delete rubidium@10083: */ 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@10433: 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@10433: 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@10442: if (w->caption_color != old_player) continue; Darkvater@5120: rubidium@10442: switch (w->window_class) { rubidium@10442: case WC_PLAYER_COLOR: rubidium@10442: case WC_FINANCES: rubidium@10442: case WC_STATION_LIST: rubidium@10442: case WC_TRAINS_LIST: rubidium@10442: case WC_ROADVEH_LIST: rubidium@10442: case WC_SHIPS_LIST: rubidium@10442: case WC_AIRCRAFT_LIST: rubidium@10442: case WC_BUY_COMPANY: rubidium@10442: case WC_COMPANY: rubidium@10442: continue; rubidium@10442: rubidium@10442: default: rubidium@10442: w->caption_color = new_player; rubidium@10442: break; rubidium@10442: } 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 belugas@6939: * gets a white border for a brief period of time to visualize its "activation" belugas@6939: * @param cls WindowClass of the window to activate belugas@6939: * @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@10434: 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@10442: switch (w->window_class) { rubidium@10442: case WC_MAIN_TOOLBAR: rubidium@10442: case WC_STATUS_BAR: rubidium@10442: case WC_NEWS_WINDOW: rubidium@10442: case WC_SEND_NETWORK_MSG: rubidium@10442: return true; rubidium@10442: rubidium@10442: default: rubidium@10442: return false; rubidium@10442: } 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@10442: Window *tempz = *wz; Darkvater@5124: memmove(wz, wz + 1, (byte*)vz - (byte*)wz); Darkvater@5124: *vz = tempz; truelight@0: rubidium@10434: 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: rubidium@10258: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; tron@2639: if (w->window_class != WC_MAIN_WINDOW && !IsVitalWindow(w)) return w; darkvater@763: } rubidium@10258: NOT_REACHED(); darkvater@763: } darkvater@763: rubidium@10487: /** rubidium@10487: * Assign widgets to a new window by initialising its widget pointers, and by rubidium@10487: * copying the widget array \a widget to \c w->widget to allow for resizable rubidium@10487: * windows. belugas@6939: * @param w Window on which to attach the widget array rubidium@10487: * @param widget pointer of widget array to fill the window with rubidium@10487: * rubidium@10487: * @post \c w->widget points to allocated memory and contains the copied widget array except for the terminating widget, rubidium@10487: * \c w->widget_count contains number of widgets in the allocated memory. rubidium@10487: */ rubidium@10488: static void AssignWidgetToWindow(Window *w, const Widget *widget) truelight@867: { truelight@867: if (widget != NULL) { truelight@867: uint index = 1; truelight@867: rubidium@10442: for (const Widget *wi = widget; wi->type != WWT_LAST; wi++) index++; tron@2639: rubidium@10487: 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@10461: /** rubidium@10461: * Initializes a new Window. rubidium@10461: * 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 belugas@6939: * @param x offset in pixels from the left of the screen belugas@6939: * @param y offset in pixels from the top of the screen rubidium@7837: * @param min_width minimum width in pixels of the window rubidium@7837: * @param min_height minimum height in pixels of the window belugas@6939: * @param cls see WindowClass class of the window, used for identification and grouping belugas@6939: * @param *widget see Widget pointer to the window layout and various elements belugas@6939: * @param window_number number being assigned to the new window rubidium@10525: * @return Window pointer of the newly created window rubidium@10525: */ rubidium@10498: void Window::Initialize(int x, int y, int min_width, int min_height, rubidium@10641: 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@10164: if (_last_z_window == endof(_z_windows)) { rubidium@10461: Window *w = FindDeletableWindow(); Darkvater@5124: if (w == NULL) w = ForceFindDeletableWindow(); rubidium@10433: delete w; truelight@0: } truelight@0: belugas@6928: /* Set up window properties */ rubidium@10461: this->window_class = cls; rubidium@10461: this->flags4 = WF_WHITE_BORDER_MASK; // just opened windows have a white border rubidium@10461: this->caption_color = 0xFF; rubidium@10461: this->left = x; rubidium@10461: this->top = y; rubidium@10461: this->width = min_width; rubidium@10461: this->height = min_height; rubidium@10461: AssignWidgetToWindow(this, widget); rubidium@10461: this->resize.width = min_width; rubidium@10461: this->resize.height = min_height; rubidium@10461: this->resize.step_width = 1; rubidium@10461: this->resize.step_height = 1; rubidium@10461: this->window_number = window_number; Darkvater@5124: rubidium@10461: /* Hacky way of specifying always-on-top windows. These windows are rubidium@10461: * always above other windows because they are moved below them. rubidium@10461: * status-bar is above news-window because it has been created earlier. rubidium@10461: * Also, as the chat-window is excluded from this, it will always be rubidium@10461: * the last window, thus always on top. rubidium@10461: * XXX - Yes, ugly, probably needs something like w->always_on_top flag rubidium@10461: * to implement correctly, but even then you need some kind of distinction rubidium@10461: * between on-top of chat/news and status windows, because these conflict */ rubidium@10461: Window **wz = _last_z_window; rubidium@10461: if (wz != _z_windows && this->window_class != WC_SEND_NETWORK_MSG && this->window_class != WC_HIGHSCORE && this->window_class != WC_ENDSCREEN) { rubidium@10461: if (FindWindowById(WC_MAIN_TOOLBAR, 0) != NULL) wz--; rubidium@10461: if (FindWindowById(WC_STATUS_BAR, 0) != NULL) wz--; rubidium@10461: if (FindWindowById(WC_NEWS_WINDOW, 0) != NULL) wz--; rubidium@10461: if (FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL) wz--; Darkvater@5124: rubidium@10461: assert(wz >= _z_windows); rubidium@10461: if (wz != _last_z_window) memmove(wz + 1, wz, (byte*)_last_z_window - (byte*)wz); rubidium@10461: } Darkvater@5124: rubidium@10461: *wz = this; rubidium@10461: _last_z_window++; rubidium@10498: } rubidium@7837: rubidium@10498: /** rubidium@10788: * Resize window towards the default size. rubidium@10788: * Prior to construction, a position for the new window (for its default size) rubidium@10788: * has been found with LocalGetWindowPlacement(). Initially, the window is rubidium@10788: * constructed with minimal size. Resizing the window to its default size is rubidium@10788: * done here. rubidium@10498: * @param def_width default width in pixels of the window rubidium@10498: * @param def_height default height in pixels of the window rubidium@10788: * @see Window::Window(), Window::Initialize() rubidium@10498: */ rubidium@10498: void Window::FindWindowPlacementAndResize(int def_width, int def_height) rubidium@10498: { rubidium@7849: /* Try to make windows smaller when our window is too small. rubidium@7849: * w->(width|height) is normally the same as min_(width|height), rubidium@7849: * but this way the GUIs can be made a little more dynamic; rubidium@7849: * one can use the same spec for multiple windows and those rubidium@7849: * can then determine the real minimum size of the window. */ rubidium@10461: if (this->width != def_width || this->height != def_height) { rubidium@7840: /* Think about the overlapping toolbars when determining the minimum window size */ rubidium@7840: int free_height = _screen.height; rubidium@7840: const Window *wt = FindWindowById(WC_STATUS_BAR, 0); rubidium@7840: if (wt != NULL) free_height -= wt->height; rubidium@7840: wt = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@7840: if (wt != NULL) free_height -= wt->height; rubidium@7840: rubidium@10461: int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0); rubidium@10461: int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0); rubidium@7837: rubidium@7837: /* X and Y has to go by step.. calculate it. rubidium@7837: * The cast to int is necessary else x/y are implicitly casted to rubidium@7837: * unsigned int, which won't work. */ rubidium@10461: if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width; rubidium@10461: if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height; rubidium@7837: rubidium@10461: ResizeWindow(this, enlarge_x, enlarge_y); rubidium@7838: rubidium@10486: Point size; rubidium@10486: Point diff; rubidium@10486: size.x = this->width; rubidium@10486: size.y = this->height; rubidium@10486: diff.x = enlarge_x; rubidium@10486: diff.y = enlarge_y; rubidium@10486: this->OnResize(size, diff); rubidium@7840: } rubidium@7838: rubidium@10461: int nx = this->left; rubidium@10461: int ny = this->top; truelight@7960: rubidium@10461: if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width); rubidium@7860: rubidium@7840: const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@10498: ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height); truelight@7960: nx = max(nx, 0); truelight@7960: rubidium@10461: if (this->viewport != NULL) { rubidium@10461: this->viewport->left += nx - this->left; rubidium@10461: this->viewport->top += ny - this->top; truelight@7960: } rubidium@10461: this->left = nx; rubidium@10461: this->top = ny; rubidium@7837: rubidium@10461: this->SetDirty(); truelight@0: } truelight@0: rubidium@10498: void Window::FindWindowPlacementAndResize(const WindowDesc *desc) rubidium@10498: { rubidium@10498: this->FindWindowPlacementAndResize(desc->default_width, desc->default_height); rubidium@10498: } rubidium@10498: 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 belugas@6939: * @param cls see WindowClass class of the window, used for identification and grouping belugas@6939: * @param *widget see Widget pointer to the window layout and various elements rubidium@10083: * @return Window pointer of the newly created window rubidium@10083: */ rubidium@10625: Window::Window(int x, int y, int width, int height, WindowClass cls, const Widget *widget) bjarni@4520: { rubidium@10641: this->Initialize(x, y, width, height, cls, widget, 0); bjarni@4520: } truelight@0: rubidium@10788: /** rubidium@10788: * Decide whether a given rectangle is a good place to open a completely visible new window. rubidium@10788: * The new window should be within screen borders, and not overlap with another already rubidium@10788: * existing window (except for the main window in the background). rubidium@10788: * @param left Left edge of the rectangle rubidium@10788: * @param top Top edge of the rectangle rubidium@10788: * @param width Width of the rectangle rubidium@10788: * @param height Height of the rectangle rubidium@10788: * @param pos If rectangle is good, use this parameter to return the top-left corner of the new window rubidium@10788: * @return Boolean indication that the rectangle is a good place for the new window rubidium@10788: */ peter1138@7028: static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos) truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: peter1138@7028: int right = width + left; peter1138@7028: int bottom = height + top; truelight@0: rubidium@10442: if (left < 0 || top < 22 || right > _screen.width || bottom > _screen.height) return false; truelight@0: belugas@6928: /* 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: peter1138@7028: pos.x = left; peter1138@7028: pos.y = top; truelight@0: return true; truelight@0: } truelight@0: rubidium@10788: /** rubidium@10788: * Decide whether a given rectangle is a good place to open a mostly visible new window. rubidium@10788: * The new window should be mostly within screen borders, and not overlap with another already rubidium@10788: * existing window (except for the main window in the background). rubidium@10788: * @param left Left edge of the rectangle rubidium@10788: * @param top Top edge of the rectangle rubidium@10788: * @param width Width of the rectangle rubidium@10788: * @param height Height of the rectangle rubidium@10788: * @param pos If rectangle is good, use this parameter to return the top-left corner of the new window rubidium@10788: * @return Boolean indication that the rectangle is a good place for the new window rubidium@10788: */ peter1138@7028: static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos) truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: rubidium@10788: /* Left part of the rectangle may be at most 1/4 off-screen, rubidium@10788: * right part of the rectangle may be at most 1/2 off-screen rubidium@10788: */ Darkvater@5120: if (left < -(width>>2) || left > _screen.width - (width>>1)) return false; rubidium@10788: /* 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: belugas@6928: /* 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: peter1138@7028: pos.x = left; peter1138@7028: pos.y = top; truelight@0: return true; truelight@0: } truelight@0: rubidium@10788: /** rubidium@10788: * Find a good place for opening a new window of a given width and height. rubidium@10788: * @param width Width of the new window rubidium@10788: * @param height Height of the new window rubidium@10788: * @return Top-left coordinate of the new window rubidium@10788: */ tron@1095: static Point GetAutoPlacePosition(int width, int height) tron@1095: { Darkvater@5124: Window* const *wz; truelight@0: Point pt; truelight@0: rubidium@10788: /* First attempt, try top-left of the screen */ peter1138@7028: if (IsGoodAutoPlace1(0, 24, width, height, pt)) return pt; truelight@0: rubidium@10788: /* Second attempt, try around all existing windows with a distance of 2 pixels. rubidium@10788: * The new window must be entirely on-screen, and not overlap with an existing window. rubidium@10788: * Eight starting points are tried, two at each corner. rubidium@10788: */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; tron@2639: if (w->window_class == WC_MAIN_WINDOW) continue; truelight@0: peter1138@7028: if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace1(w->left - width - 2, w->top, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace1(w->left, w->top - height - 2, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace1(w->left - width - 2, w->top + w->height - height, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2, width, height, pt)) return pt; truelight@158: } truelight@158: rubidium@10788: /* Third attempt, try around all existing windows with a distance of 2 pixels. rubidium@10788: * The new window may be partly off-screen, and must not overlap with an existing window. rubidium@10788: * Only four starting points are tried. rubidium@10788: */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; tron@2639: if (w->window_class == WC_MAIN_WINDOW) continue; truelight@0: peter1138@7028: if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace2(w->left - width - 2, w->top, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt; peter1138@7028: if (IsGoodAutoPlace2(w->left, w->top - height - 2, width, height, pt)) return pt; truelight@0: } truelight@0: rubidium@10788: /* Fourth and final attempt, put window at diagonal starting from (0, 24), try multiples rubidium@10788: * of (+5, +5) rubidium@10788: */ truelight@0: { peter1138@7028: int left = 0, top = 24; truelight@158: peter1138@7028: 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: belugas@8217: /** rubidium@10395: * Compute the position of the top-left corner of a new window that is opened. belugas@8217: * rubidium@10396: * By default position a child window at an offset of 10/10 of its parent. rubidium@10396: * With the exception of WC_BUILD_TOOLBAR (build railway/roads/ship docks/airports) rubidium@10396: * and WC_SCEN_LAND_GEN (landscaping). Whose child window has an offset of 0/36 of rubidium@10396: * its parent. So it's exactly under the parent toolbar and no buttons will be covered. rubidium@10396: * However if it falls too extremely outside window positions, reposition rubidium@10396: * it to an automatic place. rubidium@10396: * belugas@8217: * @param *desc The pointer to the WindowDesc to be created belugas@8217: * @param window_number the window number of the new window belugas@8217: * rubidium@10395: * @return Coordinate of the top-left corner of the new window belugas@8217: */ rubidium@10395: 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: belugas@8217: pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10); rubidium@7837: if (pt.x > _screen.width + 10 - desc->default_width) { rubidium@7837: pt.x = (_screen.width + 10 - desc->default_width) - 20; Darkvater@5071: } belugas@8217: 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@10396: case WDP_ALIGN_TBR: // Align the right side with the top toolbar Darkvater@5072: w = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@7837: pt.x = (w->left + w->width) - desc->default_width; rubidium@10396: break; rubidium@10396: rubidium@10396: 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@10396: rubidium@10396: case WDP_AUTO: // Find a good automatic position for the window rubidium@10395: return GetAutoPlacePosition(desc->default_width, desc->default_height); rubidium@10396: rubidium@10396: case WDP_CENTER: // Centre the window horizontally rubidium@7837: pt.x = (_screen.width - desc->default_width) / 2; Darkvater@5072: break; rubidium@10396: 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@10396: case WDP_CENTER: // Centre the window vertically rubidium@7837: pt.y = (_screen.height - desc->default_height) / 2; Darkvater@5072: break; rubidium@10396: 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@10396: 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@10395: return pt; rubidium@10395: } rubidium@10395: rubidium@10395: /** rubidium@10395: * Set the positions of a new window from a WindowDesc and open it. rubidium@10395: * rubidium@10395: * @param *desc The pointer to the WindowDesc to be created rubidium@10395: * @param window_number the window number of the new window rubidium@10395: * rubidium@10395: * @return Window pointer of the newly created window rubidium@10395: */ rubidium@10525: Window::Window(const WindowDesc *desc, WindowNumber window_number) rubidium@10395: { rubidium@10395: Point pt = LocalGetWindowPlacement(desc, window_number); rubidium@10641: this->Initialize(pt.x, pt.y, desc->minimum_width, desc->minimum_height, desc->cls, desc->widgets, window_number); rubidium@10461: 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 belugas@6939: * @param x position x to query belugas@6939: * @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: { rubidium@10258: for (Window * const *wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: Window *w = *--wz; skidd13@8450: 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@10162: /** rubidium@10162: * (re)initialize the windowing system rubidium@10162: */ rubidium@6573: void InitWindowSystem() darkvater@152: { dominik@136: IConsoleClose(); Darkvater@1474: Darkvater@5124: _last_z_window = _z_windows; rubidium@10258: _mouseover_last_w = NULL; Darkvater@1397: _no_scroll = 0; rubidium@10771: _scrolling_viewport = 0; truelight@0: } truelight@0: rubidium@10162: /** rubidium@10162: * Close down the windowing system rubidium@10162: */ rubidium@6573: void UnInitWindowSystem() Darkvater@1474: { rubidium@10442: while (_last_z_window != _z_windows) delete _z_windows[0]; Darkvater@1474: } Darkvater@1474: rubidium@10162: /** rubidium@10162: * Reset the windowing system, by means of shutting it down followed by re-initialization rubidium@10162: */ 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@10442: Window *w = *--wz; belugas@6928: /* 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@10434: w->SetDirty(); truelight@0: } rubidium@10486: w->OnMouseLoop(); truelight@0: } truelight@0: Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { rubidium@10442: Window *w = *--wz; truelight@158: rubidium@10442: if (w->flags4 & WF_TIMEOUT_MASK && !(--w->flags4 & WF_TIMEOUT_MASK)) { rubidium@10486: w->OnTimeout(); belugas@8528: 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@10442: Window *w = GetCallbackWnd(); tron@2639: if (w == NULL) return; truelight@0: rubidium@10486: Point pt = GetTileBelowCursor(); rubidium@10486: if (pt.x == -1) { truelight@0: _thd.selend.x = -1; truelight@0: return; truelight@0: } rubidium@10486: rubidium@10486: 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@10442: Window *w = GetCallbackWnd(); truelight@0: tron@2639: if (w != NULL) { belugas@6928: /* send an event in client coordinates. */ rubidium@10486: Point pt; rubidium@10486: pt.x = _cursor.pos.x - w->left; rubidium@10486: pt.y = _cursor.pos.y - w->top; rubidium@10486: w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y)); truelight@0: } smatz@9081: smatz@9081: ResetObjectToPlace(); smatz@9081: truelight@0: return false; truelight@0: } truelight@0: rubidium@6573: static bool HandleMouseOver() truelight@543: { rubidium@10163: Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y); truelight@543: belugas@6928: /* We changed window, put a MOUSEOVER event to the last window */ rubidium@10163: if (_mouseover_last_w != NULL && _mouseover_last_w != w) { rubidium@10163: /* Reset mouse-over coordinates of previous window */ rubidium@10486: Point pt = { -1, -1 }; rubidium@10486: _mouseover_last_w->OnMouseOver(pt, 0); truelight@543: } rubidium@10163: rubidium@10163: /* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */ rubidium@10163: _mouseover_last_w = w; truelight@543: tron@2639: if (w != NULL) { belugas@6928: /* send an event in client coordinates. */ rubidium@10486: Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top }; rubidium@10486: int widget = 0; truelight@543: if (w->widget != NULL) { rubidium@10486: widget = GetWidgetFromPos(w, pt.x, pt.y); truelight@543: } rubidium@10486: w->OnMouseOver(pt, widget); truelight@543: } truelight@543: belugas@6928: /* Mouseover never stops execution */ truelight@543: return true; truelight@543: } truelight@543: rubidium@10083: /** rubidium@10083: * Resize the window. rubidium@10083: * 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 belugas@6977: * @param x delta x-size of changed window (positive if larger, etc.) rubidium@10083: * @param y delta y-size of changed window rubidium@10083: */ 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@10434: w->SetDirty(); rubidium@10442: 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@10434: 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; belugas@6928: /* Get out immediately if no window is being dragged at all. */ tron@2639: if (!_dragging_window) return true; truelight@0: belugas@6928: /* 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: belugas@6928: /* 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@10434: w->SetDirty(); truelight@0: rubidium@10442: int x = _cursor.pos.x + _drag_delta.x; rubidium@10442: int y = _cursor.pos.y + _drag_delta.y; rubidium@10442: int nx = x; rubidium@10442: int ny = y; tron@350: rubidium@10775: if (_settings_client.gui.window_snap_radius != 0) { Darkvater@5137: Window* const *vz; Darkvater@5124: rubidium@10775: int hsnap = _settings_client.gui.window_snap_radius; rubidium@10775: 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) { belugas@6928: /* 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: belugas@6928: /* 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) { belugas@6928: /* 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: belugas@6928: /* 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) { belugas@6928: /* 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: belugas@6928: /* 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) { belugas@6928: /* 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: belugas@6928: /* 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: belugas@6928: /* Make sure the window doesn't leave the screen belugas@6928: * 13 is the height of the title bar */ skidd13@8418: nx = Clamp(nx, 13 - t->right, _screen.width - 13 - t->left); skidd13@8418: ny = Clamp(ny, 0, _screen.height - 13); tron@350: belugas@6928: /* Make sure the title bar isn't hidden by behind the main tool bar */ rubidium@10442: 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@10434: 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@10434: 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@10486: Point size; rubidium@10486: Point diff; rubidium@10486: size.x = x + w->width; rubidium@10486: size.y = y + w->height; rubidium@10486: diff.x = x; rubidium@10486: diff.y = y; rubidium@10486: 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@10083: /** rubidium@10083: * Start window dragging rubidium@10083: * @param w Window to start dragging rubidium@10083: */ 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@10083: /** rubidium@10083: * Start resizing a window rubidium@10083: * @param w Window to start resizing rubidium@10083: */ 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: belugas@6928: /* Get out quickly if no item is being scrolled */ tron@2639: if (!_scrolling_scrollbar) return true; truelight@0: belugas@6928: /* 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) { belugas@6928: /* Abort if no button is clicked any more. */ truelight@0: if (!_left_button_down) { truelight@0: w->flags4 &= ~WF_SCROLL_MIDDLE; rubidium@10434: w->SetDirty(); truelight@0: break; truelight@158: } truelight@0: rubidium@10442: int i; rubidium@10442: Scrollbar *sb; rubidium@10442: 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: belugas@6928: /* Find the item we want to move to and make sure it's inside bounds. */ rubidium@10442: 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@10434: 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: { rubidium@10775: 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@10442: Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y); truelight@4335: truelight@8351: if (!(_right_button_down || scrollwheel_scrolling) || w == NULL) { truelight@8351: _cursor.fix_at = false; truelight@8351: _scrolling_viewport = false; truelight@8351: return true; truelight@8351: } truelight@8351: glx@10504: if (w == FindWindowById(WC_MAIN_WINDOW, 0) && w->viewport->follow_vehicle != INVALID_VEHICLE) { belugas@8255: /* If the main window is following a vehicle, then first let go of it! */ glx@10504: const Vehicle *veh = GetVehicle(w->viewport->follow_vehicle); belugas@8255: ScrollMainWindowTo(veh->x_pos, veh->y_pos, true); /* This also resets follow_vehicle */ belugas@8255: return true; belugas@8255: } belugas@8255: rubidium@10486: Point delta; rubidium@10775: if (_settings_client.gui.reverse_scroll) { rubidium@10486: delta.x = -_cursor.delta.x; rubidium@10486: delta.y = -_cursor.delta.y; tron@2680: } else { rubidium@10486: delta.x = _cursor.delta.x; rubidium@10486: delta.y = _cursor.delta.y; tron@2680: } tron@2680: bjarni@6615: if (scrollwheel_scrolling) { bjarni@6615: /* We are using scrollwheels for scrolling */ rubidium@10486: delta.x = _cursor.h_wheel; rubidium@10486: 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@10486: 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@10442: Window * const *wz = FindWindowZPosition(w); rubidium@10258: 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@10434: 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: Darkvater@5086: /** Handle keyboard input. rubidium@10486: * @param raw_key Lower 8 bits contain the ASCII character, the higher 16 bits the keycode rubidium@10083: */ rubidium@10486: void HandleKeypress(uint32 raw_key) truelight@0: { belugas@8515: /* Stores if a window with a textfield for typing is open belugas@8515: * If this is the case, keypress events are only passed to windows with text fields and belugas@8515: * to thein this main toolbar. */ belugas@8515: 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: belugas@6928: /* Setup event */ rubidium@10486: uint16 key = GB(raw_key, 0, 16); rubidium@10486: uint16 keycode = GB(raw_key, 16, 16); truelight@0: egladil@8673: /* egladil@8673: * The Unicode standard defines an area called the private use area. Code points in this egladil@8673: * area are reserved for private use and thus not portable between systems. For instance, egladil@8673: * Apple defines code points for the arrow keys in this area, but these are only printable egladil@8673: * on a system running OS X. We don't want these keys to show up in text fields and such, egladil@8673: * and thus we have to clear the unicode character when we encounter such a key. egladil@8673: */ rubidium@10486: if (key >= 0xE000 && key <= 0xF8FF) key = 0; egladil@8673: egladil@8673: /* egladil@8673: * If both key and keycode is zero, we don't bother to process the event. egladil@8673: */ rubidium@10486: if (key == 0 && keycode == 0) return; egladil@8673: belugas@8515: /* check if we have a query string window open before allowing hotkeys */ belugas@8515: if (FindWindowById(WC_QUERY_STRING, 0) != NULL || belugas@8515: FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL || belugas@8515: FindWindowById(WC_GENERATE_LANDSCAPE, 0) != NULL || belugas@8515: FindWindowById(WC_CONSOLE, 0) != NULL || belugas@8515: FindWindowById(WC_SAVELOAD, 0) != NULL || belugas@8515: FindWindowById(WC_COMPANY_PASSWORD_WINDOW, 0) != NULL) { belugas@8515: query_open = true; belugas@8515: } belugas@8515: belugas@6928: /* Call the event, start with the uppermost window. */ rubidium@10083: for (Window* const *wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: Window *w = *--wz; Darkvater@5124: belugas@8515: /* if a query window is open, only call the event for certain window types */ belugas@8515: if (query_open && belugas@8515: w->window_class != WC_QUERY_STRING && belugas@8515: w->window_class != WC_SEND_NETWORK_MSG && belugas@8515: w->window_class != WC_GENERATE_LANDSCAPE && belugas@8515: w->window_class != WC_CONSOLE && belugas@8515: w->window_class != WC_SAVELOAD && belugas@8515: w->window_class != WC_COMPANY_PASSWORD_WINDOW) { dominik@651: continue; tron@2639: } rubidium@10607: if (w->OnKeyPress(key, keycode) == Window::ES_HANDLED) return; truelight@0: } Darkvater@1637: rubidium@10486: Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0); rubidium@10486: /* When there is no toolbar w is null, check for that */ rubidium@10486: if (w != NULL) w->OnKeyPress(key, keycode); truelight@0: } truelight@0: rubidium@10083: /** rubidium@10083: * State of CONTROL key has changed rubidium@10083: */ smatz@9082: void HandleCtrlChanged() smatz@9082: { smatz@9082: /* Call the event, start with the uppermost window. */ smatz@9082: for (Window* const *wz = _last_z_window; wz != _z_windows;) { smatz@9082: Window *w = *--wz; rubidium@10607: if (w->OnCTRLStateChange() == Window::ES_HANDLED) return; smatz@9082: } smatz@9082: } smatz@9082: rubidium@10083: /** rubidium@10083: * Local counter that is incremented each time an mouse input event is detected. rubidium@10083: * The counter is used to stop auto-scrolling. rubidium@10083: * @see HandleAutoscroll() rubidium@10083: * @see HandleMouseEvents() rubidium@10083: */ Darkvater@5090: static int _input_events_this_tick = 0; Darkvater@5090: rubidium@10083: /** rubidium@10083: * If needed and switched on, perform auto scrolling (automatically rubidium@10083: * moving window contents when mouse is near edge of the window). rubidium@10083: */ 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: rubidium@10775: if (_settings_client.gui.autoscroll && _game_mode != GM_MENU && !IsGeneratingWorld()) { rubidium@10442: int x = _cursor.pos.x; rubidium@10442: int y = _cursor.pos.y; rubidium@10442: Window *w = FindWindowFromPt(x, y); Darkvater@5090: if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL) return; rubidium@10442: ViewPort *vp = IsPtInWindowViewport(w, x, y); Darkvater@5090: if (vp != NULL) { Darkvater@5090: x -= vp->left; Darkvater@5090: y -= vp->top; rubidium@10442: belugas@6928: /* here allows scrolling in both x and y axis */ Darkvater@5090: #define scrollspeed 3 Darkvater@5090: if (x - 15 < 0) { glx@10504: w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom); Darkvater@5090: } else if (15 - (vp->width - x) > 0) { glx@10504: w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom); Darkvater@5090: } Darkvater@5090: if (y - 15 < 0) { glx@10504: w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom); Darkvater@5090: } else if (15 - (vp->height - y) > 0) { glx@10504: 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: truelight@7505: enum MouseClick { truelight@7505: MC_NONE = 0, truelight@7505: MC_LEFT, truelight@7505: MC_RIGHT, truelight@7505: MC_DOUBLE_LEFT, truelight@7505: truelight@7514: MAX_OFFSET_DOUBLE_CLICK = 5, ///< How much the mouse is allowed to move to call it a double click truelight@7514: TIME_BETWEEN_DOUBLE_CLICK = 500, ///< Time between 2 left clicks before it becoming a double click, in ms truelight@7505: }; truelight@7505: rubidium@10258: extern void UpdateTileSelection(); rubidium@10258: extern bool VpHandlePlaceSizingDrag(); rubidium@10258: rubidium@10771: static void ScrollMainViewport(int x, int y) rubidium@10771: { rubidium@10771: if (_game_mode != GM_MENU) { rubidium@10771: Window *w = FindWindowById(WC_MAIN_WINDOW, 0); rubidium@10771: assert(w); rubidium@10771: rubidium@10771: w->viewport->dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom); rubidium@10771: w->viewport->dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom); rubidium@10771: } rubidium@10771: } rubidium@10771: rubidium@10771: /** rubidium@10771: * Describes all the different arrow key combinations the game allows rubidium@10771: * when it is in scrolling mode. rubidium@10771: * The real arrow keys are bitwise numbered as rubidium@10771: * 1 = left rubidium@10771: * 2 = up rubidium@10771: * 4 = right rubidium@10771: * 8 = down rubidium@10771: */ rubidium@10771: static const int8 scrollamt[16][2] = { rubidium@10771: { 0, 0}, ///< no key specified rubidium@10771: {-2, 0}, ///< 1 : left rubidium@10771: { 0, -2}, ///< 2 : up rubidium@10771: {-2, -1}, ///< 3 : left + up rubidium@10771: { 2, 0}, ///< 4 : right rubidium@10771: { 0, 0}, ///< 5 : left + right = nothing rubidium@10771: { 2, -1}, ///< 6 : right + up rubidium@10771: { 0, -2}, ///< 7 : right + left + up = up rubidium@10771: { 0 ,2}, ///< 8 : down rubidium@10771: {-2 ,1}, ///< 9 : down + left rubidium@10771: { 0, 0}, ///< 10 : down + up = nothing rubidium@10771: {-2, 0}, ///< 11 : left + up + down = left rubidium@10771: { 2, 1}, ///< 12 : down + right rubidium@10771: { 0, 2}, ///< 13 : left + right + down = down rubidium@10771: { 2, 0}, ///< 14 : right + up + down = right rubidium@10771: { 0, 0}, ///< 15 : left + up + right + down = nothing rubidium@10771: }; rubidium@10771: rubidium@10795: static void HandleKeyScrolling() rubidium@10771: { rubidium@10771: if (_dirkeys && !_no_scroll) { rubidium@10771: int factor = _shift_pressed ? 50 : 10; rubidium@10771: ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor); rubidium@10771: } rubidium@10771: } rubidium@10771: truelight@7505: void MouseLoop(MouseClick click, int mousewheel) truelight@0: { truelight@0: DecreaseWindowCounters(); truelight@0: HandlePlacePresize(); truelight@0: UpdateTileSelection(); rubidium@10795: 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: rubidium@10775: bool scrollwheel_scrolling = _settings_client.gui.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0); truelight@7505: if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return; truelight@0: rubidium@10442: int x = _cursor.pos.x; rubidium@10442: int y = _cursor.pos.y; rubidium@10442: Window *w = FindWindowFromPt(x, y); tron@2639: if (w == NULL) return; rubidium@10442: Darkvater@5667: if (!MaybeBringWindowToFront(w)) return; rubidium@10442: 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) { rubidium@10775: if (_settings_client.gui.scrollwheel_scrolling == 0) { rubidium@10586: /* Send mousewheel event to window */ rubidium@10486: 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) { truelight@7505: if (scrollwheel_scrolling) click = MC_RIGHT; // we are using the scrollwheel in a viewport, so we emulate right mouse button truelight@4337: switch (click) { truelight@7505: case MC_DOUBLE_LEFT: truelight@7505: case MC_LEFT: Darkvater@5568: DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite); smatz@8414: if (_thd.place_mode != VHM_NONE && belugas@6928: /* 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: smatz@8414: 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: truelight@7505: 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; truelight@7505: truelight@7505: default: truelight@7505: break; truelight@0: } truelight@0: } else { tron@2631: switch (click) { rubidium@10457: case MC_DOUBLE_LEFT: rubidium@10457: DispatchLeftClickEvent(w, x - w->left, y - w->top, true); rubidium@10457: if (_mouseover_last_w == NULL) break; // The window got removed. rubidium@10457: /* fallthough, and also give a single-click for backwards compatibility */ rubidium@10457: case MC_LEFT: rubidium@10457: DispatchLeftClickEvent(w, x - w->left, y - w->top, false); rubidium@10457: break; rubidium@10457: 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 */ truelight@7505: case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break; tron@2631: } truelight@0: } truelight@0: } truelight@0: rubidium@10083: /** rubidium@10083: * Handle a mouse event from the video driver rubidium@10083: */ rubidium@6573: void HandleMouseEvents() pasky@1570: { truelight@7505: static int double_click_time = 0; truelight@7505: static int double_click_x = 0; truelight@7505: 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: belugas@6928: /* Mouse event? */ rubidium@10442: MouseClick click = MC_NONE; pasky@1570: if (_left_button_down && !_left_button_clicked) { truelight@7505: click = MC_LEFT; truelight@7514: if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK && truelight@7505: double_click_x != 0 && abs(_cursor.pos.x - double_click_x) < MAX_OFFSET_DOUBLE_CLICK && truelight@7505: double_click_y != 0 && abs(_cursor.pos.y - double_click_y) < MAX_OFFSET_DOUBLE_CLICK) { truelight@7505: click = MC_DOUBLE_LEFT; truelight@7505: } truelight@7514: double_click_time = _realtime_tick; truelight@7505: double_click_x = _cursor.pos.x; truelight@7505: 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; truelight@7505: click = MC_RIGHT; Darkvater@5090: _input_events_this_tick++; pasky@1570: } pasky@1570: rubidium@10442: 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@10083: /** rubidium@10083: * Regular call from the global game loop rubidium@10083: */ rubidium@6573: void InputLoop() Darkvater@5090: { rubidium@10941: HandleKeyScrolling(); Darkvater@5090: HandleMouseEvents(); Darkvater@5090: HandleAutoscroll(); Darkvater@5090: } Darkvater@5090: rubidium@10083: /** rubidium@10083: * Update the continuously changing contents of the windows, such as the viewports rubidium@10083: */ 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@10486: (*--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@10434: 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@7950: DrawChatMessage(); belugas@6928: /* Redraw mouse cursor in case it was hidden */ truelight@0: DrawMouseCursor(); truelight@158: } truelight@0: rubidium@10083: /** rubidium@10255: * Mark window as dirty (in need of repainting) rubidium@10255: * @param cls Window class rubidium@10255: * @param number Window number in that class rubidium@10083: */ 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@10434: if (w->window_class == cls && w->window_number == number) w->SetDirty(); truelight@0: } truelight@0: } truelight@0: rubidium@10255: /** rubidium@10083: * Mark a particular widget in a particular window as dirty (in need of repainting) rubidium@10083: * @param cls Window class rubidium@10083: * @param number Window number in that class rubidium@10083: * @param widget_index Index number of the widget that needs repainting rubidium@10083: */ 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) { glx@8524: w->InvalidateWidget(widget_index); truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@10255: /** rubidium@10083: * Mark all windows of a particular class as dirty (in need of repainting) rubidium@10083: * @param cls Window class rubidium@10083: */ tron@2788: void InvalidateWindowClasses(WindowClass cls) truelight@0: { Darkvater@5137: Window* const *wz; tron@2639: Darkvater@5124: FOR_ALL_WINDOWS(wz) { rubidium@10434: if ((*wz)->window_class == cls) (*wz)->SetDirty(); truelight@0: } truelight@0: } truelight@0: rubidium@10083: /** rubidium@10083: * Mark window data as invalid (in need of re-computing) rubidium@10083: * @param w Window with invalid data rubidium@10083: */ rubidium@10485: void InvalidateThisWindowData(Window *w, int data) bjarni@4766: { rubidium@10486: w->OnInvalidateData(data); rubidium@10434: w->SetDirty(); bjarni@4766: } bjarni@4766: rubidium@10083: /** rubidium@10255: * Mark window data of the window of a given class and specific window number as invalid (in need of re-computing) rubidium@10083: * @param cls Window class rubidium@10083: * @param number Window number within the class rubidium@10083: */ rubidium@10485: 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@10485: if (w->window_class == cls && w->window_number == number) InvalidateThisWindowData(w, data); bjarni@4766: } bjarni@4766: } bjarni@4766: rubidium@10083: /** rubidium@10083: * Mark window data of all windows of a given class as invalid (in need of re-computing) rubidium@10083: * @param cls Window class rubidium@10083: */ rubidium@10485: void InvalidateWindowClassesData(WindowClass cls, int data) bjarni@4766: { Darkvater@5124: Window* const *wz; bjarni@4766: Darkvater@5124: FOR_ALL_WINDOWS(wz) { rubidium@10485: if ((*wz)->window_class == cls) InvalidateThisWindowData(*wz, data); bjarni@4739: } bjarni@4739: } truelight@0: rubidium@10083: /** rubidium@10083: * Dispatch WE_TICK event over all windows rubidium@10083: */ rubidium@6573: void CallWindowTickEvent() truelight@0: { rubidium@10771: if (_scroller_click_timeout > 3) { rubidium@10771: _scroller_click_timeout -= 3; rubidium@10771: } else { rubidium@10771: _scroller_click_timeout = 0; rubidium@10771: } rubidium@10771: rubidium@10258: for (Window * const *wz = _last_z_window; wz != _z_windows;) { rubidium@10486: (*--wz)->OnTick(); truelight@0: } truelight@0: } truelight@0: rubidium@10258: /** rubidium@10258: * Try to delete a non-vital window. rubidium@10258: * Non-vital windows are windows other than the game selection, main toolbar, rubidium@10258: * status bar, toolbar menu, and tooltip windows. Stickied windows are also rubidium@10258: * considered vital. rubidium@10258: */ 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@10433: delete w; Darkvater@5121: goto restart_search; truelight@0: } truelight@0: } truelight@0: } truelight@0: belugas@6939: /** 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@10433: delete *wz; Darkvater@5121: goto restart_search; tron@4077: } darkvater@763: } darkvater@763: } darkvater@763: belugas@6939: /** 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@10083: /** rubidium@10083: * (Re)position main toolbar window at the screen rubidium@10083: * @param w Window structure of the main toolbar window, may also be \c NULL rubidium@10083: * @return X coordinate of left edge of the repositioned toolbar window rubidium@10083: */ 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: rubidium@10775: 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: rubidium@10768: void SetVScrollCount(Window *w, int num) rubidium@10768: { rubidium@10768: w->vscroll.count = num; rubidium@10768: num -= w->vscroll.cap; rubidium@10768: if (num < 0) num = 0; rubidium@10768: if (num < w->vscroll.pos) w->vscroll.pos = num; rubidium@10768: } rubidium@10768: rubidium@10768: void SetVScroll2Count(Window *w, int num) rubidium@10768: { rubidium@10768: w->vscroll2.count = num; rubidium@10768: num -= w->vscroll2.cap; rubidium@10768: if (num < 0) num = 0; rubidium@10768: if (num < w->vscroll2.pos) w->vscroll2.pos = num; rubidium@10768: } rubidium@10768: rubidium@10768: void SetHScrollCount(Window *w, int num) rubidium@10768: { rubidium@10768: w->hscroll.count = num; rubidium@10768: num -= w->hscroll.cap; rubidium@10768: if (num < 0) num = 0; rubidium@10768: if (num < w->hscroll.pos) w->hscroll.pos = num; rubidium@10768: } rubidium@10768: rubidium@10083: /** rubidium@10083: * Relocate all windows to fit the new size of the game application screen rubidium@10083: * @param neww New width of the game application screen rubidium@10083: * @param newh New height of the game appliction screen rubidium@10083: */ 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; truelight@7122: vp->virtual_width = ScaleByZoom(neww, vp->zoom); truelight@7122: 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@7858: if (neww - w->width != 0) { rubidium@7858: ResizeWindow(w, min(neww, 640) - w->width, 0); rubidium@7858: rubidium@10486: Point size; rubidium@10486: Point delta; rubidium@10486: size.x = w->width; rubidium@10486: size.y = w->height; rubidium@10486: delta.x = neww - w->width; rubidium@10486: delta.y = 0; rubidium@10486: w->OnResize(size, delta); rubidium@7858: } rubidium@7858: 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: skidd13@8418: 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: skidd13@8418: 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: smatz@9194: default: { tron@2989: left = w->left; tron@2989: if (left + (w->width >> 1) >= neww) left = neww - w->width; rubidium@8519: if (left < 0) left = 0; rubidium@8519: tron@2989: top = w->top; tron@2989: if (top + (w->height >> 1) >= newh) top = newh - w->height; smatz@9194: smatz@9194: const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0); smatz@9194: if (wt != NULL) { rubidium@10183: if (top < wt->height && wt->left < (w->left + w->width) && (wt->left + wt->width) > w->left) top = wt->height; smatz@9194: if (top >= newh) top = newh - 1; smatz@9194: } else { smatz@9194: if (top < 0) top = 0; smatz@9194: } smatz@9194: } 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: } belugas@10589: belugas@10589: /** Destructor of the base class PickerWindowBase belugas@10589: * Main utility is to stop the base Window destructor from triggering belugas@10589: * a free while the child will already be free, in this case by the ResetObjectToPlace(). belugas@10589: */ belugas@10589: PickerWindowBase::~PickerWindowBase() belugas@10589: { belugas@10589: this->window_class = WC_INVALID; // stop the ancestor from freeing the already (to be) child belugas@10589: ResetObjectToPlace(); belugas@10589: }