]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
Applied Angus patch to compile on DEC C++ and to avoid name clashes
[lyx.git] / src / WorkArea.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 #include <cmath>
13 #include <cctype>
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "WorkArea.h"
19 #include "debug.h"
20 #include "support/lstrings.h"
21 #include "LyXView.h"
22 #include "lyxfunc.h"
23
24 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
25 #include "lyxlookup.h"
26 #endif
27
28 using std::endl;
29
30 FL_OBJECT * figinset_canvas;
31
32 // needed to make the c++ compiler find the correct version of abs.
33 // This is at least true for g++.
34 //using std::abs;
35
36 namespace {
37
38 inline
39 void waitForX()
40 {
41         XSync(fl_get_display(), 0);
42 }
43
44 } // anon namespace
45
46
47 extern "C" {
48 // Just a bunch of C wrappers around static members of WorkArea
49         void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
50         {
51                 WorkArea::scroll_cb(ob, buf);
52         }
53
54         int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
55                                            FL_Coord, FL_Coord, 
56                                            int key, void * xev)
57         {
58                 return WorkArea::work_area_handler(ob, event,
59                                                    0, 0, key, xev);
60         }
61 }
62
63
64 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
65         : workareapixmap(0), painter_(*this)
66 {
67         fl_freeze_all_forms();
68
69         figinset_canvas = 0;
70
71         if (lyxerr.debugging(Debug::GUI))
72                 lyxerr << "Creating work area: +"
73                        << xpos << '+' << ypos << ' '
74                        << width << 'x' << height << endl;
75         //
76         FL_OBJECT * obj;
77         int const bw = int(std::abs(float(fl_get_border_width())));
78
79         // We really want to get rid of figinset_canvas.
80         ::figinset_canvas = figinset_canvas = obj =
81                   fl_add_canvas(FL_NORMAL_CANVAS,
82                                 xpos + 1, ypos + 1, 1, 1, "");
83         fl_set_object_boxtype(obj, FL_NO_BOX);
84         fl_set_object_resize(obj, FL_RESIZE_ALL);
85         fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
86         
87         // a box
88         if (lyxerr.debugging(Debug::GUI))
89                 lyxerr << "\tbackground box: +"
90                        << xpos << '+' << ypos << ' '
91                        << width - 15 << 'x' << height << endl;
92         backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
93                                          xpos, ypos,
94                                          width - 15,
95                                          height,"");
96         fl_set_object_resize(obj, FL_RESIZE_ALL);
97         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
98
99         //
100         // THE SCROLLBAR
101         //
102
103         scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
104                                            xpos + width - 15,
105                                            ypos, 17, height, "");
106         fl_set_object_boxtype(obj, FL_UP_BOX);
107         fl_set_object_resize(obj, FL_RESIZE_ALL);
108         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
109         obj->u_vdata = this;
110         fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
111
112         ///
113         /// The free object
114
115         // Create the workarea pixmap
116         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
117
118         // We add this object as late as possible to avoit problems
119         // with drawing.
120         if (lyxerr.debugging(Debug::GUI))
121                 lyxerr << "\tfree object: +"
122                        << xpos + bw << '+' << ypos + bw << ' '
123                        << width - 15 - 2 * bw << 'x'
124                        << height - 2 * bw << endl;
125         
126         work_area = obj = fl_add_free(FL_ALL_FREE,
127                                       xpos + bw, ypos + bw,
128                                       width - 15 - 2 * bw, // scrollbarwidth
129                                       height - 2 * bw, "",
130                                       C_WorkArea_work_area_handler);
131         obj->wantkey = FL_KEY_ALL;
132         obj->u_vdata = this; /* This is how we pass the WorkArea
133                                 to the work_area_handler. */
134         fl_set_object_boxtype(obj,FL_DOWN_BOX);
135         fl_set_object_resize(obj, FL_RESIZE_ALL);
136         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
137
138         fl_unfreeze_all_forms();
139 }
140
141
142 WorkArea::~WorkArea()
143 {
144         if (workareapixmap)
145                 XFreePixmap(fl_get_display(), workareapixmap);
146 }
147
148
149 bool WorkArea::belowMouse() const
150 {
151         FL_Coord x, y;
152         unsigned int button;
153         fl_get_mouse(&x, &y, &button);
154         FL_Coord ulx = work_area->form->x + work_area->x;
155         FL_Coord uly = work_area->form->y + work_area->y;
156         FL_Coord w = work_area->w;
157         FL_Coord h = work_area->h;
158         if (x > ulx && y > uly && x < ulx + h && y < uly + w)
159                 return true;
160         return false;
161         
162         
163         //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
164         //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
165         //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
166         //return work_area->belowmouse;
167 }
168
169
170 void WorkArea::resize(int xpos, int ypos, int width, int height)
171 {
172         fl_freeze_all_forms();
173         
174         int const bw = int(std::abs(float(fl_get_border_width())));
175
176         // a box
177         fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
178         
179         //
180         // THE SCROLLBAR
181         //
182         fl_set_object_geometry(scrollbar, xpos + width - 15,
183                                ypos, 17, height);
184
185         // Create the workarea pixmap
186         createPixmap(width - 15 - 2 * bw, height - 2 * bw);
187
188         // the free object
189         fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
190                                width - 15 - 2 * bw,
191                                height - 2 * bw);
192
193         fl_unfreeze_all_forms();
194
195 }
196
197
198 void WorkArea::createPixmap(int width, int height)
199 {
200         static int cur_width = -1;
201         static int cur_height = -1;
202
203         if (cur_width == width && cur_height == height && workareapixmap)
204                 return;
205         
206         cur_width = width;
207         cur_height = height;
208
209         if (workareapixmap)
210                 XFreePixmap(fl_get_display(), workareapixmap);
211
212         if (lyxerr.debugging(Debug::GUI))
213                 lyxerr << "Creating pixmap ("
214                        << width << 'x' << height << ")" << endl;
215         
216         workareapixmap = XCreatePixmap(fl_get_display(),
217                                        RootWindow(fl_get_display(), 0),
218                                        width,
219                                        height, 
220                                        fl_get_visual_depth());
221         if (lyxerr.debugging(Debug::GUI))
222                 lyxerr << "\tpixmap=" << workareapixmap << endl;
223 }
224
225
226 void WorkArea::greyOut() const
227 {
228         fl_winset(FL_ObjWin(work_area));
229         fl_rectangle(1, work_area->x, work_area->y,
230                      work_area->w, work_area->h, FL_GRAY63);
231 }
232
233
234 void WorkArea::setFocus() const
235 {
236         fl_set_focus_object(work_area->form, work_area);
237 }
238
239
240 void WorkArea::setScrollbar(double pos, double length_fraction) const
241 {
242         fl_set_scrollbar_value(scrollbar, pos);
243         fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
244 }
245
246
247 void WorkArea::setScrollbarBounds(double l1, double l2) const
248 {
249         fl_set_scrollbar_bounds(scrollbar, l1, l2);
250 }
251
252
253 void WorkArea::setScrollbarIncrements(double inc) const
254 {
255         fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
256 }
257
258
259 // Callback for scrollbar slider
260 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
261 {
262         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
263         // If we really want the accellerating scroll we can do that
264         // from here. IMHO that is a waste of effort since we already
265         // have other ways to move fast around in the document. (Lgb)
266         area->scrollCB(fl_get_scrollbar_value(ob));
267         waitForX();
268 }
269
270
271 bool Lgb_bug_find_hack = false;
272
273 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
274                                 FL_Coord, FL_Coord ,
275                                 int key, void * xev)
276 {
277         static int x_old = -1;
278         static int y_old = -1;
279         static long scrollbar_value_old = -1;
280         
281         XEvent * ev = static_cast<XEvent*>(xev);
282         WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
283
284         if (!area) return 1;
285         
286         switch (event){
287         case FL_DRAW:
288                 if (!area->work_area ||
289                     !area->work_area->form->visible)
290                         return 1;
291                 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
292                 area->createPixmap(area->workWidth(), area->height());
293                 Lgb_bug_find_hack = true;
294                 area->workAreaExpose();
295                 Lgb_bug_find_hack = false;
296                 break;
297         case FL_PUSH:
298                 if (!ev || ev->xbutton.button == 0) break;
299                 // Should really have used xbutton.state
300                 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
301                 area->workAreaButtonPress(ev->xbutton.x - ob->x,
302                                           ev->xbutton.y - ob->y,
303                                           ev->xbutton.button);
304                 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
305                 break; 
306         case FL_RELEASE:
307                 if (!ev || ev->xbutton.button == 0) break;
308                 // Should really have used xbutton.state
309                 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
310                 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
311                                       ev->xbutton.y - ob->y,
312                                       ev->xbutton.button);
313                 break;
314 #if FL_REVISION < 89
315         case FL_MOUSE:
316 #else
317         case FL_DRAG:
318 #endif
319                 if (!ev || ! area->scrollbar) break;
320                 if (ev->xmotion.x != x_old ||
321                     ev->xmotion.y != y_old ||
322                     fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
323                         ) {
324                         lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
325                         area->workAreaMotionNotify(ev->xmotion.x - ob->x,
326                                              ev->xmotion.y - ob->y,
327                                              ev->xbutton.state);
328                 }
329                 break;
330 #if FL_REVISION < 89
331         case FL_KEYBOARD:
332 #else
333         case FL_KEYPRESS:
334 #endif
335         {
336                 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
337                 
338                 KeySym keysym = 0;
339                 char dummy[1];
340                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
341 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
342                 // XForms < 0.89.5 does not have compose support
343                 // so we are using our own compose support
344                 LyXLookupString(ev, dummy, 1, &keysym);
345 #else
346                 XLookupString(xke, dummy, 1, &keysym, 0);
347 #endif
348                 if (lyxerr.debugging(Debug::KEY)) {
349                         char const * tmp = XKeysymToString(key);
350                         char const * tmp2 = XKeysymToString(keysym);
351                         string const stm = (tmp ? tmp : "");
352                         string const stm2 = (tmp2 ? tmp2 : "");
353                         
354                         lyxerr << "WorkArea: Key is `" << stm << "' ["
355                                << key << "]" << endl;
356                         lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
357                                << keysym << "]" << endl;
358                 }
359
360 #if FL_REVISION < 89
361                 if (keysym == NoSymbol) {
362                         lyxerr[Debug::KEY]
363                                 << "Empty kdb action (probably composing)"
364                                 << endl;
365                         break;
366                 }
367                 KeySym ret_key = keysym;
368 #else
369                 // Note that we need this handling because of a bug
370                 // in XForms 0.89, if this bug is resolved in the way I hope
371                 // we can just use the keysym directly with out looking
372                 // at key at all. (Lgb)
373                 KeySym ret_key = 0;
374                 if (!key) {
375                         // We migth have to add more keysyms here also,
376                         // we will do that as the issues arise. (Lgb)
377                         if (keysym == XK_space) {
378                                 ret_key = keysym;
379                                 lyxerr[Debug::KEY] << "Using keysym [A]"
380                                                    << endl;
381                         } else
382                                 break;
383                 } else {
384                         // It seems that this was a bit optimistic...
385                         // With this hacking things seems to be better (Lgb)
386                         //if (!iscntrl(key)) {
387                         //      ret_key = key;
388                         //      lyxerr[Debug::KEY]
389                         //              << "Using key [B]\n"
390                         //              << "Uchar["
391                         //              << static_cast<unsigned char>(key)
392                         //              << endl;
393                         //} else {
394                                 ret_key = (keysym ? keysym : key);
395                                 lyxerr[Debug::KEY] << "Using keysym [B]"
396                                                    << endl;
397                                 //}
398                 }
399                 
400 #endif  
401                 unsigned int const ret_state = xke->state;
402
403                 // If you have a better way to handle "wild-output" of
404                 // characters after the key has been released than the one
405                 // below, please contact me. (Lgb)
406                 static Time last_time_pressed = 0;
407                 static unsigned int last_key_pressed = 0;
408                 static unsigned int last_state_pressed = 0;
409                 lyxerr[Debug::KEY] << "Workarea Diff: "
410                                    << xke->time - last_time_pressed
411                                    << endl;
412                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
413                     && ret_state == last_state_pressed
414                     && xke->keycode == last_key_pressed) {
415                         lyxerr[Debug::KEY]
416                                 << "Workarea: Purging X events." << endl;
417                         //lyxerr << "Workarea Events: "
418                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
419                         //       << endl;
420                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
421                                 XSync(fl_get_display(), 1);
422                         // This purge make f.ex. scrolling stop immidiatly when
423                         // releasing the PageDown button. The question is if
424                         // this purging of XEvents can cause any harm...
425                         // after some testing I can see no problems, but
426                         // I'd like other reports too.
427                         break;
428                 }
429                 last_time_pressed = xke->time;
430                 last_key_pressed = xke->keycode;
431                 last_state_pressed = ret_state;
432                 
433                 area->workAreaKeyPress(ret_key, ret_state);
434         }
435         break;
436
437 #if FL_REVISION >= 89
438         case FL_KEYRELEASE:
439                 lyxerr << "Workarea event: KEYRELEASE" << endl;
440                 break;
441 #endif
442
443         case FL_FOCUS:
444                 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
445                 area->workAreaFocus();
446                 break;
447         case FL_UNFOCUS:
448                 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
449                 area->workAreaUnfocus();
450                 break;
451         case FL_ENTER:
452                 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
453                 area->workAreaEnter();
454                 break;
455         case FL_LEAVE:
456                 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
457                 area->workAreaLeave();
458                 break;
459         case FL_DBLCLICK:
460                 if (!ev) break;
461                 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
462                 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
463                                           ev->xbutton.y - ob->y,
464                                           ev->xbutton.button);
465                 break;
466         case FL_TRPLCLICK:
467                 if (!ev) break;
468                 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
469                 area->workAreaTripleClick(ev->xbutton.x - ob->x,
470                                           ev->xbutton.y - ob->y,
471                                           ev->xbutton.button);
472                 break;
473         case FL_OTHER:
474                 if (!ev) break;
475                         lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
476
477                 break;
478         }
479   
480         return 1;
481 }
482
483
484 namespace {
485
486 string clipboard_selection;
487 bool clipboard_read = false;
488
489 extern "C"
490 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
491                         void const * data, long size) 
492 {
493         clipboard_selection.erase();
494
495         if (size > 0)
496                 clipboard_selection.reserve(size);
497         for (int i = 0; i < size; ++i)
498                 clipboard_selection += static_cast<char const *>(data)[i];
499         clipboard_read = true;
500         return 0;
501 }
502
503 } // namespace anon
504
505 string const WorkArea::getClipboard() const 
506 {
507         clipboard_read = false;
508         
509         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
510                 return string();
511
512         XEvent ev;
513         
514         while (!clipboard_read) {
515                 if (fl_check_forms() == FL_EVENT) {
516                         lyxerr << "LyX: This shouldn't happen..." << endl;
517                         fl_XNextEvent(&ev);
518                 }
519         }
520         return clipboard_selection;
521 }
522
523         
524 void WorkArea::putClipboard(string const & s) const
525 {
526         static string hold;
527         hold = s;
528         
529         fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);
530 }