]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dc.cpp
Make wxWindow::HasScrollbar() respect wxScrolled::ShowScrollbars().
[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 if (cr)
185 cairo_reference(cr);
186 return cr;
187 }
188 //-----------------------------------------------------------------------------
189
190 wxWindowDCImpl::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 }
228 else
229 SetGraphicsContext(wxGraphicsContext::Create());
230 }
231 //-----------------------------------------------------------------------------
232
233 wxClientDCImpl::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
266 SetGraphicsContext(wxGraphicsContext::Create());
267 }
268 //-----------------------------------------------------------------------------
269
270 wxPaintDCImpl::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
283 wxScreenDCImpl::wxScreenDCImpl(wxScreenDC* owner)
284 : base_type(owner, 0)
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
294 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner)
295 : base_type(owner)
296 {
297 m_ok = false;
298 }
299
300 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxBitmap& bitmap)
301 : base_type(owner, 0)
302 , m_bitmap(bitmap)
303 {
304 Setup();
305 }
306
307 wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxDC*)
308 : base_type(owner)
309 {
310 m_ok = false;
311 }
312
313 wxBitmap wxMemoryDCImpl::DoGetAsBitmap(const wxRect* subrect) const
314 {
315 return subrect ? m_bitmap.GetSubBitmap(*subrect) : m_bitmap;
316 }
317
318 void wxMemoryDCImpl::DoSelect(const wxBitmap& bitmap)
319 {
320 m_bitmap = bitmap;
321 Setup();
322 }
323
324 const wxBitmap& wxMemoryDCImpl::GetSelectedBitmap() const
325 {
326 return m_bitmap;
327 }
328
329 wxBitmap& wxMemoryDCImpl::GetSelectedBitmap()
330 {
331 return m_bitmap;
332 }
333
334 void 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
349 wxGTKCairoDC::wxGTKCairoDC(cairo_t* cr)
350 : base_type(new wxGTKCairoDCImpl(this, 0))
351 {
352 cairo_reference(cr);
353 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
354 }
355
356 #else
357
358 #include "wx/gtk/dc.h"
359
360 //-----------------------------------------------------------------------------
361 // wxGTKDCImpl
362 //-----------------------------------------------------------------------------
363
364 IMPLEMENT_ABSTRACT_CLASS(wxGTKDCImpl, wxDCImpl)
365
366 wxGTKDCImpl::wxGTKDCImpl( wxDC *owner )
367 : wxDCImpl( owner )
368 {
369 m_ok = FALSE;
370
371 m_pen = *wxBLACK_PEN;
372 m_font = *wxNORMAL_FONT;
373 m_brush = *wxWHITE_BRUSH;
374 }
375
376 wxGTKDCImpl::~wxGTKDCImpl()
377 {
378 }
379
380 void wxGTKDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
381 {
382 m_clipping = TRUE;
383 m_clipX1 = x;
384 m_clipY1 = y;
385 m_clipX2 = x + width;
386 m_clipY2 = y + height;
387 }
388
389 // ---------------------------------------------------------------------------
390 // get DC capabilities
391 // ---------------------------------------------------------------------------
392
393 void wxGTKDCImpl::DoGetSizeMM( int* width, int* height ) const
394 {
395 int w = 0;
396 int h = 0;
397 GetOwner()->GetSize( &w, &h );
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) );
400 }
401
402 // Resolution in pixels per logical inch
403 wxSize wxGTKDCImpl::GetPPI() const
404 {
405 // TODO (should probably be pure virtual)
406 return wxSize(0, 0);
407 }
408 #endif