1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/dcmemory.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 #include "wx/gtk/dcmemory.h"
13 
14 #include "wx/gtk/private/wrapgtk.h"
15 
16 //-----------------------------------------------------------------------------
17 // wxMemoryDCImpl
18 //-----------------------------------------------------------------------------
19 
20 wxIMPLEMENT_ABSTRACT_CLASS(wxMemoryDCImpl, wxWindowDCImpl);
21 
wxMemoryDCImpl(wxMemoryDC * owner)22 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner )
23   : wxWindowDCImpl( owner )
24 {
25     Init();
26 }
27 
wxMemoryDCImpl(wxMemoryDC * owner,wxBitmap & bitmap)28 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner, wxBitmap& bitmap)
29   : wxWindowDCImpl( owner )
30 {
31     Init();
32     DoSelect(bitmap);
33 }
34 
wxMemoryDCImpl(wxMemoryDC * owner,wxDC * WXUNUSED (dc))35 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner, wxDC *WXUNUSED(dc) )
36   : wxWindowDCImpl( owner )
37 {
38     Init();
39 }
40 
~wxMemoryDCImpl()41 wxMemoryDCImpl::~wxMemoryDCImpl()
42 {
43 }
44 
Init()45 void wxMemoryDCImpl::Init()
46 {
47     m_ok = false;
48 
49     m_cmap = gtk_widget_get_default_colormap();
50 
51     m_context = gdk_pango_context_get();
52     // Note: The Sun customised version of Pango shipping with Solaris 10
53     // crashes if the language is left NULL (see bug 1374114)
54     pango_context_set_language( m_context, gtk_get_default_language() );
55     m_layout = pango_layout_new( m_context );
56     m_fontdesc = pango_font_description_copy( pango_context_get_font_description( m_context ) );
57 }
58 
DoSelect(const wxBitmap & bitmap)59 void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap )
60 {
61     Destroy();
62 
63     m_selected = bitmap;
64     if (m_selected.IsOk())
65     {
66         m_gdkwindow = m_selected.GetPixmap();
67 
68         m_selected.PurgeOtherRepresentations(wxBitmap::Pixmap);
69 
70         SetUpDC( true );
71     }
72     else
73     {
74         m_ok = false;
75         m_gdkwindow = NULL;
76     }
77 }
78 
SetPen(const wxPen & penOrig)79 void wxMemoryDCImpl::SetPen( const wxPen& penOrig )
80 {
81     wxPen pen( penOrig );
82     if ( m_selected.IsOk() &&
83             m_selected.GetDepth() == 1 &&
84                 (pen != *wxTRANSPARENT_PEN) )
85     {
86         pen.SetColour( pen.GetColour() == *wxWHITE ? *wxBLACK : *wxWHITE );
87     }
88 
89     wxWindowDCImpl::SetPen( pen );
90 }
91 
SetBrush(const wxBrush & brushOrig)92 void wxMemoryDCImpl::SetBrush( const wxBrush& brushOrig )
93 {
94     wxBrush brush( brushOrig );
95     if ( m_selected.IsOk() &&
96             m_selected.GetDepth() == 1 &&
97                 (brush != *wxTRANSPARENT_BRUSH) )
98     {
99         brush.SetColour( brush.GetColour() == *wxWHITE ? *wxBLACK : *wxWHITE);
100     }
101 
102     wxWindowDCImpl::SetBrush( brush );
103 }
104 
SetBackground(const wxBrush & brushOrig)105 void wxMemoryDCImpl::SetBackground( const wxBrush& brushOrig )
106 {
107     wxBrush brush(brushOrig);
108 
109     if ( m_selected.IsOk() &&
110             m_selected.GetDepth() == 1 &&
111                 (brush != *wxTRANSPARENT_BRUSH) )
112     {
113         brush.SetColour( brush.GetColour() == *wxWHITE ? *wxBLACK : *wxWHITE );
114     }
115 
116     wxWindowDCImpl::SetBackground( brush );
117 }
118 
SetTextForeground(const wxColour & col)119 void wxMemoryDCImpl::SetTextForeground( const wxColour& col )
120 {
121     if ( m_selected.IsOk() && m_selected.GetDepth() == 1 )
122         wxWindowDCImpl::SetTextForeground( col == *wxWHITE ? *wxBLACK : *wxWHITE);
123     else
124         wxWindowDCImpl::SetTextForeground( col );
125 }
126 
SetTextBackground(const wxColour & col)127 void wxMemoryDCImpl::SetTextBackground( const wxColour &col )
128 {
129     if (m_selected.IsOk() && m_selected.GetDepth() == 1)
130         wxWindowDCImpl::SetTextBackground( col == *wxWHITE ? *wxBLACK : *wxWHITE );
131     else
132         wxWindowDCImpl::SetTextBackground( col );
133 }
134 
DoGetSize(int * width,int * height) const135 void wxMemoryDCImpl::DoGetSize( int *width, int *height ) const
136 {
137     if (m_selected.IsOk())
138     {
139         if (width) (*width) = m_selected.GetWidth();
140         if (height) (*height) = m_selected.GetHeight();
141     }
142     else
143     {
144         if (width) (*width) = 0;
145         if (height) (*height) = 0;
146     }
147 }
148 
DoGetAsBitmap(const wxRect * subrect) const149 wxBitmap wxMemoryDCImpl::DoGetAsBitmap(const wxRect *subrect) const
150 {
151     wxBitmap bmp = GetSelectedBitmap();
152     return subrect ? bmp.GetSubBitmap(*subrect) : bmp;
153 }
154 
GetSelectedBitmap() const155 const wxBitmap& wxMemoryDCImpl::GetSelectedBitmap() const
156 {
157     return m_selected;
158 }
159 
GetSelectedBitmap()160 wxBitmap& wxMemoryDCImpl::GetSelectedBitmap()
161 {
162     return m_selected;
163 }
164 
GetHandle() const165 void* wxMemoryDCImpl::GetHandle() const
166 {
167     const wxBitmap& bmp = GetSelectedBitmap();
168     return bmp.GetPixmap();
169 }
170