]> git.lyx.org Git - features.git/blob - src/frontends/gtk/GWorkArea.C
Rename EnumLColor as LColor_color.
[features.git] / src / frontends / gtk / GWorkArea.C
1 /**
2  * \file GWorkArea.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Huang Ying
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12 #include <gtkmm.h>
13 #include <X11/Xft/Xft.h>
14
15 #include "GWorkArea.h"
16 #include "GView.h"
17 #include "GtkmmX.h"
18 #include "GLyXKeySym.h"
19
20 #include "debug.h"
21 #include "funcrequest.h"
22 #include "LColor.h"
23
24 ColorCache colorCache;
25
26
27 ColorCache::~ColorCache()
28 {
29         clear();
30 }
31
32
33 Gdk::Color * ColorCache::getColor(LColor_color clr)
34 {
35         MapIt it = cache_.find(clr);
36         return it == cache_.end() ? 0 : it->second;
37 }
38
39
40 XftColor * ColorCache::getXftColor(LColor_color clr)
41 {
42         MapIt2 it = cache2_.find(clr);
43         return it == cache2_.end() ? 0 : it->second;
44 }
45
46
47 void ColorCache::cacheColor(LColor_color clr, Gdk::Color * gclr)
48 {
49         cache_[clr] = gclr;
50 }
51
52
53 void ColorCache::cacheXftColor(LColor_color clr, XftColor * xclr)
54 {
55         cache2_[clr] = xclr;
56 }
57
58
59 void ColorCache::clear()
60 {
61         MapIt it = cache_.begin();
62         for (; it != cache_.end(); ++it)
63                 delete it->second;
64         cache_.clear();
65         MapIt2 it2 = cache2_.begin();
66         for (; it2 != cache2_.end(); ++it2)
67                 delete it2->second;
68         cache2_.clear();
69 }
70
71
72 XftColor * ColorHandler::getXftColor(LColor_color clr)
73 {
74         XftColor * xclr = colorCache.getXftColor(clr);
75         if (!xclr) {
76                 xclr = new XftColor;
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);
84         }
85         return xclr;
86 }
87
88
89 Gdk::Color * ColorHandler::getGdkColor(LColor_color clr)
90 {
91         Gdk::Color * gclr = colorCache.getColor(clr);
92         if (!gclr) {
93                 gclr = new Gdk::Color;
94                 gclr->parse(lcolor.getX11Name(clr));
95                 owner_.getColormap()->alloc_color(*gclr);
96                 colorCache.cacheColor(clr, gclr);
97         }
98         return gclr;
99 }
100
101
102 namespace
103 {
104
105
106 mouse_button::state gtkButtonState(unsigned int state)
107 {
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;
121         return b;
122 }
123
124
125 key_modifier::state gtkKeyState(guint state)
126 {
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;
134         return k;
135 }
136
137
138 void inputCommitRelay(GtkIMContext */*imcontext*/, gchar * str, GWorkArea * area)
139 {
140         area->inputCommit(str);
141 }
142
143
144 }
145
146
147 GWorkArea::GWorkArea(int width, int height)
148         : workAreaPixmap_(0), painter_(*this), draw_(0), colorHandler_(*this)
149 {
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));
167         workArea_.show();
168         vscrollbar_.get_adjustment()->signal_value_changed().connect(
169                 SigC::slot(*this, &GWorkArea::onScroll));
170         vscrollbar_.show();
171         hbox_.children().push_back(Gtk::Box_Helpers::Element(workArea_));
172         hbox_.children().push_back(
173                 Gtk::Box_Helpers::Element(vscrollbar_,Gtk::PACK_SHRINK));
174         hbox_.show();
175         GView::instance()->getVBox().children().push_back(
176                 Gtk::Box_Helpers::Element(hbox_));
177         workArea_.set_flags(workArea_.get_flags() | Gtk::CAN_DEFAULT |
178                             Gtk::CAN_FOCUS);
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),
184                          this);
185 }
186
187
188 GWorkArea::~GWorkArea()
189 {
190         g_object_unref(imContext_);
191 }
192
193
194 Painter & GWorkArea::getPainter()
195 {
196         return painter_;
197 }
198
199
200 int GWorkArea::workWidth() const
201 {
202         return workArea_.get_width();
203 }
204
205
206 int GWorkArea::workHeight() const
207 {
208         return workArea_.get_height();
209 }
210
211
212 int GWorkArea::xpos() const
213 {
214         return 0;
215 }
216
217
218 int GWorkArea::ypos() const
219 {
220         return 0;
221 }
222
223
224 Glib::RefPtr<Gdk::Window> GWorkArea::getWindow()
225 {
226         return workArea_.get_window();
227 }
228
229
230 Display * GWorkArea::getDisplay() const
231 {
232         return GDK_WINDOW_XDISPLAY(
233                 const_cast<GdkWindow*>(workArea_.get_window()->gobj()));
234 }
235
236
237 Glib::RefPtr<Gdk::Pixmap> GWorkArea::getPixmap()
238 {
239         return workAreaPixmap_;
240 }
241
242
243 Glib::RefPtr<Gdk::GC> GWorkArea::getGC()
244 {
245         return workAreaGC_;
246 }
247
248
249 Glib::RefPtr<Gdk::Colormap> GWorkArea::getColormap()
250 {
251         return workArea_.get_colormap();
252 }
253
254
255 XftDraw * GWorkArea::getXftDraw()
256 {
257         return draw_;
258 }
259
260
261 ColorHandler & GWorkArea::getColorHandler()
262 {
263         return colorHandler_;
264 }
265
266
267 bool GWorkArea::onExpose(GdkEventExpose * event)
268 {
269         workArea_.get_window()->draw_drawable(
270                 workArea_.get_style()->get_black_gc(),
271                 workAreaPixmap_,
272                 event->area.x, event->area.y,
273                 event->area.x, event->area.y,
274                 event->area.width, event->area.height);
275         return true;
276 }
277
278
279 bool GWorkArea::onConfigure(GdkEventConfigure * /*event*/)
280 {
281         int x, y, width, height, depth;
282         workArea_.get_window()->get_geometry(x, y, width, height, depth);
283         if (draw_)
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,
293                               visual, colormap);
294         if (!workAreaGC_) {
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());
300         }
301         workAreaResize();
302         return true;
303 }
304
305
306 void GWorkArea::setScrollbarParams(int height, int pos, int line_height)
307 {
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();
316                 return;
317         }
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();
324 }
325
326
327 void GWorkArea::onScroll()
328 {
329         double val = vscrollbar_.get_adjustment()->get_value();
330         scrollDocView(static_cast<int>(val));
331 }
332
333
334 bool GWorkArea::onButtonPress(GdkEventButton * event)
335 {
336         kb_action ka = LFUN_MOUSE_PRESS;
337         switch (event->type) {
338         case GDK_BUTTON_PRESS:
339                 ka = LFUN_MOUSE_PRESS;
340                 break;
341         case GDK_2BUTTON_PRESS:
342                 ka = LFUN_MOUSE_DOUBLE;
343                 break;
344         case GDK_3BUTTON_PRESS:
345                 ka = LFUN_MOUSE_TRIPLE;
346                 break;
347         default:
348                 break;
349         }
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();
355         return true;
356 }
357
358
359 bool GWorkArea::onButtonRelease(GdkEventButton * event)
360 {
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)));
365         return true;
366 }
367
368
369 bool GWorkArea::onMotionNotify(GdkEventMotion * event)
370 {
371         static guint32 timeBefore;
372         Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
373         double step = adjustment->get_step_increment();
374         double value = adjustment->get_value();
375         if (event->x < 0)
376                 value -= step;
377         else if (event->x > workArea_.get_height())
378                 value += step;
379         if (value != adjustment->get_value()) {
380                 if (event->time - timeBefore > 200) {
381                         adjustment->set_value(value);
382                         adjustment->value_changed();
383                 }
384                 timeBefore = event->time;
385         }
386         dispatch(FuncRequest(LFUN_MOUSE_MOTION,
387                              static_cast<int>(event->x),
388                              static_cast<int>(event->y),
389                              gtkButtonState(event->state)));
390         return true;
391 }
392
393
394 void GWorkArea::inputCommit(gchar * str)
395 {
396         inputCache_ = Glib::locale_from_utf8(str);
397 }
398
399
400 bool GWorkArea::onKeyPress(GdkEventKey * event)
401 {
402 #ifdef I18N
403         inputCache_ = "";
404         bool inputGet = gtk_im_context_filter_keypress(imContext_, event);
405         // cope with ascii
406         if ((inputGet && inputCache_.size() == 1 && inputCache_[0] < 128) ||
407             !inputGet) {
408 #endif
409                 GLyXKeySym *glk = new GLyXKeySym(event->keyval);
410                 workAreaKeyPress(LyXKeySymPtr(glk),
411                                  gtkKeyState(event->state));
412 #ifdef I18N
413         } else if (!inputCache_.empty())
414                 workAreaCJK_IMprocess(inputCache_.size(), inputCache_.data());
415 #endif
416         return true;
417 }
418
419
420 void GWorkArea::onClipboardGet(Gtk::SelectionData & /*selection_data*/,
421                                guint /*info*/)
422 {
423         selectionRequested();
424 }
425
426
427 void GWorkArea::onClipboardClear()
428 {
429 //      selectionLost();
430 }
431
432
433 void GWorkArea::haveSelection(bool toHave) const
434 {
435         if (toHave) {
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));
445         }
446 }
447
448
449 string const GWorkArea::getClipboard() const
450 {
451         Glib::RefPtr<Gtk::Clipboard> clipboard =
452                 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
453         return Glib::locale_from_utf8(clipboard->wait_for_text());
454 }
455
456
457 void GWorkArea::putClipboard(string const & str) const
458 {
459         Glib::RefPtr<Gtk::Clipboard> clipboard =
460                 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
461         clipboard->set_text(Glib::locale_to_utf8(str));
462 }