REM >BASIC:Window
REM
REM Copyright 2001-2014, Stephen Fryatt (info@stevefryatt.org.uk)
REM
REM This file is part of WimpLib:
REM
REM   http://www.stevefryatt.org.uk/software/
REM
REM Licensed under the EUPL, Version 1.2 only (the "Licence");
REM You may not use this work except in compliance with the
REM Licence.
REM
REM You may obtain a copy of the Licence at:
REM
REM   http://joinup.ec.europa.eu/software/page/eupl
REM
REM Unless required by applicable law or agreed to in
REM writing, software distributed under the Licence is
REM distributed on an "AS IS" basis, WITHOUT WARRANTIES
REM OR CONDITIONS OF ANY KIND, either express or implied.
REM
REM See the Licence for the specific language governing
REM permissions and limitations under the Licence.
:
REM Window handling library
REM
REM The variable a% is reqired to point to 256 bytes of memory.

REM Test if a window is open.
REM
REM \param window%		The handle of the window to test.
REM \return			TRUE if the window is open; FALSE if not.
:
DEF FNwindow_is_open(!a%)
SYS "Wimp_GetWindowState",,a%
=(a%!32 AND &10000) = &10000


REM Open a window at the top of the window stack.
REM
REM \param window%		The handle of the window to open.
:
DEF PROCwindow_open(!a%)
SYS "Wimp_GetWindowState",,a%
a%!28 = -1
SYS "Wimp_OpenWindow",,a%
ENDPROC


REM Open a window at the top of the window stack, at given X and Y coordinates.
REM
REM \param window%		The handle of the window to open.
REM \param x%			The X coordinate of the top-left of the window.
REM \param y%			The Y coordinate of the top-left of the window.
:
DEF PROCwindow_open_at(!a%, x%, y%)
SYS "Wimp_GetWindowState",,a%
a%!8 = y% - (a%!16 - a%!8)
a%!12 = x% + (a%!12 - a%!4)
a%!16 = y%
a%!4 = x%
SYS "Wimp_OpenWindow",,a%
ENDPROC


REM Open a window at the top of the window stack, centred at the given X
REM and Y coordinates.
REM
REM \param window%		The handle of the window to open.
REM \param x%			The X coordinate of the centre of the window.
REM \param y%			The Y coordinate of the centre of the window.
:
DEF PROCwindow_open_centred_at(!a%, x%, y%)
LOCAL width%, height%
SYS "Wimp_GetWindowState",,a%
width% = (a%!12 - a%!4)
height% = (a%!16 - a%!8)
a%!4 = x% - width% / 2
a%!8 = y% - height% / 2
IF a%!8 < 96 THEN a%!8 = 96
a%!12 = a%!4 + width%
a%!16 = a%!8 + height%
SYS "Wimp_OpenWindow",,a%
ENDPROC


REM Open a window transiently at the top of the window stack, at given X and
REM Y coordinates.
REM
REM \param window%		The handle of the window to open.
REM \param x%			The X coordinate of the top-left of the window.
REM \param y%			The Y coordinate of the top-left of the window.
:
DEF PROCwindow_open_as_menu_at(window%, x%, y%)
SYS "Wimp_CreateMenu",,window%, x% - 64, y%
ENDPROC


REM Close a window which is currently open.
REM
REM \param window%		The handle of the window to close.
:
DEF PROCwindow_close(!a%)
SYS "Wimp_CloseWindow",,a%
ENDPROC


REM Open a window nested inside an already open parent window.
REM
REM \param window%		The handle of the window to open.
REM \param parent%		The handle of the parent window.
:
DEF PROCwindow_open_nested(!a%, parent%)
SYS "Wimp_GetWindowState",,a%
a%!28 = -1
SYS "Wimp_OpenWindow",,a%, &4B534154, parent%, &0
ENDPROC


REM Move a window into the space occupied by an icon in an already open
REM parent window.
REM
REM \param window%		The handle of the window to open.
REM \param parent%		The handle of the parent window.
REM \param icon%		The icon in the parent window to locate the
REM				child window inside.
REM \param margin%		The margin in OS units to leave on the right-
REM				hand side of the child window (for a scrollbar).
REM \param behind%		The handle of a window to open behind.
:
DEF PROCwindow_open_nested_in_icon(!a%, parent%, icon%, margin%, behind%)
a%!100 = parent%
SYS "Wimp_GetWindowState",, a% + 100
a%!200 = parent%
a%!204 = icon%
SYS "Wimp_GetIconState",, a% + 200
SYS "Wimp_GetWindowState",, a%
a%!4 = a%!104 - a%!120 + a%!208 + 8
a%!8 = a%!116 - a%!124 + a%!212 + 8
a%!12 = a%!104 - a%!120 + a%!216 - 8 - margin%
a%!16 = a%!116 - a%!124 + a%!220 - 8
a%!28 = behind%
SYS "Wimp_OpenWindow",,a%, &4B534154, parent%, &0
ENDPROC


REM Force the redraw of the visible area of an open window, plus the window's
REM title bar
REM
REM \param window%		The handle of the window to redraw.
:
DEF PROCwindow_force_redraw(!a%)
SYS "Wimp_GetWindowState",,a%
SYS "Wimp_ForceRedraw",!a%,a%!20,(a%!24)-(a%!16-a%!8),(a%!20)+(a%!12-a%!4),a%!24
SYS "Wimp_ForceRedraw",-1,a%!4,a%!16,a%!12+40,a%!16+40
ENDPROC

