Window creation/X11: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 9: Line 9:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program creatFenX1164.s */
/* program creatFenX1164.s */
Line 156: Line 156:
.include "../includeARM64.inc"
.include "../includeARM64.inc"


</syntaxhighlight>
</lang>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 163: Line 163:
Using the X11 & plotutil libraries is not part of any of the original UNESCO/IFIP [[ALGOL 68]]
Using the X11 & plotutil libraries is not part of any of the original UNESCO/IFIP [[ALGOL 68]]
reports. As at December 2008 only [[ALGOL 68G]] comes with these built in.
reports. As at December 2008 only [[ALGOL 68G]] comes with these built in.
<lang algol68>FILE window;
<syntaxhighlight lang="algol68">FILE window;
draw device (window, "X", "600x400");
draw device (window, "X", "600x400");
open (window, "Hello, World!", stand draw channel);
open (window, "Hello, World!", stand draw channel);
Line 172: Line 172:
draw show (window);
draw show (window);
VOID (read char);
VOID (read char);
close (window)</lang>
close (window)</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>


/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
Line 650: Line 650:
bx lr @ leave function
bx lr @ leave function
iMagicNumber: .int 0xCCCCCCCD
iMagicNumber: .int 0xCCCCCCCD
</syntaxhighlight>
</lang>




=={{header|BaCon}}==
=={{header|BaCon}}==


<syntaxhighlight lang="c">
<lang c>
'--- added a flush to exit cleanly
'--- added a flush to exit cleanly
PRAGMA LDFLAGS `pkg-config --cflags --libs x11`
PRAGMA LDFLAGS `pkg-config --cflags --libs x11`
Line 719: Line 719:
FLUSH(d)
FLUSH(d)
CLOSE_DISPLAY(d)
CLOSE_DISPLAY(d)
</lang>
</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
Line 728: Line 728:
* gcc hello-x.c -L/usr/X11R6/lib -lX11 -o hello-x
* gcc hello-x.c -L/usr/X11R6/lib -lX11 -o hello-x


<lang c>#include <X11/Xlib.h>
<syntaxhighlight lang="c">#include <X11/Xlib.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 764: Line 764:
XCloseDisplay(d);
XCloseDisplay(d);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=== XCB ===
=== XCB ===
Line 772: Line 772:
* gcc -o helloxcb helloxcb.c -lxcb
* gcc -o helloxcb helloxcb.c -lxcb


<lang c>#include <stdlib.h>
<syntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
Line 854: Line 854:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|COBOL}}==
=={{header|COBOL}}==
Tested with GnuCOBOL, and only on 64bit architecture, GNU/Linux. Nods to the C XLib version.
Tested with GnuCOBOL, and only on 64bit architecture, GNU/Linux. Nods to the C XLib version.


<lang cobol> identification division.
<syntaxhighlight lang="cobol"> identification division.
program-id. x11-hello.
program-id. x11-hello.
installation. cobc -x x11-hello.cob -lX11
installation. cobc -x x11-hello.cob -lX11
Line 1,033: Line 1,033:


goback.
goback.
end program x11-hello.</lang>
end program x11-hello.</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 1,046: Line 1,046:
This example uses CLX, which is the de facto standard X11 library for Common Lisp. CLX is not a binding to Xlib; it is a Lisp library implementing the X11 protocol.
This example uses CLX, which is the de facto standard X11 library for Common Lisp. CLX is not a binding to Xlib; it is a Lisp library implementing the X11 protocol.


<lang lisp>;;; Single-file/interactive setup; large applications should define an ASDF system instead
<syntaxhighlight lang="lisp">;;; Single-file/interactive setup; large applications should define an ASDF system instead


(let* ((display (open-default-display))
(let* ((display (open-default-display))
Line 1,076: Line 1,076:
(close-display display)))
(close-display display)))


</syntaxhighlight>
</lang>
=={{header|Forth}}==
=={{header|Forth}}==
Running Ubuntu 20.04 64-bit
Running Ubuntu 20.04 64-bit
Using : Gforth 0.7.9_20211014
Using : Gforth 0.7.9_20211014


<syntaxhighlight lang="forth">
<lang Forth>
warnings off
warnings off
require xlib.fs
require xlib.fs
Line 1,147: Line 1,147:
close-graphics
close-graphics
bye
bye
</syntaxhighlight>
</lang>


=={{header|Go}}==
=={{header|Go}}==
{{trans|C}}
{{trans|C}}
<lang go>package main
<syntaxhighlight lang="go">package main


// Copyright (c) 2013 Alex Kesling
// Copyright (c) 2013 Alex Kesling
Line 1,267: Line 1,267:
}
}
return
return
}</lang>
}</syntaxhighlight>
Screen capture:
Screen capture:


