]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dc.cpp
Move code that does not need realized GdkWinow out of realize handler
[wxWidgets.git] / src / gtk / dc.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk/dc.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
6c9a19aa 5// Copyright: (c) 1998 Robert Roebling
65571936 6// Licence: wxWindows licence
c801d85f
KB
7/////////////////////////////////////////////////////////////////////////////
8
14f355c2
VS
9// For compilers that support precompilation, includes "wx.h".
10#include "wx/wxprec.h"
c801d85f 11
9dc44eff
PC
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
23wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner)
24 : base_type(owner)
25{
26 m_width = 0;
27 m_height = 0;
28}
29
f30b9eed
PC
30wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner, int)
31 : base_type(owner, 0)
32{
33 m_width = 0;
34 m_height = 0;
35}
36
9dc44eff 37wxGTKCairoDCImpl::wxGTKCairoDCImpl(wxDC* owner, wxWindow* window)
f30b9eed 38 : base_type(owner, 0)
9dc44eff
PC
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
48void 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
63void wxGTKCairoDCImpl::DoDrawIcon(const wxIcon& icon, int x, int y)
64{
65 DoDrawBitmap(icon, x, y, true);
66}
67
68#if wxUSE_IMAGE
69bool wxDoFloodFill(wxDC* dc, int x, int y, const wxColour& col, wxFloodFillStyle style);
70
71bool wxGTKCairoDCImpl::DoFloodFill(int x, int y, const wxColour& col, wxFloodFillStyle style)
72{
73 return wxDoFloodFill(GetOwner(), x, y, col, style);
74}
75#endif
76
77wxBitmap wxGTKCairoDCImpl::DoGetAsBitmap(const wxRect* /*subrect*/) const
78{
79 wxFAIL_MSG("DoGetAsBitmap not implemented");
80 return wxBitmap();
81}
82
83bool 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
109void wxGTKCairoDCImpl::DoGetSize(int* width, int* height) const
110{
111 if (width)
112 *width = m_width;
113 if (height)
114 *height = m_height;
115}
116
117bool 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)
5ca21fe7 156 maskSurf = *mask;
9dc44eff
PC
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
179void* wxGTKCairoDCImpl::GetCairoContext() const
180{
181 cairo_t* cr = NULL;
182 if (m_graphicContext)
183 cr = static_cast<cairo_t*>(m_graphicContext->GetNativeContext());
184 if (cr)
185 cairo_reference(cr);
186 return cr;
187}
188//-----------------------------------------------------------------------------
189
190wxWindowDCImpl::wxWindowDCImpl(wxWindowDC* owner, wxWindow* window)
191 : base_type(owner, window)
192{
193 GtkWidget* widget = window->m_wxwindow;
194 if (widget == NULL)
195 widget = window->m_widget;
196 GdkWindow* gdkWindow = NULL;
197 if (widget)
198 {
199 gdkWindow = gtk_widget_get_window(widget);
200 m_ok = true;
201 }
202 if (gdkWindow)
203 {
204 cairo_t* cr = gdk_cairo_create(gdkWindow);
205 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
206 GtkAllocation a;
207 gtk_widget_get_allocation(widget, &a);
208 int x, y;
209 if (gtk_widget_get_has_window(widget))
210 {
211 m_width = gdk_window_get_width(gdkWindow);
212 m_height = gdk_window_get_height(gdkWindow);
213 x = m_width - a.width;
214 y = m_height - a.height;
215 }
216 else
217 {
218 m_width = a.width;
219 m_height = a.height;
220 x = a.x;
221 y = a.y;
222 cairo_rectangle(cr, a.x, a.y, a.width, a.height);
223 cairo_clip(cr);
224 }
225 if (x || y)
226 SetDeviceLocalOrigin(x, y);
227 }
f30b9eed
PC
228 else
229 SetGraphicsContext(wxGraphicsContext::Create());
9dc44eff
PC
230}
231//-----------------------------------------------------------------------------
232
233wxClientDCImpl::wxClientDCImpl(wxClientDC* owner, wxWindow* window)
234 : base_type(owner, window)
235{
236 GtkWidget* widget = window->m_wxwindow;
237 if (widget == NULL)
238 widget = window->m_widget;
239 GdkWindow* gdkWindow = NULL;
240 if (widget)
241 {
242 gdkWindow = gtk_widget_get_window(widget);
243 m_ok = true;
244 }
245 if (gdkWindow)
246 {
247 cairo_t* cr = gdk_cairo_create(gdkWindow);
248 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
249 if (gtk_widget_get_has_window(widget))
250 {
251 m_width = gdk_window_get_width(gdkWindow);
252 m_height = gdk_window_get_height(gdkWindow);
253 }
254 else
255 {
256 GtkAllocation a;
257 gtk_widget_get_allocation(widget, &a);
258 m_width = a.width;
259 m_height = a.height;
260 cairo_rectangle(cr, a.x, a.y, a.width, a.height);
261 cairo_clip(cr);
262 SetDeviceLocalOrigin(a.x, a.y);
263 }
264 }
265 else
f30b9eed 266 SetGraphicsContext(wxGraphicsContext::Create());
9dc44eff
PC
267}
268//-----------------------------------------------------------------------------
269
270wxPaintDCImpl::wxPaintDCImpl(wxPaintDC* owner, wxWindow* window)
271 : base_type(owner, window)
272{
273 cairo_t* cr = window->GTKPaintContext();
274 wxCHECK_RET(cr, "using wxPaintDC without being in a native paint event");
275 GdkWindow* gdkWindow = gtk_widget_get_window(window->m_wxwindow);
276 m_width = gdk_window_get_width(gdkWindow);
277 m_height = gdk_window_get_height(gdkWindow);
278 cairo_reference(cr);
279 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
280}
281//-----------------------------------------------------------------------------
282
283wxScreenDCImpl::wxScreenDCImpl(wxScreenDC* owner)
f30b9eed 284 : base_type(owner, 0)
9dc44eff
PC
285{
286 GdkWindow* window = gdk_get_default_root_window();
287 m_width = gdk_window_get_width(window);
288 m_height = gdk_window_get_height(window);
289 cairo_t* cr = gdk_cairo_create(window);
290 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
291}
292//-----------------------------------------------------------------------------
293
294wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner)
295 : base_type(owner)
296{
297 m_ok = false;
298}
299
300wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxBitmap& bitmap)
f30b9eed 301 : base_type(owner, 0)
9dc44eff
PC
302 , m_bitmap(bitmap)
303{
304 Setup();
305}
306
307wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxDC*)
308 : base_type(owner)
309{
310 m_ok = false;
311}
312
313wxBitmap wxMemoryDCImpl::DoGetAsBitmap(const wxRect* subrect) const
314{
315 return subrect ? m_bitmap.GetSubBitmap(*subrect) : m_bitmap;
316}
317
318void wxMemoryDCImpl::DoSelect(const wxBitmap& bitmap)
319{
320 m_bitmap = bitmap;
321 Setup();
322}
323
324const wxBitmap& wxMemoryDCImpl::GetSelectedBitmap() const
325{
326 return m_bitmap;
327}
328
329wxBitmap& wxMemoryDCImpl::GetSelectedBitmap()
330{
331 return m_bitmap;
332}
333
334void wxMemoryDCImpl::Setup()
335{
336 wxGraphicsContext* gc = NULL;
337 m_ok = m_bitmap.IsOk();
338 if (m_ok)
339 {
340 m_width = m_bitmap.GetWidth();
341 m_height = m_bitmap.GetHeight();
342 cairo_t* cr = m_bitmap.CairoCreate();
343 gc = wxGraphicsContext::CreateFromNative(cr);
344 }
345 SetGraphicsContext(gc);
346}
347//-----------------------------------------------------------------------------
348
349wxGTKCairoDC::wxGTKCairoDC(cairo_t* cr)
f30b9eed 350 : base_type(new wxGTKCairoDCImpl(this, 0))
9dc44eff
PC
351{
352 cairo_reference(cr);
353 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
354}
355
356#else
357
18591d70 358#include "wx/gtk/dc.h"
d296fd8f 359
d296fd8f 360//-----------------------------------------------------------------------------
888dde65 361// wxGTKDCImpl
d296fd8f
RR
362//-----------------------------------------------------------------------------
363
888dde65 364IMPLEMENT_ABSTRACT_CLASS(wxGTKDCImpl, wxDCImpl)
c801d85f 365
03647350 366wxGTKDCImpl::wxGTKDCImpl( wxDC *owner )
888dde65 367 : wxDCImpl( owner )
c801d85f 368{
4bc67cc5 369 m_ok = FALSE;
a23fd0e1 370
4bc67cc5
RR
371 m_pen = *wxBLACK_PEN;
372 m_font = *wxNORMAL_FONT;
41bf0eb3 373 m_brush = *wxWHITE_BRUSH;
ff7b1510 374}
c801d85f 375
888dde65 376wxGTKDCImpl::~wxGTKDCImpl()
ab171e95
RR
377{
378}
379
888dde65 380void wxGTKDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
c801d85f 381{
4bc67cc5
RR
382 m_clipping = TRUE;
383 m_clipX1 = x;
384 m_clipY1 = y;
385 m_clipX2 = x + width;
386 m_clipY2 = y + height;
ff7b1510 387}
c801d85f 388
a23fd0e1
VZ
389// ---------------------------------------------------------------------------
390// get DC capabilities
391// ---------------------------------------------------------------------------
c801d85f 392
888dde65 393void wxGTKDCImpl::DoGetSizeMM( int* width, int* height ) const
c801d85f 394{
4bc67cc5
RR
395 int w = 0;
396 int h = 0;
ab171e95 397 GetOwner()->GetSize( &w, &h );
ce83033f
VZ
398 if (width) *width = int( double(w) / (m_userScaleX*m_mm_to_pix_x) );
399 if (height) *height = int( double(h) / (m_userScaleY*m_mm_to_pix_y) );
7bcb11d3
JS
400}
401
402// Resolution in pixels per logical inch
888dde65 403wxSize wxGTKDCImpl::GetPPI() const
7bcb11d3
JS
404{
405 // TODO (should probably be pure virtual)
406 return wxSize(0, 0);
ff7b1510 407}
9dc44eff 408#endif