1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
15 #pragma implementation
20 #include "support/lstrings.h"
21 #include "BufferView.h"
25 FL_OBJECT * figinset_canvas;
27 // need to make the c++ compiler find the correct version of abs.
28 // This is at least true for g++.
34 XSync(fl_get_display(), 0);
38 // Just a bunch of C wrappers around static members of WorkArea
39 void C_WorkArea_scroll_cb(FL_OBJECT * ob, long buf)
41 WorkArea::scroll_cb(ob, buf);
44 int C_WorkArea_work_area_handler(FL_OBJECT * ob, int event,
48 return WorkArea::work_area_handler(ob, event,
55 WorkArea::WorkArea(BufferView * o, int xpos, int ypos, int width, int height)
56 : owner_(o), workareapixmap(0), painter_(*this)
58 fl_freeze_all_forms();
62 if (lyxerr.debugging())
63 lyxerr << "Creating work area: +"
64 << xpos << '+' << ypos << ' '
65 << width << 'x' << height << endl;
68 const int bw = int(std::abs(float(fl_get_border_width())));
70 // We really want to get rid of figinset_canvas.
71 ::figinset_canvas = figinset_canvas = obj =
72 fl_add_canvas(FL_NORMAL_CANVAS,
73 xpos + 1, ypos + 1, 1, 1, "");
74 fl_set_object_boxtype(obj, FL_NO_BOX);
75 fl_set_object_resize(obj, FL_RESIZE_ALL);
76 fl_set_object_gravity(obj, NorthWestGravity, NorthWestGravity);
79 if (lyxerr.debugging())
80 lyxerr << "\tbackground box: +"
81 << xpos << '+' << ypos << ' '
82 << width - 15 << 'x' << height << endl;
83 backgroundbox = obj = fl_add_box(FL_BORDER_BOX,
87 fl_set_object_resize(obj, FL_RESIZE_ALL);
88 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
94 scrollbar = obj = fl_add_scrollbar(FL_VERT_SCROLLBAR,
96 ypos, 17, height, "");
97 fl_set_object_boxtype(obj, FL_UP_BOX);
98 fl_set_object_resize(obj, FL_RESIZE_ALL);
99 fl_set_object_gravity(obj, NorthEastGravity, SouthEastGravity);
101 fl_set_object_callback(obj, C_WorkArea_scroll_cb, 0);
106 // Create the workarea pixmap
107 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
109 // We add this object as late as possible to avoit problems
111 if (lyxerr.debugging())
112 lyxerr << "\tfree object: +"
113 << xpos + bw << '+' << ypos + bw << ' '
114 << width - 15 - 2 * bw << 'x'
115 << height - 2 * bw << endl;
117 work_area = obj = fl_add_free(FL_INPUT_FREE,
118 xpos + bw, ypos + bw,
119 width - 15 - 2 * bw, // scrollbarwidth
121 C_WorkArea_work_area_handler);
122 obj->wantkey = FL_KEY_TAB;
123 obj->u_vdata = this; /* This is how we pass the WorkArea
124 to the work_area_handler. */
125 fl_set_object_boxtype(obj,FL_DOWN_BOX);
126 fl_set_object_resize(obj, FL_RESIZE_ALL);
127 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
129 fl_unfreeze_all_forms();
133 WorkArea::~WorkArea()
136 XFreePixmap(fl_display, workareapixmap);
140 bool WorkArea::belowMouse() const
144 fl_get_mouse(&x, &y, &button);
145 FL_Coord ulx = work_area->form->x + work_area->x;
146 FL_Coord uly = work_area->form->y + work_area->y;
147 FL_Coord w = work_area->w;
148 FL_Coord h = work_area->h;
149 if (x > ulx && y > uly && x < ulx + h && y < uly + w)
154 //lyxerr << "Mouse: (" << x << ", " << y <<") button = " << button << endl;
155 //lyxerr << "Workarea: (" << work_area->x + work_area->form->x << ", " << work_area->y + work_area->form->y << ", " << work_area->w << ", " << work_area->h << ")" << endl;
156 //lyxerr << "Below mouse: " << work_area->belowmouse << endl;
157 //return work_area->belowmouse;
161 void WorkArea::resize(int xpos, int ypos, int width, int height)
163 fl_freeze_all_forms();
165 const int bw = int(std::abs(float(fl_get_border_width())));
168 fl_set_object_geometry(backgroundbox, xpos, ypos, width - 15, height);
173 fl_set_object_geometry(scrollbar, xpos + width - 15,
176 // Create the workarea pixmap
177 createPixmap(width - 15 - 2 * bw, height - 2 * bw);
180 fl_set_object_geometry(work_area, xpos + bw, ypos + bw,
184 fl_unfreeze_all_forms();
189 void WorkArea::createPixmap(int width, int height)
191 static int cur_width = -1;
192 static int cur_height = -1;
194 if (cur_width == width && cur_height == height && workareapixmap)
201 XFreePixmap(fl_display, workareapixmap);
203 if (lyxerr.debugging())
204 lyxerr << "Creating pixmap ("
205 << width << 'x' << height << ")" << endl;
207 workareapixmap = XCreatePixmap(fl_display,
208 RootWindow(fl_display, 0),
211 fl_get_visual_depth());
212 if (lyxerr.debugging())
213 lyxerr << "\tpixmap=" << workareapixmap << endl;
217 void WorkArea::greyOut() const
219 fl_winset(FL_ObjWin(work_area));
220 fl_rectangle(1, work_area->x, work_area->y,
221 work_area->w, work_area->h, FL_GRAY63);
225 void WorkArea::setFocus() const
227 fl_set_focus_object(work_area->form, work_area);
231 void WorkArea::setScrollbar(double pos, double length_fraction) const
233 fl_set_scrollbar_value(scrollbar, pos);
234 fl_set_scrollbar_size(scrollbar, scrollbar->h * length_fraction);
238 void WorkArea::setScrollbarBounds(double l1, double l2) const
240 fl_set_scrollbar_bounds(scrollbar, l1, l2);
244 void WorkArea::setScrollbarIncrements(double inc) const
246 fl_set_scrollbar_increment(scrollbar, work_area->h - inc, inc);
250 // Callback for scrollbar slider
251 void WorkArea::scroll_cb(FL_OBJECT * ob, long)
253 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
254 // If we really want the accellerating scroll we can do that
255 // from here. IMHO that is a waste of effort since we already
256 // have other ways to move fast around in the document. (Lgb)
257 area->owner_->scrollCB(fl_get_scrollbar_value(ob));
261 bool Lgb_bug_find_hack = false;
263 int WorkArea::work_area_handler(FL_OBJECT * ob, int event,
265 int /*key*/, void * xev)
267 static int x_old = -1;
268 static int y_old = -1;
269 static long scrollbar_value_old = -1;
271 XEvent * ev = static_cast<XEvent*>(xev);
272 WorkArea * area = static_cast<WorkArea*>(ob->u_vdata);
278 if (!area->work_area ||
279 !area->work_area->form->visible)
281 lyxerr.debug() << "Workarea event: DRAW" << endl;
282 area->createPixmap(area->workWidth(), area->height());
283 Lgb_bug_find_hack = true;
285 area->owner_->workAreaExpose();
286 Lgb_bug_find_hack = false;
290 // Should really have used xbutton.state
291 lyxerr.debug() << "Workarea event: PUSH" << endl;
292 //workAreaButtonPress(ev->xbutton.x - ob->x,
293 // ev->xbutton.y - ob->y,
294 // ev->xbutton.button);
296 area->owner_->workAreaButtonPress(ev->xbutton.x - ob->x,
297 ev->xbutton.y - ob->y,
302 // Should really have used xbutton.state
303 lyxerr.debug() << "Workarea event: RELEASE" << endl;
304 //workAreaButtonRelease(ev->xbutton.x - ob->x,
305 // ev->xbutton.y - ob->y,
306 // ev->xbutton.button);
307 area->owner_->workAreaButtonRelease(ev->xbutton.x - ob->x,
308 ev->xbutton.y - ob->y,
312 if (!ev || ! area->scrollbar) break;
313 if (ev->xmotion.x != x_old ||
314 ev->xmotion.y != y_old ||
315 fl_get_scrollbar_value(area->scrollbar) != scrollbar_value_old
317 lyxerr.debug() << "Workarea event: MOUSE" << endl;
318 //workAreaMotionNotify(ev->xmotion.x - ob->x,
319 // ev->xmotion.y - ob->y,
320 // ev->xbutton.state);
322 area->owner_->workAreaMotionNotify(ev->xmotion.x - ob->x,
323 ev->xmotion.y - ob->y,
327 // Done by the raw callback:
328 // case FL_KEYBOARD: WorkAreaKeyPress(ob, 0,0,0,ev,0); break;
330 lyxerr.debug() << "Workarea event: FOCUS" << endl;
334 lyxerr.debug() << "Workarea event: UNFOCUS" << endl;
338 lyxerr.debug() << "Workarea event: ENTER" << endl;
340 area->owner_->enterView();
343 lyxerr.debug() << "Workarea event: LEAVE" << endl;
345 area->owner_->leaveView();
349 lyxerr.debug() << "Workarea event: DBLCLICK" << endl;
350 //workAreaDoubleClick(ev->xbutton.x - ob->x,
351 // ev->xbutton.y - ob->y,
352 // ev->xbutton.button);
353 area->owner_->doubleClick(ev->xbutton.x - ob->x,
354 ev->xbutton.y - ob->y,
359 lyxerr.debug() << "Workarea event: TRPLCLICK" << endl;
360 //workAreaTripleClick(ev->xbutton.x - ob->x,
361 // ev->xbutton.y - ob->y,
362 // ev->xbutton.button);
363 area->owner_->tripleClick(ev->xbutton.x - ob->x,
364 ev->xbutton.y - ob->y,
369 lyxerr.debug() << "Workarea event: OTHER" << endl;
378 static string clipboard_selection;
379 static bool clipboard_read = false;
383 int request_clipboard_cb(FL_OBJECT * /*ob*/, long /*type*/,
384 void const * data, long size)
386 clipboard_selection.erase();
387 if (size == 0) return 0; // no selection
389 clipboard_selection.reserve(size);
390 for (int i = 0; i < size; ++i) {
391 clipboard_selection += static_cast<char const *>(data)[i];
393 clipboard_read = true;
398 string WorkArea::getClipboard() const
400 clipboard_read = false;
402 if (fl_request_clipboard(work_area, 0, request_clipboard_cb) == -1)
407 while (!clipboard_read) {
408 if (fl_check_forms() == FL_EVENT) {
409 lyxerr << "LyX: This shouldn't happen..." << endl;
413 return clipboard_selection;
417 void WorkArea::putClipboard(string const & s) const
422 fl_stuff_clipboard(work_area, 0, hold.c_str(), hold.size(), 0);