summaryrefslogtreecommitdiff
path: root/examples/viewer-x.c
diff options
context:
space:
mode:
authorBehdad Esfahbod <behdad@gnome.org>2006-02-01 22:10:11 +0000
committerBehdad Esfahbod <behdad@src.gnome.org>2006-02-01 22:10:11 +0000
commit9ca400bee42e2ad658681c8d0161ec18492964ef (patch)
tree350c697c7ee55429830a84933d58f7ad6b4d29ca /examples/viewer-x.c
parenteda8c1ea592c855cc675d7a37dd5b6937c5e996f (diff)
downloadpango-9ca400bee42e2ad658681c8d0161ec18492964ef.tar.gz
Driver for X-based viewer. Used by pangocairo-view and pangoxft-view
2006-02-01 Behdad Esfahbod <behdad@gnome.org> * examples/viewer-x.c, examples/viewer-x.h: Driver for X-based viewer. Used by pangocairo-view and pangoxft-view currently. * examples/Makefile.am, examples/cairoview.c, examples/xftview.c, examples/pangoft2topgm.c, examples/renderdemo.h, examples/renderdemo.c: Adjust to the above change.
Diffstat (limited to 'examples/viewer-x.c')
-rw-r--r--examples/viewer-x.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/examples/viewer-x.c b/examples/viewer-x.c
new file mode 100644
index 00000000..6546612b
--- /dev/null
+++ b/examples/viewer-x.c
@@ -0,0 +1,169 @@
+/* viewer-x.c: Common code X-based rendering demos
+ *
+ * Copyright (C) 1999,2004,2005 Red Hat, Inc.
+ * Copyright (C) 2001 Sun Microsystems
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "viewer-x.h"
+#include "renderdemo.h"
+
+/* initialized by main() */
+static Display *display;
+static int screen;
+static Window window;
+static Pixmap pixmap;
+
+/* initialized by do_init() */
+static PangoContext *context;
+static int width, height;
+
+/* runtime stuff */
+static Region update_region = NULL;
+static gboolean show_borders;
+
+static void
+update (void)
+{
+ GC gc;
+ XRectangle extents;
+ int width, height;
+
+ XClipBox (update_region, &extents);
+
+ width = extents.width;
+ height = extents.height;
+
+ gc = XCreateGC (display, pixmap, 0, NULL);
+
+ XCopyArea (display, pixmap, window, gc,
+ extents.x, extents.y,
+ extents.width, extents.height,
+ extents.x, extents.y);
+
+ XFreeGC (display, gc);
+
+ XDestroyRegion (update_region);
+ update_region = NULL;
+}
+
+static void
+expose (XExposeEvent *xev)
+{
+ XRectangle r;
+
+ if (!update_region)
+ update_region = XCreateRegion ();
+
+ r.x = xev->x;
+ r.y = xev->y;
+ r.width = xev->width;
+ r.height = xev->height;
+
+ XUnionRectWithRegion (&r, update_region, update_region);
+}
+
+int
+main (int argc, char **argv)
+{
+ XEvent xev;
+ unsigned long bg;
+ XSizeHints size_hints;
+ unsigned int quit_keycode;
+ unsigned int borders_keycode;
+
+ g_type_init();
+
+ parse_options (argc, argv);
+
+ display = XOpenDisplay (NULL);
+ if (!display)
+ fail ("Cannot open display %s\n", XDisplayName (NULL));
+ screen = DefaultScreen (display);
+
+ do_init (display, screen, &context, &width, &height);
+
+ bg = WhitePixel (display, screen);
+ window = XCreateSimpleWindow (display, DefaultRootWindow (display),
+ 0, 0, width, height, 0,
+ bg, bg);
+ pixmap = XCreatePixmap (display, window, width, height,
+ DefaultDepth (display, screen));
+ XSelectInput (display, window, ExposureMask | KeyPressMask);
+
+ XMapWindow (display, window);
+ XmbSetWMProperties (display, window,
+ get_options_string (),
+ NULL, NULL, 0, NULL, NULL, NULL);
+
+ memset ((char *)&size_hints, 0, sizeof (XSizeHints));
+ size_hints.flags = PSize | PMaxSize;
+ size_hints.width = width; size_hints.height = height; /* for compat only */
+ size_hints.max_width = width; size_hints.max_height = height;
+
+ XSetWMNormalHints (display, window, &size_hints);
+
+ borders_keycode = XKeysymToKeycode(display, 'B');
+ quit_keycode = XKeysymToKeycode(display, 'Q');
+
+ do_render (display, screen, window, pixmap, context, width, height, show_borders);
+
+ while (1)
+ {
+ if (!XPending (display) && update_region)
+ update ();
+
+ XNextEvent (display, &xev);
+ switch (xev.xany.type) {
+ case KeyPress:
+ if (xev.xkey.keycode == quit_keycode)
+ goto done;
+ else if (xev.xkey.keycode == borders_keycode)
+ {
+ XRectangle r;
+ show_borders = !show_borders;
+ do_render (display, screen, window, pixmap, context, width, height, show_borders);
+
+ if (!update_region)
+ update_region = XCreateRegion ();
+
+ r.x = 0;
+ r.y = 0;
+ r.width = width;
+ r.height = height;
+ XUnionRectWithRegion (&r, update_region, update_region);
+ }
+ break;
+ case Expose:
+ expose (&xev.xexpose);
+ break;
+ }
+ }
+
+done:
+
+ g_object_unref (context);
+ XFreePixmap (display, pixmap);
+ finalize ();
+
+ return 0;
+}