]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dc.cpp
Add virtual ~wxAnyScrollHelperBase() to fix compiler warning.
[wxWidgets.git] / src / gtk / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dc.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #ifdef __WXGTK3__
13
14 #include "wx/window.h"
15 #include "wx/dcclient.h"
16 #include "wx/dcmemory.h"
17 #include "wx/dcscreen.h"
18 #include "wx/icon.h"
19 #include "wx/gtk/dc.h"
20
21 #include <gtk/gtk.h>
22
23 wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner)
24 : base_type(owner)
25 {
26 m_width = 0;
27 m_height = 0;
28 }
29
30 wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner, int)
31 : base_type(owner, 0)
32 {
33 m_width = 0;
34 m_height = 0;
35 }
36
37 wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner, wxWindow* window)
38 : base_type(owner, 0)
39 {
40 m_window = window;
41 m_font = window->GetFont();
42 m_textForegroundColour = window->GetForegroundColour();
43 m_textBackgroundColour = window->GetBackgroundColour();
44 m_width = 0;
45 m_height = 0;
46 }
47
48 void wxGTKCairoDCImpl::DoDrawBitmap(const wxBitmap& bitmap, int x, int y, bool useMask)
49 {
50 wxCHECK_RET(IsOk(), "invalid DC");
51
52 cairo_t* cr = NULL;
53 if (m_graphicContext)
54 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
55 if (cr)
56 {
57 cairo_save(cr);
58 bitmap.Draw(cr, x, y, useMask, &m_textForegroundColour, &m_textBackgroundColour);
59 cairo_restore(cr);
60 }
61 }
62
63 void wxGTKCairoDCImpl::DoDrawIcon(const wxIcon& icon, int x, int y)
64 {
65 DoDrawBitmap(icon, x, y, true);
66 }
67
68 #if wxUSE_IMAGE
69 bool wxDoFloodFill(wxDC* dc, int x, int y, const wxColour& col, wxFloodFillStyle style);
70
71 bool wxGTKCairoDCImpl::DoFloodFill(int x, int y, const wxColour& col, wxFloodFillStyle style)
72 {
73 return wxDoFloodFill(GetOwner(), x, y, col, style);
74 }
75 #endif
76
77 wxBitmap wxGTKCairoDCImpl::DoGetAsBitmap(const wxRect* /*subrect*/) const
78 {
79 wxFAIL_MSG("DoGetAsBitmap not implemented");
80 return wxBitmap();
81 }
82
83 bool wxGTKCairoDCImpl::DoGetPixel(int x, int y, wxColour* col) const
84 {
85 if (col)
86 {
87 cairo_t* cr = NULL;
88 if (m_graphicContext)
89 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
90 if (cr)
91 {
92 cairo_surface_t* surface = cairo_get_target(cr);
93 x = LogicalToDeviceX(x);
94 y = LogicalToDeviceY(y);
95 GdkPixbuf* pixbuf = gdk_pixbuf_get_from_surface(surface, x, y, 1, 1);
96 if (pixbuf)
97 {
98 const guchar* src = gdk_pixbuf_get_pixels(pixbuf);
99 col->Set(src[0], src[1], src[2]);
100 g_object_unref(pixbuf);
101 return true;
102 }
103 *col = wxColour();
104 }
105 }
106 return false;
107 }
108
109 void wxGTKCairoDCImpl::DoGetSize(int* width, int* height) const
110 {
111 if (width)
112 *width = m_width;
113 if (height)
114 *height = m_height;
115 }
116
117 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)
118 {
119 wxCHECK_MSG(IsOk(), false, "invalid DC");
120 wxCHECK_MSG(source && source->IsOk(), false, "invalid source DC");
121
122 cairo_t* cr = NULL;
123 if (m_graphicContext)
124 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
125 cairo_t* cr_src = NULL;
126 wxGraphicsContext* gc_src = source->GetGraphicsContext();
127 if (gc_src)
128 cr_src = static_cast<cairo_t*>(gc_src->GetNativeContext());
129
130 if (cr == NULL || cr_src == NULL)
131 return false;
132
133 const int xsrc_dev = source->LogicalToDeviceX(xsrc);
134 const int ysrc_dev = source->LogicalToDeviceY(ysrc);
135
136 cairo_surface_t* surface = cairo_get_target(cr_src);
137 cairo_surface_flush(surface);
138 cairo_save(cr);
139 cairo_translate(cr, xdest, ydest);
140 cairo_rectangle(cr, 0, 0, dstWidth, dstHeight);
141 double sx, sy;
142 source->GetUserScale(&sx, &sy);
143 cairo_scale(cr, dstWidth / (sx * srcWidth), dstHeight / (sy * srcHeight));
144 cairo_set_source_surface(cr, surface, -xsrc_dev, -ysrc_dev);
145 const wxRasterOperationMode rop_save = m_logicalFunction;
146 SetLogicalFunction(rop);
147 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
148 cairo_surface_t* maskSurf = NULL;
149 if (useMask)
150 {
151 const wxBitmap& bitmap = source->GetImpl()->GetSelectedBitmap();
152 if (bitmap.IsOk())
153 {
154 wxMask* mask = bitmap.GetMask();
155 if (mask)
156 maskSurf = *mask;
157 }
158 }
159 if (maskSurf)
160 {
161 int xsrcMask_dev = xsrc_dev;
162 int ysrcMask_dev = ysrc_dev;
163 if (xsrcMask != -1)
164 xsrcMask_dev = source->LogicalToDeviceX(xsrcMask);
165 if (ysrcMask != -1)
166 ysrcMask_dev = source->LogicalToDeviceY(ysrcMask);
167 cairo_clip(cr);
168 cairo_mask_surface(cr, maskSurf, -xsrcMask_dev, -ysrcMask_dev);
169 }
170 else
171 {
172 cairo_fill(cr);
173 }
174 cairo_restore(cr);
175 m_logicalFunction = rop_save;
176 return true;
177 }
178
179 void* wxGTKCairoDCImpl::GetCairoContext() const
180 {
181 cairo_t* cr = NULL;
182 if (m_graphicContext)
183 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
184 return cr;
185 }
186 //-----------------------------------------------------------------------------
187
188 wxWindowDCImpl::wxWindowDCImpl(wxWindowDC* owner, wxWindow* window)
189 : base_type(owner, window)
190 {
191 GtkWidget* widget = window->m_wxwindow;
192 if (widget == NULL)
193 widget = window->m_widget;
194 GdkWindow* gdkWindow = NULL;
195 if (widget)
196 {
197 gdkWindow = gtk_widget_get_window(widget);
198 m_ok = true;
199 }
200 if (gdkWindow)
201 {
202 cairo_t* cr = gdk_cairo_create(gdkWindow);
203 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
204 GtkAllocation a;
205 gtk_widget_get_allocation(widget, &a);
206 int x, y;
207 if (gtk_widget_get_has_window(widget))
208 {
209 m_width = gdk_window_get_width(gdkWindow);
210 m_height = gdk_window_get_height(gdkWindow);
211 x = m_width - a.width;
212 y = m_height - a.height;
213 }
214 else
215 {
216 m_width = a.width;
217 m_height = a.height;
218 x = a.x;
219 y = a.y;
220 cairo_rectangle(cr, a.x, a.y, a.width, a.height);
221 cairo_clip(cr);
222 }
223 if (x || y)
224 SetDeviceLocalOrigin(x, y);
225 }
226 else
227 SetGraphicsContext(wxGraphicsContext::Create());
228 }
229 //-----------------------------------------------------------------------------
230
231 wxClientDCImpl::wxClientDCImpl(wxClientDC* owner, wxWindow* window)
232 : base_type(owner, window)
233 {
234 GtkWidget* widget = window->m_wxwindow;
235 if (widget == NULL)
236 widget = window->m_widget;
237 GdkWindow* gdkWindow = NULL;
238 if (widget)
239 {
240 gdkWindow = gtk_widget_get_window(widget);
241 m_ok = true;
242 }
243 if (gdkWindow)
244 {
245 cairo_t* cr = gdk_cairo_create(gdkWindow);
246 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
247 if (gtk_widget_get_has_window(widget))
248 {
249 m_width = gdk_window_get_width(gdkWindow);
250 m_height = gdk_window_get_height(gdkWindow);
251 }
252 else
253 {
254 GtkAllocation a;
255 gtk_widget_get_allocation(widget, &a);
256 m_width = a.width;
257 m_height = a.height;
258 cairo_rectangle(cr, a.x, a.y, a.width, a.height);
259 cairo_clip(cr);
260 SetDeviceLocalOrigin(a.x, a.y);
261 }
262 }
263 else
264 SetGraphicsContext(wxGraphicsContext::Create());
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, 0)
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, 0)
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, 0))
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