]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GWorkArea.C
Turn LyX into a singleton class. Kill the BufferView cache.
[lyx.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 using std::string;
25
26
27 ColorCache colorCache;
28
29
30 ColorCache::~ColorCache()
31 {
32         clear();
33 }
34
35
36 Gdk::Color * ColorCache::getColor(LColor_color clr)
37 {
38         MapIt it = cache_.find(clr);
39         return it == cache_.end() ? 0 : it->second;
40 }
41
42
43 XftColor * ColorCache::getXftColor(LColor_color clr)
44 {
45         MapIt2 it = cache2_.find(clr);
46         return it == cache2_.end() ? 0 : it->second;
47 }
48
49
50 void ColorCache::cacheColor(LColor_color clr, Gdk::Color * gclr)
51 {
52         cache_[clr] = gclr;
53 }
54
55
56 void ColorCache::cacheXftColor(LColor_color clr, XftColor * xclr)
57 {
58         cache2_[clr] = xclr;
59 }
60
61
62 void ColorCache::clear()
63 {
64         MapIt it = cache_.begin();
65         for (; it != cache_.end(); ++it)
66                 delete it->second;
67         cache_.clear();
68         MapIt2 it2 = cache2_.begin();
69         for (; it2 != cache2_.end(); ++it2)
70                 delete it2->second;
71         cache2_.clear();
72 }
73
74
75 XftColor * ColorHandler::getXftColor(LColor_color clr)
76 {
77         XftColor * xclr = colorCache.getXftColor(clr);
78         if (!xclr) {
79                 xclr = new XftColor;
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,
85                                   lcolor.getX11Name(clr).c_str(), xclr);
86                 colorCache.cacheXftColor(clr, xclr);
87         }
88         return xclr;
89 }
90
91
92 Gdk::Color * ColorHandler::getGdkColor(LColor_color clr)
93 {
94         Gdk::Color * gclr = colorCache.getColor(clr);
95         if (!gclr) {
96                 gclr = new Gdk::Color;
97                 gclr->parse(lcolor.getX11Name(clr));
98                 owner_.getColormap()->alloc_color(*gclr);
99                 colorCache.cacheColor(clr, gclr);
100         }
101         return gclr;
102 }
103
104
105 namespace
106 {
107
108
109 mouse_button::state gtkButtonState(unsigned int state)
110 {
111         mouse_button::state b = mouse_button::none;
112         if (state & GDK_BUTTON1_MASK)
113                 b = mouse_button::button1;
114         else if (state & GDK_BUTTON2_MASK)
115                 b = mouse_button::button2;
116         else if (state & GDK_BUTTON3_MASK)
117                 b = mouse_button::button3;
118         else if (state & GDK_BUTTON3_MASK)
119                 b = mouse_button::button3;
120         else if (state & GDK_BUTTON4_MASK)
121                 b = mouse_button::button4;
122         else if (state & GDK_BUTTON5_MASK)
123                 b = mouse_button::button5;
124         return b;
125 }
126
127
128 key_modifier::state gtkKeyState(guint state)
129 {
130         key_modifier::state k = key_modifier::none;
131         if (state & GDK_CONTROL_MASK)
132                 k |= key_modifier::ctrl;
133         if (state & GDK_SHIFT_MASK)
134                 k |= key_modifier::shift;
135         if (state & GDK_MOD1_MASK)
136                 k |= key_modifier::alt;
137         return k;
138 }
139
140
141 void inputCommitRelay(GtkIMContext */*imcontext*/, gchar * str, GWorkArea * area)
142 {
143         area->inputCommit(str);
144 }
145
146
147 }
148
149
150 GWorkArea::GWorkArea(int width, int height)
151         : workAreaPixmap_(0), painter_(*this), draw_(0), colorHandler_(*this)
152 {
153         workArea_.set_size_request(width, height);
154         workArea_.set_double_buffered(false);
155         workArea_.add_events(Gdk::STRUCTURE_MASK | Gdk::EXPOSURE_MASK |
156                              Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
157                              Gdk::KEY_PRESS_MASK | Gdk::BUTTON1_MOTION_MASK);
158         workArea_.signal_expose_event().connect(
159                 SigC::slot(*this, &GWorkArea::onExpose));
160         workArea_.signal_configure_event().connect(
161                 SigC::slot(*this, &GWorkArea::onConfigure));
162         workArea_.signal_button_press_event().connect(
163                 SigC::slot(*this, &GWorkArea::onButtonPress));
164         workArea_.signal_button_release_event().connect(
165                 SigC::slot(*this, &GWorkArea::onButtonRelease));
166         workArea_.signal_key_press_event().connect(
167                 SigC::slot(*this, &GWorkArea::onKeyPress));
168         workArea_.signal_motion_notify_event().connect(
169                 SigC::slot(*this, &GWorkArea::onMotionNotify));
170         workArea_.show();
171         vscrollbar_.get_adjustment()->signal_value_changed().connect(
172                 SigC::slot(*this, &GWorkArea::onScroll));
173         vscrollbar_.show();
174         hbox_.children().push_back(Gtk::Box_Helpers::Element(workArea_));
175         hbox_.children().push_back(
176                 Gtk::Box_Helpers::Element(vscrollbar_,Gtk::PACK_SHRINK));
177         hbox_.show();
178         GView::instance()->getVBox().children().push_back(
179                 Gtk::Box_Helpers::Element(hbox_));
180         workArea_.set_flags(workArea_.get_flags() | Gtk::CAN_DEFAULT |
181                             Gtk::CAN_FOCUS);
182         workArea_.grab_default();
183         GView::instance()->setGWorkArea(&workArea_);
184         imContext_ = GTK_IM_CONTEXT(gtk_im_multicontext_new());
185         g_signal_connect(G_OBJECT(imContext_), "commit",
186                          G_CALLBACK(&inputCommitRelay),
187                          this);
188 }
189
190
191 GWorkArea::~GWorkArea()
192 {
193         g_object_unref(imContext_);
194 }
195
196
197 Painter & GWorkArea::getPainter()
198 {
199         return painter_;
200 }
201
202
203 int GWorkArea::workWidth() const
204 {
205         return workArea_.get_width();
206 }
207
208
209 int GWorkArea::workHeight() const
210 {
211         return workArea_.get_height();
212 }
213
214
215 int GWorkArea::xpos() const
216 {
217         return 0;
218 }
219
220
221 int GWorkArea::ypos() const
222 {
223         return 0;
224 }
225
226
227 Glib::RefPtr<Gdk::Window> GWorkArea::getWindow()
228 {
229         return workArea_.get_window();
230 }
231
232
233 Display * GWorkArea::getDisplay() const
234 {
235         return GDK_WINDOW_XDISPLAY(
236                 const_cast<GdkWindow*>(workArea_.get_window()->gobj()));
237 }
238
239
240 Glib::RefPtr<Gdk::Pixmap> GWorkArea::getPixmap()
241 {
242         return workAreaPixmap_;
243 }
244
245
246 Glib::RefPtr<Gdk::GC> GWorkArea::getGC()
247 {
248         return workAreaGC_;
249 }
250
251
252 Glib::RefPtr<Gdk::Colormap> GWorkArea::getColormap()
253 {
254         return workArea_.get_colormap();
255 }
256
257
258 XftDraw * GWorkArea::getXftDraw()
259 {
260         return draw_;
261 }
262
263
264 ColorHandler & GWorkArea::getColorHandler()
265 {
266         return colorHandler_;
267 }
268
269
270 bool GWorkArea::onExpose(GdkEventExpose * event)
271 {
272         workArea_.get_window()->draw_drawable(
273                 workArea_.get_style()->get_black_gc(),
274                 workAreaPixmap_,
275                 event->area.x, event->area.y,
276                 event->area.x, event->area.y,
277                 event->area.width, event->area.height);
278         return true;
279 }
280
281
282 bool GWorkArea::onConfigure(GdkEventConfigure * /*event*/)
283 {
284         int x, y, width, height, depth;
285         workArea_.get_window()->get_geometry(x, y, width, height, depth);
286         if (draw_)
287                 XftDrawDestroy(draw_);
288         workAreaPixmap_ = Gdk::Pixmap::create(workArea_.get_window(),
289                                               width, height, depth);
290         Pixmap pixmap = GDK_PIXMAP_XID(workAreaPixmap_->gobj());
291         Colormap colormap = GDK_COLORMAP_XCOLORMAP(
292                 workArea_.get_colormap()->gobj());
293         Visual * visual = GDK_VISUAL_XVISUAL(
294                 workArea_.get_colormap()->get_visual()->gobj());
295         draw_ = XftDrawCreate(getDisplay(), pixmap,
296                               visual, colormap);
297         if (!workAreaGC_) {
298                 workAreaGC_ = Gdk::GC::create(workArea_.get_window());
299                 Gdk::Cursor cursor(Gdk::XTERM);
300                 workArea_.get_window()->set_cursor(cursor);
301                 gtk_im_context_set_client_window(
302                         imContext_, workArea_.get_window()->gobj());
303         }
304         workAreaResize();
305         return true;
306 }
307
308
309 void GWorkArea::setScrollbarParams(int height, int pos, int line_height)
310 {
311         Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
312         adjustment->set_lower(0);
313         int workAreaHeight = workHeight();
314         if (!height || height < workAreaHeight) {
315                 adjustment->set_upper(workAreaHeight);
316                 adjustment->set_page_size(workAreaHeight);
317                 adjustment->set_value(0);
318                 adjustment->changed();
319                 return;
320         }
321         adjustment->set_step_increment(line_height);
322         adjustment->set_page_increment(workAreaHeight - line_height);
323         adjustment->set_upper(height);
324         adjustment->set_page_size(workAreaHeight);
325         adjustment->set_value(pos);
326         adjustment->changed();
327 }
328
329
330 void GWorkArea::onScroll()
331 {
332         double val = vscrollbar_.get_adjustment()->get_value();
333         scrollDocView(static_cast<int>(val));
334 }
335
336
337 bool GWorkArea::onButtonPress(GdkEventButton * event)
338 {
339         kb_action ka = LFUN_MOUSE_PRESS;
340         switch (event->type) {
341         case GDK_BUTTON_PRESS:
342                 ka = LFUN_MOUSE_PRESS;
343                 break;
344         case GDK_2BUTTON_PRESS:
345                 ka = LFUN_MOUSE_DOUBLE;
346                 break;
347         case GDK_3BUTTON_PRESS:
348                 ka = LFUN_MOUSE_TRIPLE;
349                 break;
350         default:
351                 break;
352         }
353         dispatch(FuncRequest(ka,
354                              static_cast<int>(event->x),
355                              static_cast<int>(event->y),
356                              static_cast<mouse_button::state>(event->button)));
357         workArea_.grab_focus();
358         return true;
359 }
360
361
362 bool GWorkArea::onButtonRelease(GdkEventButton * event)
363 {
364         dispatch(FuncRequest(LFUN_MOUSE_RELEASE,
365                              static_cast<int>(event->x),
366                              static_cast<int>(event->y),
367                              static_cast<mouse_button::state>(event->button)));
368         return true;
369 }
370
371
372 bool GWorkArea::onMotionNotify(GdkEventMotion * event)
373 {
374         static guint32 timeBefore;
375         Gtk::Adjustment * adjustment = vscrollbar_.get_adjustment();
376         double step = adjustment->get_step_increment();
377         double value = adjustment->get_value();
378         if (event->x < 0)
379                 value -= step;
380         else if (event->x > workArea_.get_height())
381                 value += step;
382         if (value != adjustment->get_value()) {
383                 if (event->time - timeBefore > 200) {
384                         adjustment->set_value(value);
385                         adjustment->value_changed();
386                 }
387                 timeBefore = event->time;
388         }
389         dispatch(FuncRequest(LFUN_MOUSE_MOTION,
390                              static_cast<int>(event->x),
391                              static_cast<int>(event->y),
392                              gtkButtonState(event->state)));
393         return true;
394 }
395
396
397 void GWorkArea::inputCommit(gchar * str)
398 {
399         inputCache_ = Glib::locale_from_utf8(str);
400 }
401
402
403 bool GWorkArea::onKeyPress(GdkEventKey * event)
404 {
405 #ifdef I18N
406         inputCache_ = "";
407         bool inputGet = gtk_im_context_filter_keypress(imContext_, event);
408         // cope with ascii
409         if ((inputGet && inputCache_.size() == 1 && inputCache_[0] < 128) ||
410             !inputGet) {
411 #endif
412                 GLyXKeySym *glk = new GLyXKeySym(event->keyval);
413                 workAreaKeyPress(LyXKeySymPtr(glk),
414                                  gtkKeyState(event->state));
415 #ifdef I18N
416         } else if (!inputCache_.empty())
417                 workAreaCJK_IMprocess(inputCache_.size(), inputCache_.data());
418 #endif
419         return true;
420 }
421
422
423 void GWorkArea::onClipboardGet(Gtk::SelectionData & /*selection_data*/,
424                                guint /*info*/)
425 {
426         selectionRequested();
427 }
428
429
430 void GWorkArea::onClipboardClear()
431 {
432 //      selectionLost();
433 }
434
435
436 void GWorkArea::haveSelection(bool toHave) const
437 {
438         if (toHave) {
439                 Glib::RefPtr<Gtk::Clipboard> clipboard =
440                         Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
441                 std::vector<Gtk::TargetEntry> listTargets;
442                 listTargets.push_back(Gtk::TargetEntry("UTF8_STRING"));
443                 clipboard->set(listTargets,
444                                SigC::slot(const_cast<GWorkArea&>(*this),
445                                           &GWorkArea::onClipboardGet),
446                                SigC::slot(const_cast<GWorkArea&>(*this),
447                                           &GWorkArea::onClipboardClear));
448         }
449 }
450
451
452 string const GWorkArea::getClipboard() const
453 {
454         Glib::RefPtr<Gtk::Clipboard> clipboard =
455                 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
456         return Glib::locale_from_utf8(clipboard->wait_for_text());
457 }
458
459
460 void GWorkArea::putClipboard(string const & str) const
461 {
462         Glib::RefPtr<Gtk::Clipboard> clipboard =
463                 Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
464         clipboard->set_text(Glib::locale_to_utf8(str));
465 }