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 * ====================================================== */
14 #pragma implementation
20 #include "lyxrc.h" // lyxrc.show_banner
21 #include "version.h" // lyx_version
23 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
24 #include "lyxlookup.h"
27 #include "support/filetools.h" // LibFileSearch
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
36 FL_OBJECT * figinset_canvas;
38 // needed to make the c++ compiler find the correct version of abs.
39 // This is at least true for g++.
47 XSync(fl_get_display(), 0);
54 // Just a bunch of C wrappers around static members of WorkArea
56 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
58 WorkArea::scroll_cb(ob, buf);
63 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
67 return WorkArea::work_area_handler(ob, event,
73 WorkArea::WorkArea(int xpos, int ypos, int width, int height)
74 : splash_(0), splash_text_(0), workareapixmap(0), painter_(*this)
76 fl_freeze_all_forms();
80 if (lyxerr.debugging(Debug::WORKAREA))
81 lyxerr << "Creating work area: +"
82 << xpos << '+' << ypos << ' '
83 << width << 'x' << height << endl;
86 int const bw = int(std::abs(float(fl_get_border_width())));
88 // We really want to get rid of figinset_canvas.
89 ::figinset_canvas = figinset_canvas = obj =
90 fl_add_canvas(FL_NORMAL_CANVAS,
91 xpos + 1, ypos + 1, 1, 1, "");
92 fl_set_object_boxtype(obj, FL_NO_BOX);
93 fl_set_object_resize(obj, FL_RESIZE_ALL);
94 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
97 if (lyxerr.debugging(Debug::WORKAREA))
98 lyxerr << "\tbackground box: +"
99 << xpos << '+' << ypos << ' '
100 << width - 15 << 'x' << height << endl;
101 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
105 fl_set_object_resize(obj, FL_RESIZE_ALL);
106 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
108 // Add a splash screen to the centre of the work area
109 string const splash_file = (lyxrc.show_banner) ?
110 LibFileSearch("images", "banner", "xpm") : string();
112 if (!splash_file.empty()) {
113 int const splash_w = 425;
114 int const splash_h = 290;
115 int const splash_x = xpos + (width - 15 - splash_w) / 2;
116 int const splash_y = ypos + (height - splash_h) / 2;
118 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
120 splash_w, splash_h, "");
121 fl_set_pixmapbutton_file(obj, splash_file.c_str());
122 fl_set_pixmapbutton_focus_outline(obj, 3);
123 fl_set_object_boxtype(obj, FL_NO_BOX);
125 int const text_x = splash_x + 260;
126 int const text_y = splash_y + 255;
128 fl_add_text(FL_NORMAL_TEXT, text_x, text_y, 160, 16,
130 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
131 fl_mapcolor(FL_FREE_COL2, 0x2b, 0x47, 0x82);
132 fl_mapcolor(FL_FREE_COL3, 0xe1, 0xd2, 0x9b);
133 fl_set_object_color(obj, FL_FREE_COL2, FL_FREE_COL2);
134 fl_set_object_lcol(obj, FL_FREE_COL3);
135 fl_set_object_lalign(obj, FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
136 fl_set_object_lstyle(obj, FL_BOLD_STYLE);
143 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
145 ypos, 17, height, "");
146 fl_set_object_boxtype(obj, FL_UP_BOX);
147 fl_set_object_resize(obj, FL_RESIZE_ALL);
148 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
150 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
155 // Create the workarea pixmap
156 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
158 // We add this object as late as possible to avoit problems
160 if (lyxerr.debugging(Debug::WORKAREA))
161 lyxerr << "\tfree object: +"
162 << xpos + bw << '+' << ypos + bw << ' '
163 << width - 15 - 2 * bw << 'x'
164 << height - 2 * bw << endl;
166 work_area = obj = fl_add_free(FL_ALL_FREE,
167 xpos + bw, ypos + bw,
168 width - 15 - 2 * bw, // scrollbarwidth
170 C_WorkArea_work_area_handler);
171 obj->wantkey = FL_KEY_ALL;
172 obj->u_vdata = this; /* This is how we pass the WorkArea
173 to the work_area_handler. */
174 fl_set_object_boxtype(obj,FL_DOWN_BOX);
175 fl_set_object_resize(obj, FL_RESIZE_ALL);
176 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
178 fl_unfreeze_all_forms();
182 WorkArea::~WorkArea()
185 XFreePixmap(fl_get_display(), workareapixmap);
189 bool WorkArea::belowMouse() const
193 fl_get_mouse(&x, &y, &button);
194 FL_Coord ulx = work_area->form->x + work_area->x;
195 FL_Coord uly = work_area->form->y + work_area->y;
196 FL_Coord w = work_area->w;
197 FL_Coord h = work_area->h;
198 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
204 void WorkArea::resize(int xpos, int ypos, int width, int height)
206 fl_freeze_all_forms();
208 int const bw = int(std::abs(float(fl_get_border_width())));
211 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
216 fl_set_object_geometry(scrollbar, xpos + width - 15,
219 // Create the workarea pixmap
220 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
223 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
227 fl_unfreeze_all_forms();
232 void destroy_object(FL_OBJECT * obj)
240 fl_delete_object(obj);
246 void WorkArea::createPixmap(int width, int height)
248 // Three calls to createPixmap are needed to draw the initial view
249 // of LyX. Any more and the splash is destroyed.
250 static int counter = 0;
251 if (++counter == 4) {
252 destroy_object(splash_);
254 destroy_object(splash_text_);
258 static int cur_width = -1;
259 static int cur_height = -1;
261 if (cur_width == width && cur_height == height && workareapixmap)
268 XFreePixmap(fl_get_display(), workareapixmap);
270 if (lyxerr.debugging(Debug::WORKAREA))
271 lyxerr << "Creating pixmap ("
272 << width << 'x' << height << ")" << endl;
274 workareapixmap = XCreatePixmap(fl_get_display(),
275 RootWindow(fl_get_display(), 0),
278 fl_get_visual_depth());
279 if (lyxerr.debugging(Debug::WORKAREA))
280 lyxerr << "\tpixmap=" << workareapixmap << endl;
284 void WorkArea::greyOut() const
287 fl_winset(FL_ObjWin(work_area));
288 fl_rectangle(1, work_area->x, work_area->y,
289 work_area->w, work_area->h, FL_GRAY63);
294 void WorkArea::setFocus() const
296 fl_set_focus_object(work_area->form, work_area);
300 void WorkArea::setScrollbar(double pos, double length_fraction) const
302 fl_set_scrollbar_value(scrollbar, pos);
303 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
307 void WorkArea::setScrollbarBounds(double l1, double l2) const
309 fl_set_scrollbar_bounds(scrollbar, l1, l2);
313 void WorkArea::setScrollbarIncrements(double inc) const
315 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
319 // Callback for scrollbar slider
320 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
322 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
323 // If we really want the accellerating scroll we can do that
324 // from here. IMHO that is a waste of effort since we already
325 // have other ways to move fast around in the document. (Lgb)
326 area->scrollCB(fl_get_scrollbar_value(ob));
331 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
335 static int x_old = -1;
336 static int y_old = -1;
337 static long scrollbar_value_old = -1;
339 XEvent * ev = static_cast<XEvent*>(xev);
340 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
346 if (!area->work_area ||
347 !area->work_area->form->visible)
349 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
350 area->createPixmap(area->workWidth(), area->height());
351 area->workAreaExpose();
354 if (!ev || ev->xbutton.button == 0) break;
355 // Should really have used xbutton.state
356 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
357 area->workAreaButtonPress(ev->xbutton.x - ob->x,
358 ev->xbutton.y - ob->y,
360 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
363 if (!ev || ev->xbutton.button == 0) break;
364 // Should really have used xbutton.state
365 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
366 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
367 ev->xbutton.y - ob->y,
375 if (!ev || ! area->scrollbar) break;
376 if (ev->xmotion.x != x_old ||
377 ev->xmotion.y != y_old ||
378 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
380 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
381 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
382 ev->xmotion.y - ob->y,
392 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
396 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
397 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
398 // XForms < 0.89.5 does not have compose support
399 // so we are using our own compose support
400 LyXLookupString(ev, dummy, 1, &keysym);
402 XLookupString(xke, dummy, 1, &keysym, 0);
404 if (lyxerr.debugging(Debug::KEY)) {
405 char const * tmp = XKeysymToString(key);
406 char const * tmp2 = XKeysymToString(keysym);
407 string const stm = (tmp ? tmp : "");
408 string const stm2 = (tmp2 ? tmp2 : "");
410 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
411 << key << "]" << endl;
412 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
413 << keysym << "]" << endl;
416 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
417 if (keysym == NoSymbol) {
419 << "Empty kdb action (probably composing)"
423 KeySym ret_key = keysym;
425 // Note that we need this handling because of a bug
426 // in XForms 0.89, if this bug is resolved in the way I hope
427 // we can just use the keysym directly with out looking
428 // at key at all. (Lgb)
431 // We migth have to add more keysyms here also,
432 // we will do that as the issues arise. (Lgb)
433 if (keysym == XK_space) {
435 lyxerr[Debug::KEY] << "Using keysym [A]"
440 // It seems that this was a bit optimistic...
441 // With this hacking things seems to be better (Lgb)
442 //if (!iscntrl(key)) {
444 // lyxerr[Debug::KEY]
445 // << "Using key [B]\n"
447 // << static_cast<unsigned char>(key)
450 ret_key = (keysym ? keysym : key);
451 lyxerr[Debug::KEY] << "Using keysym [B]"
457 unsigned int const ret_state = xke->state;
459 // If you have a better way to handle "wild-output" of
460 // characters after the key has been released than the one
461 // below, please contact me. (Lgb)
462 static Time last_time_pressed;
463 static unsigned int last_key_pressed;
464 static unsigned int last_state_pressed;
465 lyxerr[Debug::KEY] << "Workarea Diff: "
466 << xke->time - last_time_pressed
468 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
469 && ret_state == last_state_pressed
470 && xke->keycode == last_key_pressed) {
472 << "Workarea: Purging X events." << endl;
473 //lyxerr << "Workarea Events: "
474 // << XEventsQueued(fl_get_display(), QueuedAlready)
476 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
477 XSync(fl_get_display(), 1);
478 // This purge make f.ex. scrolling stop immidiatly when
479 // releasing the PageDown button. The question is if
480 // this purging of XEvents can cause any harm...
481 // after some testing I can see no problems, but
482 // I'd like other reports too.
485 last_time_pressed = xke->time;
486 last_key_pressed = xke->keycode;
487 last_state_pressed = ret_state;
489 area->workAreaKeyPress(ret_key, ret_state);
493 #if FL_REVISION >= 89
495 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
500 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
501 area->workAreaFocus();
504 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
505 area->workAreaUnfocus();
508 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
509 area->workAreaEnter();
512 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
513 area->workAreaLeave();
517 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
518 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
519 ev->xbutton.y - ob->y,
524 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
525 area->workAreaTripleClick(ev->xbutton.x - ob->x,
526 ev->xbutton.y - ob->y,
531 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
541 string clipboard_selection;
542 bool clipboard_read = false;
547 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
548 void const * data, long size)
550 clipboard_selection.erase();
553 clipboard_selection.reserve(size);
554 for (int i = 0; i < size; ++i)
555 clipboard_selection +=
556 static_cast<char const *>(data)[i];
557 clipboard_read = true;
565 string const WorkArea::getClipboard() const
567 clipboard_read = false;
569 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
574 while (!clipboard_read) {
575 if (fl_check_forms() == FL_EVENT) {
577 lyxerr << "Received unhandled X11 event" << endl;
578 lyxerr << "Type: 0x" << std::hex << ev.xany.type <<
579 "Target: 0x" << std::hex << ev.xany.window << endl;
582 return clipboard_selection;
586 void WorkArea::putClipboard(string const & s) const
591 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);