1 ;;; xlib-hello.el --- Hello world example using new xlib.
3 ;; Copyright (C) 2003-2005 by XWEM Org.
5 ;; Author: Eric M. Ludlam <zappo@gnu.ai.mit.edu>
6 ;; Modified: Zajcev Evgeny <zevlg@yandex.ru>
8 ;; X-CVS: $Id: xlib-hello.el,v 1.8 2005-04-04 19:55:28 lg Exp $
10 ;; This file is part of XWEM.
12 ;; XWEM is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XWEM is distributed in the hope that it will be useful, but WITHOUT
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 ;; License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 ;;; Synched up with: Not in FSF
31 ;; This program very close to first hello program that Eric Ludlam
32 ;; wrote at least arcs, text and lines drawing did not changed, only
35 ;; Many hard codes, do not use as real example, this hello world
36 ;; application needed only to show that xlib works, not as
37 ;; recommendation how to write applications that uses xlib.
43 (defconst XH-event-mask
44 (Xmask-or XM-Exposure XM-StructureNotify XM-KeyPress XM-KeyRelease
45 XM-ButtonPress XM-ButtonRelease))
51 (defvar XH-buttons nil "List of buttons.")
52 (defvar XH-close-buttons nil "List of buttons to dismiss.")
54 (defun XX-Hello (dname)
55 (interactive "sDisplay: ")
57 (let ((xdpy (XOpenDisplay dname)))
58 (if (not (X-Dpy-p xdpy))
59 (message "Can't open display '%s'" dname)
61 (setf (X-Dpy-log-buffer xdpy) "Xlog")
62 (let ((w (XCreateWindow xdpy nil 20 20 100 100 4 nil nil nil
63 (make-X-Attr :override-redirect t
64 :background-pixel (XWhitePixel xdpy)
65 :border-pixel (XBlackPixel xdpy)
66 :event-mask XH-event-mask)))
67 (cmap (XDefaultColormap xdpy))
68 (co (make-X-Color :dpy xdpy)))
71 (message "Can't create window.")
74 (XAllocNamedColor xdpy cmap "Red" co)
75 (setq XH-gc-1 (XCreateGC xdpy w (make-X-Gc :dpy xdpy
76 :id (X-Dpy-get-id xdpy)
78 :background (XWhitePixel xdpy)
79 :line-style X-LineSolid
81 (XAllocNamedColor xdpy cmap "Green" co)
82 (setq XH-gc-2 (XCreateGC xdpy w (make-X-Gc :dpy xdpy
83 :id (X-Dpy-get-id xdpy)
85 :background (XWhitePixel xdpy)
86 :line-style X-LineDoubleDash
89 ;; Setup events handling
90 (X-Win-EventHandler-add w 'XH-events-handler)
95 (defun XH-button-press (xdpy win xev)
97 (let ((x (X-Event-xbutton-event-x xev))
98 (y (X-Event-xbutton-event-y xev)))
100 (<= x (+ 20 (X-Image-width (nth 1 XH-buttons))))
102 (<= y (+ 20 (X-Image-height (nth 1 XH-buttons)))))
103 (XH-show-button xdpy win 20 20 1)
104 (message "Hellow world!"))
107 (<= x (+ 20 (X-Image-width (nth 1 XH-close-buttons))))
109 (<= y (+ 60 (X-Image-height (nth 1 XH-close-buttons)))))
111 (XH-show-close-button xdpy win 20 60 1)
112 (message "XH: Exiting ..")
113 (XSelectInput xdpy win 0)
114 (XDestroyWindow xdpy win))
118 (defun XH-button-release (xdpy win xev)
119 "Button release event."
120 (XH-show-button xdpy win 20 20 0))
122 (defun XH-expose (xdpy w xev)
124 (XDrawLine xdpy w XH-gc-2 5 5 100 50)
125 (XDrawPoint xdpy w XH-gc-1 20 5)
126 (XFillRectangle xdpy w XH-gc-2 2 38 38 15)
127 (XDrawRectangle xdpy w XH-gc-1 2 38 38 15)
128 (XDrawString xdpy w XH-gc-1 5 50 "HELLO!")
129 (XDrawSegments xdpy w XH-gc-2 (list (cons '(100 . 0) '(50 . 10))
130 (cons '(100 . 100) '(50 . 90))))
131 (XDrawArc xdpy w XH-gc-1 50 50 20 20 0 360)
132 (XFillArc xdpy w XH-gc-2 55 55 10 10 0 360)
134 ;; Show 'Press me' button
135 (XH-show-button xdpy w 20 20 0)
137 ;; Show 'Dismiss' button
138 (XH-show-close-button xdpy w 20 60 0)
141 (defun XH-events-handler (xdpy w xev)
142 "X hello events dispatcher."
145 (message "XH got Exposure event ..")
146 (XH-expose xdpy w xev))
149 (message "XH got KeyPress event .."))
152 (message "XH got KeyRelease event .."))
155 (message "XH got ButtonPress event ..")
156 (XH-button-press xdpy w xev))
159 (message "XH got ButtonRelease event ..")
160 (XH-button-release xdpy w xev))
166 XH-close-buttons nil))
168 (t (message "XH Got event: %d" (X-Event-type xev)))))
170 (defun XH-show-button (dpy win x y &optional state)
171 "Show 'Press Me' button."
174 (require 'xpm-button)
175 (let ((buts (xpm-button-create "Press Me" 2 "green4" "#a0d0a0")))
177 (mapcar (lambda (but)
178 (X:xpm-img-from-data dpy (aref but 2)))
184 (XImagePut dpy (XDefaultGC dpy) win x y (nth state XH-buttons)))
186 (defun XH-show-close-button (dpy win x y &optional state)
187 "Show 'Dismiss' button."
188 (unless XH-close-buttons
189 (require 'xpm-button)
190 (let ((buts (xpm-button-create "Dismiss" 4 "Red4" "gray80")))
191 (setq XH-close-buttons
192 (mapcar (lambda (but)
193 (X:xpm-img-from-data dpy (aref but 2)))
199 (XImagePut dpy (XDefaultGC dpy) win x y (nth state XH-close-buttons)))
202 (provide 'xlib-hello)
204 ;;; xlib-hello.el ends here