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"
37 #include "wx/sysopt.h"
39 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
41 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
44 #define MIN_LARGE_HEIGHT 30
48 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
50 const wxSize
& size
, long style
,
51 const wxValidator
& validator
,
54 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
57 wxString
label1(wxStripMenuCodes(label
));
58 wxXmString
text( label1
);
60 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
63 * Patch Note (important)
64 * There is no major reason to put a defaultButtonThickness here.
65 * Not requesting it give the ability to put wxButton with a spacing
66 * as small as requested. However, if some button become a DefaultButton,
67 * other buttons are no more aligned -- This is why we set
68 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
69 * in the ::SetDefaultButton method.
71 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
72 xmPushButtonWidgetClass
,
74 wxFont::GetFontTag(), m_font
.GetFontType(XtDisplay(parentWidget
)),
75 XmNlabelString
, text(),
76 XmNrecomputeSize
, False
,
77 // See comment for wxButton::SetDefault
78 // XmNdefaultButtonShadowThickness, 1,
81 XtAddCallback ((Widget
) m_mainWidget
,
82 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
85 wxSize best
= GetBestSize();
86 if( size
.x
!= -1 ) best
.x
= size
.x
;
87 if( size
.y
!= -1 ) best
.y
= size
.y
;
89 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
90 pos
.x
, pos
.y
, best
.x
, best
.y
);
92 ChangeBackgroundColour();
97 void wxButton::SetDefaultShadowThicknessAndResize()
99 Widget buttonWidget
= (Widget
)GetMainWidget();
100 bool managed
= XtIsManaged( buttonWidget
);
102 XtUnmanageChild( buttonWidget
);
104 XtVaSetValues( buttonWidget
,
105 XmNdefaultButtonShadowThickness
, 1,
109 XtManageChild( buttonWidget
);
111 // this can't currently be done, because user code that calls SetDefault
112 // will break otherwise
114 wxSize best
= GetBestSize(), actual
= GetSize();
115 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
116 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
124 void wxButton::SetDefault()
126 wxWindow
*parent
= GetParent();
128 parent
->SetDefaultItem(this);
130 // We initially do not set XmNdefaultShadowThickness, to have
131 // small buttons. Unfortunately, buttons are now mis-aligned. We
132 // try to correct this now -- setting this ressource to 1 for each
133 // button in the same row. Because it's very hard to find
134 // wxButton in the same row, correction is straighforward: we set
135 // resource for all wxButton in this parent (but not sub panels)
137 for (wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst ();
138 node
; node
= node
->GetNext ())
140 wxWindow
*win
= node
->GetData ();
141 wxButton
*item
= wxDynamicCast(win
, wxButton
);
143 item
->SetDefaultShadowThicknessAndResize();
146 XtVaSetValues ((Widget
) parent
->GetMainWidget(),
147 XmNdefaultButton
, (Widget
) GetMainWidget(),
151 static inline bool wxMotifLargeButtons()
153 return wxSystemOptions::HasOption("motif.largebuttons")
154 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
158 wxSize
wxButton::GetDefaultSize()
160 // TODO: check font size as in wxMSW ? MB
161 // Note: this is the button size (text + margin + shadow + defaultBorder)
162 return wxSize( MIN_WIDTH
, MIN_LARGE_HEIGHT
);
165 wxSize
wxButton::DoGetBestSize() const
167 if( wxMotifLargeButtons() )
168 return OldGetBestSize();
170 wxSize best
= wxControl::DoGetBestSize();
172 if( HasFlag( wxBU_EXACTFIT
) )
174 else if( best
.x
< MIN_WIDTH
)
180 wxSize
wxButton::OldGetBestSize() const
182 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
184 XtVaGetValues( (Widget
)m_mainWidget
,
185 XmNmarginWidth
, &xmargin
,
186 XmNmarginHeight
, &ymargin
,
187 XmNhighlightThickness
, &highlight
,
188 XmNshadowThickness
, &shadow
,
189 XmNdefaultButtonShadowThickness
, &defThickness
,
192 int x
= 0; int y
= 0;
193 GetTextExtent( GetLabel(), &x
, &y
);
195 int margin
= highlight
* 2 +
196 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
197 wxSize
best( x
+ xmargin
* 2 + margin
,
198 y
+ ymargin
* 2 + margin
);
200 // all buttons have at least the standard size unless the user explicitly
201 // wants them to be of smaller size and used wxBU_EXACTFIT style when
202 // creating the button
203 if( !HasFlag( wxBU_EXACTFIT
) )
205 wxSize def
= GetDefaultSize();
206 int margin
= highlight
* 2 +
207 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
220 void wxButton::Command (wxCommandEvent
& event
)
222 ProcessCommand (event
);
225 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
227 if (!wxGetWindowFromTable(w
))
228 // Widget has been deleted!
231 wxButton
*item
= (wxButton
*) clientData
;
232 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
233 event
.SetEventObject(item
);
234 item
->ProcessCommand (event
);