aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/misc.cc108
1 files changed, 43 insertions, 65 deletions
diff --git a/src/misc.cc b/src/misc.cc
index 481705c..28a63e3 100644
--- a/src/misc.cc
+++ b/src/misc.cc
@@ -25,42 +25,29 @@
25#include <stdlib.h> 25#include <stdlib.h>
26#include <string.h> 26#include <string.h>
27#include <stdio.h> 27#include <stdio.h>
28#include <iostream>
29#include <X11/Xutil.h>
30using namespace std;
31
32//------- strdup ------------------------
33//TODO: comment this
34//----------------------------------------
35char *Misc::strdup(const char *s) {
36 int l = strlen(s) + 1;
37 char *n = new char[l];
38 strncpy(n, s, l);
39 return n;
40}
28 41
29// ----- start code stealing ----
30 42
31// ---------------------------------------------------------------------- 43// ----------------------------------------------------------------------
32// xvertext, Copyright (c) 1992 Alan Richardson (mppa3@uk.ac.sussex.syma) 44// xvertext, Copyright (c) 1992 Alan Richardson (mppa3@uk.ac.sussex.syma)
33// ---------------------------------------------------------------------- 45// ----------------------------------------------------------------------
34 46
35int xv_errno; //TODO: ?
36
37static char *my_strdup(char *str);
38
39//------- getTabAlignmentString ----------
40//Routine to mimic `strdup()' (some machines don't have it)
41//----------------------------------------
42static char *my_strdup(char *str)
43{
44 char *s;
45
46 if (str == NULL) return NULL;
47
48 s = (char *)malloc((unsigned)(strlen(str)+1));
49 /* this error is highly unlikely ... */
50 if (s == NULL) {
51 fprintf(stderr, "Fatal error: my_strdup(): Couldn't do malloc!\n");
52 exit(1);
53 }
54
55 strcpy(s, str);
56 return s;
57}
58
59//------- XRotLoadFont ------------------- 47//------- XRotLoadFont -------------------
60// Load the rotated version of a given font 48// Load the rotated version of a given font
61//---------------------------------------- 49//----------------------------------------
62XRotFontStruct *XRotLoadFont(Display *dpy, char *fontname, float angle) 50Misc::XRotFontStruct *Misc::XRotLoadFont(Display *dpy, char *fontname, float angle) {
63{
64 char val; 51 char val;
65 XImage *I1, *I2; 52 XImage *I1, *I2;
66 Pixmap canvas; 53 Pixmap canvas;
@@ -97,17 +84,17 @@ XRotFontStruct *XRotLoadFont(Display *dpy, char *fontname, float angle)
97 /* load the font ... */ 84 /* load the font ... */
98 fontstruct = XLoadQueryFont(dpy, fontname); 85 fontstruct = XLoadQueryFont(dpy, fontname);
99 if (fontstruct == NULL) { 86 if (fontstruct == NULL) {
100 xv_errno = XV_NOFONT; 87 cerr<<"Fluxbox::Misc: No font"<<endl;
101 return NULL; 88 return 0;
102 } 89 }
103 90
104 XSetFont(dpy, font_gc, fontstruct->fid); 91 XSetFont(dpy, font_gc, fontstruct->fid);
105 92
106 /* allocate space for rotated font ... */ 93 /* allocate space for rotated font ... */
107 rotfont = (XRotFontStruct *)malloc((unsigned)sizeof(XRotFontStruct)); 94 rotfont = new XRotFontStruct;
108 if (rotfont == NULL) { 95 if (rotfont == 0) {
109 xv_errno = XV_NOMEM; 96 cerr<<"Fluxbox::Misc: out of memory"<<endl;
110 return NULL; 97 return 0;
111 } 98 }
112 99
113 /* determine which characters are defined in font ... */ 100 /* determine which characters are defined in font ... */
@@ -119,7 +106,7 @@ XRotFontStruct *XRotLoadFont(Display *dpy, char *fontname, float angle)
119 if (max_char>126) max_char = 126; 106 if (max_char>126) max_char = 126;
120 107
121 /* some overall font data ... */ 108 /* some overall font data ... */
122 rotfont->name = my_strdup(fontname); 109 rotfont->name = Misc::strdup(fontname);
123 rotfont->dir = dir; 110 rotfont->dir = dir;
124 rotfont->min_char = min_char; 111 rotfont->min_char = min_char;
125 rotfont->max_char = max_char; 112 rotfont->max_char = max_char;
@@ -171,18 +158,14 @@ XRotFontStruct *XRotLoadFont(Display *dpy, char *fontname, float angle)
171 boxlen/2 - descent, text, 1); 158 boxlen/2 - descent, text, 1);
172 159
173 /* reserve memory for first XImage ... */ 160 /* reserve memory for first XImage ... */
174 vertdata = (unsigned char *) malloc((unsigned)(vert_len*vert_h)); 161 vertdata = new unsigned char[vert_len*vert_h]; //(unsigned char *) malloc((unsigned)(vert_len*vert_h));
175 if (vertdata == NULL) { 162
176 xv_errno = XV_NOMEM;
177 return NULL;
178 }
179
180 /* create the XImage ... */ 163 /* create the XImage ... */
181 I1 = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap, 164 I1 = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap,
182 0, (char *)vertdata, vert_w, vert_h, 8, 0); 165 0, (char *)vertdata, vert_w, vert_h, 8, 0);
183 166
184 if (I1 == NULL) { 167 if (I1 == NULL) {
185 xv_errno = XV_NOXIMAGE; 168 cerr<<"Fluxbox::Misc: Cant create ximage."<<endl;
186 return NULL; 169 return NULL;
187 } 170 }
188 171
@@ -209,19 +192,15 @@ XRotFontStruct *XRotLoadFont(Display *dpy, char *fontname, float angle)
209 rotfont->per_char[ichar-32].glyph.bit_h = bit_h; 192 rotfont->per_char[ichar-32].glyph.bit_h = bit_h;
210 193
211 /* reserve memory for the rotated image ... */ 194 /* reserve memory for the rotated image ... */
212 bitdata = (unsigned char *)calloc((unsigned)(bit_h*bit_len), 1); 195 bitdata = new unsigned char[bit_h*bit_len]; //(unsigned char *)calloc((unsigned)(bit_h*bit_len), 1);
213 if (bitdata == NULL) {
214 xv_errno = XV_NOMEM;
215 return NULL;
216 }
217 196
218 /* create the image ... */ 197 /* create the image ... */
219 I2 = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap, 0, 198 I2 = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap, 0,
220 (char *)bitdata, bit_w, bit_h, 8, 0); 199 (char *)bitdata, bit_w, bit_h, 8, 0);
221 200
222 if (I2 == NULL) { 201 if (I2 == NULL) {
223 xv_errno = XV_NOXIMAGE; 202 cerr<<"Font::Misc: Cant create ximage!"<<endl;
224 return NULL; 203 return 0;
225 } 204 }
226 205
227 I2->byte_order = I2->bitmap_bit_order = MSBFirst; 206 I2->byte_order = I2->bitmap_bit_order = MSBFirst;
@@ -275,27 +254,27 @@ XRotFontStruct *XRotLoadFont(Display *dpy, char *fontname, float angle)
275// Free the resources associated with a 254// Free the resources associated with a
276// rotated font 255// rotated font
277//---------------------------------------- 256//----------------------------------------
278void XRotUnloadFont(Display *dpy, XRotFontStruct *rotfont) 257void Misc::XRotUnloadFont(Display *dpy, XRotFontStruct *rotfont)
279{ 258{
280 int ichar; 259 int ichar;
281 260
282 if (rotfont->dir == 0) 261 if (rotfont->dir == 0)
283 XFreeFont(dpy, rotfont->xfontstruct); 262 XFreeFont(dpy, rotfont->xfontstruct);
284 else { 263 else {
285 /* loop through each character, freeing its pixmap ... */ 264 /* loop through each character, freeing its pixmap ... */
286 for (ichar = rotfont->min_char-32; ichar <= rotfont->max_char-32; 265 for (ichar = rotfont->min_char-32; ichar <= rotfont->max_char-32;
287 ichar++) 266 ichar++)
288 XFreePixmap(dpy, rotfont->per_char[ichar].glyph.bm); 267 XFreePixmap(dpy, rotfont->per_char[ichar].glyph.bm);
289 } 268 }
290 /* rotfont should never be referenced again ... */ 269
291 free((char *)rotfont->name); 270 delete rotfont->name;
292 free((char *)rotfont); 271 delete rotfont;
293} 272}
294 273
295//------- XRotTextWidth ------------------ 274//------- XRotTextWidth ------------------
296// Returns the width of a rotated string 275// Returns the width of a rotated string
297//---------------------------------------- 276//----------------------------------------
298unsigned int XRotTextWidth(XRotFontStruct *rotfont, char *str, int len) 277unsigned int Misc::XRotTextWidth(XRotFontStruct *rotfont, char *str, int len)
299{ 278{
300 int i, width = 0, ichar; 279 int i, width = 0, ichar;
301 280
@@ -320,7 +299,7 @@ unsigned int XRotTextWidth(XRotFontStruct *rotfont, char *str, int len)
320//------- XRotDrawString ----------------- 299//------- XRotDrawString -----------------
321// A front end to XRotDrawString : mimics XDrawString 300// A front end to XRotDrawString : mimics XDrawString
322//---------------------------------------- 301//----------------------------------------
323void XRotDrawString(Display *dpy, XRotFontStruct *rotfont, Drawable drawable, 302void Misc::XRotDrawString(Display *dpy, XRotFontStruct *rotfont, Drawable drawable,
324 GC gc, int x, int y, char *str, int len) 303 GC gc, int x, int y, char *str, int len)
325{ 304{
326 static GC my_gc = 0; 305 static GC my_gc = 0;
@@ -385,9 +364,8 @@ void XRotDrawString(Display *dpy, XRotFontStruct *rotfont, Drawable drawable,
385 } 364 }
386} 365}
387 366
388// ---- stop code stealing ----
389 367
390void DrawString(Display *display, Window w, GC gc, FFont *font, 368void Misc::DrawString(Display *display, Window w, GC gc, Misc::Font *font,
391 unsigned int text_w, unsigned int size_w, 369 unsigned int text_w, unsigned int size_w,
392 unsigned int bevel_w, char *text) { 370 unsigned int bevel_w, char *text) {
393//Draw title string 371//Draw title string
@@ -414,11 +392,11 @@ void DrawString(Display *display, Window w, GC gc, FFont *font,
414 } 392 }
415 393
416 switch (font->justify) { 394 switch (font->justify) {
417 case FFont::Right: 395 case Misc::Font::RIGHT:
418 dx += size_w - l; 396 dx += size_w - l;
419 break; 397 break;
420 398
421 case FFont::Center: 399 case Misc::Font::CENTER:
422 dx += (size_w - l) / 2; 400 dx += (size_w - l) / 2;
423 break; 401 break;
424 default: 402 default:
@@ -442,7 +420,7 @@ void DrawString(Display *display, Window w, GC gc, FFont *font,
442} 420}
443 421
444 422
445void DrawRotString(Display *display, Window w, GC gc, XRotFontStruct *font, 423void Misc::DrawRotString(Display *display, Window w, GC gc, XRotFontStruct *font,
446 unsigned int align, unsigned int text_w, 424 unsigned int align, unsigned int text_w,
447 unsigned int size_w, unsigned int size_h, 425 unsigned int size_w, unsigned int size_h,
448 unsigned int bevel_w, char *text) { 426 unsigned int bevel_w, char *text) {
@@ -462,11 +440,11 @@ void DrawRotString(Display *display, Window w, GC gc, XRotFontStruct *font,
462 } 440 }
463 } 441 }
464 442
465 if (align == FFont::Right) 443 if (align == Misc::Font::RIGHT)
466 size_h = l; 444 size_h = l;
467 else if (align == FFont::Center) 445 else if (align == Misc::Font::CENTER)
468 size_h = (size_h + l) / 2; 446 size_h = (size_h + l) / 2;
469 else 447 else //LEFT
470 size_h -= (dx * 4); 448 size_h -= (dx * 4);
471 449
472 // To get it in the "center" of the tab 450 // To get it in the "center" of the tab