Added wxDC::CreateGraphicsContext and implemented it for a few DCs
[wxWidgets.git] / src / gtk / dcmemory.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dcmemory.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/dcmemory.h"
14 #include "wx/gtk/dcmemory.h"
15
16 #include <gdk/gdk.h>
17 #include <gtk/gtk.h>
18
19 #if wxUSE_GRAPHICS_CONTEXT
20 #include "wx/graphics.h"
21 #endif
22
23 //-----------------------------------------------------------------------------
24 // wxMemoryDCImpl
25 //-----------------------------------------------------------------------------
26
27 IMPLEMENT_ABSTRACT_CLASS(wxMemoryDCImpl, wxWindowDCImpl)
28
29 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner )
30 : wxWindowDCImpl( owner )
31 {
32 Init();
33 }
34
35 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner, wxBitmap& bitmap)
36 : wxWindowDCImpl( owner )
37 {
38 Init();
39 DoSelect(bitmap);
40 }
41
42 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner, wxDC *WXUNUSED(dc) )
43 : wxWindowDCImpl( owner )
44 {
45 Init();
46 }
47
48 wxMemoryDCImpl::~wxMemoryDCImpl()
49 {
50 g_object_unref(m_context);
51 }
52
53 void wxMemoryDCImpl::Init()
54 {
55 m_ok = false;
56
57 m_cmap = gtk_widget_get_default_colormap();
58
59 m_context = gdk_pango_context_get();
60 // Note: The Sun customised version of Pango shipping with Solaris 10
61 // crashes if the language is left NULL (see bug 1374114)
62 pango_context_set_language( m_context, gtk_get_default_language() );
63 m_layout = pango_layout_new( m_context );
64 m_fontdesc = pango_font_description_copy( pango_context_get_font_description( m_context ) );
65 }
66
67 #if wxUSE_GRAPHICS_CONTEXT
68 wxGraphicsContext* wxMemoryDCImpl::CreateGraphicsContext()
69 {
70 wxMemoryDC *memdc = (wxMemoryDC*) GetOwner();
71 return wxGraphicsRenderer::GetDefaultRenderer()->CreateContext( *memdc );
72 }
73 #endif
74
75 void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap )
76 {
77 Destroy();
78
79 m_selected = bitmap;
80 if (m_selected.Ok())
81 {
82 m_gdkwindow = m_selected.GetPixmap();
83
84 m_selected.PurgeOtherRepresentations(wxBitmap::Pixmap);
85
86 SetUpDC( true );
87 }
88 else
89 {
90 m_ok = false;
91 m_gdkwindow = (GdkWindow *) NULL;
92 }
93 }
94
95 void wxMemoryDCImpl::SetPen( const wxPen& penOrig )
96 {
97 wxPen pen( penOrig );
98 if ( m_selected.Ok() &&
99 m_selected.GetDepth() == 1 &&
100 (pen != *wxTRANSPARENT_PEN) )
101 {
102 pen.SetColour( pen.GetColour() == *wxWHITE ? *wxBLACK : *wxWHITE );
103 }
104
105 wxWindowDCImpl::SetPen( pen );
106 }
107
108 void wxMemoryDCImpl::SetBrush( const wxBrush& brushOrig )
109 {
110 wxBrush brush( brushOrig );
111 if ( m_selected.Ok() &&
112 m_selected.GetDepth() == 1 &&
113 (brush != *wxTRANSPARENT_BRUSH) )
114 {
115 brush.SetColour( brush.GetColour() == *wxWHITE ? *wxBLACK : *wxWHITE);
116 }
117
118 wxWindowDCImpl::SetBrush( brush );
119 }
120
121 void wxMemoryDCImpl::SetBackground( const wxBrush& brushOrig )
122 {
123 wxBrush brush(brushOrig);
124
125 if ( m_selected.Ok() &&
126 m_selected.GetDepth() == 1 &&
127 (brush != *wxTRANSPARENT_BRUSH) )
128 {
129 brush.SetColour( brush.GetColour() == *wxWHITE ? *wxBLACK : *wxWHITE );
130 }
131
132 wxWindowDCImpl::SetBackground( brush );
133 }
134
135 void wxMemoryDCImpl::SetTextForeground( const wxColour& col )
136 {
137 if ( m_selected.Ok() && m_selected.GetDepth() == 1 )
138 wxWindowDCImpl::SetTextForeground( col == *wxWHITE ? *wxBLACK : *wxWHITE);
139 else
140 wxWindowDCImpl::SetTextForeground( col );
141 }
142
143 void wxMemoryDCImpl::SetTextBackground( const wxColour &col )
144 {
145 if (m_selected.Ok() && m_selected.GetDepth() == 1)
146 wxWindowDCImpl::SetTextBackground( col == *wxWHITE ? *wxBLACK : *wxWHITE );
147 else
148 wxWindowDCImpl::SetTextBackground( col );
149 }
150
151 void wxMemoryDCImpl::DoGetSize( int *width, int *height ) const
152 {
153 if (m_selected.Ok())
154 {
155 if (width) (*width) = m_selected.GetWidth();
156 if (height) (*height) = m_selected.GetHeight();
157 }
158 else
159 {
160 if (width) (*width) = 0;
161 if (height) (*height) = 0;
162 }
163 }
164
165 wxBitmap wxMemoryDCImpl::DoGetAsBitmap(const wxRect *subrect) const
166 {
167 wxBitmap bmp = GetSelectedBitmap();
168 return subrect ? bmp.GetSubBitmap(*subrect) : bmp;
169 }
170
171 const wxBitmap& wxMemoryDCImpl::GetSelectedBitmap() const
172 {
173 return m_selected;
174 }
175
176 wxBitmap& wxMemoryDCImpl::GetSelectedBitmap()
177 {
178 return m_selected;
179 }
180