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"
37 FL_OBJECT * figinset_canvas;
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(abs(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(abs(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 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
332 static int x_old = -1;
333 static int y_old = -1;
334 static long scrollbar_value_old = -1;
336 XEvent * ev = static_cast<XEvent*>(xev);
337 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
343 if (!area->work_area ||
344 !area->work_area->form->visible)
346 lyxerr[Debug::WORKAREA] << "Workarea event: DRAW" << endl;
347 area->createPixmap(area->workWidth(), area->height());
348 area->workAreaExpose();
351 if (!ev || ev->xbutton.button == 0) break;
352 // Should really have used xbutton.state
353 lyxerr[Debug::WORKAREA] << "Workarea event: PUSH" << endl;
354 area->workAreaButtonPress(ev->xbutton.x - ob->x,
355 ev->xbutton.y - ob->y,
357 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
360 if (!ev || ev->xbutton.button == 0) break;
361 // Should really have used xbutton.state
362 lyxerr[Debug::WORKAREA] << "Workarea event: RELEASE" << endl;
363 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
364 ev->xbutton.y - ob->y,
372 if (!ev || ! area->scrollbar) break;
373 if (ev->xmotion.x != x_old ||
374 ev->xmotion.y != y_old ||
375 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
377 lyxerr[Debug::WORKAREA] << "Workarea event: MOUSE" << endl;
378 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
379 ev->xmotion.y - ob->y,
389 lyxerr[Debug::WORKAREA] << "Workarea event: KEYBOARD" << endl;
393 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
394 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
395 // XForms < 0.89.5 does not have compose support
396 // so we are using our own compose support
397 LyXLookupString(ev, dummy, 1, &keysym);
399 XLookupString(xke, dummy, 1, &keysym, 0);
401 if (lyxerr.debugging(Debug::KEY)) {
402 char const * tmp = XKeysymToString(key);
403 char const * tmp2 = XKeysymToString(keysym);
404 string const stm = (tmp ? tmp : "");
405 string const stm2 = (tmp2 ? tmp2 : "");
407 lyxerr[Debug::KEY] << "WorkArea: Key is `" << stm << "' ["
408 << key << "]" << endl;
409 lyxerr[Debug::KEY] << "WorkArea: Keysym is `" << stm2 << "' ["
410 << keysym << "]" << endl;
413 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
414 if (keysym == NoSymbol) {
416 << "Empty kdb action (probably composing)"
420 KeySym ret_key = keysym;
422 // Note that we need this handling because of a bug
423 // in XForms 0.89, if this bug is resolved in the way I hope
424 // we can just use the keysym directly with out looking
425 // at key at all. (Lgb)
428 // We migth have to add more keysyms here also,
429 // we will do that as the issues arise. (Lgb)
430 if (keysym == XK_space) {
432 lyxerr[Debug::KEY] << "Using keysym [A]"
437 // It seems that this was a bit optimistic...
438 // With this hacking things seems to be better (Lgb)
439 //if (!iscntrl(key)) {
441 // lyxerr[Debug::KEY]
442 // << "Using key [B]\n"
444 // << static_cast<unsigned char>(key)
447 ret_key = (keysym ? keysym : key);
448 lyxerr[Debug::KEY] << "Using keysym [B]"
454 unsigned int const ret_state = xke->state;
456 // If you have a better way to handle "wild-output" of
457 // characters after the key has been released than the one
458 // below, please contact me. (Lgb)
459 static Time last_time_pressed;
460 static unsigned int last_key_pressed;
461 static unsigned int last_state_pressed;
462 lyxerr[Debug::KEY] << "Workarea Diff: "
463 << xke->time - last_time_pressed
465 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
466 && ret_state == last_state_pressed
467 && xke->keycode == last_key_pressed) {
469 << "Workarea: Purging X events." << endl;
470 //lyxerr << "Workarea Events: "
471 // << XEventsQueued(fl_get_display(), QueuedAlready)
473 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
474 XSync(fl_get_display(), 1);
475 // This purge make f.ex. scrolling stop immidiatly when
476 // releasing the PageDown button. The question is if
477 // this purging of XEvents can cause any harm...
478 // after some testing I can see no problems, but
479 // I'd like other reports too.
482 last_time_pressed = xke->time;
483 last_key_pressed = xke->keycode;
484 last_state_pressed = ret_state;
486 area->workAreaKeyPress(ret_key, ret_state);
490 #if FL_REVISION >= 89
492 lyxerr[Debug::WORKAREA] << "Workarea event: KEYRELEASE" << endl;
497 lyxerr[Debug::WORKAREA] << "Workarea event: FOCUS" << endl;
498 area->workAreaFocus();
501 lyxerr[Debug::WORKAREA] << "Workarea event: UNFOCUS" << endl;
502 area->workAreaUnfocus();
505 lyxerr[Debug::WORKAREA] << "Workarea event: ENTER" << endl;
506 area->workAreaEnter();
509 lyxerr[Debug::WORKAREA] << "Workarea event: LEAVE" << endl;
510 area->workAreaLeave();
514 lyxerr[Debug::WORKAREA] << "Workarea event: DBLCLICK" << endl;
515 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
516 ev->xbutton.y - ob->y,
521 lyxerr[Debug::WORKAREA] << "Workarea event: TRPLCLICK" << endl;
522 area->workAreaTripleClick(ev->xbutton.x - ob->x,
523 ev->xbutton.y - ob->y,
528 lyxerr[Debug::WORKAREA] << "Workarea event: OTHER" << endl;
538 string clipboard_selection;
539 bool clipboard_read = false;
544 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
545 void const * data, long size)
547 clipboard_selection.erase();
550 clipboard_selection.reserve(size);
551 for (int i = 0; i < size; ++i)
552 clipboard_selection +=
553 static_cast<char const *>(data)[i];
554 clipboard_read = true;
562 string const WorkArea::getClipboard() const
564 clipboard_read = false;
566 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
571 while (!clipboard_read) {
572 if (fl_check_forms() == FL_EVENT) {
574 lyxerr << "Received unhandled X11 event" << endl;
575 lyxerr << "Type: 0x" << std::hex << ev.xany.type <<
576 "Target: 0x" << std::hex << ev.xany.window << endl;
579 return clipboard_selection;
583 void WorkArea::putClipboard(string const & s) const
588 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);