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/stockitem.h"
37 #include "wx/motif/private.h"
38 #include "wx/sysopt.h"
40 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
42 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
45 #define MIN_LARGE_HEIGHT 30
49 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& lbl
,
51 const wxSize
& size
, long style
,
52 const wxValidator
& validator
,
56 if (label
.empty() && wxIsStockID(id
))
57 label
= wxGetStockLabel(id
);
59 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
62 wxString
label1(wxStripMenuCodes(label
));
63 wxXmString
text( label1
);
65 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
68 * Patch Note (important)
69 * There is no major reason to put a defaultButtonThickness here.
70 * Not requesting it give the ability to put wxButton with a spacing
71 * as small as requested. However, if some button become a DefaultButton,
72 * other buttons are no more aligned -- This is why we set
73 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
74 * in the ::SetDefaultButton method.
76 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
77 xmPushButtonWidgetClass
,
79 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
80 XmNlabelString
, text(),
81 XmNrecomputeSize
, False
,
82 // See comment for wxButton::SetDefault
83 // XmNdefaultButtonShadowThickness, 1,
86 XtAddCallback ((Widget
) m_mainWidget
,
87 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
90 wxSize best
= GetBestSize();
91 if( size
.x
!= -1 ) best
.x
= size
.x
;
92 if( size
.y
!= -1 ) best
.y
= size
.y
;
94 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
95 pos
.x
, pos
.y
, best
.x
, best
.y
);
97 ChangeBackgroundColour();
102 void wxButton::SetDefaultShadowThicknessAndResize()
104 Widget buttonWidget
= (Widget
)GetMainWidget();
105 bool managed
= XtIsManaged( buttonWidget
);
107 XtUnmanageChild( buttonWidget
);
109 XtVaSetValues( buttonWidget
,
110 XmNdefaultButtonShadowThickness
, 1,
114 XtManageChild( buttonWidget
);
116 // this can't currently be done, because user code that calls SetDefault
117 // will break otherwise
119 wxSize best
= GetBestSize(), actual
= GetSize();
120 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
121 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
126 InvalidateBestSize();
130 void wxButton::SetDefault()
132 wxWindow
*parent
= GetParent();
134 parent
->SetDefaultItem(this);
136 // We initially do not set XmNdefaultShadowThickness, to have
137 // small buttons. Unfortunately, buttons are now mis-aligned. We
138 // try to correct this now -- setting this ressource to 1 for each
139 // button in the same row. Because it's very hard to find
140 // wxButton in the same row, correction is straighforward: we set
141 // resource for all wxButton in this parent (but not sub panels)
143 for (wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst ();
144 node
; node
= node
->GetNext ())
146 wxWindow
*win
= node
->GetData ();
147 wxButton
*item
= wxDynamicCast(win
, wxButton
);
149 item
->SetDefaultShadowThicknessAndResize();
152 XtVaSetValues ((Widget
) parent
->GetMainWidget(),
153 XmNdefaultButton
, (Widget
) GetMainWidget(),
157 static inline bool wxMotifLargeButtons()
159 return wxSystemOptions::HasOption("motif.largebuttons")
160 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
164 wxSize
wxButton::GetDefaultSize()
166 // TODO: check font size as in wxMSW ? MB
167 // Note: this is the button size (text + margin + shadow + defaultBorder)
168 return wxSize( MIN_WIDTH
, MIN_LARGE_HEIGHT
);
171 wxSize
wxButton::DoGetBestSize() const
173 if( wxMotifLargeButtons() )
174 return OldGetBestSize();
176 wxSize best
= wxControl::DoGetBestSize();
178 if( HasFlag( wxBU_EXACTFIT
) )
180 else if( best
.x
< MIN_WIDTH
)
186 wxSize
wxButton::GetMinSize() const
188 if( wxMotifLargeButtons() )
189 return OldGetMinSize();
191 return DoGetBestSize();
194 wxSize
wxButton::OldGetMinSize() const
196 return OldGetBestSize();
199 wxSize
wxButton::OldGetBestSize() const
201 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
203 XtVaGetValues( (Widget
)m_mainWidget
,
204 XmNmarginWidth
, &xmargin
,
205 XmNmarginHeight
, &ymargin
,
206 XmNhighlightThickness
, &highlight
,
207 XmNshadowThickness
, &shadow
,
208 XmNdefaultButtonShadowThickness
, &defThickness
,
211 int x
= 0; int y
= 0;
212 GetTextExtent( GetLabel(), &x
, &y
);
214 int margin
= highlight
* 2 +
215 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
217 wxSize
best( x
+ xmargin
* 2 + margin
,
218 y
+ ymargin
* 2 + margin
);
220 // all buttons have at least the standard size unless the user explicitly
221 // wants them to be of smaller size and used wxBU_EXACTFIT style when
222 // creating the button
223 if( !HasFlag( wxBU_EXACTFIT
) )
225 wxSize def
= GetDefaultSize();
226 int margin
= highlight
* 2 +
227 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
240 void wxButton::Command (wxCommandEvent
& event
)
242 ProcessCommand (event
);
245 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
247 if (!wxGetWindowFromTable(w
))
248 // Widget has been deleted!
251 wxButton
*item
= (wxButton
*) clientData
;
252 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
253 event
.SetEventObject(item
);
254 item
->ProcessCommand (event
);