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