]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/gizmos/statpict.cpp
Applied patch [ 735948 ] wxStaticPicture contrib
[wxWidgets.git] / contrib / src / gizmos / statpict.cpp
CommitLineData
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
12#ifdef __GNUG__
13 #pragma implementation "statpict.h"
14#endif
15
16#include "wx/defs.h"
17
18#include "statpict.h"
19#include "wx/dcclient.h"
20
21#if !USE_SHARED_LIBRARY
22IMPLEMENT_DYNAMIC_CLASS(wxStaticPicture, wxControl)
23#endif
24
25/*
26 * wxStaticPicture
27 */
28
29BEGIN_EVENT_TABLE(wxStaticPicture, wxControl)
30 EVT_PAINT(wxStaticPicture::OnPaint)
31END_EVENT_TABLE()
32
33bool wxStaticPicture::Create(wxWindow *parent, wxWindowID id,
34 const wxBitmap& bitmap,
35 const wxPoint& pos,
36 const wxSize& s,
37 long style,
38 const wxString& name)
39{
40 SetName(name);
41
42 wxSize size = s ;
43 if ( bitmap.Ok() )
44 {
45 if ( size.x == -1 )
46 size.x = bitmap.GetWidth() ;
47 if ( size.y == -1 )
48 size.y = bitmap.GetHeight() ;
49 }
50
51 m_backgroundColour = parent->GetBackgroundColour() ;
52 m_foregroundColour = parent->GetForegroundColour() ;
53
54 Bitmap = bitmap;
55 Align = 0;
56 Scale = 0;
57 ScaleX = ScaleY = 1;
58
59#ifndef __WXMSW__
60 LastScaleX = LastScaleY = -1;
61 if ( Bitmap.Ok() )
62 OriginalImage = Bitmap.ConvertToImage();
63#endif
64
65 if ( id == -1 )
66 m_windowId = (int)NewControlId();
67 else
68 m_windowId = id;
69
70 m_windowStyle = style;
71
72 bool ret = wxControl::Create( parent, id, pos, size, style, wxDefaultValidator, name );
73
74 SetBestSize( size ) ;
75
76 return ret;
77}
78
79void wxStaticPicture::SetBitmap( const wxBitmap& bmp )
80{
81 Bitmap = bmp;
82#ifndef __WXMSW__
83 if ( Bitmap.Ok() )
84 OriginalImage = Bitmap.ConvertToImage();
85 LastScaleX = LastScaleY = -1;
86#endif
87}
88
89void wxStaticPicture::OnPaint(wxPaintEvent& WXUNUSED(event))
90{
91 if ( !Bitmap.Ok() )
92 return;
93
94 wxPaintDC dc( this );
95 PrepareDC( dc );
96 dc.BeginDrawing();
97
98 wxSize sz = GetSize();
99 wxSize bmpsz( Bitmap.GetWidth(), Bitmap.GetHeight() );
100 float sx = 1.0f, sy = 1.0f;
101
102 if ( Scale & wxSCALE_UNIFORM )
103 {
104 float _sx = (float)sz.GetWidth() / (float)bmpsz.GetWidth();
105 float _sy = (float)sz.GetHeight() / (float)bmpsz.GetHeight();
106 sx = sy = _sx < _sy ? _sx : _sy;
107 }
108 else
109 if ( Scale & wxSCALE_CUSTOM )
110 {
111 sx = ScaleX;
112 sy = ScaleY;
113 }
114 else
115 {
116 if ( Scale & wxSCALE_HORIZONTAL )
117 sx = (float)sz.x/(float)bmpsz.x;
118 if ( Scale & wxSCALE_VERTICAL )
119 sy = (float)sz.y/(float)bmpsz.y;
120 }
121
122 bmpsz = wxSize( (int)(bmpsz.x*sx), (int)(bmpsz.y*sy) );
123
124 wxPoint pos( 0, 0 );
125
126 if ( Align & wxALIGN_CENTER_HORIZONTAL ) pos.x = (sz.x-bmpsz.x)/2;
127 else if ( Align & wxALIGN_RIGHT ) pos.x = sz.x-bmpsz.x;
128
129 if ( Align & wxALIGN_CENTER_VERTICAL ) pos.y = (sz.y-bmpsz.y)/2;
130 else if ( Align & wxALIGN_BOTTOM ) pos.y = sz.y-bmpsz.y;
131
132 if ( Scale )
133 {
134#ifdef __WXMSW__
135 double ux, uy;
136 dc.GetUserScale( &ux, &uy );
137 dc.SetUserScale( ux*sx, uy*sy );
138 dc.DrawBitmap( Bitmap, (int)((float)pos.x/sx), (int)((float)pos.y/sy) );
139 dc.SetUserScale( ux, uy );
140#else
141 if ( LastScaleX != sx || LastScaleY != sy )
142 {
143 LastScaleX = sx;
144 LastScaleY = sy;
145 ScaledBitmap = wxBitmap( OriginalImage.Scale( bmpsz.x, bmpsz.y ) );
146 }
147 dc.DrawBitmap( ScaledBitmap, pos.x, pos.y );
148#endif
149 }
150 else
151 dc.DrawBitmap( Bitmap, pos.x, pos.y );
152
153 dc.EndDrawing();
154}
155
156//WXDLLEXPORT_DATA(const wxChar *) wxStaticPictureNameStr = wxT("message");
157const wxChar * wxStaticPictureNameStr = wxT("message");