1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "button.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
25 #include "wx/button.h"
28 #pragma message disable nosimpint
30 #include <Xm/PushBG.h>
33 #pragma message enable nosimpint
36 #include "wx/motif/private.h"
38 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
40 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
44 bool wxButton
::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
46 const wxSize
& size
, long style
,
47 const wxValidator
& validator
,
50 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
53 wxString
label1(wxStripMenuCodes(label
));
54 wxXmString
text( label1
);
56 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
59 * Patch Note (important)
60 * There is no major reason to put a defaultButtonThickness here.
61 * Not requesting it give the ability to put wxButton with a spacing
62 * as small as requested. However, if some button become a DefaultButton,
63 * other buttons are no more aligned -- This is why we set
64 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
65 * in the ::SetDefaultButton method.
67 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
68 xmPushButtonWidgetClass
,
70 wxFont
::GetFontTag(), m_font
.GetFontType(XtDisplay(parentWidget
)),
71 XmNlabelString
, text(),
72 XmNrecomputeSize
, False
,
73 // See comment for wxButton::SetDefault
74 // XmNdefaultButtonShadowThickness, 1,
77 XtAddCallback ((Widget
) m_mainWidget
,
78 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
81 wxSize best
= GetBestSize();
82 if( size
.x
!= -1 ) best
.x
= size
.x
;
83 if( size
.y
!= -1 ) best
.y
= size
.y
;
85 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
86 pos
.x
, pos
.y
, best
.x
, best
.y
);
88 ChangeBackgroundColour();
93 void wxButton
::SetDefaultShadowThicknessAndResize()
95 Widget buttonWidget
= (Widget
)GetMainWidget();
96 bool managed
= XtIsManaged( buttonWidget
);
98 XtUnmanageChild( buttonWidget
);
100 XtVaSetValues( buttonWidget
,
101 XmNdefaultButtonShadowThickness
, 1,
105 XtManageChild( buttonWidget
);
107 // this can't currently be done, because user code that calls SetDefault
108 // will break otherwise
110 wxSize best
= GetBestSize(), actual
= GetSize();
111 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
112 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
120 void wxButton
::SetDefault()
122 wxWindow
*parent
= GetParent();
124 parent
->SetDefaultItem(this);
126 // We initially do not set XmNdefaultShadowThickness, to have
127 // small buttons. Unfortunately, buttons are now mis-aligned. We
128 // try to correct this now -- setting this ressource to 1 for each
129 // button in the same row. Because it's very hard to find
130 // wxButton in the same row, correction is straighforward: we set
131 // resource for all wxButton in this parent (but not sub panels)
133 for (wxWindowList
::compatibility_iterator node
= parent
->GetChildren().GetFirst ();
134 node
; node
= node
->GetNext ())
136 wxWindow
*win
= node
->GetData ();
137 wxButton
*item
= wxDynamicCast(win
, wxButton
);
139 item
->SetDefaultShadowThicknessAndResize();
142 XtVaSetValues ((Widget
) parent
->GetMainWidget(),
143 XmNdefaultButton
, (Widget
) GetMainWidget(),
148 wxSize wxButton
::GetDefaultSize()
150 // TODO: check font size as in wxMSW ? MB
151 // Note: this is the button size (text + margin + shadow + defaultBorder)
152 return wxSize(78,30);
155 wxSize wxButton
::DoGetBestSize() const
157 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
159 XtVaGetValues( (Widget
)m_mainWidget
,
160 XmNmarginWidth
, &xmargin
,
161 XmNmarginHeight
, &ymargin
,
162 XmNhighlightThickness
, &highlight
,
163 XmNshadowThickness
, &shadow
,
164 XmNdefaultButtonShadowThickness
, &defThickness
,
167 int x
= 0; int y
= 0;
168 GetTextExtent( GetLabel(), &x
, &y
);
170 int margin
= highlight
* 2 +
171 ( defThickness ?
( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
172 wxSize
best( x
+ xmargin
* 2 + margin
,
173 y
+ ymargin
* 2 + margin
);
175 // all buttons have at least the standard size unless the user explicitly
176 // wants them to be of smaller size and used wxBU_EXACTFIT style when
177 // creating the button
178 if( !HasFlag( wxBU_EXACTFIT
) )
180 wxSize def
= GetDefaultSize();
181 int margin
= highlight
* 2 +
182 ( defThickness ?
( shadow
* 4 + defThickness
* 4 ) : 0 );
195 void wxButton
::Command (wxCommandEvent
& event
)
197 ProcessCommand (event
);
200 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
202 if (!wxGetWindowFromTable(w
))
203 // Widget has been deleted!
206 wxButton
*item
= (wxButton
*) clientData
;
207 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
208 event
.SetEventObject(item
);
209 item
->ProcessCommand (event
);