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"
27 #include "funcrequest.h"
30 using boost::shared_ptr;
39 mouse_button::state gButtonToLyx(guint gdkbutton)
41 // GDK uses int 1,2,3 but lyx uses enums (1,2,4)
44 return mouse_button::button1;
46 return mouse_button::button2;
48 return mouse_button::button3;
50 return mouse_button::button4;
52 return mouse_button::button5;
55 // This shouldn't happen, according to gdk docs
56 lyxerr << "gButtonToLyx: unhandled button index\n";
57 return mouse_button::button1;
62 ColorCache colorCache;
64 Gdk::Color * ColorCache::getColor(LColor_color clr)
66 MapIt it = cache_.find(clr);
67 return it == cache_.end() ? 0 : it->second.get();
71 XftColor * ColorCache::getXftColor(LColor_color clr)
73 MapIt2 it = cache2_.find(clr);
74 return it == cache2_.end() ? 0 : it->second.get();
78 void ColorCache::cacheColor(LColor_color clr, Gdk::Color * gclr)
80 cache_[clr] = shared_ptr<Gdk::Color>(gclr);
84 void ColorCache::cacheXftColor(LColor_color clr, XftColor * xclr)
86 cache2_[clr] = shared_ptr<XftColor>(xclr);
90 void ColorCache::clear()
97 XftColor * ColorHandler::getXftColor(LColor_color clr)
99 XftColor * xclr = colorCache.getXftColor(clr);
102 Colormap colormap = GDK_COLORMAP_XCOLORMAP(
103 owner_.getColormap()->gobj());
104 Visual * visual = GDK_VISUAL_XVISUAL(
105 owner_.getColormap()->get_visual()->gobj());
106 XftColorAllocName(owner_.getDisplay(), visual, colormap,
108 lcolor.getX11Name(clr).c_str())
110 colorCache.cacheXftColor(clr, xclr);
116 Gdk::Color * ColorHandler::getGdkColor(LColor_color clr)
118 Gdk::Color * gclr = colorCache.getColor(clr);
120 gclr = new Gdk::Color;
121 gclr->parse(lcolor.getX11Name(clr));
122 owner_.getColormap()->alloc_color(*gclr);
123 colorCache.cacheColor(clr, gclr);
133 mouse_button::state gtkButtonState(unsigned int state)
135 mouse_button::state b = mouse_button::none;
136 if (state & GDK_BUTTON1_MASK)
137 b = mouse_button::button1;
138 else if (state & GDK_BUTTON2_MASK)
139 b = mouse_button::button2;
140 else if (state & GDK_BUTTON3_MASK)
141 b = mouse_button::button3;
142 else if (state & GDK_BUTTON3_MASK)
143 b = mouse_button::button3;
144 else if (state & GDK_BUTTON4_MASK)
145 b = mouse_button::button4;
146 else if (state & GDK_BUTTON5_MASK)
147 b = mouse_button::button5;
152 key_modifier::state gtkKeyState(guint state)
154 key_modifier::state k = key_modifier::none;
155 if (state & GDK_CONTROL_MASK)
156 k |= key_modifier::ctrl;
157 if (state & GDK_SHIFT_MASK)
158 k |= key_modifier::shift;
159 if (state & GDK_MOD1_MASK)
160 k |= key_modifier::alt;
165 void inputCommitRelay(GtkIMContext */*imcontext*/, gchar * str, GWorkArea * area)
167 area->inputCommit(str);
174 GWorkArea::GWorkArea(LyXView & owner, int width, int height)
175 : workAreaPixmap_(0), painter_(*this), draw_(0), colorHandler_(*this)
177 workArea_.set_size_request(width, height);
178 workArea_.set_double_buffered(false);
179 workArea_.add_events(Gdk::STRUCTURE_MASK | Gdk::EXPOSURE_MASK |
180 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
181 Gdk::KEY_PRESS_MASK | Gdk::BUTTON1_MOTION_MASK);
182 workArea_.signal_expose_event().connect(
183 sigc::mem_fun(*this, &GWorkArea::onExpose));
184 workArea_.signal_configure_event().connect(
185 sigc::mem_fun(*this, &GWorkArea::onConfigure));
186 workArea_.signal_button_press_event().connect(
187 sigc::mem_fun(*this, &GWorkArea::onButtonPress));
188 workArea_.signal_button_release_event().connect(
189 sigc::mem_fun(*this, &GWorkArea::onButtonRelease));
190 workArea_.signal_key_press_event().connect(
191 sigc::mem_fun(*this, &GWorkArea::onKeyPress));
192 workArea_.signal_motion_notify_event().connect(
193 sigc::mem_fun(*this, &GWorkArea::onMotionNotify));
195 vscrollbar_.get_adjustment()->signal_value_changed().connect(
196 sigc::mem_fun(*this, &GWorkArea::onScroll));
197 workArea_.signal_scroll_event().connect(
198 sigc::mem_fun(*this, &GWorkArea::onScrollWheel));
200 hbox_.children().push_back(Gtk::Box_Helpers::Element(workArea_));
201 hbox_.children().push_back(
202 Gtk::Box_Helpers::Element(vscrollbar_,Gtk::PACK_SHRINK));
205 GView & gview = static_cast<GView &>(owner);
206 gview.getBox(GView::Center).children().push_back(
207 Gtk::Box_Helpers::Element(hbox_));
209 workArea_.set_flags(workArea_.get_flags() | Gtk::CAN_DEFAULT |
211 workArea_.grab_default();
212 gview.setGWorkArea(&workArea_);
213 imContext_ = GTK_IM_CONTEXT(gtk_im_multicontext_new());
214 g_signal_connect(G_OBJECT(imContext_), "commit",
215 G_CALLBACK(&inputCommitRelay),
220 GWorkArea::~GWorkArea()
222 g_object_unref(imContext_);
226 Painter & GWorkArea::getPainter()
232 int GWorkArea::workWidth() const
234 return workArea_.get_width();
238 int GWorkArea::workHeight() const
240 return workArea_.get_height();
244 int GWorkArea::xpos() const
250 int GWorkArea::ypos() const
256 Glib::RefPtr<Gdk::Window> GWorkArea::getWindow()
258 return workArea_.get_window();
262 Display * GWorkArea::getDisplay() const
264 return GDK_WINDOW_XDISPLAY(
265 const_cast<GdkWindow*>(workArea_.get_window()->gobj()));
269 Glib::RefPtr<Gdk::Pixmap> GWorkArea::getPixmap()
271 return workAreaPixmap_;
275 Glib::RefPtr<Gdk::GC> GWorkArea::getGC()
281 Glib::RefPtr<Gdk::Colormap> GWorkArea::getColormap()
283 return workArea_.get_colormap();
287 XftDraw * GWorkArea::getXftDraw()
293 ColorHandler & GWorkArea::getColorHandler()
295 return colorHandler_;
299 bool GWorkArea::onExpose(GdkEventExpose * event)
301 workArea_.get_window()->draw_drawable(
302 workArea_.get_style()->get_black_gc(),
304 event->area.x, event->area.y,
305 event->area.x, event->area.y,
306 event->area.width, event->area.height);
311 bool GWorkArea::onConfigure(GdkEventConfigure * /*event*/)
313 int x, y, width, height, depth;
314 workArea_.get_window()->get_geometry(x, y, width, height, depth);
316 XftDrawDestroy(draw_);
317 workAreaPixmap_ = Gdk::Pixmap::create(workArea_.get_window(),
318 width, height, depth);
319 Pixmap pixmap = GDK_PIXMAP_XID(workAreaPixmap_->gobj());
320 Colormap colormap = GDK_COLORMAP_XCOLORMAP(
321 workArea_.get_colormap()->gobj());
322 Visual * visual = GDK_VISUAL_XVISUAL(
323 workArea_.get_colormap()->get_visual()->gobj());
324 draw_ = XftDrawCreate(getDisplay(), pixmap,
327 workAreaGC_ = Gdk::GC::create(workArea_.get_window());
328 Gdk::Cursor cursor(Gdk::XTERM);
329 workArea_.get_window()->set_cursor(cursor);
330 gtk_im_context_set_client_window(
331 imContext_, workArea_.get_window()->gobj());
338 void GWorkArea::setScrollbarParams(int height, int pos, int line_height)
340 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
341 adjustment->set_lower(0);
342 int workAreaHeight = workHeight();
343 if (!height || height < workAreaHeight) {
344 adjustment->set_upper(workAreaHeight);
345 adjustment->set_page_size(workAreaHeight);
346 adjustment->set_value(0);
347 adjustment->changed();
350 adjustment->set_step_increment(line_height * 3);
351 adjustment->set_page_increment(workAreaHeight - line_height);
352 // Allow the user half a screen of blank at the end
353 // to make scrollbar consistant with centering the cursor
354 adjustment->set_upper(height + workAreaHeight / 2);
355 adjustment->set_page_size(workAreaHeight);
356 adjustment->set_value(pos);
357 adjustment->changed();
361 void GWorkArea::onScroll()
363 double val = vscrollbar_.get_adjustment()->get_value();
364 scrollDocView(static_cast<int>(val));
368 bool GWorkArea::onScrollWheel(GdkEventScroll * event)
370 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
373 if (event->state & GDK_CONTROL_MASK)
374 step = adjustment->get_page_increment();
376 step = adjustment->get_step_increment();
378 if (event->direction == GDK_SCROLL_UP)
381 double target = adjustment->get_value() + step;
382 // Prevent the user getting a whole screen of blank when they
383 // try to scroll past the end of the doc
384 double max = adjustment->get_upper() - workHeight();
388 adjustment->set_value(target);
393 bool GWorkArea::onButtonPress(GdkEventButton * event)
395 kb_action ka = LFUN_MOUSE_PRESS;
396 switch (event->type) {
397 case GDK_BUTTON_PRESS:
398 ka = LFUN_MOUSE_PRESS;
400 case GDK_2BUTTON_PRESS:
401 ka = LFUN_MOUSE_DOUBLE;
403 case GDK_3BUTTON_PRESS:
404 ka = LFUN_MOUSE_TRIPLE;
409 dispatch(FuncRequest(ka,
410 static_cast<int>(event->x),
411 static_cast<int>(event->y),
412 gButtonToLyx(event->button)));
413 workArea_.grab_focus();
418 bool GWorkArea::onButtonRelease(GdkEventButton * event)
420 dispatch(FuncRequest(LFUN_MOUSE_RELEASE,
421 static_cast<int>(event->x),
422 static_cast<int>(event->y),
423 gButtonToLyx(event->button)));
428 bool GWorkArea::onMotionNotify(GdkEventMotion * event)
430 static guint32 timeBefore;
431 Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
432 double step = adjustment->get_step_increment();
433 double value = adjustment->get_value();
436 else if (event->x > workArea_.get_height())
438 if (value != adjustment->get_value()) {
439 if (event->time - timeBefore > 200) {
440 adjustment->set_value(value);
441 adjustment->value_changed();
443 timeBefore = event->time;
445 dispatch(FuncRequest(LFUN_MOUSE_MOTION,
446 static_cast<int>(event->x),
447 static_cast<int>(event->y),
448 gtkButtonState(event->state)));
453 void GWorkArea::inputCommit(gchar * str)
455 inputCache_ = Glib::locale_from_utf8(str);
459 bool GWorkArea::onKeyPress(GdkEventKey * event)
463 bool inputGet = gtk_im_context_filter_keypress(imContext_, event);
465 if ((inputGet && inputCache_.size() == 1 && inputCache_[0] < 128) ||
468 GLyXKeySym *glk = new GLyXKeySym(event->keyval);
469 workAreaKeyPress(LyXKeySymPtr(glk),
470 gtkKeyState(event->state));
472 } else if (!inputCache_.empty())
473 workAreaCJK_IMprocess(inputCache_.size(), inputCache_.data());
479 void GWorkArea::onClipboardGet(Gtk::SelectionData & /*selection_data*/,
482 selectionRequested();
486 void GWorkArea::onClipboardClear()
492 void GWorkArea::haveSelection(bool toHave) const
495 Glib::RefPtr<Gtk::Clipboard> clipboard =
496 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
497 std::vector<Gtk::TargetEntry> listTargets;
498 listTargets.push_back(Gtk::TargetEntry("UTF8_STRING"));
499 clipboard->set(listTargets,
500 sigc::mem_fun(const_cast<GWorkArea&>(*this),
501 &GWorkArea::onClipboardGet),
502 sigc::mem_fun(const_cast<GWorkArea&>(*this),
503 &GWorkArea::onClipboardClear));
508 string const GWorkArea::getClipboard() const
510 Glib::RefPtr<Gtk::Clipboard> clipboard =
511 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
512 return Glib::locale_from_utf8(clipboard->wait_for_text());
516 void GWorkArea::putClipboard(string const & str) const
518 Glib::RefPtr<Gtk::Clipboard> clipboard =
519 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
520 clipboard->set_text(Glib::locale_to_utf8(str));
523 } // namespace frontend