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