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 
wxGTKCairoDCImpl(wxDC * owner)23 wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner)
24     : base_type(owner)
25 {
26     m_width = 0;
27     m_height = 0;
28 }
29 
wxGTKCairoDCImpl(wxDC * owner,int)30 wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner, int)
31     : base_type(owner, 0)
32 {
33     m_width = 0;
34     m_height = 0;
35 }
36 
wxGTKCairoDCImpl(wxDC * owner,wxWindow * window)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 
Clear()48 void wxGTKCairoDCImpl::Clear()
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         double x1, y1, x2, y2;
58         cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
59         m_graphicContext->SetBrush(m_backgroundBrush);
60         m_graphicContext->SetPen(wxPen());
61         const wxCompositionMode mode = m_graphicContext->GetCompositionMode();
62         m_graphicContext->SetCompositionMode(wxCOMPOSITION_SOURCE);
63         m_graphicContext->DrawRectangle(x1, y1, x2 - x1, y2 - y1);
64         m_graphicContext->SetCompositionMode(mode);
65         m_graphicContext->SetPen(m_pen);
66         m_graphicContext->SetBrush(m_brush);
67     }
68 }
69 
DoDrawBitmap(const wxBitmap & bitmap,int x,int y,bool useMask)70 void wxGTKCairoDCImpl::DoDrawBitmap(const wxBitmap& bitmap, int x, int y, bool useMask)
71 {
72     wxCHECK_RET(IsOk(), "invalid DC");
73 
74     cairo_t* cr = NULL;
75     if (m_graphicContext)
76         cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
77     if (cr)
78     {
79         cairo_save(cr);
80         bitmap.Draw(cr, x, y, useMask, &m_textForegroundColour, &m_textBackgroundColour);
81         cairo_restore(cr);
82     }
83 }
84 
DoDrawIcon(const wxIcon & icon,int x,int y)85 void wxGTKCairoDCImpl::DoDrawIcon(const wxIcon& icon, int x, int y)
86 {
87     DoDrawBitmap(icon, x, y, true);
88 }
89 
90 #if wxUSE_IMAGE
91 bool wxDoFloodFill(wxDC* dc, int x, int y, const wxColour& col, wxFloodFillStyle style);
92 
DoFloodFill(int x,int y,const wxColour & col,wxFloodFillStyle style)93 bool wxGTKCairoDCImpl::DoFloodFill(int x, int y, const wxColour& col, wxFloodFillStyle style)
94 {
95     return wxDoFloodFill(GetOwner(), x, y, col, style);
96 }
97 #endif
98 
DoGetAsBitmap(const wxRect *) const99 wxBitmap wxGTKCairoDCImpl::DoGetAsBitmap(const wxRect* /*subrect*/) const
100 {
101     wxFAIL_MSG("DoGetAsBitmap not implemented");
102     return wxBitmap();
103 }
104 
DoGetPixel(int x,int y,wxColour * col) const105 bool wxGTKCairoDCImpl::DoGetPixel(int x, int y, wxColour* col) const
106 {
107     if (col)
108     {
109         cairo_t* cr = NULL;
110         if (m_graphicContext)
111             cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
112         if (cr)
113         {
114             cairo_surface_t* surface = cairo_get_target(cr);
115             x = LogicalToDeviceX(x);
116             y = LogicalToDeviceY(y);
117             GdkPixbuf* pixbuf = gdk_pixbuf_get_from_surface(surface, x, y, 1, 1);
118             if (pixbuf)
119             {
120                 const guchar* src = gdk_pixbuf_get_pixels(pixbuf);
121                 col->Set(src[0], src[1], src[2]);
122                 g_object_unref(pixbuf);
123                 return true;
124             }
125             *col = wxColour();
126         }
127     }
128     return false;
129 }
130 
DoGetSize(int * width,int * height) const131 void wxGTKCairoDCImpl::DoGetSize(int* width, int* height) const
132 {
133     if (width)
134         *width = m_width;
135     if (height)
136         *height = m_height;
137 }
138 
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)139 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)
140 {
141     wxCHECK_MSG(IsOk(), false, "invalid DC");
142     wxCHECK_MSG(source && source->IsOk(), false, "invalid source DC");
143 
144     cairo_t* cr = NULL;
145     if (m_graphicContext)
146         cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
147     cairo_t* cr_src = NULL;
148     wxGraphicsContext* gc_src = source->GetGraphicsContext();
149     if (gc_src)
150         cr_src = static_cast<cairo_t*>(gc_src->GetNativeContext());
151 
152     if (cr == NULL || cr_src == NULL)
153         return false;
154 
155     const int xsrc_dev = source->LogicalToDeviceX(xsrc);
156     const int ysrc_dev = source->LogicalToDeviceY(ysrc);
157 
158     cairo_surface_t* surface = cairo_get_target(cr_src);
159     cairo_surface_flush(surface);
160     cairo_save(cr);
161     cairo_translate(cr, xdest, ydest);
162     cairo_rectangle(cr, 0, 0, dstWidth, dstHeight);
163     double sx, sy;
164     source->GetUserScale(&sx, &sy);
165     cairo_scale(cr, dstWidth / (sx * srcWidth), dstHeight / (sy * srcHeight));
166     cairo_set_source_surface(cr, surface, -xsrc_dev, -ysrc_dev);
167     const wxRasterOperationMode rop_save = m_logicalFunction;
168     SetLogicalFunction(rop);
169     cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
170     cairo_surface_t* maskSurf = NULL;
171     if (useMask)
172     {
173         const wxBitmap& bitmap = source->GetImpl()->GetSelectedBitmap();
174         if (bitmap.IsOk())
175         {
176             wxMask* mask = bitmap.GetMask();
177             if (mask)
178                 maskSurf = *mask;
179         }
180     }
181     if (maskSurf)
182     {
183         int xsrcMask_dev = xsrc_dev;
184         int ysrcMask_dev = ysrc_dev;
185         if (xsrcMask != -1)
186             xsrcMask_dev = source->LogicalToDeviceX(xsrcMask);
187         if (ysrcMask != -1)
188             ysrcMask_dev = source->LogicalToDeviceY(ysrcMask);
189         cairo_clip(cr);
190         cairo_mask_surface(cr, maskSurf, -xsrcMask_dev, -ysrcMask_dev);
191     }
192     else
193     {
194         cairo_fill(cr);
195     }
196     cairo_restore(cr);
197     m_logicalFunction = rop_save;
198     return true;
199 }
200 
GetCairoContext() const201 void* wxGTKCairoDCImpl::GetCairoContext() const
202 {
203     cairo_t* cr = NULL;
204     if (m_graphicContext)
205         cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
206     return cr;
207 }
208 //-----------------------------------------------------------------------------
209 
wxWindowDCImpl(wxWindowDC * owner,wxWindow * window)210 wxWindowDCImpl::wxWindowDCImpl(wxWindowDC* owner, wxWindow* window)
211     : base_type(owner, window)
212 {
213     GtkWidget* widget = window->m_wxwindow;
214     if (widget == NULL)
215         widget = window->m_widget;
216     GdkWindow* gdkWindow = NULL;
217     if (widget)
218     {
219         gdkWindow = gtk_widget_get_window(widget);
220         m_ok = true;
221     }
222     if (gdkWindow)
223     {
224         cairo_t* cr = gdk_cairo_create(gdkWindow);
225         wxGraphicsContext* gc = wxGraphicsContext::CreateFromNative(cr);
226         gc->EnableOffset(true);
227         SetGraphicsContext(gc);
228         GtkAllocation a;
229         gtk_widget_get_allocation(widget, &a);
230         int x, y;
231         if (gtk_widget_get_has_window(widget))
232         {
233             m_width = gdk_window_get_width(gdkWindow);
234             m_height = gdk_window_get_height(gdkWindow);
235             x = m_width - a.width;
236             y = m_height - a.height;
237         }
238         else
239         {
240             m_width = a.width;
241             m_height = a.height;
242             x = a.x;
243             y = a.y;
244             cairo_rectangle(cr, a.x, a.y, a.width, a.height);
245             cairo_clip(cr);
246         }
247         if (x || y)
248             SetDeviceLocalOrigin(x, y);
249     }
250     else
251         SetGraphicsContext(wxGraphicsContext::Create());
252 }
253 //-----------------------------------------------------------------------------
254 
wxClientDCImpl(wxClientDC * owner,wxWindow * window)255 wxClientDCImpl::wxClientDCImpl(wxClientDC* owner, wxWindow* window)
256     : base_type(owner, window)
257 {
258     GtkWidget* widget = window->m_wxwindow;
259     if (widget == NULL)
260         widget = window->m_widget;
261     GdkWindow* gdkWindow = NULL;
262     if (widget)
263     {
264         gdkWindow = gtk_widget_get_window(widget);
265         m_ok = true;
266     }
267     if (gdkWindow)
268     {
269         cairo_t* cr = gdk_cairo_create(gdkWindow);
270         wxGraphicsContext* gc = wxGraphicsContext::CreateFromNative(cr);
271         gc->EnableOffset(true);
272         SetGraphicsContext(gc);
273         if (gtk_widget_get_has_window(widget))
274         {
275             m_width = gdk_window_get_width(gdkWindow);
276             m_height = gdk_window_get_height(gdkWindow);
277         }
278         else
279         {
280             GtkAllocation a;
281             gtk_widget_get_allocation(widget, &a);
282             m_width = a.width;
283             m_height = a.height;
284             cairo_rectangle(cr, a.x, a.y, a.width, a.height);
285             cairo_clip(cr);
286             SetDeviceLocalOrigin(a.x, a.y);
287         }
288     }
289     else
290         SetGraphicsContext(wxGraphicsContext::Create());
291 }
292 //-----------------------------------------------------------------------------
293 
wxPaintDCImpl(wxPaintDC * owner,wxWindow * window)294 wxPaintDCImpl::wxPaintDCImpl(wxPaintDC* owner, wxWindow* window)
295     : base_type(owner, window)
296 {
297     cairo_t* cr = window->GTKPaintContext();
298     wxCHECK_RET(cr, "using wxPaintDC without being in a native paint event");
299     GdkWindow* gdkWindow = gtk_widget_get_window(window->m_wxwindow);
300     m_width = gdk_window_get_width(gdkWindow);
301     m_height = gdk_window_get_height(gdkWindow);
302     cairo_reference(cr);
303     wxGraphicsContext* gc = wxGraphicsContext::CreateFromNative(cr);
304     gc->EnableOffset(true);
305     SetGraphicsContext(gc);
306 }
307 //-----------------------------------------------------------------------------
308 
wxScreenDCImpl(wxScreenDC * owner)309 wxScreenDCImpl::wxScreenDCImpl(wxScreenDC* owner)
310     : base_type(owner, 0)
311 {
312     GdkWindow* window = gdk_get_default_root_window();
313     m_width = gdk_window_get_width(window);
314     m_height = gdk_window_get_height(window);
315     cairo_t* cr = gdk_cairo_create(window);
316     wxGraphicsContext* gc = wxGraphicsContext::CreateFromNative(cr);
317     gc->EnableOffset(true);
318     SetGraphicsContext(gc);
319 }
320 //-----------------------------------------------------------------------------
321 
wxMemoryDCImpl(wxMemoryDC * owner)322 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner)
323     : base_type(owner)
324 {
325     m_ok = false;
326 }
327 
wxMemoryDCImpl(wxMemoryDC * owner,wxBitmap & bitmap)328 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxBitmap& bitmap)
329     : base_type(owner, 0)
330     , m_bitmap(bitmap)
331 {
332     Setup();
333 }
334 
wxMemoryDCImpl(wxMemoryDC * owner,wxDC *)335 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxDC*)
336     : base_type(owner)
337 {
338     m_ok = false;
339 }
340 
DoGetAsBitmap(const wxRect * subrect) const341 wxBitmap wxMemoryDCImpl::DoGetAsBitmap(const wxRect* subrect) const
342 {
343     return subrect ? m_bitmap.GetSubBitmap(*subrect) : m_bitmap;
344 }
345 
DoSelect(const wxBitmap & bitmap)346 void wxMemoryDCImpl::DoSelect(const wxBitmap& bitmap)
347 {
348     m_bitmap = bitmap;
349     Setup();
350 }
351 
GetSelectedBitmap() const352 const wxBitmap& wxMemoryDCImpl::GetSelectedBitmap() const
353 {
354     return m_bitmap;
355 }
356 
GetSelectedBitmap()357 wxBitmap& wxMemoryDCImpl::GetSelectedBitmap()
358 {
359     return m_bitmap;
360 }
361 
Setup()362 void wxMemoryDCImpl::Setup()
363 {
364     wxGraphicsContext* gc = NULL;
365     m_ok = m_bitmap.IsOk();
366     if (m_ok)
367     {
368         m_width = m_bitmap.GetWidth();
369         m_height = m_bitmap.GetHeight();
370         cairo_t* cr = m_bitmap.CairoCreate();
371         gc = wxGraphicsContext::CreateFromNative(cr);
372         gc->EnableOffset(true);
373     }
374     SetGraphicsContext(gc);
375 }
376 //-----------------------------------------------------------------------------
377 
wxGTKCairoDC(cairo_t * cr)378 wxGTKCairoDC::wxGTKCairoDC(cairo_t* cr)
379     : base_type(new wxGTKCairoDCImpl(this, 0))
380 {
381     cairo_reference(cr);
382     wxGraphicsContext* gc = wxGraphicsContext::CreateFromNative(cr);
383     gc->EnableOffset(true);
384     SetGraphicsContext(gc);
385 }
386 
387 #else
388 
389 #include "wx/gtk/dc.h"
390 
391 //-----------------------------------------------------------------------------
392 // wxGTKDCImpl
393 //-----------------------------------------------------------------------------
394 
IMPLEMENT_ABSTRACT_CLASS(wxGTKDCImpl,wxDCImpl)395 IMPLEMENT_ABSTRACT_CLASS(wxGTKDCImpl, wxDCImpl)
396 
397 wxGTKDCImpl::wxGTKDCImpl( wxDC *owner )
398    : wxDCImpl( owner )
399 {
400     m_ok = FALSE;
401 
402     m_pen = *wxBLACK_PEN;
403     m_font = *wxNORMAL_FONT;
404     m_brush = *wxWHITE_BRUSH;
405 }
406 
~wxGTKDCImpl()407 wxGTKDCImpl::~wxGTKDCImpl()
408 {
409 }
410 
DoSetClippingRegion(wxCoord x,wxCoord y,wxCoord width,wxCoord height)411 void wxGTKDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
412 {
413     m_clipping = TRUE;
414     m_clipX1 = x;
415     m_clipY1 = y;
416     m_clipX2 = x + width;
417     m_clipY2 = y + height;
418 }
419 
420 // ---------------------------------------------------------------------------
421 // get DC capabilities
422 // ---------------------------------------------------------------------------
423 
DoGetSizeMM(int * width,int * height) const424 void wxGTKDCImpl::DoGetSizeMM( int* width, int* height ) const
425 {
426     int w = 0;
427     int h = 0;
428     GetOwner()->GetSize( &w, &h );
429     if (width) *width = int( double(w) / (m_userScaleX*m_mm_to_pix_x) );
430     if (height) *height = int( double(h) / (m_userScaleY*m_mm_to_pix_y) );
431 }
432 
433 // Resolution in pixels per logical inch
GetPPI() const434 wxSize wxGTKDCImpl::GetPPI() const
435 {
436     // TODO (should probably be pure virtual)
437     return wxSize(0, 0);
438 }
439 #endif
440