]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dc.cpp
Add wxTranslations::GetTranslatedString().
[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());
9dc44eff
PC
184 return cr;
185}
186//-----------------------------------------------------------------------------
187
188wxWindowDCImpl::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 }
f30b9eed
PC
226 else
227 SetGraphicsContext(wxGraphicsContext::Create());
9dc44eff
PC
228}
229//-----------------------------------------------------------------------------
230
231wxClientDCImpl::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
f30b9eed 264 SetGraphicsContext(wxGraphicsContext::Create());
9dc44eff
PC
265}
266//-----------------------------------------------------------------------------
267
268wxPaintDCImpl::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
281wxScreenDCImpl::wxScreenDCImpl(wxScreenDC* owner)
f30b9eed 282 : base_type(owner, 0)
9dc44eff
PC
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
292wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner)
293 : base_type(owner)
294{
295 m_ok = false;
296}
297
298wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxBitmap& bitmap)
f30b9eed 299 : base_type(owner, 0)
9dc44eff
PC
300 , m_bitmap(bitmap)
301{
302 Setup();
303}
304
305wxMemoryDCImpl::wxMemoryDCImpl(wxMemoryDC* owner, wxDC*)
306 : base_type(owner)
307{
308 m_ok = false;
309}
310
311wxBitmap wxMemoryDCImpl::DoGetAsBitmap(const wxRect* subrect) const
312{
313 return subrect ? m_bitmap.GetSubBitmap(*subrect) : m_bitmap;
314}
315
316void wxMemoryDCImpl::DoSelect(const wxBitmap& bitmap)
317{
318 m_bitmap = bitmap;
319 Setup();
320}
321
322const wxBitmap& wxMemoryDCImpl::GetSelectedBitmap() const
323{
324 return m_bitmap;
325}
326
327wxBitmap& wxMemoryDCImpl::GetSelectedBitmap()
328{
329 return m_bitmap;
330}
331
332void 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
347wxGTKCairoDC::wxGTKCairoDC(cairo_t* cr)
f30b9eed 348 : base_type(new wxGTKCairoDCImpl(this, 0))
9dc44eff
PC
349{
350 cairo_reference(cr);
351 SetGraphicsContext(wxGraphicsContext::CreateFromNative(cr));
352}
353
354#else
355
18591d70 356#include "wx/gtk/dc.h"
d296fd8f 357
d296fd8f 358//-----------------------------------------------------------------------------
888dde65 359// wxGTKDCImpl
d296fd8f
RR
360//-----------------------------------------------------------------------------
361
888dde65 362IMPLEMENT_ABSTRACT_CLASS(wxGTKDCImpl, wxDCImpl)
c801d85f 363
03647350 364wxGTKDCImpl::wxGTKDCImpl( wxDC *owner )
888dde65 365 : wxDCImpl( owner )
c801d85f 366{
4bc67cc5 367 m_ok = FALSE;
a23fd0e1 368
4bc67cc5
RR
369 m_pen = *wxBLACK_PEN;
370 m_font = *wxNORMAL_FONT;
41bf0eb3 371 m_brush = *wxWHITE_BRUSH;
ff7b1510 372}
c801d85f 373
888dde65 374wxGTKDCImpl::~wxGTKDCImpl()
ab171e95
RR
375{
376}
377
888dde65 378void wxGTKDCImpl::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
c801d85f 379{
4bc67cc5
RR
380 m_clipping = TRUE;
381 m_clipX1 = x;
382 m_clipY1 = y;
383 m_clipX2 = x + width;
384 m_clipY2 = y + height;
ff7b1510 385}
c801d85f 386
a23fd0e1
VZ
387// ---------------------------------------------------------------------------
388// get DC capabilities
389// ---------------------------------------------------------------------------
c801d85f 390
888dde65 391void wxGTKDCImpl::DoGetSizeMM( int* width, int* height ) const
c801d85f 392{
4bc67cc5
RR
393 int w = 0;
394 int h = 0;
ab171e95 395 GetOwner()->GetSize( &w, &h );
ce83033f
VZ
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) );
7bcb11d3
JS
398}
399
400// Resolution in pixels per logical inch
888dde65 401wxSize wxGTKDCImpl::GetPPI() const
7bcb11d3
JS
402{
403 // TODO (should probably be pure virtual)
404 return wxSize(0, 0);
ff7b1510 405}
9dc44eff 406#endif