]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xscreen.C
remove defaults stuff, let Qt handle no toolbar
[lyx.git] / src / frontends / xforms / xscreen.C
1 /**
2  * \file xscreen.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14
15 #include <algorithm>
16 #include <X11/Xlib.h>
17
18 #include "frontends/screen.h"
19 #include "frontends/font_metrics.h"
20 #include "XWorkArea.h"
21 #include "xscreen.h"
22 #include "lyxtext.h"
23 #include "lyxrow.h"
24 #include "Painter.h"
25 #include "WorkArea.h"
26 #include "buffer.h"
27 #include "BufferView.h"
28 #include "insets/insettext.h"
29 #include "ColorHandler.h"
30 #include "language.h"
31 #include "debug.h"
32
33 using std::endl;
34 using std::max;
35 using std::min;
36
37 namespace {
38
39 GC createGC()
40 {
41         XGCValues val;
42         val.foreground = BlackPixel(fl_get_display(),
43                                     DefaultScreen(fl_get_display()));
44
45         val.function = GXcopy;
46         val.graphics_exposures = false;
47         val.line_style = LineSolid;
48         val.line_width = 0;
49         return XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
50                          GCForeground | GCFunction | GCGraphicsExposures
51                          | GCLineWidth | GCLineStyle, &val);
52 }
53
54 } // namespace anon
55
56
57 XScreen::XScreen(XWorkArea & o)
58         : LyXScreen(), owner_(o)
59 {
60         // the cursor isnt yet visible
61         cursor_pixmap = 0;
62         cursor_pixmap_x = 0;
63         cursor_pixmap_y = 0;
64         cursor_pixmap_w = 0;
65         cursor_pixmap_h = 0;
66
67         // We need this GC
68         gc_copy = createGC();
69 }
70
71
72 XScreen::~XScreen()
73 {
74         XFreeGC(fl_get_display(), gc_copy);
75 }
76
77
78 void XScreen::setCursorColor()
79 {
80         if (!lyxColorHandler.get())
81                 return;
82
83         GC gc = lyxColorHandler->getGCForeground(LColor::cursor);
84
85         XGCValues val;
86         XGetGCValues(fl_get_display(),
87                      gc, GCForeground, &val);
88         XChangeGC(fl_get_display(), gc_copy, GCForeground, &val);
89 }
90
91
92 void XScreen::showManualCursor(LyXText const * text, int x, int y,
93                                  int asc, int desc, Cursor_Shape shape)
94 {
95         // Update the cursor color.
96         setCursorColor();
97
98         int const y1 = max(y - text->top_y() - asc, 0);
99         int const y_tmp = min(y - text->top_y() + desc,
100                               static_cast<int>(owner_.workHeight()));
101
102         // Secure against very strange situations
103         int const y2 = max(y_tmp, y1);
104
105         if (cursor_pixmap) {
106                 XFreePixmap(fl_get_display(), cursor_pixmap);
107                 cursor_pixmap = 0;
108         }
109
110         if (y2 > 0 && y1 < int(owner_.workHeight())) {
111                 cursor_pixmap_h = y2 - y1 + 1;
112                 cursor_pixmap_y = y1;
113
114                 switch (shape) {
115                 case BAR_SHAPE:
116                         cursor_pixmap_w = 1;
117                         cursor_pixmap_x = x;
118                         break;
119                 case L_SHAPE:
120                         cursor_pixmap_w = cursor_pixmap_h/3;
121                         cursor_pixmap_x = x;
122                         break;
123                 case REVERSED_L_SHAPE:
124                         cursor_pixmap_w = cursor_pixmap_h/3;
125                         cursor_pixmap_x = x - cursor_pixmap_w + 1;
126                         break;
127                 }
128
129                 cursor_pixmap =
130                         XCreatePixmap (fl_get_display(),
131                                        fl_root,
132                                        cursor_pixmap_w,
133                                        cursor_pixmap_h,
134                                        fl_get_visual_depth());
135                 XCopyArea (fl_get_display(),
136                            owner_.getWin(),
137                            cursor_pixmap,
138                            gc_copy,
139                            owner_.xpos() + cursor_pixmap_x,
140                            owner_.ypos() + cursor_pixmap_y,
141                            cursor_pixmap_w,
142                            cursor_pixmap_h,
143                            0, 0);
144                 XDrawLine(fl_get_display(),
145                           owner_.getWin(),
146                           gc_copy,
147                           x + owner_.xpos(),
148                           y1 + owner_.ypos(),
149                           x + owner_.xpos(),
150                           y2 + owner_.ypos());
151                 switch (shape) {
152                 case BAR_SHAPE:
153                         break;
154                 case L_SHAPE:
155                 case REVERSED_L_SHAPE:
156                         int const rectangle_h = (cursor_pixmap_h + 10) / 20;
157                         XFillRectangle(fl_get_display(),
158                                        owner_.getWin(),
159                                        gc_copy,
160                                        cursor_pixmap_x + owner_.xpos(),
161                                        y2 - rectangle_h + 1 + owner_.ypos(),
162                                        cursor_pixmap_w - 1, rectangle_h);
163                         break;
164                 }
165
166         }
167         cursor_visible_ = true;
168 }
169
170
171 void XScreen::hideCursor()
172 {
173         if (!cursor_visible_) return;
174
175         if (cursor_pixmap) {
176                 XCopyArea (fl_get_display(),
177                            cursor_pixmap,
178                            owner_.getWin(),
179                            gc_copy,
180                            0, 0,
181                            cursor_pixmap_w, cursor_pixmap_h,
182                            cursor_pixmap_x + owner_.xpos(),
183                            cursor_pixmap_y + owner_.ypos());
184         }
185         cursor_visible_ = false;
186 }
187
188
189 void XScreen::expose(int x, int y, int w, int h)
190 {
191         lyxerr[Debug::GUI] << "expose " << w << 'x' << h
192                 << '+' << x << '+' << y << endl;
193         XCopyArea(fl_get_display(),
194                   owner_.getPixmap(),
195                   owner_.getWin(),
196                   gc_copy,
197                   x, y, w, h,
198                   x + owner_.xpos(),
199                   y + owner_.ypos());
200 }
201
202
203 void XScreen::draw(LyXText * text, BufferView * bv, unsigned int y)
204 {
205         if (cursor_visible_)
206                 hideCursor();
207
208         int const old_first = text->top_y();
209         text->top_y(y);
210
211         // is any optimization possible?
212         if ((y - old_first) < owner_.workHeight()
213             && (old_first - y) < owner_.workHeight())
214         {
215                 if (text->top_y() < old_first) {
216                         drawFromTo(text, bv, 0, old_first - text->top_y(), 0, 0);
217                         XCopyArea(fl_get_display(),
218                                   owner_.getWin(),
219                                   owner_.getWin(),
220                                   gc_copy,
221                                   owner_.xpos(),
222                                   owner_.ypos(),
223                                   owner_.workWidth(),
224                                   owner_.workHeight() - old_first + text->top_y(),
225                                   owner_.xpos(),
226                                   owner_.ypos() + old_first - text->top_y()
227                                 );
228                         // expose the area drawn
229                         expose(0, 0,
230                                owner_.workWidth(),
231                                old_first - text->top_y());
232                 } else  {
233                         drawFromTo(text, bv,
234                                    owner_.workHeight() + old_first - text->top_y(),
235                                    owner_.workHeight(), 0, 0);
236                         XCopyArea(fl_get_display(),
237                                   owner_.getWin(),
238                                   owner_.getWin(),
239                                   gc_copy,
240                                   owner_.xpos(),
241                                   owner_.ypos() + text->top_y() - old_first,
242                                   owner_.workWidth(),
243                                   owner_.workHeight() + old_first - text->top_y(),
244                                   owner_.xpos(),
245                                   owner_.ypos());
246                         // expose the area drawn
247                         expose(0, owner_.workHeight() + old_first - text->top_y(),
248                                owner_.workWidth(), text->top_y() - old_first);
249                 }
250         } else {
251                 // make a dumb new-draw
252                 drawFromTo(text, bv, 0, owner_.workHeight(), 0, 0);
253                 expose(0, 0, owner_.workWidth(), owner_.workHeight());
254         }
255
256         XSync(fl_get_display(), 0);
257 }