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