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