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 #include <X11/Xft/Xft.h>
15 #include "GWorkArea.h"
18 #include "GLyXKeySym.h"
21 #include "funcrequest.h"
27 ColorCache colorCache;
30 ColorCache::~ColorCache()
36 Gdk::Color * ColorCache::getColor(LColor_color clr)
38 MapIt it = cache_.find(clr);
39 return it == cache_.end() ? 0 : it->second;
43 XftColor * ColorCache::getXftColor(LColor_color clr)
45 MapIt2 it = cache2_.find(clr);
46 return it == cache2_.end() ? 0 : it->second;
50 void ColorCache::cacheColor(LColor_color clr, Gdk::Color * gclr)
56 void ColorCache::cacheXftColor(LColor_color clr, XftColor * xclr)
62 void ColorCache::clear()
64 MapIt it = cache_.begin();
65 for (; it != cache_.end(); ++it)
68 MapIt2 it2 = cache2_.begin();
69 for (; it2 != cache2_.end(); ++it2)
75 XftColor * ColorHandler::getXftColor(LColor_color clr)
77 XftColor * xclr = colorCache.getXftColor(clr);
80 Colormap colormap = GDK_COLORMAP_XCOLORMAP(
81 owner_.getColormap()->gobj());
82 Visual * visual = GDK_VISUAL_XVISUAL(
83 owner_.getColormap()->get_visual()->gobj());
84 XftColorAllocName(owner_.getDisplay(), visual, colormap,
86 lcolor.getX11Name(clr).c_str())
88 colorCache.cacheXftColor(clr, xclr);
94 Gdk::Color * ColorHandler::getGdkColor(LColor_color clr)
96 Gdk::Color * gclr = colorCache.getColor(clr);
98 gclr = new Gdk::Color;
99 gclr->parse(lcolor.getX11Name(clr));
100 owner_.getColormap()->alloc_color(*gclr);
101 colorCache.cacheColor(clr, gclr);
111 mouse_button::state gtkButtonState(unsigned int state)
113 mouse_button::state b = mouse_button::none;
114 if (state & GDK_BUTTON1_MASK)
115 b = mouse_button::button1;
116 else if (state & GDK_BUTTON2_MASK)
117 b = mouse_button::button2;
118 else if (state & GDK_BUTTON3_MASK)
119 b = mouse_button::button3;
120 else if (state & GDK_BUTTON3_MASK)
121 b = mouse_button::button3;
122 else if (state & GDK_BUTTON4_MASK)
123 b = mouse_button::button4;
124 else if (state & GDK_BUTTON5_MASK)
125 b = mouse_button::button5;
130 key_modifier::state gtkKeyState(guint state)
132 key_modifier::state k = key_modifier::none;
133 if (state & GDK_CONTROL_MASK)
134 k |= key_modifier::ctrl;
135 if (state & GDK_SHIFT_MASK)
136 k |= key_modifier::shift;
137 if (state & GDK_MOD1_MASK)
138 k |= key_modifier::alt;
143 void inputCommitRelay(GtkIMContext */*imcontext*/, gchar * str, GWorkArea * area)
145 area->inputCommit(str);
152 GWorkArea::GWorkArea(int width, int height)
153 : workAreaPixmap_(0), painter_(*this), draw_(0), colorHandler_(*this)
155 workArea_.set_size_request(width, height);
156 workArea_.set_double_buffered(false);
157 workArea_.add_events(Gdk::STRUCTURE_MASK | Gdk::EXPOSURE_MASK |
158 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
159 Gdk::KEY_PRESS_MASK | Gdk::BUTTON1_MOTION_MASK);
160 workArea_.signal_expose_event().connect(
161 SigC::slot(*this, &GWorkArea::onExpose));
162 workArea_.signal_configure_event().connect(
163 SigC::slot(*this, &GWorkArea::onConfigure));
164 workArea_.signal_button_press_event().connect(
165 SigC::slot(*this, &GWorkArea::onButtonPress));
166 workArea_.signal_button_release_event().connect(
167 SigC::slot(*this, &GWorkArea::onButtonRelease));
168 workArea_.signal_key_press_event().connect(
169 SigC::slot(*this, &GWorkArea::onKeyPress));
170 workArea_.signal_motion_notify_event().connect(
171 SigC::slot(*this, &GWorkArea::onMotionNotify));
173 vscrollbar_.get_adjustment()->signal_value_changed().connect(
174 SigC::slot(*this, &GWorkArea::onScroll));
176 hbox_.children().push_back(Gtk::Box_Helpers::Element(workArea_));
177 hbox_.children().push_back(
178 Gtk::Box_Helpers::Element(vscrollbar_,Gtk::PACK_SHRINK));
180 GView::instance()->getVBox().children().push_back(
181 Gtk::Box_Helpers::Element(hbox_));
182 workArea_.set_flags(workArea_.get_flags() | Gtk::CAN_DEFAULT |
184 workArea_.grab_default();
185 GView::instance()->setGWorkArea(&workArea_);
186 imContext_ = GTK_IM_CONTEXT(gtk_im_multicontext_new());
187 g_signal_connect(G_OBJECT(imContext_), "commit",
188 G_CALLBACK(&inputCommitRelay),
193 GWorkArea::~GWorkArea()
195 g_object_unref(imContext_);
199 Painter & GWorkArea::getPainter()
205 int GWorkArea::workWidth() const
207 return workArea_.get_width();
211 int GWorkArea::workHeight() const
213 return workArea_.get_height();
217 int GWorkArea::xpos() const
223 int GWorkArea::ypos() const
229 Glib::RefPtr<Gdk::Window> GWorkArea::getWindow()
231 return workArea_.get_window();
235 Display * GWorkArea::getDisplay() const
237 return GDK_WINDOW_XDISPLAY(
238 const_cast<GdkWindow*>(workArea_.get_window()->gobj()));
242 Glib::RefPtr<Gdk::Pixmap> GWorkArea::getPixmap()
244 return workAreaPixmap_;
248 Glib::RefPtr<Gdk::GC> GWorkArea::getGC()
254 Glib::RefPtr<Gdk::Colormap> GWorkArea::getColormap()
256 return workArea_.get_colormap();
260 XftDraw * GWorkArea::getXftDraw()
266 ColorHandler & GWorkArea::getColorHandler()
268 return colorHandler_;
272 bool GWorkArea::onExpose(GdkEventExpose * event)
274 workArea_.get_window()->draw_drawable(
275 workArea_.get_style()->get_black_gc(),
277 event->area.x, event->area.y,
278 event->area.x, event->area.y,
279 event->area.width, event->area.height);
284 bool GWorkArea::onConfigure(GdkEventConfigure * /*event*/)
286 int x, y, width, height, depth;
287 workArea_.get_window()->get_geometry(x, y, width, height, depth);
289 XftDrawDestroy(draw_);
290 workAreaPixmap_ = Gdk::Pixmap::create(workArea_.get_window(),
291 width, height, depth);
292 Pixmap pixmap = GDK_PIXMAP_XID(workAreaPixmap_->gobj());
293 Colormap colormap = GDK_COLORMAP_XCOLORMAP(
294 workArea_.get_colormap()->gobj());
295 Visual * visual = GDK_VISUAL_XVISUAL(
296 workArea_.get_colormap()->get_visual()->gobj());
297 draw_ = XftDrawCreate(getDisplay(), pixmap,
300 workAreaGC_ = Gdk::GC::create(workArea_.get_window());
301 Gdk::Cursor cursor(Gdk::XTERM);
302 workArea_.get_window()->set_cursor(cursor);
303 gtk_im_context_set_client_window(
304 imContext_, workArea_.get_window()->gobj());
311 void GWorkArea::setScrollbarParams(int height, int pos, int line_height)
313 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
314 adjustment->set_lower(0);
315 int workAreaHeight = workHeight();
316 if (!height || height < workAreaHeight) {
317 adjustment->set_upper(workAreaHeight);
318 adjustment->set_page_size(workAreaHeight);
319 adjustment->set_value(0);
320 adjustment->changed();
323 adjustment->set_step_increment(line_height);
324 adjustment->set_page_increment(workAreaHeight - line_height);
325 adjustment->set_upper(height);
326 adjustment->set_page_size(workAreaHeight);
327 adjustment->set_value(pos);
328 adjustment->changed();
332 void GWorkArea::onScroll()
334 double val = vscrollbar_.get_adjustment()->get_value();
335 scrollDocView(static_cast<int>(val));
339 bool GWorkArea::onButtonPress(GdkEventButton * event)
341 kb_action ka = LFUN_MOUSE_PRESS;
342 switch (event->type) {
343 case GDK_BUTTON_PRESS:
344 ka = LFUN_MOUSE_PRESS;
346 case GDK_2BUTTON_PRESS:
347 ka = LFUN_MOUSE_DOUBLE;
349 case GDK_3BUTTON_PRESS:
350 ka = LFUN_MOUSE_TRIPLE;
355 dispatch(FuncRequest(ka,
356 static_cast<int>(event->x),
357 static_cast<int>(event->y),
358 static_cast<mouse_button::state>(event->button)));
359 workArea_.grab_focus();
364 bool GWorkArea::onButtonRelease(GdkEventButton * event)
366 dispatch(FuncRequest(LFUN_MOUSE_RELEASE,
367 static_cast<int>(event->x),
368 static_cast<int>(event->y),
369 static_cast<mouse_button::state>(event->button)));
374 bool GWorkArea::onMotionNotify(GdkEventMotion * event)
376 static guint32 timeBefore;
377 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
378 double step = adjustment->get_step_increment();
379 double value = adjustment->get_value();
382 else if (event->x > workArea_.get_height())
384 if (value != adjustment->get_value()) {
385 if (event->time - timeBefore > 200) {
386 adjustment->set_value(value);
387 adjustment->value_changed();
389 timeBefore = event->time;
391 dispatch(FuncRequest(LFUN_MOUSE_MOTION,
392 static_cast<int>(event->x),
393 static_cast<int>(event->y),
394 gtkButtonState(event->state)));
399 void GWorkArea::inputCommit(gchar * str)
401 inputCache_ = Glib::locale_from_utf8(str);
405 bool GWorkArea::onKeyPress(GdkEventKey * event)
409 bool inputGet = gtk_im_context_filter_keypress(imContext_, event);
411 if ((inputGet && inputCache_.size() == 1 && inputCache_[0] < 128) ||
414 GLyXKeySym *glk = new GLyXKeySym(event->keyval);
415 workAreaKeyPress(LyXKeySymPtr(glk),
416 gtkKeyState(event->state));
418 } else if (!inputCache_.empty())
419 workAreaCJK_IMprocess(inputCache_.size(), inputCache_.data());
425 void GWorkArea::onClipboardGet(Gtk::SelectionData & /*selection_data*/,
428 selectionRequested();
432 void GWorkArea::onClipboardClear()
438 void GWorkArea::haveSelection(bool toHave) const
441 Glib::RefPtr<Gtk::Clipboard> clipboard =
442 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
443 std::vector<Gtk::TargetEntry> listTargets;
444 listTargets.push_back(Gtk::TargetEntry("UTF8_STRING"));
445 clipboard->set(listTargets,
446 SigC::slot(const_cast<GWorkArea&>(*this),
447 &GWorkArea::onClipboardGet),
448 SigC::slot(const_cast<GWorkArea&>(*this),
449 &GWorkArea::onClipboardClear));
454 string const GWorkArea::getClipboard() const
456 Glib::RefPtr<Gtk::Clipboard> clipboard =
457 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
458 return Glib::locale_from_utf8(clipboard->wait_for_text());
462 void GWorkArea::putClipboard(string const & str) const
464 Glib::RefPtr<Gtk::Clipboard> clipboard =
465 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
466 clipboard->set_text(Glib::locale_to_utf8(str));