]> git.lyx.org Git - lyx.git/blob - src/lyxdraw.C
white-space changes, removed definitions.h several enum changes because of this,...
[lyx.git] / src / lyxdraw.C
1 #include <config.h>
2
3 #include "lyxdraw.h"
4 #include "debug.h"
5
6 extern int reverse_video;
7 extern int mono_video;
8 extern char label_color[];
9 extern char math_color[];
10 extern char math_frame_color[];
11 extern char latex_color[];
12 extern char foot_color[];
13 extern char new_line_color[];
14 extern char fill_color[];
15 extern int fast_selection;
16 extern char on_off_line_color[];
17 extern string background_color;
18 extern char lighted_color[];
19 extern char selection_color[];
20 extern char note_color[];
21 extern char note_frame_color[];
22
23 Colormap color_map = 0;
24 long int background_pixels;
25
26 // X11 color names
27 char const * const X11Color[11] = 
28 { "black", "black", "white", "red", "green", "blue", "cyan", "magenta", 
29   "yellow", "black", "black" };
30  
31 // Different graphic contexts
32 static GC clear_gc = 0;
33 static GC latex_gc = 0;
34 static GC foot_gc = 0;
35 static GC new_line_gc = 0;
36 static GC math_gc = 0;
37 static GC math_frame_gc = 0;
38 static GC fill_gc = 0;
39 static GC note_gc = 0;
40 static GC note_frame_gc = 0;
41 static GC copy_gc = 0;
42 static GC select_gc = 0;
43 static GC on_off_line_gc = 0;
44 static GC thin_on_off_line_gc = 0;
45 static GC thick_line_gc = 0;
46 static GC lighted_gc = 0;
47 static GC selection_gc = 0;
48 static GC accent_gc[10] = { 0, 0, 0, 0, 0,
49                             0, 0, 0, 0, 0 };
50 static GC color_gc[11] = { 0, 0, 0, 0, 0, 0,
51                            0, 0, 0, 0, 0 };
52 static GC minipage_gc = 0;
53
54
55 // Allocates color and sets it as foreground
56 // Returns "false" if unsuccesful
57 bool setForegroundColor(char const * const color, XGCValues & val)
58 {
59         XColor xcol;
60         if (!color_map) {
61                 color_map = fl_state[fl_get_vclass()].colormap;
62         }
63         if (!mono_video) {
64                 if (XParseColor(fl_display, color_map, color, &xcol)
65                     && XAllocColor(fl_display, color_map, &xcol))
66                 {
67                         val.foreground = xcol.pixel;
68                 } else {
69                         lyxerr << "LyX: Couldn't get color " << color << endl;
70                         return false;
71                 }
72         }
73         return true;
74 }
75
76
77 static
78 void do_reverse_video(XGCValues &val)
79 {
80         if (reverse_video) {
81                 val.foreground= WhitePixel(fl_display,
82                                           DefaultScreen(fl_display));
83                 val.background= BlackPixel(fl_display,
84                                           DefaultScreen(fl_display));
85         } else {
86                 val.foreground= BlackPixel(fl_display,
87                                           DefaultScreen(fl_display));
88                 val.background= WhitePixel(fl_display,
89                                           DefaultScreen(fl_display));
90         }
91 }
92
93
94 static
95 GC make_normal_gc(const char* color)
96 {
97         XGCValues val;
98         do_reverse_video(val);
99         val.function = GXcopy;
100         val.graphics_exposures = false;
101         setForegroundColor(color, val);
102         return XCreateGC(fl_display, fl_root,
103                          GCBackground | GCForeground | GCFunction |
104                          GCGraphicsExposures, &val);
105 }
106
107
108 static
109 GC GetNoteGC()
110 {
111         if (note_gc) return note_gc;
112         
113         note_gc = make_normal_gc(note_color);
114
115         return note_gc;
116 }
117
118
119 static
120 GC GetNoteFrameGC()
121 {
122         if (note_frame_gc) return note_frame_gc;
123         
124         note_frame_gc = make_normal_gc(note_frame_color);
125
126         return note_frame_gc;
127 }
128
129
130 static
131 GC GetMathGC()
132 {
133         if (math_gc) return math_gc;
134         
135         math_gc = make_normal_gc(math_color);
136
137         return math_gc;
138 }
139
140
141 static
142 GC GetLatexGC()
143 {
144         if (latex_gc) return latex_gc;
145
146         XGCValues val;
147         if (reverse_video ^ mono_video) {
148                 val.foreground= WhitePixel(fl_display, DefaultScreen(fl_display));
149                 val.background= BlackPixel(fl_display, DefaultScreen(fl_display));
150         } else {
151                 val.foreground= BlackPixel(fl_display, DefaultScreen(fl_display));
152                 val.background= WhitePixel(fl_display, DefaultScreen(fl_display));
153         }
154         val.function= GXcopy;
155         val.graphics_exposures = false;
156         setForegroundColor(latex_color, val);
157         latex_gc = XCreateGC(fl_display, fl_root, GCBackground 
158                              | GCForeground | GCFunction | GCGraphicsExposures, 
159                              &val);
160         XFlush(fl_display);
161         
162         return latex_gc;
163 }
164
165
166 static
167 GC GetFootGC()
168 {
169         if (foot_gc) return foot_gc;
170         
171         foot_gc = make_normal_gc(foot_color);
172         
173         return foot_gc;
174 }
175
176
177 static
178 GC GetNewLineGC()
179 {
180         if (new_line_gc) return new_line_gc;
181  
182         new_line_gc = make_normal_gc(new_line_color);
183  
184         return new_line_gc;
185 }
186  
187  
188 static
189 GC GetMathFrameGC()
190 {
191         if (math_frame_gc) return math_frame_gc;
192         
193         math_frame_gc = make_normal_gc(math_frame_color);
194         
195         return math_frame_gc;
196 }
197
198
199 static
200 GC GetFillGC()
201 {
202         if (fill_gc) return fill_gc;
203         
204         fill_gc = make_normal_gc(fill_color);
205         
206         return fill_gc;
207 }
208
209
210 static
211 GC GetClearGC()
212 {
213         if (clear_gc) return clear_gc;
214         
215         XGCValues val;
216
217         if (reverse_video) {
218                 val.foreground= BlackPixel(fl_display,
219                                           DefaultScreen(fl_display));
220                 val.background= WhitePixel(fl_display,
221                                           DefaultScreen(fl_display));
222         } else {
223                 val.background= BlackPixel(fl_display,
224                                           DefaultScreen(fl_display));
225                 val.foreground= WhitePixel(fl_display,
226                                           DefaultScreen(fl_display));
227         }
228         val.function= GXcopy;
229         val.graphics_exposures = false;
230         if (!fast_selection && background_color != "white") {
231                 setForegroundColor(background_color.c_str(), val);
232         }
233         background_pixels = val.foreground;
234         
235         clear_gc = XCreateGC(fl_display, fl_root, GCBackground 
236                              | GCForeground | GCFunction | GCGraphicsExposures, 
237                              &val);
238         XFlush(fl_display);
239         
240         return clear_gc;
241 }
242
243
244 static
245 GC GetOnOffLineGC()
246 {
247         if (on_off_line_gc) return on_off_line_gc;
248         
249         XGCValues val;
250         do_reverse_video(val);
251
252         val.function= GXcopy;
253         val.graphics_exposures = false;
254         setForegroundColor(on_off_line_color, val);
255         val.line_width = 0;
256         val.line_style = LineOnOffDash;
257         on_off_line_gc = XCreateGC(fl_display, fl_root,
258                                    GCBackground | GCForeground | GCFunction |
259                                    GCGraphicsExposures | GCLineWidth |
260                                    GCLineStyle , &val);
261         XFlush(fl_display);
262
263         return on_off_line_gc;
264 }
265
266
267 static
268 GC GetThickLineGC()
269 {
270         if (thick_line_gc) return thick_line_gc;
271         XGCValues val;
272         do_reverse_video(val);
273
274         val.function= GXcopy;
275         val.graphics_exposures = false;
276         val.line_width = 2;
277         val.line_style = LineSolid;
278         thick_line_gc = XCreateGC(fl_display, fl_root, GCBackground 
279                                   | GCForeground | GCFunction | GCGraphicsExposures
280                                   | GCLineWidth | GCLineStyle , &val);
281         XFlush(fl_display);
282         
283         return thick_line_gc;
284 }
285
286
287 static
288 GC GetThinOnOffLineGC()
289 {
290         if (thin_on_off_line_gc) return thin_on_off_line_gc;
291         XGCValues val;
292         do_reverse_video(val);
293
294         val.function= GXcopy;
295         val.graphics_exposures = false;
296         val.line_style = LineOnOffDash;
297         val.line_width = 0;
298         thin_on_off_line_gc = 
299                 XCreateGC(fl_display, fl_root, GCBackground
300                           | GCForeground | GCFunction | GCGraphicsExposures
301                           | GCLineWidth | GCLineStyle , &val);
302         XFlush(fl_display);
303         
304         return thin_on_off_line_gc;
305 }
306
307
308 static
309 GC GetCopyGC()
310 {
311         if (copy_gc) return copy_gc;
312         XGCValues val;
313         do_reverse_video(val);
314
315         val.function= GXcopy;
316         val.graphics_exposures = false;
317         val.line_style = LineSolid;
318         val.line_width = 0;
319         copy_gc = XCreateGC(fl_display, fl_root, GCBackground
320                             | GCForeground | GCFunction | GCGraphicsExposures
321                             | GCLineWidth | GCLineStyle , &val);
322         XFlush(fl_display);
323
324         return copy_gc;
325 }
326
327
328 static
329 GC GetSelectGC()
330 {
331         if (select_gc) return select_gc;
332         XGCValues val;
333         unsigned int a = BlackPixel(fl_display, DefaultScreen(fl_display));
334         unsigned int b = WhitePixel(fl_display, DefaultScreen(fl_display)); 
335         val.plane_mask = a^b;
336         val.line_style = LineSolid;
337         val.line_width = 2;
338         val.graphics_exposures = false;
339         val.function= GXinvert;
340         select_gc = XCreateGC(fl_display, fl_root,
341                               GCFunction | GCGraphicsExposures | GCPlaneMask
342                                       | GCLineWidth | GCLineStyle , &val);
343         XFlush(fl_display);
344
345         return select_gc; 
346 }
347
348
349 static
350 GC GetSelectionGC()
351 {
352         if (selection_gc) return selection_gc;
353         
354         XGCValues val;
355
356         if (!reverse_video) {
357                 val.foreground= BlackPixel(fl_display,
358                                           DefaultScreen(fl_display));
359                 val.background= WhitePixel(fl_display,
360                                           DefaultScreen(fl_display));
361         } else {
362                 val.background= BlackPixel(fl_display,
363                                           DefaultScreen(fl_display));
364                 val.foreground= WhitePixel(fl_display,
365                                           DefaultScreen(fl_display));
366         }
367         
368         val.function= GXcopy;
369         val.graphics_exposures = false;
370         if (!fast_selection && selection_color[0] != 0) {
371                 if (!setForegroundColor(selection_color, val)) {
372                         fast_selection = True;
373                         if (clear_gc) clear_gc = 0;
374                         lyxerr << "     Will use FastSelection-method."
375                                << endl;
376                 }
377         }
378         selection_gc = XCreateGC(fl_display, fl_root,
379                                  GCBackground | GCForeground | GCFunction
380                                  | GCGraphicsExposures, &val);
381
382         return selection_gc;
383 }
384
385
386 static
387 GC GetLightedGC()
388 {
389         if (lighted_gc) return lighted_gc;
390         XGCValues val;
391         if (reverse_video) {
392                 val.background= BlackPixel(fl_display, DefaultScreen(fl_display));
393         } else {
394                 val.background= WhitePixel(fl_display, DefaultScreen(fl_display));
395         }
396         val.foreground= val.background;
397         val.function= GXcopy;
398         val.graphics_exposures = false;
399         val.line_style = LineSolid;
400         val.line_width = 0;
401         setForegroundColor(lighted_color, val);
402         lighted_gc = XCreateGC(fl_display, fl_root, GCBackground
403                                | GCForeground | GCFunction | GCGraphicsExposures
404                                | GCLineWidth | GCLineStyle , &val);
405         XFlush(fl_display);
406
407         return lighted_gc;
408 }
409
410
411 GC GetColorGC(LyXFont::FONT_COLOR color)
412 {
413         if (color_gc[color]) return color_gc[color];
414                        
415         XGCValues val;
416         do_reverse_video(val);
417
418         val.function= GXcopy;
419         val.graphics_exposures = false;
420         setForegroundColor(X11Color[color], val);
421         val.line_width = 0;
422         val.line_style = LineSolid;
423         color_gc[color] = XCreateGC(fl_display, fl_root,
424                                     GCBackground | GCForeground | GCFunction |
425                                     GCGraphicsExposures | GCLineWidth |
426                                     GCLineStyle , &val);
427         XFlush(fl_display);
428
429         return color_gc[color];
430 }
431
432
433 GC GetAccentGC(LyXFont const &f, int line_width)
434 {
435         if (line_width>= 10) line_width = 9;
436         
437         if (accent_gc[line_width]) return accent_gc[line_width];
438         
439         // Build this one from normal text GC, but change linewidth.
440         XGCValues val;
441         GC gc = f.getGC();
442         XGetGCValues(fl_display, gc, GCBackground | GCForeground | 
443                      GCFunction | GCGraphicsExposures | GCLineWidth | 
444                      GCLineStyle | GCPlaneMask, &val);
445
446         val.line_width = line_width;
447         val.cap_style = CapRound;
448         val.join_style = JoinRound;
449         
450         GC ac = XCreateGC(fl_display, fl_root, 
451                           GCBackground | GCForeground | GCFunction | 
452                           GCGraphicsExposures | GCLineWidth | 
453                           GCLineStyle | GCPlaneMask, &val);
454         
455         XFlush(fl_display);
456         accent_gc[line_width] = ac;
457
458         return accent_gc[line_width];
459 }
460
461
462 static
463 GC GetMinipageGC()
464 {
465         if (minipage_gc) return minipage_gc;
466         XGCValues val;
467         do_reverse_video(val);
468
469         val.function= GXcopy;
470         val.graphics_exposures = false;
471         val.line_style = LineOnOffDash;
472         val.line_width = 0;
473         setForegroundColor(fill_color, val);
474         minipage_gc = XCreateGC(fl_display, fl_root,
475                                 GCBackground | GCForeground | GCFunction |
476                                 GCGraphicsExposures | GCLineWidth |
477                                 GCLineStyle , &val);
478         XFlush(fl_display);
479
480         return minipage_gc;
481 }
482
483
484 GC getGC(gc_type typ)
485 {
486         GC gc = 0;
487         switch(typ) {
488         case gc_clear:
489                 gc = GetClearGC();
490                 break;
491         case gc_latex:
492                 gc = GetLatexGC();
493                 break;
494         case gc_foot:
495                 gc = GetFootGC();
496                 break;
497         case gc_new_line:
498                 gc = GetNewLineGC();
499                 break;
500         case gc_math:
501                 gc = GetMathGC();
502                 break;
503         case gc_math_frame:
504                 gc = GetMathFrameGC();
505                 break;
506         case gc_fill:
507                 gc = GetFillGC();
508                 break;
509         case gc_copy:
510                 gc = GetCopyGC();
511                 break;
512         case gc_select:
513                 gc = GetSelectGC();
514                 break;
515         case gc_on_off_line:
516                 gc = GetOnOffLineGC();
517                 break;
518         case gc_thin_on_off_line:
519                 gc = GetThinOnOffLineGC();
520                 break;
521         case gc_thick_line:
522                 gc = GetThickLineGC();
523                 break;
524         case gc_lighted:
525                 gc = GetLightedGC();
526                 break;
527         case gc_selection:
528                 gc = GetSelectionGC();
529                 break;
530         case gc_minipage:
531                 gc = GetMinipageGC();
532                 break;
533         case gc_note:
534                 gc = GetNoteGC();
535                 break;
536         case gc_note_frame:
537                 gc = GetNoteFrameGC();
538                 break;
539         }
540         return gc;
541 }