3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCXX_CONCEPT_CHECKS
15 #undef _GLIBCXX_CONCEPT_CHECKS
17 #ifdef _GLIBCPP_CONCEPT_CHECKS
18 #undef _GLIBCPP_CONCEPT_CHECKS
21 #include "GWorkArea.h"
24 #include "GLyXKeySym.h"
26 #include "BufferView.h"
28 #include "funcrequest.h"
32 using boost::shared_ptr;
41 mouse_button::state gButtonToLyx(guint gdkbutton)
43 // GDK uses int 1,2,3 but lyx uses enums (1,2,4)
46 return mouse_button::button1;
48 return mouse_button::button2;
50 return mouse_button::button3;
52 return mouse_button::button4;
54 return mouse_button::button5;
57 // This shouldn't happen, according to gdk docs
58 lyxerr << "gButtonToLyx: unhandled button index\n";
59 return mouse_button::button1;
64 ColorCache colorCache;
66 Gdk::Color * ColorCache::getColor(LColor_color clr)
68 MapIt it = cache_.find(clr);
69 return it == cache_.end() ? 0 : it->second.get();
73 XftColor * ColorCache::getXftColor(LColor_color clr)
75 MapIt2 it = cache2_.find(clr);
76 return it == cache2_.end() ? 0 : it->second.get();
80 void ColorCache::cacheColor(LColor_color clr, Gdk::Color * gclr)
82 cache_[clr] = shared_ptr<Gdk::Color>(gclr);
86 void ColorCache::cacheXftColor(LColor_color clr, XftColor * xclr)
88 cache2_[clr] = shared_ptr<XftColor>(xclr);
92 void ColorCache::clear()
99 XftColor * ColorHandler::getXftColor(LColor_color clr)
101 XftColor * xclr = colorCache.getXftColor(clr);
104 Colormap colormap = GDK_COLORMAP_XCOLORMAP(
105 owner_.getColormap()->gobj());
106 Visual * visual = GDK_VISUAL_XVISUAL(
107 owner_.getColormap()->get_visual()->gobj());
108 XftColorAllocName(owner_.getDisplay(), visual, colormap,
110 lcolor.getX11Name(clr).c_str())
112 colorCache.cacheXftColor(clr, xclr);
118 Gdk::Color * ColorHandler::getGdkColor(LColor_color clr)
120 Gdk::Color * gclr = colorCache.getColor(clr);
122 gclr = new Gdk::Color;
123 gclr->parse(lcolor.getX11Name(clr));
124 owner_.getColormap()->alloc_color(*gclr);
125 colorCache.cacheColor(clr, gclr);
135 mouse_button::state gtkButtonState(unsigned int state)
137 mouse_button::state b = mouse_button::none;
138 if (state & GDK_BUTTON1_MASK)
139 b = mouse_button::button1;
140 else if (state & GDK_BUTTON2_MASK)
141 b = mouse_button::button2;
142 else if (state & GDK_BUTTON3_MASK)
143 b = mouse_button::button3;
144 else if (state & GDK_BUTTON3_MASK)
145 b = mouse_button::button3;
146 else if (state & GDK_BUTTON4_MASK)
147 b = mouse_button::button4;
148 else if (state & GDK_BUTTON5_MASK)
149 b = mouse_button::button5;
154 key_modifier::state gtkKeyState(guint state)
156 key_modifier::state k = key_modifier::none;
157 if (state & GDK_CONTROL_MASK)
158 k |= key_modifier::ctrl;
159 if (state & GDK_SHIFT_MASK)
160 k |= key_modifier::shift;
161 if (state & GDK_MOD1_MASK)
162 k |= key_modifier::alt;
167 void inputCommitRelay(GtkIMContext */*imcontext*/, gchar * str, GWorkArea * area)
169 area->inputCommit(str);
176 GWorkArea::GWorkArea(LyXView & owner, int width, int height)
177 : view_(owner), workAreaPixmap_(0), painter_(*this), draw_(0), colorHandler_(*this),
180 workArea_.set_size_request(width, height);
181 workArea_.set_double_buffered(false);
182 workArea_.add_events(Gdk::STRUCTURE_MASK | Gdk::EXPOSURE_MASK |
183 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
184 Gdk::KEY_PRESS_MASK | Gdk::BUTTON1_MOTION_MASK);
185 workArea_.signal_expose_event().connect(
186 sigc::mem_fun(*this, &GWorkArea::onExpose));
187 workArea_.signal_configure_event().connect(
188 sigc::mem_fun(*this, &GWorkArea::onConfigure));
189 workArea_.signal_button_press_event().connect(
190 sigc::mem_fun(*this, &GWorkArea::onButtonPress));
191 workArea_.signal_button_release_event().connect(
192 sigc::mem_fun(*this, &GWorkArea::onButtonRelease));
193 workArea_.signal_key_press_event().connect(
194 sigc::mem_fun(*this, &GWorkArea::onKeyPress));
195 workArea_.signal_motion_notify_event().connect(
196 sigc::mem_fun(*this, &GWorkArea::onMotionNotify));
198 vscrollbar_.get_adjustment()->signal_value_changed().connect(
199 sigc::mem_fun(*this, &GWorkArea::onScroll));
200 workArea_.signal_scroll_event().connect(
201 sigc::mem_fun(*this, &GWorkArea::onScrollWheel));
203 hbox_.children().push_back(Gtk::Box_Helpers::Element(workArea_));
204 hbox_.children().push_back(
205 Gtk::Box_Helpers::Element(vscrollbar_,Gtk::PACK_SHRINK));
208 GView & gview = static_cast<GView &>(owner);
209 gview.getBox(GView::Center).children().push_back(
210 Gtk::Box_Helpers::Element(hbox_));
212 workArea_.set_flags(workArea_.get_flags() | Gtk::CAN_DEFAULT |
214 workArea_.grab_default();
215 gview.setGWorkArea(&workArea_);
216 imContext_ = GTK_IM_CONTEXT(gtk_im_multicontext_new());
217 g_signal_connect(G_OBJECT(imContext_), "commit",
218 G_CALLBACK(&inputCommitRelay),
223 GWorkArea::~GWorkArea()
225 g_object_unref(imContext_);
229 Painter & GWorkArea::getPainter()
235 int GWorkArea::workWidth() const
237 return workArea_.get_width();
241 int GWorkArea::workHeight() const
243 return workArea_.get_height();
247 int GWorkArea::xpos() const
253 int GWorkArea::ypos() const
259 Glib::RefPtr<Gdk::Window> GWorkArea::getWindow()
261 return workArea_.get_window();
265 Display * GWorkArea::getDisplay() const
267 return GDK_WINDOW_XDISPLAY(
268 const_cast<GdkWindow*>(workArea_.get_window()->gobj()));
272 Glib::RefPtr<Gdk::Pixmap> GWorkArea::getPixmap()
274 return workAreaPixmap_;
278 Glib::RefPtr<Gdk::GC> GWorkArea::getGC()
284 Glib::RefPtr<Gdk::Colormap> GWorkArea::getColormap()
286 return workArea_.get_colormap();
290 XftDraw * GWorkArea::getXftDraw()
296 ColorHandler & GWorkArea::getColorHandler()
298 return colorHandler_;
302 bool GWorkArea::onExpose(GdkEventExpose * event)
304 workArea_.get_window()->draw_drawable(
305 workArea_.get_style()->get_black_gc(),
307 event->area.x, event->area.y,
308 event->area.x, event->area.y,
309 event->area.width, event->area.height);
314 bool GWorkArea::onConfigure(GdkEventConfigure * /*event*/)
316 int x, y, width, height, depth;
317 workArea_.get_window()->get_geometry(x, y, width, height, depth);
319 XftDrawDestroy(draw_);
320 workAreaPixmap_ = Gdk::Pixmap::create(workArea_.get_window(),
321 width, height, depth);
322 Pixmap pixmap = GDK_PIXMAP_XID(workAreaPixmap_->gobj());
323 Colormap colormap = GDK_COLORMAP_XCOLORMAP(
324 workArea_.get_colormap()->gobj());
325 Visual * visual = GDK_VISUAL_XVISUAL(
326 workArea_.get_colormap()->get_visual()->gobj());
327 draw_ = XftDrawCreate(getDisplay(), pixmap,
330 workAreaGC_ = Gdk::GC::create(workArea_.get_window());
331 Gdk::Cursor cursor(Gdk::XTERM);
332 workArea_.get_window()->set_cursor(cursor);
333 gtk_im_context_set_client_window(
334 imContext_, workArea_.get_window()->gobj());
336 view_.workArea()->resizeBufferView();
341 void GWorkArea::setScrollbarParams(int height, int pos, int line_height)
348 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
349 adjustment->set_lower(0);
350 int workAreaHeight = workHeight();
351 if (!height || height < workAreaHeight) {
352 adjustment->set_upper(workAreaHeight);
353 adjustment->set_page_size(workAreaHeight);
354 adjustment->set_value(0);
355 adjustment->changed();
359 adjustment->set_step_increment(line_height * 3);
360 adjustment->set_page_increment(workAreaHeight - line_height);
361 // Allow the user half a screen of blank at the end
362 // to make scrollbar consistant with centering the cursor
363 adjustment->set_upper(height + workAreaHeight / 4);
364 adjustment->set_page_size(workAreaHeight);
365 adjustment->set_value(pos);
366 adjustment->changed();
371 void GWorkArea::onScroll()
378 double val = vscrollbar_.get_adjustment()->get_value();
379 view_.workArea()->scrollBufferView(static_cast<int>(val));
384 bool GWorkArea::onScrollWheel(GdkEventScroll * event)
386 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
389 if (event->state & GDK_CONTROL_MASK)
390 step = adjustment->get_page_increment();
392 step = adjustment->get_step_increment();
394 if (event->direction == GDK_SCROLL_UP)
397 double target = adjustment->get_value() + step;
398 // Prevent the user getting a whole screen of blank when they
399 // try to scroll past the end of the doc
400 double max = adjustment->get_upper() - workHeight();
404 adjustment->set_value(target);
409 bool GWorkArea::onButtonPress(GdkEventButton * event)
411 kb_action ka = LFUN_MOUSE_PRESS;
412 switch (event->type) {
413 case GDK_BUTTON_PRESS:
414 ka = LFUN_MOUSE_PRESS;
416 case GDK_2BUTTON_PRESS:
417 ka = LFUN_MOUSE_DOUBLE;
419 case GDK_3BUTTON_PRESS:
420 ka = LFUN_MOUSE_TRIPLE;
425 view_.workArea()->dispatch(FuncRequest(ka,
426 static_cast<int>(event->x),
427 static_cast<int>(event->y),
428 gButtonToLyx(event->button)));
429 workArea_.grab_focus();
434 bool GWorkArea::onButtonRelease(GdkEventButton * event)
436 view_.workArea()->dispatch(FuncRequest(LFUN_MOUSE_RELEASE,
437 static_cast<int>(event->x),
438 static_cast<int>(event->y),
439 gButtonToLyx(event->button)));
444 bool GWorkArea::onMotionNotify(GdkEventMotion * event)
446 static guint32 timeBefore;
447 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
448 double step = adjustment->get_step_increment();
449 double value = adjustment->get_value();
452 else if (event->x > workArea_.get_height())
454 if (value != adjustment->get_value()) {
455 if (event->time - timeBefore > 200) {
456 adjustment->set_value(value);
457 adjustment->value_changed();
459 timeBefore = event->time;
461 view_.workArea()->dispatch(FuncRequest(LFUN_MOUSE_MOTION,
462 static_cast<int>(event->x),
463 static_cast<int>(event->y),
464 gtkButtonState(event->state)));
469 void GWorkArea::inputCommit(gchar * str)
471 inputCache_ = Glib::locale_from_utf8(str);
475 bool GWorkArea::onKeyPress(GdkEventKey * event)
479 // bool inputGet = gtk_im_context_filter_keypress(imContext_, event);
480 // // cope with ascii
481 // if ((inputGet && inputCache_.size() == 1 && inputCache_[0] < 128) ||
484 GLyXKeySym *glk = new GLyXKeySym(event->keyval);
485 view_.workArea()->processKeySym(LyXKeySymPtr(glk),
486 gtkKeyState(event->state));
488 // } else if (!inputCache_.empty())
489 // workAreaCJK_IMprocess(inputCache_.size(), inputCache_.data());
495 void GWorkArea::onClipboardGet(Gtk::SelectionData & /*selection_data*/,
498 lyx::docstring const sel = view_.view()->requestSelection();
500 view_.gui().selection().put(sel);
504 void GWorkArea::onClipboardClear()
510 void GWorkArea::haveSelection(bool toHave)
513 Glib::RefPtr<Gtk::Clipboard> clipboard =
514 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
515 std::vector<Gtk::TargetEntry> listTargets;
516 listTargets.push_back(Gtk::TargetEntry("UTF8_STRING"));
517 clipboard->set(listTargets,
518 sigc::mem_fun(const_cast<GWorkArea&>(*this),
519 &GWorkArea::onClipboardGet),
520 sigc::mem_fun(const_cast<GWorkArea&>(*this),
521 &GWorkArea::onClipboardClear));
525 } // namespace frontend