support for GTK3
[wxWidgets.git] / src / gtk / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dc.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __WXGTK3__
14
15 #include "wx/window.h"
16 #include "wx/dcclient.h"
17 #include "wx/dcmemory.h"
18 #include "wx/dcscreen.h"
19 #include "wx/icon.h"
20 #include "wx/gtk/dc.h"
21
22 #include <gtk/gtk.h>
23
24 wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner)
25 : base_type(owner)
26 {
27 m_width = 0;
28 m_height = 0;
29 }
30
31 wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner, wxWindow* window)
32 : base_type(owner)
33 {
34 m_window = window;
35 m_font = window->GetFont();
36 m_textForegroundColour = window->GetForegroundColour();
37 m_textBackgroundColour = window->GetBackgroundColour();
38 m_width = 0;
39 m_height = 0;
40 }
41
42 void wxGTKCairoDCImpl::DoDrawBitmap(const wxBitmap& bitmap, int x, int y, bool useMask)
43 {
44 wxCHECK_RET(IsOk(), "invalid DC");
45
46 cairo_t* cr = NULL;
47 if (m_graphicContext)
48 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
49 if (cr)
50 {
51 cairo_save(cr);
52 bitmap.Draw(cr, x, y, useMask, &m_textForegroundColour, &m_textBackgroundColour);
53 cairo_restore(cr);
54 }
55 }
56
57 void wxGTKCairoDCImpl::DoDrawIcon(const wxIcon& icon, int x, int y)
58 {
59 DoDrawBitmap(icon, x, y, true);
60 }
61
62 #if wxUSE_IMAGE
63 bool wxDoFloodFill(wxDC* dc, int x, int y, const wxColour& col, wxFloodFillStyle style);
64
65 bool wxGTKCairoDCImpl::DoFloodFill(int x, int y, const wxColour& col, wxFloodFillStyle style)
66 {
67 return wxDoFloodFill(GetOwner(), x, y, col, style);
68 }
69 #endif
70
71 wxBitmap wxGTKCairoDCImpl::DoGetAsBitmap(const wxRect* /*subrect*/) const
72 {
73 wxFAIL_MSG("DoGetAsBitmap not implemented");
74 return wxBitmap();
75 }
76
77 bool wxGTKCairoDCImpl::DoGetPixel(int x, int y, wxColour* col) const
78 {
79 if (col)
80 {
81 cairo_t* cr = NULL;
82 if (m_graphicContext)
83 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
84 if (cr)
85 {
86 cairo_surface_t* surface = cairo_get_target(cr);
87 x = LogicalToDeviceX(x);
88 y = LogicalToDeviceY(y);
89 GdkPixbuf* pixbuf = gdk_pixbuf_get_from_surface(surface, x, y, 1, 1);
90 if (pixbuf)
91 {
92 const guchar* src = gdk_pixbuf_get_pixels(pixbuf);
93 col->Set(src[0], src[1], src[2]);
94 g_object_unref(pixbuf);
95 return true;
96 }
97 *col = wxColour();
98 }
99 }
100 return false;
101 }
102
103 void wxGTKCairoDCImpl::DoGetSize(int* width, int* height) const
104 {
105 if (width)
106 *width = m_width;
107 if (height)
108 *height = m_height;
109 }
110
111 bool wxGTKCairoDCImpl::DoStretchBlit(int xdest, int ydest, int dstWidth, int dstHeight, wxDC* source, int xsrc, int ysrc, int srcWidth, int srcHeight, wxRasterOperationMode rop, bool useMask, int xsrcMask, int ysrcMask)
112 {
113 wxCHECK_MSG(IsOk(), false, "invalid DC");
114 wxCHECK_MSG(source && source->IsOk(), false, "invalid source DC");
115
116 cairo_t* cr = NULL;
117 if (m_graphicContext)
118 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
119 cairo_t* cr_src = NULL;
120 wxGraphicsContext* gc_src = source->GetGraphicsContext();
121 if (gc_src)
122 cr_src = static_cast<cairo_t*>(gc_src->GetNativeContext());
123
124 if (cr == NULL || cr_src == NULL)
125 return false;
126
127 const int xsrc_dev = source->LogicalToDeviceX(xsrc);
128 const int ysrc_dev = source->LogicalToDeviceY(ysrc);
129
130 cairo_surface_t* surface = cairo_get_target(cr_src);
131 cairo_surface_flush(surface);
132 cairo_save(cr);
133 cairo_translate(cr, xdest, ydest);
134 cairo_rectangle(cr, 0, 0, dstWidth, dstHeight);
135 double sx, sy;
136 source->GetUserScale(&sx, &sy);
137 cairo_scale(cr, dstWidth / (sx * srcWidth), dstHeight / (sy * srcHeight));
138 cairo_set_source_surface(cr, surface, -xsrc_dev, -ysrc_dev);
139 const wxRasterOperationMode rop_save = m_logicalFunction;
140 SetLogicalFunction(rop);
141 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
142 cairo_surface_t* maskSurf = NULL;
143 if (useMask)
144 {
145 const wxBitmap& bitmap = source->GetImpl()->GetSelectedBitmap();
146 if (bitmap.IsOk())
147 {
148 wxMask* mask = bitmap.GetMask();
149 if (mask)
150 maskSurf = mask->GetBitmap();
151 }
152 }
153 if (maskSurf)
154 {
155 int xsrcMask_dev = xsrc_dev;
156 int ysrcMask_dev = ysrc_dev;
157 if (xsrcMask != -1)
158 xsrcMask_dev = source->LogicalToDeviceX(xsrcMask);
159 if (ysrcMask != -1)
160 ysrcMask_dev = source->LogicalToDeviceY(ysrcMask);
161 cairo_clip(cr);
162 cairo_mask_surface(cr, maskSurf, -xsrcMask_dev, -ysrcMask_dev);
163 }
164 else
165 {
166 cairo_fill(cr);
167 }
168 cairo_restore(cr);
169 m_logicalFunction = rop_save;
170 return true;
171 }
172
173 void* wxGTKCairoDCImpl::GetCairoContext() const
174 {
175 cairo_t* cr = NULL;
176 if (m_graphicContext)
177 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
178 if (cr)
179 cairo_reference(cr);
180 return cr;
181 }
182 //-----------------------------------------------------------------------------
183
184 wxWindowDCImpl::wxWindowDCImpl(wxWindowDC* owner, wxWindow* window)
185 : base_type(owner, window)
186 {
187 GtkWidget* widget = window->m_wxwindow;
188 if (widget == NULL)
189 widget = window->m_widget;
190 GdkWindow* gdkWindow = NULL;
191 if (widget)
192 {
193 gdkWindow = gtk_widget_get_window(widget);
194 m_ok = true;
195 }
196 if (gdkWindow)
197 {
198 cairo_t* cr = gdk_cairo_create(gdkWindow);
199 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
200 GtkAllocation a;
201 gtk_widget_get_allocation(widget, &a);
202 int x, y;
203 if (gtk_widget_get_has_window(widget))
204 {
205 m_width = gdk_window_get_width(gdkWindow);
206 m_height = gdk_window_get_height(gdkWindow);
207 x = m_width - a.width;
208 y = m_height - a.height;
209 }
210 else
211 {
212 m_width = a.width;
213 m_height = a.height;
214 x = a.x;
215 y = a.y;
216 cairo_rectangle(cr, a.x, a.y, a.width, a.height);
217 cairo_clip(cr);
218 }
219 if (x || y)
220 SetDeviceLocalOrigin(x, y);
221 }
222 }
223 //-----------------------------------------------------------------------------
224
225 wxClientDCImpl::wxClientDCImpl(wxClientDC* owner, wxWindow* window)
226 : base_type(owner, window)
227 {
228 GtkWidget* widget = window->m_wxwindow;
229 if (widget == NULL)
230 widget = window->m_widget;
231 GdkWindow* gdkWindow = NULL;
232 if (widget)
233 {
234 gdkWindow = gtk_widget_get_window(widget);
235 m_ok = true;
236 }
237 if (gdkWindow)
238 {
239 cairo_t* cr = gdk_cairo_create(gdkWindow);
240 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
241 if (gtk_widget_get_has_window(widget))
242 {
243 m_width = gdk_window_get_width(gdkWindow);
244 m_height = gdk_window_get_height(gdkWindow);
245 }
246 else
247 {
248 GtkAllocation a;
249 gtk_widget_get_allocation(widget, &a);
250 m_width = a.width;
251 m_height = a.height;
252 cairo_rectangle(cr, a.x, a.y, a.width, a.height);
253 cairo_clip(cr);
254 SetDeviceLocalOrigin(a.x, a.y);
255 }
256 }
257 else
258 {
259 // create something that can be used for measuring, but not drawing
260 cairo_t* cr = gdk_cairo_create(gdk_get_default_root_window());
261 cairo_rectangle(cr, 0, 0, 0, 0);
262 cairo_clip(cr);
263 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
264 }
265 }
266 //-----------------------------------------------------------------------------
267
268 wxPaintDCImpl::wxPaintDCImpl(wxPaintDC* owner, wxWindow* window)
269 : base_type(owner, window)
270 {
271 cairo_t* cr = window->GTKPaintContext();
272 wxCHECK_RET(cr, "using wxPaintDC without being in a native paint event");
273 GdkWindow* gdkWindow = gtk_widget_get_window(window->m_wxwindow);
274 m_width = gdk_window_get_width(gdkWindow);
275 m_height = gdk_window_get_height(gdkWindow);
276 cairo_reference(cr);
277 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
278 }
279 //-----------------------------------------------------------------------------
280
281 wxScreenDCImpl::wxScreenDCImpl(wxScreenDC* owner)
282 : base_type(owner)
283 {
284 GdkWindow* window = gdk_get_default_root_window();
285 m_width = gdk_window_get_width(window);
286 m_height = gdk_window_get_height(window);
287 cairo_t* cr = gdk_cairo_create(window);
288 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
289 }
290 //-----------------------------------------------------------------------------
291
292 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner)
293 : base_type(owner)
294 {
295 m_ok = false;
296 }
297
298 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxBitmap& bitmap)
299 : base_type(owner)
300 , m_bitmap(bitmap)
301 {
302 Setup();
303 }
304
305 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxDC*)
306 : base_type(owner)
307 {
308 m_ok = false;
309 }
310
311 wxBitmap wxMemoryDCImpl::DoGetAsBitmap(const wxRect* subrect) const
312 {
313 return subrect ? m_bitmap.GetSubBitmap(*subrect) : m_bitmap;
314 }
315
316 void wxMemoryDCImpl::DoSelect(const wxBitmap& bitmap)
317 {
318 m_bitmap = bitmap;
319 Setup();
320 }
321
322 const wxBitmap& wxMemoryDCImpl::GetSelectedBitmap() const
323 {
324 return m_bitmap;
325 }
326
327 wxBitmap& wxMemoryDCImpl::GetSelectedBitmap()
328 {
329 return m_bitmap;
330 }
331
332 void wxMemoryDCImpl::Setup()
333 {
334 wxGraphicsContext* gc = NULL;
335 m_ok = m_bitmap.IsOk();
336 if (m_ok)
337 {
338 m_width = m_bitmap.GetWidth();
339 m_height = m_bitmap.GetHeight();
340 cairo_t* cr = m_bitmap.CairoCreate();
341 gc = wxGraphicsContext::CreateFromNative(cr);
342 }
343 SetGraphicsContext(gc);
344 }
345 //-----------------------------------------------------------------------------
346
347 wxGTKCairoDC::wxGTKCairoDC(cairo_t* cr)
348 : base_type(new wxGTKCairoDCImpl(this))
349 {
350 cairo_reference(cr);
351 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
352 }
353
354 #else
355
356 #include "wx/gtk/dc.h"
357
358 //-----------------------------------------------------------------------------
359 // wxGTKDCImpl
360 //-----------------------------------------------------------------------------
361
362 IMPLEMENT_ABSTRACT_CLASS(wxGTKDCImpl, wxDCImpl)
363
364 wxGTKDCImpl::wxGTKDCImpl( wxDC *owner )
365 : wxDCImpl( owner )
366 {
367 m_ok = FALSE;
368
369 m_pen = *wxBLACK_PEN;
370 m_font = *wxNORMAL_FONT;
371 m_brush = *wxWHITE_BRUSH;
372 }
373
374 wxGTKDCImpl::~wxGTKDCImpl()
375 {
376 }
377
378 void wxGTKDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
379 {
380 m_clipping = TRUE;
381 m_clipX1 = x;
382 m_clipY1 = y;
383 m_clipX2 = x + width;
384 m_clipY2 = y + height;
385 }
386
387 // ---------------------------------------------------------------------------
388 // get DC capabilities
389 // ---------------------------------------------------------------------------
390
391 void wxGTKDCImpl::DoGetSizeMM( int* width, int* height ) const
392 {
393 int w = 0;
394 int h = 0;
395 GetOwner()->GetSize( &w, &h );
396 if (width) *width = int( double(w) / (m_userScaleX*m_mm_to_pix_x) );
397 if (height) *height = int( double(h) / (m_userScaleY*m_mm_to_pix_y) );
398 }
399
400 // Resolution in pixels per logical inch
401 wxSize wxGTKDCImpl::GetPPI() const
402 {
403 // TODO (should probably be pure virtual)
404 return wxSize(0, 0);
405 }
406 #endif