Window management: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(25 intermediate revisions by 12 users not shown)
Line 12:
{{omit from|PostScript}}
{{omit from|Retro}}
{{omit from|SQL PL|It does not handle GUI}}
{{omit from|TI-83 BASIC|Does not have windows.}}
{{omit from|TI-89 BASIC|Does not have windows.}}
Line 17 ⟶ 18:
Treat windows or at least window identities as [[wp:First-class_object|first class objects]].
 
* Store window identities in variables, compare them for equality.
* Provide examples of performing some of the following:
** hide,
**hide, show, close, minimize, maximize, move, and resize a window.
** show,
** close,
** minimize,
** maximize,
** move,     and
** resize a window.
 
 
The window of interest may or may not have been created by your program.
<br><br>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="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
Line 43 ⟶ 53:
}
WinClose, % window
return</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> SWP_NOMOVE = 2
SWP_NOZORDER = 4
SW_MAXIMIZE = 3
Line 84 ⟶ 94:
PRINT "Closing the window in two seconds..."
WAIT 200
QUIT</langsyntaxhighlight>
 
=={{header|C}}==
C does not have any standard windowing library, although cross platform libraries are available, and although it's technically possible to create windows from scratch given the '''''Dark powers''''' that C commands, I chose the simplest option, the Windows API. On running this program, the user is taken over all the sub tasks listed in this task one by one.
===Windows===
<syntaxhighlight lang="c">
#include<windows.h>
#include<unistd.h>
#include<stdio.h>
 
const char g_szClassName[] = "weirdWindow";
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd[3];
MSG Msg;
int i,x=0,y=0;
char str[3][100];
int maxX = GetSystemMetrics(SM_CXSCREEN), maxY = GetSystemMetrics(SM_CYSCREEN);
char messages[15][180] = {"Welcome to the Rosettacode Window C implementation.",
"If you can see two blank windows just behind this message box, you are in luck.",
"Let's get started....",
"Yes, you will be seeing a lot of me :)",
"Now to get started with the tasks, the windows here are stored in an array of type HWND, the array is called hwnd (yes, I know it's very innovative.)",
"Let's compare the windows for equality.",
"Now let's hide Window 1.",
"Now let's see Window 1 again.",
"Let's close Window 2, bye, bye, Number 2 !",
"Let's minimize Window 1.",
"Now let's maximize Window 1.",
"And finally we come to the fun part, watch Window 1 move !",
"Let's double Window 1 in size for all the good work.",
"That's all folks ! (You still have to close that window, that was not part of the contract, sue me :D )"};
 
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
return 0;
}
 
