Window creation/X11: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Tcl: Added implementation)
(→‎{{header|Tcl}}: Strip waste space)
Line 102: Line 102:
{{libheader|Tk}}
{{libheader|Tk}}
With a bit of added space between the elements to make them more obvious...
With a bit of added space between the elements to make them more obvious...
<lang Tcl>
<lang Tcl>package require Tk
package require Tk
label .l -text "Hello World" -padx 5 -pady 5 -relief solid
label .l -text "Hello World" -padx 5 -pady 5 -relief solid
pack .l -padx 5 -pady 5
pack .l -padx 5 -pady 5</lang>
</lang>

Revision as of 09:43, 8 May 2009

Task
Window creation/X11
You are encouraged to solve this task according to the task description, using any language you may know.

Create a simple Xlib application that draws a box and "Hello World" in a window

ALGOL 68

Works with: ALGOL 68G version tested with release mk15-0.8b.fc9.i386

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.

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)

C

Compile with:

  • gcc -L/usr/X11R6/lib -lX11 hello-x.c -o hello-x

<lang c>#include <X11/Xlib.h>

  1. include <stdio.h>
  2. include <stdlib.h>
  3. 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;

} </lang>

OCaml

Library: OCaml-Xlib

execute as a script with:

ocaml -I +Xlib Xlib.cma script.ml

or compile to native code:

ocamlopt -I +Xlib Xlib.cmxa prog.ml -o prog

or to make a standalone script add these lines at the beginning of the file:

#!/usr/bin/env ocaml
#directory "+Xlib"
#load "Xlib.cma"

<lang ocaml>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;
</lang>

Tcl

Library: Tk

With a bit of added space between the elements to make them more obvious... <lang Tcl>package require Tk label .l -text "Hello World" -padx 5 -pady 5 -relief solid pack .l -padx 5 -pady 5</lang>