Line 1,276: Line 1,276:
groovy WindowCreation.groovy
groovy WindowCreation.groovy


<lang groovy>import javax.swing.*
<syntaxhighlight lang="groovy">import javax.swing.*
import java.awt.*
import java.awt.*
import java.awt.event.WindowAdapter
import java.awt.event.WindowAdapter
Line 1,306: Line 1,306:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|GUISS}}==
=={{header|GUISS}}==
Graphical User Interface Support Script is really a language for operating a computer, rather than programming one, so we cannot do this via X11 libraries. The example uses leafpad for our open window, and the box symbols to enclose our text:
Graphical User Interface Support Script is really a language for operating a computer, rather than programming one, so we cannot do this via X11 libraries. The example uses leafpad for our open window, and the box symbols to enclose our text:


<lang guiss>Start,Programs,Applications,Editors,Leafpad,Textbox,
<syntaxhighlight lang="guiss">Start,Programs,Applications,Editors,Leafpad,Textbox,
Type:[openbox]Hello World[pling][closebox]</lang>
Type:[openbox]Hello World[pling][closebox]</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
Using {{libheader|X11}} from [http://hackage.haskell.org/packages/hackage.html HackageDB]
Using {{libheader|X11}} from [http://hackage.haskell.org/packages/hackage.html HackageDB]
<lang haskell>import Graphics.X11.Xlib
<syntaxhighlight lang="haskell">import Graphics.X11.Xlib
import Control.Concurrent (threadDelay)
import Control.Concurrent (threadDelay)


Line 1,338: Line 1,338:
destroyWindow display xwin
destroyWindow display xwin
closeDisplay display
closeDisplay display
</syntaxhighlight>
</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon provide a portable graphics implementation that does not rely upon a toolkit. The intent is to be platform independent and the same code runs on multiple platforms without change and producing results with only minor variations. Icon and Unicon graphics are implemented in X-Windows as well as MS-Windows and others. There are additional 3D graphics capabilities implemented using opengl.
Icon and Unicon provide a portable graphics implementation that does not rely upon a toolkit. The intent is to be platform independent and the same code runs on multiple platforms without change and producing results with only minor variations. Icon and Unicon graphics are implemented in X-Windows as well as MS-Windows and others. There are additional 3D graphics capabilities implemented using opengl.
<lang Icon>procedure main()
<syntaxhighlight lang="icon">procedure main()
W1 := open("X-Window","g","size=250,250","bg=black","fg=red") | stop("unable to open window")
W1 := open("X-Window","g","size=250,250","bg=black","fg=red") | stop("unable to open window")
FillRectangle(W1,50,50,150,150)
FillRectangle(W1,50,50,150,150)
Line 1,348: Line 1,348:
end
end


link graphics</lang>
link graphics</syntaxhighlight>


{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
Line 1,360: Line 1,360:
run
run
java WindowExample
java WindowExample
<lang java>import javax.swing.JFrame;
<syntaxhighlight lang="java">import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.SwingUtilities;


Line 1,380: Line 1,380:
frame.setVisible(true);
frame.setVisible(true);
}
}
}</lang>The previous example works but doesn't write any text or draw any box; the following does both.<lang java>import java.awt.*;
}</syntaxhighlight>The previous example works but doesn't write any text or draw any box; the following does both.<syntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.geom.*;
Line 1,406: Line 1,406:
f.setVisible(true);
f.setVisible(true);
}
}
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
This was based on https://en.wikipedia.org/wiki/Xlib, and mostly quoted from from the XLib.jl test2() testing code function.
This was based on https://en.wikipedia.org/wiki/Xlib, and mostly quoted from from the XLib.jl test2() testing code function.
<lang julia>
<syntaxhighlight lang="julia">
using Xlib
using Xlib


Line 1,451: Line 1,451:


x11demo()
x11demo()
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C}}
{{trans|C}}
{{libheader|Xlib}}
{{libheader|Xlib}}
<lang scala>// Kotlin Native v0.3
<syntaxhighlight lang="scala">// Kotlin Native v0.3


import kotlinx.cinterop.*
import kotlinx.cinterop.*
Line 1,487: Line 1,487:
XCloseDisplay(d)
XCloseDisplay(d)
nativeHeap.free(e)
nativeHeap.free(e)
}</lang>
}</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
Line 1,494: Line 1,494:
Ao for this task we use the M2000 way to make a form and do something on it.
Ao for this task we use the M2000 way to make a form and do something on it.


