1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/button.cpp
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
19 #include "wx/button.h"
22 #pragma message disable nosimpint
24 #include <Xm/PushBG.h>
27 #pragma message enable nosimpint
32 #include "wx/toplevel.h"
35 #include "wx/stockitem.h"
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
& lbl
,
50 const wxSize
& size
, long style
,
51 const wxValidator
& validator
,
55 if (label
.empty() && wxIsStockID(id
))
56 label
= wxGetStockLabel(id
);
58 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
62 wxXmString
text( GetLabelText(label
) );
64 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
67 * Patch Note (important)
68 * There is no major reason to put a defaultButtonThickness here.
69 * Not requesting it give the ability to put wxButton with a spacing
70 * as small as requested. However, if some button become a DefaultButton,
71 * other buttons are no more aligned -- This is why we set
72 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
73 * in the ::SetDefaultButton method.
75 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
76 xmPushButtonWidgetClass
,
78 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
79 XmNlabelString
, text(),
80 XmNrecomputeSize
, False
,
81 // See comment for wxButton::SetDefault
82 // XmNdefaultButtonShadowThickness, 1,
85 XtAddCallback ((Widget
) m_mainWidget
,
86 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
89 wxSize best
= GetBestSize();
90 if( size
.x
!= -1 ) best
.x
= size
.x
;
91 if( size
.y
!= -1 ) best
.y
= size
.y
;
94 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
95 pos
.x
, pos
.y
, best
.x
, best
.y
);
100 void wxButton::SetDefaultShadowThicknessAndResize()
102 Widget buttonWidget
= (Widget
)GetMainWidget();
103 bool managed
= XtIsManaged( buttonWidget
);
105 XtUnmanageChild( buttonWidget
);
107 XtVaSetValues( buttonWidget
,
108 XmNdefaultButtonShadowThickness
, 1,
112 XtManageChild( buttonWidget
);
114 // this can't currently be done, because user code that calls SetDefault
115 // will break otherwise
117 wxSize best
= GetBestSize(), actual
= GetSize();
118 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
119 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
124 InvalidateBestSize();
128 void wxButton::SetDefault()
130 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
132 tlw
->SetDefaultItem(this);
134 // We initially do not set XmNdefaultShadowThickness, to have
135 // small buttons. Unfortunately, buttons are now mis-aligned. We
136 // try to correct this now -- setting this ressource to 1 for each
137 // button in the same row. Because it's very hard to find
138 // wxButton in the same row, correction is straighforward: we set
139 // resource for all wxButton in this parent (but not sub panels)
141 wxWindow
*parent
= GetParent();
142 for (wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst ();
143 node
; node
= node
->GetNext ())
145 wxWindow
*win
= node
->GetData ();
146 wxButton
*item
= wxDynamicCast(win
, wxButton
);
148 item
->SetDefaultShadowThicknessAndResize();
151 XtVaSetValues ((Widget
) parent
->GetMainWidget(),
152 XmNdefaultButton
, (Widget
) GetMainWidget(),
156 static inline bool wxMotifLargeButtons()
158 return wxSystemOptions::HasOption("motif.largebuttons")
159 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
163 wxSize
wxButton::GetDefaultSize()
165 // TODO: check font size as in wxMSW ? MB
166 // Note: this is the button size (text + margin + shadow + defaultBorder)
167 return wxSize( MIN_WIDTH
, MIN_LARGE_HEIGHT
);
170 wxSize
wxButton::DoGetBestSize() const
172 if( wxMotifLargeButtons() )
173 return OldGetBestSize();
175 wxSize best
= wxControl::DoGetBestSize();
177 if( HasFlag( wxBU_EXACTFIT
) )
179 else if( best
.x
< MIN_WIDTH
)
185 wxSize
wxButton::GetMinSize() const
187 if( wxMotifLargeButtons() )
188 return OldGetMinSize();
190 return DoGetBestSize();
193 wxSize
wxButton::OldGetMinSize() const
195 return OldGetBestSize();
198 wxSize
wxButton::OldGetBestSize() const
200 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
202 XtVaGetValues( (Widget
)m_mainWidget
,
203 XmNmarginWidth
, &xmargin
,
204 XmNmarginHeight
, &ymargin
,
205 XmNhighlightThickness
, &highlight
,
206 XmNshadowThickness
, &shadow
,
207 XmNdefaultButtonShadowThickness
, &defThickness
,
210 int x
= 0; int y
= 0;
211 GetTextExtent( GetLabel(), &x
, &y
);
213 int margin
= highlight
* 2 +
214 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
216 wxSize
best( x
+ xmargin
* 2 + margin
,
217 y
+ ymargin
* 2 + margin
);
219 // all buttons have at least the standard size unless the user explicitly
220 // wants them to be of smaller size and used wxBU_EXACTFIT style when
221 // creating the button
222 if( !HasFlag( wxBU_EXACTFIT
) )
224 wxSize def
= GetDefaultSize();
225 int margin
= highlight
* 2 +
226 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
239 void wxButton::Command (wxCommandEvent
& event
)
241 ProcessCommand (event
);
244 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
246 if (!wxGetWindowFromTable(w
))
247 // Widget has been deleted!
250 wxButton
*item
= (wxButton
*) clientData
;
251 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
252 event
.SetEventObject(item
);
253 item
->ProcessCommand (event
);