1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gdkprivate-x11.h"
29 static void gdk_x11_drawable_destroy (GdkDrawable *drawable);
31 static void gdk_x11_draw_rectangle (GdkDrawable *drawable,
38 static void gdk_x11_draw_arc (GdkDrawable *drawable,
47 static void gdk_x11_draw_polygon (GdkDrawable *drawable,
52 static void gdk_x11_draw_text (GdkDrawable *drawable,
59 static void gdk_x11_draw_text_wc (GdkDrawable *drawable,
66 static void gdk_x11_draw_drawable (GdkDrawable *drawable,
75 static void gdk_x11_draw_points (GdkDrawable *drawable,
79 static void gdk_x11_draw_segments (GdkDrawable *drawable,
83 static void gdk_x11_draw_lines (GdkDrawable *drawable,
89 GdkDrawableClass _gdk_x11_drawable_class = {
90 gdk_x11_drawable_destroy,
92 gdk_x11_draw_rectangle,
97 gdk_x11_draw_drawable,
99 gdk_x11_draw_segments,
103 /*****************************************************
104 * X11 specific implementations of generic functions *
105 *****************************************************/
108 gdk_drawable_get_colormap (GdkDrawable *drawable)
110 GdkDrawablePrivate *drawable_private;
111 XWindowAttributes window_attributes;
113 g_return_val_if_fail (drawable != NULL, NULL);
114 drawable_private = (GdkDrawablePrivate*) drawable;
116 if (!GDK_DRAWABLE_DESTROYED (drawable))
118 if (drawable_private->colormap == NULL &&
119 GDK_IS_WINDOW (drawable))
121 XGetWindowAttributes (GDK_DRAWABLE_XDISPLAY (drawable),
122 GDK_DRAWABLE_XID (drawable),
124 drawable_private->colormap = gdk_colormap_lookup (window_attributes.colormap);
127 return drawable_private->colormap;
134 gdk_drawable_set_colormap (GdkDrawable *drawable,
135 GdkColormap *colormap)
137 GdkDrawablePrivate *drawable_private;
138 GdkColormapPrivateX *colormap_private;
140 g_return_if_fail (drawable != NULL);
141 g_return_if_fail (colormap != NULL);
143 drawable_private = (GdkDrawablePrivate *)drawable;
144 colormap_private = (GdkColormapPrivateX *)colormap;
146 if (!GDK_DRAWABLE_DESTROYED (drawable))
148 if (GDK_IS_WINDOW (drawable))
150 g_return_if_fail (colormap_private->base.visual !=
151 ((GdkColormapPrivate *)(drawable_private->colormap))->visual);
153 XSetWindowColormap (GDK_DRAWABLE_XDISPLAY (drawable),
154 GDK_DRAWABLE_XID (drawable),
155 colormap_private->xcolormap);
158 if (drawable_private->colormap)
159 gdk_colormap_unref (drawable_private->colormap);
160 drawable_private->colormap = colormap;
161 gdk_colormap_ref (drawable_private->colormap);
163 if (GDK_IS_WINDOW (drawable) &&
164 drawable_private->window_type != GDK_WINDOW_TOPLEVEL)
165 gdk_window_add_colormap_windows (drawable);
172 gdk_x11_drawable_destroy (GdkDrawable *drawable)
178 gdk_x11_draw_rectangle (GdkDrawable *drawable,
187 XFillRectangle (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
188 GDK_GC_GET_XGC (gc), x, y, width, height);
190 XDrawRectangle (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
191 GDK_GC_GET_XGC (gc), x, y, width, height);
195 gdk_x11_draw_arc (GdkDrawable *drawable,
206 XFillArc (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
207 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
209 XDrawArc (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
210 GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
214 gdk_x11_draw_polygon (GdkDrawable *drawable,
224 (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
226 tmp_npoints = npoints + 1;
227 tmp_points = g_new (XPoint, tmp_npoints);
228 tmp_points[npoints].x = points[0].x;
229 tmp_points[npoints].y = points[0].y;
233 tmp_npoints = npoints;
234 tmp_points = g_new (XPoint, tmp_npoints);
237 for (i=0; i<npoints; i++)
239 tmp_points[i].x = points[i].x;
240 tmp_points[i].y = points[i].y;
244 XFillPolygon (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
245 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
247 XDrawLines (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
248 GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);
255 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
257 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
260 gdk_x11_draw_text (GdkDrawable *drawable,
268 if (font->type == GDK_FONT_FONT)
270 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
271 XSetFont(GDK_DRAWABLE_XDISPLAY (drawable), GDK_GC_GET_XGC (gc), xfont->fid);
272 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
274 XDrawString (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
275 GDK_GC_GET_XGC (gc), x, y, text, text_length);
279 XDrawString16 (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
280 GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
283 else if (font->type == GDK_FONT_FONTSET)
285 XFontSet fontset = (XFontSet) GDK_FONT_XFONT (font);
286 XmbDrawString (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
287 fontset, GDK_GC_GET_XGC (gc), x, y, text, text_length);
290 g_error("undefined font type\n");
294 gdk_x11_draw_text_wc (GdkDrawable *drawable,
299 const GdkWChar *text,
302 if (font->type == GDK_FONT_FONT)
304 XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
307 XSetFont(GDK_DRAWABLE_XDISPLAY (drawable), GDK_GC_GET_XGC (gc), xfont->fid);
308 text_8bit = g_new (gchar, text_length);
309 for (i=0; i<text_length; i++) text_8bit[i] = text[i];
310 XDrawString (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
311 GDK_GC_GET_XGC (gc), x, y, text_8bit, text_length);
314 else if (font->type == GDK_FONT_FONTSET)
316 if (sizeof(GdkWChar) == sizeof(wchar_t))
318 XwcDrawString (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
319 (XFontSet) GDK_FONT_XFONT (font),
320 GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
326 text_wchar = g_new (wchar_t, text_length);
327 for (i=0; i<text_length; i++) text_wchar[i] = text[i];
328 XwcDrawString (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
329 (XFontSet) GDK_FONT_XFONT (font),
330 GDK_GC_GET_XGC (gc), x, y, text_wchar, text_length);
335 g_error("undefined font type\n");
339 gdk_x11_draw_drawable (GdkDrawable *drawable,
349 int src_depth = gdk_drawable_get_depth (src);
350 int dest_depth = gdk_drawable_get_depth (drawable);
354 XCopyArea (GDK_DRAWABLE_XDISPLAY (drawable),
355 GDK_DRAWABLE_XID (src),
356 GDK_DRAWABLE_XID (drawable),
362 else if (dest_depth != 0 && src_depth == dest_depth)
364 XCopyArea (GDK_DRAWABLE_XDISPLAY (drawable),
365 GDK_DRAWABLE_XID (src),
366 GDK_DRAWABLE_XID (drawable),
373 g_warning ("Attempt to copy between drawables of mismatched depths!\n");
377 gdk_x11_draw_points (GdkDrawable *drawable,
382 /* We special-case npoints == 1, because X will merge multiple
383 * consecutive XDrawPoint requests into a PolyPoint request
387 XDrawPoint (GDK_DRAWABLE_XDISPLAY (drawable),
388 GDK_DRAWABLE_XID (drawable),
390 points[0].x, points[0].y);
395 XPoint *tmp_points = g_new (XPoint, npoints);
397 for (i=0; i<npoints; i++)
399 tmp_points[i].x = points[i].x;
400 tmp_points[i].y = points[i].y;
403 XDrawPoints (GDK_DRAWABLE_XDISPLAY (drawable),
404 GDK_DRAWABLE_XID (drawable),
415 gdk_x11_draw_segments (GdkDrawable *drawable,
420 /* We special-case nsegs == 1, because X will merge multiple
421 * consecutive XDrawLine requests into a PolySegment request
425 XDrawLine (GDK_DRAWABLE_XDISPLAY (drawable), GDK_DRAWABLE_XID (drawable),
426 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
427 segs[0].x2, segs[0].y2);
432 XSegment *tmp_segs = g_new (XSegment, nsegs);
434 for (i=0; i<nsegs; i++)
436 tmp_segs[i].x1 = segs[i].x1;
437 tmp_segs[i].x2 = segs[i].x2;
438 tmp_segs[i].y1 = segs[i].y1;
439 tmp_segs[i].y2 = segs[i].y2;
442 XDrawSegments (GDK_DRAWABLE_XDISPLAY (drawable),
443 GDK_DRAWABLE_XID (drawable),
452 gdk_x11_draw_lines (GdkDrawable *drawable,
458 XPoint *tmp_points = g_new (XPoint, npoints);
460 for (i=0; i<npoints; i++)
462 tmp_points[i].x = points[i].x;
463 tmp_points[i].y = points[i].y;
466 XDrawLines (GDK_DRAWABLE_XDISPLAY (drawable),
467 GDK_DRAWABLE_XID (drawable),