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