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