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
.GetFontType(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
;
129 void wxButton::SetDefault()
131 wxWindow
*parent
= GetParent();
133 parent
->SetDefaultItem(this);
135 // We initially do not set XmNdefaultShadowThickness, to have
136 // small buttons. Unfortunately, buttons are now mis-aligned. We
137 // try to correct this now -- setting this ressource to 1 for each
138 // button in the same row. Because it's very hard to find
139 // wxButton in the same row, correction is straighforward: we set
140 // resource for all wxButton in this parent (but not sub panels)
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::OldGetBestSize() const
187 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
189 XtVaGetValues( (Widget
)m_mainWidget
,
190 XmNmarginWidth
, &xmargin
,
191 XmNmarginHeight
, &ymargin
,
192 XmNhighlightThickness
, &highlight
,
193 XmNshadowThickness
, &shadow
,
194 XmNdefaultButtonShadowThickness
, &defThickness
,
197 int x
= 0; int y
= 0;
198 GetTextExtent( GetLabel(), &x
, &y
);
200 int margin
= highlight
* 2 +
201 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
202 wxSize
best( x
+ xmargin
* 2 + margin
,
203 y
+ ymargin
* 2 + margin
);
205 // all buttons have at least the standard size unless the user explicitly
206 // wants them to be of smaller size and used wxBU_EXACTFIT style when
207 // creating the button
208 if( !HasFlag( wxBU_EXACTFIT
) )
210 wxSize def
= GetDefaultSize();
211 int margin
= highlight
* 2 +
212 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
225 void wxButton::Command (wxCommandEvent
& event
)
227 ProcessCommand (event
);
230 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
232 if (!wxGetWindowFromTable(w
))
233 // Widget has been deleted!
236 wxButton
*item
= (wxButton
*) clientData
;
237 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
238 event
.SetEventObject(item
);
239 item
->ProcessCommand (event
);