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
) )
61 wxString
label1(wxStripMenuCodes(label
));
62 wxXmString
text( label1
);
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
;
93 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
94 pos
.x
, pos
.y
, best
.x
, best
.y
);
96 ChangeBackgroundColour();
101 void wxButton::SetDefaultShadowThicknessAndResize()
103 Widget buttonWidget
= (Widget
)GetMainWidget();
104 bool managed
= XtIsManaged( buttonWidget
);
106 XtUnmanageChild( buttonWidget
);
108 XtVaSetValues( buttonWidget
,
109 XmNdefaultButtonShadowThickness
, 1,
113 XtManageChild( buttonWidget
);
115 // this can't currently be done, because user code that calls SetDefault
116 // will break otherwise
118 wxSize best
= GetBestSize(), actual
= GetSize();
119 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
120 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
125 InvalidateBestSize();
129 void wxButton::SetDefault()
131 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
133 tlw
->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 wxWindow
*parent
= GetParent();
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
);