1 /* viewer-x.c: Common code for X-based viewers
3 * Copyright (C) 1999,2004,2005 Red Hat, Inc.
4 * Copyright (C) 2001 Sun Microsystems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "viewer-render.h"
28 x_view_init (gpointer instance,
29 const PangoViewer *klass G_GNUC_UNUSED)
31 XViewer *x = (XViewer *)instance;
33 x->display = XOpenDisplay (NULL);
35 fail ("Cannot open display %s", XDisplayName (NULL));
37 x->screen = DefaultScreen (x->display);
41 x_view_create (const PangoViewer *klass)
45 instance = g_slice_new (XViewer);
47 x_view_init (instance, klass);
53 x_view_destroy (gpointer instance)
55 XViewer *x = (XViewer *)instance;
57 XCloseDisplay (x->display);
59 g_slice_free (XViewer, instance);
63 x_view_create_surface (gpointer instance,
67 XViewer *x = (XViewer *) instance;
70 pixmap = XCreatePixmap (x->display, DefaultRootWindow (x->display), width, height,
71 DefaultDepth (x->display, x->screen));
73 return (gpointer) pixmap;
77 x_view_destroy_surface (gpointer instance,
80 XViewer *x = (XViewer *) instance;
81 Pixmap pixmap = (Pixmap) surface;
83 XFreePixmap (x->display, pixmap);
87 update (Display *display,
90 Region *update_region)
95 XClipBox (*update_region, &extents);
97 gc = XCreateGC (display, pixmap, 0, NULL);
99 XCopyArea (display, pixmap, window, gc,
100 extents.x, extents.y,
101 extents.width, extents.height,
102 extents.x, extents.y);
104 XFreeGC (display, gc);
106 XDestroyRegion (*update_region);
107 *update_region = NULL;
111 expose (XExposeEvent *xev,
112 Region *update_region)
117 *update_region = XCreateRegion ();
121 r.width = xev->width;
122 r.height = xev->height;
124 XUnionRectWithRegion (&r, *update_region, *update_region);
128 x_view_create_window (gpointer instance,
133 XViewer *x = (XViewer *) instance;
136 XSizeHints size_hints;
138 bg = WhitePixel (x->display, x->screen);
139 window = XCreateSimpleWindow (x->display, DefaultRootWindow (x->display),
140 0, 0, width, height, 0,
143 XSelectInput (x->display, window, ExposureMask | KeyPressMask);
145 XMapWindow (x->display, window);
146 XmbSetWMProperties (x->display, window,
148 NULL, NULL, 0, NULL, NULL, NULL);
150 memset ((char *)&size_hints, 0, sizeof (XSizeHints));
151 size_hints.flags = PSize | PMaxSize;
152 size_hints.width = width; size_hints.height = height; /* for compat only */
153 size_hints.max_width = width; size_hints.max_height = height;
155 XSetWMNormalHints (x->display, window, &size_hints);
157 return (gpointer) window;
161 x_view_destroy_window (gpointer instance,
164 XViewer *x = (XViewer *) instance;
165 Window win = (Window) window;
167 XDestroyWindow (x->display, win);
171 x_view_display (gpointer instance,
178 XViewer *x = (XViewer *) instance;
179 Pixmap pixmap = (Pixmap) surface;
180 Window window = (Window) win;
183 Region update_region;
184 unsigned int quit_keycode;
185 unsigned int annotate_keycode;
187 /* force a full redraw */
188 update_region = XCreateRegion ();
193 XUnionRectWithRegion (&r, update_region, update_region);
195 annotate_keycode = XKeysymToKeycode(x->display, 'B');
196 quit_keycode = XKeysymToKeycode(x->display, 'Q');
200 if (!XPending (x->display) && update_region)
201 update (x->display, pixmap, window, &update_region);
203 XNextEvent (x->display, &xev);
204 switch (xev.xany.type) {
206 if (xev.xkey.keycode == quit_keycode)
207 return GINT_TO_POINTER (-1);
208 else if (xev.xkey.keycode == annotate_keycode)
210 return GUINT_TO_POINTER (GPOINTER_TO_INT (state) + 1);
214 expose (&xev.xexpose, &update_region);
220 const PangoViewer x_viewer = {
227 x_view_create_surface,
228 x_view_destroy_surface,
231 x_view_create_window,
232 x_view_destroy_window,