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