1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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 SetCanAddEventHandler(TRUE
);
80 wxSize best
= GetBestSize();
81 if( size
.x
!= -1 ) best
.x
= size
.x
;
82 if( size
.y
!= -1 ) best
.y
= size
.y
;
84 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
85 pos
.x
, pos
.y
, best
.x
, best
.y
);
87 ChangeBackgroundColour();
92 void wxButton::SetDefaultShadowThicknessAndResize()
94 Widget buttonWidget
= (Widget
)GetMainWidget();
95 bool managed
= XtIsManaged( buttonWidget
);
97 XtUnmanageChild( buttonWidget
);
99 XtVaSetValues( buttonWidget
,
100 XmNdefaultButtonShadowThickness
, 1,
104 XtManageChild( buttonWidget
);
106 // this can't currently be done, because user code that calls SetDefault
107 // will break otherwise
109 wxSize best
= GetBestSize(), actual
= GetSize();
110 if( best
.x
< actual
.x
) best
.x
= actual
.x
;
111 if( best
.y
< actual
.y
) best
.y
= actual
.y
;
119 void wxButton::SetDefault()
121 wxWindow
*parent
= GetParent();
123 parent
->SetDefaultItem(this);
125 // We initially do not set XmNdefaultShadowThickness, to have
126 // small buttons. Unfortunately, buttons are now mis-aligned. We
127 // try to correct this now -- setting this ressource to 1 for each
128 // button in the same row. Because it's very hard to find
129 // wxButton in the same row, correction is straighforward: we set
130 // resource for all wxButton in this parent (but not sub panels)
132 for (wxWindowList::Node
* node
= parent
->GetChildren().GetFirst ();
133 node
; node
= node
->GetNext ())
135 wxWindow
*win
= node
->GetData ();
136 wxButton
*item
= wxDynamicCast(win
, wxButton
);
138 item
->SetDefaultShadowThicknessAndResize();
141 XtVaSetValues ((Widget
) parent
->GetMainWidget(),
142 XmNdefaultButton
, (Widget
) GetMainWidget(),
147 wxSize
wxButton::GetDefaultSize()
149 // TODO: check font size as in wxMSW ? MB
150 // Note: this is the button size (text + margin + shadow + defaultBorder)
151 return wxSize(78,30);
154 wxSize
wxButton::DoGetBestSize() const
156 Dimension xmargin
, ymargin
, highlight
, shadow
, defThickness
;
158 XtVaGetValues( (Widget
)m_mainWidget
,
159 XmNmarginWidth
, &xmargin
,
160 XmNmarginHeight
, &ymargin
,
161 XmNhighlightThickness
, &highlight
,
162 XmNshadowThickness
, &shadow
,
163 XmNdefaultButtonShadowThickness
, &defThickness
,
166 int x
= 0; int y
= 0;
167 GetTextExtent( GetLabel(), &x
, &y
);
169 int margin
= highlight
* 2 +
170 ( defThickness
? ( ( shadow
+ defThickness
) * 4 ) : ( shadow
* 2 ) );
171 wxSize
best( x
+ xmargin
* 2 + margin
,
172 y
+ ymargin
* 2 + margin
);
174 // all buttons have at least the standard size unless the user explicitly
175 // wants them to be of smaller size and used wxBU_EXACTFIT style when
176 // creating the button
177 if( !HasFlag( wxBU_EXACTFIT
) )
179 wxSize def
= GetDefaultSize();
180 int margin
= highlight
* 2 +
181 ( defThickness
? ( shadow
* 4 + defThickness
* 4 ) : 0 );
194 void wxButton::Command (wxCommandEvent
& event
)
196 ProcessCommand (event
);
199 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
201 if (!wxGetWindowFromTable(w
))
202 // Widget has been deleted!
205 wxButton
*item
= (wxButton
*) clientData
;
206 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
207 event
.SetEventObject(item
);
208 item
->ProcessCommand (event
);