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