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