]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/button.cpp
4 // Author: David Webster
7 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/button.h"
20 #include "wx/bmpbuttn.h"
21 #include "wx/settings.h"
22 #include "wx/dcscreen.h"
23 #include "wx/scrolwin.h"
24 #include "wx/toplevel.h"
27 #include "wx/stockitem.h"
28 #include "wx/os2/private.h"
30 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
33 // Should be at the very least less than winDEFAULT_BUTTON_MARGIN
35 #define FOCUS_MARGIN 3
38 #define BST_CHECKED 0x0001
43 bool wxButton::Create( wxWindow
* pParent
,
45 const wxString
& rsLbl
,
49 const wxValidator
& rValidator
,
50 const wxString
& rsName
)
52 wxString
rsLabel(rsLbl
);
53 if (rsLabel
.empty() && wxIsStockID(vId
))
54 rsLabel
= wxGetStockLabel(vId
);
56 wxString sLabel
= ::wxPMTextToLabel(rsLabel
);
60 SetValidator(rValidator
);
62 m_windowStyle
= lStyle
;
63 pParent
->AddChild((wxButton
*)this);
65 m_windowId
= NewControlId();
68 lStyle
= WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
;
71 // OS/2 PM does not have Right/Left/Top/Bottom styles.
72 // We will have to define an additional style when we implement notebooks
73 // for a notebook page button
75 if (m_windowStyle
& wxCLIP_SIBLINGS
)
76 lStyle
|= WS_CLIPSIBLINGS
;
78 m_hWnd
= (WXHWND
)::WinCreateWindow( GetHwndOf(pParent
) // Parent handle
79 ,WC_BUTTON
// A Button class window
80 ,sLabel
.c_str() // Button text
81 ,lStyle
// Button style
82 ,0, 0, 0, 0 // Location and size
83 ,GetHwndOf(pParent
) // Owner handle
84 ,HWND_TOP
// Top of Z-Order
86 ,NULL
// No control data
87 ,NULL
// No Presentation parameters
95 // Subclass again for purposes of dialog editing mode
98 wxFont
* pButtonFont
= new wxFont( 8
103 SetFont(*pButtonFont
);
113 } // end of wxButton::Create
115 wxButton::~wxButton()
117 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
121 if (tlw
->GetDefaultItem() == this)
124 // Don't leave the panel with invalid default item
126 tlw
->SetDefaultItem(NULL
);
129 } // end of wxButton::~wxButton
131 // ----------------------------------------------------------------------------
132 // size management including autosizing
133 // ----------------------------------------------------------------------------
135 wxSize
wxButton::DoGetBestSize() const
137 wxString rsLabel
= wxGetWindowText(GetHWND());
141 wxFont vFont
= (wxFont
)GetFont();
143 GetTextExtent( rsLabel
148 wxGetCharSize( GetHWND()
155 // Add a margin - the button is wider than just its label
157 nWidthButton
+= 3 * nWidthChar
;
160 // The button height is proportional to the height of the font used
162 int nHeightButton
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar
);
165 // Need a little extra to make it look right
167 nHeightButton
+= (int)(nHeightChar
/1.5);
169 if (!HasFlag(wxBU_EXACTFIT
))
171 wxSize vSize
= GetDefaultSize();
173 if (nWidthButton
> vSize
.x
)
174 vSize
.x
= nWidthButton
;
175 if (nHeightButton
> vSize
.y
)
176 vSize
.y
= nHeightButton
;
179 return wxSize( nWidthButton
182 } // end of wxButton::DoGetBestSize
185 wxSize
wxButton::GetDefaultSize()
187 static wxSize vSizeBtn
;
193 vDc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
196 // The size of a standard button in the dialog units is 50x14,
197 // translate this to pixels
198 // NB1: the multipliers come from the Windows convention
199 // NB2: the extra +1/+2 were needed to get the size be the same as the
200 // size of the buttons in the standard dialog - I don't know how
201 // this happens, but on my system this size is 75x23 in pixels and
202 // 23*8 isn't even divisible by 14... Would be nice to understand
203 // why these constants are needed though!
204 vSizeBtn
.x
= (50 * (vDc
.GetCharWidth() + 1))/4;
205 vSizeBtn
.y
= ((14 * vDc
.GetCharHeight()) + 2)/8;
208 } // end of wxButton::GetDefaultSize
210 void wxButton::Command (
211 wxCommandEvent
& rEvent
214 ProcessCommand (rEvent
);
215 } // end of wxButton::Command
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 bool wxButton::SendClickEvent()
223 wxCommandEvent
vEvent( wxEVT_BUTTON
227 vEvent
.SetEventObject(this);
228 return ProcessCommand(vEvent
);
229 } // end of wxButton::SendClickEvent
231 wxWindow
*wxButton::SetDefault()
234 // Set this one as the default button both for wxWidgets and Windows
236 wxWindow
* pWinOldDefault
= wxButtonBase::SetDefault();
238 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
), false);
239 SetDefaultStyle( this, true );
241 return pWinOldDefault
;
242 } // end of wxButton::SetDefault
244 void wxButton::SetTmpDefault()
246 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
248 wxCHECK_RET( tlw
, wxT("button without top level window?") );
250 wxWindow
* pWinOldDefault
= tlw
->GetDefaultItem();
252 tlw
->SetTmpDefaultItem(this);
253 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
), false);
254 SetDefaultStyle( this, true );
255 } // end of wxButton::SetTmpDefault
257 void wxButton::UnsetTmpDefault()
259 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
261 wxCHECK_RET( tlw
, wxT("button without top level window?") );
263 tlw
->SetTmpDefaultItem(NULL
);
265 wxWindow
* pWinOldDefault
= tlw
->GetDefaultItem();
267 SetDefaultStyle( this, false );
268 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
), true );
269 } // end of wxButton::UnsetTmpDefault
271 void wxButton::SetDefaultStyle(
278 // We may be called with NULL pointer -- simpler to do the check here than
279 // in the caller which does wxDynamicCast()
285 // First, let DefDlgProc() know about the new default button
289 if (!wxTheApp
->IsActive())
293 // In OS/2 the dialog/panel doesn't really know it has a default
294 // button, the default button simply has that style. We'll just
295 // simulate by setting focus to it
299 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtn
), QWL_STYLE
);
300 if (!(lStyle
& BS_DEFAULT
) == bOn
)
302 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
305 lStyle
|= BS_DEFAULT
;
307 lStyle
&= ~BS_DEFAULT
;
308 ::WinSetWindowULong(GetHwndOf(pBtn
), QWL_STYLE
, lStyle
);
313 // Redraw the button - it will notice itself that it's not the
314 // default one any longer
319 } // end of wxButton::UpdateDefaultStyle
321 // ----------------------------------------------------------------------------
322 // event/message handlers
323 // ----------------------------------------------------------------------------
325 bool wxButton::OS2Command(WXUINT uParam
, WXWORD
WXUNUSED(wId
))
327 bool bProcessed
= false;
331 case BN_CLICKED
: // normal buttons send this
332 case BN_DBLCLICKED
: // owner-drawn ones also send this
333 bProcessed
= SendClickEvent();
338 } // end of wxButton::OS2Command
340 WXHBRUSH
wxButton::OnCtlColor( WXHDC
WXUNUSED(pDC
),
341 WXHWND
WXUNUSED(pWnd
),
342 WXUINT
WXUNUSED(nCtlColor
),
343 WXUINT
WXUNUSED(uMessage
),
344 WXWPARAM
WXUNUSED(wParam
),
345 WXLPARAM
WXUNUSED(lParam
) )
347 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
351 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
352 } // end of wxButton::OnCtlColor
354 void wxButton::MakeOwnerDrawn()
358 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
359 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
364 lStyle
|= BS_USERBUTTON
;
365 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
367 } // end of wxButton::MakeOwnerDrawn
369 WXDWORD
wxButton::OS2GetStyle(
371 , WXDWORD
* pdwExstyle
375 // Buttons never have an external border, they draw their own one
377 WXDWORD dwStyle
= wxControl::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
382 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
383 // each other in any resizable dialog which has more than one button in
386 dwStyle
|= WS_CLIPSIBLINGS
;
388 } // end of wxButton::OS2GetStyle
390 MRESULT
wxButton::WindowProc( WXUINT uMsg
,
395 // When we receive focus, we want to temporary become the default button in
396 // our parent panel so that pressing "Enter" would activate us -- and when
397 // losing it we should restore the previous default button as well
399 if (uMsg
== WM_SETFOCUS
)
401 if (SHORT1FROMMP(lParam
) == TRUE
)
407 // Let the default processign take place too
411 else if (uMsg
== WM_BUTTON1DBLCLK
)
414 // Emulate a click event to force an owner-drawn button to change its
415 // appearance - without this, it won't do it
417 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
423 // And conitnue with processing the message normally as well
428 // Let the base class do all real processing
430 return (wxControl::OS2WindowProc( uMsg
434 } // end of wxWindowProc