summaryrefslogtreecommitdiff
path: root/src/XDraw.cpp
blob: 948742bee92926148003eb085969b90eef225220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
 /********************************************************************************
 *  File: XDraw.cpp   								*
 *  Date: 20080805            							*
 *  Author: Gaspar Fernández (helyo@totaki.com) 				*
 *										*
 *  Copyright (C) 2008   Gaspar Fernández					*
 *										*
 *  This program is free software: you can redistribute it and/or modify	*
 *  it under the terms of the GNU General Public License as published by	*
 *  the Free Software Foundation, either version 3 of the License, or 		*
 *  (at your option) any later version.  	      	  	   		*
 *										*
 *  This program 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 General Public License for more details.				*
 *										*
 *  You should have received a copy of the GNU General Public License		*
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.	*
 *									        *
 ********************************************************************************
 *   Description:
 *      It containt functions to display XPM images using Xlib. Also we can 
 *    render text and make some drawing in colors.
 *      Based in Xpm.cc by Per Linden (included in Temperature.app 1.4) released
 *    under GNU GPL v2 (or later) license.
 *
 *   Change History:
 *    Date       Author     Modification
 *    
 ********************************************************************************/ 

#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/extensions/shape.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include "XDraw.h"
#include "errors.h"
#include <cstring>

