Window management: Difference between revisions
No edit summary |
(Added Oz.) |
||
Line 28:
WinClose, % window
return</lang>
=={{header|Oz}}==
We use QTk, Oz' default GUI toolkit.
QTk takes a declarative description of the GUI and from this creates objects which represent the GUI parts. So windows are represented by objects and thus have an identity.
We create two windows with a simple GUI. The user can use each window to send messages to the window or its neighboring window. (Sending messages is the same as 'calling methods' in Oz.)
We also wrap the Window objects in a procedure in order to extend their functionality. This is interesting
because it shows how to extend an object's interface even when we don't have control over object creation.
<lang oz>declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
%% The messages that can be sent to the windows.
WindowActions =
[hide show close
iconify deiconify
maximize restore
set(minsize:minsize(width:400 height:400))
set(minsize:minsize(width:200 height:200))
set(geometry:geometry(x:0 y:0))
set(geometry:geometry(x:500 y:500))
]
%% Two windows, still uninitialized.
Windows = windows(window1:_
window2:_)
fun {CreateWindow}
Message = {NewCell WindowActions.1}
ReceiverName = {NewCell {Arity Windows}.1}
fun {ButtonText}
"Send"#" "#{ValueToString @Message}#" to "#@ReceiverName
end
Button
Desc =
td(title:"Window Management"
lr(listbox(init:{Arity Windows}
glue:nswe
tdscrollbar:true
actionh:proc {$ W}
ReceiverName := {GetSelected W}
{Button set(text:{ButtonText})}
end
)
listbox(init:{Map WindowActions ValueToString}
glue:nswe
tdscrollbar:true
actionh:proc {$ A}
Message := {GetSelected A}
{Button set(text:{ButtonText})}
end
)
glue:nswe
)
button(text:{ButtonText}
glue:we
handle:Button
action:proc {$}
{Windows.@ReceiverName @Message}
end
)
)
Window = {Extend {QTk.build Desc}}
in
{Window show}
Window
end
%% Adds two methods to a toplevel instance.
%% For maximize and restore we have to interact directly with Tk
%% because that functionality is not part of the QTk library.
fun {Extend Toplevel}
proc {$ A}
case A of maximize then
{Tk.send wm(state Toplevel zoomed)}
[] restore then
{Tk.send wm(state Toplevel normal)}
else
{Toplevel A}
end
end
end
%% Returns the current entry of a listbox
%% as an Oz value.
fun {GetSelected LB}
Entries = {LB get($)}
Index = {LB get(firstselection:$)}
in
{Compiler.virtualStringToValue {Nth Entries Index}}
end
fun {ValueToString V}
{Value.toVirtualString V 100 100}
end
in
{Record.forAll Windows CreateWindow}</lang>
=={{header|Tcl}}==
|
Revision as of 16:17, 8 January 2010
You are encouraged to solve this task according to the task description, using any language you may know.
Treat windows or at least window identities as first class objects.
- Store window identities in variables, compare them for equality.
- Provide examples of performing some of the following:
- hide, show, close, minimize, maximize, move, and resize a window.
The window of interest may or may not have been created by your program.
AutoHotkey
<lang AutoHotkey>F1:: ;; when user hits the F1 key, do the following WinGetTitle, window, A ; get identity of active window into a variable WinMove, %window%, , 100, 100, 800, 800 ; move window to coordinates, 100, 100
; and change size to 800 x 800 pixels
sleep, 2000 WinHide, % window ; hide window TrayTip, hidden, window is hidden, 2 sleep, 2000 WinShow, % window ; show window again loop, {
inputbox, name, what was the name of your window? if (name = window) ; compare window variables for equality { msgbox you got it break }
- else try again
} WinClose, % window return</lang>
Oz
We use QTk, Oz' default GUI toolkit. QTk takes a declarative description of the GUI and from this creates objects which represent the GUI parts. So windows are represented by objects and thus have an identity.
We create two windows with a simple GUI. The user can use each window to send messages to the window or its neighboring window. (Sending messages is the same as 'calling methods' in Oz.)
We also wrap the Window objects in a procedure in order to extend their functionality. This is interesting because it shows how to extend an object's interface even when we don't have control over object creation.
<lang oz>declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
%% The messages that can be sent to the windows. WindowActions = [hide show close iconify deiconify maximize restore set(minsize:minsize(width:400 height:400)) set(minsize:minsize(width:200 height:200)) set(geometry:geometry(x:0 y:0)) set(geometry:geometry(x:500 y:500)) ]
%% Two windows, still uninitialized. Windows = windows(window1:_ window2:_)
fun {CreateWindow} Message = {NewCell WindowActions.1} ReceiverName = {NewCell {Arity Windows}.1} fun {ButtonText} "Send"#" "#{ValueToString @Message}#" to "#@ReceiverName end Button Desc = td(title:"Window Management" lr(listbox(init:{Arity Windows} glue:nswe tdscrollbar:true actionh:proc {$ W} ReceiverName := {GetSelected W} {Button set(text:{ButtonText})} end ) listbox(init:{Map WindowActions ValueToString} glue:nswe tdscrollbar:true actionh:proc {$ A} Message := {GetSelected A} {Button set(text:{ButtonText})} end ) glue:nswe ) button(text:{ButtonText} glue:we handle:Button action:proc {$} {Windows.@ReceiverName @Message} end ) ) Window = {Extend {QTk.build Desc}} in {Window show} Window end
%% Adds two methods to a toplevel instance. %% For maximize and restore we have to interact directly with Tk %% because that functionality is not part of the QTk library. fun {Extend Toplevel} proc {$ A} case A of maximize then {Tk.send wm(state Toplevel zoomed)} [] restore then {Tk.send wm(state Toplevel normal)} else {Toplevel A} end end end
%% Returns the current entry of a listbox %% as an Oz value. fun {GetSelected LB} Entries = {LB get($)} Index = {LB get(firstselection:$)} in {Compiler.virtualStringToValue {Nth Entries Index}} end
fun {ValueToString V} {Value.toVirtualString V 100 100} end
in
{Record.forAll Windows CreateWindow}</lang>
Tcl
<lang tcl>package require Tk
- How to open a window
proc openWin {} {
global win if {[info exists win] && [winfo exists $win]} { # Already existing; just reset wm deiconify $win wm state $win normal return } catch {destroy $win} ;# Squelch the old one set win [toplevel .t] pack [label $win.label -text "This is the window being manipulated"] \ -fill both -expand 1
}
- How to close a window
proc closeWin {} {
global win if {[info exists win] && [winfo exists $win]} { destroy $win }
}
- How to minimize a window
proc minimizeWin {} {
global win if {[info exists win] && [winfo exists $win]} { wm state $win iconic }
}
- How to maximize a window
proc maximizeWin {} {
global win if {[info exists win] && [winfo exists $win]} { wm state $win zoomed catch {wm attribute $win -zoomed 1} ;# Hack for X11 }
}
- How to move a window
proc moveWin {} {
global win if {[info exists win] && [winfo exists $win]} { scan [wm geometry $win] "%dx%d+%d+%d" width height x y wm geometry $win +[incr x 10]+[incr y 10] }
}
- How to resize a window
proc resizeWin {} {
global win if {[info exists win] && [winfo exists $win]} { scan [wm geometry $win] "%dx%d+%d+%d" width height x y wm geometry $win [incr width 10]x[incr height 10] }
}
grid [label .l -text "Window handle:"] [label .l2 -textvariable win] grid [button .b1 -text "Open/Reset" -command openWin] - grid [button .b2 -text "Close" -command closeWin] - grid [button .b3 -text "Minimize" -command minimizeWin] - grid [button .b4 -text "Maximize" -command maximizeWin] - grid [button .b5 -text "Move" -command moveWin] - grid [button .b6 -text "Resize" -command resizeWin] -</lang>