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"
24 ColorCache colorCache;
27 ColorCache::~ColorCache()
33 Gdk::Color * ColorCache::getColor(LColor_color clr)
35 MapIt it = cache_.find(clr);
36 return it == cache_.end() ? 0 : it->second;
40 XftColor * ColorCache::getXftColor(LColor_color clr)
42 MapIt2 it = cache2_.find(clr);
43 return it == cache2_.end() ? 0 : it->second;
47 void ColorCache::cacheColor(LColor_color clr, Gdk::Color * gclr)
53 void ColorCache::cacheXftColor(LColor_color clr, XftColor * xclr)
59 void ColorCache::clear()
61 MapIt it = cache_.begin();
62 for (; it != cache_.end(); ++it)
65 MapIt2 it2 = cache2_.begin();
66 for (; it2 != cache2_.end(); ++it2)
72 XftColor * ColorHandler::getXftColor(LColor_color clr)
74 XftColor * xclr = colorCache.getXftColor(clr);
77 Colormap colormap = GDK_COLORMAP_XCOLORMAP(
78 owner_.getColormap()->gobj());
79 Visual * visual = GDK_VISUAL_XVISUAL(
80 owner_.getColormap()->get_visual()->gobj());
81 XftColorAllocName(owner_.getDisplay(), visual, colormap,
82 lcolor.getX11Name(clr).c_str(), xclr);
83 colorCache.cacheXftColor(clr, xclr);
89 Gdk::Color * ColorHandler::getGdkColor(LColor_color clr)
91 Gdk::Color * gclr = colorCache.getColor(clr);
93 gclr = new Gdk::Color;
94 gclr->parse(lcolor.getX11Name(clr));
95 owner_.getColormap()->alloc_color(*gclr);
96 colorCache.cacheColor(clr, gclr);
106 mouse_button::state gtkButtonState(unsigned int state)
108 mouse_button::state b = mouse_button::none;
109 if (state & GDK_BUTTON1_MASK)
110 b = mouse_button::button1;
111 else if (state & GDK_BUTTON2_MASK)
112 b = mouse_button::button2;
113 else if (state & GDK_BUTTON3_MASK)
114 b = mouse_button::button3;
115 else if (state & GDK_BUTTON3_MASK)
116 b = mouse_button::button3;
117 else if (state & GDK_BUTTON4_MASK)
118 b = mouse_button::button4;
119 else if (state & GDK_BUTTON5_MASK)
120 b = mouse_button::button5;
125 key_modifier::state gtkKeyState(guint state)
127 key_modifier::state k = key_modifier::none;
128 if (state & GDK_CONTROL_MASK)
129 k |= key_modifier::ctrl;
130 if (state & GDK_SHIFT_MASK)
131 k |= key_modifier::shift;
132 if (state & GDK_MOD1_MASK)
133 k |= key_modifier::alt;
138 void inputCommitRelay(GtkIMContext */*imcontext*/, gchar * str, GWorkArea * area)
140 area->inputCommit(str);
147 GWorkArea::GWorkArea(int width, int height)
148 : workAreaPixmap_(0), painter_(*this), draw_(0), colorHandler_(*this)
150 workArea_.set_size_request(width, height);
151 workArea_.set_double_buffered(false);
152 workArea_.add_events(Gdk::STRUCTURE_MASK | Gdk::EXPOSURE_MASK |
153 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
154 Gdk::KEY_PRESS_MASK | Gdk::BUTTON1_MOTION_MASK);
155 workArea_.signal_expose_event().connect(
156 SigC::slot(*this, &GWorkArea::onExpose));
157 workArea_.signal_configure_event().connect(
158 SigC::slot(*this, &GWorkArea::onConfigure));
159 workArea_.signal_button_press_event().connect(
160 SigC::slot(*this, &GWorkArea::onButtonPress));
161 workArea_.signal_button_release_event().connect(
162 SigC::slot(*this, &GWorkArea::onButtonRelease));
163 workArea_.signal_key_press_event().connect(
164 SigC::slot(*this, &GWorkArea::onKeyPress));
165 workArea_.signal_motion_notify_event().connect(
166 SigC::slot(*this, &GWorkArea::onMotionNotify));
168 vscrollbar_.get_adjustment()->signal_value_changed().connect(
169 SigC::slot(*this, &GWorkArea::onScroll));
171 hbox_.children().push_back(Gtk::Box_Helpers::Element(workArea_));
172 hbox_.children().push_back(
173 Gtk::Box_Helpers::Element(vscrollbar_,Gtk::PACK_SHRINK));
175 GView::instance()->getVBox().children().push_back(
176 Gtk::Box_Helpers::Element(hbox_));
177 workArea_.set_flags(workArea_.get_flags() | Gtk::CAN_DEFAULT |
179 workArea_.grab_default();
180 GView::instance()->setGWorkArea(&workArea_);
181 imContext_ = GTK_IM_CONTEXT(gtk_im_multicontext_new());
182 g_signal_connect(G_OBJECT(imContext_), "commit",
183 G_CALLBACK(&inputCommitRelay),
188 GWorkArea::~GWorkArea()
190 g_object_unref(imContext_);
194 Painter & GWorkArea::getPainter()
200 int GWorkArea::workWidth() const
202 return workArea_.get_width();
206 int GWorkArea::workHeight() const
208 return workArea_.get_height();
212 int GWorkArea::xpos() const
218 int GWorkArea::ypos() const
224 Glib::RefPtr<Gdk::Window> GWorkArea::getWindow()
226 return workArea_.get_window();
230 Display * GWorkArea::getDisplay() const
232 return GDK_WINDOW_XDISPLAY(
233 const_cast<GdkWindow*>(workArea_.get_window()->gobj()));
237 Glib::RefPtr<Gdk::Pixmap> GWorkArea::getPixmap()
239 return workAreaPixmap_;
243 Glib::RefPtr<Gdk::GC> GWorkArea::getGC()
249 Glib::RefPtr<Gdk::Colormap> GWorkArea::getColormap()
251 return workArea_.get_colormap();
255 XftDraw * GWorkArea::getXftDraw()
261 ColorHandler & GWorkArea::getColorHandler()
263 return colorHandler_;
267 bool GWorkArea::onExpose(GdkEventExpose * event)
269 workArea_.get_window()->draw_drawable(
270 workArea_.get_style()->get_black_gc(),
272 event->area.x, event->area.y,
273 event->area.x, event->area.y,
274 event->area.width, event->area.height);
279 bool GWorkArea::onConfigure(GdkEventConfigure * /*event*/)
281 int x, y, width, height, depth;
282 workArea_.get_window()->get_geometry(x, y, width, height, depth);
284 XftDrawDestroy(draw_);
285 workAreaPixmap_ = Gdk::Pixmap::create(workArea_.get_window(),
286 width, height, depth);
287 Pixmap pixmap = GDK_PIXMAP_XID(workAreaPixmap_->gobj());
288 Colormap colormap = GDK_COLORMAP_XCOLORMAP(
289 workArea_.get_colormap()->gobj());
290 Visual * visual = GDK_VISUAL_XVISUAL(
291 workArea_.get_colormap()->get_visual()->gobj());
292 draw_ = XftDrawCreate(getDisplay(), pixmap,
295 workAreaGC_ = Gdk::GC::create(workArea_.get_window());
296 Gdk::Cursor cursor(Gdk::XTERM);
297 workArea_.get_window()->set_cursor(cursor);
298 gtk_im_context_set_client_window(
299 imContext_, workArea_.get_window()->gobj());
306 void GWorkArea::setScrollbarParams(int height, int pos, int line_height)
308 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
309 adjustment->set_lower(0);
310 int workAreaHeight = workHeight();
311 if (!height || height < workAreaHeight) {
312 adjustment->set_upper(workAreaHeight);
313 adjustment->set_page_size(workAreaHeight);
314 adjustment->set_value(0);
315 adjustment->changed();
318 adjustment->set_step_increment(line_height);
319 adjustment->set_page_increment(workAreaHeight - line_height);
320 adjustment->set_upper(height);
321 adjustment->set_page_size(workAreaHeight);
322 adjustment->set_value(pos);
323 adjustment->changed();
327 void GWorkArea::onScroll()
329 double val = vscrollbar_.get_adjustment()->get_value();
330 scrollDocView(static_cast<int>(val));
334 bool GWorkArea::onButtonPress(GdkEventButton * event)
336 kb_action ka = LFUN_MOUSE_PRESS;
337 switch (event->type) {
338 case GDK_BUTTON_PRESS:
339 ka = LFUN_MOUSE_PRESS;
341 case GDK_2BUTTON_PRESS:
342 ka = LFUN_MOUSE_DOUBLE;
344 case GDK_3BUTTON_PRESS:
345 ka = LFUN_MOUSE_TRIPLE;
350 dispatch(FuncRequest(ka,
351 static_cast<int>(event->x),
352 static_cast<int>(event->y),
353 static_cast<mouse_button::state>(event->button)));
354 workArea_.grab_focus();
359 bool GWorkArea::onButtonRelease(GdkEventButton * event)
361 dispatch(FuncRequest(LFUN_MOUSE_RELEASE,
362 static_cast<int>(event->x),
363 static_cast<int>(event->y),
364 static_cast<mouse_button::state>(event->button)));
369 bool GWorkArea::onMotionNotify(GdkEventMotion * event)
371 static guint32 timeBefore;
372 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
373 double step = adjustment->get_step_increment();
374 double value = adjustment->get_value();
377 else if (event->x > workArea_.get_height())
379 if (value != adjustment->get_value()) {
380 if (event->time - timeBefore > 200) {
381 adjustment->set_value(value);
382 adjustment->value_changed();
384 timeBefore = event->time;
386 dispatch(FuncRequest(LFUN_MOUSE_MOTION,
387 static_cast<int>(event->x),
388 static_cast<int>(event->y),
389 gtkButtonState(event->state)));
394 void GWorkArea::inputCommit(gchar * str)
396 inputCache_ = Glib::locale_from_utf8(str);
400 bool GWorkArea::onKeyPress(GdkEventKey * event)
404 bool inputGet = gtk_im_context_filter_keypress(imContext_, event);
406 if ((inputGet && inputCache_.size() == 1 && inputCache_[0] < 128) ||
409 GLyXKeySym *glk = new GLyXKeySym(event->keyval);
410 workAreaKeyPress(LyXKeySymPtr(glk),
411 gtkKeyState(event->state));
413 } else if (!inputCache_.empty())
414 workAreaCJK_IMprocess(inputCache_.size(), inputCache_.data());
420 void GWorkArea::onClipboardGet(Gtk::SelectionData & /*selection_data*/,
423 selectionRequested();
427 void GWorkArea::onClipboardClear()
433 void GWorkArea::haveSelection(bool toHave) const
436 Glib::RefPtr<Gtk::Clipboard> clipboard =
437 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
438 std::vector<Gtk::TargetEntry> listTargets;
439 listTargets.push_back(Gtk::TargetEntry("UTF8_STRING"));
440 clipboard->set(listTargets,
441 SigC::slot(const_cast<GWorkArea&>(*this),
442 &GWorkArea::onClipboardGet),
443 SigC::slot(const_cast<GWorkArea&>(*this),
444 &GWorkArea::onClipboardClear));
449 string const GWorkArea::getClipboard() const
451 Glib::RefPtr<Gtk::Clipboard> clipboard =
452 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
453 return Glib::locale_from_utf8(clipboard->wait_for_text());
457 void GWorkArea::putClipboard(string const & str) const
459 Glib::RefPtr<Gtk::Clipboard> clipboard =
460 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
461 clipboard->set_text(Glib::locale_to_utf8(str));