]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XWorkArea.C
Performance patches for tabulars from Edwin and John. Removed some
[lyx.git] / src / frontends / xforms / XWorkArea.C
1 /**
2  * \file XWorkArea.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "XWorkArea.h"
17 #include "debug.h"
18 #include "LyXView.h"
19 #include "XLyXKeySym.h"
20 #include "ColorHandler.h"
21
22 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
23 #include "lyxlookup.h"
24 #endif
25
26 #include "support/filetools.h" // LibFileSearch
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
29
30 #include <cmath>
31 #include <cctype>
32
33 // xforms doesn't define this (but it should be in <forms.h>).
34 extern "C"
35 FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
36
37 using std::endl;
38 using std::abs;
39 using std::hex;
40
41 namespace {
42
43 inline
44 void waitForX()
45 {
46         XSync(fl_get_display(), 0);
47 }
48
49
50 void setXtermCursor(Window win)
51 {
52         static Cursor cursor;
53         static bool cursor_undefined = true;
54         if (cursor_undefined) {
55                 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
56                 XFlush(fl_get_display());
57                 cursor_undefined = false;
58         }
59         XDefineCursor(fl_get_display(), win, cursor);
60         XFlush(fl_get_display());
61 }
62
63
64 mouse_button::state x_button_state(unsigned int button)
65 {
66         mouse_button::state b = mouse_button::none;
67         switch (button) {
68                 case Button1:
69                         b = mouse_button::button1;
70                         break;
71                 case Button2:
72                         b = mouse_button::button2;
73                         break;
74                 case Button3:
75                         b = mouse_button::button3;
76                         break;
77                 case Button4:
78                         b = mouse_button::button4;
79                         break;
80                 case Button5:
81                         b = mouse_button::button5;
82                         break;
83                 default: // FIXME
84                         break;
85         }
86         return b;
87 }
88
89
90 mouse_button::state x_motion_state(unsigned int state)
91 {
92         mouse_button::state b = mouse_button::none;
93         if (state & Button1MotionMask)
94                 b |= mouse_button::button1;
95         if (state & Button2MotionMask)
96                 b |= mouse_button::button2;
97         if (state & Button3MotionMask)
98                 b |= mouse_button::button3;
99         if (state & Button4MotionMask)
100                 b |= mouse_button::button4;
101         if (state & Button5MotionMask)
102                 b |= mouse_button::button5;
103         return b;
104 }
105
106
107 key_modifier::state x_key_state(unsigned int state)
108 {
109         key_modifier::state k = key_modifier::none;
110         if (state & ControlMask)
111                 k |= key_modifier::ctrl;
112         if (state & ShiftMask)
113                 k |= key_modifier::shift;
114         if (state & Mod1Mask)
115                 k |= key_modifier::alt;
116         return k;
117 }
118
119
120 } // anon namespace
121
122
123 extern "C" {
124         // Just a bunch of C wrappers around static members of XWorkArea
125         static
126         void C_XWorkArea_scroll_cb(FL_OBJECT * ob, long)
127         {
128                 XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
129                 area->scroll_cb();
130         }
131
132
133         static
134         int C_XWorkArea_work_area_handler(FL_OBJECT * ob, int event,
135                                          FL_Coord, FL_Coord,
136                                          int key, void * xev)
137         {
138                 return XWorkArea::work_area_handler(ob, event,
139                                                    0, 0, key, xev);
140         }
141
142         static
143         int C_XWorkAreaEventCB(FL_FORM * form, void * xev) {
144                 XWorkArea * wa = static_cast<XWorkArea*>(form->u_vdata);
145                 return wa->event_cb(static_cast<XEvent*>(xev));
146         }
147 }
148
149
150 XWorkArea::XWorkArea(int x, int y, int w, int h)
151         : workareapixmap(0), painter_(*this)
152 {
153         fl_freeze_all_forms();
154
155         FL_OBJECT * obj;
156
157         if (lyxerr.debugging(Debug::WORKAREA))
158                 lyxerr << "\tbackground box: +"
159                        << x << '+' << y << ' '
160                        << w - 15 << 'x' << h << endl;
161         backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
162                                          x, y,
163                                          w - 15,
164                                          h, "");
165         fl_set_object_resize(obj, FL_RESIZE_ALL);
166         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
167
168         scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
169                                            x + w - 15,
170                                            y, 17, h, "");
171         fl_set_object_boxtype(obj, FL_UP_BOX);
172         fl_set_object_resize(obj, FL_RESIZE_ALL);
173         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
174         obj->u_vdata = this;
175         fl_set_object_callback(obj, C_XWorkArea_scroll_cb, 0);
176         fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
177         fl_set_scrollbar_value(scrollbar, 0.0);
178         fl_set_scrollbar_size(scrollbar, scrollbar->h);
179
180         int const bw = int(abs(fl_get_border_width()));
181
182         // Create the workarea pixmap
183         // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
184
185         if (lyxerr.debugging(Debug::WORKAREA))
186                 lyxerr << "\tfree object: +"
187                        << x + bw << '+' << y + bw << ' '
188                        << w - 15 - 2 * bw << 'x'
189                        << h - 2 * bw << endl;
190
191         // We add this object as late as possible to avoid problems
192         // with drawing.
193         // FIXME: like ??
194         work_area = obj = fl_add_free(FL_ALL_FREE,
195                                       x + bw, y + bw,
196                                       w - 15 - 2 * bw,
197                                       h - 2 * bw, "",
198                                       C_XWorkArea_work_area_handler);
199         obj->wantkey = FL_KEY_ALL;
200         obj->u_vdata = this;
201
202         fl_set_object_boxtype(obj,FL_DOWN_BOX);
203         fl_set_object_resize(obj, FL_RESIZE_ALL);
204         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
205
206         /// X selection hook - xforms gets it wrong
207         fl_current_form->u_vdata = this;
208         fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_XWorkAreaEventCB);
209
210         fl_unfreeze_all_forms();
211
212         XGCValues val;
213
214         val.function = GXcopy;
215         copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
216                 GCFunction, &val);
217 }
218
219
220 XWorkArea::~XWorkArea()
221 {
222         XFreeGC(fl_get_display(), copy_gc);
223         if (workareapixmap)
224                 XFreePixmap(fl_get_display(), workareapixmap);
225 }
226
227
228 namespace {
229 void destroy_object(FL_OBJECT * obj)
230 {
231         if (!obj)
232                 return;
233
234         if (obj->visible) {
235                 fl_hide_object(obj);
236         }
237         fl_delete_object(obj);
238         fl_free_object(obj);
239 }
240 } // namespace anon
241
242
243 void XWorkArea::redraw(int width, int height)
244 {
245         static int cur_width = -1;
246         static int cur_height = -1;
247
248         if (cur_width == width && cur_height == height && workareapixmap) {
249                 XCopyArea(fl_get_display(),
250                         getPixmap(), getWin(), copy_gc,
251                         0, 0, width, height, xpos(), ypos());
252                 return;
253         }
254
255         cur_width = width;
256         cur_height = height;
257
258         if (lyxerr.debugging(Debug::WORKAREA)) {
259                 lyxerr << "(Re)creating pixmap ("
260                        << width << 'x' << height << ")" << endl;
261         }
262
263         if (workareapixmap) {
264                 XFreePixmap(fl_get_display(), workareapixmap);
265         }
266
267         workareapixmap = XCreatePixmap(fl_get_display(),
268                                        RootWindow(fl_get_display(), 0),
269                                        width,
270                                        height,
271                                        fl_get_visual_depth());
272  
273         workAreaResize();
274 }
275
276
277 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
278 {
279         // we need to cache this for scroll_cb
280         doc_height_ = height;
281
282         if (height == 0) {
283                 fl_set_scrollbar_value(scrollbar, 0.0);
284                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
285                 return;
286         }
287
288         long const work_height = workHeight();
289
290         lyxerr[Debug::GUI] << "scroll: height now " << height << endl;
291         lyxerr[Debug::GUI] << "scroll: work_height " << work_height << endl;
292
293         /* If the text is smaller than the working area, the scrollbar
294          * maximum must be the working area height. No scrolling will
295          * be possible */
296         if (height <= work_height) {
297                 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !" << endl;
298                 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
299                 fl_set_scrollbar_value(scrollbar, pos);
300                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
301                 return;
302         }
303
304         fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
305         fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
306
307         fl_set_scrollbar_value(scrollbar, pos);
308
309         double const slider_size =
310                 (height == 0) ? 1.0 : 1.0 / double(height);
311
312         fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
313 }
314
315
316 // callback for scrollbar slider
317 void XWorkArea::scroll_cb()
318 {
319         double const val = fl_get_scrollbar_value(scrollbar);
320         lyxerr[Debug::GUI] << "scroll: val: " << val << endl;
321         lyxerr[Debug::GUI] << "scroll: height: " << scrollbar->h << endl;
322         lyxerr[Debug::GUI] << "scroll: docheight: " << doc_height_ << endl;
323         scrollDocView(int(val));
324         waitForX();
325 }
326
327
328 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
329                                 FL_Coord, FL_Coord ,
330                                 int key, void * xev)
331 {
332         static int x_old = -1;
333         static int y_old = -1;
334         static long scrollbar_value_old = -1;
335
336         XEvent * ev = static_cast<XEvent*>(xev);
337         XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
338
339         if (!area) return 1;
340
341         switch (event) {
342         case FL_DRAW:
343                 if (!area->work_area ||
344                     !area->work_area->form->visible)
345                         return 1;
346                 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
347                 area->redraw(area->workWidth(), area->workHeight());
348                 break;
349         case FL_PUSH:
350                 if (!ev || ev->xbutton.button == 0) break;
351                 // Should really have used xbutton.state
352                 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
353                 area->workAreaButtonPress(ev->xbutton.x - ob->x,
354                                           ev->xbutton.y - ob->y,
355                                           x_button_state(ev->xbutton.button));
356                 break;
357         case FL_RELEASE:
358                 if (!ev || ev->xbutton.button == 0) break;
359                 // Should really have used xbutton.state
360                 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
361                 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
362                                       ev->xbutton.y - ob->y,
363                                       x_button_state(ev->xbutton.button));
364                 break;
365 #if FL_VERSION < 1 && FL_REVISION < 89
366         case FL_MOUSE:
367 #else
368         case FL_DRAG:
369 #endif
370                 if (!ev || ! area->scrollbar) break;
371                 if (ev->xmotion.x != x_old ||
372                     ev->xmotion.y != y_old ||
373                     fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
374                         ) {
375                         x_old = ev->xmotion.x;
376                         y_old = ev->xmotion.y;
377                         scrollbar_value_old = fl_get_scrollbar_value(area->scrollbar);
378                         lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
379                         area->workAreaMotionNotify(ev->xmotion.x - ob->x,
380                                              ev->xmotion.y - ob->y,
381                                              x_motion_state(ev->xbutton.state));
382                 }
383                 break;
384 #if FL_VERSION < 1 && FL_REVISION < 89
385         case FL_KEYBOARD:
386 #else
387         case FL_KEYPRESS:
388 #endif
389         {
390                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
391
392                 KeySym keysym = 0;
393                 char dummy[1];
394                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
395 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
396                 // XForms < 0.89.5 does not have compose support
397                 // so we are using our own compose support
398                 LyXLookupString(ev, dummy, 1, &keysym);
399 #else
400                 XLookupString(xke, dummy, 1, &keysym, 0);
401 //              int num_keys = XLookupString(xke, dummy, 10, &keysym, &xcs);
402 //              lyxerr << "We have " << num_keys << " keys in the returned buffer" << endl;
403 //              lyxerr << "Our dummy string is " << dummy << endl;
404 #endif
405
406                 if (lyxerr.debugging(Debug::KEY)) {
407                         char const * tmp = XKeysymToString(key);
408                         char const * tmp2 = XKeysymToString(keysym);
409                         string const stm = (tmp ? tmp : "");
410                         string const stm2 = (tmp2 ? tmp2 : "");
411
412                         lyxerr[Debug::KEY] << "XWorkArea: Key is `" << stm << "' ["
413                                << key << "]" << endl;
414                         lyxerr[Debug::KEY] << "XWorkArea: Keysym is `" << stm2 << "' ["
415                                << keysym << "]" << endl;
416                 }
417
418 #if FL_VERSION < 1 && (FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5))
419                 if (keysym == NoSymbol) {
420                         lyxerr[Debug::KEY]
421                                 << "Empty kdb action (probably composing)"
422                                 << endl;
423                         break;
424                 }
425                 KeySym ret_key = keysym;
426 #else
427                 // Note that we need this handling because of a bug
428                 // in XForms 0.89, if this bug is resolved in the way I hope
429                 // we can just use the keysym directly with out looking
430                 // at key at all. (Lgb)
431                 KeySym ret_key = 0;
432                 if (!key) {
433                         // We migth have to add more keysyms here also,
434                         // we will do that as the issues arise. (Lgb)
435                         if (keysym == XK_space) {
436                                 ret_key = keysym;
437                                 lyxerr[Debug::KEY] << "Using keysym [A]"
438                                                    << endl;
439                         } else
440                                 break;
441                 } else {
442                         // It seems that this was a bit optimistic...
443                         // With this hacking things seems to be better (Lgb)
444                         //if (!iscntrl(key)) {
445                         //      ret_key = key;
446                         //      lyxerr[Debug::KEY]
447                         //              << "Using key [B]\n"
448                         //              << "Uchar["
449                         //              << static_cast<unsigned char>(key)
450                         //              << endl;
451                         //} else {
452                                 ret_key = (keysym ? keysym : key);
453                                 lyxerr[Debug::KEY] << "Using keysym [B]"
454                                                    << endl;
455                                 //}
456                 }
457
458 #endif
459                 unsigned int const ret_state = xke->state;
460
461                 // If you have a better way to handle "wild-output" of
462                 // characters after the key has been released than the one
463                 // below, please contact me. (Lgb)
464                 static Time last_time_pressed;
465                 static unsigned int last_key_pressed;
466                 static unsigned int last_state_pressed;
467                 lyxerr[Debug::KEY] << "Workarea Diff: "
468                                    << xke->time - last_time_pressed
469                                    << endl;
470                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
471                     && ret_state == last_state_pressed
472                     && xke->keycode == last_key_pressed) {
473                         lyxerr[Debug::KEY]
474                                 << "Workarea: Purging X events." << endl;
475                         //lyxerr << "Workarea Events: "
476                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
477                         //       << endl;
478                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
479                                 XSync(fl_get_display(), 1);
480                         // This purge make f.ex. scrolling stop immidiatly when
481                         // releasing the PageDown button. The question is if
482                         // this purging of XEvents can cause any harm...
483                         // after some testing I can see no problems, but
484                         // I'd like other reports too.
485                         break;
486                 }
487                 last_time_pressed = xke->time;
488                 last_key_pressed = xke->keycode;
489                 last_state_pressed = ret_state;
490
491                 XLyXKeySym * xlk = new XLyXKeySym;
492                 xlk->initFromKeySym(ret_key);
493
494                 area->workAreaKeyPress(LyXKeySymPtr(xlk),
495                                        x_key_state(ret_state));
496         }
497         break;
498
499 #if FL_VERSION > 0 || FL_REVISION >= 89
500         case FL_KEYRELEASE:
501                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
502                 break;
503 #endif
504
505         case FL_ENTER:
506                 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
507                 break;
508         case FL_LEAVE:
509                 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
510                 break;
511         case FL_DBLCLICK:
512                 if (!ev) break;
513                 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
514                 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
515                                           ev->xbutton.y - ob->y,
516                                           x_button_state(ev->xbutton.button));
517                 break;
518         case FL_TRPLCLICK:
519                 if (!ev) break;
520                 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
521                 area->workAreaTripleClick(ev->xbutton.x - ob->x,
522                                           ev->xbutton.y - ob->y,
523                                           x_button_state(ev->xbutton.button));
524                 break;
525         case FL_OTHER:
526                 if (!ev) break;
527                 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
528                 break;
529         }
530
531         return 1;
532 }
533
534
535 namespace {
536
537 string clipboard_selection;
538 bool clipboard_read = false;
539
540 extern "C" {
541
542         static
543         int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
544                                  void const * data, long size)
545         {
546                 clipboard_selection.erase();
547
548                 if (size > 0)
549                         clipboard_selection.reserve(size);
550                 for (int i = 0; i < size; ++i)
551                         clipboard_selection +=
552                                 static_cast<char const *>(data)[i];
553                 clipboard_read = true;
554                 return 0;
555         }
556
557 }
558
559 } // namespace anon
560
561
562 int XWorkArea::event_cb(XEvent * xev)
563 {
564         int ret = 0;
565         switch (xev->type) {
566                 case SelectionRequest:
567                         lyxerr[Debug::GUI] << "X requested selection." << endl;
568                         selectionRequested();
569                         break;
570                 case SelectionClear:
571                         lyxerr[Debug::GUI] << "Lost selection." << endl;
572                         selectionLost();
573                         break;
574         }
575         return ret;
576 }
577
578
579 void XWorkArea::haveSelection(bool yes) const
580 {
581         if (!yes) {
582                 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
583                 return;
584         }
585
586         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
587 }
588
589
590 string const XWorkArea::getClipboard() const
591 {
592         clipboard_read = false;
593
594         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
595                 return string();
596
597         XEvent ev;
598
599         while (!clipboard_read) {
600                 if (fl_check_forms() == FL_EVENT) {
601                         fl_XNextEvent(&ev);
602                         lyxerr << "Received unhandled X11 event" << endl;
603                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
604                                 " Target: 0x" << hex << ev.xany.window << endl;
605                 }
606         }
607         return clipboard_selection;
608 }
609
610
611 void XWorkArea::putClipboard(string const & s) const
612 {
613         static string hold;
614         hold = s;
615
616         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
617 }