1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XtDisplay XTDISPLAY
21 #include "wx/button.h"
24 #pragma message disable nosimpint
26 #include <Xm/PushBG.h>
29 #pragma message enable nosimpint
32 #include "wx/stockitem.h"
33 #include "wx/motif/private.h"
34 #include "wx/sysopt.h"
36 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
38 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
41 #define MIN_LARGE_HEIGHT 30
45 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& lbl
,
47 const wxSize
& size
, long style
,
48 const wxValidator
& validator
,
52 if (label
.empty() && wxIsStockID(id
))
53 label
= wxGetStockLabel(id
);
55 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
58 wxString
label1(wxStripMenuCodes(label
));
59 wxXmString
text( label1
);
61 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
64 * Patch Note (important)
65 * There is no major reason to put a defaultButtonThickness here.
66 * Not requesting it give the ability to put wxButton with a spacing
67 * as small as requested. However, if some button become a DefaultButton,
68 * other buttons are no more aligned -- This is why we set
69 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
70 * in the ::SetDefaultButton method.
72 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
73 xmPushButtonWidgetClass
,
75 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
76 XmNlabelString
, text(),
77 XmNrecomputeSize
, False
,
78 // See comment for wxButton::SetDefault
79 // XmNdefaultButtonShadowThickness, 1,
82 XtAddCallback ((Widget
) m_mainWidget
,
83 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
86 wxSize best
= GetBestSize();
87 if( size
.x
!= -1 ) best
.x
= size
.x
;
88 if( size
.y
!= -1 ) best
.y
= size
.y
;
90 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
91 pos
.x
, pos
.y
, best
.x
, best
.y
);
93 ChangeBackgroundColour();
98 void wxButton::SetDefaultShadowThicknessAndResize()
100 Widget buttonWidget
= (Widget
)GetMainWidget();
101 bool managed
= XtIsManaged( buttonWidget
);
103 XtUnmanageChild( buttonWidget
);
105 XtVaSetValues( buttonWidget
,
106 XmNdefaultButtonShadowThickness
, 1,
110 XtManageChild( buttonWidget
);
112 // this can't currently be done, because user code that calls SetDefault
113 // will break otherwise
115 wxSize best
= GetBestSize(), actual
= GetSize();
116 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
117 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
122 InvalidateBestSize();
126 void wxButton::SetDefault()
128 wxWindow
*parent
= GetParent();
130 parent
->SetDefaultItem(this);
132 // We initially do not set XmNdefaultShadowThickness, to have
133 // small buttons. Unfortunately, buttons are now mis-aligned. We
134 // try to correct this now -- setting this ressource to 1 for each
135 // button in the same row. Because it's very hard to find
136 // wxButton in the same row, correction is straighforward: we set
137 // resource for all wxButton in this parent (but not sub panels)
139 for (wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst ();
140 node
; node
= node
->GetNext ())
142 wxWindow
*win
= node
->GetData ();
143 wxButton
*item
= wxDynamicCast(win
, wxButton
);
145 item
->SetDefaultShadowThicknessAndResize();
148 XtVaSetValues ((Widget
) parent
->GetMainWidget(),
149 XmNdefaultButton
, (Widget
) GetMainWidget(),
153 static inline bool wxMotifLargeButtons()
155 return wxSystemOptions::HasOption("motif.largebuttons")
156 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
160 wxSize
wxButton::GetDefaultSize()
162 // TODO: check font size as in wxMSW ? MB
163 // Note: this is the button size (text + margin + shadow + defaultBorder)
164 return wxSize( MIN_WIDTH
, MIN_LARGE_HEIGHT
);
167 wxSize
wxButton::DoGetBestSize() const
169 if( wxMotifLargeButtons() )
170 return OldGetBestSize();
172 wxSize best
= wxControl::DoGetBestSize();
174 if( HasFlag( wxBU_EXACTFIT
) )
176 else if( best
.x
< MIN_WIDTH
)
182 wxSize
wxButton::GetMinSize() const
184 if( wxMotifLargeButtons() )
185 return OldGetMinSize();
187 return DoGetBestSize();
190 wxSize
wxButton::OldGetMinSize() const
192 return OldGetBestSize();
195 wxSize
wxButton::OldGetBestSize() const
197 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
199 XtVaGetValues( (Widget
)m_mainWidget
,
200 XmNmarginWidth
, &xmargin
,
201 XmNmarginHeight
, &ymargin
,
202 XmNhighlightThickness
, &highlight
,
203 XmNshadowThickness
, &shadow
,
204 XmNdefaultButtonShadowThickness
, &defThickness
,
207 int x
= 0; int y
= 0;
208 GetTextExtent( GetLabel(), &x
, &y
);
210 int margin
= highlight
* 2 +
211 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
213 wxSize
best( x
+ xmargin
* 2 + margin
,
214 y
+ ymargin
* 2 + margin
);
216 // all buttons have at least the standard size unless the user explicitly
217 // wants them to be of smaller size and used wxBU_EXACTFIT style when
218 // creating the button
219 if( !HasFlag( wxBU_EXACTFIT
) )
221 wxSize def
= GetDefaultSize();
222 int margin
= highlight
* 2 +
223 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
236 void wxButton::Command (wxCommandEvent
& event
)
238 ProcessCommand (event
);
241 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
243 if (!wxGetWindowFromTable(w
))
244 // Widget has been deleted!
247 wxButton
*item
= (wxButton
*) clientData
;
248 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
249 event
.SetEventObject(item
);
250 item
->ProcessCommand (event
);