]> git.lyx.org Git - lyx.git/blob - src/WorkArea.C
fix typo that put too many include paths for most people
[lyx.git] / src / 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 "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 "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 #endif
405                 if (lyxerr.debugging(Debug::KEY)) {
406                         char const * tmp = XKeysymToString(key);
407                         char const * tmp2 = XKeysymToString(keysym);
408                         string const stm = (tmp ? tmp : "");
409                         string const stm2 = (tmp2 ? tmp2 : "");
410
411                         lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
412                                << key << "]" << endl;
413                         lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
414                                << keysym << "]" << endl;
415                 }
416
417 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
418                 if (keysym == NoSymbol) {
419                         lyxerr[Debug::KEY]
420                                 << "Empty kdb action (probably composing)"
421                                 << endl;
422                         break;
423                 }
424                 KeySym ret_key = keysym;
425 #else
426                 // Note that we need this handling because of a bug
427                 // in XForms 0.89, if this bug is resolved in the way I hope
428                 // we can just use the keysym directly with out looking
429                 // at key at all. (Lgb)
430                 KeySym ret_key = 0;
431                 if (!key) {
432                         // We migth have to add more keysyms here also,
433                         // we will do that as the issues arise. (Lgb)
434                         if (keysym == XK_space) {
435                                 ret_key = keysym;
436                                 lyxerr[Debug::KEY] << "Using keysym [A]"
437                                                    << endl;
438                         } else
439                                 break;
440                 } else {
441                         // It seems that this was a bit optimistic...
442                         // With this hacking things seems to be better (Lgb)
443                         //if (!iscntrl(key)) {
444                         //      ret_key = key;
445                         //      lyxerr[Debug::KEY]
446                         //              << "Using key [B]\n"
447                         //              << "Uchar["
448                         //              << static_cast<unsigned char>(key)
449                         //              << endl;
450                         //} else {
451                                 ret_key = (keysym ? keysym : key);
452                                 lyxerr[Debug::KEY] << "Using keysym [B]"
453                                                    << endl;
454                                 //}
455                 }
456
457 #endif
458                 unsigned int const ret_state = xke->state;
459
460                 // If you have a better way to handle "wild-output" of
461                 // characters after the key has been released than the one
462                 // below, please contact me. (Lgb)
463                 static Time last_time_pressed;
464                 static unsigned int last_key_pressed;
465                 static unsigned int last_state_pressed;
466                 lyxerr[Debug::KEY] << "Workarea Diff: "
467                                    << xke->time - last_time_pressed
468                                    << endl;
469                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
470                     && ret_state == last_state_pressed
471                     && xke->keycode == last_key_pressed) {
472                         lyxerr[Debug::KEY]
473                                 << "Workarea: Purging X events." << endl;
474                         //lyxerr << "Workarea Events: "
475                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
476                         //       << endl;
477                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
478                                 XSync(fl_get_display(), 1);
479                         // This purge make f.ex. scrolling stop immidiatly when
480                         // releasing the PageDown button. The question is if
481                         // this purging of XEvents can cause any harm...
482                         // after some testing I can see no problems, but
483                         // I'd like other reports too.
484                         break;
485                 }
486                 last_time_pressed = xke->time;
487                 last_key_pressed = xke->keycode;
488                 last_state_pressed = ret_state;
489
490                 area->workAreaKeyPress(ret_key, ret_state);
491         }
492         break;
493
494 #if FL_REVISION >= 89
495         case FL_KEYRELEASE:
496                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
497                 break;
498 #endif
499
500         case FL_FOCUS:
501                 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
502                 area->workAreaFocus();
503                 break;
504         case FL_UNFOCUS:
505                 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
506                 area->workAreaUnfocus();
507                 break;
508         case FL_ENTER:
509                 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
510                 area->workAreaEnter();
511                 break;
512         case FL_LEAVE:
513                 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
514                 area->workAreaLeave();
515                 break;
516         case FL_DBLCLICK:
517                 if (!ev) break;
518                 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
519                 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
520                                           ev->xbutton.y - ob->y,
521                                           ev->xbutton.button);
522                 break;
523         case FL_TRPLCLICK:
524                 if (!ev) break;
525                 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
526                 area->workAreaTripleClick(ev->xbutton.x - ob->x,
527                                           ev->xbutton.y - ob->y,
528                                           ev->xbutton.button);
529                 break;
530         case FL_OTHER:
531                 if (!ev) break;
532                 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
533                 break;
534         }
535
536         return 1;
537 }
538
539
540 namespace {
541
542 string clipboard_selection;
543 bool clipboard_read = false;
544
545 extern "C" {
546
547         static
548         int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
549                                  void const * data, long size)
550         {
551                 clipboard_selection.erase();
552
553                 if (size > 0)
554                         clipboard_selection.reserve(size);
555                 for (int i = 0; i < size; ++i)
556                         clipboard_selection +=
557                                 static_cast<char const *>(data)[i];
558                 clipboard_read = true;
559                 return 0;
560         }
561
562 }
563
564 } // namespace anon
565
566
567 int WorkArea::event_cb(XEvent * xev)
568 {
569         int ret = 0;
570         switch (xev->type) {
571                 case SelectionRequest:
572                         lyxerr[Debug::GUI] << "X requested selection." << endl;
573                         selectionRequested.emit();
574 //                      ret = 1;
575                         break;
576                 case SelectionClear:
577                         lyxerr[Debug::GUI] << "Lost selection." << endl;
578                         selectionLost.emit();
579 //                      ret = 1;
580                         break;
581         }
582         return ret;
583 }
584
585
586 void WorkArea::haveSelection(bool yes) const
587 {
588         if (!yes) {
589                 XSetSelectionOwner(fl_get_display(), XA_PRIMARY, None, CurrentTime);
590                 return;
591         }
592
593         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, FL_ObjWin(work_area), CurrentTime);
594 }
595
596
597 string const WorkArea::getClipboard() const
598 {
599         clipboard_read = false;
600
601         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
602                 return string();
603
604         XEvent ev;
605
606         while (!clipboard_read) {
607                 if (fl_check_forms() == FL_EVENT) {
608                         fl_XNextEvent(&ev);
609                         lyxerr << "Received unhandled X11 event" << endl;
610                         lyxerr << "Type: 0x" << hex << ev.xany.type <<
611                                 " Target: 0x" << hex << ev.xany.window << endl;
612                 }
613         }
614         return clipboard_selection;
615 }
616
617
618 void WorkArea::putClipboard(string const & s) const
619 {
620         static string hold;
621         hold = s;
622
623         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
624 }