Fix buffer overrun in Linux/x86_64 (Pixel is a 64 bit long, passing
[wxWidgets.git] / src / motif / dcmemory.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/dcmemory.cpp
3 // Purpose: wxMemoryDC class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/dcmemory.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/utils.h"
19 #include "wx/settings.h"
20 #endif
21
22 #ifdef __VMS__
23 #pragma message disable nosimpint
24 #endif
25 #include <Xm/Xm.h>
26 #ifdef __VMS__
27 #pragma message enable nosimpint
28 #endif
29
30 #include "wx/motif/private.h"
31
32 //-----------------------------------------------------------------------------
33 // wxMemoryDC
34 //-----------------------------------------------------------------------------
35
36 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxWindowDC)
37
38 wxMemoryDC::wxMemoryDC(void)
39 {
40 m_ok = true;
41 m_display = wxGetDisplay();
42
43 Display* display = (Display*) m_display;
44
45 XGCValues gcvalues;
46 gcvalues.foreground = BlackPixel (display, DefaultScreen (display));
47 gcvalues.background = WhitePixel (display, DefaultScreen (display));
48 gcvalues.graphics_exposures = False;
49 gcvalues.subwindow_mode = IncludeInferiors;
50 gcvalues.line_width = 1;
51 m_gc = (WXGC) XCreateGC (display, RootWindow (display, DefaultScreen (display)),
52 GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode,
53 &gcvalues);
54
55 m_backgroundPixel = gcvalues.background;
56
57 SetBrush (* wxWHITE_BRUSH);
58 SetPen (* wxBLACK_PEN);
59 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
60 }
61
62 wxMemoryDC::wxMemoryDC( wxDC* dc )
63 {
64 m_ok = true;
65 if (dc && dc->IsKindOf(CLASSINFO(wxWindowDC)))
66 m_display = ((wxWindowDC*)dc)->GetDisplay();
67 else
68 m_display = wxGetDisplay();
69
70 Display* display = (Display*) m_display;
71
72 XGCValues gcvalues;
73 gcvalues.foreground = BlackPixel (display, DefaultScreen (display));
74 gcvalues.background = WhitePixel (display, DefaultScreen (display));
75 gcvalues.graphics_exposures = False;
76 gcvalues.subwindow_mode = IncludeInferiors;
77 gcvalues.line_width = 1;
78 m_gc = (WXGC) XCreateGC (display, RootWindow (display, DefaultScreen (display)),
79 GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode,
80 &gcvalues);
81
82 m_backgroundPixel = gcvalues.background;
83
84 SetBrush (* wxWHITE_BRUSH);
85 SetPen (* wxBLACK_PEN);
86 }
87
88 wxMemoryDC::~wxMemoryDC(void)
89 {
90 }
91
92 void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
93 {
94 m_bitmap = bitmap;
95
96 if (m_gc)
97 XFreeGC((Display*) m_display, (GC) m_gc);
98 m_gc = (WXGC) NULL;
99
100 if (m_bitmap.Ok() && (bitmap.GetDisplay() == m_display))
101 {
102 m_pixmap = m_bitmap.GetDrawable();
103 Display* display = (Display*) m_display;
104
105 XGCValues gcvalues;
106 gcvalues.foreground = BlackPixel (display, DefaultScreen (display));
107 gcvalues.background = WhitePixel (display, DefaultScreen (display));
108 gcvalues.graphics_exposures = False;
109 gcvalues.subwindow_mode = IncludeInferiors;
110 gcvalues.line_width = 1;
111 m_gc = (WXGC) XCreateGC (display, (Drawable)m_pixmap/* RootWindow (display, DefaultScreen (display)) */,
112 GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode,
113 &gcvalues);
114
115 m_backgroundPixel = gcvalues.background;
116 m_ok = true;
117
118 SetBrush (* wxWHITE_BRUSH);
119 SetPen (* wxBLACK_PEN);
120 }
121 else
122 {
123 m_ok = false;
124 m_pixmap = (WXPixmap) 0;
125 };
126 }
127
128 void wxMemoryDC::DoGetSize( int *width, int *height ) const
129 {
130 if (m_bitmap.Ok())
131 {
132 if (width) (*width) = m_bitmap.GetWidth();
133 if (height) (*height) = m_bitmap.GetHeight();
134 }
135 else
136 {
137 if (width) (*width) = 0;
138 if (height) (*height) = 0;
139 };
140 }