]>
Commit | Line | Data |
---|---|---|
fca36d9a JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: statpict.cpp | |
3 | // Purpose: wxStaticPicture | |
4 | // Author: Wade Brainerd (wadeb@wadeb.com) | |
5 | // Modified by: | |
6 | // Created: 2003-05-01 | |
7 | // RCS-ID: | |
8 | // Copyright: (c) Wade Brainerd | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
0199503b | 12 | #include "wx/wxprec.h" |
fca36d9a JS |
13 | #include "wx/defs.h" |
14 | ||
bce78bdc | 15 | #include "wx/gizmos/statpict.h" |
fca36d9a JS |
16 | #include "wx/dcclient.h" |
17 | ||
fca36d9a | 18 | IMPLEMENT_DYNAMIC_CLASS(wxStaticPicture, wxControl) |
934b2e18 RD |
19 | WXDLLIMPEXP_GIZMOS const wxChar * wxStaticPictureNameStr = wxT("staticPicture"); |
20 | ||
fca36d9a JS |
21 | /* |
22 | * wxStaticPicture | |
23 | */ | |
24 | ||
25 | BEGIN_EVENT_TABLE(wxStaticPicture, wxControl) | |
26 | EVT_PAINT(wxStaticPicture::OnPaint) | |
27 | END_EVENT_TABLE() | |
28 | ||
29 | bool wxStaticPicture::Create(wxWindow *parent, wxWindowID id, | |
30 | const wxBitmap& bitmap, | |
31 | const wxPoint& pos, | |
32 | const wxSize& s, | |
33 | long style, | |
34 | const wxString& name) | |
35 | { | |
36 | SetName(name); | |
37 | ||
38 | wxSize size = s ; | |
39 | if ( bitmap.Ok() ) | |
40 | { | |
422d0ff0 | 41 | if ( size.x == wxDefaultCoord ) |
fca36d9a | 42 | size.x = bitmap.GetWidth() ; |
422d0ff0 | 43 | if ( size.y == wxDefaultCoord ) |
fca36d9a JS |
44 | size.y = bitmap.GetHeight() ; |
45 | } | |
46 | ||
47 | m_backgroundColour = parent->GetBackgroundColour() ; | |
48 | m_foregroundColour = parent->GetForegroundColour() ; | |
49 | ||
50 | Bitmap = bitmap; | |
51 | Align = 0; | |
52 | Scale = 0; | |
53 | ScaleX = ScaleY = 1; | |
54 | ||
55 | #ifndef __WXMSW__ | |
56 | LastScaleX = LastScaleY = -1; | |
57 | if ( Bitmap.Ok() ) | |
58 | OriginalImage = Bitmap.ConvertToImage(); | |
59 | #endif | |
60 | ||
a2d49353 | 61 | if ( id == wxID_ANY ) |
fca36d9a JS |
62 | m_windowId = (int)NewControlId(); |
63 | else | |
64 | m_windowId = id; | |
65 | ||
66 | m_windowStyle = style; | |
67 | ||
68 | bool ret = wxControl::Create( parent, id, pos, size, style, wxDefaultValidator, name ); | |
69 | ||
70 | SetBestSize( size ) ; | |
71 | ||
72 | return ret; | |
73 | } | |
74 | ||
75 | void wxStaticPicture::SetBitmap( const wxBitmap& bmp ) | |
76 | { | |
77 | Bitmap = bmp; | |
78 | #ifndef __WXMSW__ | |
79 | if ( Bitmap.Ok() ) | |
80 | OriginalImage = Bitmap.ConvertToImage(); | |
81 | LastScaleX = LastScaleY = -1; | |
82 | #endif | |
83 | } | |
84 | ||
85 | void wxStaticPicture::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
86 | { | |
87 | if ( !Bitmap.Ok() ) | |
88 | return; | |
89 | ||
90 | wxPaintDC dc( this ); | |
91 | PrepareDC( dc ); | |
92 | dc.BeginDrawing(); | |
93 | ||
94 | wxSize sz = GetSize(); | |
95 | wxSize bmpsz( Bitmap.GetWidth(), Bitmap.GetHeight() ); | |
96 | float sx = 1.0f, sy = 1.0f; | |
97 | ||
98 | if ( Scale & wxSCALE_UNIFORM ) | |
99 | { | |
100 | float _sx = (float)sz.GetWidth() / (float)bmpsz.GetWidth(); | |
101 | float _sy = (float)sz.GetHeight() / (float)bmpsz.GetHeight(); | |
102 | sx = sy = _sx < _sy ? _sx : _sy; | |
103 | } | |
104 | else | |
105 | if ( Scale & wxSCALE_CUSTOM ) | |
106 | { | |
107 | sx = ScaleX; | |
108 | sy = ScaleY; | |
109 | } | |
110 | else | |
111 | { | |
112 | if ( Scale & wxSCALE_HORIZONTAL ) | |
113 | sx = (float)sz.x/(float)bmpsz.x; | |
114 | if ( Scale & wxSCALE_VERTICAL ) | |
115 | sy = (float)sz.y/(float)bmpsz.y; | |
116 | } | |
117 | ||
118 | bmpsz = wxSize( (int)(bmpsz.x*sx), (int)(bmpsz.y*sy) ); | |
119 | ||
120 | wxPoint pos( 0, 0 ); | |
121 | ||
122 | if ( Align & wxALIGN_CENTER_HORIZONTAL ) pos.x = (sz.x-bmpsz.x)/2; | |
123 | else if ( Align & wxALIGN_RIGHT ) pos.x = sz.x-bmpsz.x; | |
124 | ||
125 | if ( Align & wxALIGN_CENTER_VERTICAL ) pos.y = (sz.y-bmpsz.y)/2; | |
126 | else if ( Align & wxALIGN_BOTTOM ) pos.y = sz.y-bmpsz.y; | |
127 | ||
128 | if ( Scale ) | |
129 | { | |
130 | #ifdef __WXMSW__ | |
131 | double ux, uy; | |
132 | dc.GetUserScale( &ux, &uy ); | |
133 | dc.SetUserScale( ux*sx, uy*sy ); | |
134 | dc.DrawBitmap( Bitmap, (int)((float)pos.x/sx), (int)((float)pos.y/sy) ); | |
135 | dc.SetUserScale( ux, uy ); | |
136 | #else | |
137 | if ( LastScaleX != sx || LastScaleY != sy ) | |
138 | { | |
139 | LastScaleX = sx; | |
140 | LastScaleY = sy; | |
141 | ScaledBitmap = wxBitmap( OriginalImage.Scale( bmpsz.x, bmpsz.y ) ); | |
142 | } | |
143 | dc.DrawBitmap( ScaledBitmap, pos.x, pos.y ); | |
144 | #endif | |
145 | } | |
146 | else | |
147 | dc.DrawBitmap( Bitmap, pos.x, pos.y ); | |
148 | ||
149 | dc.EndDrawing(); | |
150 | } | |
151 |