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