*** empty log message ***
[wxWidgets.git] / src / os2 / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: button.cpp
3 // Purpose: wxButton
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/button.h"
17 #include "wx/brush.h"
18 #include "wx/panel.h"
19 #include "wx/bmpbuttn.h"
20 #include "wx/settings.h"
21 #include "wx/dcscreen.h"
22 #endif
23
24 #include "wx/os2/private.h"
25
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
28 #endif
29
30 // Button
31
32 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
33 const wxPoint& pos,
34 const wxSize& size, long style,
35 const wxValidator& validator,
36 const wxString& name)
37 {
38 SetName(name);
39 SetValidator(validator);
40 m_windowStyle = style;
41
42 parent->AddChild((wxButton *)this);
43
44 if (id == -1)
45 m_windowId = NewControlId();
46 else
47 m_windowId = id;
48
49 // TODO: create button
50
51 return FALSE;
52 }
53
54 wxButton::~wxButton()
55 {
56 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
57 if ( panel )
58 {
59 if ( panel->GetDefaultItem() == this )
60 {
61 // don't leave the panel with invalid default item
62 panel->SetDefaultItem(NULL);
63 }
64 }
65 }
66
67 // ----------------------------------------------------------------------------
68 // size management including autosizing
69 // ----------------------------------------------------------------------------
70
71 wxSize wxButton::DoGetBestSize()
72 {
73 wxString label = wxGetWindowText(GetHWND());
74 int wBtn;
75 GetTextExtent(label, &wBtn, NULL);
76
77 int wChar, hChar;
78 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
79
80 // add a margin - the button is wider than just its label
81 wBtn += 3*wChar;
82
83 // the button height is proportional to the height of the font used
84 int hBtn = 0;// TODO: BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
85
86 return wxSize(wBtn, hBtn);
87 }
88
89 /* static */
90 wxSize wxButton::GetDefaultSize()
91 {
92 static wxSize s_sizeBtn;
93
94 if ( s_sizeBtn.x == 0 )
95 {
96 wxScreenDC dc;
97 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
98
99 // the size of a standard button in the dialog units is 50x14,
100 // translate this to pixels
101 // NB1: the multipliers come from the Windows convention
102 // NB2: the extra +1/+2 were needed to get the size be the same as the
103 // size of the buttons in the standard dialog - I don't know how
104 // this happens, but on my system this size is 75x23 in pixels and
105 // 23*8 isn't even divisible by 14... Would be nice to understand
106 // why these constants are needed though!
107 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
108 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
109 }
110
111 return s_sizeBtn;
112 }
113
114 void wxButton::Command (wxCommandEvent & event)
115 {
116 ProcessCommand (event);
117 }
118
119 // ----------------------------------------------------------------------------
120 // helpers
121 // ----------------------------------------------------------------------------
122
123 bool wxButton::SendClickEvent()
124 {
125 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
126 event.SetEventObject(this);
127
128 return ProcessCommand(event);
129 }
130
131 void wxButton::SetDefault()
132 {
133 wxWindow *parent = GetParent();
134 wxButton *btnOldDefault = NULL;
135 wxPanel *panel = wxDynamicCast(parent, wxPanel);
136 if (panel)
137 panel->SetDefaultItem(this);
138
139 // TODO: make button the default
140 }
141
142 // ----------------------------------------------------------------------------
143 // event/message handlers
144 // ----------------------------------------------------------------------------
145
146 bool wxButton::OS2Command(WXUINT param, WXWORD id)
147 {
148 bool processed = FALSE;
149 // TODO
150 /*
151 switch ( param )
152 {
153 case 1: // 1 for accelerator
154 case BN_CLICKED:
155 processed = SendClickEvent();
156 break;
157 }
158 */
159 return processed;
160 }
161
162 WXHBRUSH wxButton::OnCtlColor(WXHDC pDC,
163 WXHWND pWnd,
164 WXUINT nCtlColor,
165 WXUINT message,
166 WXWPARAM wParam,
167 WXLPARAM lParam)
168 {
169 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
170
171 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
172 }
173
174