Window creation: Difference between revisions
(updated and simplified) |
(→{{header|PureBasic}}: expanding the task) |
||
(88 intermediate revisions by 41 users not shown) | |||
Line 6: | Line 6: | ||
{{libheader|GtkAda}} |
{{libheader|GtkAda}} |
||
{{uses from|library|GtkAda|component1=Window|component2=Widget|component3=Handlers|component4=Main}} |
{{uses from|library|GtkAda|component1=Window|component2=Widget|component3=Handlers|component4=Main}} |
||
< |
<syntaxhighlight lang="ada">with Gtk.Window; use Gtk.Window; |
||
with Gtk.Widget; use Gtk.Widget; |
with Gtk.Widget; use Gtk.Widget; |
||
Line 46: | Line 46: | ||
Gtk.Main.Main; |
Gtk.Main.Main; |
||
end Windowed_Application;</ |
end Windowed_Application;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 52: | Line 52: | ||
{{works with|ELLA ALGOL 68|[https://sourceforge.net/projects/algol68/files/algol68toc/algol68toc_1.14 algol68toc_1.14]}} |
{{works with|ELLA ALGOL 68|[https://sourceforge.net/projects/algol68/files/algol68toc/algol68toc_1.14 algol68toc_1.14]}} |
||
'''Compile command:''' ca -l gtk-3 -l gdk-3 -l atk-1.0 -l gio-2.0 -l pangocairo-1.0 -l gdk_pixbuf-2.0 -l cairo-gobject -l pango-1.0 -l cairo -l gobject-2.0 -l glib-2.0 firstgtk.a68 |
'''Compile command:''' ca -l gtk-3 -l gdk-3 -l atk-1.0 -l gio-2.0 -l pangocairo-1.0 -l gdk_pixbuf-2.0 -l cairo-gobject -l pango-1.0 -l cairo -l gobject-2.0 -l glib-2.0 firstgtk.a68 |
||
< |
<syntaxhighlight lang="algol68">PROGRAM firstgtk CONTEXT VOID |
||
USE standard |
USE standard |
||
BEGIN |
BEGIN |
||
Line 90: | Line 90: | ||
gtk main |
gtk main |
||
END |
END |
||
FINISH</ |
FINISH</syntaxhighlight> |
||
=={{header|AmigaBASIC}}== |
|||
<syntaxhighlight lang="amigabasic">WINDOW 2,"New Window"</syntaxhighlight> |
|||
=={{header|AurelBasic}}== |
|||
<syntaxhighlight lang="aurelbasic">WIN 0 0 400 300 "New Window"</syntaxhighlight> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">Gui, Add, Text,, Hello |
||
Gui, Show</ |
Gui, Show</syntaxhighlight> |
||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
< |
<syntaxhighlight lang="autoit">GUICreate("Test") |
||
GUISetState(@SW_SHOW) |
GUISetState(@SW_SHOW) |
||
Line 105: | Line 114: | ||
Exit |
Exit |
||
EndSwitch |
EndSwitch |
||
Until False</ |
Until False</syntaxhighlight> |
||
=={{header| |
=={{header|BaCon}}== |
||
BaCon includes a Highlevel Universal GUI abstraction layer (HUG). The implementation is based on GTK. |
|||
<lang AurelBasic>WIN 0 0 400 300 "New Window"</lang> |
|||
<syntaxhighlight lang="freebasic">REM empty window |
|||
INCLUDE "hug.bac" |
|||
mainwin = WINDOW("Rosetta Code empty", 400, 300) |
|||
REM start gtk event loop... |
|||
DISPLAY</syntaxhighlight> |
|||
=={{header|BASIC256}}== |
|||
BASIC256 it has a built-in graphics mode. |
|||
<syntaxhighlight lang="basic256">clg |
|||
text (50,50, "I write in the graphics area")</syntaxhighlight> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"WINLIB2" |
||
dlg% = FN_newdialog("GUI Window", 0, 0, 200, 150, 8, 1000) |
dlg% = FN_newdialog("GUI Window", 0, 0, 200, 150, 8, 1000) |
||
PROC_showdialog(dlg%)</ |
PROC_showdialog(dlg%)</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Line 125: | Line 148: | ||
'''Compile Command:''' gcc `sdl-config --cflags` `sdl-config --libs` SDL_Window.c -o window |
'''Compile Command:''' gcc `sdl-config --cflags` `sdl-config --libs` SDL_Window.c -o window |
||
< |
<syntaxhighlight lang="c">/* |
||
* Opens an 800x600 16bit color window. |
* Opens an 800x600 16bit color window. |
||
* Done here with ANSI C. |
* Done here with ANSI C. |
||
Line 146: | Line 169: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===GTK=== |
===GTK=== |
||
Line 154: | Line 177: | ||
'''Compile command:''' gcc `gtk-config --cflags` `gtk-config --libs` -o window window.c |
'''Compile command:''' gcc `gtk-config --cflags` `gtk-config --libs` -o window window.c |
||
< |
<syntaxhighlight lang="c">#include <gtk/gtk.h> |
||
int |
int |
||
Line 169: | Line 192: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===GTK2=== |
===GTK2=== |
||
Line 176: | Line 199: | ||
'''Compile command:''' gcc -Wall -pedantic `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o window window.c |
'''Compile command:''' gcc -Wall -pedantic `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o window window.c |
||
< |
<syntaxhighlight lang="c">#include <gtk/gtk.h> |
||
int |
int |
||
Line 190: | Line 213: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===GLUT=== |
===GLUT=== |
||
Line 202: | Line 225: | ||
We ''are'' registering a keypress callback, which isn't strictly necessary; It simply allows us to use a keypress to close the program rather than depending on the windowing system the program is run under. |
We ''are'' registering a keypress callback, which isn't strictly necessary; It simply allows us to use a keypress to close the program rather than depending on the windowing system the program is run under. |
||
< |
<syntaxhighlight lang="c">// A C+GLUT implementation of the Creating a Window task at Rosetta Code |
||
// http://rosettacode.org/wiki/Creating_a_Window |
// http://rosettacode.org/wiki/Creating_a_Window |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 243: | Line 266: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
|||
{{libheader|Windows Forms}} |
|||
{{uses from|library|.NET Framework|component1=System.Windows.Forms|component2=System.Windows.Forms.Window|component3=System.Windows.Forms.Form|component4=System.Windows.Forms.Application}} |
|||
<syntaxhighlight lang="csharp">using System; |
|||
using System.Windows.Forms; |
|||
public class Window { |
|||
[STAThread] |
|||
static void Main() { |
|||
Form form = new Form(); |
|||
form.Text = "Window"; |
|||
form.Disposed += delegate { Application.Exit(); }; |
|||
form.Show(); |
|||
Application.Run(); |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 250: | Line 293: | ||
'''Compiler command:''' qmake -pro; qmake |
'''Compiler command:''' qmake -pro; qmake |
||
< |
<syntaxhighlight lang="cpp">#include <QApplication> |
||
#include <QMainWindow> |
#include <QMainWindow> |
||
Line 259: | Line 302: | ||
window.show(); |
window.show(); |
||
return app.exec(); |
return app.exec(); |
||
}</ |
}</syntaxhighlight> |
||
{{libheader|GTK}} |
{{libheader|GTK}} |
||
Line 265: | Line 308: | ||
'''Compiler command:''' g++ filename.cc -o test `pkg-config --cflags --libs gtkmm-2.4` |
'''Compiler command:''' g++ filename.cc -o test `pkg-config --cflags --libs gtkmm-2.4` |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <gtkmm.h> |
#include <gtkmm.h> |
||
Line 285: | Line 328: | ||
exit( 0 ) ; |
exit( 0 ) ; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
|||
{{libheader|Windows Forms}} |
|||
{{uses from|library|.NET Framework|component1=System.Windows.Forms|component2=System.Windows.Forms.Window|component3=System.Windows.Forms.Form|component4=System.Windows.Forms.Application}} |
|||
<lang csharp>using System; |
|||
using System.Windows.Forms; |
|||
public class Window { |
|||
[STAThread] |
|||
static void Main() { |
|||
Form form = new Form(); |
|||
form.Text = "Window"; |
|||
form.Disposed += delegate { Application.Exit(); }; |
|||
form.Show(); |
|||
Application.Run(); |
|||
} |
|||
}</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 311: | Line 334: | ||
{{uses from|library|Swing|component1=JFrame}} |
{{uses from|library|Swing|component1=JFrame}} |
||
< |
<syntaxhighlight lang="clojure">(import '(javax.swing JFrame)) |
||
(let [frame (JFrame. "A Window")] |
(let [frame (JFrame. "A Window")] |
||
(doto frame |
(doto frame |
||
(.setSize 600 800) |
(.setSize 600 800) |
||
(.setVisible true)))</ |
(.setVisible true)))</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 324: | Line 347: | ||
{{works with|LispWorks}} |
{{works with|LispWorks}} |
||
< |
<syntaxhighlight lang="lisp">(capi:display (make-instance 'capi:interface :title "A Window"))</syntaxhighlight> |
||
==={{libheader|CLIM}}=== |
==={{libheader|CLIM}}=== |
||
Line 332: | Line 355: | ||
Setting up the environment: |
Setting up the environment: |
||
< |
<syntaxhighlight lang="lisp">(require :mcclim) |
||
(cl:defpackage #:rc-window |
(cl:defpackage #:rc-window |
||
(:use #:clim-lisp #:clim)) |
(:use #:clim-lisp #:clim)) |
||
(cl:in-package #:rc-window)</ |
(cl:in-package #:rc-window)</syntaxhighlight> |
||
The actual definition and display: |
The actual definition and display: |
||
< |
<syntaxhighlight lang="lisp">(define-application-frame rc-window () |
||
() |
() |
||
(:layouts (:default))) |
(:layouts (:default))) |
||
(run-frame-top-level (make-application-frame 'rc-window))</ |
(run-frame-top-level (make-application-frame 'rc-window))</syntaxhighlight> |
||
Note: This creates a small, useless window ("frame"). Useful frames will have some ''panes'' defined inside them. |
Note: This creates a small, useless window ("frame"). Useful frames will have some ''panes'' defined inside them. |
||
Line 351: | Line 374: | ||
Works with the Armed Bear Common Lisp implementation that targets the JVM. |
Works with the Armed Bear Common Lisp implementation that targets the JVM. |
||
< |
<syntaxhighlight lang="lisp">(defun create-window () |
||
"Creates a window" |
"Creates a window" |
||
(let ((window (jnew (jconstructor "javax.swing.JFrame")))) |
(let ((window (jnew (jconstructor "javax.swing.JFrame")))) |
||
Line 357: | Line 380: | ||
window (make-immediate-object t :boolean)))) |
window (make-immediate-object t :boolean)))) |
||
(create-window)</ |
(create-window)</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{libheader|FLTK4d}} |
{{libheader|FLTK4d}} |
||
< |
<syntaxhighlight lang="d"> module Window; |
||
import fltk4d.all; |
import fltk4d.all; |
||
Line 369: | Line 392: | ||
window.show; |
window.show; |
||
FLTK.run; |
FLTK.run; |
||
}</ |
}</syntaxhighlight> |
||
{{libheader|Derelict}} |
{{libheader|Derelict}} |
||
{{libheader|SDL}} |
{{libheader|SDL}} |
||
< |
<syntaxhighlight lang="d"> import derelict.sdl.sdl; |
||
int main(char[][] args) |
int main(char[][] args) |
||
Line 404: | Line 427: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{libheader|QD}} |
{{libheader|QD}} |
||
QD is a simple and easy-to-use wrapper around SDL. |
QD is a simple and easy-to-use wrapper around SDL. |
||
< |
<syntaxhighlight lang="d"> import qd; |
||
void main() { |
void main() { |
||
screen(640, 480); |
screen(640, 480); |
||
while (true) events(); |
while (true) events(); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 420: | Line 442: | ||
This first example is a minimalist approach using Delphi's standard Window (form) creation procedure. In Delphi 7, this will create a single Window executable of 362KB. |
This first example is a minimalist approach using Delphi's standard Window (form) creation procedure. In Delphi 7, this will create a single Window executable of 362KB. |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
// The project file (Project1.dpr) |
// The project file (Project1.dpr) |
||
Line 479: | Line 501: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
This second example demonstrates a 'pure' Windows API approach (i.e. NOT using the Delphi Visual Component Library). In Delphi 7, this will create a single Window executable of 15KB. |
This second example demonstrates a 'pure' Windows API approach (i.e. NOT using the Delphi Visual Component Library). In Delphi 7, this will create a single Window executable of 15KB. |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Project3; |
program Project3; |
||
Line 547: | Line 569: | ||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Dragon}}== |
|||
{{libheader|GUI}} |
|||
<syntaxhighlight lang="dragon">select "GUI" |
|||
window = newWindow("Window") |
|||
window.setSize(400,600) |
|||
window.setVisible() |
|||
</syntaxhighlight> |
|||
=={{header|E}}== |
=={{header|E}}== |
||
Line 566: | Line 598: | ||
Platform independent EiffelVision 2 Library |
Platform independent EiffelVision 2 Library |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
APPLICATION |
APPLICATION |
||
inherit |
inherit |
||
Line 584: | Line 616: | ||
first_window: MAIN_WINDOW |
first_window: MAIN_WINDOW |
||
-- Main window. |
-- Main window. |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
MAIN_WINDOW |
MAIN_WINDOW |
||
inherit |
inherit |
||
Line 636: | Line 668: | ||
main_container_created: main_container /= Void |
main_container_created: main_container /= Void |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Windows Forms}} |
{{libheader|Windows Forms}} |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
APPLICATION |
APPLICATION |
||
inherit |
inherit |
||
Line 658: | Line 690: | ||
{WINFORMS_APPLICATION}.run_form (Current) |
{WINFORMS_APPLICATION}.run_form (Current) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
<syntaxhighlight lang="lisp">(make-frame)</syntaxhighlight> |
|||
<lang lisp> |
|||
(make-frame) |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
===ARWEN=== |
===ARWEN=== |
||
{{libheader|ARWEN}} |
{{libheader|ARWEN}} |
||
< |
<syntaxhighlight lang="euphoria">include arwen.ew |
||
constant win = create(Window, "ARWEN window", 0, 0,100,100,640,480,{0,0}) |
constant win = create(Window, "ARWEN window", 0, 0,100,100,640,480,{0,0}) |
||
WinMain(win, SW_NORMAL) |
WinMain(win, SW_NORMAL) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===EuGTK=== |
===EuGTK=== |
||
{{libheader|EuGTK}} |
{{libheader|EuGTK}} |
||
< |
<syntaxhighlight lang="euphoria">include GtkEngine.e |
||
constant win = create(GtkWindow) |
constant win = create(GtkWindow,"title=EuGTK Window;size=640x480;border=10;$destroy=Quit") |
||
connect(win,"destroy",quit) |
|||
set(win,"title","Simple Window") |
|||
set(win,"default size",300,100) |
|||
set(win,"position",GTK_WIN_POS_CENTER) |
|||
show_all(win) |
show_all(win) |
||
main()</ |
main()</syntaxhighlight> |
||
===EuWinGUI=== |
===EuWinGUI=== |
||
{{libheader|EuWinGUI}} |
{{libheader|EuWinGUI}} |
||
< |
<syntaxhighlight lang="euphoria">include EuWinGUI.ew |
||
Window("EuWinGUI window",100,100,640,480) |
Window("EuWinGUI window",100,100,640,480) |
||
Line 699: | Line 725: | ||
end while |
end while |
||
CloseApp(0)</ |
CloseApp(0)</syntaxhighlight> |
||
===Win32Lib=== |
===Win32Lib=== |
||
{{libheader|Win32Lib}} |
{{libheader|Win32Lib}} |
||
< |
<syntaxhighlight lang="euphoria">include Win32Lib.ew |
||
constant win = createEx( Window, "Win32Lib", 0, Default, Default, 640, 480, 0, 0 ) |
constant win = createEx( Window, "Win32Lib", 0, Default, Default, 640, 480, 0, 0 ) |
||
WinMain( win, Normal )</ |
WinMain( win, Normal )</syntaxhighlight> |
||
===wxEuphoria=== |
===wxEuphoria=== |
||
{{libheader|wxEuphoria}} |
{{libheader|wxEuphoria}} |
||
< |
<syntaxhighlight lang="euphoria">include wxeu/wxeud.e |
||
constant win = create( wxFrame, {0, -1, "wxEuphoria window", -1, -1, 640, 480} ) |
constant win = create( wxFrame, {0, -1, "wxEuphoria window", -1, -1, 640, 480} ) |
||
wxMain( win )</ |
wxMain( win )</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
|||
Everything is provided by the .NET runtime so this is almost identical to [[C_sharp]]. |
|||
{{libheader|Windows Forms}} |
|||
<syntaxhighlight lang="fsharp"> open System.Windows.Forms |
|||
[<System.STAThread>] |
|||
do |
|||
Form(Text = "F# Window") |
|||
|> Application.Run</syntaxhighlight> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: ui ui.gadgets.labels ; |
||
"This is a window..." <label> "Really?" open-window</ |
"This is a window..." <label> "Really?" open-window</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
using fwt |
using fwt |
||
Line 733: | Line 770: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 740: | Line 777: | ||
'''gtk-server command:''' gtk-server -fifo=ffl-fifo & |
'''gtk-server command:''' gtk-server -fifo=ffl-fifo & |
||
< |
<syntaxhighlight lang="forth">include ffl/gsv.fs |
||
\ Open the connection to the gtk-server and load the Gtk2 definitions |
\ Open the connection to the gtk-server and load the Gtk2 definitions |
||
Line 772: | Line 809: | ||
gsv+close drop |
gsv+close drop |
||
[THEN]</ |
[THEN]</syntaxhighlight> |
||
===iMops=== |
|||
{{works with|iMops on MacOS}} |
|||
<syntaxhighlight lang="iMops"> |
|||
Window+ w \ create a window |
|||
View v \ create a view |
|||
300 30 430 230 put: frameRect \ size a rectangle for the view |
|||
frameRect " Test" docWindow v new: w \ activate the view and window |
|||
show: w \ display the window |
|||
</syntaxhighlight> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic"> |
|||
#Include "windows.bi" |
|||
Dim As HWND Window_Main |
|||
Dim As MSG msg |
|||
'Create the window: |
|||
Window_Main = CreateWindow("#32770", "I am a window - close me!", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 350, 200, 0, 0, 0, 0) |
|||
'Windows message loop: |
|||
While GetMessage(@msg, Window_Main, 0, 0) |
|||
TranslateMessage(@msg) |
|||
DispatchMessage(@msg) |
|||
If msg.hwnd = Window_Main And msg.message = WM_COMMAND Then End |
|||
Wend |
|||
End |
|||
</syntaxhighlight> |
|||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink"> |
||
g=(new graphics).show[] |
g=(new graphics).show[] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header| |
=={{header|FutureBasic}}== |
||
<syntaxhighlight lang="futurebasic">window 1</syntaxhighlight> |
|||
Everything is provided by the .NET runtime so this is almost identical to [[C_sharp]]. |
|||
=={{header|Gambas}}== |
|||
<syntaxhighlight lang="gambas">Public Sub Form_Open() |
|||
End</syntaxhighlight> |
|||
{{libheader|Windows Forms}} |
|||
<lang fsharp> open System.Windows.Forms |
|||
[<System.STAThread>] |
|||
do |
|||
Form(Text = "F# Window") |
|||
|> Application.Run</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
===GTK=== |
===GTK=== |
||
{{libheader|go-gtk}} |
{{libheader|go-gtk}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 806: | Line 873: | ||
window.Show() |
window.Show() |
||
gtk.Main() |
gtk.Main() |
||
}</ |
}</syntaxhighlight> |
||
===SDL=== |
===SDL=== |
||
{{libheader|Go-SDL}} |
{{libheader|Go-SDL}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 831: | Line 898: | ||
} |
} |
||
window.Destroy() |
window.Destroy() |
||
}</ |
}</syntaxhighlight> |
||
===X11=== |
===X11=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 855: | Line 922: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 868: | Line 935: | ||
We will open notepad as a window here. |
We will open notepad as a window here. |
||
<lang |
<syntaxhighlight lang="guiss">Start,Programs,Accessories,Notepad</syntaxhighlight> |
||
To close the window: |
To close the window: |
||
<lang |
<syntaxhighlight lang="guiss">Window:Notepad,Button:Close</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 878: | Line 945: | ||
A simple graphics library, designed to give the programmer access to most interesting parts of the Win32 Graphics Device Interface and X11 library without exposing the programmer to the pain and anguish usually associated with using these interfaces. |
A simple graphics library, designed to give the programmer access to most interesting parts of the Win32 Graphics Device Interface and X11 library without exposing the programmer to the pain and anguish usually associated with using these interfaces. |
||
< |
<syntaxhighlight lang="haskell">import Graphics.HGL |
||
aWindow = runGraphics $ |
aWindow = runGraphics $ |
||
withWindow_ "Rosetta Code task: Creating a window" (300, 200) $ \ w -> do |
withWindow_ "Rosetta Code task: Creating a window" (300, 200) $ \ w -> do |
||
drawInWindow w $ text (100, 100) "Hello World" |
drawInWindow w $ text (100, 100) "Hello World" |
||
getKey w</ |
getKey w</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">WINDOW(WINdowhandle=handle, Width=80, Height=-400, X=1, Y=1/2, TItle="Rosetta Window_creation Example") |
||
! window units: as pixels < 0, as relative window size 0...1, ascurrent character sizes > 1 |
! window units: as pixels < 0, as relative window size 0...1, ascurrent character sizes > 1 |
||
WRITE(WINdowhandle=handle) '... some output ...'</ |
WRITE(WINdowhandle=handle) '... some output ...'</syntaxhighlight> |
||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
Line 900: | Line 967: | ||
Icon and Unicon windowing is portable between Windows and X-Windows environments. |
Icon and Unicon windowing is portable between Windows and X-Windows environments. |
||
==={{header|Icon}}=== |
==={{header|Icon}}=== |
||
< |
<syntaxhighlight lang="icon">link graphics |
||
procedure main(arglist) |
procedure main(arglist) |
||
Line 906: | Line 973: | ||
WOpen("size=300, 300", "fg=blue", "bg=light gray") |
WOpen("size=300, 300", "fg=blue", "bg=light gray") |
||
WDone() |
WDone() |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 914: | Line 981: | ||
The Icon solution works in Unicon. An Unicon-only version is as follows: |
The Icon solution works in Unicon. An Unicon-only version is as follows: |
||
< |
<syntaxhighlight lang="unicon"> |
||
import gui |
import gui |
||
$include "guih.icn" |
$include "guih.icn" |
||
Line 932: | Line 999: | ||
w.show_modal () |
w.show_modal () |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
A minimalist modal dialog: |
A minimalist modal dialog: |
||
<syntaxhighlight lang="j"> wdinfo 'Hamlet';'To be, or not to be: that is the question:'</syntaxhighlight> |
|||
<lang j> title=: '"Hamlet -- Act 3, Scene 1"' |
|||
text=: '"To be, or not to be: that is the question:"' |
|||
wd 'mb ',title,text |
|||
</lang> |
|||
A free-standing window: |
A free-standing window: |
||
< |
<syntaxhighlight lang="j">MINWDW=: noun define |
||
pc minwdw; |
pc minwdw; |
||
pas 162 85;pcenter; |
pas 162 85;pcenter; |
||
rem form end; |
|||
) |
) |
||
Line 956: | Line 1,020: | ||
) |
) |
||
minwdw_run ''</ |
minwdw_run ''</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{libheader|Swing}} |
{{libheader|Swing}} |
||
< |
<syntaxhighlight lang="java">import javax.swing.JFrame; |
||
public class Main { |
public class Main { |
||
Line 969: | Line 1,033: | ||
w.setVisible(true); |
w.setVisible(true); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
window.open("webpage.html", "windowname", "width=800,height=600"); |
window.open("webpage.html", "windowname", "width=800,height=600"); |
||
=={{header|Julia}}== |
|||
<syntaxhighlight lang="julia"># v0.6 |
|||
using Tk |
|||
w = Toplevel("Example")</syntaxhighlight> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="kotlin">import javax.swing.JFrame |
||
fun main(args : Array<String>) { |
fun main(args : Array<String>) { |
||
JFrame("Title").apply { |
|||
setSize(800, 600) |
|||
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) |
|||
defaultCloseOperation = JFrame.EXIT_ON_CLOSE |
|||
w.setSize(800, 600) |
|||
isVisible = true |
|||
} |
|||
}</lang> |
|||
}</syntaxhighlight> |
|||
=={{header|Liberty BASIC}}== |
|||
Minimum code required to fulfill the task. |
|||
<syntaxhighlight lang="lb">nomainwin |
|||
open "GUI Window" for window as #1 |
|||
wait |
|||
</syntaxhighlight> |
|||
As it would properly be used in a real program. |
|||
<syntaxhighlight lang="lb">nomainwin |
|||
open "GUI Window" for window as #1 |
|||
#1 "trapclose Quit" |
|||
wait |
|||
sub Quit hndl$ |
|||
close #hndl$ |
|||
end |
|||
end sub |
|||
</syntaxhighlight> |
|||
=={{header|Lingo}}== |
|||
<syntaxhighlight lang="lingo">win = window().new("New Window") |
|||
w = 320 |
|||
h = 240 |
|||
firstScreen = _system.deskTopRectList[1] |
|||
x = firstScreen.width/2 - w/2 |
|||
y = firstScreen.height/2- h/2 |
|||
win.rect = rect(x,y,x+w,y+h) |
|||
-- Director needs a binary movie file (*.dir) for opening new windows. But this |
|||
-- movie file can be totally empty, and if it's write protected in the filesystem, |
|||
-- it can be re-used for multiple windows. |
|||
win.filename = _movie.path & "empty.dir" |
|||
win.open()</syntaxhighlight> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{libheader|IUPLua}} |
{{libheader|IUPLua}} |
||
< |
<syntaxhighlight lang="lua">local iup = require "iuplua" |
||
iup.dialog{ |
iup.dialog{ |
||
Line 999: | Line 1,102: | ||
iup.MainLoop() |
iup.MainLoop() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header| |
=={{header|M2000 Interpreter}}== |
||
Window has title by default the name of variable, here MyForm |
|||
<lang Mathematica> |
|||
Window by default has 6000 twips width by 4000 twips height |
|||
CreateDocument[] |
|||
</lang> |
|||
Here we open MyForm as modal window (with 1 as first parameter, after "Show") |
|||
=={{header|Liberty BASIC}}== |
|||
Minimum code required to fulfill the task. |
|||
Window show a square in left side of title (we can change this to be at right), so this close the window (if we have proper event function, we can quit the closing). Also using key combination Alt-F4 close window too (also send event). |
|||
<lang lb>nomainwin |
|||
open "GUI Window" for window as #1 |
|||
Window open in the monitor where we see the mouse pointer. |
|||
wait |
|||
</lang> |
|||
M2000 Windows have own decoration, independent from platform (same to Xp, Windows 7, Windows 8, Windows 10 and Linux using Wine). |
|||
As it would properly be used in a real program. |
|||
<lang lb>nomainwin |
|||
open "GUI Window" for window as #1 |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
#1 "trapclose Quit" |
|||
Module DisplayWindow { |
|||
wait |
|||
Declare MyForm Form |
|||
sub Quit hndl$ |
|||
Method MyForm,"Show",1 |
|||
close #hndl$ |
|||
} |
|||
end |
|||
DisplayWindow |
|||
end sub |
|||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">CreateDocument[]</syntaxhighlight> |
|||
=={{header|mIRC Scripting Language}}== |
=={{header|mIRC Scripting Language}}== |
||
Line 1,034: | Line 1,139: | ||
.window -Cp +d @WindowName 600 480 |
.window -Cp +d @WindowName 600 480 |
||
} |
} |
||
=={{header|Nanoquery}}== |
|||
This program creates and displays a 300x300 window with no contents and the title Nanoquery. |
|||
<syntaxhighlight lang="nanoquery">w = new(Nanoquery.Util.Windows.Window).setSize(300,300).setTitle("Nanoquery").show()</syntaxhighlight> |
|||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
{{libheader|Swing}} |
{{libheader|Swing}} |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols binary |
options replace format comments java crossref symbols binary |
||
Line 1,091: | Line 1,200: | ||
method isFalse() public static returns boolean |
method isFalse() public static returns boolean |
||
return \(1 == 1) |
return \(1 == 1) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
=== gtk3 / gintro === |
|||
{{libheader|gintro}} |
|||
<syntaxhighlight lang="nim">import gintro/[glib, gobject, gtk, gio] |
|||
proc activate(app: Application) = |
|||
## Activate the application. |
|||
let window = newApplicationWindow(app) |
|||
window.setTitle("Window for Rosetta") |
|||
window.setSizeRequest(640, 480) |
|||
window.showAll() |
|||
let app = newApplication(Application, "Rosetta.Window") |
|||
discard app.connect("activate", activate) |
|||
discard app.run()</syntaxhighlight> |
|||
=== gtk2 === |
=== gtk2 === |
||
This is example 9 from the Araq/Nim github repository (modified to include a quit button) |
This is example 9 from the Araq/Nim github repository (modified to include a quit button) |
||
< |
<syntaxhighlight lang="nim">import gdk2, glib2, gtk2 |
||
gdk2, glib2, gtk2 |
|||
proc thisDestroy(widget: pWidget, data: pgpointer){.cdecl.} = |
|||
main_quit() |
|||
const |
const |
||
Inside |
Inside = "Mouse is over label" |
||
OutSide |
OutSide = "Mouse is not over label" |
||
# Context transmitted to callback. |
|||
var |
|||
type Context = object |
|||
OverButton: bool |
|||
label: PLabel |
|||
overButton: bool |
|||
proc changeLabel(p: PWidget; event: gdk2.PEventCrossing; context: var Context) {.cdecl.} = |
|||
context.label.set_text(if context.overButton: OutSide else: Inside) |
|||
context.overButton = not context.overButton |
|||
proc thisDestroy(widget: PWidget, data: Pgpointer) {.cdecl.} = |
|||
main_quit() |
|||
var context: Context |
|||
nim_init() |
nim_init() |
||
var window = window_new(gtk2.WINDOW_TOPLEVEL) |
|||
var stackbox = vbox_new(TRUE, 10) |
|||
var button1 = button_new("Move mouse over button") |
|||
var buttonstyle = copy(get_style(Button1)) |
|||
ButtonStyle.bg[STATE_PRELIGHT].pixel = 0 |
|||
ButtonStyle.bg[STATE_PRELIGHT].red = -1'i16 |
|||
ButtonStyle.bg[STATE_PRELIGHT].blue = 0'i16 |
|||
ButtonStyle.bg[STATE_PRELIGHT].green = 0'i16 |
|||
set_style(button1, buttonstyle) |
|||
var button2 = button_new() |
|||
var ALabel = label_new(Outside) |
|||
var button3 = button_new("Quit") |
|||
let window = window_new(gtk2.WINDOW_TOPLEVEL) |
|||
let stackbox = vbox_new(true, 10) |
|||
let button1 = button_new("Move mouse over button") |
|||
let buttonstyle = copy(button1.get_style()) |
|||
buttonstyle.bg[STATE_PRELIGHT] = TColor(pixel: 0, red: 255, green: 0, blue: 0) |
|||
button1.set_style(buttonstyle) |
|||
let button2 = button_new() |
|||
context = Context(label: label_new(Outside), overButton: false) |
|||
let button3 = button_new("Quit") |
|||
button2.add(context.label) |
|||
proc ChangeLabel(P: PWidget, Event: gdk2.PEventCrossing, |
|||
stackbox.pack_start(button1, true, true, 0) |
|||
Data: var bool){.cdecl.} = |
|||
stackbox.pack_start(button2, true, true, 0) |
|||
if Not Data: set_text(ALabel, Inside) |
|||
stackbox.pack_start(button3, true, true, 0) |
|||
else: set_text(ALabel, Outside) |
|||
window.set_border_width(5) |
|||
Data = Not Data |
|||
window.add(stackbox) |
|||
discard window.signal_connect("destroy", SIGNAL_FUNC(thisDestroy), nil) |
|||
discard button1.signal_connect("enter_notify_event", SIGNAL_FUNC(changeLabel), addr(context)) |
|||
add(button2, ALAbel) |
|||
discard button1.signal_connect("leave_notify_event", SIGNAL_FUNC(changeLabel), addr(context)) |
|||
pack_start(stackbox, button1, TRUE, TRUE, 0) |
|||
discard button3.signal_connect("clicked", SIGNAL_FUNC(thisDestroy), nil) |
|||
pack_start(stackbox, button2, TRUE, TRUE, 0) |
|||
pack_start(stackbox, button3, TRUE, TRUE, 0) |
|||
window.show_all() |
|||
set_border_width(Window, 5) |
|||
main()</syntaxhighlight> |
|||
add(window, stackbox) |
|||
discard signal_connect(window, "destroy", |
|||
SIGNAL_FUNC(thisDestroy), nil) |
|||
overbutton = False |
|||
discard signal_connect(button1, "enter_notify_event", |
|||
SIGNAL_FUNC(ChangeLabel), addr(OverButton)) |
|||
discard signal_connect(button1, "leave_notify_event", |
|||
SIGNAL_FUNC(ChangeLabel), addr(OverButton)) |
|||
discard signal_connect(button3, "clicked", |
|||
SIGNAL_FUNC(thisDestroy), nil) |
|||
show_all(window) |
|||
main()</lang> |
|||
=== SDL === |
=== SDL === |
||
< |
<syntaxhighlight lang="nim">import |
||
sdl, sdl_image, colors |
sdl, sdl_image, colors |
||
Line 1,197: | Line 1,317: | ||
greeting.freeSurface() |
greeting.freeSurface() |
||
screen.freeSurface() |
screen.freeSurface() |
||
sdl.Quit()</ |
sdl.Quit()</syntaxhighlight> |
||
=== X11 === |
=== X11 === |
||
<lang |
<syntaxhighlight lang="nim">import x11/[xlib, xutil, x] |
||
const |
const |
||
WINDOW_WIDTH = 400 |
WINDOW_WIDTH = 400 |
||
WINDOW_HEIGHT = 300 |
WINDOW_HEIGHT = 300 |
||
var |
|||
width, height: cuint |
|||
display: PDisplay |
|||
screen: cint |
|||
depth: int |
|||
win: TWindow |
|||
sizeHints: TXSizeHints |
|||
type WindowData = tuple[display: PDisplay; window: Window] |
|||
proc create_window = |
|||
width = WINDOW_WIDTH |
|||
height = WINDOW_HEIGHT |
|||
proc createWindow: WindowData = |
|||
display = XOpenDisplay(nil) |
|||
let width: cuint = WINDOW_WIDTH |
|||
let height: cuint = WINDOW_HEIGHT |
|||
var sizeHints: XSizeHints |
|||
let display = XOpenDisplay(nil) |
|||
if display == nil: |
if display == nil: |
||
echo |
echo "Connection to X server failed." |
||
quit |
quit QuitFailure |
||
screen = XDefaultScreen(display) |
let screen = XDefaultScreen(display) |
||
depth = XDefaultDepth(display, screen) |
|||
var rootwin = XRootWindow(display, screen) |
var rootwin = XRootWindow(display, screen) |
||
win = XCreateSimpleWindow(display, rootwin, 100, 10, |
let win = XCreateSimpleWindow(display, rootwin, 100, 10, width, height, 5, |
||
XBlackPixel(display, screen), XWhitePixel(display, screen)) |
|||
sizeHints.flags = PSize or PMinSize or PMaxSize |
|||
XBlackPixel(display, screen), |
|||
sizeHints.min_width = width.cint |
|||
XWhitePixel(display, screen)) |
|||
sizeHints.max_width = width.cint |
|||
size_hints.flags = PSize or PMinSize or PMaxSize |
|||
sizeHints.min_height = height.cint |
|||
sizeHints.max_height = height.cint |
|||
discard XSetStandardProperties( |
|||
size_hints.min_height = height.cint |
|||
display, win, "Simple Window", "window", 0, nil, 0, addr(sizeHints)) |
|||
size_hints.max_height = height.cint |
|||
discard |
discard XSelectInput(display, win, ButtonPressMask or KeyPressMask or PointerMotionMask) |
||
0, nil, 0, addr(size_hints)) |
|||
discard XSelectInput(display, win, ButtonPressMask or KeyPressMask or |
|||
PointerMotionMask) |
|||
discard XMapWindow(display, win) |
discard XMapWindow(display, win) |
||
result = (display, win) |
|||
proc closeWindow(data: WindowData) = |
|||
proc close_window = |
|||
discard XDestroyWindow(display, |
discard XDestroyWindow(data.display, data.window) |
||
discard XCloseDisplay(display) |
discard XCloseDisplay(data.display) |
||
var |
|||
xev: TXEvent |
|||
proc processEvent(xev: var XEvent) = |
|||
proc process_event = |
|||
var key: |
var key: KeySym |
||
case |
case xev.theType.int |
||
of KeyPress: |
of KeyPress: |
||
key = XLookupKeysym(cast[ptr |
key = XLookupKeysym(cast[ptr XKeyEvent](addr(xev)), 0) |
||
if key.int != 0: |
if key.int != 0: |
||
echo |
echo "keyboard event ", key.int |
||
if key.int == 65307: # <Esc> |
if key.int == 65307: # <Esc> |
||
quit |
quit QuitSuccess |
||
of ButtonPressMask, PointerMotionMask: |
of ButtonPressMask, PointerMotionMask: |
||
echo "Mouse event" |
|||
else: |
else: |
||
discard |
|||
proc eventloop = |
proc eventloop(data: WindowData) = |
||
var xev: XEvent |
|||
discard XFlush(display) |
|||
discard XFlush(data.display) |
|||
var numEvents = XPending(data.display).int |
|||
while num_events != 0: |
|||
while numEvents != 0: |
|||
dec(num_events) |
|||
dec numEvents |
|||
discard XNextEvent(display, addr(xev)) |
|||
discard XNextEvent(data.display, addr(xev)) |
|||
process_event() |
|||
processEvent(xev) |
|||
let windata = createWindow() |
|||
create_window() |
|||
while true: |
while true: |
||
eventloop() |
eventloop(windata) |
||
windata.closeWindow()</syntaxhighlight> |
|||
close_window() |
|||
</lang> |
|||
=== glut === |
=== glut === |
||
< |
<syntaxhighlight lang="nim">import glut |
||
var win: int = 0 |
var win: int = 0 |
||
Line 1,288: | Line 1,398: | ||
win = glutCreateWindow("Goodbye, World!") |
win = glutCreateWindow("Goodbye, World!") |
||
glutKeyboardFunc(TGlut1Char2IntCallback(myOnKeyPress)) |
glutKeyboardFunc(TGlut1Char2IntCallback(myOnKeyPress)) |
||
glutMainLoop()</ |
glutMainLoop()</syntaxhighlight> |
||
=== win === |
=== win === |
||
< |
<syntaxhighlight lang="nim"># test a Windows GUI application |
||
import |
import |
||
Line 1,299: | Line 1,409: | ||
# {stdcall, import: "MessageBox", header: "<windows.h>"} |
# {stdcall, import: "MessageBox", header: "<windows.h>"} |
||
discard MessageBox(0, "Hello World!", "Nim GUI Application", 0)</ |
discard MessageBox(0, "Hello World!", "Nim GUI Application", 0)</syntaxhighlight> |
||
=== IUP === |
=== IUP === |
||
< |
<syntaxhighlight lang="nim">import iup |
||
# assumes you have the iup .dll or .so installed |
# assumes you have the iup .dll or .so installed |
||
Line 1,310: | Line 1,420: | ||
# now use a Dialog box to show a message |
# now use a Dialog box to show a message |
||
var lbl = |
var lbl = label("Hello World") |
||
setAttribute(lbl,"PADDING","10x10") |
setAttribute(lbl,"PADDING","10x10") |
||
var contents = |
var contents = hbox(lbl, nil) |
||
#SetAttribute(contents, "MARGIN", "5x5") |
#SetAttribute(contents, "MARGIN", "5x5") |
||
var dlg = |
var dlg = dialog(contents) |
||
#SetAttribute(dlg, "SIZE", "100x50") |
#SetAttribute(dlg, "SIZE", "100x50") |
||
Line 1,325: | Line 1,435: | ||
discard mainloop() |
discard mainloop() |
||
iup. |
iup.close()</syntaxhighlight> |
||
=== wxWidgets === |
|||
This example works on MacOS but should be cross platform and native |
|||
<syntaxhighlight lang="nim">import wx |
|||
{.experimental.} |
|||
const |
|||
TITLE = "Rosetta Code - Window Creation Nim" |
|||
WIDTH = 300 |
|||
HEIGHT = 300 |
|||
let |
|||
POSITION = construct_wxPoint(100,100) |
|||
SIZE = construct_wxSize(WIDTH,HEIGHT) |
|||
let window = cnew construct_wxFrame(nil, wxID_ANY, TITLE, POSITION, SIZE) |
|||
window.show(true) |
|||
run_main_loop()</syntaxhighlight> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
use Gtk2; |
use Gtk2; |
||
Line 1,343: | Line 1,474: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
Line 1,352: | Line 1,483: | ||
It opens a 800×600 window, centered on the screen, with title "A Window". |
It opens a 800×600 window, centered on the screen, with title "A Window". |
||
< |
<syntaxhighlight lang="objc">#include <Foundation/Foundation.h> |
||
#include <AppKit/AppKit.h> |
#include <AppKit/AppKit.h> |
||
Line 1,400: | Line 1,531: | ||
} |
} |
||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
{{libheader|LablTk}} |
{{libheader|LablTk}} |
||
< |
<syntaxhighlight lang="ocaml">let () = |
||
let top = Tk.openTk() in |
let top = Tk.openTk() in |
||
Wm.title_set top "An Empty Window"; |
Wm.title_set top "An Empty Window"; |
||
Wm.geometry_set top "240x180"; |
Wm.geometry_set top "240x180"; |
||
Tk.mainLoop (); |
Tk.mainLoop (); |
||
;;</ |
;;</syntaxhighlight> |
||
execute with: |
execute with: |
||
ocaml -I +labltk labltk.cma sample.ml |
ocaml -I +labltk labltk.cma sample.ml |
||
with the [http://caml.inria.fr/pub/docs/manual-ocaml/manual039.html Graphics] module: |
with the [http://caml.inria.fr/pub/docs/manual-ocaml/manual039.html Graphics] module: |
||
< |
<syntaxhighlight lang="ocaml">open Graphics |
||
let () = |
let () = |
||
open_graph " 800x600"; |
open_graph " 800x600"; |
||
let _ = read_line() in |
let _ = read_line() in |
||
close_graph ()</ |
close_graph ()</syntaxhighlight> |
||
execute with: |
execute with: |
||
ocaml graphics.cma tmp.ml |
ocaml graphics.cma tmp.ml |
||
{{libheader|LablGTK2}} |
{{libheader|LablGTK2}} |
||
< |
<syntaxhighlight lang="ocaml">open GMain |
||
let window = GWindow.window ~border_width:2 () |
let window = GWindow.window ~border_width:2 () |
||
Line 1,435: | Line 1,566: | ||
button#connect#clicked ~callback:window#destroy; |
button#connect#clicked ~callback:window#destroy; |
||
window#show (); |
window#show (); |
||
Main.main ()</ |
Main.main ()</syntaxhighlight> |
||
execute with: |
execute with: |
||
ocaml -I +lablgtk2 lablgtk.cma gtkInit.cmo sample.ml |
ocaml -I +lablgtk2 lablgtk.cma gtkInit.cmo sample.ml |
||
{{libheader|OCamlSDL}} |
{{libheader|OCamlSDL}} |
||
< |
<syntaxhighlight lang="ocaml">let () = |
||
Sdl.init [`VIDEO]; |
Sdl.init [`VIDEO]; |
||
let _ = Sdlvideo.set_video_mode 200 200 [] in |
let _ = Sdlvideo.set_video_mode 200 200 [] in |
||
Sdltimer.delay 2000; |
Sdltimer.delay 2000; |
||
Sdl.quit ()</ |
Sdl.quit ()</syntaxhighlight> |
||
execute with: |
execute with: |
||
ocaml bigarray.cma -I +sdl sdl.cma sample.ml |
ocaml bigarray.cma -I +sdl sdl.cma sample.ml |
||
{{libheader|OCamlSDL2}} |
|||
<syntaxhighlight lang="ocaml">open Sdl |
|||
let () = |
|||
let width, height = (640, 480) in |
|||
Sdl.init [`VIDEO]; |
|||
let window, renderer = |
|||
Render.create_window_and_renderer |
|||
~width ~height ~flags:[] |
|||
in |
|||
let rgb = (0, 255, 0) in |
|||
let a = 255 in |
|||
Render.set_draw_color renderer rgb a; |
|||
Render.clear renderer; |
|||
Render.render_present renderer; |
|||
Timer.delay 3000; |
|||
Sdl.quit ()</syntaxhighlight> |
|||
execute with: |
|||
ocaml -I +sdl2 sdl2.cma sample.ml |
|||
{{libheader|ocaml-sfml}} |
{{libheader|ocaml-sfml}} |
||
< |
<syntaxhighlight lang="ocaml">let () = |
||
let app = SFRenderWindow.make (640, 480) "OCaml-SFML Windowing" in |
let app = SFRenderWindow.make (640, 480) "OCaml-SFML Windowing" in |
||
let rec loop() = |
let rec loop () = |
||
let continue = |
let continue = |
||
match SFRenderWindow. |
match SFRenderWindow.pollEvent app with |
||
| Some SFEvent.Closed -> false |
| Some SFEvent.Closed -> false |
||
| _ -> true |
| _ -> true |
||
Line 1,460: | Line 1,611: | ||
SFRenderWindow.clear app SFColor.black; |
SFRenderWindow.clear app SFColor.black; |
||
SFRenderWindow.display app; |
SFRenderWindow.display app; |
||
if continue then loop() |
if continue then loop () |
||
in |
in |
||
loop()</ |
loop ()</syntaxhighlight> |
||
execute with: |
execute with: |
||
ocaml |
ocaml -I /tmp/ocaml-sfml/src sfml_system.cma sfml_window.cma sfml_graphics.cma win.ml |
||
{{libheader|OCaml-Xlib}} |
{{libheader|OCaml-Xlib}} |
||
< |
<syntaxhighlight lang="ocaml">open Xlib |
||
let () = |
let () = |
||
Line 1,478: | Line 1,629: | ||
let _ = xNextEventFun d in (* waits any key-press event *) |
let _ = xNextEventFun d in (* waits any key-press event *) |
||
xCloseDisplay d; |
xCloseDisplay d; |
||
;;</ |
;;</syntaxhighlight> |
||
execute with: |
execute with: |
||
ocaml -I +Xlib Xlib.cma sample.ml |
ocaml -I +Xlib Xlib.cma sample.ml |
||
=={{header|Odin}}== |
|||
<syntaxhighlight lang="odin">package main |
|||
import "vendor:sdl2" |
|||
main :: proc() { |
|||
using sdl2 |
|||
window: ^Window = --- |
|||
renderer: ^Renderer = --- |
|||
event: Event = --- |
|||
Init(INIT_VIDEO) |
|||
CreateWindowAndRenderer( |
|||
640, 480, |
|||
WINDOW_SHOWN, |
|||
&window, &renderer |
|||
) |
|||
SetWindowTitle(window, "Empty window") |
|||
RenderPresent(renderer) |
|||
for event.type != .QUIT { |
|||
Delay(10) |
|||
PollEvent(&event) |
|||
} |
|||
DestroyRenderer(renderer) |
|||
DestroyWindow(window) |
|||
Quit() |
|||
}</syntaxhighlight> |
|||
=={{header|OpenEdge ABL/Progress 4GL}}== |
=={{header|OpenEdge ABL/Progress 4GL}}== |
||
<syntaxhighlight lang="openedgeabl"> |
|||
<lang OpenEdgeABL> |
|||
DEFINE VAR C-Win AS WIDGET-HANDLE NO-UNDO. |
DEFINE VAR C-Win AS WIDGET-HANDLE NO-UNDO. |
||
Line 1,519: | Line 1,701: | ||
WAIT-FOR CLOSE OF THIS-PROCEDURE. |
WAIT-FOR CLOSE OF THIS-PROCEDURE. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">functor |
||
import |
import |
||
Application |
Application |
||
Line 1,539: | Line 1,721: | ||
Window = {QTk.build GUIDescription} |
Window = {QTk.build GUIDescription} |
||
{Window show} |
{Window show} |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free_Pascal}} |
{{works with|Free_Pascal}} |
||
{{libheader|SysUtils}}{{libheader|SDL}} |
{{libheader|SysUtils}}{{libheader|SDL}} |
||
< |
<syntaxhighlight lang="pascal">Program WindowCreation_SDL; |
||
{$linklib SDL} |
{$linklib SDL} |
||
Line 1,560: | Line 1,742: | ||
sleep(2000); |
sleep(2000); |
||
SDL_Quit; |
SDL_Quit; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 1,566: | Line 1,748: | ||
==={{libheader|Perl/Tk}}=== |
==={{libheader|Perl/Tk}}=== |
||
< |
<syntaxhighlight lang="perl"> use Tk; |
||
MainWindow->new(); |
MainWindow->new(); |
||
MainLoop;</ |
MainLoop;</syntaxhighlight> |
||
==={{libheader|Perl/SDL}}=== |
==={{libheader|Perl/SDL}}=== |
||
< |
<syntaxhighlight lang="perl"> use SDL::App; |
||
use SDL::Event; |
use SDL::Event; |
||
Line 1,578: | Line 1,760: | ||
$app->loop({ |
$app->loop({ |
||
SDL_QUIT() => sub { exit 0; }, |
SDL_QUIT() => sub { exit 0; }, |
||
});</ |
});</syntaxhighlight> |
||
==={{libheader|Perl/ |
==={{libheader|Perl/Gtk3}}=== |
||
< |
<syntaxhighlight lang="perl"> use Gtk3 '-init'; |
||
$window = |
$window = Gtk3::Window->new; |
||
$window->signal_connect( |
$window->signal_connect( |
||
destroy => sub { |
destroy => sub { Gtk3->main_quit; } |
||
); |
); |
||
$window->show_all; |
$window->show_all; |
||
Gtk3->main;</syntaxhighlight> |
|||
==={{libheader|Perl/Gtk2}}=== |
|||
<lang perl> use Gtk2 '-init'; |
|||
$window = Gtk2::Window->new; |
|||
$window->signal_connect( |
|||
destroy => sub { Gtk2->main_quit; } |
|||
); |
|||
$window->show_all; |
|||
Gtk2->main;</lang> |
|||
==={{libheader|Perl/Qt}}=== |
==={{libheader|Perl/Qt}}=== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use QtGui4; |
use QtGui4; |
||
Line 1,608: | Line 1,780: | ||
my $window = Qt::MainWindow; |
my $window = Qt::MainWindow; |
||
$window->show; |
$window->show; |
||
exit $app->exec;</ |
exit $app->exec;</syntaxhighlight> |
||
==={{libheader|XUL::Gui}}=== |
|||
<lang perl>use XUL::Gui; |
|||
display Window;</lang> |
|||
==={{libheader|Perl/Wx}}=== |
==={{libheader|Perl/Wx}}=== |
||
< |
<syntaxhighlight lang="perl"> use Wx; |
||
$window = Wx::Frame->new(undef, -1, 'title'); |
$window = Wx::Frame->new(undef, -1, 'title'); |
||
$window->Show; |
$window->Show; |
||
Wx::SimpleApp->new->MainLoop;</ |
Wx::SimpleApp->new->MainLoop;</syntaxhighlight> |
||
=={{header|Perl 6}}== |
|||
<b>Library</b> [https://github.com/perl6/gtk-simple GTK] |
|||
Exit either by clicking the button or the close window control in the upper corner. |
|||
<lang perl6>use GTK::Simple; |
|||
my GTK::Simple::App $app .= new(title => 'Simple GTK Window'); |
|||
$app.size_request(250, 100); |
|||
$app.set_content( |
|||
GTK::Simple::VBox.new( |
|||
my $button = GTK::Simple::Button.new(label => 'Exit'), |
|||
) |
|||
); |
|||
$app.border_width = 40; |
|||
$button.clicked.tap: { $app.exit } |
|||
$app.run;</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
|||
The following is included in the distro as demo\rosetta\Window_creation.exw, |
|||
{{libheader|Phix/pGUI}} |
|||
which also contains some older windows-32-bit-only alternatives. |
|||
Works on Windows/Linux, 32/64-bit, and you can run this online [http://phix.x10.mx/p2js/Window_creation.htm here]. |
|||
<lang Phix>include pGUI.e |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Window_creation.exw</span> |
|||
function esc_close(Ihandle /*ih*/, atom c) |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
return iff(c=K_ESC?IUP_CLOSE:IUP_CONTINUE) |
|||
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
|||
end function |
|||
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span> |
|||
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hello"</span><span style="color: #0000FF;">)},</span><span style="color: #008000;">"MARGIN=200x200"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"TITLE=Hello"</span><span style="color: #0000FF;">))</span> |
|||
IupOpen("../pGUI/") |
|||
<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> |
|||
Ihandle dlg = IupDialog(IupVbox({IupLabel("hello")},"MARGIN=200x200")) |
|||
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span> |
|||
IupSetAttribute(dlg,"TITLE","Hello") |
|||
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span> |
|||
IupSetCallback(dlg, "K_ANY", Icallback("esc_close")) |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
IupShow(dlg) |
|||
<!--</syntaxhighlight>--> |
|||
IupMainLoop() |
|||
IupClose()</lang> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/openGl.l") |
||
(glutInit) |
(glutInit) |
||
(glutCreateWindow "Goodbye, World!") |
(glutCreateWindow "Goodbye, World!") |
||
(keyboardFunc '(() (bye))) |
(keyboardFunc '(() (bye))) |
||
(glutMainLoop)</ |
(glutMainLoop)</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
{{libheader|WPK}} |
{{libheader|WPK}} |
||
<lang |
<syntaxhighlight lang="powershell">New-Window -Show</syntaxhighlight> |
||
{{libheader|Windows Forms}} |
{{libheader|Windows Forms}} |
||
< |
<syntaxhighlight lang="powershell">$form = New-Object Windows.Forms.Form |
||
$form.Text = "A Window" |
$form.Text = "A Window" |
||
$form.Size = New-Object Drawing.Size(150,150) |
$form.Size = New-Object Drawing.Size(150,150) |
||
$form.ShowDialog() | Out-Null</ |
$form.ShowDialog() | Out-Null</syntaxhighlight> |
||
=={{header|Processing}}== |
|||
<syntaxhighlight lang="java"> |
|||
size(1000,1000); |
|||
</syntaxhighlight> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Works with SWI-Prolog which has a graphic interface XPCE. |
Works with SWI-Prolog which has a graphic interface XPCE. |
||
<lang |
<syntaxhighlight lang="prolog">?- new(D, window('Prolog Window')), send(D, open).</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Define.i MyWin, Event, x, y |
||
x = 400 |
|||
y = 300 |
|||
If OpenWindow(0, 0, 0, x, y, "I am a window - PureBasic", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) |
|||
If CreateImage(0, x, y) And StartDrawing(ImageOutput(0)) |
|||
DrawingMode(#PB_2DDrawing_Transparent) |
|||
; Event loop |
|||
Box(0, 0, x, y, #White) |
|||
Repeat |
|||
For i = 1 To 10 |
|||
DrawText(x/3, y/2, "Hello World!", #Black) |
|||
Select Event |
|||
;DrawText(Random(200), Random(200), "Hello World!", RGB(Random(255), Random(255), Random(255))) |
|||
Next i |
|||
StopDrawing() |
|||
ImageGadget(0, 0, 0, x, y, ImageID(0)) |
|||
EndIf |
|||
Repeat |
|||
Event = WaitWindowEvent() |
|||
Select Event |
|||
Case #PB_Event_Gadget |
Case #PB_Event_Gadget |
||
; Handle any gadget events here |
|||
Case #PB_Event_CloseWindow |
Case #PB_Event_CloseWindow |
||
Break |
|||
EndSelect |
EndSelect |
||
ForEver |
ForEver |
||
EndIf</syntaxhighlight> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|2.4 and 2.5}} |
{{works with|Python|2.4 and 2.5}} |
||
{{libheader|Tkinter}} |
==={{libheader|Tkinter}}=== |
||
< |
<syntaxhighlight lang="python"> import Tkinter |
||
w = Tkinter.Tk() |
w = Tkinter.Tk() |
||
w.mainloop()</ |
w.mainloop()</syntaxhighlight> |
||
{{works with|Python|3.7}} |
|||
<!-- see also https://stackoverflow.com/questions/673174/which-tkinter-modules-were-renamed-in-python-3 --> |
|||
<syntaxhighlight lang="python">import tkinter |
|||
w = tkinter.Tk() |
|||
w.mainloop()</syntaxhighlight> |
|||
{{libheader|wxPython}} |
==={{libheader|wxPython}}=== |
||
< |
<syntaxhighlight lang="python"> from wxPython.wx import * |
||
class MyApp(wxApp): |
class MyApp(wxApp): |
||
Line 1,718: | Line 1,887: | ||
app = MyApp(0) |
app = MyApp(0) |
||
app.MainLoop()</ |
app.MainLoop()</syntaxhighlight> |
||
{{libheader|Pythonwin}} |
==={{libheader|Pythonwin}}=== |
||
< |
<syntaxhighlight lang="python"> import win32ui |
||
from pywin.mfc.dialog import Dialog |
from pywin.mfc.dialog import Dialog |
||
d = Dialog(win32ui.IDD_SIMPLE_INPUT) |
d = Dialog(win32ui.IDD_SIMPLE_INPUT) |
||
d.CreateWindow()</ |
d.CreateWindow()</syntaxhighlight> |
||
{{libheader|PyGTK}} |
==={{libheader|PyGTK}}=== |
||
< |
<syntaxhighlight lang="python"> import gtk |
||
window = gtk.Window() |
window = gtk.Window() |
||
window.show() |
window.show() |
||
gtk.main()</ |
gtk.main()</syntaxhighlight> |
||
{{libheader|PyQT}} |
==={{libheader|PyQT}}=== |
||
< |
<syntaxhighlight lang="python"> from PyQt4.QtGui import * |
||
app = QApplication([]) |
app = QApplication([]) |
||
Line 1,741: | Line 1,910: | ||
win.show() |
win.show() |
||
app.exec_()</ |
app.exec_()</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
Although R cannot create windows itself, it has wrappers for several GUI toolkits. tcl/tk is shipped with R by default, and you can create windows with that. |
Although R cannot create windows itself, it has wrappers for several GUI toolkits. tcl/tk is shipped with R by default, and you can create windows with that. |
||
<syntaxhighlight lang="r"> |
|||
<lang r> |
|||
win <- tktoplevel() |
win <- tktoplevel() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{libheader|gWidgets}} |
{{libheader|gWidgets}} |
||
The gWidgets packages let you write GUIs in a toolkit independent way. You can create a window with |
The gWidgets packages let you write GUIs in a toolkit independent way. You can create a window with |
||
<syntaxhighlight lang="r"> |
|||
<lang r> |
|||
library(gWidgetstcltk) #or e.g. gWidgetsRGtk2 |
library(gWidgetstcltk) #or e.g. gWidgetsRGtk2 |
||
win <- gwindow() |
win <- gwindow() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket/gui |
#lang racket/gui |
||
(send (new frame% |
(send (new frame% |
||
Line 1,765: | Line 1,934: | ||
[width 100] [height 100]) |
[width 100] [height 100]) |
||
show #t) |
show #t) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<b>Library</b> [https://github.com/perl6/gtk-simple GTK] |
|||
Exit either by clicking the button or the close window control in the upper corner. |
|||
<syntaxhighlight lang="raku" line>use GTK::Simple; |
|||
use GTK::Simple::App; |
|||
my GTK::Simple::App $app .= new(title => 'Simple GTK Window'); |
|||
$app.size-request(250, 100); |
|||
$app.set-content( |
|||
GTK::Simple::VBox.new( |
|||
my $button = GTK::Simple::Button.new(label => 'Exit'), |
|||
) |
|||
); |
|||
$app.border-width = 40; |
|||
$button.clicked.tap: { $app.exit } |
|||
$app.run;</syntaxhighlight> |
|||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
Line 1,777: | Line 1,970: | ||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
<syntaxhighlight lang="rebol"> |
|||
<lang REBOL> |
|||
view layout [size 100x100] |
view layout [size 100x100] |
||
</syntaxhighlight> |
|||
</lang> |
|||
'size' needed to show the close-window button. |
'size' needed to show the close-window button. |
||
=={{header|Red}}== |
|||
Empty Window with close [X] button |
|||
<syntaxhighlight lang="red">>>view [] |
|||
</syntaxhighlight> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
Load "guilib.ring" |
Load "guilib.ring" |
||
Line 1,793: | Line 1,991: | ||
show()} |
show()} |
||
exec()} |
exec()} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Line 1,799: | Line 1,997: | ||
{{libheader|Ruby/Tk}} |
{{libheader|Ruby/Tk}} |
||
< |
<syntaxhighlight lang="ruby"> require 'tk' |
||
window = TkRoot::new() |
window = TkRoot::new() |
||
window::mainloop()</ |
window::mainloop()</syntaxhighlight> |
||
{{libheader|GTK}} |
{{libheader|GTK}} |
||
< |
<syntaxhighlight lang="ruby"> require 'gtk2' |
||
window = Gtk::Window.new.show |
window = Gtk::Window.new.show |
||
Gtk.main</ |
Gtk.main</syntaxhighlight> |
||
{{libheader|Shoes}} |
{{libheader|Shoes}} |
||
<lang |
<syntaxhighlight lang="ruby">Shoes.app {}</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
Show a empty browser with a button to "Close Me" |
Show a empty browser with a button to "Close Me" |
||
< |
<syntaxhighlight lang="runbasic">html "Close me!" |
||
button #c, "Close Me", [doExit] |
button #c, "Close Me", [doExit] |
||
wait |
wait |
||
Line 1,831: | Line 2,029: | ||
history.go(-a); |
history.go(-a); |
||
</script>" |
</script>" |
||
wait</ |
wait</syntaxhighlight> |
||
=={{header| |
=={{header|Rust}}== |
||
{{libheader| |
{{libheader|winit}} |
||
<syntaxhighlight lang="rust">use winit::event::{Event, WindowEvent}; // winit 0.24 |
|||
use winit::event_loop::{ControlFlow, EventLoop}; |
|||
use winit::window::WindowBuilder; |
|||
fn main() { |
|||
<lang scheme> |
|||
let event_loop = EventLoop::new(); |
|||
#!r6rs |
|||
let _win = WindowBuilder::new() |
|||
.with_title("Window") |
|||
.build(&event_loop).unwrap(); |
|||
event_loop.run(move |ev, _, flow| { |
|||
;; PS-TK example: display simple frame |
|||
match ev { |
|||
Event::WindowEvent { |
|||
event: WindowEvent::CloseRequested, .. |
|||
} => { |
|||
*flow = ControlFlow::Exit; |
|||
} |
|||
_ => {} |
|||
} |
|||
}); |
|||
}</syntaxhighlight> |
|||
(import (rnrs) |
|||
(lib pstk main) ; change this to refer to your installation of PS/Tk |
|||
) |
|||
(define tk (tk-start)) |
|||
(tk/wm 'title tk "PS-Tk Example: Frame") |
|||
(tk-event-loop tk) |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|sdljava}} |
{{libheader|sdljava}} |
||
{{libheader|Scala Java Swing interoperability}} |
|||
<lang scala>import javax.swing.JFrame |
|||
<syntaxhighlight lang="scala">import javax.swing.JFrame |
|||
object ShowWindow{ |
object ShowWindow{ |
||
Line 1,863: | Line 2,069: | ||
jf.setVisible(true) |
jf.setVisible(true) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Using native Scala libraries (which are wrappers over Java libraries): |
Using native Scala libraries (which are wrappers over Java libraries): |
||
< |
<syntaxhighlight lang="scala">import scala.swing._ |
||
import scala.swing.Swing._ |
import scala.swing.Swing._ |
||
Line 1,875: | Line 2,081: | ||
preferredSize = ((800, 600):Dimension) |
preferredSize = ((800, 600):Dimension) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scheme}}== |
|||
{{libheader|Scheme/PsTk}} |
|||
<syntaxhighlight lang="scheme"> |
|||
#!r6rs |
|||
;; PS-TK example: display simple frame |
|||
(import (rnrs) |
|||
(lib pstk main) ; change this to refer to your installation of PS/Tk |
|||
) |
|||
(define tk (tk-start)) |
|||
(tk/wm 'title tk "PS-Tk Example: Frame") |
|||
(tk-event-loop tk) |
|||
</syntaxhighlight> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 1,884: | Line 2,108: | ||
The program waits until a key is pressed and exits. |
The program waits until a key is pressed and exits. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "draw.s7i"; |
include "draw.s7i"; |
||
include "keybd.s7i"; |
include "keybd.s7i"; |
||
Line 1,893: | Line 2,117: | ||
KEYBOARD := GRAPH_KEYBOARD; |
KEYBOARD := GRAPH_KEYBOARD; |
||
ignore(getc(KEYBOARD)); |
ignore(getc(KEYBOARD)); |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
===Tk=== |
===Tk=== |
||
< |
<syntaxhighlight lang="ruby">var tk = require('Tk'); |
||
%s'MainWindow'.new; |
%s'MainWindow'.new; |
||
tk.MainLoop;</ |
tk.MainLoop;</syntaxhighlight> |
||
===Gtk2=== |
===Gtk2=== |
||
< |
<syntaxhighlight lang="ruby">var gtk2 = require('Gtk2') -> init; |
||
var window = %s'Gtk2::Window'.new; |
var window = %s'Gtk2::Window'.new; |
||
window.signal_connect(destroy => func(*_) { gtk2.main_quit }); |
window.signal_connect(destroy => func(*_) { gtk2.main_quit }); |
||
window.show_all; |
window.show_all; |
||
gtk2.main;</ |
gtk2.main;</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
{{works with|Pharo}} |
{{works with|Pharo}} |
||
{{works with|Squeak}} |
{{works with|Squeak}} |
||
<lang |
<syntaxhighlight lang="smalltalk">SystemWindow new openInWorld.</syntaxhighlight> |
||
{{works with|Smalltalk/X}} |
{{works with|Smalltalk/X}} |
||
< |
<syntaxhighlight lang="smalltalk">|top| |
||
top := TopView new. |
top := TopView new. |
||
top add: (Label label: 'Hello World') in:(0.0@0.0 corner:1.0@1.0). |
top add: (Label label: 'Hello World') in:(0.0@0.0 corner:1.0@1.0). |
||
top open</ |
top open</syntaxhighlight> |
||
=={{header|Standard ML}}== |
|||
Works with PolyML (XWindows/Motif module) |
|||
<syntaxhighlight lang="standard ml"> |
|||
open XWindows ; |
|||
open Motif ; |
|||
val showWindow = fn () => |
|||
let |
|||
val shell = XtAppInitialise "" "demo" "top" [] [XmNwidth 400, XmNheight 300 ] ; |
|||
val main = XmCreateMainWindow shell "main" [XmNmappedWhenManaged true ] ; |
|||
val buttn = XmCreateDrawnButton main "stop" [ XmNlabelString "Exit"] ; |
|||
val quit = fn (w,c,t) => (XtUnrealizeWidget shell; t) ; |
|||
in |
|||
( |
|||
XtSetCallbacks buttn [ (XmNactivateCallback, quit) ] XmNarmCallback ; |
|||
XtManageChildren [buttn]; |
|||
XtManageChild main; |
|||
XtRealizeWidget shell |
|||
) |
|||
end; |
|||
</syntaxhighlight> |
|||
call |
|||
<syntaxhighlight lang="standard ml"> |
|||
showWindow () |
|||
</syntaxhighlight> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
Loading the [[Tk]] package is all that is required to get an initial window: |
Loading the [[Tk]] package is all that is required to get an initial window: |
||
<lang |
<syntaxhighlight lang="tcl">package require Tk</syntaxhighlight> |
||
If you need an additional window: |
If you need an additional window: |
||
<lang |
<syntaxhighlight lang="tcl">toplevel .top</syntaxhighlight> |
||
If you are using the increasingly popular [http://www.equi4.com/tclkit.html tclkit] under MS Windows, all you have to do is associate the tclkit with the extension “<tt>.tcl</tt>” and then create an <i>empty</i> file with, e.g., with the name <tt>nothing.tcl</tt>. Double-clicking that will “open a window” (an empty one). |
If you are using the increasingly popular [http://www.equi4.com/tclkit.html tclkit] under MS Windows, all you have to do is associate the tclkit with the extension “<tt>.tcl</tt>” and then create an <i>empty</i> file with, e.g., with the name <tt>nothing.tcl</tt>. Double-clicking that will “open a window” (an empty one). |
||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |
||
< |
<syntaxhighlight lang="ti89b">:Text "Rosetta Code"</syntaxhighlight> |
||
=={{header|Toka}}== |
=={{header|Toka}}== |
||
Line 1,941: | Line 2,194: | ||
=={{header|TorqueScript}}== |
=={{header|TorqueScript}}== |
||
<syntaxhighlight lang="torquescript"> |
|||
<lang TorqueScript> |
|||
new GuiControl(GuiName) |
new GuiControl(GuiName) |
||
{ |
{ |
||
Line 1,980: | Line 2,233: | ||
canvas.pushDialog(GuiName); |
canvas.pushDialog(GuiName); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|TXR}}== |
|||
TXR has no library module for connecting to SDL, X11's Xlib, or GTK2. |
|||
All of these examples are completely self-contained, using the FFI capability in TXR, which can bind to any library whose interface is defined in terms of C functions and types. |
|||
No C header file is processed, and not a single line of C has to be compiled. |
|||
===SDL=== |
|||
{{trans|C}} |
|||
A wait for a SDL key-up event is added, missing in the C version, so that the window does not just appear and disappear. |
|||
Note that SDL's header file uses a <code>enum</code> for the event constants like <code>SDL_KEYUP</code>. But then in <code>union SD_Event</code>, the event field declared as <code>UInt8</code>. (That's how it appears on my Ubuntu system; newer versions of SDL seems to have switched the type field, and other fields of the event structures, to <code>UInt32</code>.) |
|||
Here, we exploit TXR's capability to define enumerations of specific types: we make the event enumeration based on <code>uint8</code>, giving it a <code>typedef</code> name, and then use that <code>typedef</code> in the <code>SD_Event</code> union. |
|||
<syntaxhighlight lang="txrlisp">(defvarl SDL_INIT_VIDEO #x00000020) |
|||
(defvarl SDL_SWSURFACE #x00000000) |
|||
(defvarl SDL_HWPALETTE #x20000000) |
|||
(typedef SDL_Surface (cptr SDL_Surface)) |
|||
(typedef SDL_EventType (enumed uint8 SDL_EventType |
|||
(SDL_KEYUP 3) |
|||
(SDL_QUIT 12))) |
|||
(typedef SDL_Event (union SD_Event |
|||
(type SDL_EventType) |
|||
(pad (array 8 uint32)))) |
|||
(with-dyn-lib "libSDL.so" |
|||
(deffi SDL_Init "SDL_Init" int (uint32)) |
|||
(deffi SDL_SetVideoMode "SDL_SetVideoMode" |
|||
SDL_Surface (int int int uint32)) |
|||
(deffi SDL_GetError "SDL_GetError" str ()) |
|||
(deffi SDL_WaitEvent "SDL_WaitEvent" int ((ptr-out SDL_Event))) |
|||
(deffi SDL_Quit "SDL_Quit" void ())) |
|||
(when (neql 0 (SDL_Init SDL_INIT_VIDEO)) |
|||
(put-string `unable to initialize SDL: @(SDL_GetError)`) |
|||
(exit nil)) |
|||
(unwind-protect |
|||
(progn |
|||
(SDL_SetVideoMode 800 600 16 (logior SDL_SWSURFACE SDL_HWPALETTE)) |
|||
(let ((e (make-union (ffi SDL_Event)))) |
|||
(until* (memql (union-get e 'type) '(SDL_KEYUP SDL_QUIT)) |
|||
(SDL_WaitEvent e)))) |
|||
(SDL_Quit))</syntaxhighlight> |
|||
===X11=== |
|||
{{trans|C}} |
|||
One difference between the C original and this one is that the XLib macros for direct structure access, like <code>DefaultGC</code>, <code>DefaultScreen</code> or <code>WhitePixel</code> are not used; rather the correspoding C functions are used via FFI: <code>XDefaultScreen</code> and so on. The macro approach can be mimiced in detail, at the cost of a significant increase in verbosity (cloning the full declaration of the <code>_XDisplay</code> struct declaration, and reproducing the macros). |
|||
Also, this uses an enumeration for the events, so when the event type is decoded from the <code>XEvent</code> union, it comes out as a Lisp symbol. |
|||
<syntaxhighlight lang="txrlisp">(typedef XID uint32) |
|||
(typedef Window XID) |
|||
(typedef Drawable XID) |
|||
(typedef Display (cptr Display)) |
|||
(typedef GC (cptr GC)) |
|||
(typedef XEventType (enum _XEventType |
|||
(KeyPress 2) |
|||
(Expose 12))) |
|||
(defvarl KeyPressMask (ash 1 0)) |
|||
(defvarl ExposureMask (ash 1 15)) |
|||
(typedef XEvent (union _XEvent |
|||
(type XEventType) |
|||
(pad (array 24 long)))) |
|||
(defvarl NULL cptr-null) |
|||
(with-dyn-lib "libX11.so" |
|||
(deffi XOpenDisplay "XOpenDisplay" Display (bstr)) |
|||
(deffi XCloseDisplay "XCloseDisplay" int (Display)) |
|||
(deffi XDefaultScreen "XDefaultScreen" int (Display)) |
|||
(deffi XRootWindow "XRootWindow" Window (Display int)) |
|||
(deffi XBlackPixel "XBlackPixel" ulong (Display int)) |
|||
(deffi XWhitePixel "XWhitePixel" ulong (Display int)) |
|||
(deffi XCreateSimpleWindow "XCreateSimpleWindow" Window (Display |
|||
Window |
|||
int int |
|||
uint uint uint |
|||
ulong ulong)) |
|||
(deffi XSelectInput "XSelectInput" int (Display Window long)) |
|||
(deffi XMapWindow "XMapWindow" int (Display Window)) |
|||
(deffi XNextEvent "XNextEvent" int (Display (ptr-out XEvent))) |
|||
(deffi XDefaultGC "XDefaultGC" GC (Display int)) |
|||
(deffi XFillRectangle "XFillRectangle" int (Display Drawable GC |
|||
int int uint uint)) |
|||
(deffi XDrawString "XDrawString" int (Display Drawable GC |
|||
int int bstr int))) |
|||
(let* ((msg "Hello, world!") |
|||
(d (XOpenDisplay nil))) |
|||
(when (equal d NULL) |
|||
(put-line "Cannot-open-display" *stderr*) |
|||
(exit 1)) |
|||
(let* ((s (XDefaultScreen d)) |
|||
(w (XCreateSimpleWindow d (XRootWindow d s) 10 10 100 100 1 |
|||
(XBlackPixel d s) (XWhitePixel d s)))) |
|||
(XSelectInput d w (logior ExposureMask KeyPressMask)) |
|||
(XMapWindow d w) |
|||
(while t |
|||
(let ((e (make-union (ffi XEvent)))) |
|||
(XNextEvent d e) |
|||
(caseq (union-get e 'type) |
|||
(Expose |
|||
(XFillRectangle d w (XDefaultGC d s) 20 20 10 10) |
|||
(XDrawString d w (XDefaultGC d s) 10 50 msg (length msg))) |
|||
(KeyPress (return))))) |
|||
(XCloseDisplay d)))</syntaxhighlight> |
|||
===GTK2=== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang="txrlisp">(typedef GtkObject* (cptr GtkObject)) |
|||
(typedef GtkWidget* (cptr GtkWidget)) |
|||
(typedef GtkWidget* (cptr GtkWidget)) |
|||
(typedef GtkWindowType (enum GtkWindowType |
|||
GTK_WINDOW_TOPLEVEL |
|||
GTK_WINDOW_POPUP)) |
|||
(with-dyn-lib "libgtk-x11-2.0.so.0" |
|||
(deffi gtk_init "gtk_init" void ((ptr int) (ptr (ptr (zarray str))))) |
|||
(deffi gtk_window_new "gtk_window_new" GtkWidget* (GtkWindowType)) |
|||
(deffi gtk_signal_connect_full "gtk_signal_connect_full" |
|||
ulong (GtkObject* str closure closure val closure int int)) |
|||
(deffi gtk_widget_show "gtk_widget_show" void (GtkWidget*)) |
|||
(deffi gtk_main "gtk_main" void ()) |
|||
(deffi-sym gtk_main_quit "gtk_main_quit")) |
|||
(defmacro GTK_OBJECT (cptr) |
|||
^(cptr-cast 'GtkObject ,cptr)) |
|||
(defmacro gtk_signal_connect (object name func func-data) |
|||
^(gtk_signal_connect_full ,object ,name ,func cptr-null |
|||
,func-data cptr-null 0 0)) |
|||
(gtk_init (length *args*) (vec-list *args*)) |
|||
(let ((window (gtk_window_new 'GTK_WINDOW_TOPLEVEL))) |
|||
(gtk_signal_connect (GTK_OBJECT window) "destroy" gtk_main_quit nil) |
|||
(gtk_widget_show window) |
|||
(gtk_main))</syntaxhighlight> |
|||
===Win32/Win64=== |
|||
This solution is based on the "Your First Windows Program" example in MSDN. It registers a Window class, creates a Window and runs a Windows message loop against a custom <code>WndProc</code> function that is written in Lisp, which handles <code>WM_QUIT</code> and <code>WM_PAINT</code> events exactly like its C counterpart. All necessary basic types, structures, constants and foreign functions are declared using the TXR FFI language. |
|||
Note that the <code>CW_USEDEFAULT</code> constant in the Windows header files is defined as <code>0x80000000</code>. This is out of range of the signed <code>int</code> arguments of <code>CreateWindowEx</code> with which it is used. Microsoft is relying on an implementation-defined C conversion to turn this value into the most negative <code>int</code>. When the original constant was used in the TXR translation, TXR's FFI '''uncovered this little problem''' by throwing an exception arising from the out-of-range conversion attempt. The fix is to specify the correct value directly as <code>#x-80000000</code>. |
|||
<syntaxhighlight lang="txrlisp">(typedef LRESULT int-ptr-t) |
|||
(typedef LPARAM int-ptr-t) |
|||
(typedef WPARAM uint-ptr-t) |
|||
(typedef UINT uint32) |
|||
(typedef LONG int32) |
|||
(typedef WORD uint16) |
|||
(typedef DWORD uint32) |
|||
(typedef LPVOID cptr) |
|||
(typedef BOOL (bool int32)) |
|||
(typedef BYTE uint8) |
|||
(typedef HWND (cptr HWND)) |
|||
(typedef HINSTANCE (cptr HINSTANCE)) |
|||
(typedef HICON (cptr HICON)) |
|||
(typedef HCURSOR (cptr HCURSOR)) |
|||
(typedef HBRUSH (cptr HBRUSH)) |
|||
(typedef HMENU (cptr HMENU)) |
|||
(typedef HDC (cptr HDC)) |
|||
(typedef ATOM WORD) |
|||
(typedef LPCTSTR wstr) |
|||
(defvarl NULL cptr-null) |
|||
(typedef WNDCLASS (struct WNDCLASS |
|||
(style UINT) |
|||
(lpfnWndProc closure) |
|||
(cbClsExtra int) |
|||
(cbWndExtra int) |
|||
(hInstance HINSTANCE) |
|||
(hIcon HICON) |
|||
(hCursor HCURSOR) |
|||
(hbrBackground HBRUSH) |
|||
(lpszMenuName LPCTSTR) |
|||
(lpszClassName LPCTSTR))) |
|||
(defmeth WNDCLASS :init (me) |
|||
(zero-fill (ffi WNDCLASS) me)) |
|||
(typedef POINT (struct POINT |
|||
(x LONG) |
|||
(y LONG))) |
|||
(typedef MSG (struct MSG |
|||
(hwnd HWND) |
|||
(message UINT) |
|||
(wParam WPARAM) |
|||
(lParam LPARAM) |
|||
(time DWORD) |
|||
(pt POINT))) |
|||
(typedef RECT (struct RECT |
|||
(left LONG) |
|||
(top LONG) |
|||
(right LONG) |
|||
(bottom LONG))) |
|||
(typedef PAINTSTRUCT (struct PAINTSTRUCT |
|||
(hdc HDC) |
|||
(fErase BOOL) |
|||
(rcPaint RECT) |
|||
(fRestore BOOL) |
|||
(fIncUpdate BOOL) |
|||
(rgbReserved (array 32 BYTE)))) |
|||
(defvarl CW_USEDEFAULT #x-80000000) |
|||
(defvarl WS_OVERLAPPEDWINDOW #x00cf0000) |
|||
(defvarl SW_SHOWDEFAULT 5) |
|||
(defvarl WM_DESTROY 2) |
|||
(defvarl WM_PAINT 15) |
|||
(defvarl COLOR_WINDOW 5) |
|||
(deffi-cb wndproc-fn LRESULT (HWND UINT LPARAM WPARAM)) |
|||
(with-dyn-lib "kernel32.dll" |
|||
(deffi GetModuleHandle "GetModuleHandleW" HINSTANCE (wstr))) |
|||
(with-dyn-lib "user32.dll" |
|||
(deffi RegisterClass "RegisterClassW" ATOM ((ptr-in WNDCLASS))) |
|||
(deffi CreateWindowEx "CreateWindowExW" HWND (DWORD |
|||
LPCTSTR LPCTSTR |
|||
DWORD |
|||
int int int int |
|||
HWND HMENU HINSTANCE |
|||
LPVOID)) |
|||
(deffi ShowWindow "ShowWindow" BOOL (HWND int)) |
|||
(deffi GetMessage "GetMessageW" BOOL ((ptr-out MSG) HWND UINT UINT)) |
|||
(deffi TranslateMessage "TranslateMessage" BOOL ((ptr-in MSG))) |
|||
(deffi DispatchMessage "DispatchMessageW" LRESULT ((ptr-in MSG))) |
|||
(deffi PostQuitMessage "PostQuitMessage" void (int)) |
|||
(deffi DefWindowProc "DefWindowProcW" LRESULT (HWND UINT LPARAM WPARAM)) |
|||
(deffi BeginPaint "BeginPaint" HDC (HWND (ptr-out PAINTSTRUCT))) |
|||
(deffi EndPaint "EndPaint" BOOL (HWND (ptr-in PAINTSTRUCT))) |
|||
(deffi FillRect "FillRect" int (HDC (ptr-in RECT) HBRUSH))) |
|||
(defun WindowProc (hwnd uMsg wParam lParam) |
|||
(caseql* uMsg |
|||
(WM_DESTROY |
|||
(PostQuitMessage 0) |
|||
0) |
|||
(WM_PAINT |
|||
(let* ((ps (new PAINTSTRUCT)) |
|||
(hdc (BeginPaint hwnd ps))) |
|||
(FillRect hdc ps.rcPaint (cptr-int (succ COLOR_WINDOW) 'HBRUSH)) |
|||
(EndPaint hwnd ps) |
|||
0)) |
|||
(t (DefWindowProc hwnd uMsg wParam lParam)))) |
|||
(let* ((hInstance (GetModuleHandle nil)) |
|||
(wc (new WNDCLASS |
|||
lpfnWndProc [wndproc-fn WindowProc] |
|||
hInstance hInstance |
|||
lpszClassName "Sample Window Class"))) |
|||
(RegisterClass wc) |
|||
(let ((hwnd (CreateWindowEx 0 wc.lpszClassName "Learn to Program Windows" |
|||
WS_OVERLAPPEDWINDOW |
|||
CW_USEDEFAULT CW_USEDEFAULT |
|||
CW_USEDEFAULT CW_USEDEFAULT |
|||
NULL NULL hInstance NULL))) |
|||
(unless (equal hwnd NULL) |
|||
(ShowWindow hwnd SW_SHOWDEFAULT) |
|||
(let ((msg (new MSG))) |
|||
(while (GetMessage msg NULL 0 0) |
|||
(TranslateMessage msg) |
|||
(DispatchMessage msg))))))</syntaxhighlight> |
|||
=={{header|VBA}}== |
|||
<pre>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
|||
Need to reference the following object library (From the Tools menu, choose References) |
|||
Microsoft Forms 2.0 Object Library |
|||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
|||
And : |
|||
Programmatic Access to Visual Basic Project must be trusted. See it in Macro's security!</pre> |
|||
<syntaxhighlight lang="vb"> |
|||
Option Explicit |
|||
Sub InsertForm() |
|||
Dim myForm As Object, strname As String |
|||
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3) |
|||
strname = myForm.Name |
|||
VBA.UserForms.Add(strname).Show |
|||
End Sub</syntaxhighlight> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Creates an empty window with ID 'A' near the upper left corner of document area, with height of 20 text lines and width of 80 characters. |
Creates an empty window with ID 'A' near the upper left corner of document area, with height of 20 text lines and width of 80 characters. |
||
< |
<syntaxhighlight lang="vedit">Win_Create(A, 2, 5, 20, 80)</syntaxhighlight> |
||
Note: if you run this command while in Visual Mode, you should adjust your active window smaller so that the created window will not be hidden behind it (since the active window is always on top). |
Note: if you run this command while in Visual Mode, you should adjust your active window smaller so that the created window will not be hidden behind it (since the active window is always on top). |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
< |
<syntaxhighlight lang="vb"> Dim newForm as new Form |
||
newForm.Text = "It's a new window" |
newForm.Text = "It's a new window" |
||
newForm.Show()</ |
newForm.Show()</syntaxhighlight> |
||
=={{header|V (Vlang)}}== |
|||
{{omit from|Batch File|No access to GUI functions.}} |
|||
<syntaxhighlight lang="Zig"> |
|||
{{omit from|PHP|PHP cannot create windows by itself}} |
|||
import gg |
|||
{{omit from|Unlambda|No access to GUI functions.}} |
|||
import gx |
|||
fn main() { |
|||
mut app := gg.new_context( |
|||
bg_color: gx.white |
|||
resizable: true |
|||
create_window: true |
|||
width: 600 |
|||
height: 600 |
|||
frame_fn: frame |
|||
window_title: "Empty Window" |
|||
) |
|||
app.run() |
|||
} |
|||
fn frame(mut ctx gg.Context) { |
|||
ctx.begin() |
|||
ctx.end() |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Wren}}== |
|||
{{libheader|DOME}} |
|||
<syntaxhighlight lang="wren">import "dome" for Window |
|||
class EmptyWindow { |
|||
construct new(width, height) { |
|||
Window.title = "Empty window" |
|||
Window.resize(width, height) |
|||
} |
|||
init() {} |
|||
update() {} |
|||
draw(alpha) {} |
|||
} |
|||
var Game = EmptyWindow.new(600, 600)</syntaxhighlight> |
|||
=={{header|X86 Assembly}}== |
=={{header|X86 Assembly}}== |
||
{{libheader|GTK}}<br> |
{{libheader|GTK}}<br> |
||
{{works with|NASM}} |
{{works with|NASM}} |
||
< |
<syntaxhighlight lang="asm"> |
||
;GTK imports and defines etc. |
;GTK imports and defines etc. |
||
%define GTK_WINDOW_TOPLEVEL 0 |
%define GTK_WINDOW_TOPLEVEL 0 |
||
Line 2,059: | Line 2,669: | ||
exit_sig_msg db "-> Rage quitting..",10,0 |
exit_sig_msg db "-> Rage quitting..",10,0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
<br> |
<br> |
||
{{works with|MASM}} |
{{works with|MASM}} |
||
< |
<syntaxhighlight lang="asm"> |
||
.586 |
.586 |
||
.model flat, stdcall |
.model flat, stdcall |
||
Line 2,138: | Line 2,748: | ||
WndProc endp |
WndProc endp |
||
end start |
end start |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Yabasic}}== |
|||
<syntaxhighlight lang="yabasic">open window 400,200 //minimum line required to accomplish the indicated task |
|||
clear screen |
|||
text 200,100,"I am a window - close me!","cc" |
|||
end</syntaxhighlight> |
|||
{{omit from|Batch File|No access to GUI functions.}} |
|||
{{omit from|EasyLang}} |
|||
{{omit from|Logtalk}} |
{{omit from|Logtalk}} |
||
{{omit from|Maxima}} |
{{omit from|Maxima}} |
||
{{omit from|PARI/GP}} |
{{omit from|PARI/GP}} |
||
{{omit from|PHP|PHP cannot create windows by itself}} |
|||
{{omit from|PostScript}} |
{{omit from|PostScript}} |
||
{{omit from|Retro}} |
{{omit from|Retro}} |
||
{{omit from|SQL PL|It does not handle GUI}} |
|||
{{omit from|TI-83 BASIC|Doesn't run in a windowed interface}} |
|||
{{omit from|Unlambda|No access to GUI functions.}} |
Revision as of 23:30, 9 March 2024
You are encouraged to solve this task according to the task description, using any language you may know.
Display a GUI window. The window need not have any contents, but should respond to requests to be closed.
Ada
with Gtk.Window; use Gtk.Window;
with Gtk.Widget; use Gtk.Widget;
with Gtk.Handlers;
with Gtk.Main;
procedure Windowed_Application is
Window : Gtk_Window;
package Handlers is new Gtk.Handlers.Callback (Gtk_Widget_Record);
package Return_Handlers is
new Gtk.Handlers.Return_Callback (Gtk_Widget_Record, Boolean);
function Delete_Event (Widget : access Gtk_Widget_Record'Class)
return Boolean is
begin
return False;
end Delete_Event;
procedure Destroy (Widget : access Gtk_Widget_Record'Class) is
begin
Gtk.Main.Main_Quit;
end Destroy;
begin
Gtk.Main.Init;
Gtk.Window.Gtk_New (Window);
Return_Handlers.Connect
( Window,
"delete_event",
Return_Handlers.To_Marshaller (Delete_Event'Access)
);
Handlers.Connect
( Window,
"destroy",
Handlers.To_Marshaller (Destroy'Access)
);
Show (Window);
Gtk.Main.Main;
end Windowed_Application;
ALGOL 68
Compile command: ca -l gtk-3 -l gdk-3 -l atk-1.0 -l gio-2.0 -l pangocairo-1.0 -l gdk_pixbuf-2.0 -l cairo-gobject -l pango-1.0 -l cairo -l gobject-2.0 -l glib-2.0 firstgtk.a68
PROGRAM firstgtk CONTEXT VOID
USE standard
BEGIN
MODE GDATA = REF BITS;
MODE GUINT = BITS;
MODE GSIZE = BITS;
MODE GTYPE = GSIZE;
MODE GTYPECLASS = STRUCT(GTYPE g type);
MODE GTYPEINSTANCE = STRUCT(REF GTYPECLASS g class);
MODE GTKWIDGETPRIVATE = REF BITS;
MODE GOBJECT = STRUCT(
GTYPEINSTANCE g type instance,
GUINT ref count,
REF GDATA qdata);
MODE GTKWIDGET = STRUCT(
GOBJECT parent instance,
REF GTKWIDGETPRIVATE priv);
PROC(REF INT,REF CCHARPTRPTR)VOID gtk init = ALIEN "GTK_INIT"
"#define GTK_INIT(argc,argv) gtk_init(argc,argv)";
PROC(INT)REF GTKWIDGET gtk window new = ALIEN "GTK_WINDOW_NEW"
"#define GTK_WINDOW_NEW(type) (void *)gtk_window_new(type)";
PROC(REF GTKWIDGET)VOID gtk widget show = ALIEN "GTK_WIDGET_SHOW"
"#define GTK_WIDGET_SHOW(widget) gtk_widget_show((void *)widget)";
PROC gtk main = VOID: CODE "gtk_main();";
INT gtk window toplevel = 0;
FILE argf;
REF GTKWIDGET window;
open(argf,"",arg channel);
gtk init(argc,argv);
window:=gtk window new(gtk window toplevel);
gtk widget show(window);
gtk main
END
FINISH
AmigaBASIC
WINDOW 2,"New Window"
AurelBasic
WIN 0 0 400 300 "New Window"
AutoHotkey
Gui, Add, Text,, Hello
Gui, Show
AutoIt
GUICreate("Test")
GUISetState(@SW_SHOW)
Do
Switch GUIGetMsg()
Case -3 ; $GUI_EVENT_CLOSE
Exit
EndSwitch
Until False
BaCon
BaCon includes a Highlevel Universal GUI abstraction layer (HUG). The implementation is based on GTK.
REM empty window
INCLUDE "hug.bac"
mainwin = WINDOW("Rosetta Code empty", 400, 300)
REM start gtk event loop...
DISPLAY
BASIC256
BASIC256 it has a built-in graphics mode.
clg
text (50,50, "I write in the graphics area")
BBC BASIC
INSTALL @lib$+"WINLIB2"
dlg% = FN_newdialog("GUI Window", 0, 0, 200, 150, 8, 1000)
PROC_showdialog(dlg%)
C
SDL
Compile Command: gcc `sdl-config --cflags` `sdl-config --libs` SDL_Window.c -o window
/*
* Opens an 800x600 16bit color window.
* Done here with ANSI C.
*/
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
int main()
{
SDL_Surface *screen;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode( 800, 600, 16, SDL_SWSURFACE | SDL_HWPALETTE );
return 0;
}
GTK
Compile command: gcc `gtk-config --cflags` `gtk-config --libs` -o window window.c
#include <gtk/gtk.h>
int
main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_signal_connect(GTK_OBJECT(window), "destroy",
GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
gtk_widget_show(window);
gtk_main();
return 0;
}
GTK2
Compile command: gcc -Wall -pedantic `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o window window.c
#include <gtk/gtk.h>
int
main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show(window);
gtk_main();
return 0;
}
GLUT
Compile command: gcc -I /usr/include/ -lglut -o window window_glut.c
Note that we aren't registering a painting or drawing callback, so the window will be created with nothing drawn in it. This is almost certain to lead to a strange appearance; On many systems, dragging the window around will appear to drag a copy of what was underneath where the window was when it was originally created.
We are registering a keypress callback, which isn't strictly necessary; It simply allows us to use a keypress to close the program rather than depending on the windowing system the program is run under.
// A C+GLUT implementation of the Creating a Window task at Rosetta Code
// http://rosettacode.org/wiki/Creating_a_Window
#include <stdlib.h>
#include <GL/glut.h>
// This function is not strictly necessary to meet the requirements of the task.
void onKeyPress(unsigned char key, int x, int y)
{
// If you have any cleanup or such, you need to use C's
// onexit routine for registering cleanup callbacks.
exit(0);
}
int main(int argc, char **argv)
{
// Pulls out any command-line arguments that are specific to GLUT,
// And leaves a command-line argument set without any of those arguments
// when it returns.
// (If you want a copy, take a copy first.)
glutInit(&argc, argv);
// Tell GLUT we want to create a window.
// It won't *actually* be created until we call glutMainLoop below.
glutCreateWindow("Goodbye, World!");
// Register a callback to handle key press events (so we can quit on
// when someone hits a key) This part is not necessary to meet the
// requirements of the task.
glutKeyboardFunc(&onKeyPress);
// Put the execution of the app in glut's hands. Most GUI environments
// involve a message loop that communicate window events. GLUT handles
// most of these with defaults, except for any we register callbacks
// for. (Like the onKeyPress above.)
glutMainLoop();
return 0;
}
C#
using System;
using System.Windows.Forms;
public class Window {
[STAThread]
static void Main() {
Form form = new Form();
form.Text = "Window";
form.Disposed += delegate { Application.Exit(); };
form.Show();
Application.Run();
}
}
C++
Compiler command: qmake -pro; qmake
#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
window.show();
return app.exec();
}
Compiler command: g++ filename.cc -o test `pkg-config --cflags --libs gtkmm-2.4`
#include <iostream>
#include <gtkmm.h>
int
main( int argc, char* argv[] )
{
try
{
Gtk::Main m( argc, argv ) ;
Gtk::Window win ;
m.run( win ) ;
}
catch( std::exception const & exc )
{
std::cout << exc.what() << std::endl ;
exit( -1 ) ;
}
exit( 0 ) ;
}
Clojure
(import '(javax.swing JFrame))
(let [frame (JFrame. "A Window")]
(doto frame
(.setSize 600 800)
(.setVisible true)))
Common Lisp
(capi:display (make-instance 'capi:interface :title "A Window"))
Setting up the environment:
(require :mcclim)
(cl:defpackage #:rc-window
(:use #:clim-lisp #:clim))
(cl:in-package #:rc-window)
The actual definition and display:
(define-application-frame rc-window ()
()
(:layouts (:default)))
(run-frame-top-level (make-application-frame 'rc-window))
Note: This creates a small, useless window ("frame"). Useful frames will have some panes defined inside them.
Works with the Armed Bear Common Lisp implementation that targets the JVM.
(defun create-window ()
"Creates a window"
(let ((window (jnew (jconstructor "javax.swing.JFrame"))))
(jcall (jmethod "javax.swing.JFrame" "setVisible" "boolean")
window (make-immediate-object t :boolean))))
(create-window)
D
module Window;
import fltk4d.all;
void main() {
auto window = new Window(300, 300, "A window");
window.show;
FLTK.run;
}
import derelict.sdl.sdl;
int main(char[][] args)
{
DerelictSDL.load();
SDL_Event event;
auto done = false;
SDL_Init(SDL_INIT_VIDEO);
scope(exit) SDL_Quit();
SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL);
SDL_WM_SetCaption("My first Window", "SDL test");
while (!done)
{
if (SDL_PollEvent(&event) == 1)
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
default:
break;
}
}
}
return 0;
}
QD is a simple and easy-to-use wrapper around SDL.
import qd;
void main() {
screen(640, 480);
while (true) events();
}
Delphi
This first example is a minimalist approach using Delphi's standard Window (form) creation procedure. In Delphi 7, this will create a single Window executable of 362KB.
// The project file (Project1.dpr)
program Project1;
uses
Forms,
// Include file with Window class declaration (see below)
Unit0 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
// The Window class declaration
unit Unit1;
interface
uses
Forms;
type
TForm1 = class(TForm)
end;
var
Form1: TForm1;
implementation
{$R *.dfm} // The window definition resource (see below)
end.
// A textual rendition of the Window (form) definition file (Unit1.dfm)
object Form1: TForm1
Left = 469
Top = 142
Width = 800
Height = 600
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Shell Dlg 2'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 13
end
This second example demonstrates a 'pure' Windows API approach (i.e. NOT using the Delphi Visual Component Library). In Delphi 7, this will create a single Window executable of 15KB.
program Project3;
uses
Windows,
Messages;
var
WndClass: TWndClass;
Msg: TMsg;
winT, winL: Integer;
// Initial height/width of the window
const
winW: Integer = 800;
winH: Integer = 600;
// Callback function to processes messages sent to the window
function WindowProc(hWnd,Msg,wParam,lParam:Integer): Integer; stdcall;
begin
// Trap the WM_DESTROY message
if (Msg = WM_DESTROY) then PostQuitMessage(0);
Result := DefWindowProc(hWnd,Msg,wParam,lParam);
end;
begin
// Fill the WndClass structure with the window class attributes
// to be registered by the RegisterClass function
with WndClass do
begin
lpszClassName:= 'Form1';
lpfnWndProc := @WindowProc; // Pointer to our message handling callback
style := CS_OWNDC or // Request a unique device context
CS_VREDRAW or // Redraw window when resized vertically
CS_HREDRAW; // Redraw window when resized horizontally
hInstance := hInstance; // The instance that the window procedure of this class is within
hbrBackground := HBRUSH(COLOR_BTNFACE+1); // Background colour of the window
end;
// Register the window class for use by CreateWindow
RegisterClass(WndClass);
// Calculate initial top and left positions of the window
winT := (GetSystemMetrics(SM_CYFULLSCREEN) - winH) div 2;
winL := (GetSystemMetrics(SM_CXFULLSCREEN) - winW) div 2;
// Create the window
CreateWindow(WndClass.lpszClassName, // Class name
'Form1', // Window name
WS_OVERLAPPEDWINDOW or WS_VISIBLE, // Window style
winL, // Horizontal Position (Left)
winT, // Vertical Position (Top)
winW, // Width
winH, // Height
0, // Window parent/owner handle
0, // Menu handle
hInstance, // Handle to application instance
nil); // Pointer to window creation data
// Handle messages
while GetMessage(Msg,0,0,0) do
DispatchMessage(Msg);
end.
Dragon
select "GUI"
window = newWindow("Window")
window.setSize(400,600)
window.setVisible()
E
Swing
when (currentVat.morphInto("awt")) -> { def w := <swing:makeJFrame>("Window") w.setContentPane(<swing:makeJLabel>("Contents")) w.pack() w.show() }
Eiffel
Platform independent EiffelVision 2 Library
class
APPLICATION
inherit
EV_APPLICATION
create
make_and_launch
feature {NONE} -- Initialization
make_and_launch
-- Initialize and launch application
do
default_create
create first_window
first_window.show
launch
end
feature {NONE} -- Implementation
first_window: MAIN_WINDOW
-- Main window.
end
class
MAIN_WINDOW
inherit
EV_TITLED_WINDOW
redefine
initialize
end
create
default_create
feature {NONE} -- Initialization
initialize
-- Build the interface for this window.
do
-- Call initialize in parent class EV_TITLED_WINDOW
Precursor {EV_TITLED_WINDOW}
-- Build a container for widgets for this window
build_main_container
-- Add the container to this window
extend (main_container)
-- Add `request_close_window' to the actions taken when the user clicks
-- on the cross in the title bar.
close_request_actions.extend (agent request_close_window)
-- Set the title of the window
set_title ("Rosetta Code")
-- Set the initial size of the window
set_size (400, 400)
end
feature {NONE} -- Implementation, Close event
request_close_window
-- The user wants to close the window
do
-- Destroy this window
destroy;
-- Destroy application
(create {EV_ENVIRONMENT}).application.destroy
end
feature {NONE} -- Implementation
main_container: EV_VERTICAL_BOX
-- Main container contains all widgets displayed in this window.
-- In this case a single text area.
build_main_container
-- Create and populate `main_container'.
require
main_container_not_yet_created: main_container = Void
do
create main_container
main_container.extend (create {EV_TEXT})
ensure
main_container_created: main_container /= Void
end
end
class
APPLICATION
inherit
WINFORMS_FORM
rename
make as make_form
end
create
make
feature {NONE} -- Initialization
make
-- Run application.
do
-- Set window title
set_text ("Rosetta Code")
-- Launch application
{WINFORMS_APPLICATION}.run_form (Current)
end
end
Emacs Lisp
(make-frame)
Euphoria
ARWEN
include arwen.ew
constant win = create(Window, "ARWEN window", 0, 0,100,100,640,480,{0,0})
WinMain(win, SW_NORMAL)
EuGTK
include GtkEngine.e
constant win = create(GtkWindow,"title=EuGTK Window;size=640x480;border=10;$destroy=Quit")
show_all(win)
main()
EuWinGUI
include EuWinGUI.ew
Window("EuWinGUI window",100,100,640,480)
-- Event loop
while True do
WaitEvent()
end while
CloseApp(0)
Win32Lib
include Win32Lib.ew
constant win = createEx( Window, "Win32Lib", 0, Default, Default, 640, 480, 0, 0 )
WinMain( win, Normal )
wxEuphoria
include wxeu/wxeud.e
constant win = create( wxFrame, {0, -1, "wxEuphoria window", -1, -1, 640, 480} )
wxMain( win )
F#
Everything is provided by the .NET runtime so this is almost identical to C_sharp.
open System.Windows.Forms
[<System.STAThread>]
do
Form(Text = "F# Window")
|> Application.Run
Factor
USING: ui ui.gadgets.labels ;
"This is a window..." <label> "Really?" open-window
Fantom
using fwt
class Main
{
public static Void main ()
{
Window().open
}
}
Forth
gtk-server command: gtk-server -fifo=ffl-fifo &
include ffl/gsv.fs
\ Open the connection to the gtk-server and load the Gtk2 definitions
s" gtk-server.cfg" s" ffl-fifo" gsv+open 0= [IF]
\ Convert the string event to a widget id
: event>widget
0. 2swap >number 2drop d>s
;
0 value window
: window-creation
gtk_init
\ Create the window
GTK_WINDOW_TOPLEVEL gtk_window_new to window
window gtk_widget_show
\ Wait for an event
BEGIN
s" WAIT" gtk_server_callback
event>widget window =
UNTIL
0 gtk_exit
;
window-creation
gsv+close drop
[THEN]
iMops
Window+ w \ create a window
View v \ create a view
300 30 430 230 put: frameRect \ size a rectangle for the view
frameRect " Test" docWindow v new: w \ activate the view and window
show: w \ display the window
FreeBASIC
#Include "windows.bi"
Dim As HWND Window_Main
Dim As MSG msg
'Create the window:
Window_Main = CreateWindow("#32770", "I am a window - close me!", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 350, 200, 0, 0, 0, 0)
'Windows message loop:
While GetMessage(@msg, Window_Main, 0, 0)
TranslateMessage(@msg)
DispatchMessage(@msg)
If msg.hwnd = Window_Main And msg.message = WM_COMMAND Then End
Wend
End
Frink
g=(new graphics).show[]
FutureBasic
window 1
Gambas
Public Sub Form_Open()
End
Go
GTK
package main
import (
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
)
func main() {
gtk.Init(nil)
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.Connect("destroy",
func(*glib.CallbackContext) { gtk.MainQuit() }, "")
window.Show()
gtk.Main()
}
SDL
package main
import (
"log"
"github.com/veandco/go-sdl2/sdl"
)
func main() {
window, err := sdl.CreateWindow("RC Window Creation",
sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
320, 200, 0)
if err != nil {
log.Fatal(err)
}
for {
if _, ok := sdl.WaitEvent().(*sdl.QuitEvent); ok {
break
}
}
window.Destroy()
}
X11
package main
import (
"code.google.com/p/x-go-binding/ui"
"code.google.com/p/x-go-binding/ui/x11"
"log"
)
func main() {
win, err := x11.NewWindow()
if err != nil {
log.Fatalf("Error: %v\n", err)
}
defer win.Close()
for ev := range win.EventChan() {
switch e := ev.(type) {
case ui.ErrEvent:
log.Fatalf("Error: %v\n", e.Err)
}
}
}
Groovy
import groovy.swing.SwingBuilder new SwingBuilder().frame(title:'My Window', size:[200,100]).show()
GUISS
We will open notepad as a window here.
Start,Programs,Accessories,Notepad
To close the window:
Window:Notepad,Button:Close
Haskell
Using
from HackageDB.
A simple graphics library, designed to give the programmer access to most interesting parts of the Win32 Graphics Device Interface and X11 library without exposing the programmer to the pain and anguish usually associated with using these interfaces.
import Graphics.HGL
aWindow = runGraphics $
withWindow_ "Rosetta Code task: Creating a window" (300, 200) $ \ w -> do
drawInWindow w $ text (100, 100) "Hello World"
getKey w
HicEst
WINDOW(WINdowhandle=handle, Width=80, Height=-400, X=1, Y=1/2, TItle="Rosetta Window_creation Example")
! window units: as pixels < 0, as relative window size 0...1, ascurrent character sizes > 1
WRITE(WINdowhandle=handle) '... some output ...'
IDL
With some example values filled in:
Id = WIDGET_BASE(TITLE='Window Title',xsize=200,ysize=100) WIDGET_CONTROL, /REALIZE, id
Icon and Unicon
Icon and Unicon windowing is portable between Windows and X-Windows environments.
Icon
Unicon
The Icon solution works in Unicon. An Unicon-only version is as follows:
import gui
$include "guih.icn"
class WindowApp : Dialog ()
# -- automatically called when the dialog is created
method component_setup ()
# make sure we respond to close event
connect(self, "dispose", CLOSE_BUTTON_EVENT)
end
end
# create and show the window
procedure main ()
w := WindowApp ()
w.show_modal ()
end
J
A minimalist modal dialog:
wdinfo 'Hamlet';'To be, or not to be: that is the question:'
A free-standing window:
MINWDW=: noun define
pc minwdw;
pas 162 85;pcenter;
)
minwdw_run=: monad define
wd MINWDW
wd 'pshow;'
)
minwdw_close=: monad define
wd'pclose'
)
minwdw_run ''
Java
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) throws Exception {
JFrame w = new JFrame("Title");
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setSize(800,600);
w.setVisible(true);
}
}
JavaScript
window.open("webpage.html", "windowname", "width=800,height=600");
Julia
# v0.6
using Tk
w = Toplevel("Example")
Kotlin
import javax.swing.JFrame
fun main(args : Array<String>) {
JFrame("Title").apply {
setSize(800, 600)
defaultCloseOperation = JFrame.EXIT_ON_CLOSE
isVisible = true
}
}
Liberty BASIC
Minimum code required to fulfill the task.
nomainwin
open "GUI Window" for window as #1
wait
As it would properly be used in a real program.
nomainwin
open "GUI Window" for window as #1
#1 "trapclose Quit"
wait
sub Quit hndl$
close #hndl$
end
end sub
Lingo
win = window().new("New Window")
w = 320
h = 240
firstScreen = _system.deskTopRectList[1]
x = firstScreen.width/2 - w/2
y = firstScreen.height/2- h/2
win.rect = rect(x,y,x+w,y+h)
-- Director needs a binary movie file (*.dir) for opening new windows. But this
-- movie file can be totally empty, and if it's write protected in the filesystem,
-- it can be re-used for multiple windows.
win.filename = _movie.path & "empty.dir"
win.open()
Lua
local iup = require "iuplua"
iup.dialog{
title = "Window";
iup.vbox{
margin = "10x10";
iup.label{title = "A window"}
}
}:show()
iup.MainLoop()
M2000 Interpreter
Window has title by default the name of variable, here MyForm Window by default has 6000 twips width by 4000 twips height
Here we open MyForm as modal window (with 1 as first parameter, after "Show")
Window show a square in left side of title (we can change this to be at right), so this close the window (if we have proper event function, we can quit the closing). Also using key combination Alt-F4 close window too (also send event).
Window open in the monitor where we see the mouse pointer.
M2000 Windows have own decoration, independent from platform (same to Xp, Windows 7, Windows 8, Windows 10 and Linux using Wine).
Module DisplayWindow {
Declare MyForm Form
Method MyForm,"Show",1
}
DisplayWindow
Mathematica/Wolfram Language
CreateDocument[]
mIRC Scripting Language
Switches: C = Center Window p = Picture Window d = Desktop Window
alias CreateMyWindow { .window -Cp +d @WindowName 600 480 }
Nanoquery
This program creates and displays a 300x300 window with no contents and the title Nanoquery.
w = new(Nanoquery.Util.Windows.Window).setSize(300,300).setTitle("Nanoquery").show()
NetRexx
/* NetRexx */
options replace format comments java crossref symbols binary
import javax.swing.JFrame
import javax.swing.JLabel
import java.awt.BorderLayout
import java.awt.Font
import java.awt.Color
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parse arg showText .
select
when showText.length = 0 then addText = isTrue
when 'YES'.abbrev(showText.upper) then addText = isTrue
when showText = '.' then addText = isTrue
otherwise addText = isFalse
end
title = 'Rosetta Code - Window Creation'
createFrame(title, addText)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method createFrame(title, addText = boolean 0) public static
do
fenester = JFrame(title)
fenester.setSize(600, 200)
fenester.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
if addText then decorate(fenester)
fenester.setVisible(isTrue)
catch ex = Exception
ex.printStackTrace()
end
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method decorate(fenester = JFrame, textStr = 'This page intentionally left blank.') private static returns JFrame
textlbl = JLabel(textStr)
textfont = Font(Font.SERIF, Font.BOLD, 20)
textlbl.setHorizontalAlignment(JLabel.CENTER)
textlbl.setFont(textfont)
textlbl.setForeground(Color.ORANGE)
fenester.add(textlbl, BorderLayout.CENTER)
return fenester
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method isTrue() public static returns boolean
return (1 == 1)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method isFalse() public static returns boolean
return \(1 == 1)
Nim
gtk3 / gintro
import gintro/[glib, gobject, gtk, gio]
proc activate(app: Application) =
## Activate the application.
let window = newApplicationWindow(app)
window.setTitle("Window for Rosetta")
window.setSizeRequest(640, 480)
window.showAll()
let app = newApplication(Application, "Rosetta.Window")
discard app.connect("activate", activate)
discard app.run()
gtk2
This is example 9 from the Araq/Nim github repository (modified to include a quit button)
import gdk2, glib2, gtk2
const
Inside = "Mouse is over label"
OutSide = "Mouse is not over label"
# Context transmitted to callback.
type Context = object
label: PLabel
overButton: bool
proc changeLabel(p: PWidget; event: gdk2.PEventCrossing; context: var Context) {.cdecl.} =
context.label.set_text(if context.overButton: OutSide else: Inside)
context.overButton = not context.overButton
proc thisDestroy(widget: PWidget, data: Pgpointer) {.cdecl.} =
main_quit()
var context: Context
nim_init()
let window = window_new(gtk2.WINDOW_TOPLEVEL)
let stackbox = vbox_new(true, 10)
let button1 = button_new("Move mouse over button")
let buttonstyle = copy(button1.get_style())
buttonstyle.bg[STATE_PRELIGHT] = TColor(pixel: 0, red: 255, green: 0, blue: 0)
button1.set_style(buttonstyle)
let button2 = button_new()
context = Context(label: label_new(Outside), overButton: false)
let button3 = button_new("Quit")
button2.add(context.label)
stackbox.pack_start(button1, true, true, 0)
stackbox.pack_start(button2, true, true, 0)
stackbox.pack_start(button3, true, true, 0)
window.set_border_width(5)
window.add(stackbox)
discard window.signal_connect("destroy", SIGNAL_FUNC(thisDestroy), nil)
discard button1.signal_connect("enter_notify_event", SIGNAL_FUNC(changeLabel), addr(context))
discard button1.signal_connect("leave_notify_event", SIGNAL_FUNC(changeLabel), addr(context))
discard button3.signal_connect("clicked", SIGNAL_FUNC(thisDestroy), nil)
window.show_all()
main()
SDL
import
sdl, sdl_image, colors
var
screen, greeting: PSurface
r: TRect
event: TEvent
bgColor = colChocolate.int32
if init(INIT_VIDEO) != 0:
quit "SDL failed to initialize!"
screen = SetVideoMode(640, 480, 16, SWSURFACE or ANYFORMAT)
if screen.isNil:
quit($sdl.getError())
greeting = IMG_load("tux.png")
if greeting.isNil:
echo "Failed to load tux.png"
else:
## convert the image to alpha and free the old one
var s = greeting.displayFormatAlpha()
swap(greeting, s)
s.freeSurface()
r.x = 0
r.y = 0
block game_loop:
while true:
while pollEvent(addr event) > 0:
case event.kind
of QUITEV:
break game_loop
of KEYDOWN:
if EvKeyboard(addr event).keysym.sym == K_ESCAPE:
break game_loop
else:
discard
discard fillRect(screen, nil, bgColor)
discard blitSurface(greeting, nil, screen, addr r)
discard flip(screen)
greeting.freeSurface()
screen.freeSurface()
sdl.Quit()
X11
import x11/[xlib, xutil, x]
const
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 300
type WindowData = tuple[display: PDisplay; window: Window]
proc createWindow: WindowData =
let width: cuint = WINDOW_WIDTH
let height: cuint = WINDOW_HEIGHT
var sizeHints: XSizeHints
let display = XOpenDisplay(nil)
if display == nil:
echo "Connection to X server failed."
quit QuitFailure
let screen = XDefaultScreen(display)
var rootwin = XRootWindow(display, screen)
let win = XCreateSimpleWindow(display, rootwin, 100, 10, width, height, 5,
XBlackPixel(display, screen), XWhitePixel(display, screen))
sizeHints.flags = PSize or PMinSize or PMaxSize
sizeHints.min_width = width.cint
sizeHints.max_width = width.cint
sizeHints.min_height = height.cint
sizeHints.max_height = height.cint
discard XSetStandardProperties(
display, win, "Simple Window", "window", 0, nil, 0, addr(sizeHints))
discard XSelectInput(display, win, ButtonPressMask or KeyPressMask or PointerMotionMask)
discard XMapWindow(display, win)
result = (display, win)
proc closeWindow(data: WindowData) =
discard XDestroyWindow(data.display, data.window)
discard XCloseDisplay(data.display)
proc processEvent(xev: var XEvent) =
var key: KeySym
case xev.theType.int
of KeyPress:
key = XLookupKeysym(cast[ptr XKeyEvent](addr(xev)), 0)
if key.int != 0:
echo "keyboard event ", key.int
if key.int == 65307: # <Esc>
quit QuitSuccess
of ButtonPressMask, PointerMotionMask:
echo "Mouse event"
else:
discard
proc eventloop(data: WindowData) =
var xev: XEvent
discard XFlush(data.display)
var numEvents = XPending(data.display).int
while numEvents != 0:
dec numEvents
discard XNextEvent(data.display, addr(xev))
processEvent(xev)
let windata = createWindow()
while true:
eventloop(windata)
windata.closeWindow()
glut
import glut
var win: int = 0
proc myOnKeyPress(c: int8, v1, v2: cint) {.cdecl.} =
echo(c)
if c == 27:
glutDestroyWindow(win)
glutInit()
win = glutCreateWindow("Goodbye, World!")
glutKeyboardFunc(TGlut1Char2IntCallback(myOnKeyPress))
glutMainLoop()
win
# test a Windows GUI application
import
windows, shellapi, nb30, mmsystem, shfolder
#proc MessageBox(hWnd: int, lpText, lpCaption: CString, uType: uint): int
# {stdcall, import: "MessageBox", header: "<windows.h>"}
discard MessageBox(0, "Hello World!", "Nim GUI Application", 0)
IUP
import iup
# assumes you have the iup .dll or .so installed
discard iup.open(nil,nil)
# now use a Dialog box to show a message
var lbl = label("Hello World")
setAttribute(lbl,"PADDING","10x10")
var contents = hbox(lbl, nil)
#SetAttribute(contents, "MARGIN", "5x5")
var dlg = dialog(contents)
#SetAttribute(dlg, "SIZE", "100x50")
discard dlg.show()
# a window via a quick message box, sitting on top of the main dialog window
discard Alarm("MyTitle","Hello World","Ok", "Not Ok", nil)
discard mainloop()
iup.close()
wxWidgets
This example works on MacOS but should be cross platform and native
import wx
{.experimental.}
const
TITLE = "Rosetta Code - Window Creation Nim"
WIDTH = 300
HEIGHT = 300
let
POSITION = construct_wxPoint(100,100)
SIZE = construct_wxSize(WIDTH,HEIGHT)
let window = cnew construct_wxFrame(nil, wxID_ANY, TITLE, POSITION, SIZE)
window.show(true)
run_main_loop()
Objeck
use Gtk2;
bundle Default {
class GtkHello {
function : Main(args : String[]) ~ Nil {
window := GtkWindow->New();
delete_callback := Events->DeleteEvent(GtkWidget) ~ Nil;
window->SignalConnect(Signal->Destroy, window->As(GtkWidget), delete_callback);
window->SetTitle("Title");
window->Show();
Appliction->Main();
}
}
}
Objective-C
It opens a 800×600 window, centered on the screen, with title "A Window".
#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
@interface Win : NSWindow
{
}
- (void)applicationDidFinishLaunching: (NSNotification *)notification;
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification;
@end
@implementation Win : NSWindow
-(instancetype) init
{
if ((self = [super
initWithContentRect: NSMakeRect(0, 0, 800, 600)
styleMask: (NSTitledWindowMask | NSClosableWindowMask)
backing: NSBackingStoreBuffered
defer: NO])) {
[self setTitle: @"A Window"];
[self center];
}
return self;
}
- (void)applicationDidFinishLaunching: (NSNotification *)notification
{
[self orderFront: self];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification
{
return YES;
}
@end
int main()
{
@autoreleasepool {
[NSApplication sharedApplication];
Win *mywin = [[Win alloc] init];
[NSApp setDelegate: mywin];
[NSApp runModalForWindow: mywin];
}
return EXIT_SUCCESS;
}
OCaml
let () =
let top = Tk.openTk() in
Wm.title_set top "An Empty Window";
Wm.geometry_set top "240x180";
Tk.mainLoop ();
;;
execute with:
ocaml -I +labltk labltk.cma sample.ml
with the Graphics module:
open Graphics
let () =
open_graph " 800x600";
let _ = read_line() in
close_graph ()
execute with:
ocaml graphics.cma tmp.ml
open GMain
let window = GWindow.window ~border_width:2 ()
let button = GButton.button ~label:"Hello World" ~packing:window#add ()
let () =
window#event#connect#delete ~callback:(fun _ -> true);
window#connect#destroy ~callback:Main.quit;
button#connect#clicked ~callback:window#destroy;
window#show ();
Main.main ()
execute with:
ocaml -I +lablgtk2 lablgtk.cma gtkInit.cmo sample.ml
let () =
Sdl.init [`VIDEO];
let _ = Sdlvideo.set_video_mode 200 200 [] in
Sdltimer.delay 2000;
Sdl.quit ()
execute with:
ocaml bigarray.cma -I +sdl sdl.cma sample.ml
open Sdl
let () =
let width, height = (640, 480) in
Sdl.init [`VIDEO];
let window, renderer =
Render.create_window_and_renderer
~width ~height ~flags:[]
in
let rgb = (0, 255, 0) in
let a = 255 in
Render.set_draw_color renderer rgb a;
Render.clear renderer;
Render.render_present renderer;
Timer.delay 3000;
Sdl.quit ()
execute with:
ocaml -I +sdl2 sdl2.cma sample.ml
let () =
let app = SFRenderWindow.make (640, 480) "OCaml-SFML Windowing" in
let rec loop () =
let continue =
match SFRenderWindow.pollEvent app with
| Some SFEvent.Closed -> false
| _ -> true
in
SFRenderWindow.clear app SFColor.black;
SFRenderWindow.display app;
if continue then loop ()
in
loop ()
execute with:
ocaml -I /tmp/ocaml-sfml/src sfml_system.cma sfml_window.cma sfml_graphics.cma win.ml
open Xlib
let () =
let d = xOpenDisplay "" in
let s = xDefaultScreen d in
let w = xCreateSimpleWindow d (xRootWindow d s) 10 10 100 100 1
(xBlackPixel d s) (xWhitePixel d s) in
xSelectInput d w [KeyPressMask];
xMapWindow d w;
let _ = xNextEventFun d in (* waits any key-press event *)
xCloseDisplay d;
;;
execute with:
ocaml -I +Xlib Xlib.cma sample.ml
Odin
package main
import "vendor:sdl2"
main :: proc() {
using sdl2
window: ^Window = ---
renderer: ^Renderer = ---
event: Event = ---
Init(INIT_VIDEO)
CreateWindowAndRenderer(
640, 480,
WINDOW_SHOWN,
&window, &renderer
)
SetWindowTitle(window, "Empty window")
RenderPresent(renderer)
for event.type != .QUIT {
Delay(10)
PollEvent(&event)
}
DestroyRenderer(renderer)
DestroyWindow(window)
Quit()
}
OpenEdge ABL/Progress 4GL
DEFINE VAR C-Win AS WIDGET-HANDLE NO-UNDO.
CREATE WINDOW C-Win ASSIGN
HIDDEN = YES
TITLE = "OpenEdge Window Display"
HEIGHT = 10.67
WIDTH = 95.4
MAX-HEIGHT = 16
MAX-WIDTH = 95.4
VIRTUAL-HEIGHT = 16
VIRTUAL-WIDTH = 95.4
RESIZE = yes
SCROLL-BARS = no
STATUS-AREA = no
BGCOLOR = ?
FGCOLOR = ?
KEEP-FRAME-Z-ORDER = yes
THREE-D = yes
MESSAGE-AREA = no
SENSITIVE = yes.
VIEW C-Win.
ON WINDOW-CLOSE OF C-Win /* <insert window title> */
DO:
/* This event will close the window and terminate the procedure. */
APPLY "CLOSE":U TO THIS-PROCEDURE.
RETURN NO-APPLY.
END.
WAIT-FOR CLOSE OF THIS-PROCEDURE.
Oz
functor
import
Application
QTk at 'x-oz://system/wp/QTk.ozf'
define
proc {OnClose}
{Application.exit 0}
end
%% Descripe the GUI in a declarative style.
GUIDescription = td(label(text:"Hello World!")
action:OnClose %% Exit app when window closes.
)
%% Create a window object from the description and show it.
Window = {QTk.build GUIDescription}
{Window show}
end
Pascal
Program WindowCreation_SDL;
{$linklib SDL}
uses
SDL,
SysUtils;
var
screen: PSDL_Surface;
begin
SDL_Init(SDL_INIT_VIDEO);
screen := SDL_SetVideoMode( 800, 600, 16, (SDL_SWSURFACE or SDL_HWPALETTE) );
sleep(2000);
SDL_Quit;
end.
Perl
use Tk;
MainWindow->new();
MainLoop;
use SDL::App;
use SDL::Event;
$app = SDL::App->new;
$app->loop({
SDL_QUIT() => sub { exit 0; },
});
use Gtk3 '-init';
$window = Gtk3::Window->new;
$window->signal_connect(
destroy => sub { Gtk3->main_quit; }
);
$window->show_all;
Gtk3->main;
use strict;
use warnings;
use QtGui4;
my $app = Qt::Application(\@ARGV);
my $window = Qt::MainWindow;
$window->show;
exit $app->exec;
use Wx;
$window = Wx::Frame->new(undef, -1, 'title');
$window->Show;
Wx::SimpleApp->new->MainLoop;
Phix
Works on Windows/Linux, 32/64-bit, and you can run this online here.
-- demo\rosetta\Window_creation.exw with javascript_semantics include pGUI.e IupOpen() IupShow(IupDialog(IupVbox({IupLabel("hello")},"MARGIN=200x200"),"TITLE=Hello")) if platform()!=JS then IupMainLoop() IupClose() end if
PicoLisp
(load "@lib/openGl.l")
(glutInit)
(glutCreateWindow "Goodbye, World!")
(keyboardFunc '(() (bye)))
(glutMainLoop)
PowerShell
New-Window -Show
$form = New-Object Windows.Forms.Form
$form.Text = "A Window"
$form.Size = New-Object Drawing.Size(150,150)
$form.ShowDialog() | Out-Null
Processing
size(1000,1000);
Prolog
Works with SWI-Prolog which has a graphic interface XPCE.
?- new(D, window('Prolog Window')), send(D, open).
PureBasic
Define.i MyWin, Event, x, y
x = 400
y = 300
If OpenWindow(0, 0, 0, x, y, "I am a window - PureBasic", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If CreateImage(0, x, y) And StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_Transparent)
Box(0, 0, x, y, #White)
For i = 1 To 10
DrawText(x/3, y/2, "Hello World!", #Black)
;DrawText(Random(200), Random(200), "Hello World!", RGB(Random(255), Random(255), Random(255)))
Next i
StopDrawing()
ImageGadget(0, 0, 0, x, y, ImageID(0))
EndIf
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
; Handle any gadget events here
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
EndIf
Python
import Tkinter
w = Tkinter.Tk()
w.mainloop()
import tkinter
w = tkinter.Tk()
w.mainloop()
from wxPython.wx import *
class MyApp(wxApp):
def OnInit(self):
frame = wxFrame(NULL, -1, "Hello from wxPython")
frame.Show(true)
self.SetTopWindow(frame)
return true
app = MyApp(0)
app.MainLoop()
import win32ui
from pywin.mfc.dialog import Dialog
d = Dialog(win32ui.IDD_SIMPLE_INPUT)
d.CreateWindow()
import gtk
window = gtk.Window()
window.show()
gtk.main()
from PyQt4.QtGui import *
app = QApplication([])
win = QWidget()
win.show()
app.exec_()
R
Although R cannot create windows itself, it has wrappers for several GUI toolkits. tcl/tk is shipped with R by default, and you can create windows with that.
win <- tktoplevel()
The gWidgets packages let you write GUIs in a toolkit independent way. You can create a window with
library(gWidgetstcltk) #or e.g. gWidgetsRGtk2
win <- gwindow()
Racket
#lang racket/gui
(send (new frame%
[label "New Window"]
[width 100] [height 100])
show #t)
Raku
(formerly Perl 6) Library GTK
Exit either by clicking the button or the close window control in the upper corner.
use GTK::Simple;
use GTK::Simple::App;
my GTK::Simple::App $app .= new(title => 'Simple GTK Window');
$app.size-request(250, 100);
$app.set-content(
GTK::Simple::VBox.new(
my $button = GTK::Simple::Button.new(label => 'Exit'),
)
);
$app.border-width = 40;
$button.clicked.tap: { $app.exit }
$app.run;
RapidQ
create form as qform center width=500 height=400 end create form.showModal
REBOL
view layout [size 100x100]
'size' needed to show the close-window button.
Red
Empty Window with close [X] button
>>view []
Ring
Load "guilib.ring"
MyApp = New qApp {
win1 = new qWidget() {
setwindowtitle("Hello World")
setGeometry(100,100,370,250)
show()}
exec()}
Ruby
require 'tk'
window = TkRoot::new()
window::mainloop()
require 'gtk2'
window = Gtk::Window.new.show
Gtk.main
Shoes.app {}
Run BASIC
Show a empty browser with a button to "Close Me"
html "Close me!"
button #c, "Close Me", [doExit]
wait
' -----------------------------------------------------------------------------------
' Get outta here. depending on how may layers you are into the window (history level)
' If you are at the top level then close the window
' ----------------------------------------------------------------------------------
[doExit]
html "<script language='javascript' type='text/javascript'>
var a = history.length;
a = a - 1;
window.open('','_parent','');
window.close();
history.go(-a);
</script>"
wait
Rust
use winit::event::{Event, WindowEvent}; // winit 0.24
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
fn main() {
let event_loop = EventLoop::new();
let _win = WindowBuilder::new()
.with_title("Window")
.build(&event_loop).unwrap();
event_loop.run(move |ev, _, flow| {
match ev {
Event::WindowEvent {
event: WindowEvent::CloseRequested, ..
} => {
*flow = ControlFlow::Exit;
}
_ => {}
}
});
}
Scala
import javax.swing.JFrame
object ShowWindow{
def main(args: Array[String]){
var jf = new JFrame("Hello!")
jf.setSize(800, 600)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
jf.setVisible(true)
}
}
Using native Scala libraries (which are wrappers over Java libraries):
import scala.swing._
import scala.swing.Swing._
object SimpleWindow extends SimpleSwingApplication {
def top = new MainFrame {
title = "Hello!"
preferredSize = ((800, 600):Dimension)
}
}
Scheme
#!r6rs
;; PS-TK example: display simple frame
(import (rnrs)
(lib pstk main) ; change this to refer to your installation of PS/Tk
)
(define tk (tk-start))
(tk/wm 'title tk "PS-Tk Example: Frame")
(tk-event-loop tk)
Seed7
Seed7 has a graphics library (draw.s7i), which is operating system independend. Seed7 programs run on all supported operating systems without a change. The program below opens a small window. The program waits until a key is pressed and exits.
$ include "seed7_05.s7i";
include "draw.s7i";
include "keybd.s7i";
const proc: main is func
begin
screen(200, 200);
KEYBOARD := GRAPH_KEYBOARD;
ignore(getc(KEYBOARD));
end func;
Sidef
Tk
var tk = require('Tk');
%s'MainWindow'.new;
tk.MainLoop;
Gtk2
var gtk2 = require('Gtk2') -> init;
var window = %s'Gtk2::Window'.new;
window.signal_connect(destroy => func(*_) { gtk2.main_quit });
window.show_all;
gtk2.main;
Smalltalk
SystemWindow new openInWorld.
|top|
top := TopView new.
top add: (Label label: 'Hello World') in:(0.0@0.0 corner:1.0@1.0).
top open
Standard ML
Works with PolyML (XWindows/Motif module)
open XWindows ;
open Motif ;
val showWindow = fn () =>
let
val shell = XtAppInitialise "" "demo" "top" [] [XmNwidth 400, XmNheight 300 ] ;
val main = XmCreateMainWindow shell "main" [XmNmappedWhenManaged true ] ;
val buttn = XmCreateDrawnButton main "stop" [ XmNlabelString "Exit"] ;
val quit = fn (w,c,t) => (XtUnrealizeWidget shell; t) ;
in
(
XtSetCallbacks buttn [ (XmNactivateCallback, quit) ] XmNarmCallback ;
XtManageChildren [buttn];
XtManageChild main;
XtRealizeWidget shell
)
end;
call
showWindow ()
Tcl
Loading the Tk package is all that is required to get an initial window:
package require Tk
If you need an additional window:
toplevel .top
If you are using the increasingly popular tclkit under MS Windows, all you have to do is associate the tclkit with the extension “.tcl” and then create an empty file with, e.g., with the name nothing.tcl. Double-clicking that will “open a window” (an empty one).
TI-89 BASIC
:Text "Rosetta Code"
Toka
Toka does not inherently know about graphical environments, but can interact with them using external libraries. This example makes use of the SDL library bindings included with Toka.
needs sdl 800 600 sdl_setup
TorqueScript
new GuiControl(GuiName)
{
profile = "GuiDefaultProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "0 0";
extent = "640 480";
minExtent = "8 2";
enabled = 1;
visible = 1;
clipToParent = 1;
new GuiWindowCtrl()
{
profile = "GuiWindowProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "0 0";
extent = "100 200";
minExtent = "8 2";
enabled = 1;
visible = 1;
clipToParent = 1;
command = "canvas.popDialog(GuiName);";
accelerator = "escape";
maxLength = 255;
resizeWidth = 1;
resizeHeight = 1;
canMove = 1;
canClose = 1;
canMinimize = 1;
canMaximize = 1;
minSize = "50 50";
closeCommand = "canvas.popDialog(GuiName);";
};
};
canvas.pushDialog(GuiName);
TXR
TXR has no library module for connecting to SDL, X11's Xlib, or GTK2.
All of these examples are completely self-contained, using the FFI capability in TXR, which can bind to any library whose interface is defined in terms of C functions and types.
No C header file is processed, and not a single line of C has to be compiled.
SDL
A wait for a SDL key-up event is added, missing in the C version, so that the window does not just appear and disappear.
Note that SDL's header file uses a enum
for the event constants like SDL_KEYUP
. But then in union SD_Event
, the event field declared as UInt8
. (That's how it appears on my Ubuntu system; newer versions of SDL seems to have switched the type field, and other fields of the event structures, to UInt32
.)
Here, we exploit TXR's capability to define enumerations of specific types: we make the event enumeration based on uint8
, giving it a typedef
name, and then use that typedef
in the SD_Event
union.
(defvarl SDL_INIT_VIDEO #x00000020)
(defvarl SDL_SWSURFACE #x00000000)
(defvarl SDL_HWPALETTE #x20000000)
(typedef SDL_Surface (cptr SDL_Surface))
(typedef SDL_EventType (enumed uint8 SDL_EventType
(SDL_KEYUP 3)
(SDL_QUIT 12)))
(typedef SDL_Event (union SD_Event
(type SDL_EventType)
(pad (array 8 uint32))))
(with-dyn-lib "libSDL.so"
(deffi SDL_Init "SDL_Init" int (uint32))
(deffi SDL_SetVideoMode "SDL_SetVideoMode"
SDL_Surface (int int int uint32))
(deffi SDL_GetError "SDL_GetError" str ())
(deffi SDL_WaitEvent "SDL_WaitEvent" int ((ptr-out SDL_Event)))
(deffi SDL_Quit "SDL_Quit" void ()))
(when (neql 0 (SDL_Init SDL_INIT_VIDEO))
(put-string `unable to initialize SDL: @(SDL_GetError)`)
(exit nil))
(unwind-protect
(progn
(SDL_SetVideoMode 800 600 16 (logior SDL_SWSURFACE SDL_HWPALETTE))
(let ((e (make-union (ffi SDL_Event))))
(until* (memql (union-get e 'type) '(SDL_KEYUP SDL_QUIT))
(SDL_WaitEvent e))))
(SDL_Quit))
X11
One difference between the C original and this one is that the XLib macros for direct structure access, like DefaultGC
, DefaultScreen
or WhitePixel
are not used; rather the correspoding C functions are used via FFI: XDefaultScreen
and so on. The macro approach can be mimiced in detail, at the cost of a significant increase in verbosity (cloning the full declaration of the _XDisplay
struct declaration, and reproducing the macros).
Also, this uses an enumeration for the events, so when the event type is decoded from the XEvent
union, it comes out as a Lisp symbol.
(typedef XID uint32)
(typedef Window XID)
(typedef Drawable XID)
(typedef Display (cptr Display))
(typedef GC (cptr GC))
(typedef XEventType (enum _XEventType
(KeyPress 2)
(Expose 12)))
(defvarl KeyPressMask (ash 1 0))
(defvarl ExposureMask (ash 1 15))
(typedef XEvent (union _XEvent
(type XEventType)
(pad (array 24 long))))
(defvarl NULL cptr-null)
(with-dyn-lib "libX11.so"
(deffi XOpenDisplay "XOpenDisplay" Display (bstr))
(deffi XCloseDisplay "XCloseDisplay" int (Display))
(deffi XDefaultScreen "XDefaultScreen" int (Display))
(deffi XRootWindow "XRootWindow" Window (Display int))
(deffi XBlackPixel "XBlackPixel" ulong (Display int))
(deffi XWhitePixel "XWhitePixel" ulong (Display int))
(deffi XCreateSimpleWindow "XCreateSimpleWindow" Window (Display
Window
int int
uint uint uint
ulong ulong))
(deffi XSelectInput "XSelectInput" int (Display Window long))
(deffi XMapWindow "XMapWindow" int (Display Window))
(deffi XNextEvent "XNextEvent" int (Display (ptr-out XEvent)))
(deffi XDefaultGC "XDefaultGC" GC (Display int))
(deffi XFillRectangle "XFillRectangle" int (Display Drawable GC
int int uint uint))
(deffi XDrawString "XDrawString" int (Display Drawable GC
int int bstr int)))
(let* ((msg "Hello, world!")
(d (XOpenDisplay nil)))
(when (equal d NULL)
(put-line "Cannot-open-display" *stderr*)
(exit 1))
(let* ((s (XDefaultScreen d))
(w (XCreateSimpleWindow d (XRootWindow d s) 10 10 100 100 1
(XBlackPixel d s) (XWhitePixel d s))))
(XSelectInput d w (logior ExposureMask KeyPressMask))
(XMapWindow d w)
(while t
(let ((e (make-union (ffi XEvent))))
(XNextEvent d e)
(caseq (union-get e 'type)
(Expose
(XFillRectangle d w (XDefaultGC d s) 20 20 10 10)
(XDrawString d w (XDefaultGC d s) 10 50 msg (length msg)))
(KeyPress (return)))))
(XCloseDisplay d)))
GTK2
(typedef GtkObject* (cptr GtkObject))
(typedef GtkWidget* (cptr GtkWidget))
(typedef GtkWidget* (cptr GtkWidget))
(typedef GtkWindowType (enum GtkWindowType
GTK_WINDOW_TOPLEVEL
GTK_WINDOW_POPUP))
(with-dyn-lib "libgtk-x11-2.0.so.0"
(deffi gtk_init "gtk_init" void ((ptr int) (ptr (ptr (zarray str)))))
(deffi gtk_window_new "gtk_window_new" GtkWidget* (GtkWindowType))
(deffi gtk_signal_connect_full "gtk_signal_connect_full"
ulong (GtkObject* str closure closure val closure int int))
(deffi gtk_widget_show "gtk_widget_show" void (GtkWidget*))
(deffi gtk_main "gtk_main" void ())
(deffi-sym gtk_main_quit "gtk_main_quit"))
(defmacro GTK_OBJECT (cptr)
^(cptr-cast 'GtkObject ,cptr))
(defmacro gtk_signal_connect (object name func func-data)
^(gtk_signal_connect_full ,object ,name ,func cptr-null
,func-data cptr-null 0 0))
(gtk_init (length *args*) (vec-list *args*))
(let ((window (gtk_window_new 'GTK_WINDOW_TOPLEVEL)))
(gtk_signal_connect (GTK_OBJECT window) "destroy" gtk_main_quit nil)
(gtk_widget_show window)
(gtk_main))
Win32/Win64
This solution is based on the "Your First Windows Program" example in MSDN. It registers a Window class, creates a Window and runs a Windows message loop against a custom WndProc
function that is written in Lisp, which handles WM_QUIT
and WM_PAINT
events exactly like its C counterpart. All necessary basic types, structures, constants and foreign functions are declared using the TXR FFI language.
Note that the CW_USEDEFAULT
constant in the Windows header files is defined as 0x80000000
. This is out of range of the signed int
arguments of CreateWindowEx
with which it is used. Microsoft is relying on an implementation-defined C conversion to turn this value into the most negative int
. When the original constant was used in the TXR translation, TXR's FFI uncovered this little problem by throwing an exception arising from the out-of-range conversion attempt. The fix is to specify the correct value directly as #x-80000000
.
(typedef LRESULT int-ptr-t)
(typedef LPARAM int-ptr-t)
(typedef WPARAM uint-ptr-t)
(typedef UINT uint32)
(typedef LONG int32)
(typedef WORD uint16)
(typedef DWORD uint32)
(typedef LPVOID cptr)
(typedef BOOL (bool int32))
(typedef BYTE uint8)
(typedef HWND (cptr HWND))
(typedef HINSTANCE (cptr HINSTANCE))
(typedef HICON (cptr HICON))
(typedef HCURSOR (cptr HCURSOR))
(typedef HBRUSH (cptr HBRUSH))
(typedef HMENU (cptr HMENU))
(typedef HDC (cptr HDC))
(typedef ATOM WORD)
(typedef LPCTSTR wstr)
(defvarl NULL cptr-null)
(typedef WNDCLASS (struct WNDCLASS
(style UINT)
(lpfnWndProc closure)
(cbClsExtra int)
(cbWndExtra int)
(hInstance HINSTANCE)
(hIcon HICON)
(hCursor HCURSOR)
(hbrBackground HBRUSH)
(lpszMenuName LPCTSTR)
(lpszClassName LPCTSTR)))
(defmeth WNDCLASS :init (me)
(zero-fill (ffi WNDCLASS) me))
(typedef POINT (struct POINT
(x LONG)
(y LONG)))
(typedef MSG (struct MSG
(hwnd HWND)
(message UINT)
(wParam WPARAM)
(lParam LPARAM)
(time DWORD)
(pt POINT)))
(typedef RECT (struct RECT
(left LONG)
(top LONG)
(right LONG)
(bottom LONG)))
(typedef PAINTSTRUCT (struct PAINTSTRUCT
(hdc HDC)
(fErase BOOL)
(rcPaint RECT)
(fRestore BOOL)
(fIncUpdate BOOL)
(rgbReserved (array 32 BYTE))))
(defvarl CW_USEDEFAULT #x-80000000)
(defvarl WS_OVERLAPPEDWINDOW #x00cf0000)
(defvarl SW_SHOWDEFAULT 5)
(defvarl WM_DESTROY 2)
(defvarl WM_PAINT 15)
(defvarl COLOR_WINDOW 5)
(deffi-cb wndproc-fn LRESULT (HWND UINT LPARAM WPARAM))
(with-dyn-lib "kernel32.dll"
(deffi GetModuleHandle "GetModuleHandleW" HINSTANCE (wstr)))
(with-dyn-lib "user32.dll"
(deffi RegisterClass "RegisterClassW" ATOM ((ptr-in WNDCLASS)))
(deffi CreateWindowEx "CreateWindowExW" HWND (DWORD
LPCTSTR LPCTSTR
DWORD
int int int int
HWND HMENU HINSTANCE
LPVOID))
(deffi ShowWindow "ShowWindow" BOOL (HWND int))
(deffi GetMessage "GetMessageW" BOOL ((ptr-out MSG) HWND UINT UINT))
(deffi TranslateMessage "TranslateMessage" BOOL ((ptr-in MSG)))
(deffi DispatchMessage "DispatchMessageW" LRESULT ((ptr-in MSG)))
(deffi PostQuitMessage "PostQuitMessage" void (int))
(deffi DefWindowProc "DefWindowProcW" LRESULT (HWND UINT LPARAM WPARAM))
(deffi BeginPaint "BeginPaint" HDC (HWND (ptr-out PAINTSTRUCT)))
(deffi EndPaint "EndPaint" BOOL (HWND (ptr-in PAINTSTRUCT)))
(deffi FillRect "FillRect" int (HDC (ptr-in RECT) HBRUSH)))
(defun WindowProc (hwnd uMsg wParam lParam)
(caseql* uMsg
(WM_DESTROY
(PostQuitMessage 0)
0)
(WM_PAINT
(let* ((ps (new PAINTSTRUCT))
(hdc (BeginPaint hwnd ps)))
(FillRect hdc ps.rcPaint (cptr-int (succ COLOR_WINDOW) 'HBRUSH))
(EndPaint hwnd ps)
0))
(t (DefWindowProc hwnd uMsg wParam lParam))))
(let* ((hInstance (GetModuleHandle nil))
(wc (new WNDCLASS
lpfnWndProc [wndproc-fn WindowProc]
hInstance hInstance
lpszClassName "Sample Window Class")))
(RegisterClass wc)
(let ((hwnd (CreateWindowEx 0 wc.lpszClassName "Learn to Program Windows"
WS_OVERLAPPEDWINDOW
CW_USEDEFAULT CW_USEDEFAULT
CW_USEDEFAULT CW_USEDEFAULT
NULL NULL hInstance NULL)))
(unless (equal hwnd NULL)
(ShowWindow hwnd SW_SHOWDEFAULT)
(let ((msg (new MSG)))
(while (GetMessage msg NULL 0 0)
(TranslateMessage msg)
(DispatchMessage msg))))))
VBA
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Need to reference the following object library (From the Tools menu, choose References) Microsoft Forms 2.0 Object Library !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! And : Programmatic Access to Visual Basic Project must be trusted. See it in Macro's security!
Option Explicit
Sub InsertForm()
Dim myForm As Object, strname As String
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
strname = myForm.Name
VBA.UserForms.Add(strname).Show
End Sub
Vedit macro language
Creates an empty window with ID 'A' near the upper left corner of document area, with height of 20 text lines and width of 80 characters.
Win_Create(A, 2, 5, 20, 80)
Note: if you run this command while in Visual Mode, you should adjust your active window smaller so that the created window will not be hidden behind it (since the active window is always on top).
Visual Basic .NET
Dim newForm as new Form
newForm.Text = "It's a new window"
newForm.Show()
V (Vlang)
import gg
import gx
fn main() {
mut app := gg.new_context(
bg_color: gx.white
resizable: true
create_window: true
width: 600
height: 600
frame_fn: frame
window_title: "Empty Window"
)
app.run()
}
fn frame(mut ctx gg.Context) {
ctx.begin()
ctx.end()
}
Wren
import "dome" for Window
class EmptyWindow {
construct new(width, height) {
Window.title = "Empty window"
Window.resize(width, height)
}
init() {}
update() {}
draw(alpha) {}
}
var Game = EmptyWindow.new(600, 600)
X86 Assembly
;GTK imports and defines etc.
%define GTK_WINDOW_TOPLEVEL 0
extern gtk_init
extern gtk_window_new
extern gtk_widget_show
extern gtk_signal_connect
extern gtk_main
extern g_print
extern gtk_main_quit
bits 32
section .text
global _main
;exit signal
sig_main_exit:
push exit_sig_msg
call g_print
add esp, 4
call gtk_main_quit
ret
_main:
mov ebp, esp
sub esp, 8
push argv
push argc
call gtk_init
add esp, 8 ;stack alignment.
push GTK_WINDOW_TOPLEVEL
call gtk_window_new
add esp, 4
mov [ebp-4], eax ;ebp-4 now holds our GTKWindow pointer.
push 0
push sig_main_exit
push gtk_delete_event
push dword [ebp-4]
call gtk_signal_connect
add esp, 16
push dword [ebp-4]
call gtk_widget_show
add esp, 4
call gtk_main
section .data
;sudo argv
argc dd 1
argv dd args
args dd title
dd 0
title db "GTK Window",0
gtk_delete_event db 'delete_event',0
exit_sig_msg db "-> Rage quitting..",10,0
.586
.model flat, stdcall
option casemap:none
include /masm32/include/windows.inc
include /masm32/include/kernel32.inc
include /masm32/include/user32.inc
includelib /masm32/lib/kernel32.lib
includelib /masm32/lib/user32.lib
WinMain proto :dword,:dword,:dword,:dword
.data
ClassName db "WndClass",0
AppName db "Window!",0
.data?
hInstance dd ?
CommandLine dd ?
.code
start:
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke GetCommandLine
mov CommandLine, eax
invoke WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
WinMain proc hInst:dword, hPervInst:dword, CmdLine:dword, CmdShow:dword
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
wc.cbSize, sizeof WNDCLASSEX
wc.style, CS_HREDRAW or CS_VREDRAW
wc.lpfnWndPRoc, offset WndProc
wc.cbClsExtra,NULL
wc.cbWndExtra, NULL
push hInstance
pop wc.hInstance
mov wc.hbrBackground, COLOR_BTNFACE+1
mov wc.lpszMenuName NULL
mov wc.lpszClassName, offset ClassName
invoke LoadIcon, NULL, IDI_APPLICATION
mov wc.hIcon, eax
mov wc.hIconSm, eax
invoke LoadCursor, NULL, IDC_ARROW
mov wc.hCursor, eax
invoke RegisterClassEx, addr wc
invoke CreateWindowEx, NULL, addr ClassName, addr AppName, WS_OVERLAPPEDWINDOW, CS_USEDEFAULT, CW_USEDEFAUT,\
CW_USEDEFAUT, CW_USEDEFAUT, NULL, NULL, hInst, NULL
mov hwnd, eax
invoke ShowWindow, hwnd, SW_SHOWNORMAL
invoke UpdateWindow, hwnd
.while TRUE
invoke GetMessage, addr msg, NULL, 0,0
.break .if (!eax)
invoke TranslateMessage, addr msg
invoke DispatchMessage, addr msg
.endw
mov eax, msg.wParam
ret
WinMain endp
WndProc proc hWnd:dword, uMsg:dword, wParam:dword, lParam:dword
mov eax, uMsg
.if eax==WM_DESTROY
invoke PostQuitMessage, NULL
.else
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
.endif
xor eax, eax
ret
WndProc endp
end start
Yabasic
open window 400,200 //minimum line required to accomplish the indicated task
clear screen
text 200,100,"I am a window - close me!","cc"
end
- Programming Tasks
- GUI
- Ada
- GTK
- GtkAda
- ALGOL 68
- AmigaBASIC
- AurelBasic
- AutoHotkey
- AutoIt
- BaCon
- BASIC256
- BBC BASIC
- C
- SDL
- ANSI C
- Gtk2
- GLUT
- C sharp
- Windows Forms
- .NET Framework
- C++
- Qt
- Clojure
- Swing
- Common Lisp
- CAPI
- CLIM
- D
- FLTK4d
- Derelict
- QD
- Delphi
- Dragon
- E
- Eiffel
- Emacs Lisp
- Euphoria
- ARWEN
- EuGTK
- EuWinGUI
- Win32Lib
- WxEuphoria
- F Sharp
- Factor
- Fantom
- Forth
- Forth Foundation Library
- FreeBASIC
- Frink
- FutureBasic
- Gambas
- Go
- Go-gtk
- Go-SDL
- Groovy
- GUISS
- Haskell
- HGL
- HicEst
- IDL
- Icon
- Icon Programming Library
- Unicon
- J
- Java
- JavaScript
- Julia
- Kotlin
- Liberty BASIC
- Lingo
- Lua
- IUPLua
- M2000 Interpreter
- Mathematica
- Wolfram Language
- MIRC Scripting Language
- Nanoquery
- NetRexx
- Nim
- Gintro
- Objeck
- Objective-C
- OCaml
- LablTk
- LablGTK2
- OCamlSDL
- OCamlSDL2
- Ocaml-sfml
- OCaml-Xlib
- Odin
- OpenEdge ABL/Progress 4GL
- Oz
- Pascal
- SysUtils
- Perl
- Perl/Tk
- Perl/SDL
- Perl/Gtk3
- Perl/Qt
- Perl/Wx
- Phix
- Phix/basics
- Phix/pGUI
- PicoLisp
- PowerShell
- WPK
- Processing
- Prolog
- PureBasic
- Python
- Tkinter
- WxPython
- Pythonwin
- PyGTK
- PyQT
- R
- GWidgets
- Racket
- Raku
- RapidQ
- REBOL
- Red
- Ring
- Ruby
- Ruby/Tk
- Shoes
- Run BASIC
- Rust
- Winit
- Scala
- Sdljava
- Scala Java Swing interoperability
- Scheme
- Scheme/PsTk
- Seed7
- Sidef
- Smalltalk
- Standard ML
- Tcl
- Tk
- TI-89 BASIC
- Toka
- TorqueScript
- TXR
- VBA
- Vedit macro language
- Visual Basic .NET
- V (Vlang)
- Wren
- DOME
- X86 Assembly
- Yabasic
- Batch File/Omit
- EasyLang/Omit
- Logtalk/Omit
- Maxima/Omit
- PARI/GP/Omit
- PHP/Omit
- PostScript/Omit
- Retro/Omit
- SQL PL/Omit
- TI-83 BASIC/Omit
- Unlambda/Omit