]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XWorkArea.C
remove preamble dialog from the qt frontend
[lyx.git] / src / frontends / xforms / XWorkArea.C
1 /**
2  * \file XWorkArea.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "XWorkArea.h"
19 #include "debug.h"
20 #include "XLyXKeySym.h"
21 #include "funcrequest.h"
22 #include "Timeout.h"
23
24 using std::endl;
25 using std::abs;
26 using std::hex;
27 using std::dec;
28
29 namespace {
30
31 inline
32 void waitForX(bool discard)
33 {
34         XSync(fl_get_display(), discard);
35 }
36
37
38 mouse_button::state x_button_state(unsigned int button)
39 {
40         mouse_button::state b = mouse_button::none;
41         switch (button) {
42         case FL_MBUTTON1:
43                 b = mouse_button::button1;
44                 break;
45         case FL_MBUTTON2:
46                 b = mouse_button::button2;
47                 break;
48         case FL_MBUTTON3:
49                 b = mouse_button::button3;
50                 break;
51         case FL_MBUTTON4:
52                 b = mouse_button::button4;
53                 break;
54         case FL_MBUTTON5:
55                 b = mouse_button::button5;
56                 break;
57         }
58         return b;
59 }
60
61
62 key_modifier::state x_key_state(unsigned int state)
63 {
64         key_modifier::state k = key_modifier::none;
65         if (state & ControlMask)
66                 k |= key_modifier::ctrl;
67         if (state & ShiftMask)
68                 k |= key_modifier::shift;
69         if (state & Mod1Mask)
70                 k |= key_modifier::alt;
71         return k;
72 }
73
74
75 extern "C" {
76
77 void C_scroll_cb(FL_OBJECT * ob, long)
78 {
79         XWorkArea * area = static_cast<XWorkArea *>(ob->u_vdata);
80         area->scroll_cb();
81 }
82
83
84 int C_work_area_handler(FL_OBJECT * ob, int event, FL_Coord, FL_Coord,
85                         int key, void * xev)
86 {
87         return XWorkArea::work_area_handler(ob, event, 0, 0, key, xev);
88 }
89
90
91 int C_event_cb(FL_FORM * form, void * xev)
92 {
93         XWorkArea * area = static_cast<XWorkArea *>(form->u_vdata);
94         return area->event_cb(static_cast<XEvent *>(xev));
95 }
96
97 } // extern "C"
98 } // namespace anon
99
100
101 XWorkArea::XWorkArea(int x, int y, int w, int h)
102         : workareapixmap(0), painter_(*this)
103 {
104         if (lyxerr.debugging(Debug::WORKAREA)) {
105                 lyxerr << "\tbackground box: +"
106                        << x << '+' << y << ' '
107                        << w - 15 << 'x' << h << endl;
108         }
109
110         fl_freeze_all_forms();
111
112         FL_OBJECT * obj;
113
114         obj = fl_add_box(FL_BORDER_BOX, x, y, w - 15, h, "");
115         fl_set_object_resize(obj, FL_RESIZE_ALL);
116         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
117
118         scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
119                                            x + w - 15, y, 17, h, "");
120         fl_set_object_boxtype(obj, FL_UP_BOX);
121         fl_set_object_resize(obj, FL_RESIZE_ALL);
122         fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
123         obj->u_vdata = this;
124         fl_set_object_callback(obj, C_scroll_cb, 0);
125         fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
126         fl_set_scrollbar_value(scrollbar, 0.0);
127         fl_set_scrollbar_size(scrollbar, scrollbar->h);
128
129         int const bw = int(abs(fl_get_border_width()));
130
131         // Create the workarea pixmap
132         // FIXME remove redraw(w - 15 - 2 * bw, h - 2 * bw);
133
134         if (lyxerr.debugging(Debug::WORKAREA))
135                 lyxerr << "\tfree object: +"
136                        << x + bw << '+' << y + bw << ' '
137                        << w - 15 - 2 * bw << 'x'
138                        << h - 2 * bw << endl;
139
140         // We add this object as late as possible to avoid problems
141         // with drawing.
142         // FIXME: like ??
143         work_area = obj = fl_add_free(FL_ALL_FREE,
144                                       x + bw, y + bw,
145                                       w - 15 - 2 * bw,
146                                       h - 2 * bw, "",
147                                       C_work_area_handler);
148         obj->wantkey = FL_KEY_ALL;
149         obj->u_vdata = this;
150
151         fl_set_object_boxtype(obj,FL_DOWN_BOX);
152         fl_set_object_resize(obj, FL_RESIZE_ALL);
153         fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
154
155         /// X selection hook - xforms gets it wrong
156         fl_current_form->u_vdata = this;
157         fl_register_raw_callback(fl_current_form, FL_ALL_EVENT, C_event_cb);
158
159         fl_unfreeze_all_forms();
160
161         XGCValues val;
162
163         val.function = GXcopy;
164         copy_gc = XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
165                             GCFunction, &val);
166 }
167
168
169 XWorkArea::~XWorkArea()
170 {
171         XFreeGC(fl_get_display(), copy_gc);
172         if (workareapixmap)
173                 XFreePixmap(fl_get_display(), workareapixmap);
174 }
175
176
177 void XWorkArea::redraw(int width, int height)
178 {
179         static int cur_width = -1;
180         static int cur_height = -1;
181
182         if (cur_width == width && cur_height == height && workareapixmap) {
183                 XCopyArea(fl_get_display(),
184                           getPixmap(), getWin(), copy_gc,
185                           0, 0, width, height, xpos(), ypos());
186                 return;
187         }
188
189         cur_width = width;
190         cur_height = height;
191
192         if (lyxerr.debugging(Debug::WORKAREA)) {
193                 lyxerr << "(Re)creating pixmap ("
194                        << width << 'x' << height << ')' << endl;
195         }
196
197         if (workareapixmap) {
198                 XFreePixmap(fl_get_display(), workareapixmap);
199         }
200
201         workareapixmap = XCreatePixmap(fl_get_display(),
202                                        RootWindow(fl_get_display(), 0),
203                                        width,
204                                        height,
205                                        fl_get_visual_depth());
206
207         workAreaResize();
208 }
209
210
211 void XWorkArea::setScrollbarParams(int height, int pos, int line_height)
212 {
213         // we need to cache this for scroll_cb
214         doc_height_ = height;
215
216         if (height == 0) {
217                 fl_set_scrollbar_value(scrollbar, 0.0);
218                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
219                 return;
220         }
221
222         long const work_height = workHeight();
223
224         if (lyxerr.debugging(Debug::GUI)) {
225                 lyxerr << "scroll: height now " << height << '\n'
226                        << "scroll: work_height " << work_height << endl;
227         }
228
229         /* If the text is smaller than the working area, the scrollbar
230          * maximum must be the working area height. No scrolling will
231          * be possible */
232         if (height <= work_height) {
233                 lyxerr[Debug::GUI] << "scroll: doc smaller than workarea !"
234                                    << endl;
235                 fl_set_scrollbar_bounds(scrollbar, 0.0, 0.0);
236                 fl_set_scrollbar_value(scrollbar, pos);
237                 fl_set_scrollbar_size(scrollbar, scrollbar->h);
238                 return;
239         }
240
241         fl_set_scrollbar_bounds(scrollbar, 0.0, height - work_height);
242         fl_set_scrollbar_increment(scrollbar, work_area->h - line_height, line_height);
243
244         fl_set_scrollbar_value(scrollbar, pos);
245
246         double const slider_size =
247                 (height == 0) ? 1.0 : 1.0 / double(height);
248
249         fl_set_scrollbar_size(scrollbar, scrollbar->h * slider_size);
250 }
251
252
253 // callback for scrollbar slider
254 void XWorkArea::scroll_cb()
255 {
256         double const val = fl_get_scrollbar_value(scrollbar);
257
258         if (lyxerr.debugging(Debug::GUI)) {
259                 lyxerr << "scroll: val: " << val << '\n'
260                        << "scroll: height: " << scrollbar->h << '\n'
261                        << "scroll: docheight: " << doc_height_ << endl;
262         }
263
264         scrollDocView(int(val));
265         waitForX(false);
266 }
267
268  
269 int XWorkArea::work_area_handler(FL_OBJECT * ob, int event,
270                                  FL_Coord, FL_Coord,
271                                  int key, void * xev)
272 {
273         XEvent * ev = static_cast<XEvent*>(xev);
274         XWorkArea * area = static_cast<XWorkArea*>(ob->u_vdata);
275
276         if (!area) return 1;
277
278         switch (event) {
279         case FL_DRAW:
280                 if (!area->work_area || !area->work_area->form->visible)
281                         return 1;
282                 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
283                 area->redraw(area->workWidth(), area->workHeight());
284                 break;
285         case FL_PUSH:
286                 if (!ev || ev->xbutton.button == 0) break;
287                 // Should really have used xbutton.state
288                 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
289                 area->dispatch(
290                         FuncRequest(LFUN_MOUSE_PRESS,
291                                     ev->xbutton.x - ob->x,
292                                     ev->xbutton.y - ob->y,
293                                     x_button_state(key)));
294                 break;
295         case FL_RELEASE:
296                 if (!ev || ev->xbutton.button == 0) break;
297                 // Should really have used xbutton.state
298                 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
299                 area->dispatch(
300                         FuncRequest(LFUN_MOUSE_RELEASE,
301                                     ev->xbutton.x - ob->x,
302                                     ev->xbutton.y - ob->y,
303                                     x_button_state(key)));
304                 break;
305         case FL_DRAG:
306         {
307                 if (!ev || !area->scrollbar)
308                         break;
309
310                 int const drag_x = ev->xmotion.x;
311                 int const drag_y = ev->xmotion.y;
312                 int const area_y = ob->y;
313                 int const area_h = ob->h;
314
315                 // Check if the mouse is above or below the workarea
316                 if (drag_y <= area_y || drag_y >= area_y + area_h) {
317                         // The mouse button is depressed and we are outside the
318                         // workarea. That means we are simultaneously selecting
319                         // text and scrolling the view.
320                         // Use a Timeout to react to a drag events only every
321                         // 200ms. All intervening events are discarded,
322                         // allowing the user to control position easily.
323                         static int const discard_interval = 200;
324                         static Timeout timeout(discard_interval);
325
326                         if (timeout.running())
327                                 break;
328                         // The timeout is not running, so process the
329                         // event, first starting the timeout to discard future
330                         // events.
331                         timeout.start();
332                 }
333
334                 static int x_old = -1;
335                 static int y_old = -1;
336                 static double scrollbar_value_old = -1.0;
337
338                 double const scrollbar_value =
339                         fl_get_scrollbar_value(area->scrollbar);
340
341                 if (drag_x != x_old || drag_y != y_old ||
342                     scrollbar_value != scrollbar_value_old) {
343                         x_old = drag_x;
344                         y_old = drag_y;
345                         scrollbar_value_old = scrollbar_value;
346
347                         lyxerr[Debug::WORKAREA] << "Workarea event: DRAG"
348                                                 << endl;
349
350                         // It transpires that ev->xbutton.button == 0 when
351                         // the mouse is dragged, so it cannot be used to
352                         // initialise x_button_state and hence FuncRequest.
353
354                         // The 'key' that is passed into the function does
355                         // contain the necessary info, however.
356
357                         // It is for this reason that x_button_state has
358                         // been modified to work with key
359                         // rather than ev->xbutton.button.
360
361                         // Angus 15 Oct 2002.
362                         FuncRequest cmd(LFUN_MOUSE_MOTION,
363                                         ev->xbutton.x - ob->x,
364                                         ev->xbutton.y - ob->y,
365                                         x_button_state(key));
366                         area->dispatch(cmd);
367                 }
368                 break;
369         }
370
371         case FL_KEYPRESS:
372         {
373                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYPRESS" << endl;
374
375                 KeySym keysym = 0;
376                 char dummy[1];
377                 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
378                 XLookupString(xke, dummy, 1, &keysym, 0);
379
380                 if (lyxerr.debugging(Debug::KEY)) {
381                         char const * const tmp  = XKeysymToString(key);
382                         char const * const tmp2 = XKeysymToString(keysym);
383                         string const stm  = (tmp ? tmp : string());
384                         string const stm2 = (tmp2 ? tmp2 : string());
385
386                         lyxerr << "XWorkArea: Key is `" << stm
387                                << "' [" << key << "]\n"
388                                << "XWorkArea: Keysym is `" << stm2
389                                << "' [" << keysym << ']' << endl;
390                 }
391
392                 // Note that we need this handling because of a bug
393                 // in XForms 0.89, if this bug is resolved in the way I hope
394                 // we can just use the keysym directly without looking
395                 // at key at all. (Lgb)
396                 KeySym ret_key = 0;
397                 if (!key) {
398                         // We might have to add more keysyms here also,
399                         // we will do that as the issues arise. (Lgb)
400                         if (keysym == XK_space) {
401                                 ret_key = keysym;
402                                 lyxerr[Debug::KEY] << "Using keysym [A]"
403                                                    << endl;
404                         } else
405                                 break;
406                 } else {
407                         // It seems that this was a bit optimistic...
408                         // With this hacking things seems to be better (Lgb)
409                         //if (!iscntrl(key)) {
410                         //      ret_key = key;
411                         //      lyxerr[Debug::KEY]
412                         //              << "Using key [B]\n"
413                         //              << "Uchar["
414                         //              << static_cast<unsigned char>(key)
415                         //              << endl;
416                         //} else {
417                         ret_key = (keysym ? keysym : key);
418                         lyxerr[Debug::KEY] << "Using keysym [B]"
419                                            << endl;
420                                 //}
421                 }
422
423                 unsigned int const ret_state = xke->state;
424
425                 // If you have a better way to handle "wild-output" of
426                 // characters after the key has been released than the one
427                 // below, please contact me. (Lgb)
428                 static Time last_time_pressed;
429                 static unsigned int last_key_pressed;
430                 static unsigned int last_state_pressed;
431                 lyxerr[Debug::KEY] << "Workarea Diff: "
432                                    << xke->time - last_time_pressed
433                                    << endl;
434                 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
435                     && ret_state == last_state_pressed
436                     && xke->keycode == last_key_pressed) {
437                         lyxerr[Debug::KEY]
438                                 << "Workarea: Purging X events." << endl;
439                         //lyxerr << "Workarea Events: "
440                         //       << XEventsQueued(fl_get_display(), QueuedAlready)
441                         //       << endl;
442                         if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
443                                 waitForX(true);
444                         // This purge make f.ex. scrolling stop immediately when
445                         // releasing the PageDown button. The question is if
446                         // this purging of XEvents can cause any harm...
447                         // after some testing I can see no problems, but
448                         // I'd like other reports too.
449                         break;
450                 }
451                 last_time_pressed = xke->time;
452                 last_key_pressed = xke->keycode;
453                 last_state_pressed = ret_state;
454
455                 XLyXKeySym * xlk = new XLyXKeySym;
456                 xlk->initFromKeySym(ret_key);
457
458                 area->workAreaKeyPress(LyXKeySymPtr(xlk),
459                                        x_key_state(ret_state));
460         }
461         break;
462
463         case FL_KEYRELEASE:
464                 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
465                 break;
466
467         case FL_ENTER:
468                 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
469                 fl_set_cursor(FL_ObjWin(area->work_area), XC_xterm);
470                 break;
471         case FL_LEAVE:
472                 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
473                 // There should be no need for this. But there is.
474                 fl_set_cursor(FL_ObjWin(area->work_area), FL_DEFAULT_CURSOR);
475                 break;
476         case FL_DBLCLICK:
477                 if (ev) {
478                         lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
479                         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
480                                         ev->xbutton.x - ob->x,
481                                         ev->xbutton.y - ob->y,
482                                         x_button_state(key));
483                         area->dispatch(cmd);
484                 }
485                 break;
486         case FL_TRPLCLICK:
487                 if (ev) {
488                         lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
489                         FuncRequest cmd(LFUN_MOUSE_TRIPLE,
490                                         ev->xbutton.x - ob->x,
491                                         ev->xbutton.y - ob->y,
492                                         x_button_state(key));
493                         area->dispatch(cmd);
494                 }
495                 break;
496         case FL_OTHER:
497                 if (ev)
498                         lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
499                 break;
500         }
501
502         return 1;
503 }
504
505
506 namespace {
507
508 string clipboard_selection;
509 bool clipboard_read = false;
510
511 extern "C" {
512
513 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
514                          void const * data, long size)
515 {
516         clipboard_selection.erase();
517
518         if (size > 0)
519                 clipboard_selection.reserve(size);
520         for (int i = 0; i < size; ++i)
521                 clipboard_selection += static_cast<char const *>(data)[i];
522         clipboard_read = true;
523         return 0;
524 }
525
526 } // extern "C"
527 } // namespace anon
528
529
530 int XWorkArea::event_cb(XEvent * xev)
531 {
532         switch (xev->type) {
533         case SelectionRequest:
534                 lyxerr[Debug::GUI] << "X requested selection." << endl;
535                 selectionRequested();
536                 break;
537         case SelectionClear:
538                 lyxerr[Debug::GUI] << "Lost selection." << endl;
539                 selectionLost();
540                 break;
541         }
542         return 0;
543 }
544
545
546 void XWorkArea::haveSelection(bool yes) const
547 {
548         Window const owner = yes ? FL_ObjWin(work_area) : None;
549         XSetSelectionOwner(fl_get_display(), XA_PRIMARY, owner, CurrentTime);
550 }
551
552
553 string const XWorkArea::getClipboard() const
554 {
555         clipboard_read = false;
556
557         if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
558                 return string();
559
560         XEvent ev;
561
562         while (!clipboard_read) {
563                 if (fl_check_forms() == FL_EVENT) {
564                         fl_XNextEvent(&ev);
565                         lyxerr << "Received unhandled X11 event\n"
566                                << "Type: 0x" << hex << ev.xany.type
567                                << " Target: 0x" << hex << ev.xany.window
568                                << dec << endl;
569                 }
570         }
571         return clipboard_selection;
572 }
573
574
575 void XWorkArea::putClipboard(string const & s) const
576 {
577         static string hold;
578         hold = s;
579
580         fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);
581 }