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