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