using namespace std;

 
/*************************************************************
 *     Constructor XDraw                                     *
 *************************************************************
 *  Description:                                             *
 *      Load a background image into a Window shown in a     *
 *  Display                                                  *
 *                                                           *
 * Input:                                                    *
 *     Display* disp - Current display                       *
 *     Window   root - Root Window where to draw             *
 *     const char data* - XPM Image data                     *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
XDraw::XDraw(Display* disp, Window root, const char* data) 

{
   xDisplay = disp;
   defaultWin = root;
   
   load_bkgrnd(data);
}

/*************************************************************
 *     Method: load_bkgrnd()                                 *
 *************************************************************
 *  Description:                                             *
 *     Load Xpm data as a background image.                  *
 *                                                           *
 * Input:                                                    *
 *     const char* data - XPM image data                     *
 *                                                           *
 * Output:                                                   *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void XDraw::load_bkgrnd(const char* data)
{
   int error;

   Attributes.valuemask = 0;
   error = XpmCreatePixmapFromBuffer(xDisplay, defaultWin, (char *) data, &Image, &Mask, &Attributes);
   if (error!=XpmSuccess)
     error_handler(ERR_XPMERROR,NULL);
}

/*************************************************************
 *     Destructor ~XDraw                                     *
 *************************************************************
 *  Description:                                             *
 *     Frees memory                                          *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
XDraw::~XDraw() 
{
  xpmfree();
}

/*************************************************************
 *     Method xpmfree()                                      *
 *************************************************************
 *  Description:                                             *
 *     Frees Memory                                          *
 *                                                           *
 * Input:                                                    *
 *     Nothing                                               *
 *                                                           *
 * Output:                                                   *
 *     Nothing                                               *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void XDraw::xpmfree()
{
   if (Image) {
      XFreePixmap(xDisplay, Image);
   }

   if (Mask) {
      XFreePixmap(xDisplay, Mask);
   }
}

/*************************************************************
 *     Method: replace_background()                          *
 *************************************************************
 *  Description:                                             *
 *    Replaces background image data in the current window   *
 *                                                           *
 * Input:                                                    *
 *    const char* bkg_data - New XPM image to replace actual *
 *                          background data                  *
 *                                                           *
 * Output:                                                   *
 *    Nothing                                                *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void XDraw::replace_background(const char* bkg_data)
{
  xpmfree();			// Free image and mask
  load_bkgrnd(bkg_data);
}

/*************************************************************
 *     Function: setWindowPixmap(), setWindowPixmapShaped(), *
 *               setwpxmap()                                 * 
 *************************************************************
 *  Description:                                             *
 *    Renders the image into the Window                      *
 *    We can do this with the default window, or with        *
 *    another one.                                           *
 *                                                           *
 * Input:                                                    *
 *    Window win - Window to render the image                *
 *    bool shaped - Wether we have to apply mask or not.     *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void XDraw::setWindowPixmap(Window win) 
{
  setwpxmap(win, false);
}

void XDraw::setWindowPixmap() 
{
  setwpxmap(defaultWin, false);
}

void XDraw::setwpxmap(Window win, bool shaped)	// SetWindowPixmap
{
   XResizeWindow(xDisplay, win, Attributes.width, Attributes.height);
   XSetWindowBackgroundPixmap(xDisplay, win, Image);
   if (shaped)
     XShapeCombineMask(xDisplay, win, ShapeBounding, 0, 0, Mask, ShapeSet);
   XClearWindow(xDisplay, win);
}

void XDraw::setWindowPixmapShaped(Window win) 
{
  setwpxmap(win, true);
}

void XDraw::setWindowPixmapShaped() 
{
  setwpxmap(defaultWin, true);
}

/*************************************************************
 *     Function: setDefaultWindow                            *
 *************************************************************
 *  Description:                                             *
 *    Replaces default window with another one.              *
 *                                                           *
 * Input:                                                    *
 *    Window win - The new default window to work with.      *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void XDraw::setDefaultWindow(Window win)
{
  defaultWin = win;
}

/*************************************************************
 *     Function: Sync, doxsync                               *
 *************************************************************
 *  Description:                                             *
 *     XSync the Window                                      *
 *                                                           *
 * Input:                                                    *
 *   Window win - The Window                                *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void XDraw::Sync(Window win)
{
  doxsync(win);
}

void XDraw::Sync()
{
  doxsync(defaultWin);
}

void XDraw::doxsync(Window win)
{
  XClearWindow(xDisplay, win);
  XSync(xDisplay, win);
}

/*************************************************************
 *     Method: DrawRect                                      *
 *************************************************************
 *  Description:                                             *
 *    Draws a filled rectangle                               *
 *                                                           *
 * Input:                                                    *
 *   int x, int y - X, Y positions                           *
 *   unsigned int w, unsigned int h - Width and Height       *
 *   XDrawColor color - Color of the filled rectangle        *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void XDraw::DrawRect(int x, int y, unsigned int w, unsigned int h, XDrawColor color)
{
   GC           gc;
   XGCValues    gcv;

   gcv.foreground=setColor(color);
   gc = XCreateGC(xDisplay, Image, GCForeground, &gcv);

   XFillRectangle(xDisplay, Image, gc, x,y,w,h);
}

/*************************************************************
 *     Function: setColor                                    *
 *************************************************************
 *  Description:                                             *
 *     Gets unsigned long value, from rgb data. It can be    *
 *   extracted from individual ints or from a XDrawColor     *
 *   structure                                               *
 *                                                           *
 * Input:                                                    *
 *   int red, int gree, int blue (rgb data)                  *
 *   or XDrawColor color (rgb data within a XDrawColor       *
 *                        structure)                         *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
unsigned long XDraw::setColor(XDrawColor color)
{
	unsigned long ret_color;
	int r, g, b;
	r = (int)((256-color.r) * 255) & 0xff;
	g = (int)((256-color.g) * 255) & 0xff;
	b = (int)((256-color.b) * 255) & 0xff;
	
	ret_color = (r<<16)+(g<<8)+b;
	return ret_color;
}

unsigned long XDraw::setColor(int red, int green, int blue)
{
	unsigned long ret_color;
	int r, g, b;
	r = (int)((256-red) * 255) & 0xff;
	g = (int)((256-green) * 255) & 0xff;
	b = (int)((256-blue) * 255) & 0xff;
	
	ret_color = (r<<16)+(g<<8)+b;
	return ret_color;
}

/*************************************************************
 *     Function: drawString                                  *
 *************************************************************
 *  Description:                                             *
 *     Renders text into the Image                           *
 *                                                           *
 * Input:                                                    *
 *    int x, int y - X, Y position of the text               *
 *     (if x==CENTER_TEXT) it will be centered.              *
 *    int maxX - max. width in pixels of the text            *
 *    XDrawColor color - Color to render the text            *
 *    char* font - Font to use                               *
 *    char* str - Text to render                             *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
void XDraw::drawString(int x, int y, int maxX, XDrawColor color, char* font, char* str)
{
   XFontStruct* fontStruct;
   GC           gc;
   XGCValues    gcv;
   if ((fontStruct = XLoadQueryFont(xDisplay, font)) == 0)
     error_handler(ERR_BADFONT, font);
   

   gcv.foreground=setColor(color);
   gc = XCreateGC(xDisplay, Image, GCForeground, &gcv);

   int strLength = strlen(str);
   int strWidth = XTextWidth(fontStruct, str, strLength);
   if (strWidth>maxX)
     {
       maxX=maxX-XTextWidth(fontStruct, (char*)"...", 3);
       strLength--;
       while (strWidth>maxX)
	 {
	   strLength--;
	   strWidth = XTextWidth(fontStruct, str, strLength);
	 }
       str[strLength]='\0';	// Cut string
       strcat(str,"...");	// Add ... at the end
       strLength=strLength+3;
     }
   if (x==CENTER_TEXT)
     x = (Attributes.width / 2) - (strWidth / 2);

   XSetFont(xDisplay, gc, fontStruct->fid);
   XDrawString(xDisplay, Image, gc, x, y, str, strLength);

   XFreeGC(xDisplay, gc);
   XFreeFont(xDisplay, fontStruct);
}

/*************************************************************
 *     Function: setDrawColor()                              *
 *************************************************************
 *  Description:                                             *
 *     Inserts individual rgb data into a XDrawColor         *
 *  structure.                                               *
 *                                                           *
 * Input:                                                    *
 *   int r, int g, int b - RGB data                          *
 *                                                           *
 * Output:                                                   *
 *   XDrawColor structure containing rgb data                *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
XDraw::XDrawColor setDrawColor(int r, int g, int b)
{
  XDraw::XDrawColor color;
  color.r=r;
  color.b=b;
  color.g=g;
  return color;
}

/*************************************************************
 *     Function: testfont                                    *
 *************************************************************
 *  Description:                                             *
 *     Can we use this font?                                 *
 *     Tests if it is installed on the system and it is      *
 *  usable.                                                  *
 *                                                           *
 * Input:                                                    *
 *    char* font - Font to test                              *
 *    Display* disp - Display to use                         *
 *                                                           *
 * Output:                                                   *
 *   True if we can use it, false if we can't                *
 *                                                           *
 * Change History:                                           *
 *  Date      Author      Modification                       *
 *                                                           *
 *************************************************************/ 
bool testfont(const char *font, Display *disp)
{
  XFontStruct* fontstruct;
  return ((fontstruct = XLoadQueryFont(disp, font)) != 0);
}