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