2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file window.cpp Windowing system, widgets and events */
|
2007-02-23 01:48:53 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2006-10-06 21:10:14 +00:00
|
|
|
#include <stdarg.h>
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "player_func.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "gfx_func.h"
|
2004-08-24 08:34:28 +00:00
|
|
|
#include "console.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "viewport_func.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
#include "genworld.h"
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "blitter/factory.hpp"
|
2007-12-19 19:44:29 +00:00
|
|
|
#include "window_gui.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "zoom_func.h"
|
2007-12-25 09:48:53 +00:00
|
|
|
#include "core/alloc_func.hpp"
|
2007-12-26 11:45:43 +00:00
|
|
|
#include "map_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_base.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2008-04-17 21:21:01 +00:00
|
|
|
#include "cheat_func.h"
|
2008-05-06 22:17:12 +00:00
|
|
|
#include "window_func.h"
|
2008-05-07 13:10:15 +00:00
|
|
|
#include "tilehighlight_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/sprites.h"
|
|
|
|
|
2008-04-13 19:06:30 +00:00
|
|
|
static Point _drag_delta; ///< delta between mouse cursor and upper left corner of dragged window
|
|
|
|
static Window *_mouseover_last_w = NULL; ///< Window of the last MOUSEOVER event
|
2004-11-10 21:14:16 +00:00
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* List of windows opened at the screen.
|
|
|
|
* Uppermost window is at _z_windows[_last_z_window - 1],
|
|
|
|
* bottom window is at _z_windows[0]
|
|
|
|
*/
|
2008-04-13 19:25:14 +00:00
|
|
|
Window *_z_windows[MAX_NUMBER_OF_WINDOWS];
|
2006-11-18 17:07:05 +00:00
|
|
|
Window **_last_z_window; ///< always points to the next free space in the z-array
|
2006-11-18 16:47:02 +00:00
|
|
|
|
2008-01-13 13:36:01 +00:00
|
|
|
Point _cursorpos_drag_start;
|
|
|
|
|
|
|
|
int _scrollbar_start_pos;
|
|
|
|
int _scrollbar_size;
|
|
|
|
byte _scroller_click_timeout;
|
|
|
|
|
|
|
|
bool _scrolling_scrollbar;
|
|
|
|
bool _scrolling_viewport;
|
|
|
|
|
|
|
|
byte _special_mouse_mode;
|
|
|
|
|
|
|
|
|
2008-05-04 10:05:50 +00:00
|
|
|
/**
|
2008-05-10 13:46:36 +00:00
|
|
|
* Call the window event handler for handling event \a e.
|
|
|
|
* This is a temporary helper functions that will be removed
|
|
|
|
* once all windows that still rely on WindowEvent and
|
|
|
|
* WindowEventCodes have been rewritten to use the 'OnXXX'
|
|
|
|
* event handlers.
|
2008-05-04 10:05:50 +00:00
|
|
|
* @param e Window event to handle
|
|
|
|
*/
|
|
|
|
void Window::HandleWindowEvent(WindowEvent *e)
|
|
|
|
{
|
|
|
|
if (wndproc != NULL) wndproc(this, e);
|
|
|
|
}
|
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
void Window::OnPaint()
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_PAINT;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::OnKeyPress(uint16 key, uint16 keycode)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_KEYPRESS;
|
|
|
|
e.we.keypress.key = key;
|
|
|
|
e.we.keypress.keycode = keycode;
|
|
|
|
e.we.keypress.cont = true;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
|
|
|
|
return e.we.keypress.cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::OnCTRLStateChange()
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_CTRL_CHANGED;
|
|
|
|
e.we.ctrl.cont = true;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
|
|
|
|
return e.we.ctrl.cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnClick(Point pt, int widget)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_CLICK;
|
|
|
|
e.we.click.pt = pt;
|
|
|
|
e.we.click.widget = widget;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnDoubleClick(Point pt, int widget)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_DOUBLE_CLICK;
|
|
|
|
e.we.click.pt = pt;
|
|
|
|
e.we.click.widget = widget;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnRightClick(Point pt, int widget)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_RCLICK;
|
|
|
|
e.we.click.pt = pt;
|
|
|
|
e.we.click.widget = widget;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnDragDrop(Point pt, int widget)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_DRAGDROP;
|
|
|
|
e.we.click.pt = pt;
|
|
|
|
e.we.click.widget = widget;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnScroll(Point delta)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_SCROLL;
|
|
|
|
e.we.scroll.delta = delta;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnMouseOver(Point pt, int widget)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_MOUSEOVER;
|
|
|
|
e.we.click.pt = pt;
|
|
|
|
e.we.click.widget = widget;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnMouseWheel(int wheel)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_MOUSEWHEEL;
|
|
|
|
e.we.wheel.wheel = wheel;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnMouseLoop()
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_MOUSELOOP;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnTick()
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_TICK;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnHundredthTick()
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_100_TICKS;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnTimeout()
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_TIMEOUT;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnResize(Point new_size, Point delta)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_RESIZE;
|
|
|
|
e.we.sizing.size = new_size;
|
|
|
|
e.we.sizing.diff = delta;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnDropdownSelect(int widget, int index)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_DROPDOWN_SELECT;
|
|
|
|
e.we.dropdown.button = widget;
|
|
|
|
e.we.dropdown.index = index;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnQueryTextFinished(char *str)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_ON_EDIT_TEXT;
|
|
|
|
e.we.edittext.str = str;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnInvalidateData(int data)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_INVALIDATE_DATA;
|
|
|
|
e.we.invalidate.data = data;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnPlaceObject(Point pt, TileIndex tile)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_PLACE_OBJ;
|
|
|
|
e.we.place.pt = pt;
|
|
|
|
e.we.place.tile = tile;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnPlaceObjectAbort()
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_ABORT_PLACE_OBJ;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Window::OnPlaceDrag(ViewportPlaceMethod select_method, byte select_proc, Point pt)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_PLACE_DRAG;
|
|
|
|
e.we.place.select_method = select_method;
|
|
|
|
e.we.place.select_proc = select_proc;
|
|
|
|
e.we.place.pt = pt;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnPlaceMouseUp(ViewportPlaceMethod select_method, byte select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_PLACE_MOUSEUP;
|
|
|
|
e.we.place.select_method = select_method;
|
|
|
|
e.we.place.select_proc = select_proc;
|
|
|
|
e.we.place.pt = pt;
|
|
|
|
e.we.place.tile = end_tile;
|
|
|
|
e.we.place.starttile = start_tile;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::OnPlacePresize(Point pt, TileIndex tile)
|
|
|
|
{
|
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_PLACE_PRESIZE;
|
|
|
|
e.we.place.pt = pt;
|
|
|
|
e.we.place.tile = tile;
|
|
|
|
this->HandleWindowEvent(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-02 00:59:48 +00:00
|
|
|
void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...)
|
|
|
|
{
|
|
|
|
va_list wdg_list;
|
|
|
|
|
|
|
|
va_start(wdg_list, widgets);
|
|
|
|
|
|
|
|
while (widgets != WIDGET_LIST_END) {
|
|
|
|
SetWidgetDisabledState(widgets, disab_stat);
|
|
|
|
widgets = va_arg(wdg_list, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(wdg_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDECL Window::SetWidgetsHiddenState(bool hidden_stat, int widgets, ...)
|
|
|
|
{
|
|
|
|
va_list wdg_list;
|
|
|
|
|
|
|
|
va_start(wdg_list, widgets);
|
|
|
|
|
|
|
|
while (widgets != WIDGET_LIST_END) {
|
|
|
|
SetWidgetHiddenState(widgets, hidden_stat);
|
|
|
|
widgets = va_arg(wdg_list, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(wdg_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...)
|
|
|
|
{
|
|
|
|
va_list wdg_list;
|
|
|
|
|
|
|
|
va_start(wdg_list, widgets);
|
|
|
|
|
|
|
|
while (widgets != WIDGET_LIST_END) {
|
|
|
|
SetWidgetLoweredState(widgets, lowered_stat);
|
|
|
|
widgets = va_arg(wdg_list, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(wdg_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::RaiseButtons()
|
|
|
|
{
|
2008-05-07 08:27:55 +00:00
|
|
|
for (uint i = 0; i < this->widget_count; i++) {
|
2007-12-08 01:20:47 +00:00
|
|
|
if (this->IsWidgetLowered(i)) {
|
|
|
|
this->RaiseWidget(i);
|
|
|
|
this->InvalidateWidget(i);
|
2007-12-02 00:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-07 17:42:15 +00:00
|
|
|
void Window::InvalidateWidget(byte widget_index) const
|
2007-12-02 00:59:48 +00:00
|
|
|
{
|
|
|
|
const Widget *wi = &this->widget[widget_index];
|
|
|
|
|
|
|
|
/* Don't redraw the window if the widget is invisible or of no-type */
|
|
|
|
if (wi->type == WWT_EMPTY || IsWidgetHidden(widget_index)) return;
|
|
|
|
|
|
|
|
SetDirtyBlocks(this->left + wi->left, this->top + wi->top, this->left + wi->right + 1, this->top + wi->bottom + 1);
|
|
|
|
}
|
|
|
|
|
2007-12-08 02:55:47 +00:00
|
|
|
void Window::HandleButtonClick(byte widget)
|
|
|
|
{
|
|
|
|
this->LowerWidget(widget);
|
|
|
|
this->flags4 |= 5 << WF_TIMEOUT_SHL;
|
|
|
|
this->InvalidateWidget(widget);
|
|
|
|
}
|
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
static void StartWindowDrag(Window *w);
|
|
|
|
static void StartWindowSizing(Window *w);
|
2006-01-05 12:40:50 +00:00
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Dispatch left mouse-button (possibly double) click in window.
|
|
|
|
* @param w Window to dispatch event in
|
|
|
|
* @param x X coordinate of the click
|
|
|
|
* @param y Y coordinate of the click
|
|
|
|
* @param double_click Was it a double click?
|
|
|
|
*/
|
2007-06-22 10:57:53 +00:00
|
|
|
static void DispatchLeftClickEvent(Window *w, int x, int y, bool double_click)
|
2005-11-04 14:01:44 +00:00
|
|
|
{
|
2008-05-10 13:46:36 +00:00
|
|
|
int widget = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->desc_flags & WDF_DEF_WIDGET) {
|
2008-05-10 13:46:36 +00:00
|
|
|
widget = GetWidgetFromPos(w, x, y);
|
|
|
|
if (widget < 0) return; // exit if clicked outside of widgets
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2004-09-12 14:12:33 +00:00
|
|
|
/* don't allow any interaction if the button has been disabled */
|
2008-05-10 13:46:36 +00:00
|
|
|
if (w->IsWidgetDisabled(widget)) return;
|
2006-12-04 13:57:04 +00:00
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
const Widget *wi = &w->widget[widget];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-24 14:15:17 +00:00
|
|
|
if (wi->type & WWB_MASK) {
|
2004-09-12 14:12:33 +00:00
|
|
|
/* special widget handling for buttons*/
|
2006-02-01 07:36:15 +00:00
|
|
|
switch (wi->type) {
|
2006-10-24 14:15:17 +00:00
|
|
|
case WWT_PANEL | WWB_PUSHBUTTON: /* WWT_PUSHBTN */
|
|
|
|
case WWT_IMGBTN | WWB_PUSHBUTTON: /* WWT_PUSHIMGBTN */
|
|
|
|
case WWT_TEXTBTN | WWB_PUSHBUTTON: /* WWT_PUSHTXTBTN */
|
2008-05-10 13:46:36 +00:00
|
|
|
w->HandleButtonClick(widget);
|
2006-10-24 14:15:17 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-02 17:23:04 +00:00
|
|
|
} else if (wi->type == WWT_SCROLLBAR || wi->type == WWT_SCROLL2BAR || wi->type == WWT_HSCROLLBAR) {
|
2008-05-10 13:46:36 +00:00
|
|
|
ScrollbarClickHandler(w, wi, x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (w->desc_flags & WDF_STD_BTN) {
|
2008-05-10 13:46:36 +00:00
|
|
|
if (widget == 0) { /* 'X' */
|
2008-05-06 21:28:30 +00:00
|
|
|
delete w;
|
2005-01-11 12:15:08 +00:00
|
|
|
return;
|
2005-01-23 13:09:35 +00:00
|
|
|
}
|
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
if (widget == 1) { /* 'Title bar' */
|
2006-11-18 16:47:02 +00:00
|
|
|
StartWindowDrag(w);
|
2005-01-23 13:42:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-03 19:45:18 +00:00
|
|
|
|
2005-01-23 13:42:26 +00:00
|
|
|
if (w->desc_flags & WDF_RESIZABLE && wi->type == WWT_RESIZEBOX) {
|
2006-11-18 16:47:02 +00:00
|
|
|
StartWindowSizing(w);
|
2008-05-10 13:46:36 +00:00
|
|
|
w->InvalidateWidget(widget);
|
2005-01-23 13:42:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-01-03 19:45:18 +00:00
|
|
|
|
|
|
|
if (w->desc_flags & WDF_STICKY_BUTTON && wi->type == WWT_STICKYBOX) {
|
|
|
|
w->flags4 ^= WF_STICKY;
|
2008-05-10 13:46:36 +00:00
|
|
|
w->InvalidateWidget(widget);
|
2005-01-23 13:42:26 +00:00
|
|
|
return;
|
2004-12-15 23:33:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-16 12:30:52 +00:00
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
Point pt = { x, y };
|
|
|
|
|
|
|
|
if (double_click) {
|
|
|
|
w->OnDoubleClick(pt, widget);
|
|
|
|
} else {
|
|
|
|
w->OnClick(pt, widget);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Dispatch right mouse-button click in window.
|
|
|
|
* @param w Window to dispatch event in
|
|
|
|
* @param x X coordinate of the click
|
|
|
|
* @param y Y coordinate of the click
|
|
|
|
*/
|
2006-07-26 03:33:12 +00:00
|
|
|
static void DispatchRightClickEvent(Window *w, int x, int y)
|
2005-11-04 14:01:44 +00:00
|
|
|
{
|
2008-05-10 13:46:36 +00:00
|
|
|
int widget = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* default tooltips handler? */
|
|
|
|
if (w->desc_flags & WDF_STD_TOOLTIPS) {
|
2008-05-10 13:46:36 +00:00
|
|
|
widget = GetWidgetFromPos(w, x, y);
|
|
|
|
if (widget < 0) return; // exit if clicked outside of widgets
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
if (w->widget[widget].tooltips != 0) {
|
|
|
|
GuiShowTooltips(w->widget[widget].tooltips);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
Point pt = { x, y };
|
|
|
|
w->OnRightClick(pt, widget);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Dispatch the mousewheel-action to the window.
|
|
|
|
* The window will scroll any compatible scrollbars if the mouse is pointed over the bar or its contents
|
|
|
|
* @param w Window
|
2005-07-08 00:14:19 +00:00
|
|
|
* @param widget the widget where the scrollwheel was used
|
|
|
|
* @param wheel scroll up or down
|
|
|
|
*/
|
2006-07-26 03:33:12 +00:00
|
|
|
static void DispatchMouseWheelEvent(Window *w, int widget, int wheel)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-08 00:14:19 +00:00
|
|
|
if (widget < 0) return;
|
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
const Widget *wi1 = &w->widget[widget];
|
|
|
|
const Widget *wi2 = &w->widget[widget + 1];
|
2005-07-08 00:14:19 +00:00
|
|
|
|
2005-01-11 00:24:27 +00:00
|
|
|
/* The listbox can only scroll if scrolling was done on the scrollbar itself,
|
2005-01-15 08:58:31 +00:00
|
|
|
* or on the listbox (and the next item is (must be) the scrollbar)
|
2005-01-11 00:24:27 +00:00
|
|
|
* XXX - should be rewritten as a widget-dependent scroller but that's
|
|
|
|
* not happening until someone rewrites the whole widget-code */
|
2008-05-07 08:27:55 +00:00
|
|
|
Scrollbar *sb;
|
2005-01-15 08:58:31 +00:00
|
|
|
if ((sb = &w->vscroll, wi1->type == WWT_SCROLLBAR) || (sb = &w->vscroll2, wi1->type == WWT_SCROLL2BAR) ||
|
2005-01-11 00:24:27 +00:00
|
|
|
(sb = &w->vscroll2, wi2->type == WWT_SCROLL2BAR) || (sb = &w->vscroll, wi2->type == WWT_SCROLLBAR) ) {
|
|
|
|
|
|
|
|
if (sb->count > sb->cap) {
|
2007-11-19 18:38:10 +00:00
|
|
|
int pos = Clamp(sb->pos + wheel, 0, sb->count - sb->cap);
|
2005-01-11 00:24:27 +00:00
|
|
|
if (pos != sb->pos) {
|
|
|
|
sb->pos = pos;
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2005-01-11 00:24:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Generate repaint events for the visible part of window *wz within the rectangle.
|
|
|
|
*
|
|
|
|
* The function goes recursively upwards in the window stack, and splits the rectangle
|
|
|
|
* into multiple pieces at the window edges, so obscured parts are not redrawn.
|
|
|
|
*
|
|
|
|
* @param wz Pointer into window stack, pointing at the window that needs to be repainted
|
|
|
|
* @param left Left edge of the rectangle that should be repainted
|
|
|
|
* @param top Top edge of the rectangle that should be repainted
|
|
|
|
* @param right Right edge of the rectangle that should be repainted
|
|
|
|
* @param bottom Bottom edge of the rectangle that should be repainted
|
|
|
|
*/
|
2006-11-18 16:47:02 +00:00
|
|
|
static void DrawOverlappedWindow(Window* const *wz, int left, int top, int right, int bottom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-19 22:24:18 +00:00
|
|
|
Window* const *vz = wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
while (++vz != _last_z_window) {
|
|
|
|
const Window *v = *vz;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (right > v->left &&
|
2005-07-08 22:25:24 +00:00
|
|
|
bottom > v->top &&
|
2004-08-09 17:04:08 +00:00
|
|
|
left < v->left + v->width &&
|
|
|
|
top < v->top + v->height) {
|
2008-04-07 20:28:58 +00:00
|
|
|
/* v and rectangle intersect with eeach other */
|
|
|
|
int x;
|
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
if (left < (x = v->left)) {
|
2006-11-18 16:47:02 +00:00
|
|
|
DrawOverlappedWindow(wz, left, top, x, bottom);
|
|
|
|
DrawOverlappedWindow(wz, x, top, right, bottom);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
if (right > (x = v->left + v->width)) {
|
2006-11-18 16:47:02 +00:00
|
|
|
DrawOverlappedWindow(wz, left, top, x, bottom);
|
|
|
|
DrawOverlappedWindow(wz, x, top, right, bottom);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
if (top < (x = v->top)) {
|
2006-11-18 16:47:02 +00:00
|
|
|
DrawOverlappedWindow(wz, left, top, right, x);
|
|
|
|
DrawOverlappedWindow(wz, left, x, right, bottom);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
if (bottom > (x = v->top + v->height)) {
|
2006-11-18 16:47:02 +00:00
|
|
|
DrawOverlappedWindow(wz, left, top, right, x);
|
|
|
|
DrawOverlappedWindow(wz, left, x, right, bottom);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 13:05:05 +00:00
|
|
|
/* Setup blitter, and dispatch a repaint event to window *wz */
|
2008-04-13 18:59:32 +00:00
|
|
|
DrawPixelInfo *dp = _cur_dpi;
|
|
|
|
dp->width = right - left;
|
|
|
|
dp->height = bottom - top;
|
|
|
|
dp->left = left - (*wz)->left;
|
|
|
|
dp->top = top - (*wz)->top;
|
|
|
|
dp->pitch = _screen.pitch;
|
|
|
|
dp->dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
|
|
|
|
dp->zoom = ZOOM_LVL_NORMAL;
|
2008-05-10 13:46:36 +00:00
|
|
|
(*wz)->OnPaint();
|
2008-04-13 18:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From a rectangle that needs redrawing, find the windows that intersect with the rectangle.
|
|
|
|
* These windows should be re-painted.
|
|
|
|
* @param left Left edge of the rectangle that should be repainted
|
|
|
|
* @param top Top edge of the rectangle that should be repainted
|
|
|
|
* @param right Right edge of the rectangle that should be repainted
|
|
|
|
* @param bottom Bottom edge of the rectangle that should be repainted
|
|
|
|
*/
|
|
|
|
void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
|
|
|
|
{
|
|
|
|
Window* const *wz;
|
|
|
|
DrawPixelInfo bk;
|
|
|
|
_cur_dpi = &bk;
|
|
|
|
|
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const Window *w = *wz;
|
|
|
|
if (right > w->left &&
|
|
|
|
bottom > w->top &&
|
|
|
|
left < w->left + w->width &&
|
|
|
|
top < w->top + w->height) {
|
2008-04-19 13:05:05 +00:00
|
|
|
/* Window w intersects with the rectangle => needs repaint */
|
2008-04-13 18:59:32 +00:00
|
|
|
DrawOverlappedWindow(wz, left, top, right, bottom);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-06 21:28:30 +00:00
|
|
|
/**
|
|
|
|
* Mark entire window as dirty (in need of re-paint)
|
|
|
|
* @param w Window to redraw
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
|
|
|
void Window::SetDirty() const
|
|
|
|
{
|
|
|
|
SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Mark entire window as dirty (in need of re-paint)
|
|
|
|
* @param w Window to redraw
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
2006-07-26 03:33:12 +00:00
|
|
|
void SetWindowDirty(const Window *w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-05-06 21:28:30 +00:00
|
|
|
if (w != NULL) w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-12-29 17:07:41 +00:00
|
|
|
/** Find the Window whose parent pointer points to this window
|
2007-04-09 15:06:24 +00:00
|
|
|
* @param w parent Window to find child of
|
|
|
|
* @return a Window pointer that is the child of w, or NULL otherwise */
|
2006-12-29 17:07:41 +00:00
|
|
|
static Window *FindChildWindow(const Window *w)
|
|
|
|
{
|
|
|
|
Window* const *wz;
|
|
|
|
|
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *v = *wz;
|
|
|
|
if (v->parent == w) return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
/** Find the z-value of a window. A window must already be open
|
2007-04-09 15:06:24 +00:00
|
|
|
* or the behaviour is undefined but function should never fail
|
|
|
|
* @param w window to query Z Position
|
2008-04-07 20:28:58 +00:00
|
|
|
* @return Pointer into the window-list at the position of \a w
|
|
|
|
*/
|
2006-11-18 16:47:02 +00:00
|
|
|
Window **FindWindowZPosition(const Window *w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window **wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
2006-11-18 16:47:02 +00:00
|
|
|
if (*wz == w) return wz;
|
|
|
|
}
|
2006-12-30 01:08:12 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
DEBUG(misc, 3, "Window (cls %d, number %d) is not open, probably removed by recursive calls",
|
2006-12-30 01:08:12 +00:00
|
|
|
w->window_class, w->window_number);
|
|
|
|
return NULL;
|
2006-11-18 16:47:02 +00:00
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
2008-04-19 13:05:05 +00:00
|
|
|
* Remove window and all its child windows from the window stack.
|
2008-04-07 20:28:58 +00:00
|
|
|
*/
|
2008-05-06 21:28:30 +00:00
|
|
|
Window::~Window()
|
2006-11-18 16:47:02 +00:00
|
|
|
{
|
2006-06-27 21:25:53 +00:00
|
|
|
if (_thd.place_mode != VHM_NONE &&
|
2008-05-06 21:28:30 +00:00
|
|
|
_thd.window_class == this->window_class &&
|
|
|
|
_thd.window_number == this->window_number) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ResetObjectToPlace();
|
|
|
|
}
|
|
|
|
|
2008-04-13 19:06:30 +00:00
|
|
|
/* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */
|
2008-05-06 21:28:30 +00:00
|
|
|
if (_mouseover_last_w == this) _mouseover_last_w = NULL;
|
2008-04-13 19:06:30 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
/* Find the window in the z-array, and effectively remove it
|
2008-05-04 21:36:37 +00:00
|
|
|
* by moving all windows after it one to the left. This must be
|
|
|
|
* done before removing the child so we cannot cause recursion
|
|
|
|
* between the deletion of the parent and the child. */
|
2008-05-06 21:28:30 +00:00
|
|
|
Window **wz = FindWindowZPosition(this);
|
2006-12-30 01:08:12 +00:00
|
|
|
if (wz == NULL) return;
|
2006-11-18 16:47:02 +00:00
|
|
|
memmove(wz, wz + 1, (byte*)_last_z_window - (byte*)wz);
|
|
|
|
_last_z_window--;
|
2008-04-13 19:25:14 +00:00
|
|
|
|
2008-05-15 11:17:56 +00:00
|
|
|
/* Delete all children a window might have in a head-recursive manner */
|
|
|
|
Window *child = FindChildWindow(this);
|
|
|
|
while (child != NULL) {
|
|
|
|
delete child;
|
|
|
|
child = FindChildWindow(this);
|
|
|
|
}
|
2008-05-04 21:36:37 +00:00
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_DESTROY;
|
|
|
|
this->HandleWindowEvent(&e);
|
2008-05-06 21:28:30 +00:00
|
|
|
if (this->viewport != NULL) DeleteWindowViewport(this);
|
2008-05-04 21:36:37 +00:00
|
|
|
|
2008-05-06 21:28:30 +00:00
|
|
|
this->SetDirty();
|
|
|
|
free(this->widget);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Find a window by its class and window number
|
|
|
|
* @param cls Window class
|
|
|
|
* @param number Number of the window within the window class
|
|
|
|
* @return Pointer to the found window, or \c NULL if not available
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
Window *FindWindowById(WindowClass cls, WindowNumber number)
|
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class == cls && w->window_number == number) return w;
|
2004-09-05 14:20:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Delete a window by its class and window number (if it is open).
|
|
|
|
* @param cls Window class
|
|
|
|
* @param number Number of the window within the window class
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
void DeleteWindowById(WindowClass cls, WindowNumber number)
|
|
|
|
{
|
2008-05-06 21:28:30 +00:00
|
|
|
delete FindWindowById(cls, number);
|
2004-09-05 14:20:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Delete all windows of a given class
|
|
|
|
* @param cls Window class of windows to delete
|
|
|
|
*/
|
2005-01-13 16:50:20 +00:00
|
|
|
void DeleteWindowByClass(WindowClass cls)
|
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2006-11-18 00:44:09 +00:00
|
|
|
restart_search:
|
|
|
|
/* When we find the window to delete, we need to restart the search
|
2006-11-18 16:47:02 +00:00
|
|
|
* as deleting this window could cascade in deleting (many) others
|
|
|
|
* anywhere in the z-array */
|
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
2005-01-13 16:50:20 +00:00
|
|
|
if (w->window_class == cls) {
|
2008-05-06 21:28:30 +00:00
|
|
|
delete w;
|
2006-11-18 00:44:09 +00:00
|
|
|
goto restart_search;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2005-01-13 16:50:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-18 00:14:43 +00:00
|
|
|
/** Delete all windows of a player. We identify windows of a player
|
|
|
|
* by looking at the caption colour. If it is equal to the player ID
|
|
|
|
* then we say the window belongs to the player and should be deleted
|
|
|
|
* @param id PlayerID player identifier */
|
|
|
|
void DeletePlayerWindows(PlayerID id)
|
2006-11-13 20:33:51 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2006-11-13 20:33:51 +00:00
|
|
|
|
2006-11-18 00:44:09 +00:00
|
|
|
restart_search:
|
|
|
|
/* When we find the window to delete, we need to restart the search
|
2006-11-18 16:47:02 +00:00
|
|
|
* as deleting this window could cascade in deleting (many) others
|
|
|
|
* anywhere in the z-array */
|
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
2006-11-18 00:14:43 +00:00
|
|
|
if (w->caption_color == id) {
|
2008-05-06 21:28:30 +00:00
|
|
|
delete w;
|
2006-11-18 00:44:09 +00:00
|
|
|
goto restart_search;
|
2006-11-13 20:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-18 00:14:43 +00:00
|
|
|
/* Also delete the player specific windows, that don't have a player-colour */
|
|
|
|
DeleteWindowById(WC_BUY_COMPANY, id);
|
2006-11-13 20:33:51 +00:00
|
|
|
}
|
|
|
|
|
2006-11-18 00:14:43 +00:00
|
|
|
/** Change the owner of all the windows one player can take over from another
|
|
|
|
* player in the case of a company merger. Do not change ownership of windows
|
|
|
|
* that need to be deleted once takeover is complete
|
|
|
|
* @param old_player PlayerID of original owner of the window
|
|
|
|
* @param new_player PlayerID of the new owner of the window */
|
2006-11-13 20:33:51 +00:00
|
|
|
void ChangeWindowOwner(PlayerID old_player, PlayerID new_player)
|
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
|
|
|
|
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
2006-11-13 20:33:51 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
if (w->caption_color != old_player) continue;
|
|
|
|
|
|
|
|
switch (w->window_class) {
|
|
|
|
case WC_PLAYER_COLOR:
|
|
|
|
case WC_FINANCES:
|
|
|
|
case WC_STATION_LIST:
|
|
|
|
case WC_TRAINS_LIST:
|
|
|
|
case WC_ROADVEH_LIST:
|
|
|
|
case WC_SHIPS_LIST:
|
|
|
|
case WC_AIRCRAFT_LIST:
|
|
|
|
case WC_BUY_COMPANY:
|
|
|
|
case WC_COMPANY:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
w->caption_color = new_player;
|
|
|
|
break;
|
|
|
|
}
|
2006-11-13 20:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
static void BringWindowToFront(const Window *w);
|
2006-01-05 12:40:50 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
/** Find a window and make it the top-window on the screen. The window
|
2007-04-09 15:06:24 +00:00
|
|
|
* gets a white border for a brief period of time to visualize its "activation"
|
|
|
|
* @param cls WindowClass of the window to activate
|
|
|
|
* @param number WindowNumber of the window to activate
|
2006-11-18 16:47:02 +00:00
|
|
|
* @return a pointer to the window thus activated */
|
2004-08-09 17:04:08 +00:00
|
|
|
Window *BringWindowToFrontById(WindowClass cls, WindowNumber number)
|
|
|
|
{
|
|
|
|
Window *w = FindWindowById(cls, number);
|
|
|
|
|
|
|
|
if (w != NULL) {
|
|
|
|
w->flags4 |= WF_WHITE_BORDER_MASK;
|
2006-11-18 16:47:02 +00:00
|
|
|
BringWindowToFront(w);
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2005-04-05 21:03:30 +00:00
|
|
|
static inline bool IsVitalWindow(const Window *w)
|
|
|
|
{
|
2008-05-07 08:27:55 +00:00
|
|
|
switch (w->window_class) {
|
|
|
|
case WC_MAIN_TOOLBAR:
|
|
|
|
case WC_STATUS_BAR:
|
|
|
|
case WC_NEWS_WINDOW:
|
|
|
|
case WC_SEND_NETWORK_MSG:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-05 21:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** On clicking on a window, make it the frontmost window of all. However
|
|
|
|
* there are certain windows that always need to be on-top; these include
|
|
|
|
* - Toolbar, Statusbar (always on)
|
|
|
|
* - New window, Chatbar (only if open)
|
2006-11-18 00:14:43 +00:00
|
|
|
* The window is marked dirty for a repaint if the window is actually moved
|
2005-04-05 21:03:30 +00:00
|
|
|
* @param w window that is put into the foreground
|
2006-11-18 16:47:02 +00:00
|
|
|
* @return pointer to the window, the same as the input pointer
|
2005-04-05 21:03:30 +00:00
|
|
|
*/
|
2006-11-18 16:47:02 +00:00
|
|
|
static void BringWindowToFront(const Window *w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window **wz = FindWindowZPosition(w);
|
|
|
|
Window **vz = _last_z_window;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
/* Bring the window just below the vital windows */
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2006-11-18 16:47:02 +00:00
|
|
|
if (--vz < _z_windows) return;
|
|
|
|
} while (IsVitalWindow(*vz));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
if (wz == vz) return; // window is already in the right position
|
|
|
|
assert(wz < vz);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
Window *tempz = *wz;
|
2006-11-18 16:47:02 +00:00
|
|
|
memmove(wz, wz + 1, (byte*)vz - (byte*)wz);
|
|
|
|
*vz = tempz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-04-05 21:03:30 +00:00
|
|
|
/** We have run out of windows, so find a suitable candidate for replacement.
|
|
|
|
* Keep all important windows intact. These are
|
|
|
|
* - Main window (gamefield), Toolbar, Statusbar (always on)
|
|
|
|
* - News window, Chatbar (when on)
|
|
|
|
* - Any sticked windows since we wanted to keep these
|
|
|
|
* @return w pointer to the window that is going to be deleted
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static Window *FindDeletableWindow()
|
2004-12-22 17:37:21 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class != WC_MAIN_WINDOW && !IsVitalWindow(w) && !(w->flags4 & WF_STICKY)) {
|
|
|
|
return w;
|
|
|
|
}
|
2004-12-22 17:37:21 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-05 21:03:30 +00:00
|
|
|
/** A window must be freed, and all are marked as important windows. Ease the
|
|
|
|
* restriction a bit by allowing to delete sticky windows. Keep important/vital
|
|
|
|
* windows intact (Main window, Toolbar, Statusbar, News Window, Chatbar)
|
2006-11-18 16:47:02 +00:00
|
|
|
* Start finding an appropiate candidate from the lowest z-values (bottom)
|
2005-04-05 21:03:30 +00:00
|
|
|
* @see FindDeletableWindow()
|
|
|
|
* @return w Pointer to the window that is being deleted
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static Window *ForceFindDeletableWindow()
|
2004-12-22 17:37:21 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2008-04-19 13:28:48 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
2006-11-18 16:47:02 +00:00
|
|
|
Window *w = *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class != WC_MAIN_WINDOW && !IsVitalWindow(w)) return w;
|
2004-12-22 17:37:21 +00:00
|
|
|
}
|
2008-04-19 13:28:48 +00:00
|
|
|
NOT_REACHED();
|
2004-12-22 17:37:21 +00:00
|
|
|
}
|
|
|
|
|
2006-07-26 03:33:12 +00:00
|
|
|
bool IsWindowOfPrototype(const Window *w, const Widget *widget)
|
2005-01-03 19:45:18 +00:00
|
|
|
{
|
|
|
|
return (w->original_widget == widget);
|
|
|
|
}
|
|
|
|
|
2008-05-10 13:53:11 +00:00
|
|
|
/**
|
|
|
|
* Assign widgets to a new window by initialising its widget pointers, and by
|
|
|
|
* copying the widget array \a widget to \c w->widget to allow for resizable
|
|
|
|
* windows.
|
2007-04-09 15:06:24 +00:00
|
|
|
* @param w Window on which to attach the widget array
|
2008-05-10 13:53:11 +00:00
|
|
|
* @param widget pointer of widget array to fill the window with
|
|
|
|
*
|
|
|
|
* @post \c w->widget points to allocated memory and contains the copied widget array except for the terminating widget,
|
|
|
|
* \c w->original_widget points to the original widgets,
|
|
|
|
* \c w->widget_count contains number of widgets in the allocated memory.
|
|
|
|
*/
|
2008-05-10 13:54:20 +00:00
|
|
|
static void AssignWidgetToWindow(Window *w, const Widget *widget)
|
2005-01-03 19:45:18 +00:00
|
|
|
{
|
|
|
|
w->original_widget = widget;
|
|
|
|
|
|
|
|
if (widget != NULL) {
|
|
|
|
uint index = 1;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
for (const Widget *wi = widget; wi->type != WWT_LAST; wi++) index++;
|
2005-01-03 19:45:18 +00:00
|
|
|
|
2008-05-10 13:53:11 +00:00
|
|
|
w->widget = MallocT<Widget>(index);
|
2005-11-14 19:48:04 +00:00
|
|
|
memcpy(w->widget, widget, sizeof(*w->widget) * index);
|
2006-12-04 13:36:27 +00:00
|
|
|
w->widget_count = index - 1;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2005-01-03 19:45:18 +00:00
|
|
|
w->widget = NULL;
|
2006-12-04 13:36:27 +00:00
|
|
|
w->widget_count = 0;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2005-01-03 19:45:18 +00:00
|
|
|
}
|
|
|
|
|
2008-05-08 11:31:41 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new Window.
|
|
|
|
* This function is called the constructors.
|
2006-09-02 20:09:16 +00:00
|
|
|
* See descriptions for those functions for usage
|
|
|
|
* Only addition here is window_number, which is the window_number being assigned to the new window
|
2007-04-09 15:06:24 +00:00
|
|
|
* @param x offset in pixels from the left of the screen
|
|
|
|
* @param y offset in pixels from the top of the screen
|
2007-07-27 12:49:04 +00:00
|
|
|
* @param min_width minimum width in pixels of the window
|
|
|
|
* @param min_height minimum height in pixels of the window
|
2007-04-09 15:06:24 +00:00
|
|
|
* @param *proc see WindowProc function to call when any messages/updates happen to the window
|
|
|
|
* @param cls see WindowClass class of the window, used for identification and grouping
|
|
|
|
* @param *widget see Widget pointer to the window layout and various elements
|
|
|
|
* @param window_number number being assigned to the new window
|
2008-05-13 14:43:33 +00:00
|
|
|
* @return Window pointer of the newly created window
|
|
|
|
*/
|
2008-05-11 11:41:18 +00:00
|
|
|
void Window::Initialize(int x, int y, int min_width, int min_height,
|
2008-05-13 14:43:33 +00:00
|
|
|
WindowProc *proc, WindowClass cls, const Widget *widget, int window_number)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
/* We have run out of windows, close one and use that as the place for our new one */
|
2008-04-13 19:25:14 +00:00
|
|
|
if (_last_z_window == endof(_z_windows)) {
|
2008-05-08 11:31:41 +00:00
|
|
|
Window *w = FindDeletableWindow();
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w == NULL) w = ForceFindDeletableWindow();
|
2008-05-06 21:28:30 +00:00
|
|
|
delete w;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Set up window properties */
|
2008-05-08 11:31:41 +00:00
|
|
|
this->window_class = cls;
|
|
|
|
this->flags4 = WF_WHITE_BORDER_MASK; // just opened windows have a white border
|
|
|
|
this->caption_color = 0xFF;
|
|
|
|
this->left = x;
|
|
|
|
this->top = y;
|
|
|
|
this->width = min_width;
|
|
|
|
this->height = min_height;
|
|
|
|
this->wndproc = proc;
|
|
|
|
AssignWidgetToWindow(this, widget);
|
|
|
|
this->resize.width = min_width;
|
|
|
|
this->resize.height = min_height;
|
|
|
|
this->resize.step_width = 1;
|
|
|
|
this->resize.step_height = 1;
|
|
|
|
this->window_number = window_number;
|
|
|
|
|
|
|
|
/* Hacky way of specifying always-on-top windows. These windows are
|
|
|
|
* always above other windows because they are moved below them.
|
|
|
|
* status-bar is above news-window because it has been created earlier.
|
|
|
|
* Also, as the chat-window is excluded from this, it will always be
|
|
|
|
* the last window, thus always on top.
|
|
|
|
* XXX - Yes, ugly, probably needs something like w->always_on_top flag
|
|
|
|
* to implement correctly, but even then you need some kind of distinction
|
|
|
|
* between on-top of chat/news and status windows, because these conflict */
|
|
|
|
Window **wz = _last_z_window;
|
|
|
|
if (wz != _z_windows && this->window_class != WC_SEND_NETWORK_MSG && this->window_class != WC_HIGHSCORE && this->window_class != WC_ENDSCREEN) {
|
|
|
|
if (FindWindowById(WC_MAIN_TOOLBAR, 0) != NULL) wz--;
|
|
|
|
if (FindWindowById(WC_STATUS_BAR, 0) != NULL) wz--;
|
|
|
|
if (FindWindowById(WC_NEWS_WINDOW, 0) != NULL) wz--;
|
|
|
|
if (FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL) wz--;
|
|
|
|
|
|
|
|
assert(wz >= _z_windows);
|
|
|
|
if (wz != _last_z_window) memmove(wz + 1, wz, (byte*)_last_z_window - (byte*)wz);
|
2006-11-18 16:47:02 +00:00
|
|
|
}
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2008-05-08 11:31:41 +00:00
|
|
|
*wz = this;
|
|
|
|
_last_z_window++;
|
|
|
|
|
2007-07-29 22:57:40 +00:00
|
|
|
WindowEvent e;
|
|
|
|
e.event = WE_CREATE;
|
2008-05-08 11:31:41 +00:00
|
|
|
this->HandleWindowEvent(&e);
|
2008-05-11 11:41:18 +00:00
|
|
|
}
|
2007-07-27 12:49:04 +00:00
|
|
|
|
2008-05-11 11:41:18 +00:00
|
|
|
/**
|
|
|
|
* Find a nice spot for this window and resize it towards the default size.
|
|
|
|
* @param def_width default width in pixels of the window
|
|
|
|
* @param def_height default height in pixels of the window
|
|
|
|
*/
|
|
|
|
void Window::FindWindowPlacementAndResize(int def_width, int def_height)
|
|
|
|
{
|
2007-07-28 20:59:30 +00:00
|
|
|
/* Try to make windows smaller when our window is too small.
|
|
|
|
* w->(width|height) is normally the same as min_(width|height),
|
|
|
|
* but this way the GUIs can be made a little more dynamic;
|
|
|
|
* one can use the same spec for multiple windows and those
|
|
|
|
* can then determine the real minimum size of the window. */
|
2008-05-08 11:31:41 +00:00
|
|
|
if (this->width != def_width || this->height != def_height) {
|
2007-07-27 22:08:59 +00:00
|
|
|
/* Think about the overlapping toolbars when determining the minimum window size */
|
|
|
|
int free_height = _screen.height;
|
|
|
|
const Window *wt = FindWindowById(WC_STATUS_BAR, 0);
|
|
|
|
if (wt != NULL) free_height -= wt->height;
|
|
|
|
wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
|
|
|
|
if (wt != NULL) free_height -= wt->height;
|
|
|
|
|
2008-05-08 11:31:41 +00:00
|
|
|
int enlarge_x = max(min(def_width - this->width, _screen.width - this->width), 0);
|
|
|
|
int enlarge_y = max(min(def_height - this->height, free_height - this->height), 0);
|
2007-07-27 12:49:04 +00:00
|
|
|
|
|
|
|
/* X and Y has to go by step.. calculate it.
|
|
|
|
* The cast to int is necessary else x/y are implicitly casted to
|
|
|
|
* unsigned int, which won't work. */
|
2008-05-08 11:31:41 +00:00
|
|
|
if (this->resize.step_width > 1) enlarge_x -= enlarge_x % (int)this->resize.step_width;
|
|
|
|
if (this->resize.step_height > 1) enlarge_y -= enlarge_y % (int)this->resize.step_height;
|
2007-07-27 12:49:04 +00:00
|
|
|
|
2008-05-08 11:31:41 +00:00
|
|
|
ResizeWindow(this, enlarge_x, enlarge_y);
|
2007-07-27 19:09:31 +00:00
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
Point size;
|
|
|
|
Point diff;
|
|
|
|
size.x = this->width;
|
|
|
|
size.y = this->height;
|
|
|
|
diff.x = enlarge_x;
|
|
|
|
diff.y = enlarge_y;
|
|
|
|
this->OnResize(size, diff);
|
2007-07-27 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
2008-05-08 11:31:41 +00:00
|
|
|
int nx = this->left;
|
|
|
|
int ny = this->top;
|
2007-08-23 11:08:59 +00:00
|
|
|
|
2008-05-08 11:31:41 +00:00
|
|
|
if (nx + this->width > _screen.width) nx -= (nx + this->width - _screen.width);
|
2007-07-29 20:54:37 +00:00
|
|
|
|
2007-07-27 22:08:59 +00:00
|
|
|
const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
|
2008-05-11 11:41:18 +00:00
|
|
|
ny = max(ny, (wt == NULL || this == wt || this->top == 0) ? 0 : wt->height);
|
2007-08-23 11:08:59 +00:00
|
|
|
nx = max(nx, 0);
|
|
|
|
|
2008-05-08 11:31:41 +00:00
|
|
|
if (this->viewport != NULL) {
|
|
|
|
this->viewport->left += nx - this->left;
|
|
|
|
this->viewport->top += ny - this->top;
|
2007-08-23 11:08:59 +00:00
|
|
|
}
|
2008-05-08 11:31:41 +00:00
|
|
|
this->left = nx;
|
|
|
|
this->top = ny;
|
2007-07-27 22:08:59 +00:00
|
|
|
|
2008-05-08 11:31:41 +00:00
|
|
|
this->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 11:41:18 +00:00
|
|
|
void Window::FindWindowPlacementAndResize(const WindowDesc *desc)
|
|
|
|
{
|
|
|
|
this->FindWindowPlacementAndResize(desc->default_width, desc->default_height);
|
|
|
|
}
|
|
|
|
|
2006-09-02 20:09:16 +00:00
|
|
|
/**
|
|
|
|
* Open a new window. If there is no space for a new window, close an open
|
|
|
|
* window. Try to avoid stickied windows, but if there is no else, close one of
|
|
|
|
* those as well. Then make sure all created windows are below some always-on-top
|
|
|
|
* ones. Finally set all variables and call the WE_CREATE event
|
|
|
|
* @param x offset in pixels from the left of the screen
|
|
|
|
* @param y offset in pixels from the top of the screen
|
|
|
|
* @param width width in pixels of the window
|
|
|
|
* @param height height in pixels of the window
|
2007-04-09 15:06:24 +00:00
|
|
|
* @param *proc see WindowProc function to call when any messages/updates happen to the window
|
|
|
|
* @param cls see WindowClass class of the window, used for identification and grouping
|
|
|
|
* @param *widget see Widget pointer to the window layout and various elements
|
2008-04-07 20:28:58 +00:00
|
|
|
* @return Window pointer of the newly created window
|
|
|
|
*/
|
2008-05-13 14:43:33 +00:00
|
|
|
Window::Window(int x, int y, int width, int height, WindowProc *proc, WindowClass cls, const Widget *widget)
|
2006-09-02 20:09:16 +00:00
|
|
|
{
|
2008-05-13 14:43:33 +00:00
|
|
|
this->Initialize(x, y, width, height, proc, cls, widget, 0);
|
2008-05-11 11:41:18 +00:00
|
|
|
|
|
|
|
if (proc != NULL) this->FindWindowPlacementAndResize(width, height);
|
2006-09-02 20:09:16 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
static bool IsGoodAutoPlace1(int left, int top, int width, int height, Point &pos)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
int right = width + left;
|
|
|
|
int bottom = height + top;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
if (left < 0 || top < 22 || right > _screen.width || bottom > _screen.height) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Make sure it is not obscured by any window. */
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const Window *w = *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class == WC_MAIN_WINDOW) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-05 14:20:36 +00:00
|
|
|
if (right > w->left &&
|
2005-11-14 19:48:04 +00:00
|
|
|
w->left + w->width > left &&
|
2004-08-09 17:04:08 +00:00
|
|
|
bottom > w->top &&
|
2005-11-14 19:48:04 +00:00
|
|
|
w->top + w->height > top) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-05 14:20:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
pos.x = left;
|
|
|
|
pos.y = top;
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
static bool IsGoodAutoPlace2(int left, int top, int width, int height, Point &pos)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 00:14:43 +00:00
|
|
|
if (left < -(width>>2) || left > _screen.width - (width>>1)) return false;
|
|
|
|
if (top < 22 || top > _screen.height - (height>>2)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Make sure it is not obscured by any window. */
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const Window *w = *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class == WC_MAIN_WINDOW) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-05 14:20:36 +00:00
|
|
|
if (left + width > w->left &&
|
2005-11-14 19:48:04 +00:00
|
|
|
w->left + w->width > left &&
|
2004-08-09 17:04:08 +00:00
|
|
|
top + height > w->top &&
|
2005-11-14 19:48:04 +00:00
|
|
|
w->top + w->height > top) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-05 14:20:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
pos.x = left;
|
|
|
|
pos.y = top;
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static Point GetAutoPlacePosition(int width, int height)
|
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
Point pt;
|
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
if (IsGoodAutoPlace1(0, 24, width, height, pt)) return pt;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const Window *w = *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class == WC_MAIN_WINDOW) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
if (IsGoodAutoPlace1(w->left + w->width + 2, w->top, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace1(w->left - width - 2, w->top, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace1(w->left, w->top + w->height + 2, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace1(w->left, w->top - height - 2, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace1(w->left + w->width + 2, w->top + w->height - height, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace1(w->left - width - 2, w->top + w->height - height, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace1(w->left + w->width - width, w->top + w->height + 2, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace1(w->left + w->width - width, w->top - height - 2, width, height, pt)) return pt;
|
2004-09-05 14:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const Window *w = *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class == WC_MAIN_WINDOW) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
if (IsGoodAutoPlace2(w->left + w->width + 2, w->top, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace2(w->left - width - 2, w->top, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace2(w->left, w->top + w->height + 2, width, height, pt)) return pt;
|
|
|
|
if (IsGoodAutoPlace2(w->left, w->top - height - 2, width, height, pt)) return pt;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2007-04-26 17:32:54 +00:00
|
|
|
int left = 0, top = 24;
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2007-04-26 17:32:54 +00:00
|
|
|
restart:
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const Window *w = *wz;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->left == left && w->top == top) {
|
|
|
|
left += 5;
|
|
|
|
top += 5;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
pt.x = left;
|
|
|
|
pt.y = top;
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-14 00:26:24 +00:00
|
|
|
/**
|
2008-05-04 08:49:57 +00:00
|
|
|
* Compute the position of the top-left corner of a new window that is opened.
|
2007-10-14 00:26:24 +00:00
|
|
|
*
|
2008-05-04 08:56:59 +00:00
|
|
|
* By default position a child window at an offset of 10/10 of its parent.
|
|
|
|
* With the exception of WC_BUILD_TOOLBAR (build railway/roads/ship docks/airports)
|
|
|
|
* and WC_SCEN_LAND_GEN (landscaping). Whose child window has an offset of 0/36 of
|
|
|
|
* its parent. So it's exactly under the parent toolbar and no buttons will be covered.
|
|
|
|
* However if it falls too extremely outside window positions, reposition
|
|
|
|
* it to an automatic place.
|
|
|
|
*
|
2007-10-14 00:26:24 +00:00
|
|
|
* @param *desc The pointer to the WindowDesc to be created
|
|
|
|
* @param window_number the window number of the new window
|
|
|
|
*
|
2008-05-04 08:49:57 +00:00
|
|
|
* @return Coordinate of the top-left corner of the new window
|
2007-10-14 00:26:24 +00:00
|
|
|
*/
|
2008-05-04 08:49:57 +00:00
|
|
|
static Point LocalGetWindowPlacement(const WindowDesc *desc, int window_number)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Point pt;
|
|
|
|
Window *w;
|
|
|
|
|
2006-11-11 09:47:44 +00:00
|
|
|
if (desc->parent_cls != 0 /* WC_MAIN_WINDOW */ &&
|
|
|
|
(w = FindWindowById(desc->parent_cls, window_number)) != NULL &&
|
|
|
|
w->left < _screen.width - 20 && w->left > -60 && w->top < _screen.height - 20) {
|
|
|
|
|
2007-10-14 00:26:24 +00:00
|
|
|
pt.x = w->left + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 0 : 10);
|
2007-07-27 12:49:04 +00:00
|
|
|
if (pt.x > _screen.width + 10 - desc->default_width) {
|
|
|
|
pt.x = (_screen.width + 10 - desc->default_width) - 20;
|
2006-11-11 09:47:44 +00:00
|
|
|
}
|
2007-10-14 00:26:24 +00:00
|
|
|
pt.y = w->top + ((desc->parent_cls == WC_BUILD_TOOLBAR || desc->parent_cls == WC_SCEN_LAND_GEN) ? 36 : 10);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2006-11-11 10:12:00 +00:00
|
|
|
switch (desc->left) {
|
2008-05-04 08:56:59 +00:00
|
|
|
case WDP_ALIGN_TBR: // Align the right side with the top toolbar
|
2006-11-11 10:12:00 +00:00
|
|
|
w = FindWindowById(WC_MAIN_TOOLBAR, 0);
|
2007-07-27 12:49:04 +00:00
|
|
|
pt.x = (w->left + w->width) - desc->default_width;
|
2008-05-04 08:56:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WDP_ALIGN_TBL: // Align the left side with the top toolbar
|
2006-11-11 10:12:00 +00:00
|
|
|
pt.x = FindWindowById(WC_MAIN_TOOLBAR, 0)->left;
|
|
|
|
break;
|
2008-05-04 08:56:59 +00:00
|
|
|
|
|
|
|
case WDP_AUTO: // Find a good automatic position for the window
|
2008-05-04 08:49:57 +00:00
|
|
|
return GetAutoPlacePosition(desc->default_width, desc->default_height);
|
2008-05-04 08:56:59 +00:00
|
|
|
|
|
|
|
case WDP_CENTER: // Centre the window horizontally
|
2007-07-27 12:49:04 +00:00
|
|
|
pt.x = (_screen.width - desc->default_width) / 2;
|
2006-11-11 10:12:00 +00:00
|
|
|
break;
|
2008-05-04 08:56:59 +00:00
|
|
|
|
2006-11-11 10:12:00 +00:00
|
|
|
default:
|
|
|
|
pt.x = desc->left;
|
|
|
|
if (pt.x < 0) pt.x += _screen.width; // negative is from right of the screen
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (desc->top) {
|
2008-05-04 08:56:59 +00:00
|
|
|
case WDP_CENTER: // Centre the window vertically
|
2007-07-27 12:49:04 +00:00
|
|
|
pt.y = (_screen.height - desc->default_height) / 2;
|
2006-11-11 10:12:00 +00:00
|
|
|
break;
|
2008-05-04 08:56:59 +00:00
|
|
|
|
2006-11-11 10:12:00 +00:00
|
|
|
/* WDP_AUTO sets the position at once and is controlled by desc->left.
|
|
|
|
* Both left and top must be set to WDP_AUTO */
|
|
|
|
case WDP_AUTO:
|
|
|
|
NOT_REACHED();
|
|
|
|
assert(desc->left == WDP_AUTO && desc->top != WDP_AUTO);
|
|
|
|
/* fallthrough */
|
2008-05-04 08:56:59 +00:00
|
|
|
|
2006-11-11 10:12:00 +00:00
|
|
|
default:
|
|
|
|
pt.y = desc->top;
|
|
|
|
if (pt.y < 0) pt.y += _screen.height; // negative is from bottom of the screen
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-04 08:49:57 +00:00
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the positions of a new window from a WindowDesc and open it.
|
|
|
|
*
|
|
|
|
* @param *desc The pointer to the WindowDesc to be created
|
|
|
|
* @param window_number the window number of the new window
|
|
|
|
*
|
|
|
|
* @return Window pointer of the newly created window
|
|
|
|
*/
|
2008-05-13 14:43:33 +00:00
|
|
|
Window::Window(const WindowDesc *desc, WindowNumber window_number)
|
2008-05-04 08:49:57 +00:00
|
|
|
{
|
|
|
|
Point pt = LocalGetWindowPlacement(desc, window_number);
|
2008-05-13 14:43:33 +00:00
|
|
|
this->Initialize(pt.x, pt.y, desc->minimum_width, desc->minimum_height, desc->proc, desc->cls, desc->widgets, window_number);
|
2008-05-08 11:31:41 +00:00
|
|
|
this->desc_flags = desc->flags;
|
2008-05-11 11:41:18 +00:00
|
|
|
|
|
|
|
if (desc->proc != NULL) this->FindWindowPlacementAndResize(desc->default_width, desc->default_height);
|
2006-09-02 20:09:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-18 00:14:43 +00:00
|
|
|
/** Do a search for a window at specific coordinates. For this we start
|
|
|
|
* at the topmost window, obviously and work our way down to the bottom
|
2007-04-09 15:06:24 +00:00
|
|
|
* @param x position x to query
|
|
|
|
* @param y position y to query
|
2006-11-18 00:14:43 +00:00
|
|
|
* @return a pointer to the found window if any, NULL otherwise */
|
2004-08-09 17:04:08 +00:00
|
|
|
Window *FindWindowFromPt(int x, int y)
|
|
|
|
{
|
2008-04-19 13:28:48 +00:00
|
|
|
for (Window * const *wz = _last_z_window; wz != _z_windows;) {
|
2006-11-18 16:47:02 +00:00
|
|
|
Window *w = *--wz;
|
2007-11-24 10:38:43 +00:00
|
|
|
if (IsInsideBS(x, w->left, w->width) && IsInsideBS(y, w->top, w->height)) {
|
2005-11-14 19:48:04 +00:00
|
|
|
return w;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-13 19:01:26 +00:00
|
|
|
/**
|
|
|
|
* (re)initialize the windowing system
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitWindowSystem()
|
2004-09-03 19:59:05 +00:00
|
|
|
{
|
2004-08-25 08:55:53 +00:00
|
|
|
IConsoleClose();
|
2005-03-09 19:48:20 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
_last_z_window = _z_windows;
|
2008-04-19 13:28:48 +00:00
|
|
|
_mouseover_last_w = NULL;
|
2005-02-22 14:52:20 +00:00
|
|
|
_no_scroll = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-13 19:01:26 +00:00
|
|
|
/**
|
|
|
|
* Close down the windowing system
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void UnInitWindowSystem()
|
2005-03-09 19:48:20 +00:00
|
|
|
{
|
2008-05-07 08:27:55 +00:00
|
|
|
while (_last_z_window != _z_windows) delete _z_windows[0];
|
2005-03-09 19:48:20 +00:00
|
|
|
}
|
|
|
|
|
2008-04-13 19:01:26 +00:00
|
|
|
/**
|
|
|
|
* Reset the windowing system, by means of shutting it down followed by re-initialization
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void ResetWindowSystem()
|
2005-03-09 19:48:20 +00:00
|
|
|
{
|
|
|
|
UnInitWindowSystem();
|
|
|
|
InitWindowSystem();
|
2005-05-02 17:14:31 +00:00
|
|
|
_thd.pos.x = 0;
|
|
|
|
_thd.pos.y = 0;
|
2006-01-24 18:08:04 +00:00
|
|
|
_thd.new_pos.x = 0;
|
|
|
|
_thd.new_pos.y = 0;
|
2005-03-09 19:48:20 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void DecreaseWindowCounters()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
for (wz = _last_z_window; wz != _z_windows;) {
|
2008-05-07 08:27:55 +00:00
|
|
|
Window *w = *--wz;
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Unclick scrollbar buttons if they are pressed. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->flags4 & (WF_SCROLL_DOWN | WF_SCROLL_UP)) {
|
|
|
|
w->flags4 &= ~(WF_SCROLL_DOWN | WF_SCROLL_UP);
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-05-10 13:46:36 +00:00
|
|
|
w->OnMouseLoop();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
for (wz = _last_z_window; wz != _z_windows;) {
|
2008-05-07 08:27:55 +00:00
|
|
|
Window *w = *--wz;
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
if (w->flags4 & WF_TIMEOUT_MASK && !(--w->flags4 & WF_TIMEOUT_MASK)) {
|
2008-05-10 13:46:36 +00:00
|
|
|
w->OnTimeout();
|
2007-12-08 01:20:47 +00:00
|
|
|
if (w->desc_flags & WDF_UNCLICK_BUTTONS) w->RaiseButtons();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
Window *GetCallbackWnd()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
return FindWindowById(_thd.window_class, _thd.window_number);
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void HandlePlacePresize()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_special_mouse_mode != WSM_PRESIZE) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
Window *w = GetCallbackWnd();
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w == NULL) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
Point pt = GetTileBelowCursor();
|
|
|
|
if (pt.x == -1) {
|
2004-08-09 17:04:08 +00:00
|
|
|
_thd.selend.x = -1;
|
|
|
|
return;
|
|
|
|
}
|
2008-05-10 13:46:36 +00:00
|
|
|
|
|
|
|
w->OnPlacePresize(pt, TileVirtXY(pt.x, pt.y));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static bool HandleDragDrop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_special_mouse_mode != WSM_DRAGDROP) return true;
|
|
|
|
if (_left_button_down) return false;
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
Window *w = GetCallbackWnd();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w != NULL) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* send an event in client coordinates. */
|
2008-05-10 13:46:36 +00:00
|
|
|
Point pt;
|
|
|
|
pt.x = _cursor.pos.x - w->left;
|
|
|
|
pt.y = _cursor.pos.y - w->top;
|
|
|
|
w->OnDragDrop(pt, GetWidgetFromPos(w, pt.x, pt.y));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-02-17 12:21:05 +00:00
|
|
|
|
|
|
|
ResetObjectToPlace();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static bool HandleMouseOver()
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
2008-04-13 19:06:30 +00:00
|
|
|
Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* We changed window, put a MOUSEOVER event to the last window */
|
2008-04-13 19:06:30 +00:00
|
|
|
if (_mouseover_last_w != NULL && _mouseover_last_w != w) {
|
|
|
|
/* Reset mouse-over coordinates of previous window */
|
2008-05-10 13:46:36 +00:00
|
|
|
Point pt = { -1, -1 };
|
|
|
|
_mouseover_last_w->OnMouseOver(pt, 0);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
2008-04-13 19:06:30 +00:00
|
|
|
|
|
|
|
/* _mouseover_last_w will get reset when the window is deleted, see DeleteWindow() */
|
|
|
|
_mouseover_last_w = w;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w != NULL) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* send an event in client coordinates. */
|
2008-05-10 13:46:36 +00:00
|
|
|
Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top };
|
|
|
|
int widget = 0;
|
2004-12-04 17:54:56 +00:00
|
|
|
if (w->widget != NULL) {
|
2008-05-10 13:46:36 +00:00
|
|
|
widget = GetWidgetFromPos(w, pt.x, pt.y);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
2008-05-10 13:46:36 +00:00
|
|
|
w->OnMouseOver(pt, widget);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Mouseover never stops execution */
|
2004-12-04 17:54:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Resize the window.
|
|
|
|
* Update all the widgets of a window based on their resize flags
|
2006-12-07 00:47:35 +00:00
|
|
|
* Both the areas of the old window and the new sized window are set dirty
|
|
|
|
* ensuring proper redrawal.
|
|
|
|
* @param w Window to resize
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param x delta x-size of changed window (positive if larger, etc.)
|
2008-04-07 20:28:58 +00:00
|
|
|
* @param y delta y-size of changed window
|
|
|
|
*/
|
2006-12-07 00:47:35 +00:00
|
|
|
void ResizeWindow(Window *w, int x, int y)
|
|
|
|
{
|
|
|
|
bool resize_height = false;
|
|
|
|
bool resize_width = false;
|
|
|
|
|
|
|
|
if (x == 0 && y == 0) return;
|
|
|
|
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2008-05-07 08:27:55 +00:00
|
|
|
for (Widget *wi = w->widget; wi->type != WWT_LAST; wi++) {
|
2006-12-07 00:47:35 +00:00
|
|
|
/* Isolate the resizing flags */
|
|
|
|
byte rsizeflag = GB(wi->display_flags, 0, 4);
|
|
|
|
|
|
|
|
if (rsizeflag == RESIZE_NONE) continue;
|
|
|
|
|
|
|
|
/* Resize the widget based on its resize-flag */
|
|
|
|
if (rsizeflag & RESIZE_LEFT) {
|
|
|
|
wi->left += x;
|
|
|
|
resize_width = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rsizeflag & RESIZE_RIGHT) {
|
|
|
|
wi->right += x;
|
|
|
|
resize_width = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rsizeflag & RESIZE_TOP) {
|
|
|
|
wi->top += y;
|
|
|
|
resize_height = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rsizeflag & RESIZE_BOTTOM) {
|
|
|
|
wi->bottom += y;
|
|
|
|
resize_height = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We resized at least 1 widget, so let's resize the window totally */
|
|
|
|
if (resize_width) w->width += x;
|
|
|
|
if (resize_height) w->height += y;
|
|
|
|
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2006-12-07 00:47:35 +00:00
|
|
|
}
|
2005-11-04 14:01:44 +00:00
|
|
|
|
|
|
|
static bool _dragging_window;
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static bool HandleWindowDragging()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Get out immediately if no window is being dragged at all. */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!_dragging_window) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Otherwise find the window... */
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
|
|
|
|
2004-11-10 21:14:16 +00:00
|
|
|
if (w->flags4 & WF_DRAGGING) {
|
2004-11-13 10:53:42 +00:00
|
|
|
const Widget *t = &w->widget[1]; // the title bar ... ugh
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Stop the dragging if the left mouse button was released */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!_left_button_down) {
|
2004-11-10 21:14:16 +00:00
|
|
|
w->flags4 &= ~WF_DRAGGING;
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
int x = _cursor.pos.x + _drag_delta.x;
|
|
|
|
int y = _cursor.pos.y + _drag_delta.y;
|
|
|
|
int nx = x;
|
|
|
|
int ny = y;
|
2004-11-10 21:14:16 +00:00
|
|
|
|
|
|
|
if (_patches.window_snap_radius != 0) {
|
2006-11-19 22:24:18 +00:00
|
|
|
Window* const *vz;
|
2006-11-18 16:47:02 +00:00
|
|
|
|
2004-11-11 21:20:15 +00:00
|
|
|
int hsnap = _patches.window_snap_radius;
|
|
|
|
int vsnap = _patches.window_snap_radius;
|
|
|
|
int delta;
|
2004-11-10 21:14:16 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(vz) {
|
|
|
|
const Window *v = *vz;
|
|
|
|
|
2004-11-10 21:14:16 +00:00
|
|
|
if (v == w) continue; // Don't snap at yourself
|
|
|
|
|
|
|
|
if (y + w->height > v->top && y < v->top + v->height) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Your left border <-> other right border */
|
2004-11-10 21:14:16 +00:00
|
|
|
delta = abs(v->left + v->width - x);
|
|
|
|
if (delta <= hsnap) {
|
|
|
|
nx = v->left + v->width;
|
|
|
|
hsnap = delta;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Your right border <-> other left border */
|
2004-11-10 21:14:16 +00:00
|
|
|
delta = abs(v->left - x - w->width);
|
|
|
|
if (delta <= hsnap) {
|
|
|
|
nx = v->left - w->width;
|
|
|
|
hsnap = delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-11 21:20:15 +00:00
|
|
|
if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Your left border <-> other left border */
|
2004-11-11 21:20:15 +00:00
|
|
|
delta = abs(v->left - x);
|
|
|
|
if (delta <= hsnap) {
|
|
|
|
nx = v->left;
|
|
|
|
hsnap = delta;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Your right border <-> other right border */
|
2004-11-11 21:20:15 +00:00
|
|
|
delta = abs(v->left + v->width - x - w->width);
|
|
|
|
if (delta <= hsnap) {
|
|
|
|
nx = v->left + v->width - w->width;
|
|
|
|
hsnap = delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-10 21:14:16 +00:00
|
|
|
if (x + w->width > v->left && x < v->left + v->width) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Your top border <-> other bottom border */
|
2004-11-10 21:14:16 +00:00
|
|
|
delta = abs(v->top + v->height - y);
|
|
|
|
if (delta <= vsnap) {
|
|
|
|
ny = v->top + v->height;
|
|
|
|
vsnap = delta;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Your bottom border <-> other top border */
|
2004-11-10 21:14:16 +00:00
|
|
|
delta = abs(v->top - y - w->height);
|
|
|
|
if (delta <= vsnap) {
|
|
|
|
ny = v->top - w->height;
|
|
|
|
vsnap = delta;
|
|
|
|
}
|
|
|
|
}
|
2004-11-11 21:20:15 +00:00
|
|
|
|
|
|
|
if (w->left + w->width >= v->left && w->left <= v->left + v->width) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Your top border <-> other top border */
|
2004-11-11 21:20:15 +00:00
|
|
|
delta = abs(v->top - y);
|
|
|
|
if (delta <= vsnap) {
|
|
|
|
ny = v->top;
|
|
|
|
vsnap = delta;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Your bottom border <-> other bottom border */
|
2004-11-11 21:20:15 +00:00
|
|
|
delta = abs(v->top + v->height - y - w->height);
|
|
|
|
if (delta <= vsnap) {
|
|
|
|
ny = v->top + v->height - w->height;
|
|
|
|
vsnap = delta;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Make sure the window doesn't leave the screen
|
|
|
|
* 13 is the height of the title bar */
|
2007-11-19 18:38:10 +00:00
|
|
|
nx = Clamp(nx, 13 - t->right, _screen.width - 13 - t->left);
|
|
|
|
ny = Clamp(ny, 0, _screen.height - 13);
|
2004-11-10 21:14:16 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Make sure the title bar isn't hidden by behind the main tool bar */
|
2008-05-07 08:27:55 +00:00
|
|
|
Window *v = FindWindowById(WC_MAIN_TOOLBAR, 0);
|
2004-11-13 10:53:42 +00:00
|
|
|
if (v != NULL) {
|
|
|
|
int v_bottom = v->top + v->height;
|
|
|
|
int v_right = v->left + v->width;
|
|
|
|
if (ny + t->top >= v->top && ny + t->top < v_bottom) {
|
2004-11-13 11:08:50 +00:00
|
|
|
if ((v->left < 13 && nx + t->left < v->left) ||
|
|
|
|
(v_right > _screen.width - 13 && nx + t->right > v_right)) {
|
2004-11-13 10:53:42 +00:00
|
|
|
ny = v_bottom;
|
|
|
|
} else {
|
|
|
|
if (nx + t->left > v->left - 13 &&
|
|
|
|
nx + t->right < v_right + 13) {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->top >= v_bottom) {
|
2004-11-13 10:53:42 +00:00
|
|
|
ny = v_bottom;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else if (w->left < nx) {
|
2004-11-13 10:53:42 +00:00
|
|
|
nx = v->left - 13 - t->left;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2004-11-13 10:53:42 +00:00
|
|
|
nx = v_right + 13 - t->right;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-11-13 10:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-10 21:14:16 +00:00
|
|
|
if (w->viewport != NULL) {
|
|
|
|
w->viewport->left += nx - w->left;
|
|
|
|
w->viewport->top += ny - w->top;
|
|
|
|
}
|
|
|
|
w->left = nx;
|
|
|
|
w->top = ny;
|
|
|
|
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2005-01-03 19:45:18 +00:00
|
|
|
return false;
|
|
|
|
} else if (w->flags4 & WF_SIZING) {
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
/* Stop the sizing if the left mouse button was released */
|
|
|
|
if (!_left_button_down) {
|
|
|
|
w->flags4 &= ~WF_SIZING;
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2005-01-03 19:45:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = _cursor.pos.x - _drag_delta.x;
|
|
|
|
y = _cursor.pos.y - _drag_delta.y;
|
|
|
|
|
2005-11-18 20:28:55 +00:00
|
|
|
/* X and Y has to go by step.. calculate it.
|
|
|
|
* The cast to int is necessary else x/y are implicitly casted to
|
|
|
|
* unsigned int, which won't work. */
|
|
|
|
if (w->resize.step_width > 1) x -= x % (int)w->resize.step_width;
|
2005-01-03 19:45:18 +00:00
|
|
|
|
2005-11-18 20:28:55 +00:00
|
|
|
if (w->resize.step_height > 1) y -= y % (int)w->resize.step_height;
|
2005-01-03 19:45:18 +00:00
|
|
|
|
|
|
|
/* Check if we don't go below the minimum set size */
|
|
|
|
if ((int)w->width + x < (int)w->resize.width)
|
|
|
|
x = w->resize.width - w->width;
|
|
|
|
if ((int)w->height + y < (int)w->resize.height)
|
|
|
|
y = w->resize.height - w->height;
|
|
|
|
|
|
|
|
/* Window already on size */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (x == 0 && y == 0) return false;
|
2005-01-03 19:45:18 +00:00
|
|
|
|
|
|
|
/* Now find the new cursor pos.. this is NOT _cursor, because
|
|
|
|
we move in steps. */
|
|
|
|
_drag_delta.x += x;
|
|
|
|
_drag_delta.y += y;
|
|
|
|
|
2006-12-07 00:47:35 +00:00
|
|
|
/* ResizeWindow sets both pre- and after-size to dirty for redrawal */
|
|
|
|
ResizeWindow(w, x, y);
|
2005-01-03 19:45:18 +00:00
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
Point size;
|
|
|
|
Point diff;
|
|
|
|
size.x = x + w->width;
|
|
|
|
size.y = y + w->height;
|
|
|
|
diff.x = x;
|
|
|
|
diff.y = y;
|
|
|
|
w->OnResize(size, diff);
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_dragging_window = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Start window dragging
|
|
|
|
* @param w Window to start dragging
|
|
|
|
*/
|
2006-11-18 16:47:02 +00:00
|
|
|
static void StartWindowDrag(Window *w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
w->flags4 |= WF_DRAGGING;
|
|
|
|
_dragging_window = true;
|
2005-01-03 19:45:18 +00:00
|
|
|
|
2004-11-10 21:14:16 +00:00
|
|
|
_drag_delta.x = w->left - _cursor.pos.x;
|
|
|
|
_drag_delta.y = w->top - _cursor.pos.y;
|
2005-01-03 19:45:18 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
BringWindowToFront(w);
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_DROPDOWN_MENU, 0);
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Start resizing a window
|
|
|
|
* @param w Window to start resizing
|
|
|
|
*/
|
2006-11-18 16:47:02 +00:00
|
|
|
static void StartWindowSizing(Window *w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
w->flags4 |= WF_SIZING;
|
|
|
|
_dragging_window = true;
|
2005-01-03 19:45:18 +00:00
|
|
|
|
|
|
|
_drag_delta.x = _cursor.pos.x;
|
|
|
|
_drag_delta.y = _cursor.pos.y;
|
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
BringWindowToFront(w);
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_DROPDOWN_MENU, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static bool HandleScrollbarScrolling()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Get out quickly if no item is being scrolled */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!_scrolling_scrollbar) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Find the scrolling window */
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->flags4 & WF_SCROLL_MIDDLE) {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Abort if no button is clicked any more. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!_left_button_down) {
|
|
|
|
w->flags4 &= ~WF_SCROLL_MIDDLE;
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
2004-09-05 14:20:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
int i;
|
|
|
|
Scrollbar *sb;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->flags4 & WF_HSCROLL) {
|
|
|
|
sb = &w->hscroll;
|
|
|
|
i = _cursor.pos.x - _cursorpos_drag_start.x;
|
2005-01-02 17:23:04 +00:00
|
|
|
} else if (w->flags4 & WF_SCROLL2){
|
|
|
|
sb = &w->vscroll2;
|
|
|
|
i = _cursor.pos.y - _cursorpos_drag_start.y;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
sb = &w->vscroll;
|
|
|
|
i = _cursor.pos.y - _cursorpos_drag_start.y;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Find the item we want to move to and make sure it's inside bounds. */
|
2008-05-07 08:27:55 +00:00
|
|
|
int pos = min(max(0, i + _scrollbar_start_pos) * sb->count / _scrollbar_size, max(0, sb->count - sb->cap));
|
2004-08-09 17:04:08 +00:00
|
|
|
if (pos != sb->pos) {
|
|
|
|
sb->pos = pos;
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
2004-09-05 14:20:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_scrolling_scrollbar = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static bool HandleViewportScroll()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-11 10:55:35 +00:00
|
|
|
bool scrollwheel_scrolling = _patches.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!_scrolling_viewport) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y);
|
2006-08-21 14:34:59 +00:00
|
|
|
|
2007-11-11 12:26:44 +00:00
|
|
|
if (!(_right_button_down || scrollwheel_scrolling) || w == NULL) {
|
|
|
|
_cursor.fix_at = false;
|
|
|
|
_scrolling_viewport = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
if (w == FindWindowById(WC_MAIN_WINDOW, 0) && w->viewport->follow_vehicle != INVALID_VEHICLE) {
|
2007-10-20 00:09:39 +00:00
|
|
|
/* If the main window is following a vehicle, then first let go of it! */
|
2008-05-11 15:08:44 +00:00
|
|
|
const Vehicle *veh = GetVehicle(w->viewport->follow_vehicle);
|
2007-10-20 00:09:39 +00:00
|
|
|
ScrollMainWindowTo(veh->x_pos, veh->y_pos, true); /* This also resets follow_vehicle */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
Point delta;
|
2005-11-19 12:37:28 +00:00
|
|
|
if (_patches.reverse_scroll) {
|
2008-05-10 13:46:36 +00:00
|
|
|
delta.x = -_cursor.delta.x;
|
|
|
|
delta.y = -_cursor.delta.y;
|
2005-11-19 12:37:28 +00:00
|
|
|
} else {
|
2008-05-10 13:46:36 +00:00
|
|
|
delta.x = _cursor.delta.x;
|
|
|
|
delta.y = _cursor.delta.y;
|
2005-11-19 12:37:28 +00:00
|
|
|
}
|
|
|
|
|
2007-03-11 10:55:35 +00:00
|
|
|
if (scrollwheel_scrolling) {
|
|
|
|
/* We are using scrollwheels for scrolling */
|
2008-05-10 13:46:36 +00:00
|
|
|
delta.x = _cursor.h_wheel;
|
|
|
|
delta.y = _cursor.v_wheel;
|
2007-03-11 10:55:35 +00:00
|
|
|
_cursor.v_wheel = 0;
|
|
|
|
_cursor.h_wheel = 0;
|
|
|
|
}
|
|
|
|
|
2006-08-21 14:34:59 +00:00
|
|
|
/* Create a scroll-event and send it to the window */
|
2008-05-10 13:46:36 +00:00
|
|
|
w->OnScroll(delta);
|
2006-02-06 09:18:04 +00:00
|
|
|
|
|
|
|
_cursor.delta.x = 0;
|
|
|
|
_cursor.delta.y = 0;
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-12-29 17:16:12 +00:00
|
|
|
/** Check if a window can be made top-most window, and if so do
|
|
|
|
* it. If a window does not obscure any other windows, it will not
|
|
|
|
* be brought to the foreground. Also if the only obscuring windows
|
|
|
|
* are so-called system-windows, the window will not be moved.
|
|
|
|
* The function will return false when a child window of this window is a
|
|
|
|
* modal-popup; function returns a false and child window gets a white border
|
|
|
|
* @param w Window to bring on-top
|
|
|
|
* @return false if the window has an active modal child, true otherwise */
|
|
|
|
static bool MaybeBringWindowToFront(const Window *w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-12-29 17:16:12 +00:00
|
|
|
bool bring_to_front = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class == WC_MAIN_WINDOW ||
|
|
|
|
IsVitalWindow(w) ||
|
|
|
|
w->window_class == WC_TOOLTIPS ||
|
|
|
|
w->window_class == WC_DROPDOWN_MENU) {
|
2006-12-29 17:16:12 +00:00
|
|
|
return true;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
Window * const *wz = FindWindowZPosition(w);
|
2008-04-19 13:28:48 +00:00
|
|
|
for (Window * const *uz = wz; ++uz != _last_z_window;) {
|
2006-12-29 17:16:12 +00:00
|
|
|
Window *u = *uz;
|
|
|
|
|
|
|
|
/* A modal child will prevent the activation of the parent window */
|
|
|
|
if (u->parent == w && (u->desc_flags & WDF_MODAL)) {
|
|
|
|
u->flags4 |= WF_WHITE_BORDER_MASK;
|
2008-05-06 22:08:18 +00:00
|
|
|
u->SetDirty();
|
2006-12-29 17:16:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-11-18 16:47:02 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u->window_class == WC_MAIN_WINDOW ||
|
|
|
|
IsVitalWindow(u) ||
|
|
|
|
u->window_class == WC_TOOLTIPS ||
|
|
|
|
u->window_class == WC_DROPDOWN_MENU) {
|
|
|
|
continue;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-12-29 17:16:12 +00:00
|
|
|
/* Window sizes don't interfere, leave z-order alone */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->left + w->width <= u->left ||
|
|
|
|
u->left + u->width <= w->left ||
|
|
|
|
w->top + w->height <= u->top ||
|
2005-11-14 19:48:04 +00:00
|
|
|
u->top + u->height <= w->top) {
|
|
|
|
continue;
|
|
|
|
}
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2006-12-29 17:16:12 +00:00
|
|
|
bring_to_front = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-12-29 17:16:12 +00:00
|
|
|
|
|
|
|
if (bring_to_front) BringWindowToFront(w);
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-11-15 19:35:52 +00:00
|
|
|
/** Handle keyboard input.
|
2008-05-10 13:46:36 +00:00
|
|
|
* @param raw_key Lower 8 bits contain the ASCII character, the higher 16 bits the keycode
|
2008-04-07 20:28:58 +00:00
|
|
|
*/
|
2008-05-10 13:46:36 +00:00
|
|
|
void HandleKeypress(uint32 raw_key)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-12-06 15:58:39 +00:00
|
|
|
/* Stores if a window with a textfield for typing is open
|
|
|
|
* If this is the case, keypress events are only passed to windows with text fields and
|
|
|
|
* to thein this main toolbar. */
|
|
|
|
bool query_open = false;
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2006-11-15 19:35:52 +00:00
|
|
|
/*
|
|
|
|
* During the generation of the world, there might be
|
|
|
|
* another thread that is currently building for example
|
|
|
|
* a road. To not interfere with those tasks, we should
|
|
|
|
* NOT change the _current_player here.
|
|
|
|
*
|
|
|
|
* This is not necessary either, as the only events that
|
|
|
|
* can be handled are the 'close application' events
|
|
|
|
*/
|
|
|
|
if (!IsGeneratingWorld()) _current_player = _local_player;
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Setup event */
|
2008-05-10 13:46:36 +00:00
|
|
|
uint16 key = GB(raw_key, 0, 16);
|
|
|
|
uint16 keycode = GB(raw_key, 16, 16);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-01 18:55:15 +00:00
|
|
|
/*
|
|
|
|
* The Unicode standard defines an area called the private use area. Code points in this
|
|
|
|
* area are reserved for private use and thus not portable between systems. For instance,
|
|
|
|
* Apple defines code points for the arrow keys in this area, but these are only printable
|
|
|
|
* on a system running OS X. We don't want these keys to show up in text fields and such,
|
|
|
|
* and thus we have to clear the unicode character when we encounter such a key.
|
|
|
|
*/
|
2008-05-10 13:46:36 +00:00
|
|
|
if (key >= 0xE000 && key <= 0xF8FF) key = 0;
|
2008-01-01 18:55:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If both key and keycode is zero, we don't bother to process the event.
|
|
|
|
*/
|
2008-05-10 13:46:36 +00:00
|
|
|
if (key == 0 && keycode == 0) return;
|
2008-01-01 18:55:15 +00:00
|
|
|
|
2007-12-06 15:58:39 +00:00
|
|
|
/* check if we have a query string window open before allowing hotkeys */
|
|
|
|
if (FindWindowById(WC_QUERY_STRING, 0) != NULL ||
|
|
|
|
FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL ||
|
|
|
|
FindWindowById(WC_GENERATE_LANDSCAPE, 0) != NULL ||
|
|
|
|
FindWindowById(WC_CONSOLE, 0) != NULL ||
|
|
|
|
FindWindowById(WC_SAVELOAD, 0) != NULL ||
|
|
|
|
FindWindowById(WC_COMPANY_PASSWORD_WINDOW, 0) != NULL) {
|
|
|
|
query_open = true;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Call the event, start with the uppermost window. */
|
2008-04-07 20:28:58 +00:00
|
|
|
for (Window* const *wz = _last_z_window; wz != _z_windows;) {
|
2006-11-18 16:47:02 +00:00
|
|
|
Window *w = *--wz;
|
|
|
|
|
2007-12-06 15:58:39 +00:00
|
|
|
/* if a query window is open, only call the event for certain window types */
|
|
|
|
if (query_open &&
|
|
|
|
w->window_class != WC_QUERY_STRING &&
|
|
|
|
w->window_class != WC_SEND_NETWORK_MSG &&
|
|
|
|
w->window_class != WC_GENERATE_LANDSCAPE &&
|
|
|
|
w->window_class != WC_CONSOLE &&
|
|
|
|
w->window_class != WC_SAVELOAD &&
|
|
|
|
w->window_class != WC_COMPANY_PASSWORD_WINDOW) {
|
2004-12-14 17:38:48 +00:00
|
|
|
continue;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2008-05-10 13:46:36 +00:00
|
|
|
;
|
|
|
|
if (!w->OnKeyPress(key, keycode)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-04-03 13:35:43 +00:00
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
|
|
|
|
/* When there is no toolbar w is null, check for that */
|
|
|
|
if (w != NULL) w->OnKeyPress(key, keycode);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* State of CONTROL key has changed
|
|
|
|
*/
|
2008-02-17 17:00:43 +00:00
|
|
|
void HandleCtrlChanged()
|
|
|
|
{
|
|
|
|
/* Call the event, start with the uppermost window. */
|
|
|
|
for (Window* const *wz = _last_z_window; wz != _z_windows;) {
|
|
|
|
Window *w = *--wz;
|
2008-05-10 13:46:36 +00:00
|
|
|
if (!w->OnCTRLStateChange()) break;
|
2008-02-17 17:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Local counter that is incremented each time an mouse input event is detected.
|
|
|
|
* The counter is used to stop auto-scrolling.
|
|
|
|
* @see HandleAutoscroll()
|
|
|
|
* @see HandleMouseEvents()
|
|
|
|
*/
|
2006-11-15 21:01:19 +00:00
|
|
|
static int _input_events_this_tick = 0;
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* If needed and switched on, perform auto scrolling (automatically
|
|
|
|
* moving window contents when mouse is near edge of the window).
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static void HandleAutoscroll()
|
2006-11-15 21:01:19 +00:00
|
|
|
{
|
|
|
|
if (_input_events_this_tick != 0) {
|
|
|
|
/* HandleAutoscroll is called only once per GameLoop() - so we can clear the counter here */
|
|
|
|
_input_events_this_tick = 0;
|
|
|
|
/* there were some inputs this tick, don't scroll ??? */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_patches.autoscroll && _game_mode != GM_MENU && !IsGeneratingWorld()) {
|
2008-05-07 08:27:55 +00:00
|
|
|
int x = _cursor.pos.x;
|
|
|
|
int y = _cursor.pos.y;
|
|
|
|
Window *w = FindWindowFromPt(x, y);
|
2006-11-15 21:01:19 +00:00
|
|
|
if (w == NULL || w->flags4 & WF_DISABLE_VP_SCROLL) return;
|
2008-05-07 08:27:55 +00:00
|
|
|
ViewPort *vp = IsPtInWindowViewport(w, x, y);
|
2006-11-15 21:01:19 +00:00
|
|
|
if (vp != NULL) {
|
|
|
|
x -= vp->left;
|
|
|
|
y -= vp->top;
|
2008-05-07 08:27:55 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* here allows scrolling in both x and y axis */
|
2006-11-15 21:01:19 +00:00
|
|
|
#define scrollspeed 3
|
|
|
|
if (x - 15 < 0) {
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->dest_scrollpos_x += ScaleByZoom((x - 15) * scrollspeed, vp->zoom);
|
2006-11-15 21:01:19 +00:00
|
|
|
} else if (15 - (vp->width - x) > 0) {
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->dest_scrollpos_x += ScaleByZoom((15 - (vp->width - x)) * scrollspeed, vp->zoom);
|
2006-11-15 21:01:19 +00:00
|
|
|
}
|
|
|
|
if (y - 15 < 0) {
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->dest_scrollpos_y += ScaleByZoom((y - 15) * scrollspeed, vp->zoom);
|
2006-11-15 21:01:19 +00:00
|
|
|
} else if (15 - (vp->height - y) > 0) {
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->dest_scrollpos_y += ScaleByZoom((15 - (vp->height - y)) * scrollspeed, vp->zoom);
|
2006-11-15 21:01:19 +00:00
|
|
|
}
|
|
|
|
#undef scrollspeed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-22 10:57:53 +00:00
|
|
|
enum MouseClick {
|
|
|
|
MC_NONE = 0,
|
|
|
|
MC_LEFT,
|
|
|
|
MC_RIGHT,
|
|
|
|
MC_DOUBLE_LEFT,
|
|
|
|
|
2007-06-22 20:04:21 +00:00
|
|
|
MAX_OFFSET_DOUBLE_CLICK = 5, ///< How much the mouse is allowed to move to call it a double click
|
|
|
|
TIME_BETWEEN_DOUBLE_CLICK = 500, ///< Time between 2 left clicks before it becoming a double click, in ms
|
2007-06-22 10:57:53 +00:00
|
|
|
};
|
|
|
|
|
2008-04-19 13:28:48 +00:00
|
|
|
extern void UpdateTileSelection();
|
|
|
|
extern bool VpHandlePlaceSizingDrag();
|
|
|
|
|
2007-06-22 10:57:53 +00:00
|
|
|
void MouseLoop(MouseClick click, int mousewheel)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
DecreaseWindowCounters();
|
|
|
|
HandlePlacePresize();
|
|
|
|
UpdateTileSelection();
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!VpHandlePlaceSizingDrag()) return;
|
|
|
|
if (!HandleDragDrop()) return;
|
|
|
|
if (!HandleWindowDragging()) return;
|
|
|
|
if (!HandleScrollbarScrolling()) return;
|
|
|
|
if (!HandleViewportScroll()) return;
|
|
|
|
if (!HandleMouseOver()) return;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
bool scrollwheel_scrolling = _patches.scrollwheel_scrolling == 1 && (_cursor.v_wheel != 0 || _cursor.h_wheel != 0);
|
2007-06-22 10:57:53 +00:00
|
|
|
if (click == MC_NONE && mousewheel == 0 && !scrollwheel_scrolling) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
int x = _cursor.pos.x;
|
|
|
|
int y = _cursor.pos.y;
|
|
|
|
Window *w = FindWindowFromPt(x, y);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w == NULL) return;
|
2008-05-07 08:27:55 +00:00
|
|
|
|
2006-12-29 17:16:12 +00:00
|
|
|
if (!MaybeBringWindowToFront(w)) return;
|
2008-05-07 08:27:55 +00:00
|
|
|
ViewPort *vp = IsPtInWindowViewport(w, x, y);
|
2006-08-21 14:59:58 +00:00
|
|
|
|
|
|
|
/* Don't allow any action in a viewport if either in menu of in generating world */
|
|
|
|
if (vp != NULL && (_game_mode == GM_MENU || IsGeneratingWorld())) return;
|
|
|
|
|
|
|
|
if (mousewheel != 0) {
|
2007-03-11 21:53:46 +00:00
|
|
|
if (_patches.scrollwheel_scrolling == 0) {
|
|
|
|
/* Send WE_MOUSEWHEEL event to window */
|
2008-05-10 13:46:36 +00:00
|
|
|
w->OnMouseWheel(mousewheel);
|
2007-03-11 21:53:46 +00:00
|
|
|
}
|
2006-08-21 14:59:58 +00:00
|
|
|
|
|
|
|
/* Dispatch a MouseWheelEvent for widgets if it is not a viewport */
|
|
|
|
if (vp == NULL) DispatchMouseWheelEvent(w, GetWidgetFromPos(w, x - w->left, y - w->top), mousewheel);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (vp != NULL) {
|
2007-06-22 10:57:53 +00:00
|
|
|
if (scrollwheel_scrolling) click = MC_RIGHT; // we are using the scrollwheel in a viewport, so we emulate right mouse button
|
2006-08-21 14:59:58 +00:00
|
|
|
switch (click) {
|
2007-06-22 10:57:53 +00:00
|
|
|
case MC_DOUBLE_LEFT:
|
|
|
|
case MC_LEFT:
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 2, "Cursor: 0x%X (%d)", _cursor.sprite, _cursor.sprite);
|
2007-11-18 23:13:53 +00:00
|
|
|
if (_thd.place_mode != VHM_NONE &&
|
2007-04-06 04:10:19 +00:00
|
|
|
/* query button and place sign button work in pause mode */
|
2006-08-21 14:59:58 +00:00
|
|
|
_cursor.sprite != SPR_CURSOR_QUERY &&
|
|
|
|
_cursor.sprite != SPR_CURSOR_SIGN &&
|
2007-03-06 20:59:52 +00:00
|
|
|
_pause_game != 0 &&
|
2006-08-21 14:59:58 +00:00
|
|
|
!_cheats.build_in_pause.value) {
|
|
|
|
return;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-18 23:13:53 +00:00
|
|
|
if (_thd.place_mode == VHM_NONE) {
|
2006-08-21 14:59:58 +00:00
|
|
|
HandleViewportClicked(vp, x, y);
|
|
|
|
} else {
|
|
|
|
PlaceObject();
|
|
|
|
}
|
|
|
|
break;
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2007-06-22 10:57:53 +00:00
|
|
|
case MC_RIGHT:
|
2006-08-21 14:59:58 +00:00
|
|
|
if (!(w->flags4 & WF_DISABLE_VP_SCROLL)) {
|
|
|
|
_scrolling_viewport = true;
|
|
|
|
_cursor.fix_at = true;
|
|
|
|
}
|
|
|
|
break;
|
2007-06-22 10:57:53 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-11-13 14:54:09 +00:00
|
|
|
switch (click) {
|
2008-05-07 21:45:27 +00:00
|
|
|
case MC_DOUBLE_LEFT:
|
|
|
|
DispatchLeftClickEvent(w, x - w->left, y - w->top, true);
|
|
|
|
if (_mouseover_last_w == NULL) break; // The window got removed.
|
|
|
|
/* fallthough, and also give a single-click for backwards compatibility */
|
|
|
|
case MC_LEFT:
|
|
|
|
DispatchLeftClickEvent(w, x - w->left, y - w->top, false);
|
|
|
|
break;
|
|
|
|
|
2007-03-11 11:57:11 +00:00
|
|
|
default:
|
|
|
|
if (!scrollwheel_scrolling || w == NULL || w->window_class != WC_SMALLMAP) break;
|
|
|
|
/* We try to use the scrollwheel to scroll since we didn't touch any of the buttons.
|
|
|
|
* Simulate a right button click so we can get started. */
|
|
|
|
/* fallthough */
|
2007-06-22 10:57:53 +00:00
|
|
|
case MC_RIGHT: DispatchRightClickEvent(w, x - w->left, y - w->top); break;
|
2005-11-13 14:54:09 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Handle a mouse event from the video driver
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void HandleMouseEvents()
|
2005-03-26 04:16:39 +00:00
|
|
|
{
|
2007-06-22 10:57:53 +00:00
|
|
|
static int double_click_time = 0;
|
|
|
|
static int double_click_x = 0;
|
|
|
|
static int double_click_y = 0;
|
2005-03-26 04:16:39 +00:00
|
|
|
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
/*
|
|
|
|
* During the generation of the world, there might be
|
|
|
|
* another thread that is currently building for example
|
|
|
|
* a road. To not interfere with those tasks, we should
|
|
|
|
* NOT change the _current_player here.
|
|
|
|
*
|
|
|
|
* This is not necessary either, as the only events that
|
|
|
|
* can be handled are the 'close application' events
|
|
|
|
*/
|
|
|
|
if (!IsGeneratingWorld()) _current_player = _local_player;
|
2005-03-26 04:16:39 +00:00
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Mouse event? */
|
2008-05-07 08:27:55 +00:00
|
|
|
MouseClick click = MC_NONE;
|
2005-03-26 04:16:39 +00:00
|
|
|
if (_left_button_down && !_left_button_clicked) {
|
2007-06-22 10:57:53 +00:00
|
|
|
click = MC_LEFT;
|
2007-06-22 20:04:21 +00:00
|
|
|
if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
|
2007-06-22 10:57:53 +00:00
|
|
|
double_click_x != 0 && abs(_cursor.pos.x - double_click_x) < MAX_OFFSET_DOUBLE_CLICK &&
|
|
|
|
double_click_y != 0 && abs(_cursor.pos.y - double_click_y) < MAX_OFFSET_DOUBLE_CLICK) {
|
|
|
|
click = MC_DOUBLE_LEFT;
|
|
|
|
}
|
2007-06-22 20:04:21 +00:00
|
|
|
double_click_time = _realtime_tick;
|
2007-06-22 10:57:53 +00:00
|
|
|
double_click_x = _cursor.pos.x;
|
|
|
|
double_click_y = _cursor.pos.y;
|
2005-03-26 04:16:39 +00:00
|
|
|
_left_button_clicked = true;
|
2006-11-15 21:01:19 +00:00
|
|
|
_input_events_this_tick++;
|
2005-03-26 04:16:39 +00:00
|
|
|
} else if (_right_button_clicked) {
|
|
|
|
_right_button_clicked = false;
|
2007-06-22 10:57:53 +00:00
|
|
|
click = MC_RIGHT;
|
2006-11-15 21:01:19 +00:00
|
|
|
_input_events_this_tick++;
|
2005-03-26 04:16:39 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 08:27:55 +00:00
|
|
|
int mousewheel = 0;
|
2005-03-26 04:16:39 +00:00
|
|
|
if (_cursor.wheel) {
|
|
|
|
mousewheel = _cursor.wheel;
|
|
|
|
_cursor.wheel = 0;
|
2006-11-15 21:01:19 +00:00
|
|
|
_input_events_this_tick++;
|
2005-03-26 04:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MouseLoop(click, mousewheel);
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Regular call from the global game loop
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void InputLoop()
|
2006-11-15 21:01:19 +00:00
|
|
|
{
|
|
|
|
HandleMouseEvents();
|
|
|
|
HandleAutoscroll();
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Update the continuously changing contents of the windows, such as the viewports
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void UpdateWindows()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2006-11-15 19:49:16 +00:00
|
|
|
static int we4_timer = 0;
|
|
|
|
int t = we4_timer + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (t >= 100) {
|
2006-11-18 16:47:02 +00:00
|
|
|
for (wz = _last_z_window; wz != _z_windows;) {
|
2008-05-10 13:46:36 +00:00
|
|
|
(*--wz)->OnHundredthTick();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
t = 0;
|
|
|
|
}
|
2006-11-15 19:49:16 +00:00
|
|
|
we4_timer = t;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
for (wz = _last_z_window; wz != _z_windows;) {
|
|
|
|
Window *w = *--wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->flags4 & WF_WHITE_BORDER_MASK) {
|
|
|
|
w->flags4 -= WF_WHITE_BORDER_ONE;
|
2006-11-18 00:14:43 +00:00
|
|
|
|
2008-05-06 22:08:18 +00:00
|
|
|
if (!(w->flags4 & WF_WHITE_BORDER_MASK)) w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawDirtyBlocks();
|
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
if ((*wz)->viewport != NULL) UpdateViewportPosition(*wz);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-08-19 09:38:30 +00:00
|
|
|
DrawChatMessage();
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Redraw mouse cursor in case it was hidden */
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawMouseCursor();
|
2004-09-05 14:20:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
2008-04-19 13:05:05 +00:00
|
|
|
* Mark window as dirty (in need of repainting)
|
|
|
|
* @param cls Window class
|
|
|
|
* @param number Window number in that class
|
2008-04-07 20:28:58 +00:00
|
|
|
*/
|
2005-12-24 15:01:17 +00:00
|
|
|
void InvalidateWindow(WindowClass cls, WindowNumber number)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-19 22:24:18 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const Window *w = *wz;
|
2008-05-06 22:08:18 +00:00
|
|
|
if (w->window_class == cls && w->window_number == number) w->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 13:05:05 +00:00
|
|
|
/**
|
2008-04-07 20:28:58 +00:00
|
|
|
* Mark a particular widget in a particular window as dirty (in need of repainting)
|
|
|
|
* @param cls Window class
|
|
|
|
* @param number Window number in that class
|
|
|
|
* @param widget_index Index number of the widget that needs repainting
|
|
|
|
*/
|
2005-12-24 15:01:17 +00:00
|
|
|
void InvalidateWindowWidget(WindowClass cls, WindowNumber number, byte widget_index)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-19 22:24:18 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const Window *w = *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->window_class == cls && w->window_number == number) {
|
2007-12-07 18:05:49 +00:00
|
|
|
w->InvalidateWidget(widget_index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 13:05:05 +00:00
|
|
|
/**
|
2008-04-07 20:28:58 +00:00
|
|
|
* Mark all windows of a particular class as dirty (in need of repainting)
|
|
|
|
* @param cls Window class
|
|
|
|
*/
|
2005-12-24 15:01:17 +00:00
|
|
|
void InvalidateWindowClasses(WindowClass cls)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-19 22:24:18 +00:00
|
|
|
Window* const *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
2008-05-06 22:08:18 +00:00
|
|
|
if ((*wz)->window_class == cls) (*wz)->SetDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Mark window data as invalid (in need of re-computing)
|
|
|
|
* @param w Window with invalid data
|
|
|
|
*/
|
2008-05-10 12:30:27 +00:00
|
|
|
void InvalidateThisWindowData(Window *w, int data)
|
2006-10-07 14:30:13 +00:00
|
|
|
{
|
2008-05-10 13:46:36 +00:00
|
|
|
w->OnInvalidateData(data);
|
2008-05-06 22:08:18 +00:00
|
|
|
w->SetDirty();
|
2006-10-07 14:30:13 +00:00
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
2008-04-19 13:05:05 +00:00
|
|
|
* Mark window data of the window of a given class and specific window number as invalid (in need of re-computing)
|
2008-04-07 20:28:58 +00:00
|
|
|
* @param cls Window class
|
|
|
|
* @param number Window number within the class
|
|
|
|
*/
|
2008-05-10 12:30:27 +00:00
|
|
|
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data)
|
2006-10-05 12:59:28 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2006-10-05 12:59:28 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
2008-05-10 12:30:27 +00:00
|
|
|
if (w->window_class == cls && w->window_number == number) InvalidateThisWindowData(w, data);
|
2006-10-07 14:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Mark window data of all windows of a given class as invalid (in need of re-computing)
|
|
|
|
* @param cls Window class
|
|
|
|
*/
|
2008-05-10 12:30:27 +00:00
|
|
|
void InvalidateWindowClassesData(WindowClass cls, int data)
|
2006-10-07 14:30:13 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2006-10-07 14:30:13 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
2008-05-10 12:30:27 +00:00
|
|
|
if ((*wz)->window_class == cls) InvalidateThisWindowData(*wz, data);
|
2006-10-05 12:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Dispatch WE_TICK event over all windows
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void CallWindowTickEvent()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-19 13:28:48 +00:00
|
|
|
for (Window * const *wz = _last_z_window; wz != _z_windows;) {
|
2008-05-10 13:46:36 +00:00
|
|
|
(*--wz)->OnTick();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 13:28:48 +00:00
|
|
|
/**
|
|
|
|
* Try to delete a non-vital window.
|
|
|
|
* Non-vital windows are windows other than the game selection, main toolbar,
|
|
|
|
* status bar, toolbar menu, and tooltip windows. Stickied windows are also
|
|
|
|
* considered vital.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void DeleteNonVitalWindows()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2006-11-18 00:44:09 +00:00
|
|
|
restart_search:
|
|
|
|
/* When we find the window to delete, we need to restart the search
|
2006-11-18 16:47:02 +00:00
|
|
|
* as deleting this window could cascade in deleting (many) others
|
|
|
|
* anywhere in the z-array */
|
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->window_class != WC_MAIN_WINDOW &&
|
|
|
|
w->window_class != WC_SELECT_GAME &&
|
|
|
|
w->window_class != WC_MAIN_TOOLBAR &&
|
|
|
|
w->window_class != WC_STATUS_BAR &&
|
|
|
|
w->window_class != WC_TOOLBAR_MENU &&
|
2004-12-15 23:33:04 +00:00
|
|
|
w->window_class != WC_TOOLTIPS &&
|
|
|
|
(w->flags4 & WF_STICKY) == 0) { // do not delete windows which are 'pinned'
|
2006-11-18 16:47:02 +00:00
|
|
|
|
2008-05-06 21:28:30 +00:00
|
|
|
delete w;
|
2006-11-18 00:44:09 +00:00
|
|
|
goto restart_search;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-09 15:06:24 +00:00
|
|
|
/** It is possible that a stickied window gets to a position where the
|
2004-12-22 17:37:21 +00:00
|
|
|
* 'close' button is outside the gaming area. You cannot close it then; except
|
|
|
|
* with this function. It closes all windows calling the standard function,
|
|
|
|
* then, does a little hacked loop of closing all stickied windows. Note
|
|
|
|
* that standard windows (status bar, etc.) are not stickied, so these aren't affected */
|
2007-03-07 11:47:46 +00:00
|
|
|
void DeleteAllNonVitalWindows()
|
2004-12-22 17:37:21 +00:00
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2006-11-18 00:44:09 +00:00
|
|
|
/* Delete every window except for stickied ones, then sticky ones as well */
|
2004-12-22 17:37:21 +00:00
|
|
|
DeleteNonVitalWindows();
|
2006-11-18 16:47:02 +00:00
|
|
|
|
2006-11-18 00:44:09 +00:00
|
|
|
restart_search:
|
|
|
|
/* When we find the window to delete, we need to restart the search
|
2006-11-18 16:47:02 +00:00
|
|
|
* as deleting this window could cascade in deleting (many) others
|
|
|
|
* anywhere in the z-array */
|
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
if ((*wz)->flags4 & WF_STICKY) {
|
2008-05-06 21:28:30 +00:00
|
|
|
delete *wz;
|
2006-11-18 00:44:09 +00:00
|
|
|
goto restart_search;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-12-22 17:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-09 15:06:24 +00:00
|
|
|
/** Delete all always on-top windows to get an empty screen */
|
2007-03-07 11:47:46 +00:00
|
|
|
void HideVitalWindows()
|
2005-01-11 00:54:06 +00:00
|
|
|
{
|
2007-02-02 14:32:23 +00:00
|
|
|
DeleteWindowById(WC_TOOLBAR_MENU, 0);
|
2005-01-11 00:54:06 +00:00
|
|
|
DeleteWindowById(WC_MAIN_TOOLBAR, 0);
|
|
|
|
DeleteWindowById(WC_STATUS_BAR, 0);
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* (Re)position main toolbar window at the screen
|
|
|
|
* @param w Window structure of the main toolbar window, may also be \c NULL
|
|
|
|
* @return X coordinate of left edge of the repositioned toolbar window
|
|
|
|
*/
|
2004-09-05 14:20:36 +00:00
|
|
|
int PositionMainToolbar(Window *w)
|
2004-08-16 21:02:06 +00:00
|
|
|
{
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 5, "Repositioning Main Toolbar...");
|
2004-08-16 21:02:06 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
if (w == NULL || w->window_class != WC_MAIN_TOOLBAR) {
|
2004-08-16 21:02:06 +00:00
|
|
|
w = FindWindowById(WC_MAIN_TOOLBAR, 0);
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-08-16 21:02:06 +00:00
|
|
|
|
|
|
|
switch (_patches.toolbar_pos) {
|
2006-11-11 09:47:44 +00:00
|
|
|
case 1: w->left = (_screen.width - w->width) / 2; break;
|
2005-07-08 22:25:24 +00:00
|
|
|
case 2: w->left = _screen.width - w->width; break;
|
|
|
|
default: w->left = 0;
|
2004-08-16 21:02:06 +00:00
|
|
|
}
|
|
|
|
SetDirtyBlocks(0, 0, _screen.width, w->height); // invalidate the whole top part
|
|
|
|
return w->left;
|
|
|
|
}
|
|
|
|
|
2008-04-07 20:28:58 +00:00
|
|
|
/**
|
|
|
|
* Relocate all windows to fit the new size of the game application screen
|
|
|
|
* @param neww New width of the game application screen
|
|
|
|
* @param newh New height of the game appliction screen
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
void RelocateAllWindows(int neww, int newh)
|
|
|
|
{
|
2006-11-18 16:47:02 +00:00
|
|
|
Window* const *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
Window *w = *wz;
|
2004-08-09 17:04:08 +00:00
|
|
|
int left, top;
|
2004-09-05 14:20:36 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->window_class == WC_MAIN_WINDOW) {
|
|
|
|
ViewPort *vp = w->viewport;
|
|
|
|
vp->width = w->width = neww;
|
|
|
|
vp->height = w->height = newh;
|
2007-05-15 16:08:46 +00:00
|
|
|
vp->virtual_width = ScaleByZoom(neww, vp->zoom);
|
|
|
|
vp->virtual_height = ScaleByZoom(newh, vp->zoom);
|
2004-08-09 17:04:08 +00:00
|
|
|
continue; // don't modify top,left
|
2004-09-03 19:59:05 +00:00
|
|
|
}
|
|
|
|
|
2006-11-18 17:07:05 +00:00
|
|
|
/* XXX - this probably needs something more sane. For example specying
|
|
|
|
* in a 'backup'-desc that the window should always be centred. */
|
2006-02-06 09:18:04 +00:00
|
|
|
switch (w->window_class) {
|
|
|
|
case WC_MAIN_TOOLBAR:
|
2007-07-29 19:18:22 +00:00
|
|
|
if (neww - w->width != 0) {
|
|
|
|
ResizeWindow(w, min(neww, 640) - w->width, 0);
|
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
Point size;
|
|
|
|
Point delta;
|
|
|
|
size.x = w->width;
|
|
|
|
size.y = w->height;
|
|
|
|
delta.x = neww - w->width;
|
|
|
|
delta.y = 0;
|
|
|
|
w->OnResize(size, delta);
|
2007-07-29 19:18:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-06 09:18:04 +00:00
|
|
|
top = w->top;
|
|
|
|
left = PositionMainToolbar(w); // changes toolbar orientation
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WC_SELECT_GAME:
|
|
|
|
case WC_GAME_OPTIONS:
|
|
|
|
case WC_NETWORK_WINDOW:
|
|
|
|
top = (newh - w->height) >> 1;
|
|
|
|
left = (neww - w->width) >> 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WC_NEWS_WINDOW:
|
|
|
|
top = newh - w->height;
|
|
|
|
left = (neww - w->width) >> 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WC_STATUS_BAR:
|
2007-11-19 18:38:10 +00:00
|
|
|
ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
|
2006-02-06 09:18:04 +00:00
|
|
|
top = newh - w->height;
|
|
|
|
left = (neww - w->width) >> 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WC_SEND_NETWORK_MSG:
|
2007-11-19 18:38:10 +00:00
|
|
|
ResizeWindow(w, Clamp(neww, 320, 640) - w->width, 0);
|
2006-02-06 09:18:04 +00:00
|
|
|
top = (newh - 26); // 26 = height of status bar + height of chat bar
|
|
|
|
left = (neww - w->width) >> 1;
|
|
|
|
break;
|
|
|
|
|
2006-11-18 17:07:05 +00:00
|
|
|
case WC_CONSOLE:
|
2006-11-21 22:10:52 +00:00
|
|
|
IConsoleResize(w);
|
2006-11-19 22:24:18 +00:00
|
|
|
continue;
|
2006-11-18 17:07:05 +00:00
|
|
|
|
2008-03-15 20:32:42 +00:00
|
|
|
default: {
|
2006-02-06 09:18:04 +00:00
|
|
|
left = w->left;
|
|
|
|
if (left + (w->width >> 1) >= neww) left = neww - w->width;
|
2007-12-06 20:55:48 +00:00
|
|
|
if (left < 0) left = 0;
|
|
|
|
|
2006-02-06 09:18:04 +00:00
|
|
|
top = w->top;
|
|
|
|
if (top + (w->height >> 1) >= newh) top = newh - w->height;
|
2008-03-15 20:32:42 +00:00
|
|
|
|
|
|
|
const Window *wt = FindWindowById(WC_MAIN_TOOLBAR, 0);
|
|
|
|
if (wt != NULL) {
|
2008-04-14 23:26:31 +00:00
|
|
|
if (top < wt->height && wt->left < (w->left + w->width) && (wt->left + wt->width) > w->left) top = wt->height;
|
2008-03-15 20:32:42 +00:00
|
|
|
if (top >= newh) top = newh - 1;
|
|
|
|
} else {
|
|
|
|
if (top < 0) top = 0;
|
|
|
|
}
|
|
|
|
} break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w->viewport != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
w->viewport->left += left - w->left;
|
|
|
|
w->viewport->top += top - w->top;
|
|
|
|
}
|
|
|
|
|
|
|
|
w->left = left;
|
|
|
|
w->top = top;
|
|
|
|
}
|
|
|
|
}
|