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