<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
\\ M2000 froms (windows) based on a flat, empty with no title bar, vb6 window and a user control.
\\ M2000 froms (windows) based on a flat, empty with no title bar, vb6 window and a user control.
\\ title bar is a user control in M2000 form
\\ title bar is a user control in M2000 form
Line 1,536: Line 1,536:
}
}
SquareAndText2Window
SquareAndText2Window
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Note that GUIKit is a high-level wrapper for Swing.
Note that GUIKit is a high-level wrapper for Swing.
<lang>Needs["GUIKit`"]
<syntaxhighlight lang="text">Needs["GUIKit`"]
ref = GUIRun[Widget["Panel", {
ref = GUIRun[Widget["Panel", {
Widget[
Widget[
Line 1,547: Line 1,547:
"GIF"]]}],
"GIF"]]}],
Widget["Label", { "text" -> "Hello World!"}]}
Widget["Label", { "text" -> "Hello World!"}]}
]]</lang>
]]</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import x11/[xlib,xutil,x]
<syntaxhighlight lang="nim">import x11/[xlib,xutil,x]


const
const
Line 1,625: Line 1,625:




main()</lang>
main()</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 1,640: Line 1,640:
#load "Xlib.cma"
#load "Xlib.cma"


<lang ocaml>open Xlib
<syntaxhighlight lang="ocaml">open Xlib


let () =
let () =
Line 1,663: Line 1,663:
main_loop();
main_loop();
xCloseDisplay d;
xCloseDisplay d;
;;</lang>
;;</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 1,670: Line 1,670:
from wiki.freepascal.org/X11#Examples.
from wiki.freepascal.org/X11#Examples.
Compiled with Freepascal 2.6.4-32
Compiled with Freepascal 2.6.4-32
<lang pascal>program xshowwindow;
<syntaxhighlight lang="pascal">program xshowwindow;
{$mode objfpc}{$H+}
{$mode objfpc}{$H+}


Line 1,727: Line 1,727:
ModalShowX11Window('Hello, World!');
ModalShowX11Window('Hello, World!');
end.
end.
</syntaxhighlight>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
=== X11::Protocol ===
=== X11::Protocol ===
<lang perl>#!/usr/bin/perl -w
<syntaxhighlight lang="perl">#!/usr/bin/perl -w
use strict;
use strict;
use X11::Protocol;
use X11::Protocol;
Line 1,775: Line 1,775:
for (;;) {
for (;;) {
$X->handle_input;
$X->handle_input;
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,783: Line 1,783:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
The following script works in the 32-bit version, using inlined C code
The following script works in the 32-bit version, using inlined C code
<lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
<syntaxhighlight lang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l


(load "@lib/misc.l" "@lib/gcc.l")
(load "@lib/misc.l" "@lib/gcc.l")
Line 1,830: Line 1,830:


(simpleWin 300 200 "Hello World")
(simpleWin 300 200 "Hello World")
(bye)</lang>
(bye)</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Line 1,846: Line 1,846:
* python xlib_hello_world.py
* python xlib_hello_world.py


<lang python>from Xlib import X, display
<syntaxhighlight lang="python">from Xlib import X, display


class Window:
class Window:
Line 1,879: Line 1,879:
if __name__ == "__main__":
if __name__ == "__main__":
Window(display.Display(), "Hello, World!").loop()</lang>
Window(display.Display(), "Hello, World!").loop()</syntaxhighlight>


=== XCB ===
=== XCB ===
{{libheader|python-xcb}}
{{libheader|python-xcb}}


<lang python>import xcb
<syntaxhighlight lang="python">import xcb
from xcb.xproto import *
from xcb.xproto import *
import xcb.render
import xcb.render
Line 1,926: Line 1,926:
conn.disconnect()
conn.disconnect()


main()</lang>
main()</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
Using Racket's GUI which is implemented using gtk. It's not low level, but OTOH it works on Windows and OS X too.
Using Racket's GUI which is implemented using gtk. It's not low level, but OTOH it works on Windows and OS X too.


<lang Racket>#lang racket/gui
<syntaxhighlight lang="racket">#lang racket/gui


(define frame (new frame%
(define frame (new frame%
Line 1,943: Line 1,943:
(send dc set-text-foreground "blue")
(send dc set-text-foreground "blue")
(send dc draw-text "Don't Panic!" 0 0))])
(send dc draw-text "Don't Panic!" 0 0))])
(send frame show #t)</lang>
(send frame show #t)</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 1,951: Line 1,951:
There is not yet a X11 library in Raku but we can write the minimal C mappings for this task.
There is not yet a X11 library in Raku but we can write the minimal C mappings for this task.


<lang perl6>use NativeCall;
<syntaxhighlight lang="raku" line>use NativeCall;


class Display is repr('CStruct') {
class Display is repr('CStruct') {
Line 2,012: Line 2,012:
}
}
}
}
XCloseDisplay($display);</lang>
XCloseDisplay($display);</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
{{libheader|Scala}}<lang Scala>import scala.swing.{ MainFrame, SimpleSwingApplication }
{{libheader|Scala}}<syntaxhighlight lang="scala">import scala.swing.{ MainFrame, SimpleSwingApplication }
import scala.swing.Swing.pair2Dimension
import scala.swing.Swing.pair2Dimension


