tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" 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@0: tron@350: // delta between mouse cursor and upper left corner of dragged window tron@350: static Point _drag_delta; tron@350: truelight@0: void HandleButtonClick(Window *w, byte widget) truelight@0: { truelight@0: w->click_state |= (1 << widget); truelight@0: w->flags4 |= 5 << WF_TIMEOUT_SHL; truelight@0: InvalidateWidget(w, widget); truelight@0: } truelight@0: tron@2817: tron@2817: static Window* StartWindowDrag(Window* w); tron@2817: static Window* StartWindowSizing(Window* w); tron@2817: tron@2596: static void DispatchLeftClickEvent(Window* w, int x, int y) tron@2596: { truelight@0: WindowEvent e; truelight@0: const Widget *wi; truelight@0: truelight@0: e.click.pt.x = x; truelight@0: e.click.pt.y = y; truelight@0: e.event = WE_CLICK; truelight@0: truelight@0: if (w->desc_flags & WDF_DEF_WIDGET) { truelight@158: e.click.widget = GetWidgetFromPos(w, x, y); truelight@0: if (e.click.widget < 0) return; /* exit if clicked outside of widgets */ truelight@158: truelight@0: wi = &w->widget[e.click.widget]; darkvater@222: darkvater@211: /* don't allow any interaction if the button has been disabled */ darkvater@211: if (HASBIT(w->disabled_state, e.click.widget)) darkvater@211: return; truelight@0: truelight@0: if (wi->type & 0xE0) { darkvater@211: /* special widget handling for buttons*/ truelight@0: switch(wi->type) { darkvater@211: case WWT_IMGBTN | WWB_PUSHBUTTON: /* WWT_PUSHIMGBTN */ darkvater@211: case WWT_TEXTBTN | WWB_PUSHBUTTON: /* WWT_PUSHTXTBTN */ truelight@0: HandleButtonClick(w, e.click.widget); truelight@0: break; truelight@0: case WWT_NODISTXTBTN: truelight@158: break; truelight@0: } bjarni@842: } else if (wi->type == WWT_SCROLLBAR || wi->type == WWT_SCROLL2BAR || wi->type == WWT_HSCROLLBAR) { truelight@0: ScrollbarClickHandler(w, wi, e.click.pt.x, e.click.pt.y); truelight@0: } truelight@0: truelight@0: if (w->desc_flags & WDF_STD_BTN) { darkvater@1038: if (e.click.widget == 0) { /* 'X' */ truelight@867: DeleteWindow(w); celestar@984: return; tron@1109: } tron@1109: darkvater@1112: if (e.click.widget == 1) { /* 'Title bar' */ darkvater@1112: StartWindowDrag(w); // if not return then w = StartWindowDrag(w); to get correct pointer darkvater@1112: return; darkvater@1112: } truelight@0: } truelight@867: darkvater@1112: if (w->desc_flags & WDF_RESIZABLE && wi->type == WWT_RESIZEBOX) { darkvater@1112: StartWindowSizing(w); // if not return then w = StartWindowSizing(w); to get correct pointer 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; truelight@867: InvalidateWidget(w, e.click.widget); darkvater@1112: return; darkvater@682: } truelight@0: } darkvater@1038: darkvater@1038: w->wndproc(w, &e); truelight@0: } truelight@0: tron@2596: 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) { truelight@0: e.click.widget = GetWidgetFromPos(w, x, y); truelight@0: if (e.click.widget < 0) truelight@0: return; /* exit if clicked outside of widgets */ truelight@0: truelight@0: if (w->widget[e.click.widget].tooltips != 0) { truelight@0: GuiShowTooltips(w->widget[e.click.widget].tooltips); truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: e.event = WE_RCLICK; truelight@0: e.click.pt.x = x; truelight@0: e.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: */ tron@2596: 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: truelight@0: tron@2817: static void DrawOverlappedWindow(Window* w, int left, int top, int right, int bottom); tron@2817: truelight@0: void DrawOverlappedWindowForAll(int left, int top, int right, int bottom) truelight@0: { truelight@0: Window *w; truelight@0: DrawPixelInfo bk; truelight@0: _cur_dpi = &bk; truelight@0: tron@2639: for (w = _windows; w != _last_window; w++) { 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) { tron@2639: DrawOverlappedWindow(w, left, top, right, bottom); tron@2639: } truelight@0: } truelight@0: } truelight@0: tron@2817: static void DrawOverlappedWindow(Window* w, int left, int top, int right, int bottom) truelight@0: { tron@2630: const Window* v = w; truelight@0: int x; truelight@0: truelight@0: while (++v != _last_window) { 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) { truelight@0: if (left < (x=v->left)) { truelight@0: DrawOverlappedWindow(w, left, top, x, bottom); truelight@0: DrawOverlappedWindow(w, x, top, right, bottom); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (right > (x=v->left + v->width)) { truelight@0: DrawOverlappedWindow(w, left, top, x, bottom); truelight@0: DrawOverlappedWindow(w, x, top, right, bottom); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (top < (x=v->top)) { truelight@0: DrawOverlappedWindow(w, left, top, right, x); truelight@0: DrawOverlappedWindow(w, left, x, right, bottom); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (bottom > (x=v->top + v->height)) { truelight@0: DrawOverlappedWindow(w, left, top, right, x); truelight@0: DrawOverlappedWindow(w, 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; truelight@0: dp->left = left - w->left; truelight@0: dp->top = top - w->top; truelight@0: dp->pitch = _screen.pitch; truelight@0: dp->dst_ptr = _screen.dst_ptr + top * _screen.pitch + left; truelight@0: dp->zoom = 0; truelight@0: CallWindowEventNP(w, 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: tron@2549: 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: truelight@0: void DeleteWindow(Window *w) truelight@0: { truelight@0: WindowClass wc; truelight@0: WindowNumber wn; truelight@0: ViewPort *vp; truelight@0: Window *v; truelight@0: int count; truelight@0: tron@2639: if (w == NULL) return; truelight@0: truelight@0: if (_thd.place_mode != 0 && _thd.window_class == w->window_class && _thd.window_number == w->window_number) { truelight@0: ResetObjectToPlace(); truelight@0: } truelight@0: truelight@0: wc = w->window_class; truelight@0: wn = w->window_number; truelight@0: truelight@0: CallWindowEventNP(w, WE_DESTROY); truelight@0: truelight@0: w = FindWindowById(wc, wn); truelight@0: truelight@0: vp = w->viewport; truelight@0: w->viewport = NULL; truelight@0: if (vp != NULL) { truelight@0: _active_viewports &= ~(1 << (vp - _viewports)); truelight@0: vp->width = 0; truelight@0: } truelight@0: truelight@0: SetWindowDirty(w); truelight@0: truelight@867: free(w->widget); truelight@867: truelight@0: v = --_last_window; truelight@0: count = (byte*)v - (byte*)w; tron@1033: memmove(w, w + 1, count); truelight@0: } truelight@0: truelight@0: Window *FindWindowById(WindowClass cls, WindowNumber number) truelight@0: { truelight@0: Window *w; truelight@0: tron@2639: for (w = _windows; w != _last_window; w++) { 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@999: Window *w; tron@2639: darkvater@999: for (w = _windows; w != _last_window;) { darkvater@999: if (w->window_class == cls) { darkvater@999: DeleteWindow(w); darkvater@999: w = _windows; tron@2639: } else { darkvater@999: w++; tron@2639: } darkvater@999: } darkvater@999: } darkvater@999: tron@2817: tron@2817: static Window* BringWindowToFront(Window* w); tron@2817: 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; truelight@0: SetWindowDirty(w); tron@388: w = BringWindowToFront(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@1648: * @param w window that is put into the foreground darkvater@1648: */ tron@2817: static Window* BringWindowToFront(Window* w) truelight@0: { truelight@0: Window *v; tron@386: Window temp; truelight@0: truelight@0: v = _last_window; truelight@0: do { tron@2639: if (--v < _windows) return w; darkvater@1648: } while (IsVitalWindow(v)); truelight@0: tron@2639: if (w == v) return w; truelight@0: truelight@0: assert(w < v); truelight@0: tron@386: temp = *w; tron@386: memmove(w, w + 1, (v - w) * sizeof(Window)); tron@386: *v = temp; truelight@0: tron@386: SetWindowDirty(v); truelight@0: tron@386: return v; 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: */ darkvater@763: static Window *FindDeletableWindow(void) darkvater@763: { darkvater@763: Window *w; tron@2639: darkvater@763: for (w = _windows; w < endof(_windows); w++) { 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@1648: * @see FindDeletableWindow() darkvater@1648: * @return w Pointer to the window that is being deleted darkvater@1648: */ darkvater@763: static Window *ForceFindDeletableWindow(void) darkvater@763: { darkvater@763: Window *w; tron@2639: darkvater@763: for (w = _windows;; w++) { darkvater@763: assert(w < _last_window); tron@2639: if (w->window_class != WC_MAIN_WINDOW && !IsVitalWindow(w)) return w; darkvater@763: } darkvater@763: } darkvater@763: tron@2596: bool IsWindowOfPrototype(const Window* w, const Widget* widget) truelight@867: { truelight@867: return (w->original_widget == widget); truelight@867: } truelight@867: darkvater@1648: /* Copies 'widget' to 'w->widget' to allow for resizable windows */ 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; tron@2639: const Widget* wi; truelight@867: tron@2639: for (wi = widget; wi->type != WWT_LAST; wi++) index++; tron@2639: tron@2639: w->widget = realloc(w->widget, sizeof(*w->widget) * index); tron@2639: memcpy(w->widget, widget, sizeof(*w->widget) * index); tron@2639: } else { truelight@867: w->widget = NULL; tron@2639: } truelight@867: } truelight@867: darkvater@1648: /** Open a new window. If there is no space for a new window, close an open darkvater@1648: * window. Try to avoid stickied windows, but if there is no else, close one of darkvater@1648: * those as well. Then make sure all created windows are below some always-on-top darkvater@1648: * ones. Finally set all variables and call the WE_CREATE event darkvater@1648: * @param x offset in pixels from the left of the screen darkvater@1648: * @param y offset in pixels from the top of the screen darkvater@1648: * @param width width in pixels of the window darkvater@1648: * @param height height in pixels of the window darkvater@1648: * @param *proc @see WindowProc function to call when any messages/updates happen to the window darkvater@1648: * @param cls @see WindowClass class of the window, used for identification and grouping darkvater@1648: * @param *widget @see Widget pointer to the window layout and various elements darkvater@1648: * @return @see Window pointer of the newly created window darkvater@1648: */ truelight@0: Window *AllocateWindow( darkvater@1648: int x, int y, int width, int height, darkvater@1648: WindowProc *proc, WindowClass cls, const Widget *widget) truelight@0: { darkvater@1648: Window *w = _last_window; // last window keeps track of the highest open window truelight@0: darkvater@1648: // We have run out of windows, close one and use that as the place for our new one truelight@0: if (w >= endof(_windows)) { darkvater@763: w = FindDeletableWindow(); truelight@0: tron@2639: if (w == NULL) w = ForceFindDeletableWindow(); truelight@158: darkvater@763: DeleteWindow(w); darkvater@763: w = _last_window; truelight@0: } truelight@0: darkvater@1648: /* XXX - This very strange construction makes sure that the chatbar is always darkvater@1648: * on top of other windows. Why? It is created as last_window (so, on top). darkvater@1648: * Any other window will go below toolbar/statusbar/news window, which implicitely darkvater@1648: * also means it is below the chatbar. Very likely needs heavy improvement darkvater@1648: * to de-braindeadize */ darkvater@1648: if (w != _windows && cls != WC_SEND_NETWORK_MSG) { truelight@0: Window *v; truelight@0: darkvater@1648: /* XXX - if not this order (toolbar/statusbar and then news), game would darkvater@1648: * crash because it will try to copy a negative size for the news-window. darkvater@1648: * Eg. window was already moved BELOW news (which is below toolbar/statusbar) darkvater@1648: * and now needs to move below those too. That is a negative move. */ truelight@0: v = FindWindowById(WC_MAIN_TOOLBAR, 0); darkvater@1648: if (v != NULL) { truelight@0: memmove(v+1, v, (byte*)w - (byte*)v); truelight@0: w = v; truelight@0: } truelight@0: truelight@0: v = FindWindowById(WC_STATUS_BAR, 0); darkvater@1648: if (v != NULL) { darkvater@1648: memmove(v+1, v, (byte*)w - (byte*)v); darkvater@1648: w = v; darkvater@1648: } darkvater@1648: darkvater@1648: v = FindWindowById(WC_NEWS_WINDOW, 0); darkvater@1648: if (v != NULL) { truelight@0: memmove(v+1, v, (byte*)w - (byte*)v); truelight@0: w = v; truelight@0: } truelight@0: } truelight@0: darkvater@1648: // Set up window properties darkvater@1648: memset(w, 0, sizeof(Window)); 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; truelight@0: truelight@0: _last_window++; truelight@0: truelight@0: SetWindowDirty(w); truelight@158: dominik@116: CallWindowEventNP(w, WE_CREATE); truelight@0: truelight@0: return w; truelight@0: } truelight@0: truelight@0: truelight@0: typedef struct SizeRect { truelight@0: int left,top,width,height; truelight@0: } SizeRect; truelight@0: truelight@0: truelight@0: static SizeRect _awap_r; truelight@0: truelight@0: static bool IsGoodAutoPlace1(int left, int top) truelight@0: { truelight@0: int right,bottom; truelight@0: Window *w; truelight@0: truelight@0: _awap_r.left= left; truelight@0: _awap_r.top = top; truelight@0: right = _awap_r.width + left; truelight@0: bottom = _awap_r.height + top; truelight@0: truelight@0: if (left < 0 || top < 22 || right > _screen.width || bottom > _screen.height) truelight@0: return false; truelight@0: truelight@158: // Make sure it is not obscured by any window. tron@2639: for (w = _windows; w != _last_window; w++) { 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: truelight@0: return true; truelight@0: } truelight@0: truelight@0: static bool IsGoodAutoPlace2(int left, int top) truelight@0: { truelight@0: int width,height; truelight@0: Window *w; truelight@0: truelight@0: _awap_r.left= left; truelight@0: _awap_r.top = top; truelight@0: width = _awap_r.width; truelight@0: height = _awap_r.height; truelight@0: truelight@0: if (left < -(width>>2) || left > _screen.width - (width>>1)) truelight@0: return false; truelight@0: if (top < 22 || top > _screen.height - (height>>2)) truelight@0: return false; truelight@0: truelight@158: // Make sure it is not obscured by any window. tron@2639: for (w = _windows; w != _last_window; w++) { 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: truelight@0: return true; truelight@0: } truelight@0: tron@1095: static Point GetAutoPlacePosition(int width, int height) tron@1095: { truelight@0: Window *w; truelight@0: Point pt; truelight@0: truelight@0: _awap_r.width = width; truelight@0: _awap_r.height = height; truelight@0: truelight@0: if (IsGoodAutoPlace1(0, 24)) goto ok_pos; truelight@0: tron@2639: for (w = _windows; w != _last_window; w++) { tron@2639: if (w->window_class == WC_MAIN_WINDOW) continue; truelight@0: truelight@0: if (IsGoodAutoPlace1(w->left+w->width+2,w->top)) goto ok_pos; truelight@0: if (IsGoodAutoPlace1(w->left- width-2,w->top)) goto ok_pos; truelight@0: if (IsGoodAutoPlace1(w->left,w->top+w->height+2)) goto ok_pos; truelight@0: if (IsGoodAutoPlace1(w->left,w->top- height-2)) goto ok_pos; truelight@0: if (IsGoodAutoPlace1(w->left+w->width+2,w->top+w->height-height)) goto ok_pos; truelight@0: if (IsGoodAutoPlace1(w->left- width-2,w->top+w->height-height)) goto ok_pos; truelight@0: if (IsGoodAutoPlace1(w->left+w->width-width,w->top+w->height+2)) goto ok_pos; truelight@0: if (IsGoodAutoPlace1(w->left+w->width-width,w->top- height-2)) goto ok_pos; truelight@158: } truelight@158: tron@2639: for (w = _windows; w != _last_window; w++) { tron@2639: if (w->window_class == WC_MAIN_WINDOW) continue; truelight@0: truelight@0: if (IsGoodAutoPlace2(w->left+w->width+2,w->top)) goto ok_pos; truelight@0: if (IsGoodAutoPlace2(w->left- width-2,w->top)) goto ok_pos; truelight@0: if (IsGoodAutoPlace2(w->left,w->top+w->height+2)) goto ok_pos; truelight@0: if (IsGoodAutoPlace2(w->left,w->top- height-2)) goto ok_pos; truelight@0: } truelight@0: truelight@0: { truelight@0: int left=0,top=24; truelight@158: truelight@0: restart:; tron@2639: for (w = _windows; w != _last_window; w++) { 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@158: truelight@0: ok_pos:; truelight@0: pt.x = _awap_r.left; truelight@0: pt.y = _awap_r.top; truelight@0: return pt; truelight@0: } truelight@0: truelight@0: truelight@0: Window *AllocateWindowDescFront(const WindowDesc *desc, int value) truelight@0: { truelight@0: Window *w; truelight@0: tron@2639: if (BringWindowToFrontById(desc->cls, value)) return NULL; truelight@0: w = AllocateWindowDesc(desc); truelight@0: w->window_number = value; truelight@0: return w; truelight@0: } truelight@0: truelight@0: Window *AllocateWindowDesc(const WindowDesc *desc) truelight@0: { truelight@0: Point pt; truelight@0: Window *w; truelight@0: truelight@158: if (desc->parent_cls != WC_MAIN_WINDOW && truelight@0: (w = FindWindowById(desc->parent_cls, _alloc_wnd_parent_num), _alloc_wnd_parent_num=0, w) != NULL && truelight@0: w->left < _screen.width-20 && w->left > -60 && w->top < _screen.height-20) { truelight@0: pt.x = w->left + 10; truelight@0: if (pt.x > _screen.width + 10 - desc->width) truelight@0: pt.x = (_screen.width + 10 - desc->width) - 20; truelight@0: pt.y = w->top + 10; tron@2026: } else if (desc->cls == WC_BUILD_TOOLBAR) { // open Build Toolbars aligned darkvater@68: /* Override the position if a toolbar is opened according to the place of the maintoolbar darkvater@68: * The main toolbar (WC_MAIN_TOOLBAR) is 640px in width */ darkvater@68: switch (_patches.toolbar_pos) { tron@2026: case 1: pt.x = ((_screen.width + 640) >> 1) - desc->width; break; tron@2026: case 2: pt.x = _screen.width - desc->width; break; tron@2026: default: pt.x = 640 - desc->width; darkvater@68: } darkvater@68: pt.y = desc->top; truelight@0: } else { truelight@0: pt.x = desc->left; truelight@0: pt.y = desc->top; truelight@0: if (pt.x == WDP_AUTO) { truelight@0: pt = GetAutoPlacePosition(desc->width, desc->height); truelight@0: } else { truelight@0: if (pt.x == WDP_CENTER) pt.x = (_screen.width - desc->width) >> 1; truelight@0: if (pt.y == WDP_CENTER) pt.y = (_screen.height - desc->height) >> 1; dominik@649: else if(pt.y < 0) pt.y = _screen.height + pt.y; // if y is negative, it's from the bottom of the screen truelight@0: } truelight@0: } truelight@0: truelight@0: w = AllocateWindow(pt.x, pt.y, desc->width, desc->height, desc->proc, desc->cls, desc->widgets); truelight@0: w->desc_flags = desc->flags; truelight@0: return w; truelight@0: } truelight@0: truelight@0: Window *FindWindowFromPt(int x, int y) truelight@0: { truelight@0: Window *w; truelight@0: tron@2639: for (w = _last_window; w != _windows;) { truelight@0: --w; truelight@0: if (IS_INSIDE_1D(x, w->left, w->width) && tron@2639: 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: tron@1093: void InitWindowSystem(void) darkvater@152: { dominik@136: IConsoleClose(); Darkvater@1474: truelight@0: memset(&_windows, 0, sizeof(_windows)); truelight@0: _last_window = _windows; truelight@0: memset(_viewports, 0, sizeof(_viewports)); truelight@0: _active_viewports = 0; Darkvater@1397: _no_scroll = 0; truelight@0: } truelight@0: Darkvater@1474: void UnInitWindowSystem(void) Darkvater@1474: { Darkvater@1474: Window *w; Darkvater@1474: // delete all malloced widgets Darkvater@1474: for (w = _windows; w != _last_window; w++) { Darkvater@1474: free(w->widget); Darkvater@1474: w->widget = NULL; Darkvater@1474: } Darkvater@1474: } Darkvater@1474: Darkvater@1474: void ResetWindowSystem(void) 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: tron@1093: static void DecreaseWindowCounters(void) truelight@0: { truelight@0: Window *w; truelight@0: truelight@0: Darkvater@1474: for (w = _last_window; w != _windows;) { truelight@0: --w; truelight@0: // 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@1474: for (w = _last_window; w != _windows;) { truelight@0: --w; truelight@158: truelight@0: if (w->flags4&WF_TIMEOUT_MASK && !(--w->flags4&WF_TIMEOUT_MASK)) { truelight@0: CallWindowEventNP(w, WE_TIMEOUT); tron@2639: if (w->desc_flags & WDF_UNCLICK_BUTTONS) UnclickWindowButtons(w); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1093: Window *GetCallbackWnd(void) truelight@0: { truelight@0: return FindWindowById(_thd.window_class, _thd.window_number); truelight@0: } truelight@0: tron@1093: static void HandlePlacePresize(void) 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: truelight@0: e.place.pt = GetTileBelowCursor(); truelight@0: if (e.place.pt.x == -1) { truelight@0: _thd.selend.x = -1; truelight@0: return; truelight@0: } tron@1980: e.place.tile = TileVirtXY(e.place.pt.x, e.place.pt.y); truelight@0: e.event = WE_PLACE_PRESIZE; truelight@0: w->wndproc(w, &e); truelight@0: } truelight@0: tron@1093: static bool HandleDragDrop(void) 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) { truelight@0: // send an event in client coordinates. truelight@0: e.event = WE_DRAGDROP; truelight@0: e.dragdrop.pt.x = _cursor.pos.x - w->left; truelight@0: e.dragdrop.pt.y = _cursor.pos.y - w->top; truelight@0: e.dragdrop.widget = GetWidgetFromPos(w, e.dragdrop.pt.x, e.dragdrop.pt.y); truelight@0: w->wndproc(w, &e); truelight@0: } truelight@0: return false; truelight@0: } truelight@0: tron@1093: static bool HandlePopupMenu(void) 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; truelight@0: e.popupmenu.pt = _cursor.pos; truelight@0: } else { truelight@0: _popup_menu_active = false; truelight@0: e.event = WE_POPUPMENU_SELECT; truelight@0: e.popupmenu.pt = _cursor.pos; truelight@0: } truelight@0: darkvater@1038: w->wndproc(w, &e); darkvater@1038: truelight@0: return false; truelight@0: } truelight@0: tron@1095: static bool HandleMouseOver(void) 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: truelight@543: // 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; truelight@543: e.mouseover.pt.x = -1; truelight@543: e.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) { truelight@543: // send an event in client coordinates. truelight@543: e.event = WE_MOUSEOVER; truelight@543: e.mouseover.pt.x = _cursor.pos.x - w->left; truelight@543: e.mouseover.pt.y = _cursor.pos.y - w->top; truelight@543: if (w->widget != NULL) { truelight@543: e.mouseover.widget = GetWidgetFromPos(w, e.mouseover.pt.x, e.mouseover.pt.y); truelight@543: } truelight@543: w->wndproc(w, &e); truelight@543: } truelight@543: truelight@543: // Mouseover never stops execution truelight@543: return true; truelight@543: } truelight@543: tron@2596: tron@2596: static bool _dragging_window; tron@2596: tron@1095: static bool HandleWindowDragging(void) truelight@0: { truelight@0: Window *w; truelight@0: // Get out immediately if no window is being dragged at all. tron@2639: if (!_dragging_window) return true; truelight@0: truelight@0: // Otherwise find the window... tron@350: for (w = _windows; w != _last_window; w++) { 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: truelight@0: // 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) { tron@353: int hsnap = _patches.window_snap_radius; tron@353: int vsnap = _patches.window_snap_radius; tron@353: int delta; tron@350: tron@350: for (v = _windows; v != _last_window; ++v) { 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) { tron@350: // 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: tron@350: // 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) { tron@353: // 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: tron@353: // 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) { tron@350: // 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: tron@350: // 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) { tron@353: // 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: tron@353: // 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: tron@350: // Make sure the window doesn't leave the screen tron@350: // 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: tron@370: // 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: truelight@867: SetWindowDirty(w); truelight@867: truelight@867: /* Scroll through all the windows and update the widgets if needed */ truelight@867: { truelight@867: Widget *wi = w->widget; truelight@867: bool resize_height = false; truelight@867: bool resize_width = false; truelight@867: truelight@867: while (wi->type != WWT_LAST) { truelight@867: if (wi->resize_flag != RESIZE_NONE) { truelight@867: /* Resize this widget */ truelight@867: if (wi->resize_flag & RESIZE_LEFT) { truelight@867: wi->left += x; truelight@867: resize_width = true; truelight@867: } truelight@867: if (wi->resize_flag & RESIZE_RIGHT) { truelight@867: wi->right += x; truelight@867: resize_width = true; truelight@867: } truelight@867: truelight@867: if (wi->resize_flag & RESIZE_TOP) { truelight@867: wi->top += y; truelight@867: resize_height = true; truelight@867: } truelight@867: if (wi->resize_flag & RESIZE_BOTTOM) { truelight@867: wi->bottom += y; truelight@867: resize_height = true; truelight@867: } truelight@867: } truelight@867: wi++; truelight@867: } truelight@867: truelight@867: /* We resized at least 1 widget, so let's rezise the window totally */ tron@2639: if (resize_width) w->width = x + w->width; tron@2639: if (resize_height) w->height = y + w->height; truelight@867: } truelight@867: truelight@867: e.event = WE_RESIZE; truelight@867: e.sizing.size.x = x + w->width; truelight@867: e.sizing.size.y = y + w->height; truelight@867: e.sizing.diff.x = x; truelight@867: e.sizing.diff.y = y; truelight@867: w->wndproc(w, &e); truelight@867: truelight@867: SetWindowDirty(w); truelight@867: return false; truelight@0: } truelight@0: } truelight@0: truelight@0: _dragging_window = false; truelight@0: return false; truelight@0: } truelight@0: tron@2817: static Window* 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: truelight@0: w = BringWindowToFront(w); truelight@0: DeleteWindowById(WC_DROPDOWN_MENU, 0); truelight@0: return w; truelight@0: } truelight@0: tron@2817: static Window* 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: truelight@0: w = BringWindowToFront(w); truelight@0: DeleteWindowById(WC_DROPDOWN_MENU, 0); Darkvater@1657: SetWindowDirty(w); truelight@0: return w; truelight@0: } truelight@0: truelight@0: tron@1093: static bool HandleScrollbarScrolling(void) truelight@0: { truelight@0: Window *w; truelight@0: int i; truelight@0: int pos; truelight@0: Scrollbar *sb; truelight@0: truelight@0: // Get out quickly if no item is being scrolled tron@2639: if (!_scrolling_scrollbar) return true; truelight@0: truelight@0: // Find the scrolling window tron@2639: for (w = _windows; w != _last_window; w++) { truelight@0: if (w->flags4 & WF_SCROLL_MIDDLE) { truelight@0: // 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: truelight@0: // 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: tron@1093: static bool HandleViewportScroll(void) truelight@0: { truelight@0: Window *w; truelight@0: ViewPort *vp; truelight@0: int dx,dy, x, y, sub; truelight@0: tron@2639: if (!_scrolling_viewport) return true; truelight@0: truelight@0: if (!_right_button_down) { truelight@0: stop_capt:; truelight@0: _cursor.fix_at = false; truelight@0: _scrolling_viewport = false; truelight@0: return true; truelight@0: } truelight@158: truelight@0: w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y); truelight@0: if (w == NULL) goto stop_capt; truelight@0: tron@2680: if (_patches.reverse_scroll) { tron@2680: dx = -_cursor.delta.x; tron@2680: dy = -_cursor.delta.y; tron@2680: } else { tron@2680: dx = _cursor.delta.x; tron@2680: dy = _cursor.delta.y; tron@2680: } tron@2680: truelight@0: if (w->window_class != WC_SMALLMAP) { truelight@0: vp = IsPtInWindowViewport(w, _cursor.pos.x, _cursor.pos.y); truelight@0: if (vp == NULL) truelight@0: goto stop_capt; truelight@0: tron@2680: WP(w,vp_d).scrollpos_x += dx << vp->zoom; tron@2680: WP(w,vp_d).scrollpos_y += dy << vp->zoom; tron@2680: truelight@0: _cursor.delta.x = _cursor.delta.y = 0; truelight@0: return false; truelight@0: } else { truelight@0: // scroll the smallmap ? tron@647: int hx; tron@647: int hy; tron@647: int hvx; tron@647: int hvy; truelight@158: truelight@0: _cursor.fix_at = true; truelight@158: truelight@0: x = WP(w,smallmap_d).scroll_x; truelight@0: y = WP(w,smallmap_d).scroll_y; truelight@0: truelight@0: sub = WP(w,smallmap_d).subscroll + dx; truelight@0: truelight@0: x -= (sub >> 2) << 4; truelight@0: y += (sub >> 2) << 4; truelight@0: sub &= 3; truelight@0: truelight@0: x += (dy >> 1) << 4; truelight@0: y += (dy >> 1) << 4; truelight@0: truelight@0: if (dy & 1) { truelight@0: x += 16; truelight@0: sub += 2; truelight@0: if (sub > 3) { truelight@0: sub -= 4; truelight@0: x -= 16; truelight@0: y += 16; truelight@0: } truelight@0: } truelight@0: tron@647: hx = (w->widget[4].right - w->widget[4].left) / 2; tron@647: hy = (w->widget[4].bottom - w->widget[4].top ) / 2; tron@647: hvx = hx * -4 + hy * 8; tron@647: hvy = hx * 4 + hy * 8; tron@2639: if (x < -hvx) { tron@2639: x = -hvx; tron@2639: sub = 0; tron@2639: } tron@2639: if (x > (int)MapMaxX() * 16 - hvx) { tron@2639: x = MapMaxX() * 16 - hvx; tron@2639: sub = 0; tron@2639: } tron@2639: if (y < -hvy) { tron@2639: y = -hvy; tron@2639: sub = 0; tron@2639: } tron@2639: if (y > (int)MapMaxY() * 16 - hvy) { tron@2639: y = MapMaxY() * 16 - hvy; tron@2639: sub = 0; tron@2639: } truelight@0: truelight@0: WP(w,smallmap_d).scroll_x = x; truelight@0: WP(w,smallmap_d).scroll_y = y; truelight@0: WP(w,smallmap_d).subscroll = sub; truelight@0: truelight@0: _cursor.delta.x = _cursor.delta.y = 0; truelight@0: truelight@0: SetWindowDirty(w); truelight@0: return false; truelight@0: } truelight@0: } truelight@0: truelight@0: static Window *MaybeBringWindowToFront(Window *w) truelight@0: { truelight@0: Window *u; 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) { tron@2639: return w; tron@2639: } truelight@0: darkvater@1648: for (u = w; ++u != _last_window;) { 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: 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: truelight@158: return BringWindowToFront(w); truelight@0: } truelight@0: truelight@0: return w; truelight@0: } truelight@0: darkvater@1648: /** Send a message from one window to another. The receiving window is found by darkvater@1648: * @param w @see 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: */ tron@2596: static void SendWindowMessageW(Window* w, uint msg, uint wparam, uint lparam) darkvater@1648: { darkvater@1648: WindowEvent e; darkvater@1648: darkvater@1648: e.message.event = WE_MESSAGE; darkvater@1648: e.message.msg = msg; darkvater@1648: e.message.wparam = wparam; darkvater@1648: e.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 darkvater@1648: * @param wnd_class @see WindowClass class AND darkvater@1648: * @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: */ darkvater@1648: void SendWindowMessage(WindowClass wnd_class, WindowNumber wnd_num, uint msg, uint wparam, uint 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: truelight@0: static void HandleKeypress(uint32 key) truelight@0: { truelight@0: Window *w; truelight@0: WindowEvent we; truelight@867: /* Stores if a window with a textfield for typing is open truelight@867: * If this is the case, keypress events are only passed to windows with text fields and dominik@651: * to thein this main toolbar. */ dominik@651: bool query_open = false; truelight@158: truelight@0: // Setup event truelight@0: we.keypress.event = WE_KEYPRESS; truelight@0: we.keypress.ascii = key & 0xFF; truelight@0: we.keypress.keycode = key >> 16; truelight@0: we.keypress.cont = true; truelight@0: dominik@651: // check if we have a query string window open before allowing hotkeys tron@2639: if (FindWindowById(WC_QUERY_STRING, 0) != NULL || tron@2639: FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL || tron@2639: FindWindowById(WC_CONSOLE, 0) != NULL || tron@2639: FindWindowById(WC_SAVELOAD, 0) != NULL) { dominik@651: query_open = true; tron@2639: } dominik@651: truelight@0: // Call the event, start with the uppermost window. tron@2639: for (w = _last_window; w != _windows;) { truelight@0: --w; dominik@651: // 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 && tron@2639: w->window_class != WC_CONSOLE && tron@2639: w->window_class != WC_SAVELOAD) { dominik@651: continue; tron@2639: } truelight@0: w->wndproc(w, &we); tron@2639: if (!we.keypress.cont) break; truelight@0: } Darkvater@1637: matthijs@1642: if (we.keypress.cont) { matthijs@1642: w = FindWindowById(WC_MAIN_TOOLBAR, 0); matthijs@1642: // When there is no toolbar w is null, check for that matthijs@1642: if (w != NULL) w->wndproc(w, &we); matthijs@1642: } truelight@0: } truelight@0: tron@1093: extern void UpdateTileSelection(void); tron@1093: extern bool VpHandlePlaceSizingDrag(void); truelight@0: pasky@1570: static void MouseLoop(int click, int mousewheel) truelight@0: { truelight@0: int x,y; truelight@0: Window *w; truelight@0: ViewPort *vp; 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: truelight@0: truelight@0: if (click == 0 && mousewheel == 0) { truelight@0: if (_patches.autoscroll && _game_mode != GM_MENU) { truelight@0: w = FindWindowFromPt(x, y); truelight@0: if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL ) return; truelight@0: vp = IsPtInWindowViewport(w, x, y); truelight@0: if (vp) { truelight@0: x -= vp->left; truelight@0: y -= vp->top; truelight@0: //here allows scrolling in both x and y axis truelight@0: #define scrollspeed 3 tron@2549: if (x - 15 < 0) { tron@2549: WP(w, vp_d).scrollpos_x += (x - 15) * scrollspeed << vp->zoom; tron@2549: } else if (15 - (vp->width - x) > 0) { tron@2549: WP(w, vp_d).scrollpos_x += (15 - (vp->width - x)) * scrollspeed << vp->zoom; tron@2549: } tron@2549: if (y - 15 < 0) { tron@2549: WP(w, vp_d).scrollpos_y += (y - 15) * scrollspeed << vp->zoom; tron@2549: } else if (15 - (vp->height - y) > 0) { tron@2549: WP(w,vp_d).scrollpos_y += (15 - (vp->height - y)) * scrollspeed << vp->zoom; tron@2549: } truelight@158: #undef scrollspeed truelight@0: } truelight@0: } truelight@0: return; truelight@0: } truelight@0: truelight@0: w = FindWindowFromPt(x, y); tron@2639: if (w == NULL) return; truelight@0: w = MaybeBringWindowToFront(w); truelight@0: vp = IsPtInWindowViewport(w, x, y); truelight@0: if (vp != NULL) { tron@2639: if (_game_mode == GM_MENU) return; truelight@0: darkvater@178: // only allow zooming in-out in main window, or in viewports tron@2639: if (mousewheel && tron@2639: !(w->flags4 & WF_DISABLE_VP_SCROLL) && ( tron@2639: w->window_class == WC_MAIN_WINDOW || tron@2639: w->window_class == WC_EXTRA_VIEW_PORT tron@2639: )) { darkvater@152: ZoomInOrOutToCursorWindow(mousewheel < 0,w); truelight@0: } truelight@0: truelight@0: if (click == 1) { darkvater@70: DEBUG(misc, 2) ("cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite); truelight@0: if (_thd.place_mode != 0 && darkvater@67: // query button and place sign button work in pause mode tron@2517: _cursor.sprite != SPR_CURSOR_QUERY && tron@2517: _cursor.sprite != SPR_CURSOR_SIGN && truelight@0: _pause != 0 && tron@2549: !_cheats.build_in_pause.value) { tron@2549: return; tron@2549: } truelight@158: truelight@0: if (_thd.place_mode == 0) { truelight@0: HandleViewportClicked(vp, x, y); truelight@0: } else { truelight@0: PlaceObject(); truelight@0: } truelight@0: } else if (click == 2) { truelight@0: if (!(w->flags4 & WF_DISABLE_VP_SCROLL)) { truelight@0: _scrolling_viewport = true; truelight@0: _cursor.fix_at = true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: if (mousewheel) darkvater@982: DispatchMouseWheelEvent(w, GetWidgetFromPos(w, x - w->left, y - w->top), mousewheel); truelight@0: tron@2631: switch (click) { tron@2631: case 1: DispatchLeftClickEvent(w, x - w->left, y - w->top); break; tron@2631: case 2: DispatchRightClickEvent(w, x - w->left, y - w->top); break; tron@2631: } truelight@0: } truelight@0: } truelight@0: pasky@1570: void InputLoop(void) pasky@1570: { pasky@1570: int click; pasky@1570: int mousewheel; pasky@1570: pasky@1570: _current_player = _local_player; pasky@1570: pasky@1570: // Handle pressed keys pasky@1570: if (_pressed_key) { pasky@1570: uint32 key = _pressed_key; _pressed_key = 0; pasky@1570: HandleKeypress(key); pasky@1570: } pasky@1570: pasky@1570: // 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; pasky@1570: } else if (_right_button_clicked) { pasky@1570: _right_button_clicked = false; pasky@1570: click = 2; pasky@1570: } pasky@1570: pasky@1570: mousewheel = 0; pasky@1570: if (_cursor.wheel) { pasky@1570: mousewheel = _cursor.wheel; pasky@1570: _cursor.wheel = 0; pasky@1570: } pasky@1570: pasky@1570: MouseLoop(click, mousewheel); pasky@1570: } pasky@1570: pasky@1570: truelight@0: static int _we4_timer; truelight@0: tron@1093: void UpdateWindows(void) truelight@0: { truelight@0: Window *w; truelight@0: int t; truelight@0: tron@2639: t = _we4_timer + 1; tron@2639: if (t >= 100) { tron@2639: for (w = _last_window; w != _windows;) { truelight@0: w--; truelight@0: CallWindowEventNP(w, WE_4); truelight@0: } truelight@0: t = 0; truelight@0: } truelight@0: _we4_timer = t; truelight@0: tron@2639: for (w = _last_window; w != _windows;) { truelight@0: w--; truelight@0: if (w->flags4 & WF_WHITE_BORDER_MASK) { truelight@0: w->flags4 -= WF_WHITE_BORDER_ONE; truelight@0: if (!(w->flags4 & WF_WHITE_BORDER_MASK)) { truelight@0: SetWindowDirty(w); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: DrawDirtyBlocks(); truelight@0: tron@2639: for (w = _windows; w != _last_window; w++) { tron@2639: if (w->viewport != NULL) UpdateViewportPosition(w); truelight@0: } truelight@543: DrawTextMessage(); truelight@0: // 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: { tron@2643: const Window* w; truelight@0: tron@2639: for (w = _windows; w != _last_window; w++) { tron@2639: if (w->window_class == cls && w->window_number == number) SetWindowDirty(w); truelight@0: } truelight@0: } truelight@0: tron@2596: 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 */ Darkvater@1867: if (wi->type == WWT_EMPTY || HASBIT(w->hidden_state, 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: { tron@2630: const Window* w; truelight@0: tron@2639: for (w = _windows; w != _last_window; w++) { 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: { tron@2630: const Window* w; tron@2639: tron@2639: for (w = _windows; w != _last_window; w++) { tron@2639: if (w->window_class == cls) SetWindowDirty(w); truelight@0: } truelight@0: } truelight@0: truelight@0: tron@1093: void CallWindowTickEvent(void) truelight@0: { truelight@0: Window *w; tron@2639: tron@2639: for (w = _last_window; w != _windows;) { truelight@0: --w; truelight@0: CallWindowEventNP(w, WE_TICK); truelight@0: } truelight@0: } truelight@0: tron@1093: void DeleteNonVitalWindows(void) truelight@0: { truelight@0: Window *w; tron@2639: tron@2639: for (w = _windows; w != _last_window;) { 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' truelight@0: DeleteWindow(w); truelight@0: w = _windows; truelight@0: } else { truelight@0: w++; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@867: /* 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 */ darkvater@763: void DeleteAllNonVitalWindows(void) darkvater@763: { darkvater@763: Window *w; tron@2639: darkvater@763: // Delete every window except for stickied ones darkvater@763: DeleteNonVitalWindows(); darkvater@763: // Delete all sticked windows darkvater@763: for (w = _windows; w != _last_window;) { darkvater@763: if (w->flags4 & WF_STICKY) { darkvater@763: DeleteWindow(w); darkvater@763: w = _windows; darkvater@763: } else darkvater@763: w++; darkvater@763: } darkvater@763: } darkvater@763: darkvater@983: /* Delete all always on-top windows to get an empty screen */ darkvater@983: void HideVitalWindows(void) darkvater@983: { 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@69: DEBUG(misc, 1) ("Repositioning Main Toolbar..."); darkvater@68: darkvater@68: if (w == NULL || w->window_class != WC_MAIN_TOOLBAR) darkvater@68: w = FindWindowById(WC_MAIN_TOOLBAR, 0); darkvater@68: darkvater@68: switch (_patches.toolbar_pos) { tron@2026: case 1: w->left = (_screen.width - w->width) >> 1; 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: { truelight@0: Window *w; truelight@0: tron@2639: for (w = _windows; w != _last_window; w++) { 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: dominik@126: IConsoleResize(); truelight@0: truelight@0: if (w->window_class == WC_MAIN_TOOLBAR) { truelight@0: top = w->top; darkvater@68: left = PositionMainToolbar(w); // changes toolbar orientation truelight@158: } else if (w->window_class == WC_SELECT_GAME || w->window_class == WC_GAME_OPTIONS || w->window_class == WC_NETWORK_WINDOW){ truelight@0: top = (newh - w->height) >> 1; truelight@0: left = (neww - w->width) >> 1; truelight@0: } else if (w->window_class == WC_NEWS_WINDOW) { truelight@0: top = newh - w->height; truelight@0: left = (neww - w->width) >> 1; truelight@0: } else if (w->window_class == WC_STATUS_BAR) { truelight@0: top = newh - w->height; truelight@158: left = (neww - w->width) >> 1; dominik@649: } else if (w->window_class == WC_SEND_NETWORK_MSG) { dominik@649: top = (newh - 26); // 26 = height of status bar + height of chat bar dominik@649: left = (neww - w->width) >> 1; truelight@0: } else { truelight@0: left = w->left; truelight@0: if (left + (w->width>>1) >= neww) left = neww - w->width; truelight@0: top = w->top; truelight@0: if (top + (w->height>>1) >= newh) top = newh - w->height; 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: }