More compile fixes
[erwise] / Xl / XlSetup.c
1 /*
2  * XlSetup.c --
3  *
4  * Author: Teemu Rantanen <tvr@cs.hut.fi>
5  * Copyright (c) 1992 Teemu Rantanen
6  *                    All rights reserved
7  *
8  * Created: Mon Mar  2 22:06:56 1992 tvr
9  * Last modified: Wed May 13 06:44:12 1992 tvr
10  *
11  */
12
13 #include <stdio.h>
14 #include <sys/types.h>
15
16 #include <X11/Xlib.h>
17
18 #include "HTAnchor.h"
19 #include "HTStyle.h"
20 #include "../HText/HText.h"
21
22 #include "Xl.h"
23 #include "XlStyle.h"
24 #include "XlTypes.h"
25 #include "XlFormatText.h"
26
27 #include "XlConfig.h"
28
29 int xl_junk_object(HTextObject_t * p);
30
31 /*
32  * Find a style
33  */
34 XlStyle_t *
35  xl_get_style_and_gc(p, currentgc, t)
36 HTextObject_t *p;
37 GC *currentgc;
38 HText_t *t;
39 {
40     XlStyle_t *loop = t->xl_global->XlStyles;
41
42     if (!p->style) {
43         sleep(1);
44     }
45     while (loop->fontname && strcmp(loop->styletags, p->style->SGMLTag)) {
46         loop++;
47     }
48     if (loop->fontname) {
49         if (currentgc)
50             *currentgc = t->xl_global->gcs[loop - t->xl_global->XlStyles];
51
52         return loop;
53     }
54 #ifdef XL_DEBUG
55     printf("holy mony, style not found\n");
56 #endif
57     return NULL;
58 }
59
60
61
62 /*
63  *  Setup resources for Xl (this can be changed at runtime some day)
64  */
65 int XlSetupResources(func)
66 void *(*func) ();
67 {
68     void *table;
69     char *item;
70     XlStyle_t *styles;
71
72     char *resource;
73
74     table = (void *) (*func) ((void *) NULL, C_FONTS);
75
76     styles = XlStyles;
77
78     for (styles = XlStyles; styles->fontname; styles++) {
79         resource = (char *) (*func) (table, styles->config_name);
80
81         if (resource) {
82
83             if (styles->must_free_fontname)
84                 free(styles->fontname);
85
86             styles->must_free_fontname = 1;
87
88             styles->fontname = (char *) strdup(resource);
89 #ifdef XL_DEBUG
90             printf("XlConfigure: %s -> %s\n", styles->config_name, resource);
91 #endif
92         }
93     }
94
95     return 0;
96 }
97
98
99
100
101 /*
102  *  For every page, setup Xl-stuff (including dpy, for multiple screens)
103  *  - Setup GC's
104  *  - Calculate width and height for each object
105  *  - Set page display coordinates to (0, 0)
106  *  - Delete null space-objects (only spaces on nonraw-mode)
107  *  - Something else (uh?)
108  */
109 int XlSetupText(display, window, fg, bg, htext)
110 Display *display;
111 Window window;
112 unsigned long fg;
113 unsigned long bg;
114 HText_t *htext;
115 {
116     HTextObject_t *p;
117
118     XlStyle_t *s, *s_alloc;
119
120     HTStyle *st = NULL;
121     XlStyle_t *current;
122
123     GC currentgc;
124
125     int i;
126
127     int struct_length;
128
129     /*
130      * safe
131      */
132     if (!htext)
133         return;
134
135
136     /*
137      * Duplicate Style-structure
138      */
139
140     s = XlStyles;
141
142     while (s->fontname)
143         s++;
144
145     /*
146      * NOTE!!! Add one element because of null element at the end !!!
147      */
148     struct_length = sizeof(XlStyle_t) * ((s - XlStyles) + 1);
149
150     s_alloc = s = (XlStyle_t *) malloc(struct_length);
151
152     if (!s) {
153         printf("cannot malloc on Xl\n");
154
155         exit(1);
156     }
157     memcpy(s, XlStyles, struct_length);
158
159     /*
160      * Load fonts. Note. Fonts should be cached for every display.
161      */
162     while (s->fontname) {
163         s->fontinfo =
164             (XFontStruct *) XLoadQueryFont(display, s->fontname);
165
166         if (!s->fontinfo) {
167             printf("XlSetup: Cannot load font %s\nXlSetup: trying 'fixed'.\n",
168                    s->fontname);
169
170             s->fontinfo =
171                 (XFontStruct *) XLoadQueryFont(display, "fixed");
172
173             /*
174              * Set font up on XlStyles -table so that font wont be tried
175              * every time.
176              */
177             {
178                 XlStyle_t *ss = XlStyles + (s - s_alloc);
179
180                 if (ss->must_free_fontname)
181                     free(ss->fontname);
182
183                 ss->fontname = (char *) strdup("fixed");
184             }
185
186
187             if (!s->fontinfo) {
188
189                 printf("XlSetup: Cannot load 'fixed' ... sorry.\n");
190
191                 exit(1);
192             }
193         }
194         /*
195          * Next font
196          */
197         s++;
198     }
199
200     /*
201      * Allocate space for page specific Xl data
202      */
203
204     if (!htext->xl_global) {
205
206         htext->xl_global = (XlGlobalData_t *) malloc(sizeof(XlGlobalData_t));
207
208         memset(htext->xl_global, 0, sizeof(XlGlobalData_t));
209
210     }
211     htext->xl_global->dpy = display;
212
213     htext->xl_global->window = window;
214
215     htext->xl_global->XlStyles = s_alloc;
216
217     /*
218      * Set up GC:s
219      */
220     htext->xl_global->xorgc = XCreateGC(htext->xl_global->dpy,
221                                         htext->xl_global->window,
222                                         0,
223                                         NULL);
224
225     XSetFunction(htext->xl_global->dpy, htext->xl_global->xorgc, GXxor);
226
227     XSetForeground(htext->xl_global->dpy, htext->xl_global->xorgc, fg ^ bg);
228
229
230     htext->xl_global->nr_gcs = s - s_alloc;
231
232     htext->xl_global->gcs = (GC *) malloc((s - s_alloc) * sizeof(GC));
233
234     for (s = s_alloc, i = 0; s->fontname; s++, i++) {
235
236         htext->xl_global->gcs[i] =
237             XCreateGC(htext->xl_global->dpy,
238                       htext->xl_global->window, 0, NULL);
239
240         XSetForeground(htext->xl_global->dpy,
241                        htext->xl_global->gcs[i],
242                        fg);
243
244         XSetFont(htext->xl_global->dpy,
245                  htext->xl_global->gcs[i],
246                  s->fontinfo->fid);
247
248         s->char_width = (int) XTextWidth(s->fontinfo, "i", 1);
249     }
250
251     /*
252      * Set up graphics exposures to XCopyArea GC
253      */
254     XSetGraphicsExposures(display, htext->xl_global->gcs[0], True);
255
256     /*
257      * Loop all objects
258      */
259     p = htext->first;
260
261     while (p) {
262
263         /*
264          * Allocate space for Xl specific object part
265          */
266
267         p->xl_data = (XlObjectData_t *) malloc(sizeof(XlObjectData_t));
268
269         if (!p->xl_data) {
270             printf("cannot malloc in XlSetupText\n");
271             exit(1);
272         }
273         memset(p->xl_data, 0, sizeof(XlObjectData_t));
274
275         /*
276          * Setup Xl styles. Check Xl style only on style changes
277          */
278         if (!st || (st != p->style)) {
279
280             current = xl_get_style_and_gc(p, &currentgc, htext);
281
282             st = p->style;
283         }
284         p->xl_data->style = current;
285
286         p->xl_data->gc = currentgc;
287
288         /*
289          * Set up text dimensions. Use 'XTextExtents()'.
290          */
291         p->width = (int) XTextWidth(current->fontinfo,
292                                     (const char *) p->data,
293                                     p->length);
294
295         p->height = current->fontinfo->ascent + current->fontinfo->descent;
296
297         /*
298          * Next
299          */
300
301         /*
302          * Check if this object is 'junk'-object and delete it if needed.
303          * Has to be here, because we need styles.
304          */
305         if (xl_junk_object(p)) {
306             HTextObject_t *prev = p->prev;
307
308             /*
309              * Set links
310              */
311             if (prev) {
312
313                 prev->next = p->next;
314
315             } else {
316
317                 htext->first = p->next;
318
319             }
320
321             if (p->next) {
322
323                 p->next->prev = prev;
324
325             } else {
326
327                 htext->last = prev;
328
329             }
330
331             /*
332              * Free everything allocated
333              */
334             if (p->data)
335                 free(p->data);
336
337             free(p->xl_data);
338
339             free(p);
340         }
341         p = p->next;
342
343     }
344
345     /*
346      * Cannot fail ;)
347      */
348     return 0;
349 }
350
351
352
353 /*
354  * If this object is not needed, it is 'junk' -object
355  */
356 int xl_junk_object(p)
357 HTextObject_t *p;
358 {
359     register int i;
360
361     if (p->paragraph)
362         return 0;
363
364     if (xl_object_mode(p) & STYLE_RAW)
365         return 0;
366
367     for (i = 0; i < p->length; i++)
368         if (p->data[i] != ' ')
369             return 0;
370
371     return 1;
372 }