for(i=0;i<2;i++){
sprintf(str[i],"Window Number %d",i+1);
hwnd[i] = CreateWindow(g_szClassName,str[i],WS_OVERLAPPEDWINDOW,i*maxX/2 , 0, maxX/2-10, maxY/2-10,NULL, NULL, hInstance, NULL);
if(hwnd[i] == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
return 0;
}
 
ShowWindow(hwnd[i], nCmdShow);
UpdateWindow(hwnd[i]);
}
for(i=0;i<6;i++){
MessageBox(NULL, messages[i], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
}
if(hwnd[0]==hwnd[1])
MessageBox(NULL, "Window 1 and 2 are equal.", "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
else
MessageBox(NULL, "Nope, they are not.", "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
MessageBox(NULL, messages[6], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
ShowWindow(hwnd[0], SW_HIDE);
MessageBox(NULL, messages[7], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
ShowWindow(hwnd[0], SW_SHOW);
MessageBox(NULL, messages[8], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
ShowWindow(hwnd[1], SW_HIDE);
MessageBox(NULL, messages[9], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
ShowWindow(hwnd[0], SW_MINIMIZE);
MessageBox(NULL, messages[10], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
ShowWindow(hwnd[0], SW_MAXIMIZE);
MessageBox(NULL, messages[11], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
ShowWindow(hwnd[0], SW_RESTORE);
while(x!=maxX/2||y!=maxY/2){
if(x<maxX/2)
x++;
if(y<maxY/2)
y++;
MoveWindow(hwnd[0],x,y,maxX/2-10, maxY/2-10,0);
sleep(10);
}
MessageBox(NULL, messages[12], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
MoveWindow(hwnd[0],0,0,maxX, maxY,0);
MessageBox(NULL, messages[13], "Info",MB_APPLMODAL| MB_ICONINFORMATION | MB_OK);
 
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
</syntaxhighlight>
 
=={{header|G
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">sWindow As New String[4]
'________________________
Public Sub Form_Open()
Line 174 ⟶ 327:
End If
 
End</langsyntaxhighlight>
Output:
<pre>
Line 195 ⟶ 348:
Windows identities DONOT match the original window size
</pre>
 
=={{header|Go}}==
{{libheader|gotk3}}
{{trans|Nim}}
<syntaxhighlight lang="go">package main
 
import (
"github.com/gotk3/gotk3/gtk"
"log"
"time"
)
 
func check(err error, msg string) {
if err != nil {
log.Fatal(msg, err)
}
}
 
func main() {
gtk.Init(nil)
 
window, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
check(err, "Unable to create window:")
window.SetResizable(true)
window.SetTitle("Window management")
window.SetBorderWidth(5)
window.Connect("destroy", func() {
gtk.MainQuit()
})
 
stackbox, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 10)
check(err, "Unable to create stack box:")
 
bmax, err := gtk.ButtonNewWithLabel("Maximize")
check(err, "Unable to create maximize button:")
bmax.Connect("clicked", func() {
window.Maximize()
})
 
bunmax, err := gtk.ButtonNewWithLabel("Unmaximize")
check(err, "Unable to create unmaximize button:")
bunmax.Connect("clicked", func() {
window.Unmaximize()
})
 
bicon, err := gtk.ButtonNewWithLabel("Iconize")
check(err, "Unable to create iconize button:")
bicon.Connect("clicked", func() {
window.Iconify()
})
 
bdeicon, err := gtk.ButtonNewWithLabel("Deiconize")
check(err, "Unable to create deiconize button:")
bdeicon.Connect("clicked", func() {
window.Deiconify()
})
 
bhide, err := gtk.ButtonNewWithLabel("Hide")
check(err, "Unable to create hide button:")
bhide.Connect("clicked", func() {
// not working on Ubuntu 16.04 but window 'dims' after a few seconds
window.Hide()
time.Sleep(10 * time.Second)
window.Show()
})
 
bshow, err := gtk.ButtonNewWithLabel("Show")
check(err, "Unable to create show button:")
bshow.Connect("clicked", func() {
window.Show()
})
 
bmove, err := gtk.ButtonNewWithLabel("Move")
check(err, "Unable to create move button:")
isShifted := false
bmove.Connect("clicked", func() {
w, h := window.GetSize()
if isShifted {
window.Move(w-10, h-10)
} else {
window.Move(w+10, h+10)
}
isShifted = !isShifted
})
 
bquit, err := gtk.ButtonNewWithLabel("Quit")
check(err, "Unable to create quit button:")
bquit.Connect("clicked", func() {
window.Destroy()
})
 
stackbox.PackStart(bmax, true, true, 0)
stackbox.PackStart(bunmax, true, true, 0)
stackbox.PackStart(bicon, true, true, 0)
stackbox.PackStart(bdeicon, true, true, 0)
stackbox.PackStart(bhide, true, true, 0)
stackbox.PackStart(bshow, true, true, 0)
stackbox.PackStart(bmove, true, true, 0)
stackbox.PackStart(bquit, true, true, 0)
 
window.Add(stackbox)
window.ShowAll()
gtk.Main()
}</syntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">CHARACTER title="Rosetta Window_management"
REAL :: w=-333, h=25, x=1, y=0.5 ! pixels < 0, relative window size 0...1, script character size > 1
 
Line 206 ⟶ 463:
WINDOW(WIN=wh, MAXimize) ! maximize (hides the script window)
WINDOW(Kill=wh) ! close
END</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
The demo program opens three windows, one with a running commentary on the action.
<langsyntaxhighlight Iconlang="icon">link graphics
 
procedure main()
Line 217 ⟶ 474:
 
W1 := open("Window 1","g","resize=on","size=400,400","pos=100,100","bg=black","fg=red") |
stop("UnbaleUnable to open window 1")
W2 := open("Window 2","g","resize=on","size=400,400","pos=450,450","bg=blue","fg=yellow") |
stop("UnbaleUnable to open window 2")
W3 := open("Window 3","g","resize=on","size=400,400","pos=600,150","bg=orange","fg=black") |
stop("UnbaleUnable to open window 3")
WWrite(W3,"Opened three windows")
Line 248 ⟶ 505:
delay(Delay)
WWrite(W3,"Window 2 movededmoved")
WAttrib(W2,"posx=700","posy=300")
Line 262 ⟶ 519:
WWrite(W3,"Enter Q or q here to quit")
WDone(W3)
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 269 ⟶ 526:
=={{header|Java}}==
Java cannot (easily) manipulate windows created by other programs. This code manipulates windows that it has created, but any window created in the same JVM can be controlled similarly. This example uses Swing - for AWT or SWT go figure.
<langsyntaxhighlight Javalang="java">import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
Line 287 ⟶ 544:
// Create UI on correct thread
public static void main( final String[] args ) {
EventQueue.invokeLater( () -> new WindowController() );
new Runnable() {
public void run() {
new WindowController();
}
}
);
}
 
private JComboBox<ControlledWindow> list;
 
// Button class to call the right method
Line 329 ⟶ 580:
getRootPane().setBorder( new EmptyBorder( 3, 3, 3, 3 ) );
add( new JLabel( "Add windows and control them." ), BorderLayout.NORTH );
main.add( list = new JComboBox<>() );
add( main, BorderLayout.CENTER );
controls.setLayout( new GridLayout( 0, 1, 3, 3 ) );
Line 374 ⟶ 625:
 
public void doHide() {
( (final JFrame )window list.getSelectedItem()= ).setVisiblegetWindow( false );
if ( null == window ) {
return;
}
window.setVisible( false );
}
 
public void doShow() {
( (final JFrame )window list.getSelectedItem()= ).setVisiblegetWindow( true );
if ( null == window ) {
return;
}
window.setVisible( true );
}
 
public void doClose() {
( (final JFrame )window list.getSelectedItem()= ).disposegetWindow();
if ( null == window ) {
return;
}
window.dispose();
}
 
public void doMinimise() {
( (final JFrame )window list.getSelectedItem()= ).setStategetWindow( Frame.ICONIFIED );
if ( null == window ) {
return;
}
window.setState( Frame.ICONIFIED );
}
 
public void doMaximise() {
( (final JFrame )window list.getSelectedItem()= ).setExtendedStategetWindow( Frame.MAXIMIZED_BOTH );
if ( null == window ) {
return;
}
window.setExtendedState( Frame.MAXIMIZED_BOTH );
}
 
public void doMove() {
( (final JFrame )window list.getSelectedItem= getWindow() ).setLocation;
if ( null == window ) {
return;
Integer.parseInt( JOptionPane.showInputDialog( "Horizontal position?" ) ),
}
Integer.parseInt( JOptionPane.showInputDialog( "Vertical position?" ) )
final int hPos = getInt( "Horizontal position?" );
);
if ( -1 == hPos ) {
return;
}
final int vPos = getInt( "Vertical position?" );
if ( -1 == vPos ) {
return;
}
window.setLocation ( hPos, vPos );
}
 
public void doResize() {
final JFrame window = getWindow();
if ( null == window ) {
return;
}
final int width = getInt( "Width?" );
if ( -1 == width ) {
return;
}
final int height = getInt( "Height?" );
if ( -1 == height ) {
return;
}
window.setBounds ( window.getX(), window.getY(), width, height );
}
 
private JFrame getWindow() {
final JFrame window = ( JFrame ) list.getSelectedItem();
if ( null == window ) {
JOptionPane.showMessageDialog( this, "Add a window first" );
}
return window;
}
 
private int getInt(final String prompt) {
window.setBounds
final String s = JOptionPane.showInputDialog( prompt );
(
if ( null == s window.getX(), {
window.getY(),return -1;
}
Integer.parseInt( JOptionPane.showInputDialog( "Width?" ) ),
try {
Integer.parseInt( JOptionPane.showInputDialog( "Height?" ) )
return Integer.parseInt( s );
} catch ( final NumberFormatException x ) {
JOptionPane.showMessageDialog( this, "Not a number" );
return -1;
}
}
}
</syntaxhighlight>
</lang>
 
=={{header|MathematicaJulia}}==
Uses the Gtk windowing package, so the package can manage Gtk's windows.
Mathematica can only easily access and control windows created by itself.
<syntaxhighlight lang="julia">using Gtk
 
function controlwindow(win, lab)
<lang Mathematica>nb=NotebookCreate[]; (*Create a window and store in a variable*)
sleep(4)
set_gtk_property!(lab, :label, "Hiding...")
sleep(1)
println("Hiding widow")
set_gtk_property!(win, :visible, false)
sleep(5)
set_gtk_property!(lab, :label, "Showing...")
println("Showing window")
set_gtk_property!(win, :visible, true)
sleep(5)
set_gtk_property!(lab, :label, "Resizing...")
println("Resizing window")
resize!(win, 300, 300)
sleep(4)
set_gtk_property!(lab, :label, "Maximizing...")
println("Maximizing window")
sleep(1)
maximize(win)
set_gtk_property!(lab, :label, "Closing...")
sleep(5)
println("Closing window")
destroy(win)
sleep(2)
exit(0)
end
 
function runwindow()
win = GtkWindow("Window Control Test", 500, 30) |> (GtkFrame() |> (vbox = GtkBox(:v)))
lab = GtkLabel("Window under external control")
push!(vbox, lab)
@async(controlwindow(win, lab))
 
cond = Condition()
endit(w) = notify(cond)
signal_connect(endit, win, :destroy)
showall(win)
wait(cond)
end
 
runwindow()
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica can only easily access and control windows created by itself.
<syntaxhighlight lang="mathematica">nb=NotebookCreate[]; (*Create a window and store in a variable*)
nb===nb2 (*test for equality with another window object*)
SetOptions[nb,Visible->False](*Hide*)
Line 424 ⟶ 774:
NotebookClose[nb] (*Close*)
SetOptions[nb,WindowMargins->{{x,Automatic},{y,Automatic}}](*Move to x,y screen position*)
SetOptions[nb,WindowSize->{100,100}](*Resize*)</langsyntaxhighlight>
 
=={{header|Nim}}==
==={{libheader|gintro}}===
<syntaxhighlight lang="nim">import os
import gintro/[glib, gobject, gtk, gio]
from gintro/gdk import processAllUpdates
 
type MyWindow = ref object of ApplicationWindow
isShifted: bool
 
#---------------------------------------------------------------------------------------------------
 
proc wMaximize(button: Button; window: MyWindow) =
window.maximize()
 
proc wUnmaximize(button: Button; window: MyWindow) =
window.unmaximize()
 
proc wIconify(button: Button; window: MyWindow) =
window.iconify()
 
proc wDeiconify(button: Button; window: MyWindow) =
window.deiconify()
 
proc wHide(button: Button; window: MyWindow) =
window.hide()
processAllUpdates()
os.sleep(2000)
window.show()
 
proc wShow(button: Button; window: MyWindow) =
window.show()
 
proc wMove(button: Button; window: MyWindow) =
var x, y: int
window.getPosition(x, y)
if window.isShifted:
window.move(x - 10, y - 10)
else:
window.move(x + 10, y + 10)
window.isShifted = not window.isShifted
 
proc wQuit(button: Button; window: MyWindow) =
window.destroy()
 
#---------------------------------------------------------------------------------------------------
 
proc activate(app: Application) =
## Activate the application.
 
let window = newApplicationWindow(MyWindow, app)
window.setTitle("Window management")
 
let stackBox = newBox(Orientation.vertical, 10)
stackBox.setHomogeneous(true)
 
let
bMax = newButton("maximize")
bUnmax = newButton("unmaximize")
bIcon = newButton("iconize")
bDeicon = newButton("deiconize")
bHide = newButton("hide")
bShow = newButton("show")
bMove = newButton("move")
bQuit = newButton("Quit")
 
for button in [bMax, bUnmax, bIcon, bDeicon, bHide, bShow, bMove, bQuit]:
stackBox.add button
 
window.setBorderWidth(5)
window.add(stackBox)
 
discard bMax.connect("clicked", wMaximize, window)
discard bUnmax.connect("clicked", wUnmaximize, window)
discard bIcon.connect("clicked", wIconify, window)
discard bDeicon.connect("clicked", wDeiconify, window)
discard bHide.connect("clicked", wHide, window)
discard bShow.connect("clicked", wShow, window)
discard bMove.connect("clicked", wMove, window)
discard bQuit.connect("clicked", wQuit, window)
 
window.showAll()
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
let app = newApplication(Application, "Rosetta.Window.Management")
discard app.connect("activate", activate)
discard app.run()</syntaxhighlight>
 
 
==={{libheader|Gtk2}}===
<langsyntaxhighlight lang="nim">import os
import gdk2, glib2, gtk2,
os
 
proc thisDestroy(widget: pWidget,PWidget; data: pgpointerPgpointer) {.cdecl.} =
main_quit()
proc thisMax(widget: pWidget, data: pgpointer){.cdecl.} =
maximize(get_parent_window(widget))
proc thisUnmax(widget: pWidget, data: pgpointer){.cdecl.} =
unmaximize(get_parent_window(widget))
proc thisIcon(widget: pWidget, data: pgpointer){.cdecl.} =
iconify(get_parent_window(widget))
proc thisDeicon(widget: pWidget, data: pgpointer){.cdecl.} =
deiconify(get_parent_window(widget))
proc thisHide(widget: pWidget, data: pgpointer){.cdecl.} =
hide(get_parent_window(widget))
sleep(5)
show(get_parent_window(widget))
proc thisShow(widget: pWidget, data: pgpointer){.cdecl.} =
show(get_parent_window(widget))
 
proc thisMax(widget: PWidget; data: Pgpointer) {.cdecl.} =
var isshifted: bool = false
widget.get_parent_window().maximize()
 
proc thisMove(widget: pWidget, data: pgpointer){.cdecl.} =
proc thisUnmax(widget: PWidget; data: Pgpointer) {.cdecl.} =
var w, h: gint
get_size(widget.get_parent_window(widget), Addr.unmaximize(w), Addr(h))
 
proc thisIcon(widget: PWidget; data: Pgpointer) {.cdecl.} =
widget.get_parent_window().iconify()
 
proc thisDeicon(widget: PWidget; data: Pgpointer) {.cdecl.} =
widget.get_parent_window().deiconify()
 
proc thisHide(widget: PWidget; data: Pgpointer) {.cdecl.} =
widget.get_parent_window().hide()
window_process_all_updates()
sleep(2000)
widget.get_parent_window().show()
 
proc thisShow(widget: PWidget; data: Pgpointer) {.cdecl.} =
widget.get_parent_window().show()
 
var isShifted = false
 
proc thisMove(widget: PWidget; data: Pgpointer) {.cdecl.} =
var x, y: gint
widget.get_parent_window().get_position(addr(x), addr(y))
if isshifted:
move(widget.get_parent_window(widget),.move(x w- 10, hy - 10)
else:
move(widget.get_parent_window(widget),.move(x w+ 10, hy + 10)
isshiftedisShifted = not isshiftedisShifted
 
 
nim_init()
varlet window = window_new(gtk2.WINDOW_TOPLEVEL)
discard window.allow_grow(window)
window.set_title(window,"Window management")
let
var stackbox = vbox_new(TRUE, 10)
stackbox = vbox_new(true, 10)
var bmax = button_new("maximize")
var bunmax bMax = button_new("unmaximizemaximize")
var bicon bUnmax = button_new("iconizeunmaximize")
var bdeicon bIcon = button_new("deiconizeiconize")
var bhide bDeicon = button_new("hidedeiconize")
var bshow bHide = button_new("showhide")
var bmove bShow = button_new("moveshow")
var bquit bMove = button_new("Quitmove")
bQuit = button_new("Quit")
 
pack_start(stackbox, bmax, TRUE, TRUE, 0)
stackbox.pack_start(stackbox, bunmaxbMax, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(stackbox, biconbUnmax, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(stackbox, bdeiconbIcon, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(stackbox, bhidebDeicon, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(stackbox, bshowbHide, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(stackbox, bmovebShow, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(stackbox, bquitbMove, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(bQuit, true, true, 0)
set_border_width(Window, 5)
add(window, stackbox.set_border_width(5)
window.add(stackbox)
discard signal_connect(window, "destroy",
 
SIGNAL_FUNC(thisDestroy), nil)
discard window.signal_connect("destroy", SIGNAL_FUNC(thisDestroy), nil)
discard bIcon.signal_connect("clicked", SIGNAL_FUNC(thisIcon), nil)
discard bDeicon.signal_connect("clicked", SIGNAL_FUNC(thisDeicon), nil)
discard bMax.signal_connect("clicked", SIGNAL_FUNC(thisMax), nil)
discard bUnmax.signal_connect("clicked", SIGNAL_FUNC(thisUnmax), nil)
discard bHide.signal_connect("clicked", SIGNAL_FUNC(thisHide), nil)
discard bShow.signal_connect("clicked", SIGNAL_FUNC(thisShow), nil)
discard bMove.signal_connect("clicked", SIGNAL_FUNC(thismove), nil)
discard bQuit.signal_connect("clicked", SIGNAL_FUNC(thisDestroy), nil)
window.show_all()
main()</syntaxhighlight>
 
discard signal_connect(bicon, "clicked",
SIGNAL_FUNC(thisIcon), nil)
discard signal_connect(bdeicon, "clicked",
SIGNAL_FUNC(thisDeicon), nil)
discard signal_connect(bmax, "clicked",
SIGNAL_FUNC(thisMax), nil)
discard signal_connect(bunmax, "clicked",
SIGNAL_FUNC(thisUnmax), nil)
discard signal_connect(bhide, "clicked",
SIGNAL_FUNC(thisHide), nil)
discard signal_connect(bshow, "clicked",
SIGNAL_FUNC(thisShow), nil)
discard signal_connect(bmove, "clicked",
SIGNAL_FUNC(thismove), nil)
discard signal_connect(bquit, "clicked",
SIGNAL_FUNC(thisDestroy), nil)
show_all(window)
main()</lang>
==={{libheader|IUP}}===
<langsyntaxhighlight lang="nim">import iup
 
# assumes you have the iup .dll or .so installed
Line 575 ⟶ 1,011:
discard dlg.show()
discard mainloop()
iup.Close()</langsyntaxhighlight>
 
=={{header|Oz}}==
Line 586 ⟶ 1,022:
because it shows how to extend an object's interface even when we don't have control over object creation.
 
<langsyntaxhighlight lang="oz">declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
 
Line 673 ⟶ 1,109:
end
in
{Record.forAll Windows CreateWindow}</langsyntaxhighlight>
 
=={{header|Perl}}==
Line 683 ⟶ 1,119:
I wrote the code using the Tk module, as that's what I had on my computer, and I was too lazy to install Tkx. Translating from perl/tk to Tkx should be fairly trivial.
 
<langsyntaxhighlight Perllang="perl">#!perl
use strict;
use warnings;
Line 760 ⟶ 1,196:
 
__END__
</syntaxhighlight>
</lang>
 
In the Tcl code I translated from, the second label of the main window had a textvariable attribute. For some reason, this didn't work correctly for me, either due to a bug in Tk.pm, or some other reason. Because of that, I kept around a handle to that second label ($lab), and called configure on it when I needed it's text to change.
Line 768 ⟶ 1,204:
=={{header|Phix}}==
{{trans|Nim}}
{{libheader|Phix/basics}}
Included in the distribution as demo\rosetta\Window_management.exw
<lang {{libheader|Phix>include /pGUI.e}}
<!--<syntaxhighlight lang="phix">-->
 
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Window_management.exw</span>
Ihandle dlg
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
 
function doFull(Ihandle /*ih*/)
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span>
IupSetAttribute(dlg,"FULLSCREEN","YES")
return IUP_DEFAULT
<span style="color: #008080;">function</span> <span style="color: #000000;">doFull</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"FULLSCREEN"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
function doMax(Ihandle /*ih*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupSetAttribute(dlg,"PLACEMENT","MAXIMIZED")
-- this is a work-around to get the dialog minimised (on win platform)
<span style="color: #008080;">function</span> <span style="color: #000000;">doMax</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg,"VISIBLE","YES")
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PLACEMENT"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MAXIMIZED"</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #000080;font-style:italic;">-- this is a work-around to get the dialog minimised (on win platform)</span>
end function
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VISIBLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
function doMin(Ihandle /*ih*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupSetAttribute(dlg,"PLACEMENT","MINIMIZED")
-- this is a work-around to get the dialog minimised (on win platform)
<span style="color: #008080;">function</span> <span style="color: #000000;">doMin</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg,"VISIBLE","YES")
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PLACEMENT"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MINIMIZED"</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #000080;font-style:italic;">-- this is a work-around to get the dialog minimised (on win platform)</span>
end function
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VISIBLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
function doRestore(Ihandle /*ih*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupSetAttribute(dlg,"OPACITY","255")
IupSetAttribute(dlg,"FULLSCREEN","NO")
<span style="color: #008080;">function</span> <span style="color: #000000;">doRestore</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg,"PLACEMENT","NORMAL")
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"OPACITY"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"255"</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg,"VISIBLE","YES")
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"FULLSCREEN"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"NO"</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PLACEMENT"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"NORMAL"</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VISIBLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
function doDim(Ihandle /*ih*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupSetAttribute(dlg,"OPACITY","60")
return IUP_DEFAULT
<span style="color: #008080;">function</span> <span style="color: #000000;">doDim</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"OPACITY"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"60"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
function doShow(Ihandle /*ih*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupSetAttribute(dlg,"OPACITY","255")
return IUP_DEFAULT
<span style="color: #008080;">function</span> <span style="color: #000000;">doShow</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"OPACITY"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"255"</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
function doMove(Ihandle /*ih*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
integer {x,y} = IupGetIntInt(dlg,"SCREENPOSITION")
integer shift = iff(IupGetInt(NULL,"SHIFTKEY")?-10,+10)
<span style="color: #008080;">function</span> <span style="color: #000000;">doMove</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
IupShowXY(dlg,x+shift,y+shift)
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"SCREENPOSITION"</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #004080;">integer</span> <span style="color: #000000;">shift</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"SHIFTKEY"</span><span style="color: #0000FF;">)?-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">IupShowXY</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">shift</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">+</span><span style="color: #000000;">shift</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
procedure main()
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupOpen("../pGUI/")
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
Ihandle hbox = IupHbox({IupButton("restore", Icallback("doRestore")),
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
IupButton("full screen",Icallback("doFull")),
IupButton("maximize", Icallback("doMax")),
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">hbox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"restore"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"doRestore"</span><span style="color: #0000FF;">)),</span>
IupButton("minimize", Icallback("doMin")),
<span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"full screen"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"doFull"</span><span style="color: #0000FF;">)),</span>
IupButton("dim", Icallback("doDim")),
<span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"maximize"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"doMax"</span><span style="color: #0000FF;">)),</span>
IupButton("show", Icallback("doShow")),
<span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"minimize"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"doMin"</span><span style="color: #0000FF;">)),</span>
IupButton("move", Icallback("doMove"))})
<span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dim"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"doDim"</span><span style="color: #0000FF;">)),</span>
IupSetAttribute(hbox,"MARGIN", "10x10")
<span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"show"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"doShow"</span><span style="color: #0000FF;">)),</span>
IupSetAttribute(hbox,"PADDING", "5x5")
<span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"move"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"doMove"</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hbox</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MARGIN"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"10x10"</span><span style="color: #0000FF;">)</span>
dlg = IupDialog(hbox)
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hbox</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PADDING"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"5x5"</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg,"OPACITY","255")
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hbox</span><span style="color: #0000FF;">)</span>
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"OPACITY"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"255"</span><span style="color: #0000FF;">)</span>
IupMainLoop()
IupClose()
<span style="color: #7060A8;">IupShowXY</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #004600;">IUP_CENTER</span><span style="color: #0000FF;">,</span><span style="color: #004600;">IUP_CENTER</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
main()</lang>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
The following works on ErsatzLisp, the Java version of PicoLisp.
<langsyntaxhighlight PicoLisplang="picolisp">$ ersatz/pil +
: (setq
JFrame "javax.swing.JFrame"
Line 880 ⟶ 1,322:
 
# Close window
(java Win 'dispose)</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">;- Create a linked list to store created windows.
NewList Windows()
Define i, j, dh, dw, flags, err$, x, y
Line 942 ⟶ 1,384:
Delay(2000)
 
End</langsyntaxhighlight>
 
=={{header|Python}}==
Line 948 ⟶ 1,390:
{{libheader|tkinter}}
Using the tkinter GUI:
<syntaxhighlight lang="python">
<lang Python>
from tkinter import *
import tkinter.messagebox
Line 981 ⟶ 1,423:
 
mainloop()
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket/gui
 
Line 1,002 ⟶ 1,444:
(say 'Resize) (send frame resize 100 100) (sleep 1)
(say 'Close) (send frame show #f) (sleep 1) ; that's how we close a window
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.12}}
This are generic window handling routines. They work for any window managed by the X11 display server, not just windows created by the program.
 
<syntaxhighlight lang="raku" line>use X11::libxdo;
 
my $xdo = Xdo.new;
 
say 'Visible windows:';
printf "Class: %-21s ID#: %10d pid: %5d Name: %s\n", $_<class ID pid name>
for $xdo.get-windows.sort(+*.key)».value;
sleep 2;
 
my $id = $xdo.get-active-window;
 
my ($w, $h ) = $xdo.get-window-size( $id );
my ($wx, $wy) = $xdo.get-window-location( $id );
my ($dw, $dh) = $xdo.get-desktop-dimensions( 0 );
 
$xdo.move-window( $id, 150, 150 );
 
$xdo.set-window-size( $id, 350, 350, 0 );
 
sleep .25;
 
for flat 1 .. $dw - 350, $dw - 350, {$_ - 1} … 1 -> $mx { #
my $my = (($mx / $dw * τ).sin * 500).abs.Int;
$xdo.move-window( $id, $mx, $my );
$xdo.activate-window($id);
}
 
sleep .25;
 
$xdo.move-window( $id, 150, 150 );
 
my $dx = $dw - 300;
my $dy = $dh - 300;
 
$xdo.set-window-size( $id, $dx, $dy, 0 );
 
sleep .25;
 
my $s = -1;
 
loop {
$dx += $s * ($dw / 200).ceiling;
$dy += $s * ($dh / 200).ceiling;
$xdo.set-window-size( $id, $dx, $dy, 0 );
$xdo.activate-window($id);
sleep .005;
$s *= -1 if $dy < 200;
last if $dx >= $dw;
}
 
sleep .25;
 
$xdo.set-window-size( $id, $w, $h, 0 );
$xdo.move-window( $id, $wx, $wy );
$xdo.activate-window($id);
 
sleep .25;
 
$xdo.minimize($id);
$xdo.activate-window($id);
sleep 1;
$xdo.raise-window($id);
sleep .25;
</syntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
 
Load "guilib.ring"
Line 1,162 ⟶ 1,672:
###--------------------------------------------
 
</syntaxhighlight>
</lang>
 
 
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tk
 
# How to open a window
Line 1,228 ⟶ 1,737:
grid [button .b4 -text "Maximize" -command maximizeWin] -
grid [button .b5 -text "Move" -command moveWin] -
grid [button .b6 -text "Resize" -command resizeWin] -</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Gtk-3.0}}
An embedded application so we can use the Gtk3 toolkit.
 
The following just manipulates a window created by the application itself and works fine on Ubuntu 22.04.
<syntaxhighlight lang="wren">/* Window_management.wren */
 
var GTK_WINDOW_TOPLEVEL = 0
var GTK_ORIENTATION_VERTICAL = 1
var GTK_BUTTONBOX_CENTER = 5
 
foreign class GtkWindow {
construct new(type) {}
 
foreign title=(title)
foreign setDefaultSize(width, height)
 
foreign add(widget)
foreign connectDestroy()
 
foreign showAll()
foreign iconify()
foreign deiconify()
foreign maximize()
foreign unmaximize()
foreign move(x, y)
foreign resize(width, height)
foreign hide()
foreign destroy()
}
 
foreign class GtkButtonBox {
construct new(orientation) {}
 
foreign spacing=(interval)
foreign layout=(layoutStyle)
 
foreign add(button)
}
 
foreign class GtkButton {
construct newWithLabel(label) {}
 
foreign connectClicked()
}
 
class Gdk {
foreign static flush()
}
 
class C {
foreign static sleep(secs)
}
 
var Window = GtkWindow.new(GTK_WINDOW_TOPLEVEL)
Window.title = "Window management"
Window.setDefaultSize(400, 400)
Window.connectDestroy()
 
class GtkCallbacks {
static btnClicked(label) {
if (label == "Minimize") {
Window.iconify()
} else if (label == "Unminimize") {
Window.deiconify()
} else if (label == "Maximize") {
Window.maximize()
} else if (label == "Unmaximize") {
Window.unmaximize()
} else if (label == "Move") {
Window.move(0, 0) // move to top left hand corner of display
} else if (label == "Resize") {
Window.resize(600, 600)
} else if (label == "Hide") {
Window.hide()
Gdk.flush()
C.sleep(5) // wait 5 seconds
Window.showAll()
} else if (label == "Close") {
Window.destroy()
}
}
}
 
var buttonBox = GtkButtonBox.new(GTK_ORIENTATION_VERTICAL)
buttonBox.spacing = 20
buttonBox.layout = GTK_BUTTONBOX_CENTER
Window.add(buttonBox)
 
var btnMin = GtkButton.newWithLabel("Minimize")
btnMin.connectClicked()
buttonBox.add(btnMin)
 
var btnUnmin = GtkButton.newWithLabel("Unminimize")
btnUnmin.connectClicked()
buttonBox.add(btnUnmin)
 
var btnMax = GtkButton.newWithLabel("Maximize")
btnMax.connectClicked()
buttonBox.add(btnMax)
 
var btnUnmax = GtkButton.newWithLabel("Unmaximize")
btnUnmax.connectClicked()
buttonBox.add(btnUnmax)
 
var btnMove = GtkButton.newWithLabel("Move")
btnMove.connectClicked()
buttonBox.add(btnMove)
 
var btnResize = GtkButton.newWithLabel("Resize")
btnResize.connectClicked()
buttonBox.add(btnResize)
 
var btnHide = GtkButton.newWithLabel("Hide")
btnHide.connectClicked()
buttonBox.add(btnHide)
 
var btnClose = GtkButton.newWithLabel("Close")
btnClose.connectClicked()
buttonBox.add(btnClose)
 
Window.showAll()</syntaxhighlight>
<br>
We now embed the above script in the following C program, build and run.
<syntaxhighlight lang="c">/* gcc `pkg-config --cflags gtk+-3.0` -DGDK_VERSION_MIN_REQIRED=GDK_VERSION_3_2 Window_management.c -o Window_management `pkg-config --libs gtk+-3.0` -lwren -lm */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <unistd.h>
#include "wren.h"
 
/* C <=> Wren interface functions */
 
WrenVM *vm;
 
static void btnClicked(GtkWidget *button, gpointer user_data) {
const gchar *label = gtk_button_get_label(GTK_BUTTON(button));
wrenEnsureSlots(vm, 2);
wrenGetVariable(vm, "main", "GtkCallbacks", 0);
WrenHandle *method = wrenMakeCallHandle(vm, "btnClicked(_)");
wrenSetSlotString(vm, 1, (const char *)label);
wrenCall(vm, method);
wrenReleaseHandle(vm, method);
}
 
void C_gtkWindowAllocate(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenSetSlotNewForeign(vm, 0, 0, sizeof(GtkWidget*));
GtkWindowType type = (GtkWindowType)wrenGetSlotDouble(vm, 1);
*window = gtk_window_new(type);
}
 
void C_gtkButtonBoxAllocate(WrenVM* vm) {
GtkWidget **buttonBox = (GtkWidget **)wrenSetSlotNewForeign(vm, 0, 0, sizeof(GtkWidget*));
GtkOrientation orientation = (GtkOrientation)wrenGetSlotDouble(vm, 1);
*buttonBox = gtk_button_box_new(orientation);
}
 
void C_gtkButtonAllocate(WrenVM* vm) {
GtkWidget **button = (GtkWidget **)wrenSetSlotNewForeign(vm, 0, 0, sizeof(GtkWidget*));
const gchar *label = (const gchar *)wrenGetSlotString(vm, 1);
*button = gtk_button_new_with_label(label);
}
 
void C_setTitle(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
const gchar *title = (const gchar *)wrenGetSlotString(vm, 1);
gtk_window_set_title(GTK_WINDOW(*window), title);
}
 
void C_setDefaultSize(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gint width = (gint)wrenGetSlotDouble(vm, 1);
gint height = (gint)wrenGetSlotDouble(vm, 2);
gtk_window_set_default_size(GTK_WINDOW(*window), width, height);
}
 
void C_add(WrenVM* vm) {
GtkWidget **container = (GtkWidget **)wrenGetSlotForeign(vm, 0);
GtkWidget **widget = (GtkWidget **)wrenGetSlotForeign(vm, 1);
gtk_container_add(GTK_CONTAINER(*container), *widget);
}
 
void C_connectDestroy(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
g_signal_connect(*window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
}
 
void C_showAll(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gtk_widget_show_all(*window);
}
 
void C_iconify(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gtk_window_iconify(GTK_WINDOW(*window));
}
 
void C_deiconify(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gtk_window_deiconify(GTK_WINDOW(*window));
}
 
void C_maximize(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gtk_window_maximize(GTK_WINDOW(*window));
}
 
void C_unmaximize(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gtk_window_unmaximize(GTK_WINDOW(*window));
}
 
void C_move(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gint x = (gint)wrenGetSlotDouble(vm, 1);
gint y = (gint)wrenGetSlotDouble(vm, 2);
gtk_window_move(GTK_WINDOW(*window), x, y);
}
 
void C_resize(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gint width = (gint)wrenGetSlotDouble(vm, 1);
gint height = (gint)wrenGetSlotDouble(vm, 2);
gtk_window_resize(GTK_WINDOW(*window), width, height);
}
 
void C_hide(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gtk_widget_hide(*window);
}
 
void C_destroy(WrenVM* vm) {
GtkWidget **window = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gtk_widget_destroy(*window);
}
 
void C_setSpacing(WrenVM* vm) {
GtkWidget **buttonBox = (GtkWidget **)wrenGetSlotForeign(vm, 0);
gint spacing = (gint)wrenGetSlotDouble(vm, 1);
gtk_box_set_spacing(GTK_BOX(*buttonBox), spacing);
}
 
void C_setLayout(WrenVM* vm) {
GtkWidget **buttonBox = (GtkWidget **)wrenGetSlotForeign(vm, 0);
GtkButtonBoxStyle layoutStyle = (GtkButtonBoxStyle)wrenGetSlotDouble(vm, 1);
gtk_button_box_set_layout(GTK_BUTTON_BOX(*buttonBox), layoutStyle);
}
 
void C_connectClicked(WrenVM* vm) {
GtkWidget **button = (GtkWidget **)wrenGetSlotForeign(vm, 0);
g_signal_connect(*button, "clicked", G_CALLBACK(btnClicked), NULL);
}
 
void C_flush(WrenVM* vm) {
gdk_display_flush(gdk_display_get_default());
}
 
void C_sleep(WrenVM* vm) {
unsigned int secs = (unsigned int)wrenGetSlotDouble(vm, 1);
sleep(secs);
}
 
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.allocate = NULL;
methods.finalize = NULL;
if (strcmp(module, "main") == 0) {
if (strcmp(className, "GtkWindow") == 0) {
methods.allocate = C_gtkWindowAllocate;
} else if (strcmp(className, "GtkButtonBox") == 0) {
methods.allocate = C_gtkButtonBoxAllocate;
} else if (strcmp(className, "GtkButton") == 0) {
methods.allocate = C_gtkButtonAllocate;
}
}
return methods;
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "GtkWindow") == 0) {
if (!isStatic && strcmp(signature, "title=(_)") == 0) return C_setTitle;
if (!isStatic && strcmp(signature, "setDefaultSize(_,_)") == 0) return C_setDefaultSize;
if (!isStatic && strcmp(signature, "add(_)") == 0) return C_add;
if (!isStatic && strcmp(signature, "connectDestroy()") == 0) return C_connectDestroy;
if (!isStatic && strcmp(signature, "showAll()") == 0) return C_showAll;
if (!isStatic && strcmp(signature, "iconify()") == 0) return C_iconify;
if (!isStatic && strcmp(signature, "deiconify()") == 0) return C_deiconify;
if (!isStatic && strcmp(signature, "maximize()") == 0) return C_maximize;
if (!isStatic && strcmp(signature, "unmaximize()") == 0) return C_unmaximize;
if (!isStatic && strcmp(signature, "move(_,_)") == 0) return C_move;
if (!isStatic && strcmp(signature, "resize(_,_)") == 0) return C_resize;
if (!isStatic && strcmp(signature, "hide()") == 0) return C_hide;
if (!isStatic && strcmp(signature, "destroy()") == 0) return C_destroy;
} else if (strcmp(className, "GtkButtonBox") == 0) {
if (!isStatic && strcmp(signature, "spacing=(_)") == 0) return C_setSpacing;
if (!isStatic && strcmp(signature, "layout=(_)") == 0) return C_setLayout;
if (!isStatic && strcmp(signature, "add(_)") == 0) return C_add;
} else if (strcmp(className, "GtkButton") == 0) {
if (!isStatic && strcmp(signature, "connectClicked()") == 0) return C_connectClicked;
} else if (strcmp(className, "Gdk") == 0) {
if (isStatic && strcmp(signature, "flush()") == 0) return C_flush;
} else if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "sleep(_)") == 0) return C_sleep;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main(int argc, char **argv) {
gtk_init(&argc, &argv);
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "Window_management.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
gtk_main();
wrenFreeVM(vm);
free(script);
return 0;
}</syntaxhighlight>
9,476

edits