1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2 
3    render.h for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6    Copyright (c) 2003        Derek Foreman
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    See the COPYING file for a copy of the GNU General Public License.
19 */
20 
21 #ifndef __render_h
22 #define __render_h
23 
24 #include <X11/Xlib.h> /* some platforms dont include this as needed for Xft */
25 #include <pango/pangoxft.h>
26 #include <glib.h>
27 
28 G_BEGIN_DECLS
29 
30 #include "obrender/geom.h"
31 #include "obrender/version.h"
32 
33 typedef union  _RrTextureData      RrTextureData;
34 typedef struct _RrAppearance       RrAppearance;
35 typedef struct _RrSurface          RrSurface;
36 typedef struct _RrFont             RrFont;
37 typedef struct _RrTexture          RrTexture;
38 typedef struct _RrTextureMask      RrTextureMask;
39 typedef struct _RrTextureRGBA      RrTextureRGBA;
40 typedef struct _RrTextureImage     RrTextureImage;
41 typedef struct _RrTextureText      RrTextureText;
42 typedef struct _RrTextureLineArt   RrTextureLineArt;
43 typedef struct _RrPixmapMask       RrPixmapMask;
44 typedef struct _RrInstance         RrInstance;
45 typedef struct _RrColor            RrColor;
46 typedef struct _RrImage            RrImage;
47 typedef struct _RrImageSet         RrImageSet;
48 typedef struct _RrImagePic         RrImagePic;
49 typedef struct _RrImageCache       RrImageCache;
50 typedef struct _RrButton           RrButton;
51 
52 typedef guint32 RrPixel32;  /* ARGB format, not premultiplied alpha */
53 typedef guint16 RrPixel16;
54 typedef guchar  RrPixel8;
55 
56 typedef enum {
57     RR_RELIEF_FLAT,
58     RR_RELIEF_RAISED,
59     RR_RELIEF_SUNKEN,
60     RR_RELIEF_NUM_TYPES
61 } RrReliefType;
62 
63 typedef enum {
64     RR_BEVEL_1,
65     RR_BEVEL_2,
66     RR_BEVEL_NUM_TYPES
67 } RrBevelType;
68 
69 typedef enum {
70     RR_SURFACE_NONE,
71     RR_SURFACE_PARENTREL,
72     RR_SURFACE_SOLID,
73     RR_SURFACE_SPLIT_VERTICAL,
74     RR_SURFACE_HORIZONTAL,
75     RR_SURFACE_VERTICAL,
76     RR_SURFACE_DIAGONAL,
77     RR_SURFACE_CROSS_DIAGONAL,
78     RR_SURFACE_PYRAMID,
79     RR_SURFACE_MIRROR_HORIZONTAL,
80     RR_SURFACE_NUM_TYPES
81 } RrSurfaceColorType;
82 
83 typedef enum {
84     RR_TEXTURE_NONE,
85     RR_TEXTURE_MASK,
86     RR_TEXTURE_TEXT,
87     RR_TEXTURE_LINE_ART,
88     RR_TEXTURE_RGBA,
89     RR_TEXTURE_IMAGE,
90     RR_TEXTURE_NUM_TYPES
91 } RrTextureType;
92 
93 typedef enum {
94     RR_JUSTIFY_LEFT,
95     RR_JUSTIFY_CENTER,
96     RR_JUSTIFY_RIGHT,
97     RR_JUSTIFY_NUM_TYPES
98 } RrJustify;
99 
100 /* Put middle first so it's the default */
101 typedef enum {
102     RR_ELLIPSIZE_MIDDLE,
103     RR_ELLIPSIZE_NONE,
104     RR_ELLIPSIZE_START,
105     RR_ELLIPSIZE_END,
106     RR_ELLIPSIZE_NUM_TYPES
107 } RrEllipsizeMode;
108 
109 typedef enum {
110     RR_FONTWEIGHT_LIGHT,
111     RR_FONTWEIGHT_NORMAL,
112     RR_FONTWEIGHT_SEMIBOLD,
113     RR_FONTWEIGHT_BOLD,
114     RR_FONTWEIGHT_ULTRABOLD,
115     RR_FONTWEIGHT_NUM_TYPES
116 } RrFontWeight;
117 
118 typedef enum {
119     RR_FONTSLANT_NORMAL,
120     RR_FONTSLANT_ITALIC,
121     RR_FONTSLANT_OBLIQUE,
122     RR_FONTSLANT_NUM_TYPES
123 } RrFontSlant;
124 
125 struct _RrSurface {
126     RrSurfaceColorType grad;
127     RrReliefType relief;
128     RrBevelType bevel;
129     RrColor *primary;
130     RrColor *secondary;
131     RrColor *border_color;
132     RrColor *bevel_dark;
133     RrColor *bevel_light;
134     RrColor *interlace_color;
135     gboolean interlaced;
136     gboolean border;
137     RrAppearance *parent;
138     gint parentx;
139     gint parenty;
140     RrPixel32 *pixel_data;
141     gint bevel_dark_adjust;  /* 0-255, default is 64 */
142     gint bevel_light_adjust; /* 0-255, default is 128 */
143     RrColor *split_primary;
144     RrColor *split_secondary;
145 };
146 
147 struct _RrTextureText {
148     RrFont *font;
149     RrJustify justify;
150     RrColor *color;
151     const gchar *string;
152     gint shadow_offset_x;
153     gint shadow_offset_y;
154     RrColor *shadow_color;
155     gboolean shortcut; /*!< Underline a character */
156     guint shortcut_pos; /*!< Position in bytes of the character to underline */
157     RrEllipsizeMode ellipsize;
158     gboolean flow; /* allow multiple lines.  must set maxwidth below */
159     gint maxwidth;
160     guchar shadow_alpha; /* at the bottom to improve alignment */
161 };
162 
163 struct _RrPixmapMask {
164     const RrInstance *inst;
165     Pixmap mask;
166     gint width;
167     gint height;
168     gchar *data;
169 };
170 
171 struct _RrTextureMask {
172     RrColor *color;
173     RrPixmapMask *mask;
174 };
175 
176 struct _RrTextureRGBA {
177     gint width;
178     gint height;
179     gint alpha;
180     RrPixel32 *data;
181     /* size and position to draw at (if these are zero, then it will be
182        drawn to fill the entire texture */
183     gint tx;
184     gint ty;
185     gint twidth;
186     gint theight;
187 };
188 
189 struct _RrTextureImage {
190     RrImage *image;
191     gint alpha;
192     /* size and position to draw at (if these are zero, then it will be
193        drawn to fill the entire texture */
194     gint tx;
195     gint ty;
196     gint twidth;
197     gint theight;
198 };
199 
200 struct _RrTextureLineArt {
201     RrColor *color;
202     gint x1;
203     gint y1;
204     gint x2;
205     gint y2;
206 };
207 
208 union _RrTextureData {
209     RrTextureRGBA rgba;
210     RrTextureImage image;
211     RrTextureText text;
212     RrTextureMask mask;
213     RrTextureLineArt lineart;
214 };
215 
216 struct _RrTexture {
217     /* If changing the type of a texture, you should DEFINITELY call
218        RrAppearanceClearTextures() first! */
219     RrTextureType type;
220     RrTextureData data;
221 };
222 
223 struct _RrAppearance {
224     const RrInstance *inst;
225 
226     RrSurface surface;
227     gint textures;
228     RrTexture *texture;
229     Pixmap pixmap;
230     XftDraw *xftdraw;
231 
232     /* cached for internal use */
233     gint w, h;
234 };
235 
236 /*! Holds a RGBA image picture */
237 struct _RrImagePic {
238     gint width, height;
239     RrPixel32 *data;
240     /* The sum of all the pixels.  This is used to compare pictures if their
241        hashes match. */
242     gint sum;
243 };
244 
245 typedef void (*RrImageDestroyFunc)(RrImage *image, gpointer data);
246 
247 /*! An RrImage refers to a RrImageSet.  If multiple RrImageSets end up
248   holding the same image data, they will be marged and the RrImages that
249   point to them would be updated. */
250 struct _RrImage {
251     gint ref;
252     RrImageSet *set;
253 
254     /* This function (if not NULL) will be called just before destroying
255       RrImage. */
256     RrImageDestroyFunc destroy_func;
257     gpointer           destroy_data;
258 };
259 
260 /*! An RrImage is a sort of meta-image.  It can contain multiple versions
261   of an image at different sizes, which may or may not be completely different
262   pictures */
263 struct _RrImageSet
264 {
265     RrImageCache *cache;
266 
267     /*! If a picture is loaded by a name, then it has a name attached to it.
268       This contains a list of strings, containing all names that have ever
269       been associated with the RrImageSet. A name in the RrImageCache can
270       only be associated with a single RrImageSet. */
271     GSList *names;
272 
273     /*! RrImages that point at this RrImageSet. If this is empty, then there
274       are no images using the set and it can be freed. */
275     GSList *images;
276 
277     /*! An array of "originals", that is of RrPictures that have been added
278       to the image in various sizes, and that have not been resized.  These
279       are explicitly added to the RrImageSet. */
280     RrImagePic **original;
281     gint n_original;
282     /*! An array of "resized" pictures.  When an "original" RrPicture
283       needs to be resized for drawing, it is saved in here so that it doesn't
284       need to be resized again.  These are automatically added to the
285       RrImage. */
286     RrImagePic **resized;
287     gint n_resized;
288 };
289 
290 struct _RrButton {
291     const RrInstance *inst;
292 
293     /* colors */
294     RrColor *focused_unpressed_color;
295     RrColor *unfocused_unpressed_color;
296     RrColor *focused_pressed_color;
297     RrColor *unfocused_pressed_color;
298     RrColor *focused_disabled_color;
299     RrColor *unfocused_disabled_color;
300     RrColor *focused_hover_color;
301     RrColor *unfocused_hover_color;
302     RrColor *focused_hover_toggled_color;
303     RrColor *unfocused_hover_toggled_color;
304     RrColor *focused_pressed_toggled_color;
305     RrColor *unfocused_pressed_toggled_color;
306     RrColor *focused_unpressed_toggled_color;
307     RrColor *unfocused_unpressed_toggled_color;
308 
309     /* masks */
310     RrPixmapMask *unpressed_mask;
311     RrPixmapMask *pressed_mask;
312     RrPixmapMask *disabled_mask;
313     RrPixmapMask *hover_mask;
314     RrPixmapMask *unpressed_toggled_mask;
315     RrPixmapMask *hover_toggled_mask;
316     RrPixmapMask *pressed_toggled_mask;
317 
318     /* textures */
319     RrAppearance *a_focused_unpressed;
320     RrAppearance *a_unfocused_unpressed;
321     RrAppearance *a_focused_pressed;
322     RrAppearance *a_unfocused_pressed;
323     RrAppearance *a_focused_disabled;
324     RrAppearance *a_unfocused_disabled;
325     RrAppearance *a_focused_hover;
326     RrAppearance *a_unfocused_hover;
327     RrAppearance *a_focused_unpressed_toggled;
328     RrAppearance *a_unfocused_unpressed_toggled;
329     RrAppearance *a_focused_pressed_toggled;
330     RrAppearance *a_unfocused_pressed_toggled;
331     RrAppearance *a_focused_hover_toggled;
332     RrAppearance *a_unfocused_hover_toggled;
333 
334 };
335 
336 /* these are the same on all endian machines because it seems to be dependant
337    on the endianness of the gfx card, not the cpu. */
338 #define RrDefaultAlphaOffset 24
339 #define RrDefaultRedOffset 16
340 #define RrDefaultGreenOffset 8
341 #define RrDefaultBlueOffset 0
342 
343 #define RrDefaultFontFamily       "arial,sans"
344 #define RrDefaultFontSize         8
345 #define RrDefaultFontWeight       RR_FONTWEIGHT_NORMAL
346 #define RrDefaultFontSlant        RR_FONTSLANT_NORMAL
347 
348 RrInstance* RrInstanceNew (Display *display, gint screen);
349 void        RrInstanceFree (RrInstance *inst);
350 
351 Display* RrDisplay      (const RrInstance *inst);
352 gint     RrScreen       (const RrInstance *inst);
353 Window   RrRootWindow   (const RrInstance *inst);
354 Visual*  RrVisual       (const RrInstance *inst);
355 gint     RrDepth        (const RrInstance *inst);
356 Colormap RrColormap     (const RrInstance *inst);
357 gint     RrRedOffset    (const RrInstance *inst);
358 gint     RrGreenOffset  (const RrInstance *inst);
359 gint     RrBlueOffset   (const RrInstance *inst);
360 gint     RrRedShift     (const RrInstance *inst);
361 gint     RrGreenShift   (const RrInstance *inst);
362 gint     RrBlueShift    (const RrInstance *inst);
363 gint     RrRedMask      (const RrInstance *inst);
364 gint     RrGreenMask    (const RrInstance *inst);
365 gint     RrBlueMask     (const RrInstance *inst);
366 
367 RrColor *RrColorNew   (const RrInstance *inst, gint r, gint g, gint b);
368 RrColor *RrColorCopy  (RrColor *c);
369 RrColor *RrColorParse (const RrInstance *inst, gchar *colorname);
370 void     RrColorFree  (RrColor *in);
371 
372 gint     RrColorRed   (const RrColor *c);
373 gint     RrColorGreen (const RrColor *c);
374 gint     RrColorBlue  (const RrColor *c);
375 gulong   RrColorPixel (const RrColor *c);
376 GC       RrColorGC    (RrColor *c);
377 
378 RrAppearance *RrAppearanceNew  (const RrInstance *inst, gint numtex);
379 RrAppearance *RrAppearanceCopy (RrAppearance *a);
380 void          RrAppearanceFree (RrAppearance *a);
381 void          RrAppearanceRemoveTextures(RrAppearance *a);
382 void          RrAppearanceAddTextures(RrAppearance *a, gint numtex);
383 /*! Always call this when changing the type of a texture in an appearance */
384 void          RrAppearanceClearTextures(RrAppearance *a);
385 
386 RrButton *RrButtonNew (const RrInstance *inst);
387 void      RrButtonFree(RrButton *b);
388 
389 RrFont *RrFontOpen          (const RrInstance *inst, const gchar *name,
390                              gint size, RrFontWeight weight, RrFontSlant slant);
391 RrFont *RrFontOpenDefault   (const RrInstance *inst);
392 void    RrFontClose         (RrFont *f);
393 /*! Returns an RrSize, that was allocated with g_slice_new().  Use g_slice_free() to
394   free it. */
395 RrSize *RrFontMeasureString (const RrFont *f, const gchar *str,
396                              gint shadow_offset_x, gint shadow_offset_y,
397                              gboolean flow, gint maxwidth);
398 gint    RrFontHeight        (const RrFont *f, gint shadow_offset_y);
399 gint    RrFontMaxCharWidth  (const RrFont *f);
400 
401 /* Paint into the appearance. The old pixmap is returned (if there was one). It
402    is the responsibility of the caller to call XFreePixmap on the return when
403    it is non-null. */
404 Pixmap RrPaintPixmap (RrAppearance *a, gint w, gint h);
405 void   RrPaint       (RrAppearance *a, Window win, gint w, gint h);
406 void   RrMinSize     (RrAppearance *a, gint *w, gint *h);
407 gint   RrMinWidth    (RrAppearance *a);
408 /* For text textures, if flow is TRUE, then the string must be set before
409    calling this, otherwise it doesn't need to be */
410 gint   RrMinHeight   (RrAppearance *a);
411 void   RrMargins     (RrAppearance *a, gint *l, gint *t, gint *r, gint *b);
412 
413 gboolean RrPixmapToRGBA(const RrInstance *inst,
414                         Pixmap pmap, Pixmap mask,
415                         gint *w, gint *h, RrPixel32 **data);
416 
417 /*! Create a new image cache for RrImages.
418   @param max_resized_saved The number of resized copies of an image to save
419 */
420 RrImageCache* RrImageCacheNew(gint max_resized_saved);
421 void          RrImageCacheRef(RrImageCache *self);
422 void          RrImageCacheUnref(RrImageCache *self);
423 
424 /*! Create a new image, or return one from the cache that matches.
425   @param cache The image cache.
426   @param old The current RrImage, which the new image should be added to.
427     Use this if loading a different sized version of the same image.
428     The returned RrImage should replace the one passed in as old.
429     Pass NULL here if adding an image which is (or may be) entirely new.
430   @param name The name of the icon to be loaded off disk, or used in the cache
431   @return Returns NULL if unable to load an image by the name and it is not in
432     the cache already
433 */
434 RrImage* RrImageNewFromName(RrImageCache *cache, const gchar *name);
435 
436 /*! Create a new image, or return one from the cache that matches.
437   @param cache The image cache.
438   @param data The image data in RGBA32 format.  There should be @w * @h many
439     values in the data array.
440   @param w The width of the image data.
441   @param h The height of the image data.
442   @return Returns NULL if unable to load an image by the name and it is not in
443     the cache already
444 */
445 RrImage* RrImageNewFromData(RrImageCache *cache, RrPixel32 *data,
446                             gint w, gint h);
447 
448 /*! Add a new size of a picture to an image.
449   If a picture has multiple versions of different sizes (example 16x16, 32x32
450   and so on), they should all be under the same RrImage.  This adds a new
451   size to an existing RrImage, associating the newly sized picture with the
452   others in the RrImage - classifying them as being the same logical image at a
453   different dimention.
454 */
455 void RrImageAddFromData(RrImage *image, RrPixel32 *data, gint w, gint h);
456 
457 void RrImageRef(RrImage *im);
458 void RrImageUnref(RrImage *im);
459 
460 G_END_DECLS
461 
462 #endif /*__render_h*/
463