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