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