all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
1
Task/Window-creation-X11/0DESCRIPTION
Normal file
1
Task/Window-creation-X11/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
Create a simple X11 application, using an X11 protocol library such as Xlib or XCB, that draws a box and "Hello World" in a window. Implementations of this task should ''avoid using a toolkit'' as much as possible.
|
||||
4
Task/Window-creation-X11/1META.yaml
Normal file
4
Task/Window-creation-X11/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
note: GUI
|
||||
requires:
|
||||
- Graphics
|
||||
10
Task/Window-creation-X11/ALGOL-68/window-creation-x11.alg
Normal file
10
Task/Window-creation-X11/ALGOL-68/window-creation-x11.alg
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
FILE window;
|
||||
draw device (window, "X", "600x400");
|
||||
open (window, "Hello, World!", stand draw channel);
|
||||
draw erase (window);
|
||||
draw move (window, 0.25, 0.5);
|
||||
draw colour (window, 1, 0, 0);
|
||||
draw text (window, "c", "c", "hello world");
|
||||
draw show (window);
|
||||
VOID (read char);
|
||||
close (window)
|
||||
37
Task/Window-creation-X11/C/window-creation-x11-1.c
Normal file
37
Task/Window-creation-X11/C/window-creation-x11-1.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include <X11/Xlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void) {
|
||||
Display *d;
|
||||
Window w;
|
||||
XEvent e;
|
||||
char *msg = "Hello, World!";
|
||||
int s;
|
||||
|
||||
d = XOpenDisplay(NULL);
|
||||
if (d == NULL) {
|
||||
fprintf(stderr, "Cannot open display\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
s = DefaultScreen(d);
|
||||
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
|
||||
BlackPixel(d, s), WhitePixel(d, s));
|
||||
XSelectInput(d, w, ExposureMask | KeyPressMask);
|
||||
XMapWindow(d, w);
|
||||
|
||||
while (1) {
|
||||
XNextEvent(d, &e);
|
||||
if (e.type == Expose) {
|
||||
XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
|
||||
XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
|
||||
}
|
||||
if (e.type == KeyPress)
|
||||
break;
|
||||
}
|
||||
|
||||
XCloseDisplay(d);
|
||||
return 0;
|
||||
}
|
||||
83
Task/Window-creation-X11/C/window-creation-x11-2.c
Normal file
83
Task/Window-creation-X11/C/window-creation-x11-2.c
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <xcb/xcb.h>
|
||||
|
||||
int main ()
|
||||
{
|
||||
xcb_connection_t *c;
|
||||
xcb_screen_t *screen;
|
||||
xcb_drawable_t win;
|
||||
xcb_gcontext_t foreground;
|
||||
xcb_gcontext_t background;
|
||||
xcb_generic_event_t *e;
|
||||
uint32_t mask = 0;
|
||||
uint32_t values[2];
|
||||
|
||||
char string[] = "Hello, XCB!";
|
||||
uint8_t string_len = strlen(string);
|
||||
|
||||
xcb_rectangle_t rectangles[] = {
|
||||
{40, 40, 20, 20},
|
||||
};
|
||||
|
||||
c = xcb_connect (NULL, NULL);
|
||||
|
||||
/* get the first screen */
|
||||
screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
|
||||
|
||||
/* root window */
|
||||
win = screen->root;
|
||||
|
||||
/* create black (foreground) graphic context */
|
||||
foreground = xcb_generate_id (c);
|
||||
mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
|
||||
values[0] = screen->black_pixel;
|
||||
values[1] = 0;
|
||||
xcb_create_gc (c, foreground, win, mask, values);
|
||||
|
||||
/* create white (background) graphic context */
|
||||
background = xcb_generate_id (c);
|
||||
mask = XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES;
|
||||
values[0] = screen->white_pixel;
|
||||
values[1] = 0;
|
||||
xcb_create_gc (c, background, win, mask, values);
|
||||
|
||||
/* create the window */
|
||||
win = xcb_generate_id(c);
|
||||
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
|
||||
values[0] = screen->white_pixel;
|
||||
values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
|
||||
xcb_create_window (c, /* connection */
|
||||
XCB_COPY_FROM_PARENT, /* depth */
|
||||
win, /* window Id */
|
||||
screen->root, /* parent window */
|
||||
0, 0, /* x, y */
|
||||
150, 150, /* width, height */
|
||||
10, /* border_width */
|
||||
XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
|
||||
screen->root_visual, /* visual */
|
||||
mask, values); /* masks */
|
||||
|
||||
/* map the window on the screen */
|
||||
xcb_map_window (c, win);
|
||||
|
||||
xcb_flush (c);
|
||||
|
||||
while ((e = xcb_wait_for_event (c))) {
|
||||
switch (e->response_type & ~0x80) {
|
||||
case XCB_EXPOSE:
|
||||
xcb_poly_rectangle (c, win, foreground, 1, rectangles);
|
||||
xcb_image_text_8 (c, string_len, win, background, 20, 20, string);
|
||||
xcb_flush (c);
|
||||
break;
|
||||
case XCB_KEY_PRESS:
|
||||
goto endloop;
|
||||
}
|
||||
free (e);
|
||||
}
|
||||
endloop:
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
;;; Single-file/interactive setup; large applications should define an ASDF system instead
|
||||
(cl:require :asdf)
|
||||
(asdf:operate 'asdf:load-op :clx)
|
||||
(cl:defpackage #:rc-xlib-window
|
||||
(:use #:cl #:xlib))
|
||||
(cl:in-package #:rc-xlib-window)
|
||||
|
||||
(let ((display (open-default-display)))
|
||||
(unwind-protect
|
||||
(let* ((window (create-window :parent (screen-root (display-default-screen display))
|
||||
:x 10
|
||||
:y 10
|
||||
:width 100
|
||||
:height 100
|
||||
:event-mask '(:exposure :key-press)))
|
||||
(gc (create-gcontext :drawable window)))
|
||||
(map-window window)
|
||||
(event-case (display :discard-p t)
|
||||
(exposure ()
|
||||
(draw-rectangle window gc 20 20 10 10 t)
|
||||
(draw-glyphs window gc 10 40 "Hello, World!")
|
||||
nil #| continue receiving events |#)
|
||||
(key-press ()
|
||||
t #| non-nil result signals event-case to exit |#))))
|
||||
(close-display display))
|
||||
2
Task/Window-creation-X11/GUISS/window-creation-x11.guiss
Normal file
2
Task/Window-creation-X11/GUISS/window-creation-x11.guiss
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Start,Programs,Applications,Editors,Leafpad,Textbox,
|
||||
Type:[openbox]Hello World[pling][closebox]
|
||||
60
Task/Window-creation-X11/Go/window-creation-x11.go
Normal file
60
Task/Window-creation-X11/Go/window-creation-x11.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"code.google.com/p/x-go-binding/xgb"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c, err := xgb.Dial("")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
strBytes := []byte("Hello XGB!")
|
||||
rectangles := []xgb.Rectangle{{40, 40, 20, 20}}
|
||||
|
||||
// get the first screen
|
||||
s := c.DefaultScreen()
|
||||
|
||||
// root window
|
||||
win := s.Root
|
||||
|
||||
// create black (foreground) graphic context
|
||||
fg := c.NewId()
|
||||
mask := uint32(xgb.GCForeground | xgb.GCGraphicsExposures)
|
||||
values := []uint32{s.BlackPixel, 0}
|
||||
c.CreateGC(fg, win, mask, values)
|
||||
|
||||
// create white (background) graphic context
|
||||
bg := c.NewId()
|
||||
mask = uint32(xgb.GCBackground | xgb.GCGraphicsExposures)
|
||||
values[0] = s.WhitePixel // (values[1] still 0)
|
||||
c.CreateGC(bg, win, mask, values)
|
||||
|
||||
// create the window
|
||||
win = c.NewId()
|
||||
mask = xgb.CWBackPixel | xgb.CWEventMask
|
||||
// values[0] still s.WhitePixel
|
||||
values[1] = xgb.EventMaskExposure | xgb.EventMaskKeyPress
|
||||
c.CreateWindow(0, win, s.Root, 0, 0, 150, 150, 10,
|
||||
xgb.WindowClassInputOutput, s.RootVisual, mask, values)
|
||||
c.MapWindow(win)
|
||||
|
||||
for {
|
||||
event, err := c.WaitForEvent()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
switch event.(type) {
|
||||
case xgb.ExposeEvent:
|
||||
c.PolyRectangle(win, fg, rectangles)
|
||||
c.ImageText8(win, bg, 20, 20, strBytes)
|
||||
case xgb.KeyPressEvent:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Task/Window-creation-X11/Haskell/window-creation-x11.hs
Normal file
22
Task/Window-creation-X11/Haskell/window-creation-x11.hs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import Graphics.X11.Xlib
|
||||
import Control.Concurrent (threadDelay)
|
||||
|
||||
main = do
|
||||
display <- openDisplay ""
|
||||
let defScr = defaultScreen display
|
||||
rw <- rootWindow display defScr
|
||||
|
||||
xwin <- createSimpleWindow display rw
|
||||
0 0 400 200 1
|
||||
(blackPixel display defScr)
|
||||
(whitePixel display defScr)
|
||||
|
||||
setTextProperty display xwin "Rosetta Code: X11 simple window" wM_NAME
|
||||
|
||||
mapWindow display xwin
|
||||
|
||||
sync display False
|
||||
threadDelay (5000000)
|
||||
|
||||
destroyWindow display xwin
|
||||
closeDisplay display
|
||||
7
Task/Window-creation-X11/Icon/window-creation-x11.icon
Normal file
7
Task/Window-creation-X11/Icon/window-creation-x11.icon
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
procedure main()
|
||||
W1 := open("X-Window","g","size=250,250","bg=black","fg=red") | stop("unable to open window")
|
||||
FillRectangle(W1,50,50,150,150)
|
||||
WDone(W1)
|
||||
end
|
||||
|
||||
link graphics
|
||||
21
Task/Window-creation-X11/Java/window-creation-x11-1.java
Normal file
21
Task/Window-creation-X11/Java/window-creation-x11-1.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import javax.swing.JFrame;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class WindowExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Runnable runnable = new Runnable() {
|
||||
public void run() {
|
||||
createAndShow();
|
||||
}
|
||||
};
|
||||
SwingUtilities.invokeLater(runnable);
|
||||
}
|
||||
|
||||
static void createAndShow() {
|
||||
JFrame frame = new JFrame("Hello World");
|
||||
frame.setSize(640,480);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
27
Task/Window-creation-X11/Java/window-creation-x11-2.java
Normal file
27
Task/Window-creation-X11/Java/window-creation-x11-2.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.geom.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class WindowExample extends JApplet {
|
||||
public void paint(Graphics g) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
|
||||
g2.setStroke(new BasicStroke(2.0f));
|
||||
g2.drawString("Hello java", 20, 20);
|
||||
g2.setPaint(Color.blue);
|
||||
g2.draw(new Rectangle2D.Double(40, 40, 20, 20));
|
||||
}
|
||||
|
||||
public static void main(String s[]) {
|
||||
JFrame f = new JFrame("ShapesDemo2D");
|
||||
f.addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent e) {System.exit(0);}
|
||||
});
|
||||
JApplet applet = new ShapesDemo2D();
|
||||
f.getContentPane().add("Center", applet);
|
||||
f.pack();
|
||||
f.setSize(new Dimension(150, 150));
|
||||
f.setVisible(true);
|
||||
}
|
||||
}
|
||||
24
Task/Window-creation-X11/OCaml/window-creation-x11.ocaml
Normal file
24
Task/Window-creation-X11/OCaml/window-creation-x11.ocaml
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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 [ExposureMask; KeyPressMask];
|
||||
xMapWindow d w;
|
||||
|
||||
let msg = "Hello, World!" in
|
||||
|
||||
let rec main_loop() =
|
||||
match xEventType(xNextEventFun d) with
|
||||
| Expose ->
|
||||
xFillRectangle d w (xDefaultGC d s) 20 20 10 10;
|
||||
xDrawString d w (xDefaultGC d s) 10 50 msg;
|
||||
main_loop()
|
||||
| KeyPress -> () (* exit main loop *)
|
||||
| _ -> main_loop()
|
||||
in
|
||||
main_loop();
|
||||
xCloseDisplay d;
|
||||
;;
|
||||
45
Task/Window-creation-X11/Perl/window-creation-x11.pl
Normal file
45
Task/Window-creation-X11/Perl/window-creation-x11.pl
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/perl -w
|
||||
use strict;
|
||||
use X11::Protocol;
|
||||
|
||||
my $X = X11::Protocol->new;
|
||||
|
||||
my $window = $X->new_rsrc;
|
||||
$X->CreateWindow ($window,
|
||||
$X->root, # parent window
|
||||
'InputOutput', # class
|
||||
0, # depth, copy from parent
|
||||
0, # visual, copy from parent
|
||||
0,0, # X,Y (window manager will override)
|
||||
300,100, # width,height
|
||||
0, # border width
|
||||
background_pixel => $X->black_pixel,
|
||||
event_mask => $X->pack_event_mask('Exposure',
|
||||
'ButtonPress'),
|
||||
);
|
||||
|
||||
my $gc = $X->new_rsrc;
|
||||
$X->CreateGC ($gc, $window,
|
||||
foreground => $X->white_pixel);
|
||||
|
||||
$X->{'event_handler'} = sub {
|
||||
my %event = @_;
|
||||
my $event_name = $event{'name'};
|
||||
|
||||
if ($event_name eq 'Expose') {
|
||||
$X->PolyRectangle ($window, $gc, [ 10,10, # x,y top-left corner
|
||||
30,20 ]); # width,height
|
||||
$X->PolyText8 ($window, $gc,
|
||||
10, 55, # X,Y of text baseline
|
||||
[ 0, # delta X
|
||||
'Hello ... click mouse button to exit.' ]);
|
||||
|
||||
} elsif ($event_name eq 'ButtonPress') {
|
||||
exit 0;
|
||||
}
|
||||
};
|
||||
|
||||
$X->MapWindow ($window);
|
||||
for (;;) {
|
||||
$X->handle_input;
|
||||
}
|
||||
48
Task/Window-creation-X11/PicoLisp/window-creation-x11.l
Normal file
48
Task/Window-creation-X11/PicoLisp/window-creation-x11.l
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
|
||||
|
||||
(load "@lib/misc.l" "@lib/gcc.l")
|
||||
|
||||
(gcc "x11" '("-lX11") 'simpleWin)
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
any simpleWin(any ex) {
|
||||
any x = cdr(ex);
|
||||
int dx, dy;
|
||||
Display *disp;
|
||||
int scrn;
|
||||
Window win;
|
||||
XEvent ev;
|
||||
|
||||
x = cdr(ex), dx = (int)evCnt(ex,x);
|
||||
x = cdr(x), dy = (int)evCnt(ex,x);
|
||||
x = evSym(cdr(x));
|
||||
if (disp = XOpenDisplay(NULL)) {
|
||||
char msg[bufSize(x)];
|
||||
|
||||
bufString(x, msg);
|
||||
scrn = DefaultScreen(disp);
|
||||
win = XCreateSimpleWindow(disp, RootWindow(disp,scrn), 0, 0, dx, dy,
|
||||
1, BlackPixel(disp,scrn), WhitePixel(disp,scrn) );
|
||||
XSelectInput(disp, win, ExposureMask | KeyPressMask | ButtonPressMask);
|
||||
XMapWindow(disp, win);
|
||||
for (;;) {
|
||||
XNextEvent(disp, &ev);
|
||||
switch (ev.type) {
|
||||
case Expose:
|
||||
XDrawRectangle(disp, win, DefaultGC(disp, scrn), 10, 10, dx-20, dy-20);
|
||||
XDrawString(disp, win, DefaultGC(disp, scrn), 30, 40, msg, strlen(msg));
|
||||
break;
|
||||
case KeyPress:
|
||||
case ButtonPress:
|
||||
XCloseDisplay(disp);
|
||||
return Nil;
|
||||
}
|
||||
}
|
||||
}
|
||||
return mkStr("Can't open Display");
|
||||
}
|
||||
/**/
|
||||
|
||||
(simpleWin 300 200 "Hello World")
|
||||
(bye)
|
||||
34
Task/Window-creation-X11/Python/window-creation-x11-1.py
Normal file
34
Task/Window-creation-X11/Python/window-creation-x11-1.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from Xlib import X, display
|
||||
|
||||
class Window:
|
||||
def __init__(self, display, msg):
|
||||
self.display = display
|
||||
self.msg = msg
|
||||
|
||||
self.screen = self.display.screen()
|
||||
self.window = self.screen.root.create_window(
|
||||
10, 10, 100, 100, 1,
|
||||
self.screen.root_depth,
|
||||
background_pixel=self.screen.white_pixel,
|
||||
event_mask=X.ExposureMask | X.KeyPressMask,
|
||||
)
|
||||
self.gc = self.window.create_gc(
|
||||
foreground = self.screen.black_pixel,
|
||||
background = self.screen.white_pixel,
|
||||
)
|
||||
|
||||
self.window.map()
|
||||
|
||||
def loop(self):
|
||||
while True:
|
||||
e = self.display.next_event()
|
||||
|
||||
if e.type == X.Expose:
|
||||
self.window.fill_rectangle(self.gc, 20, 20, 10, 10)
|
||||
self.window.draw_text(self.gc, 10, 50, self.msg)
|
||||
elif e.type == X.KeyPress:
|
||||
raise SystemExit
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Window(display.Display(), "Hello, World!").loop()
|
||||
43
Task/Window-creation-X11/Python/window-creation-x11-2.py
Normal file
43
Task/Window-creation-X11/Python/window-creation-x11-2.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import xcb
|
||||
from xcb.xproto import *
|
||||
import xcb.render
|
||||
|
||||
def main():
|
||||
conn = xcb.connect()
|
||||
conn.render = conn(xcb.render.key)
|
||||
|
||||
setup = conn.get_setup()
|
||||
root = setup.roots[0].root
|
||||
depth = setup.roots[0].root_depth
|
||||
visual = setup.roots[0].root_visual
|
||||
white = setup.roots[0].white_pixel
|
||||
|
||||
window = conn.generate_id()
|
||||
conn.core.CreateWindow(depth, window, root,
|
||||
0, 0, 640, 480, 0,
|
||||
WindowClass.InputOutput,
|
||||
visual,
|
||||
CW.BackPixel | CW.EventMask,
|
||||
[ white, EventMask.Exposure |
|
||||
EventMask.KeyPress ])
|
||||
|
||||
conn.core.MapWindow(window)
|
||||
conn.flush()
|
||||
|
||||
while True:
|
||||
event = conn.wait_for_event()
|
||||
|
||||
if isinstance(event, ExposeEvent):
|
||||
color = (0, 0, 65535, 65535)
|
||||
rectangle = (20, 20, 40, 40)
|
||||
# TODO, fixme:
|
||||
# I haven't been able to find what I should put for the parameter "op"
|
||||
# conn.render.FillRectangles(op, window, color, 1, rectangle)
|
||||
conn.flush()
|
||||
|
||||
elif isinstance(event, KeyPressEvent):
|
||||
break
|
||||
|
||||
conn.disconnect()
|
||||
|
||||
main()
|
||||
65
Task/Window-creation-X11/Tcl/window-creation-x11-1.tcl
Normal file
65
Task/Window-creation-X11/Tcl/window-creation-x11-1.tcl
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package provide xlib 1
|
||||
package require critcl
|
||||
|
||||
critcl::clibraries -L/usr/X11/lib -lX11
|
||||
critcl::ccode {
|
||||
#include <X11/Xlib.h>
|
||||
static Display *d;
|
||||
static GC gc;
|
||||
}
|
||||
|
||||
# Display connection functions
|
||||
critcl::cproc XOpenDisplay {Tcl_Interp* interp char* name} ok {
|
||||
d = XOpenDisplay(name[0] ? name : NULL);
|
||||
if (d == NULL) {
|
||||
Tcl_AppendResult(interp, "cannot open display", NULL);
|
||||
return TCL_ERROR;
|
||||
}
|
||||
gc = DefaultGC(d, DefaultScreen(d));
|
||||
return TCL_OK;
|
||||
}
|
||||
critcl::cproc XCloseDisplay {} void {
|
||||
XCloseDisplay(d);
|
||||
}
|
||||
|
||||
# Basic window functions
|
||||
critcl::cproc XCreateSimpleWindow {
|
||||
int x int y int width int height int events
|
||||
} int {
|
||||
int s = DefaultScreen(d);
|
||||
Window w = XCreateSimpleWindow(d, RootWindow(d,s), x, y, width, height, 0,
|
||||
BlackPixel(d,s), WhitePixel(d,s));
|
||||
XSelectInput(d, w, ExposureMask | events);
|
||||
return (int) w;
|
||||
}
|
||||
critcl::cproc XDestroyWindow {int w} void {
|
||||
XDestroyWindow(d, (Window) w);
|
||||
}
|
||||
critcl::cproc XMapWindow {int w} void {
|
||||
XMapWindow(d, (Window) w);
|
||||
}
|
||||
critcl::cproc XUnmapWindow {int w} void {
|
||||
XUnmapWindow(d, (Window) w);
|
||||
}
|
||||
|
||||
# Event receiver
|
||||
critcl::cproc XNextEvent {Tcl_Interp* interp} char* {
|
||||
XEvent e;
|
||||
XNextEvent(d, &e);
|
||||
switch (e.type) {
|
||||
case Expose: return "type expose";
|
||||
case KeyPress: return "type key";
|
||||
/* etc. This is a cheap hack version. */
|
||||
default: return "type ?";
|
||||
}
|
||||
}
|
||||
|
||||
# Painting functions
|
||||
critcl::cproc XFillRectangle {int w int x int y int width int height} void {
|
||||
XFillRectangle(d, (Window)w, gc, x, y, width, height);
|
||||
}
|
||||
critcl::cproc XDrawString {int w int x int y Tcl_Obj* msg} void {
|
||||
int len;
|
||||
const char *str = Tcl_GetStringFromObj(msg, &len);
|
||||
XDrawString(d, (Window)w, gc, x, y, str, len);
|
||||
}
|
||||
11
Task/Window-creation-X11/Tcl/window-creation-x11-2.tcl
Normal file
11
Task/Window-creation-X11/Tcl/window-creation-x11-2.tcl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package require xlib
|
||||
|
||||
XOpenDisplay {}
|
||||
set w [XCreateSimpleWindow 10 10 100 100 1]
|
||||
XMapWindow $w
|
||||
while {[lindex [XNextEvent] 0] == "expose"} {
|
||||
XFillRectangle $w 20 20 10 10
|
||||
XDrawString $w 10 50 "Hello, World!"
|
||||
}
|
||||
XDestroyWindow $w
|
||||
XCloseDisplay
|
||||
53
Task/Window-creation-X11/Tcl/window-creation-x11-3.tcl
Normal file
53
Task/Window-creation-X11/Tcl/window-creation-x11-3.tcl
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package require TclOO
|
||||
package provide x11 1
|
||||
|
||||
namespace eval ::x {
|
||||
namespace export {[a-z]*}
|
||||
namespace ensemble create
|
||||
variable mask
|
||||
array set mask {
|
||||
KeyPress 1
|
||||
KeyRelease 2
|
||||
ButtonPress 4
|
||||
ButtonRelease 8
|
||||
}
|
||||
|
||||
proc display {script} {
|
||||
XOpenDisplay {}
|
||||
catch {uplevel 1 $script} msg opts
|
||||
XCloseDisplay
|
||||
return -options $opts $msg
|
||||
}
|
||||
proc eventloop {var handlers} {
|
||||
upvar 1 $var v
|
||||
while 1 {
|
||||
set v [XNextEvent]
|
||||
uplevel 1 [list switch [dict get $v type] $handlers]
|
||||
}
|
||||
}
|
||||
|
||||
oo::class create window {
|
||||
variable w
|
||||
constructor {x y width height events} {
|
||||
set m 0
|
||||
variable ::x::mask
|
||||
foreach e $events {catch {incr m $mask($e)}}
|
||||
set w [XCreateSimpleWindow $x $y $width $height $m]
|
||||
}
|
||||
method map {} {
|
||||
XMapWindow $w
|
||||
}
|
||||
method unmap {} {
|
||||
XUnmapWindow $w
|
||||
}
|
||||
method fill {x y width height} {
|
||||
XFillRectangle $w $x $y $width $height
|
||||
}
|
||||
method text {x y string} {
|
||||
XDrawString $w $x $y $string
|
||||
}
|
||||
destructor {
|
||||
XDestroyWindow $w
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Task/Window-creation-X11/Tcl/window-creation-x11-4.tcl
Normal file
21
Task/Window-creation-X11/Tcl/window-creation-x11-4.tcl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package require x11
|
||||
|
||||
# With a display connection open, create and map a window
|
||||
x display {
|
||||
set w [x window new 10 10 100 100 KeyPress]
|
||||
$w map
|
||||
|
||||
x eventloop ev {
|
||||
expose {
|
||||
# Paint the window
|
||||
$w fill 20 20 10 10
|
||||
$w text 10 50 "Hello, World!"
|
||||
}
|
||||
key {
|
||||
# Quit the event loop
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
$w destroy
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue