1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/button.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/button.h"
17 #pragma message disable nosimpint
19 #include <Xm/PushBG.h>
22 #pragma message enable nosimpint
27 #include "wx/toplevel.h"
30 #include "wx/stockitem.h"
31 #include "wx/motif/private.h"
32 #include "wx/sysopt.h"
34 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
37 #define MIN_LARGE_HEIGHT 30
41 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& lbl
,
43 const wxSize
& size
, long style
,
44 const wxValidator
& validator
,
48 if (label
.empty() && wxIsStockID(id
))
49 label
= wxGetStockLabel(id
);
51 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
55 wxXmString
text( GetLabelText(label
) );
57 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
60 * Patch Note (important)
61 * There is no major reason to put a defaultButtonThickness here.
62 * Not requesting it give the ability to put wxButton with a spacing
63 * as small as requested. However, if some button become a DefaultButton,
64 * other buttons are no more aligned -- This is why we set
65 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
66 * in the ::SetDefaultButton method.
68 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
69 xmPushButtonWidgetClass
,
71 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
72 XmNlabelString
, text(),
73 XmNrecomputeSize
, False
,
74 // See comment for wxButton::SetDefault
75 // XmNdefaultButtonShadowThickness, 1,
78 XtAddCallback ((Widget
) m_mainWidget
,
79 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
82 wxSize best
= GetBestSize();
83 if( size
.x
!= -1 ) best
.x
= size
.x
;
84 if( size
.y
!= -1 ) best
.y
= size
.y
;
87 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
88 pos
.x
, pos
.y
, best
.x
, best
.y
);
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
;
117 InvalidateBestSize();
121 wxWindow
*wxButton::SetDefault()
123 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
125 // We initially do not set XmNdefaultShadowThickness, to have
126 // small buttons. Unfortunately, buttons are now mis-aligned. We
127 // try to correct this now -- setting this ressource to 1 for each
128 // button in the same row. Because it's very hard to find
129 // wxButton in the same row, correction is straighforward: we set
130 // resource for all wxButton in this parent (but not sub panels)
132 wxWindow
*parent
= GetParent();
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(),
149 static inline bool wxMotifLargeButtons()
151 return wxSystemOptions::HasOption("motif.largebuttons")
152 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
156 wxSize
wxButton::GetDefaultSize()
158 // TODO: check font size as in wxMSW ? MB
159 // Note: this is the button size (text + margin + shadow + defaultBorder)
160 return wxSize( MIN_WIDTH
, MIN_LARGE_HEIGHT
);
163 wxSize
wxButton::DoGetBestSize() const
165 if( wxMotifLargeButtons() )
166 return OldGetBestSize();
168 wxSize best
= wxControl::DoGetBestSize();
170 if( HasFlag( wxBU_EXACTFIT
) )
172 else if( best
.x
< MIN_WIDTH
)
178 wxSize
wxButton::GetMinSize() const
180 if( wxMotifLargeButtons() )
181 return OldGetMinSize();
183 return DoGetBestSize();
186 wxSize
wxButton::OldGetMinSize() const
188 return OldGetBestSize();
191 wxSize
wxButton::OldGetBestSize() const
193 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
195 XtVaGetValues( (Widget
)m_mainWidget
,
196 XmNmarginWidth
, &xmargin
,
197 XmNmarginHeight
, &ymargin
,
198 XmNhighlightThickness
, &highlight
,
199 XmNshadowThickness
, &shadow
,
200 XmNdefaultButtonShadowThickness
, &defThickness
,
203 int x
= 0; int y
= 0;
204 GetTextExtent( GetLabel(), &x
, &y
);
206 int margin
= highlight
* 2 +
207 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
209 wxSize
best( x
+ xmargin
* 2 + margin
,
210 y
+ ymargin
* 2 + margin
);
212 // all buttons have at least the standard size unless the user explicitly
213 // wants them to be of smaller size and used wxBU_EXACTFIT style when
214 // creating the button
215 if( !HasFlag( wxBU_EXACTFIT
) )
217 wxSize def
= GetDefaultSize();
218 int margin
= highlight
* 2 +
219 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
232 void wxButton::Command (wxCommandEvent
& event
)
234 ProcessCommand (event
);
237 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
239 if (!wxGetWindowFromTable(w
))
240 // Widget has been deleted!
243 wxButton
*item
= (wxButton
*) clientData
;
244 wxCommandEvent
event (wxEVT_BUTTON
, item
->GetId());
245 event
.SetEventObject(item
);
246 item
->ProcessCommand (event
);