Line 2,024: Line 2,024:
preferredSize = ((200, 150))
preferredSize = ((200, 150))
}
}
}</lang>
}</syntaxhighlight>


=={{header|Standard ML}}==
=={{header|Standard ML}}==
Works with PolyML
Works with PolyML
<lang Standard ML>open XWindows ;
<syntaxhighlight lang="standard ml">open XWindows ;
val dp = XOpenDisplay "" ;
val dp = XOpenDisplay "" ;
val w = XCreateSimpleWindow (RootWindow dp) origin (Area {x=0,y=0,w=400,h=300}) 3 0 0xffffff ;
val w = XCreateSimpleWindow (RootWindow dp) origin (Area {x=0,y=0,w=400,h=300}) 3 0 0xffffff ;
Line 2,034: Line 2,034:
XFlush dp ;
XFlush dp ;
XDrawString w (DefaultGC dp) (XPoint {x=10,y=50}) "Hello World!" ;
XDrawString w (DefaultGC dp) (XPoint {x=10,y=50}) "Hello World!" ;
XFlush dp ;</lang>
XFlush dp ;</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 2,041: Line 2,041:
===Low level interface===
===Low level interface===
{{libheader|critcl}}
{{libheader|critcl}}
<lang tcl>package provide xlib 1
<syntaxhighlight lang="tcl">package provide xlib 1
package require critcl
package require critcl


Line 2,105: Line 2,105:
const char *str = Tcl_GetStringFromObj(msg, &len);
const char *str = Tcl_GetStringFromObj(msg, &len);
XDrawString(d, (Window)w, gc, x, y, str, len);
XDrawString(d, (Window)w, gc, x, y, str, len);
}</lang>
}</syntaxhighlight>
Note that this only does enough for this demo. A full adaptation is too long for RosettaCode...
Note that this only does enough for this demo. A full adaptation is too long for RosettaCode...


This could then be used like this:
This could then be used like this:
<lang tcl>package require xlib
<syntaxhighlight lang="tcl">package require xlib


XOpenDisplay {}
XOpenDisplay {}
Line 2,119: Line 2,119:
}
}
XDestroyWindow $w
XDestroyWindow $w
XCloseDisplay</lang>
XCloseDisplay</syntaxhighlight>
===Higher level interface===
===Higher level interface===
Just because there is a low level package does not mean that it is pleasant to use from Tcl code. Therefore this second package wraps it up inside a higher-level package that provides a more natural way of interacting.
Just because there is a low level package does not mean that it is pleasant to use from Tcl code. Therefore this second package wraps it up inside a higher-level package that provides a more natural way of interacting.
<br>
<br>
{{works with|Tcl|8.6}} or {{libheader|TclOO}}
{{works with|Tcl|8.6}} or {{libheader|TclOO}}
<lang tcl>package require TclOO
<syntaxhighlight lang="tcl">package require TclOO
package provide x11 1
package provide x11 1


Line 2,176: Line 2,176:
}
}
}
}
}</lang>
}</syntaxhighlight>
This script puts the pieces together to carry out the details of the task.
This script puts the pieces together to carry out the details of the task.
<lang tcl>package require x11
<syntaxhighlight lang="tcl">package require x11


# With a display connection open, create and map a window
# With a display connection open, create and map a window
Line 2,198: Line 2,198:


$w destroy
$w destroy
}</lang>
}</syntaxhighlight>
Improving this by adding more sophisticated event handling and more window methods is left as an exercise.
Improving this by adding more sophisticated event handling and more window methods is left as an exercise.


Line 2,210: Line 2,210:
<br>
<br>
As it's not currently possible for Wren-cli to access Xlib directly, we embed a Wren script in a C application to complete this task.
As it's not currently possible for Wren-cli to access Xlib directly, we embed a Wren script in a C application to complete this task.
<lang ecmascript>/* window_creation_x11.wren */
<syntaxhighlight lang="ecmascript">/* window_creation_x11.wren */


var KeyPressMask = 1 << 0
var KeyPressMask = 1 << 0
Line 2,273: Line 2,273:
if (e.eventType == KeyPress) break
if (e.eventType == KeyPress) break
}
}
xd.closeDisplay()</lang>
xd.closeDisplay()</syntaxhighlight>
<br>
<br>
We now embed this Wren script in the following C program, compile and run it.
We now embed this Wren script in the following C program, compile and run it.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 2,493: Line 2,493:
free(script);
free(script);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}