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