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
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
);
36 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
39 #define MIN_LARGE_HEIGHT 30
43 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& lbl
,
45 const wxSize
& size
, long style
,
46 const wxValidator
& validator
,
50 if (label
.empty() && wxIsStockID(id
))
51 label
= wxGetStockLabel(id
);
53 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
56 wxString
label1(wxStripMenuCodes(label
));
57 wxXmString
text( label1
);
59 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
62 * Patch Note (important)
63 * There is no major reason to put a defaultButtonThickness here.
64 * Not requesting it give the ability to put wxButton with a spacing
65 * as small as requested. However, if some button become a DefaultButton,
66 * other buttons are no more aligned -- This is why we set
67 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
68 * in the ::SetDefaultButton method.
70 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
71 xmPushButtonWidgetClass
,
73 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay(parentWidget
)),
74 XmNlabelString
, text(),
75 XmNrecomputeSize
, False
,
76 // See comment for wxButton::SetDefault
77 // XmNdefaultButtonShadowThickness, 1,
80 XtAddCallback ((Widget
) m_mainWidget
,
81 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
84 wxSize best
= GetBestSize();
85 if( size
.x
!= -1 ) best
.x
= size
.x
;
86 if( size
.y
!= -1 ) best
.y
= size
.y
;
88 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
89 pos
.x
, pos
.y
, best
.x
, best
.y
);
91 ChangeBackgroundColour();
96 void wxButton::SetDefaultShadowThicknessAndResize()
98 Widget buttonWidget
= (Widget
)GetMainWidget();
99 bool managed
= XtIsManaged( buttonWidget
);
101 XtUnmanageChild( buttonWidget
);
103 XtVaSetValues( buttonWidget
,
104 XmNdefaultButtonShadowThickness
, 1,
108 XtManageChild( buttonWidget
);
110 // this can't currently be done, because user code that calls SetDefault
111 // will break otherwise
113 wxSize best
= GetBestSize(), actual
= GetSize();
114 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
115 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
120 InvalidateBestSize();
124 void wxButton::SetDefault()
126 wxWindow
*parent
= GetParent();
128 parent
->SetDefaultItem(this);
130 // We initially do not set XmNdefaultShadowThickness, to have
131 // small buttons. Unfortunately, buttons are now mis-aligned. We
132 // try to correct this now -- setting this ressource to 1 for each
133 // button in the same row. Because it's very hard to find
134 // wxButton in the same row, correction is straighforward: we set
135 // resource for all wxButton in this parent (but not sub panels)
137 for (wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst ();
138 node
; node
= node
->GetNext ())
140 wxWindow
*win
= node
->GetData ();
141 wxButton
*item
= wxDynamicCast(win
, wxButton
);
143 item
->SetDefaultShadowThicknessAndResize();
146 XtVaSetValues ((Widget
) parent
->GetMainWidget(),
147 XmNdefaultButton
, (Widget
) GetMainWidget(),
151 static inline bool wxMotifLargeButtons()
153 return wxSystemOptions::HasOption("motif.largebuttons")
154 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
158 wxSize
wxButton::GetDefaultSize()
160 // TODO: check font size as in wxMSW ? MB
161 // Note: this is the button size (text + margin + shadow + defaultBorder)
162 return wxSize( MIN_WIDTH
, MIN_LARGE_HEIGHT
);
165 wxSize
wxButton::DoGetBestSize() const
167 if( wxMotifLargeButtons() )
168 return OldGetBestSize();
170 wxSize best
= wxControl::DoGetBestSize();
172 if( HasFlag( wxBU_EXACTFIT
) )
174 else if( best
.x
< MIN_WIDTH
)
180 wxSize
wxButton::GetMinSize() const
182 if( wxMotifLargeButtons() )
183 return OldGetMinSize();
185 return DoGetBestSize();
188 wxSize
wxButton::OldGetMinSize() const
190 return OldGetBestSize();
193 wxSize
wxButton::OldGetBestSize() const
195 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
197 XtVaGetValues( (Widget
)m_mainWidget
,
198 XmNmarginWidth
, &xmargin
,
199 XmNmarginHeight
, &ymargin
,
200 XmNhighlightThickness
, &highlight
,
201 XmNshadowThickness
, &shadow
,
202 XmNdefaultButtonShadowThickness
, &defThickness
,
205 int x
= 0; int y
= 0;
206 GetTextExtent( GetLabel(), &x
, &y
);
208 int margin
= highlight
* 2 +
209 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
211 wxSize
best( x
+ xmargin
* 2 + margin
,
212 y
+ ymargin
* 2 + margin
);
214 // all buttons have at least the standard size unless the user explicitly
215 // wants them to be of smaller size and used wxBU_EXACTFIT style when
216 // creating the button
217 if( !HasFlag( wxBU_EXACTFIT
) )
219 wxSize def
= GetDefaultSize();
220 int margin
= highlight
* 2 +
221 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
234 void wxButton::Command (wxCommandEvent
& event
)
236 ProcessCommand (event
);
239 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
241 if (!wxGetWindowFromTable(w
))
242 // Widget has been deleted!
245 wxButton
*item
= (wxButton
*) clientData
;
246 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
247 event
.SetEventObject(item
);
248 item
->ProcessCommand (event
);