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"
17 #define XtDisplay XTDISPLAY
22 #include "wx/button.h"
25 #pragma message disable nosimpint
27 #include <Xm/PushBG.h>
30 #pragma message enable nosimpint
33 #include "wx/motif/private.h"
35 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
37 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
41 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
43 const wxSize
& size
, long style
,
44 const wxValidator
& validator
,
47 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
50 wxString
label1(wxStripMenuCodes(label
));
51 wxXmString
text( label1
);
53 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
56 * Patch Note (important)
57 * There is no major reason to put a defaultButtonThickness here.
58 * Not requesting it give the ability to put wxButton with a spacing
59 * as small as requested. However, if some button become a DefaultButton,
60 * other buttons are no more aligned -- This is why we set
61 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
62 * in the ::SetDefaultButton method.
64 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
65 xmPushButtonWidgetClass
,
67 wxFont::GetFontTag(), m_font
.GetFontType(XtDisplay(parentWidget
)),
68 XmNlabelString
, text(),
69 XmNrecomputeSize
, False
,
70 // See comment for wxButton::SetDefault
71 // XmNdefaultButtonShadowThickness, 1,
74 XtAddCallback ((Widget
) m_mainWidget
,
75 XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
78 wxSize best
= GetBestSize();
79 if( size
.x
!= -1 ) best
.x
= size
.x
;
80 if( size
.y
!= -1 ) best
.y
= size
.y
;
82 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
83 pos
.x
, pos
.y
, best
.x
, best
.y
);
85 ChangeBackgroundColour();
90 void wxButton::SetDefaultShadowThicknessAndResize()
92 Widget buttonWidget
= (Widget
)GetMainWidget();
93 bool managed
= XtIsManaged( buttonWidget
);
95 XtUnmanageChild( buttonWidget
);
97 XtVaSetValues( buttonWidget
,
98 XmNdefaultButtonShadowThickness
, 1,
102 XtManageChild( buttonWidget
);
104 // this can't currently be done, because user code that calls SetDefault
105 // will break otherwise
107 wxSize best
= GetBestSize(), actual
= GetSize();
108 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
109 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
117 void wxButton::SetDefault()
119 wxWindow
*parent
= GetParent();
121 parent
->SetDefaultItem(this);
123 // We initially do not set XmNdefaultShadowThickness, to have
124 // small buttons. Unfortunately, buttons are now mis-aligned. We
125 // try to correct this now -- setting this ressource to 1 for each
126 // button in the same row. Because it's very hard to find
127 // wxButton in the same row, correction is straighforward: we set
128 // resource for all wxButton in this parent (but not sub panels)
130 for (wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst ();
131 node
; node
= node
->GetNext ())
133 wxWindow
*win
= node
->GetData ();
134 wxButton
*item
= wxDynamicCast(win
, wxButton
);
136 item
->SetDefaultShadowThicknessAndResize();
139 XtVaSetValues ((Widget
) parent
->GetMainWidget(),
140 XmNdefaultButton
, (Widget
) GetMainWidget(),
145 wxSize
wxButton::GetDefaultSize()
147 // TODO: check font size as in wxMSW ? MB
148 // Note: this is the button size (text + margin + shadow + defaultBorder)
149 return wxSize(78,30);
152 wxSize
wxButton::DoGetBestSize() const
154 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
156 XtVaGetValues( (Widget
)m_mainWidget
,
157 XmNmarginWidth
, &xmargin
,
158 XmNmarginHeight
, &ymargin
,
159 XmNhighlightThickness
, &highlight
,
160 XmNshadowThickness
, &shadow
,
161 XmNdefaultButtonShadowThickness
, &defThickness
,
164 int x
= 0; int y
= 0;
165 GetTextExtent( GetLabel(), &x
, &y
);
167 int margin
= highlight
* 2 +
168 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
169 wxSize
best( x
+ xmargin
* 2 + margin
,
170 y
+ ymargin
* 2 + margin
);
172 // all buttons have at least the standard size unless the user explicitly
173 // wants them to be of smaller size and used wxBU_EXACTFIT style when
174 // creating the button
175 if( !HasFlag( wxBU_EXACTFIT
) )
177 wxSize def
= GetDefaultSize();
178 int margin
= highlight
* 2 +
179 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
192 void wxButton::Command (wxCommandEvent
& event
)
194 ProcessCommand (event
);
197 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
199 if (!wxGetWindowFromTable(w
))
200 // Widget has been deleted!
203 wxButton
*item
= (wxButton
*) clientData
;
204 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
205 event
.SetEventObject(item
);
206 item
->ProcessCommand (event
);