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::GUI))
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::GUI))
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::GUI))
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)
200 //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
201 //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
202 //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
203 //return work_area->belowmouse;
207 void WorkArea::resize(int xpos, int ypos, int width, int height)
209 fl_freeze_all_forms();
211 int const bw = int(std::abs(float(fl_get_border_width())));
214 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
219 fl_set_object_geometry(scrollbar, xpos + width - 15,
222 // Create the workarea pixmap
223 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
226 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
230 fl_unfreeze_all_forms();
235 void destroy_object(FL_OBJECT * obj)
243 fl_delete_object(obj);
249 void WorkArea::createPixmap(int width, int height)
251 // Three calls to createPixmap are needed to draw the initial view
252 // of LyX. Any more and the splash is destroyed.
253 static int counter = 0;
254 if (++counter == 4) {
255 destroy_object(splash_);
257 destroy_object(splash_text_);
261 static int cur_width = -1;
262 static int cur_height = -1;
264 if (cur_width == width && cur_height == height && workareapixmap)
271 XFreePixmap(fl_get_display(), workareapixmap);
273 if (lyxerr.debugging(Debug::GUI))
274 lyxerr << "Creating pixmap ("
275 << width << 'x' << height << ")" << endl;
277 workareapixmap = XCreatePixmap(fl_get_display(),
278 RootWindow(fl_get_display(), 0),
281 fl_get_visual_depth());
282 if (lyxerr.debugging(Debug::GUI))
283 lyxerr << "\tpixmap=" << workareapixmap << endl;
287 void WorkArea::greyOut() const
290 fl_winset(FL_ObjWin(work_area));
291 fl_rectangle(1, work_area->x, work_area->y,
292 work_area->w, work_area->h, FL_GRAY63);
297 void WorkArea::setFocus() const
299 fl_set_focus_object(work_area->form, work_area);
303 void WorkArea::setScrollbar(double pos, double length_fraction) const
305 fl_set_scrollbar_value(scrollbar, pos);
306 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
310 void WorkArea::setScrollbarBounds(double l1, double l2) const
312 fl_set_scrollbar_bounds(scrollbar, l1, l2);
316 void WorkArea::setScrollbarIncrements(double inc) const
318 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
322 // Callback for scrollbar slider
323 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
325 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
326 // If we really want the accellerating scroll we can do that
327 // from here. IMHO that is a waste of effort since we already
328 // have other ways to move fast around in the document. (Lgb)
329 area->scrollCB(fl_get_scrollbar_value(ob));
334 bool Lgb_bug_find_hack = false;
336 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
340 static int x_old = -1;
341 static int y_old = -1;
342 static long scrollbar_value_old = -1;
344 XEvent * ev = static_cast<XEvent*>(xev);
345 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
351 if (!area->work_area ||
352 !area->work_area->form->visible)
354 lyxerr[Debug::GUI] << "Workarea event: DRAW" << endl;
355 area->createPixmap(area->workWidth(), area->height());
356 Lgb_bug_find_hack = true;
357 area->workAreaExpose();
358 Lgb_bug_find_hack = false;
361 if (!ev || ev->xbutton.button == 0) break;
362 // Should really have used xbutton.state
363 lyxerr[Debug::GUI] << "Workarea event: PUSH" << endl;
364 area->workAreaButtonPress(ev->xbutton.x - ob->x,
365 ev->xbutton.y - ob->y,
367 //area->workAreaKeyPress(XK_Pointer_Button1, ev->xbutton.state);
370 if (!ev || ev->xbutton.button == 0) break;
371 // Should really have used xbutton.state
372 lyxerr[Debug::GUI] << "Workarea event: RELEASE" << endl;
373 area->workAreaButtonRelease(ev->xbutton.x - ob->x,
374 ev->xbutton.y - ob->y,
382 if (!ev || ! area->scrollbar) break;
383 if (ev->xmotion.x != x_old ||
384 ev->xmotion.y != y_old ||
385 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
387 lyxerr[Debug::GUI] << "Workarea event: MOUSE" << endl;
388 area->workAreaMotionNotify(ev->xmotion.x - ob->x,
389 ev->xmotion.y - ob->y,
399 lyxerr[Debug::KEY] << "Workarea event: KEYBOARD" << endl;
403 XKeyEvent * xke = reinterpret_cast<XKeyEvent *>(ev);
404 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
405 // XForms < 0.89.5 does not have compose support
406 // so we are using our own compose support
407 LyXLookupString(ev, dummy, 1, &keysym);
409 XLookupString(xke, dummy, 1, &keysym, 0);
411 if (lyxerr.debugging(Debug::KEY)) {
412 char const * tmp = XKeysymToString(key);
413 char const * tmp2 = XKeysymToString(keysym);
414 string const stm = (tmp ? tmp : "");
415 string const stm2 = (tmp2 ? tmp2 : "");
417 lyxerr << "WorkArea: Key is `" << stm << "' ["
418 << key << "]" << endl;
419 lyxerr << "WorkArea: Keysym is `" << stm2 << "' ["
420 << keysym << "]" << endl;
423 #if FL_REVISION < 89 || (FL_REVISION == 89 && FL_FIXLEVEL < 5)
424 if (keysym == NoSymbol) {
426 << "Empty kdb action (probably composing)"
430 KeySym ret_key = keysym;
432 // Note that we need this handling because of a bug
433 // in XForms 0.89, if this bug is resolved in the way I hope
434 // we can just use the keysym directly with out looking
435 // at key at all. (Lgb)
438 // We migth have to add more keysyms here also,
439 // we will do that as the issues arise. (Lgb)
440 if (keysym == XK_space) {
442 lyxerr[Debug::KEY] << "Using keysym [A]"
447 // It seems that this was a bit optimistic...
448 // With this hacking things seems to be better (Lgb)
449 //if (!iscntrl(key)) {
451 // lyxerr[Debug::KEY]
452 // << "Using key [B]\n"
454 // << static_cast<unsigned char>(key)
457 ret_key = (keysym ? keysym : key);
458 lyxerr[Debug::KEY] << "Using keysym [B]"
464 unsigned int const ret_state = xke->state;
466 // If you have a better way to handle "wild-output" of
467 // characters after the key has been released than the one
468 // below, please contact me. (Lgb)
469 static Time last_time_pressed;
470 static unsigned int last_key_pressed;
471 static unsigned int last_state_pressed;
472 lyxerr[Debug::KEY] << "Workarea Diff: "
473 << xke->time - last_time_pressed
475 if (xke->time - last_time_pressed < 25 // should perhaps be tunable
476 && ret_state == last_state_pressed
477 && xke->keycode == last_key_pressed) {
479 << "Workarea: Purging X events." << endl;
480 //lyxerr << "Workarea Events: "
481 // << XEventsQueued(fl_get_display(), QueuedAlready)
483 if (XEventsQueued(fl_get_display(), QueuedAlready) > 0)
484 XSync(fl_get_display(), 1);
485 // This purge make f.ex. scrolling stop immidiatly when
486 // releasing the PageDown button. The question is if
487 // this purging of XEvents can cause any harm...
488 // after some testing I can see no problems, but
489 // I'd like other reports too.
492 last_time_pressed = xke->time;
493 last_key_pressed = xke->keycode;
494 last_state_pressed = ret_state;
496 area->workAreaKeyPress(ret_key, ret_state);
500 #if FL_REVISION >= 89
502 lyxerr << "Workarea event: KEYRELEASE" << endl;
507 lyxerr[Debug::GUI] << "Workarea event: FOCUS" << endl;
508 area->workAreaFocus();
511 lyxerr[Debug::GUI] << "Workarea event: UNFOCUS" << endl;
512 area->workAreaUnfocus();
515 lyxerr[Debug::GUI] << "Workarea event: ENTER" << endl;
516 area->workAreaEnter();
519 lyxerr[Debug::GUI] << "Workarea event: LEAVE" << endl;
520 area->workAreaLeave();
524 lyxerr[Debug::GUI] << "Workarea event: DBLCLICK" << endl;
525 area->workAreaDoubleClick(ev->xbutton.x - ob->x,
526 ev->xbutton.y - ob->y,
531 lyxerr[Debug::GUI] << "Workarea event: TRPLCLICK" << endl;
532 area->workAreaTripleClick(ev->xbutton.x - ob->x,
533 ev->xbutton.y - ob->y,
538 lyxerr[Debug::GUI] << "Workarea event: OTHER" << endl;
549 string clipboard_selection;
550 bool clipboard_read = false;
555 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
556 void const * data, long size)
558 clipboard_selection.erase();
561 clipboard_selection.reserve(size);
562 for (int i = 0; i < size; ++i)
563 clipboard_selection +=
564 static_cast<char const *>(data)[i];
565 clipboard_read = true;
573 string const WorkArea::getClipboard() const
575 clipboard_read = false;
577 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
582 while (!clipboard_read) {
583 if (fl_check_forms() == FL_EVENT) {
584 lyxerr << "LyX: This shouldn't happen..." << endl;
588 return clipboard_selection;
592 void WorkArea::putClipboard(string const & s) const
597 fl_stuff_clipboard(work_area, 0, hold.data(), hold.size(), 0);