1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
15 #pragma implementation
20 #include "support/lstrings.h"
22 #include "support/filetools.h" // LibFileSearch
23 #include "lyxrc.h" // lyxrc.show_banner
24 #include "version.h" // lyx_version
25 #include "support/LAssert.h"
27 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
28 #include "lyxlookup.h"
33 FL_OBJECT * figinset_canvas;
35 // needed to make the c++ compiler find the correct version of abs.
36 // This is at least true for g++.
44 XSync(fl_get_display(), 0);
51 // Just a bunch of C wrappers around static members of WorkArea
53 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
55 WorkArea::scroll_cb(ob, buf);
60 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
64 return WorkArea::work_area_handler(ob, event,
70 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
71 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
73 fl_freeze_all_forms();
77 if (lyxerr.debugging(Debug::WORKAREA))
78 lyxerr << "Creating work area: +"
79 << xpos << '+' << ypos << ' '
80 << width << 'x' << height << endl;
83 int const bw = int(std::abs(float(fl_get_border_width())));
85 // We really want to get rid of figinset_canvas.
86 ::figinset_canvas = figinset_canvas = obj =
87 fl_add_canvas(FL_NORMAL_CANVAS,
88 xpos + 1, ypos + 1, 1, 1, "");
89 fl_set_object_boxtype(obj, FL_NO_BOX);
90 fl_set_object_resize(obj, FL_RESIZE_ALL);
91 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
94 if (lyxerr.debugging(Debug::WORKAREA))
95 lyxerr << "\tbackground box: +"
96 << xpos << '+' << ypos << ' '
97 << width - 15 << 'x' << height << endl;
98 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
102 fl_set_object_resize(obj, FL_RESIZE_ALL);
103 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
105 // Add a splash screen to the centre of the work area
106 string const splash_file = (lyxrc.show_banner) ?
107 LibFileSearch("images", "banner", "xpm") : string();
109 if (!splash_file.empty()) {
110 int const splash_w = 425;
111 int const splash_h = 290;
112 int const splash_x = xpos + (width - 15 - splash_w) / 2;
113 int const splash_y = ypos + (height - splash_h) / 2;
115 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
117 splash_w, splash_h, "");
118 fl_set_pixmapbutton_file(obj, splash_file.c_str());
119 fl_set_pixmapbutton_focus_outline(obj, 3);
120 fl_set_object_boxtype(obj, FL_NO_BOX);
122 int const text_x = splash_x + 260;
123 int const text_y = splash_y + 255;
125 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
127 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
128 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
129 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
130 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
131 fl_set_object_lcol(obj, FL_FREE_COL3);
132 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
133 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
140 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
142 ypos, 17, height, "");
143 fl_set_object_boxtype(obj, FL_UP_BOX);
144 fl_set_object_resize(obj, FL_RESIZE_ALL);
145 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
147 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
152 // Create the workarea pixmap
153 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
155 // We add this object as late as possible to avoit problems
157 if (lyxerr.debugging(Debug::WORKAREA))
158 lyxerr << "\tfree object: +"
159 << xpos + bw << '+' << ypos + bw << ' '
160 << width - 15 - 2 * bw << 'x'
161 << height - 2 * bw << endl;
163 work_area = obj = fl_add_free(FL_ALL_FREE,
164 xpos + bw, ypos + bw,
165 width - 15 - 2 * bw, // scrollbarwidth
167 C_WorkArea_work_area_handler);
168 obj->wantkey = FL_KEY_ALL;
169 obj->u_vdata = this; /* This is how we pass the WorkArea
170 to the work_area_handler. */
171 fl_set_object_boxtype(obj,FL_DOWN_BOX);
172 fl_set_object_resize(obj, FL_RESIZE_ALL);
173 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
175 fl_unfreeze_all_forms();
179 WorkArea::~WorkArea()
182 XFreePixmap(fl_get_display(), workareapixmap);
186 bool WorkArea::belowMouse() const
190 fl_get_mouse(&x, &y, &button);
191 FL_Coord ulx = work_area->form->x + work_area->x;
192 FL_Coord uly = work_area->form->y + work_area->y;
193 FL_Coord w = work_area->w;
194 FL_Coord h = work_area->h;
195 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
201 void WorkArea::resize(int xpos, int ypos, int width, int height)
203 fl_freeze_all_forms();
205 int const bw = int(std::abs(float(fl_get_border_width())));
208 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
213 fl_set_object_geometry(scrollbar, xpos + width - 15,
216 // Create the workarea pixmap
217 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
220 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
224 fl_unfreeze_all_forms();
229 void destroy_object(FL_OBJECT * obj)
237 fl_delete_object(obj);
243 void WorkArea::createPixmap(int width, int height)
245 // Three calls to createPixmap are needed to draw the initial view
246 // of LyX. Any more and the splash is destroyed.
247 static int counter = 0;
248 if (++counter == 4) {
249 destroy_object(splash_);
251 destroy_object(splash_text_);
255 static int cur_width = -1;
256 static int cur_height = -1;
258 if (cur_width == width && cur_height == height && workareapixmap)
265 XFreePixmap(fl_get_display(), workareapixmap);
267 if (lyxerr.debugging(Debug::WORKAREA))
268 lyxerr << "Creating pixmap ("
269 << width << 'x' << height << ")" << endl;
271 workareapixmap = XCreatePixmap(fl_get_display(),
272 RootWindow(fl_get_display(), 0),
275 fl_get_visual_depth());
276 if (lyxerr.debugging(Debug::WORKAREA))
277 lyxerr << "\tpixmap=" << workareapixmap << endl;
281 void WorkArea::greyOut() const
284 fl_winset(FL_ObjWin(work_area));
285 fl_rectangle(1, work_area->x, work_area->y,
286 work_area->w, work_area->h, FL_GRAY63);
291 void WorkArea::setFocus() const
293 fl_set_focus_object(work_area->form, work_area);
297 void WorkArea::setScrollbar(double pos, double length_fraction) const
299 fl_set_scrollbar_value(scrollbar, pos);
300 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
304 void WorkArea::setScrollbarBounds(double l1, double l2) const
306 fl_set_scrollbar_bounds(scrollbar, l1, l2);
310 void WorkArea::setScrollbarIncrements(double inc) const
312 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
316 // Callback for scrollbar slider
317 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
319 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
320 // If we really want the accellerating scroll we can do that
321 // from here. IMHO that is a waste of effort since we already
322 // have other ways to move fast around in the document. (Lgb)
323 area->scrollCB(fl_get_scrollbar_value(ob));
328 bool Lgb_bug_find_hack = false;
330 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
334 static int x_old = -1;
335 static int y_old = -1;
336 static long scrollbar_value_old = -1;
338 XEvent * ev = static_cast<XEvent*>(xev);
339 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
345 if (!area->work_area ||
346 !area->work_area->form->visible)
348 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
349 area->createPixmap(area->workWidth(), area->height());
350 Lgb_bug_find_hack = true;
351 area->workAreaExpose();
352 Lgb_bug_find_hack = false;
355 if (!ev || ev->xbutton.button == 0) break;
356 // Should really have used xbutton.state
357 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
358 area->workAreaButtonPress(ev->xbutton.x - ob->x,
359 ev->xbutton.y - ob->y,
361 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
364 if (!ev || ev->xbutton.button == 0) break;
365 // Should really have used xbutton.state
366 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
367 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
368 ev->xbutton.y - ob->y,
376 if (!ev || ! area->scrollbar) break;
377 if (ev->xmotion.x != x_old ||
378 ev->xmotion.y != y_old ||
379 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
381 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
382 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
383 ev->xmotion.y - ob->y,
393 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
397 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
398 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
399 // XForms < 0.89.5 does not have compose support
400 // so we are using our own compose support
401 LyXLookupString(ev, dummy, 1, &keysym);
403 XLookupString(xke, dummy, 1, &keysym, 0);
405 if (lyxerr.debugging(Debug::KEY)) {
406 char const * tmp = XKeysymToString(key);
407 char const * tmp2 = XKeysymToString(keysym);
408 string const stm = (tmp ? tmp : "");
409 string const stm2 = (tmp2 ? tmp2 : "");
411 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
412 << key << "]" << endl;
413 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
414 << keysym << "]" << endl;
417 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
418 if (keysym == NoSymbol) {
420 << "Empty kdb action (probably composing)"
424 KeySym ret_key = keysym;
426 // Note that we need this handling because of a bug
427 // in XForms 0.89, if this bug is resolved in the way I hope
428 // we can just use the keysym directly with out looking
429 // at key at all. (Lgb)
432 // We migth have to add more keysyms here also,
433 // we will do that as the issues arise. (Lgb)
434 if (keysym == XK_space) {
436 lyxerr[Debug::KEY] << "Using keysym [A]"
441 // It seems that this was a bit optimistic...
442 // With this hacking things seems to be better (Lgb)
443 //if (!iscntrl(key)) {
445 // lyxerr[Debug::KEY]
446 // << "Using key [B]\n"
448 // << static_cast<unsigned char>(key)
451 ret_key = (keysym ? keysym : key);
452 lyxerr[Debug::KEY] << "Using keysym [B]"
458 unsigned int const ret_state = xke->state;
460 // If you have a better way to handle "wild-output" of
461 // characters after the key has been released than the one
462 // below, please contact me. (Lgb)
463 static Time last_time_pressed;
464 static unsigned int last_key_pressed;
465 static unsigned int last_state_pressed;
466 lyxerr[Debug::KEY] << "Workarea Diff: "
467 << xke->time - last_time_pressed
469 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
470 && ret_state == last_state_pressed
471 && xke->keycode == last_key_pressed) {
473 << "Workarea: Purging X events." << endl;
474 //lyxerr << "Workarea Events: "
475 // << XEventsQueued(fl_get_display(), QueuedAlready)
477 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
478 XSync(fl_get_display(), 1);
479 // This purge make f.ex. scrolling stop immidiatly when
480 // releasing the PageDown button. The question is if
481 // this purging of XEvents can cause any harm...
482 // after some testing I can see no problems, but
483 // I'd like other reports too.
486 last_time_pressed = xke->time;
487 last_key_pressed = xke->keycode;
488 last_state_pressed = ret_state;
490 area->workAreaKeyPress(ret_key, ret_state);
494 #if FL_REVISION >= 89
496 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
501 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
502 area->workAreaFocus();
505 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
506 area->workAreaUnfocus();
509 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
510 area->workAreaEnter();
513 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
514 area->workAreaLeave();
518 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
519 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
520 ev->xbutton.y - ob->y,
525 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
526 area->workAreaTripleClick(ev->xbutton.x - ob->x,
527 ev->xbutton.y - ob->y,
532 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
542 string clipboard_selection;
543 bool clipboard_read = false;
548 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
549 void const * data, long size)
551 clipboard_selection.erase();
554 clipboard_selection.reserve(size);
555 for (int i = 0; i < size; ++i)
556 clipboard_selection +=
557 static_cast<char const *>(data)[i];
558 clipboard_read = true;
566 string const WorkArea::getClipboard() const
568 clipboard_read = false;
570 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
575 while (!clipboard_read) {
576 if (fl_check_forms() == FL_EVENT) {
578 lyxerr << "Received unhandled X11 event" << endl;
579 lyxerr << "Type: 0x" << std::hex << ev.xany.type <<
580 "Target: 0x" << std::hex << ev.xany.window << endl;
583 return clipboard_selection;
587 void WorkArea::putClipboard(string const & s) const
592 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);