tron@2186: /* $Id$ */ tron@2186: belugas@6443: /** @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" tron@2163: #include "functions.h" tron@679: #include "map.h" tron@2154: #include "player.h" truelight@0: #include "window.h" truelight@0: #include "gfx.h" darkvater@152: #include "viewport.h" dominik@126: #include "console.h" tron@2159: #include "variables.h" tron@2517: #include "table/sprites.h" truelight@4300: #include "genworld.h" rubidium@5838: #include "helpers.hpp" truelight@0: belugas@6928: /* delta between mouse cursor and upper left corner of dragged window */ tron@350: static Point _drag_delta; tron@350: Darkvater@5124: static Window _windows[25]; Darkvater@5124: Window *_z_windows[lengthof(_windows)]; Darkvater@5126: Window **_last_z_window; ///< always points to the next free space in the z-array Darkvater@5124: glx@4755: void CDECL SetWindowWidgetsDisabledState(Window *w, bool disab_stat, int widgets, ...) glx@4755: { glx@4755: va_list wdg_list; glx@4755: glx@4755: va_start(wdg_list, widgets); glx@4755: glx@4755: while (widgets != WIDGET_LIST_END) { glx@4757: SetWindowWidgetDisabledState(w, widgets, disab_stat); glx@4755: widgets = va_arg(wdg_list, int); glx@4755: } glx@4755: glx@4755: va_end(wdg_list); glx@4755: } glx@4755: glx@4755: void CDECL SetWindowWidgetsHiddenState(Window *w, bool hidden_stat, int widgets, ...) glx@4755: { glx@4755: va_list wdg_list; glx@4755: glx@4755: va_start(wdg_list, widgets); glx@4755: glx@4755: while (widgets != WIDGET_LIST_END) { glx@4755: SetWindowWidgetHiddenState(w, widgets, hidden_stat); glx@4755: widgets = va_arg(wdg_list, int); glx@4755: } glx@4755: glx@4755: va_end(wdg_list); glx@4755: } glx@4755: glx@4755: void CDECL SetWindowWidgetsLoweredState(Window *w, bool lowered_stat, int widgets, ...) glx@4755: { glx@4755: va_list wdg_list; glx@4755: glx@4755: va_start(wdg_list, widgets); glx@4755: glx@4755: while (widgets != WIDGET_LIST_END) { glx@4755: SetWindowWidgetLoweredState(w, widgets, lowered_stat); glx@4755: widgets = va_arg(wdg_list, int); glx@4755: } glx@4755: glx@4755: va_end(wdg_list); glx@4755: } glx@4755: belugas@4719: void RaiseWindowButtons(Window *w) belugas@4719: { rubidium@5235: uint i; belugas@4719: rubidium@5235: for (i = 0; i < w->widget_count; i++) { belugas@4719: if (IsWindowWidgetLowered(w, i)) { belugas@4719: RaiseWindowWidget(w, i); belugas@4719: InvalidateWidget(w, i); belugas@4719: } belugas@4719: } belugas@4719: } belugas@4719: truelight@0: void HandleButtonClick(Window *w, byte widget) truelight@0: { belugas@4719: LowerWindowWidget(w, widget); truelight@0: w->flags4 |= 5 << WF_TIMEOUT_SHL; truelight@0: InvalidateWidget(w, widget); truelight@0: } truelight@0: tron@2817: Darkvater@5124: static void StartWindowDrag(Window *w); Darkvater@5124: static void StartWindowSizing(Window *w); tron@2817: belugas@4171: static void DispatchLeftClickEvent(Window *w, int x, int y) tron@2596: { truelight@0: WindowEvent e; truelight@0: const Widget *wi; truelight@0: belugas@4634: e.we.click.pt.x = x; belugas@4634: e.we.click.pt.y = y; truelight@0: e.event = WE_CLICK; truelight@0: truelight@0: if (w->desc_flags & WDF_DEF_WIDGET) { belugas@4634: e.we.click.widget = GetWidgetFromPos(w, x, y); belugas@6928: if (e.we.click.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@5236: if (IsWindowWidgetDisabled(w, e.we.click.widget)) return; darkvater@222: rubidium@5236: wi = &w->widget[e.we.click.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 */ Darkvater@4938: HandleButtonClick(w, e.we.click.widget); Darkvater@4938: break; truelight@0: } bjarni@842: } else if (wi->type == WWT_SCROLLBAR || wi->type == WWT_SCROLL2BAR || wi->type == WWT_HSCROLLBAR) { belugas@4634: ScrollbarClickHandler(w, wi, e.we.click.pt.x, e.we.click.pt.y); truelight@0: } truelight@0: truelight@0: if (w->desc_flags & WDF_STD_BTN) { belugas@4634: if (e.we.click.widget == 0) { /* 'X' */ truelight@867: DeleteWindow(w); celestar@984: return; tron@1109: } tron@1109: belugas@4634: if (e.we.click.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); Darkvater@5272: InvalidateWidget(w, e.we.click.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; belugas@4634: InvalidateWidget(w, e.we.click.widget); darkvater@1112: return; darkvater@682: } truelight@0: } darkvater@1038: darkvater@1038: w->wndproc(w, &e); truelight@0: } truelight@0: belugas@4171: static void DispatchRightClickEvent(Window *w, int x, int y) tron@2596: { truelight@0: WindowEvent e; truelight@0: truelight@0: /* default tooltips handler? */ truelight@0: if (w->desc_flags & WDF_STD_TOOLTIPS) { belugas@4634: e.we.click.widget = GetWidgetFromPos(w, x, y); belugas@4634: if (e.we.click.widget < 0) belugas@6928: return; // exit if clicked outside of widgets truelight@0: belugas@4634: if (w->widget[e.we.click.widget].tooltips != 0) { belugas@4634: GuiShowTooltips(w->widget[e.we.click.widget].tooltips); truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: e.event = WE_RCLICK; belugas@4634: e.we.click.pt.x = x; belugas@4634: e.we.click.pt.y = y; truelight@158: w->wndproc(w, &e); truelight@0: } truelight@0: Darkvater@2021: /** Dispatch the mousewheel-action to the window which will scroll any Darkvater@2021: * compatible scrollbars if the mouse is pointed over the bar or its contents Darkvater@2021: * @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: const Widget *wi1, *wi2; darkvater@982: Scrollbar *sb; darkvater@982: Darkvater@2021: if (widget < 0) return; Darkvater@2021: Darkvater@2021: wi1 = &w->widget[widget]; Darkvater@2021: 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 */ 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) { darkvater@982: int pos = clamp(sb->pos + wheel, 0, sb->count - sb->cap); darkvater@982: if (pos != sb->pos) { darkvater@982: sb->pos = pos; darkvater@982: SetWindowDirty(w); darkvater@982: } truelight@0: } truelight@0: } truelight@0: } truelight@0: Darkvater@5124: static void DrawOverlappedWindow(Window* const *wz, int left, int top, int right, int bottom); tron@2817: truelight@0: void DrawOverlappedWindowForAll(int left, int top, int right, int bottom) truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: DrawPixelInfo bk; truelight@0: _cur_dpi = &bk; truelight@0: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: const Window *w = *wz; truelight@158: if (right > w->left && truelight@0: bottom > w->top && truelight@0: left < w->left + w->width && truelight@0: top < w->top + w->height) { Darkvater@5124: DrawOverlappedWindow(wz, left, top, right, bottom); tron@2639: } truelight@0: } truelight@0: } truelight@0: 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: int x; 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@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: truelight@0: { truelight@158: DrawPixelInfo *dp = _cur_dpi; truelight@0: dp->width = right - left; truelight@0: dp->height = bottom - top; Darkvater@5124: dp->left = left - (*wz)->left; Darkvater@5124: dp->top = top - (*wz)->top; truelight@0: dp->pitch = _screen.pitch; truelight@0: dp->dst_ptr = _screen.dst_ptr + top * _screen.pitch + left; truelight@0: dp->zoom = 0; Darkvater@5124: CallWindowEventNP(*wz, WE_PAINT); truelight@0: } truelight@0: } truelight@0: truelight@0: void CallWindowEventNP(Window *w, int event) truelight@0: { truelight@0: WindowEvent e; truelight@543: truelight@0: e.event = event; truelight@0: w->wndproc(w, &e); truelight@0: } truelight@0: belugas@4171: void SetWindowDirty(const Window *w) truelight@0: { tron@2549: if (w == NULL) return; truelight@0: SetDirtyBlocks(w->left, w->top, w->left + w->width, w->top + w->height); 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 belugas@6939: * @return the window that matches it */ Darkvater@5124: Window **FindWindowZPosition(const Window *w) Darkvater@5124: { Darkvater@5124: Window **wz; Darkvater@5124: Darkvater@5680: for (wz = _z_windows; wz != _last_z_window; 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: truelight@0: void DeleteWindow(Window *w) truelight@0: { Darkvater@5666: Window *v; Darkvater@5124: Window **wz; tron@2639: if (w == NULL) return; truelight@0: Darkvater@5666: /* Delete any children a window might have in a head-recursive manner */ Darkvater@5666: v = FindChildWindow(w); Darkvater@5666: if (v != NULL) DeleteWindow(v); Darkvater@5666: tron@4077: if (_thd.place_mode != VHM_NONE && tron@4077: _thd.window_class == w->window_class && tron@4077: _thd.window_number == w->window_number) { truelight@0: ResetObjectToPlace(); truelight@0: } truelight@0: truelight@0: CallWindowEventNP(w, WE_DESTROY); Darkvater@5122: if (w->viewport != NULL) DeleteWindowViewport(w); truelight@0: truelight@0: SetWindowDirty(w); truelight@867: free(w->widget); Darkvater@5124: w->widget = NULL; rubidium@5232: w->widget_count = 0; Darkvater@5666: w->parent = NULL; truelight@867: Darkvater@5124: /* Find the window in the z-array, and effectively remove it Darkvater@5124: * by moving all windows after it one to the left */ Darkvater@5124: wz = FindWindowZPosition(w); Darkvater@5680: if (wz == NULL) return; Darkvater@5124: memmove(wz, wz + 1, (byte*)_last_z_window - (byte*)wz); Darkvater@5124: _last_z_window--; truelight@0: } truelight@0: 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: truelight@0: void DeleteWindowById(WindowClass cls, WindowNumber number) truelight@0: { truelight@0: DeleteWindow(FindWindowById(cls, number)); truelight@158: } truelight@0: 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) { darkvater@999: DeleteWindow(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) { bjarni@5077: DeleteWindow(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: bjarni@5077: if (w->caption_color != old_player) continue; bjarni@5077: if (w->window_class == WC_PLAYER_COLOR) continue; bjarni@5077: if (w->window_class == WC_FINANCES) continue; bjarni@5077: if (w->window_class == WC_STATION_LIST) continue; bjarni@5077: if (w->window_class == WC_TRAINS_LIST) continue; bjarni@5077: if (w->window_class == WC_ROADVEH_LIST) continue; bjarni@5077: if (w->window_class == WC_SHIPS_LIST) continue; bjarni@5077: if (w->window_class == WC_AIRCRAFT_LIST) continue; bjarni@5077: if (w->window_class == WC_BUY_COMPANY) continue; bjarni@5077: if (w->window_class == WC_COMPANY) continue; Darkvater@5120: bjarni@5077: w->caption_color = new_player; 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); truelight@0: SetWindowDirty(w); truelight@0: } truelight@0: truelight@0: return w; truelight@0: } truelight@0: darkvater@1648: static inline bool IsVitalWindow(const Window *w) darkvater@1648: { darkvater@1648: WindowClass wc = w->window_class; darkvater@1648: return (wc == WC_MAIN_TOOLBAR || wc == WC_STATUS_BAR || wc == WC_NEWS_WINDOW || wc == WC_SEND_NETWORK_MSG); 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 *tempz; 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: Darkvater@5124: tempz = *wz; Darkvater@5124: memmove(wz, wz + 1, (byte*)vz - (byte*)wz); Darkvater@5124: *vz = tempz; truelight@0: Darkvater@5124: SetWindowDirty(w); 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: Darkvater@5124: for (wz = _z_windows;; wz++) { Darkvater@5124: Window *w = *wz; Darkvater@5124: assert(wz < _last_z_window); tron@2639: if (w->window_class != WC_MAIN_WINDOW && !IsVitalWindow(w)) return w; darkvater@763: } darkvater@763: } darkvater@763: belugas@4171: bool IsWindowOfPrototype(const Window *w, const Widget *widget) truelight@867: { truelight@867: return (w->original_widget == widget); truelight@867: } truelight@867: belugas@6939: /** Copies 'widget' to 'w->widget' to allow for resizable windows belugas@6939: * @param w Window on which to attach the widget array belugas@6939: * @param widget pointer of widget array to fill the window with */ truelight@867: void AssignWidgetToWindow(Window *w, const Widget *widget) truelight@867: { truelight@867: w->original_widget = widget; truelight@867: truelight@867: if (widget != NULL) { truelight@867: uint index = 1; belugas@4171: const Widget *wi; truelight@867: tron@2639: for (wi = widget; wi->type != WWT_LAST; wi++) index++; tron@2639: KUDr@5860: w->widget = ReallocT(w->widget, 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@6573: static Window *FindFreeWindow() Darkvater@5124: { Darkvater@5124: Window *w; Darkvater@5124: Darkvater@5124: for (w = _windows; w < endof(_windows); w++) { Darkvater@5124: Window* const *wz; Darkvater@5124: bool window_in_use = false; Darkvater@5124: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: if (*wz == w) { Darkvater@5124: window_in_use = true; Darkvater@5124: break; Darkvater@5124: } Darkvater@5124: } Darkvater@5124: Darkvater@5124: if (!window_in_use) return w; Darkvater@5124: } Darkvater@5124: Darkvater@5124: assert(_last_z_window == endof(_z_windows)); Darkvater@5124: return NULL; Darkvater@5124: } Darkvater@5124: belugas@6939: /** Open a new window. bjarni@4520: * This function is called from AllocateWindow() or AllocateWindowDesc() bjarni@4520: * See descriptions for those functions for usage bjarni@4520: * See AllocateWindow() for description of arguments. 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 belugas@6939: * @param width width in pixels of the window belugas@6939: * @param height height in pixels of the window belugas@6939: * @param *proc see WindowProc function to call when any messages/updates happen to 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 belugas@6939: * @return Window pointer of the newly created window */ bjarni@4520: static Window *LocalAllocateWindow( darkvater@1648: int x, int y, int width, int height, bjarni@4520: WindowProc *proc, WindowClass cls, const Widget *widget, int window_number) truelight@0: { Darkvater@5124: Window *w = FindFreeWindow(); truelight@0: Darkvater@5124: /* We have run out of windows, close one and use that as the place for our new one */ Darkvater@5124: if (w == NULL) { Darkvater@5124: w = FindDeletableWindow(); Darkvater@5124: if (w == NULL) w = ForceFindDeletableWindow(); Darkvater@5124: DeleteWindow(w); truelight@0: } truelight@0: belugas@6928: /* Set up window properties */ Darkvater@5120: memset(w, 0, sizeof(*w)); truelight@0: w->window_class = cls; darkvater@1648: w->flags4 = WF_WHITE_BORDER_MASK; // just opened windows have a white border truelight@0: w->caption_color = 0xFF; truelight@0: w->left = x; truelight@0: w->top = y; truelight@0: w->width = width; truelight@0: w->height = height; truelight@0: w->wndproc = proc; truelight@867: AssignWidgetToWindow(w, widget); truelight@867: w->resize.width = width; truelight@867: w->resize.height = height; truelight@867: w->resize.step_width = 1; truelight@867: w->resize.step_height = 1; bjarni@4520: w->window_number = window_number; truelight@0: Darkvater@5124: { Darkvater@5124: Window **wz = _last_z_window; Darkvater@5124: Darkvater@5124: /* Hacky way of specifying always-on-top windows. These windows are Darkvater@5124: * always above other windows because they are moved below them. Darkvater@5124: * status-bar is above news-window because it has been created earlier. Darkvater@5124: * Also, as the chat-window is excluded from this, it will always be Darkvater@5124: * the last window, thus always on top. Darkvater@5124: * XXX - Yes, ugly, probably needs something like w->always_on_top flag Darkvater@5124: * to implement correctly, but even then you need some kind of distinction Darkvater@5124: * between on-top of chat/news and status windows, because these conflict */ Darkvater@5124: if (wz != _z_windows && w->window_class != WC_SEND_NETWORK_MSG) { Darkvater@5124: if (FindWindowById(WC_MAIN_TOOLBAR, 0) != NULL) wz--; Darkvater@5124: if (FindWindowById(WC_STATUS_BAR, 0) != NULL) wz--; Darkvater@5124: if (FindWindowById(WC_NEWS_WINDOW, 0) != NULL) wz--; Darkvater@5124: if (FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL) wz--; Darkvater@5124: Darkvater@5124: assert(wz >= _z_windows); Darkvater@5124: if (wz != _last_z_window) memmove(wz + 1, wz, (byte*)_last_z_window - (byte*)wz); Darkvater@5124: } Darkvater@5124: Darkvater@5124: *wz = w; Darkvater@5124: _last_z_window++; Darkvater@5124: } truelight@0: truelight@0: SetWindowDirty(w); dominik@116: CallWindowEventNP(w, WE_CREATE); truelight@0: truelight@0: return w; truelight@0: } truelight@0: bjarni@4520: /** bjarni@4520: * Open a new window. If there is no space for a new window, close an open bjarni@4520: * window. Try to avoid stickied windows, but if there is no else, close one of bjarni@4520: * those as well. Then make sure all created windows are below some always-on-top bjarni@4520: * ones. Finally set all variables and call the WE_CREATE event bjarni@4520: * @param x offset in pixels from the left of the screen bjarni@4520: * @param y offset in pixels from the top of the screen bjarni@4520: * @param width width in pixels of the window bjarni@4520: * @param height height in pixels of the window belugas@6939: * @param *proc see WindowProc function to call when any messages/updates happen to 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: * @return Window pointer of the newly created window */ bjarni@4520: Window *AllocateWindow( bjarni@4520: int x, int y, int width, int height, bjarni@4520: WindowProc *proc, WindowClass cls, const Widget *widget) bjarni@4520: { bjarni@4520: return LocalAllocateWindow(x, y, width, height, proc, cls, widget, 0); bjarni@4520: } truelight@0: rubidium@6574: struct SizeRect { truelight@0: int left,top,width,height; rubidium@6574: }; truelight@0: truelight@0: 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: truelight@0: if (left < 0 || top < 22 || right > _screen.width || bottom > _screen.height) truelight@0: 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: peter1138@7028: static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos) truelight@0: { Darkvater@5124: Window* const *wz; truelight@0: Darkvater@5120: if (left < -(width>>2) || left > _screen.width - (width>>1)) return false; 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: tron@1095: static Point GetAutoPlacePosition(int width, int height) tron@1095: { Darkvater@5124: Window* const *wz; truelight@0: Point pt; truelight@0: peter1138@7028: if (IsGoodAutoPlace1(0, 24, width, height, pt)) return pt; truelight@0: 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: 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: 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: bjarni@4520: static Window *LocalAllocateWindowDesc(const WindowDesc *desc, int window_number) truelight@0: { truelight@0: Point pt; truelight@0: Window *w; truelight@0: Darkvater@5071: /* By default position a child window at an offset of 10/10 of its parent. Darkvater@5071: * However if it falls too extremely outside window positions, reposition Darkvater@5071: * it to an automatic place */ Darkvater@5071: if (desc->parent_cls != 0 /* WC_MAIN_WINDOW */ && Darkvater@5071: (w = FindWindowById(desc->parent_cls, window_number)) != NULL && Darkvater@5071: w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) { Darkvater@5071: truelight@0: pt.x = w->left + 10; Darkvater@5071: if (pt.x > _screen.width + 10 - desc->width) { truelight@0: pt.x = (_screen.width + 10 - desc->width) - 20; Darkvater@5071: } truelight@0: pt.y = w->top + 10; truelight@0: } else { Darkvater@5072: switch (desc->left) { Darkvater@5072: case WDP_ALIGN_TBR: { /* Align the right side with the top toolbar */ Darkvater@5072: w = FindWindowById(WC_MAIN_TOOLBAR, 0); Darkvater@5072: pt.x = (w->left + w->width) - desc->width; rubidium@6988: } break; Darkvater@5072: 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; Darkvater@5072: case WDP_AUTO: /* Find a good automatic position for the window */ Darkvater@5072: pt = GetAutoPlacePosition(desc->width, desc->height); Darkvater@5072: goto allocate_window; Darkvater@5072: case WDP_CENTER: /* Centre the window horizontally */ Darkvater@5072: pt.x = (_screen.width - desc->width) / 2; Darkvater@5072: break; 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) { Darkvater@5072: case WDP_CENTER: /* Centre the window vertically */ Darkvater@5072: pt.y = (_screen.height - desc->height) / 2; Darkvater@5072: break; 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 */ 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: Darkvater@5072: allocate_window: bjarni@4520: w = LocalAllocateWindow(pt.x, pt.y, desc->width, desc->height, desc->proc, desc->cls, desc->widgets, window_number); truelight@0: w->desc_flags = desc->flags; truelight@0: return w; truelight@0: } truelight@0: bjarni@4520: /** bjarni@4520: * Open a new window. bjarni@4520: * @param *desc The pointer to the WindowDesc to be created belugas@6939: * @return Window pointer of the newly created window bjarni@4520: */ bjarni@4520: Window *AllocateWindowDesc(const WindowDesc *desc) bjarni@4520: { bjarni@4520: return LocalAllocateWindowDesc(desc, 0); bjarni@4520: } bjarni@4520: bjarni@4520: /** bjarni@4520: * Open a new window. bjarni@4520: * @param *desc The pointer to the WindowDesc to be created bjarni@4520: * @param window_number the window number of the new window belugas@6939: * @return see Window pointer of the newly created window bjarni@4520: */ bjarni@4520: Window *AllocateWindowDescFront(const WindowDesc *desc, int window_number) bjarni@4520: { bjarni@4520: Window *w; bjarni@4520: bjarni@4520: if (BringWindowToFrontById(desc->cls, window_number)) return NULL; bjarni@4520: w = LocalAllocateWindowDesc(desc, window_number); bjarni@4520: return w; 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: { Darkvater@5124: Window* const *wz; truelight@0: Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: Window *w = *--wz; Darkvater@5120: if (IS_INSIDE_1D(x, w->left, w->width) && IS_INSIDE_1D(y, w->top, w->height)) { tron@2639: return w; tron@2639: } truelight@0: } truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: rubidium@6573: void InitWindowSystem() darkvater@152: { dominik@136: IConsoleClose(); Darkvater@1474: truelight@0: memset(&_windows, 0, sizeof(_windows)); Darkvater@5124: _last_z_window = _z_windows; Darkvater@5122: InitViewports(); Darkvater@1397: _no_scroll = 0; truelight@0: } truelight@0: rubidium@6573: void UnInitWindowSystem() Darkvater@1474: { Darkvater@5664: Window **wz; Darkvater@5893: Darkvater@5893: restart_search: Darkvater@5893: /* Delete all windows, reset z-array. Darkvater@5893: *When we find the window to delete, we need to restart the search Darkvater@5893: * as deleting this window could cascade in deleting (many) others Darkvater@5893: * anywhere in the z-array. We call DeleteWindow() so that it can properly Darkvater@5893: * release own alloc'd memory, which otherwise could result in memleaks */ Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5893: DeleteWindow(*wz); Darkvater@5893: goto restart_search; Darkvater@1474: } Darkvater@5893: Darkvater@5893: assert(_last_z_window == _z_windows); Darkvater@1474: } Darkvater@1474: 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: { truelight@0: Window *w; Darkvater@5124: Window* const *wz; truelight@0: Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: 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); truelight@0: SetWindowDirty(w); truelight@0: } truelight@0: CallWindowEventNP(w, WE_MOUSELOOP); truelight@0: } truelight@0: Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: w = *--wz; truelight@158: truelight@0: if (w->flags4&WF_TIMEOUT_MASK && !(--w->flags4&WF_TIMEOUT_MASK)) { truelight@0: CallWindowEventNP(w, WE_TIMEOUT); belugas@4719: if (w->desc_flags & WDF_UNCLICK_BUTTONS) RaiseWindowButtons(w); 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: { truelight@0: Window *w; truelight@0: WindowEvent e; truelight@0: tron@2639: if (_special_mouse_mode != WSM_PRESIZE) return; truelight@0: tron@2639: w = GetCallbackWnd(); tron@2639: if (w == NULL) return; truelight@0: belugas@4634: e.we.place.pt = GetTileBelowCursor(); belugas@4634: if (e.we.place.pt.x == -1) { truelight@0: _thd.selend.x = -1; truelight@0: return; truelight@0: } belugas@4634: e.we.place.tile = TileVirtXY(e.we.place.pt.x, e.we.place.pt.y); truelight@0: e.event = WE_PLACE_PRESIZE; truelight@0: w->wndproc(w, &e); truelight@0: } truelight@0: rubidium@6573: static bool HandleDragDrop() truelight@0: { truelight@0: Window *w; truelight@0: WindowEvent e; truelight@0: tron@2639: if (_special_mouse_mode != WSM_DRAGDROP) return true; truelight@0: tron@2639: if (_left_button_down) return false; truelight@158: truelight@0: w = GetCallbackWnd(); truelight@0: truelight@0: ResetObjectToPlace(); truelight@0: tron@2639: if (w != NULL) { belugas@6928: /* send an event in client coordinates. */ truelight@0: e.event = WE_DRAGDROP; belugas@4634: e.we.dragdrop.pt.x = _cursor.pos.x - w->left; belugas@4634: e.we.dragdrop.pt.y = _cursor.pos.y - w->top; belugas@4634: e.we.dragdrop.widget = GetWidgetFromPos(w, e.we.dragdrop.pt.x, e.we.dragdrop.pt.y); truelight@0: w->wndproc(w, &e); truelight@0: } truelight@0: return false; truelight@0: } truelight@0: rubidium@6573: static bool HandlePopupMenu() truelight@0: { truelight@0: Window *w; truelight@0: WindowEvent e; truelight@0: tron@2639: if (!_popup_menu_active) return true; truelight@0: truelight@0: w = FindWindowById(WC_TOOLBAR_MENU, 0); truelight@0: if (w == NULL) { truelight@0: _popup_menu_active = false; truelight@0: return false; truelight@0: } truelight@0: truelight@0: if (_left_button_down) { truelight@0: e.event = WE_POPUPMENU_OVER; belugas@4634: e.we.popupmenu.pt = _cursor.pos; truelight@0: } else { truelight@0: _popup_menu_active = false; truelight@0: e.event = WE_POPUPMENU_SELECT; belugas@4634: e.we.popupmenu.pt = _cursor.pos; truelight@0: } truelight@0: darkvater@1038: w->wndproc(w, &e); darkvater@1038: truelight@0: return false; truelight@0: } truelight@0: rubidium@6573: static bool HandleMouseOver() truelight@543: { truelight@543: Window *w; truelight@543: WindowEvent e; truelight@543: static Window *last_w = NULL; truelight@543: truelight@543: w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y); truelight@543: belugas@6928: /* We changed window, put a MOUSEOVER event to the last window */ tron@2639: if (last_w != NULL && last_w != w) { truelight@543: e.event = WE_MOUSEOVER; belugas@4634: e.we.mouseover.pt.x = -1; belugas@4634: e.we.mouseover.pt.y = -1; tron@2639: if (last_w->wndproc) last_w->wndproc(last_w, &e); truelight@543: } truelight@543: last_w = w; truelight@543: tron@2639: if (w != NULL) { belugas@6928: /* send an event in client coordinates. */ truelight@543: e.event = WE_MOUSEOVER; belugas@4634: e.we.mouseover.pt.x = _cursor.pos.x - w->left; belugas@4634: e.we.mouseover.pt.y = _cursor.pos.y - w->top; truelight@543: if (w->widget != NULL) { belugas@4634: e.we.mouseover.widget = GetWidgetFromPos(w, e.we.mouseover.pt.x, e.we.mouseover.pt.y); truelight@543: } truelight@543: w->wndproc(w, &e); truelight@543: } truelight@543: belugas@6928: /* Mouseover never stops execution */ truelight@543: return true; truelight@543: } truelight@543: Darkvater@5268: /** 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.) Darkvater@5268: * @param y delta y-size of changed window */ Darkvater@5268: void ResizeWindow(Window *w, int x, int y) Darkvater@5268: { Darkvater@5268: Widget *wi; Darkvater@5268: bool resize_height = false; Darkvater@5268: bool resize_width = false; Darkvater@5268: Darkvater@5268: if (x == 0 && y == 0) return; Darkvater@5268: Darkvater@5268: SetWindowDirty(w); Darkvater@5268: for (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: Darkvater@5268: SetWindowDirty(w); 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 tron@350: const Window *v; tron@350: int x; tron@350: int y; tron@350: int nx; tron@350: int ny; 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: truelight@0: SetWindowDirty(w); truelight@0: tron@350: x = _cursor.pos.x + _drag_delta.x; tron@350: y = _cursor.pos.y + _drag_delta.y; tron@350: nx = x; tron@350: ny = y; tron@350: tron@350: if (_patches.window_snap_radius != 0) { Darkvater@5137: Window* const *vz; Darkvater@5124: tron@353: int hsnap = _patches.window_snap_radius; tron@353: int vsnap = _patches.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 */ tron@370: nx = clamp(nx, 13 - t->right, _screen.width - 13 - t->left); tron@350: 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 */ tron@370: 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: truelight@0: SetWindowDirty(w); truelight@0: return false; truelight@867: } else if (w->flags4 & WF_SIZING) { truelight@867: WindowEvent e; 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; Darkvater@1657: SetWindowDirty(w); 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: truelight@867: e.event = WE_RESIZE; belugas@4634: e.we.sizing.size.x = x + w->width; belugas@4634: e.we.sizing.size.y = y + w->height; belugas@4634: e.we.sizing.diff.x = x; belugas@4634: e.we.sizing.diff.y = y; truelight@867: w->wndproc(w, &e); truelight@867: return false; truelight@0: } truelight@0: } truelight@0: truelight@0: _dragging_window = false; truelight@0: return false; truelight@0: } truelight@0: 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: 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: int i; truelight@0: int pos; truelight@0: Scrollbar *sb; 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; truelight@0: SetWindowDirty(w); truelight@0: break; truelight@158: } truelight@0: 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. */ truelight@0: 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; truelight@0: SetWindowDirty(w); 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: { truelight@4337: WindowEvent e; truelight@0: Window *w; truelight@0: bjarni@6615: bool scrollwheel_scrolling = _patches.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0); bjarni@6615: tron@2639: if (!_scrolling_viewport) return true; truelight@0: truelight@4335: w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y); truelight@4335: bjarni@6615: if (!(_right_button_down || scrollwheel_scrolling) || w == NULL) { truelight@0: _cursor.fix_at = false; truelight@0: _scrolling_viewport = false; truelight@0: return true; truelight@0: } truelight@158: tron@2680: if (_patches.reverse_scroll) { belugas@4634: e.we.scroll.delta.x = -_cursor.delta.x; belugas@4634: e.we.scroll.delta.y = -_cursor.delta.y; tron@2680: } else { belugas@4634: e.we.scroll.delta.x = _cursor.delta.x; belugas@4634: e.we.scroll.delta.y = _cursor.delta.y; tron@2680: } tron@2680: bjarni@6615: if (scrollwheel_scrolling) { bjarni@6615: /* We are using scrollwheels for scrolling */ bjarni@6615: e.we.scroll.delta.x = _cursor.h_wheel; bjarni@6615: e.we.scroll.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 */ truelight@4337: e.event = WE_SCROLL; truelight@4337: w->wndproc(w, &e); 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; Darkvater@5137: Window* const *wz; Darkvater@5137: Window* const *uz; 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: Darkvater@5124: wz = FindWindowZPosition(w); Darkvater@5124: for (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; Darkvater@5667: SetWindowDirty(u); 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@1648: /** Send a message from one window to another. The receiving window is found by belugas@6939: * @param w Window pointer pointing to the other window darkvater@1648: * @param msg Specifies the message to be sent darkvater@1648: * @param wparam Specifies additional message-specific information darkvater@1648: * @param lparam Specifies additional message-specific information darkvater@1648: */ belugas@4171: static void SendWindowMessageW(Window *w, uint msg, uint wparam, uint lparam) darkvater@1648: { darkvater@1648: WindowEvent e; darkvater@1648: belugas@4634: e.event = WE_MESSAGE; belugas@4634: e.we.message.msg = msg; belugas@4634: e.we.message.wparam = wparam; belugas@4634: e.we.message.lparam = lparam; darkvater@1648: darkvater@1648: w->wndproc(w, &e); darkvater@1648: } darkvater@1648: darkvater@1648: /** Send a message from one window to another. The receiving window is found by belugas@6928: * @param wnd_class see WindowClass class AND belugas@6928: * @param wnd_num see WindowNumber number, mostly 0 darkvater@1648: * @param msg Specifies the message to be sent darkvater@1648: * @param wparam Specifies additional message-specific information darkvater@1648: * @param lparam Specifies additional message-specific information darkvater@1648: */ rubidium@5838: void SendWindowMessage(WindowClass wnd_class, WindowNumber wnd_num, int msg, int wparam, int lparam) darkvater@1648: { darkvater@1648: Window *w = FindWindowById(wnd_class, wnd_num); darkvater@1648: if (w != NULL) SendWindowMessageW(w, msg, wparam, lparam); darkvater@1648: } darkvater@1648: Darkvater@5043: /** Send a message from one window to another. The message will be sent Darkvater@5043: * to ALL windows of the windowclass specified in the first parameter belugas@6928: * @param wnd_class see WindowClass class Darkvater@5043: * @param msg Specifies the message to be sent Darkvater@5043: * @param wparam Specifies additional message-specific information Darkvater@5043: * @param lparam Specifies additional message-specific information Darkvater@5043: */ rubidium@5838: void SendWindowMessageClass(WindowClass wnd_class, int msg, int wparam, int lparam) Darkvater@5043: { Darkvater@5124: Window* const *wz; Darkvater@5043: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: if ((*wz)->window_class == wnd_class) SendWindowMessageW(*wz, msg, wparam, lparam); Darkvater@5043: } Darkvater@5043: } Darkvater@5043: Darkvater@5086: /** Handle keyboard input. Darkvater@5086: * @param key Lower 8 bits contain the ASCII character, the higher Darkvater@5086: * 16 bits the keycode */ Darkvater@5086: void HandleKeypress(uint32 key) truelight@0: { Darkvater@5124: Window* const *wz; belugas@4634: WindowEvent e; rubidium@4549: /* Stores if a window with a textfield for typing is open rubidium@4549: * If this is the case, keypress events are only passed to windows with text fields and rubidium@4549: * to thein this main toolbar. */ dominik@651: bool query_open = false; truelight@158: Darkvater@5086: /* Darkvater@5086: * During the generation of the world, there might be Darkvater@5086: * another thread that is currently building for example Darkvater@5086: * a road. To not interfere with those tasks, we should Darkvater@5086: * NOT change the _current_player here. Darkvater@5086: * Darkvater@5086: * This is not necessary either, as the only events that Darkvater@5086: * can be handled are the 'close application' events Darkvater@5086: */ Darkvater@5086: if (!IsGeneratingWorld()) _current_player = _local_player; Darkvater@5086: belugas@6928: /* Setup event */ belugas@4634: e.event = WE_KEYPRESS; peter1138@5108: e.we.keypress.key = GB(key, 0, 16); Darkvater@5089: e.we.keypress.keycode = GB(key, 16, 16); belugas@4634: e.we.keypress.cont = true; truelight@0: belugas@6928: /* check if we have a query string window open before allowing hotkeys */ truelight@4300: if (FindWindowById(WC_QUERY_STRING, 0) != NULL || truelight@4300: FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL || truelight@4300: FindWindowById(WC_GENERATE_LANDSCAPE, 0) != NULL || truelight@4300: FindWindowById(WC_CONSOLE, 0) != NULL || truelight@4300: FindWindowById(WC_SAVELOAD, 0) != NULL) { dominik@651: query_open = true; tron@2639: } dominik@651: belugas@6928: /* Call the event, start with the uppermost window. */ Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: Window *w = *--wz; Darkvater@5124: belugas@6928: /* if a query window is open, only call the event for certain window types */ tron@2639: if (query_open && tron@2639: w->window_class != WC_QUERY_STRING && tron@2639: w->window_class != WC_SEND_NETWORK_MSG && truelight@4300: w->window_class != WC_GENERATE_LANDSCAPE && tron@2639: w->window_class != WC_CONSOLE && tron@2639: w->window_class != WC_SAVELOAD) { dominik@651: continue; tron@2639: } belugas@4634: w->wndproc(w, &e); belugas@4634: if (!e.we.keypress.cont) break; truelight@0: } Darkvater@1637: belugas@4634: if (e.we.keypress.cont) { Darkvater@5124: Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0); belugas@6928: /* When there is no toolbar w is null, check for that */ belugas@4634: if (w != NULL) w->wndproc(w, &e); matthijs@1642: } truelight@0: } truelight@0: rubidium@6573: extern void UpdateTileSelection(); rubidium@6573: extern bool VpHandlePlaceSizingDrag(); truelight@0: Darkvater@5090: static int _input_events_this_tick = 0; Darkvater@5090: rubidium@6573: static void HandleAutoscroll() Darkvater@5090: { Darkvater@5090: Window *w; Darkvater@5090: ViewPort *vp; Darkvater@5090: int x = _cursor.pos.x; Darkvater@5090: int y = _cursor.pos.y; 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: Darkvater@5090: if (_patches.autoscroll && _game_mode != GM_MENU && !IsGeneratingWorld()) { Darkvater@5090: w = FindWindowFromPt(x, y); Darkvater@5090: if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL) return; Darkvater@5090: vp = IsPtInWindowViewport(w, x, y); Darkvater@5090: if (vp != NULL) { Darkvater@5090: x -= vp->left; Darkvater@5090: y -= vp->top; belugas@6928: /* here allows scrolling in both x and y axis */ Darkvater@5090: #define scrollspeed 3 Darkvater@5090: if (x - 15 < 0) { Darkvater@5090: WP(w, vp_d).scrollpos_x += (x - 15) * scrollspeed << vp->zoom; Darkvater@5090: } else if (15 - (vp->width - x) > 0) { Darkvater@5090: WP(w, vp_d).scrollpos_x += (15 - (vp->width - x)) * scrollspeed << vp->zoom; Darkvater@5090: } Darkvater@5090: if (y - 15 < 0) { Darkvater@5090: WP(w, vp_d).scrollpos_y += (y - 15) * scrollspeed << vp->zoom; Darkvater@5090: } else if (15 - (vp->height - y) > 0) { Darkvater@5090: WP(w,vp_d).scrollpos_y += (15 - (vp->height - y)) * scrollspeed << vp->zoom; Darkvater@5090: } Darkvater@5090: #undef scrollspeed Darkvater@5090: } Darkvater@5090: } Darkvater@5090: } Darkvater@5090: Darkvater@5090: void MouseLoop(int click, int mousewheel) truelight@0: { truelight@0: int x,y; truelight@0: Window *w; truelight@0: ViewPort *vp; bjarni@6615: bool scrollwheel_scrolling = _patches.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0); truelight@158: truelight@0: DecreaseWindowCounters(); truelight@0: HandlePlacePresize(); truelight@0: UpdateTileSelection(); tron@2639: if (!VpHandlePlaceSizingDrag()) return; tron@2639: if (!HandleDragDrop()) return; tron@2639: if (!HandlePopupMenu()) return; tron@2639: if (!HandleWindowDragging()) return; tron@2639: if (!HandleScrollbarScrolling()) return; tron@2639: if (!HandleViewportScroll()) return; tron@2639: if (!HandleMouseOver()) return; truelight@543: truelight@0: x = _cursor.pos.x; truelight@0: y = _cursor.pos.y; truelight@0: bjarni@6615: if (click == 0 && mousewheel == 0 && !scrollwheel_scrolling) return; truelight@0: truelight@0: w = FindWindowFromPt(x, y); tron@2639: if (w == NULL) return; Darkvater@5667: if (!MaybeBringWindowToFront(w)) return; truelight@0: 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) { bjarni@6622: if (_patches.scrollwheel_scrolling == 0) { bjarni@6622: /* Scrollwheel is in zoom mode. Make the zoom event. */ bjarni@6622: WindowEvent e; truelight@4337: bjarni@6622: /* Send WE_MOUSEWHEEL event to window */ bjarni@6622: e.event = WE_MOUSEWHEEL; bjarni@6622: e.we.wheel.wheel = mousewheel; bjarni@6622: w->wndproc(w, &e); 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) { bjarni@6615: if (scrollwheel_scrolling) click = 2; // we are using the scrollwheel in a viewport, so we emulate right mouse button truelight@4337: switch (click) { truelight@4337: case 1: Darkvater@5568: DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite); truelight@4337: if (_thd.place_mode != 0 && 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: truelight@4337: if (_thd.place_mode == 0) { truelight@4337: HandleViewportClicked(vp, x, y); truelight@4337: } else { truelight@4337: PlaceObject(); truelight@4337: } truelight@4337: break; truelight@4337: truelight@4337: case 2: 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@0: } truelight@0: } else { tron@2631: switch (click) { tron@2631: case 1: DispatchLeftClickEvent(w, x - w->left, y - w->top); break; 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 */ tron@2631: case 2: DispatchRightClickEvent(w, x - w->left, y - w->top); break; tron@2631: } truelight@0: } truelight@0: } truelight@0: rubidium@6573: void HandleMouseEvents() pasky@1570: { pasky@1570: int click; pasky@1570: int mousewheel; 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? */ pasky@1570: click = 0; pasky@1570: if (_left_button_down && !_left_button_clicked) { pasky@1570: _left_button_clicked = true; pasky@1570: click = 1; Darkvater@5090: _input_events_this_tick++; pasky@1570: } else if (_right_button_clicked) { pasky@1570: _right_button_clicked = false; pasky@1570: click = 2; Darkvater@5090: _input_events_this_tick++; pasky@1570: } pasky@1570: pasky@1570: 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@6573: void InputLoop() Darkvater@5090: { Darkvater@5090: HandleMouseEvents(); Darkvater@5090: HandleAutoscroll(); Darkvater@5090: } Darkvater@5090: 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;) { Darkvater@5124: CallWindowEventNP(*--wz, WE_4); 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: Darkvater@5120: if (!(w->flags4 & WF_WHITE_BORDER_MASK)) SetWindowDirty(w); 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: } truelight@543: DrawTextMessage(); belugas@6928: /* Redraw mouse cursor in case it was hidden */ truelight@0: DrawMouseCursor(); truelight@158: } truelight@0: truelight@0: Darkvater@2436: int GetMenuItemIndex(const Window *w, int x, int y) truelight@0: { truelight@0: if ((x -= w->left) >= 0 && x < w->width && (y -= w->top + 1) >= 0) { truelight@0: y /= 10; truelight@0: tron@2639: if (y < WP(w, const menu_d).item_count && tron@2639: !HASBIT(WP(w, const menu_d).disabled_items, y)) { truelight@0: return y; tron@2639: } truelight@0: } truelight@0: return -1; truelight@0: } truelight@0: 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; tron@2639: if (w->window_class == cls && w->window_number == number) SetWindowDirty(w); truelight@0: } truelight@0: } truelight@0: belugas@4171: void InvalidateWidget(const Window *w, byte widget_index) truelight@0: { truelight@0: const Widget *wi = &w->widget[widget_index]; Darkvater@1867: Darkvater@1867: /* Don't redraw the window if the widget is invisible or of no-type */ rubidium@5236: if (wi->type == WWT_EMPTY || IsWindowWidgetHidden(w, widget_index)) return; Darkvater@1867: Darkvater@1867: SetDirtyBlocks(w->left + wi->left, w->top + wi->top, w->left + wi->right + 1, w->top + wi->bottom + 1); truelight@0: } truelight@0: 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) { truelight@0: InvalidateWidget(w, widget_index); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@2788: void InvalidateWindowClasses(WindowClass cls) truelight@0: { Darkvater@5137: Window* const *wz; tron@2639: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: if ((*wz)->window_class == cls) SetWindowDirty(*wz); truelight@0: } truelight@0: } truelight@0: bjarni@4766: void InvalidateThisWindowData(Window *w) bjarni@4766: { bjarni@4766: CallWindowEventNP(w, WE_INVALIDATE_DATA); Darkvater@5198: SetWindowDirty(w); bjarni@4766: } bjarni@4766: bjarni@4739: void InvalidateWindowData(WindowClass cls, WindowNumber number) bjarni@4739: { Darkvater@5124: Window* const *wz; bjarni@4739: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: Window *w = *wz; bjarni@4766: if (w->window_class == cls && w->window_number == number) InvalidateThisWindowData(w); bjarni@4766: } bjarni@4766: } bjarni@4766: bjarni@4766: void InvalidateWindowClassesData(WindowClass cls) bjarni@4766: { Darkvater@5124: Window* const *wz; bjarni@4766: Darkvater@5124: FOR_ALL_WINDOWS(wz) { Darkvater@5124: if ((*wz)->window_class == cls) InvalidateThisWindowData(*wz); bjarni@4739: } bjarni@4739: } truelight@0: rubidium@6573: void CallWindowTickEvent() truelight@0: { Darkvater@5124: Window* const *wz; tron@2639: Darkvater@5124: for (wz = _last_z_window; wz != _z_windows;) { Darkvater@5124: CallWindowEventNP(*--wz, WE_TICK); truelight@0: } truelight@0: } truelight@0: 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: truelight@0: DeleteWindow(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) { Darkvater@5124: DeleteWindow(*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: 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: darkvater@68: switch (_patches.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: 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@0: vp->virtual_width = neww << vp->zoom; truelight@0: vp->virtual_height = 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: 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: 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: 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: tron@2989: default: tron@2989: left = w->left; tron@2989: if (left + (w->width >> 1) >= neww) left = neww - w->width; tron@2989: top = w->top; tron@2989: if (top + (w->height >> 1) >= newh) top = newh - w->height; tron@2989: 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: }