merging back XTI branch part 2
[wxWidgets.git] / src / motif / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/button.cpp
3 // Purpose: wxButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/button.h"
16
17 #ifdef __VMS__
18 #pragma message disable nosimpint
19 #endif
20 #include <Xm/PushBG.h>
21 #include <Xm/PushB.h>
22 #ifdef __VMS__
23 #pragma message enable nosimpint
24 #endif
25
26
27 #ifndef WX_PRECOMP
28 #include "wx/toplevel.h"
29 #endif
30
31 #include "wx/stockitem.h"
32 #include "wx/motif/private.h"
33 #include "wx/sysopt.h"
34
35 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
36
37 #define MIN_WIDTH 78
38 #define MIN_LARGE_HEIGHT 30
39
40 // Button
41
42 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& lbl,
43 const wxPoint& pos,
44 const wxSize& size, long style,
45 const wxValidator& validator,
46 const wxString& name)
47 {
48 wxString label(lbl);
49 if (label.empty() && wxIsStockID(id))
50 label = wxGetStockLabel(id);
51
52 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
53 return false;
54 PreCreation();
55
56 wxXmString text( GetLabelText(label) );
57
58 Widget parentWidget = (Widget) parent->GetClientWidget();
59
60 /*
61 * Patch Note (important)
62 * There is no major reason to put a defaultButtonThickness here.
63 * Not requesting it give the ability to put wxButton with a spacing
64 * as small as requested. However, if some button become a DefaultButton,
65 * other buttons are no more aligned -- This is why we set
66 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
67 * in the ::SetDefaultButton method.
68 */
69 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
70 xmPushButtonWidgetClass,
71 parentWidget,
72 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
73 XmNlabelString, text(),
74 XmNrecomputeSize, False,
75 // See comment for wxButton::SetDefault
76 // XmNdefaultButtonShadowThickness, 1,
77 NULL);
78
79 XtAddCallback ((Widget) m_mainWidget,
80 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
81 (XtPointer) this);
82
83 wxSize best = GetBestSize();
84 if( size.x != -1 ) best.x = size.x;
85 if( size.y != -1 ) best.y = size.y;
86
87 PostCreation();
88 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
89 pos.x, pos.y, best.x, best.y);
90
91 return true;
92 }
93
94 void wxButton::SetDefaultShadowThicknessAndResize()
95 {
96 Widget buttonWidget = (Widget)GetMainWidget();
97 bool managed = XtIsManaged( buttonWidget );
98 if( managed )
99 XtUnmanageChild( buttonWidget );
100
101 XtVaSetValues( buttonWidget,
102 XmNdefaultButtonShadowThickness, 1,
103 NULL );
104
105 if( managed )
106 XtManageChild( buttonWidget );
107
108 // this can't currently be done, because user code that calls SetDefault
109 // will break otherwise
110 #if 0
111 wxSize best = GetBestSize(), actual = GetSize();
112 if( best.x < actual.x ) best.x = actual.x;
113 if( best.y < actual.y ) best.y = actual.y;
114
115 if( best != actual )
116 SetSize( best );
117 #endif
118 InvalidateBestSize();
119 }
120
121
122 wxWindow *wxButton::SetDefault()
123 {
124 wxWindow *oldDefault = wxButtonBase::SetDefault();
125
126 // We initially do not set XmNdefaultShadowThickness, to have
127 // small buttons. Unfortunately, buttons are now mis-aligned. We
128 // try to correct this now -- setting this ressource to 1 for each
129 // button in the same row. Because it's very hard to find
130 // wxButton in the same row, correction is straighforward: we set
131 // resource for all wxButton in this parent (but not sub panels)
132
133 wxWindow *parent = GetParent();
134 for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst ();
135 node; node = node->GetNext ())
136 {
137 wxWindow *win = node->GetData ();
138 wxButton *item = wxDynamicCast(win, wxButton);
139 if (item)
140 item->SetDefaultShadowThicknessAndResize();
141 }
142
143 XtVaSetValues ((Widget) parent->GetMainWidget(),
144 XmNdefaultButton, (Widget) GetMainWidget(),
145 NULL);
146
147 return oldDefault;
148 }
149
150 static inline bool wxMotifLargeButtons()
151 {
152 return wxSystemOptions::HasOption("motif.largebuttons")
153 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
154 }
155
156 /* static */
157 wxSize wxButton::GetDefaultSize()
158 {
159 // TODO: check font size as in wxMSW ? MB
160 // Note: this is the button size (text + margin + shadow + defaultBorder)
161 return wxSize( MIN_WIDTH, MIN_LARGE_HEIGHT );
162 }
163
164 wxSize wxButton::DoGetBestSize() const
165 {
166 if( wxMotifLargeButtons() )
167 return OldGetBestSize();
168
169 wxSize best = wxControl::DoGetBestSize();
170
171 if( HasFlag( wxBU_EXACTFIT ) )
172 return best;
173 else if( best.x < MIN_WIDTH )
174 best.x = MIN_WIDTH;
175
176 return best;
177 }
178
179 wxSize wxButton::GetMinSize() const
180 {
181 if( wxMotifLargeButtons() )
182 return OldGetMinSize();
183
184 return DoGetBestSize();
185 }
186
187 wxSize wxButton::OldGetMinSize() const
188 {
189 return OldGetBestSize();
190 }
191
192 wxSize wxButton::OldGetBestSize() const
193 {
194 Dimension xmargin, ymargin, highlight, shadow, defThickness;
195
196 XtVaGetValues( (Widget)m_mainWidget,
197 XmNmarginWidth, &xmargin,
198 XmNmarginHeight, &ymargin,
199 XmNhighlightThickness, &highlight,
200 XmNshadowThickness, &shadow,
201 XmNdefaultButtonShadowThickness, &defThickness,
202 NULL );
203
204 int x = 0; int y = 0;
205 GetTextExtent( GetLabel(), &x, &y );
206
207 int margin = highlight * 2 +
208 ( defThickness ? ( ( shadow + defThickness ) * 4 ) : ( shadow * 2 ) );
209
210 wxSize best( x + xmargin * 2 + margin,
211 y + ymargin * 2 + margin );
212
213 // all buttons have at least the standard size unless the user explicitly
214 // wants them to be of smaller size and used wxBU_EXACTFIT style when
215 // creating the button
216 if( !HasFlag( wxBU_EXACTFIT ) )
217 {
218 wxSize def = GetDefaultSize();
219 int margin = highlight * 2 +
220 ( defThickness ? ( shadow * 4 + defThickness * 4 ) : 0 );
221 def.x += margin;
222 def.y += margin;
223
224 if( def.x > best.x )
225 best.x = def.x;
226 if( def.y > best.y )
227 best.y = def.y;
228 }
229
230 return best;
231 }
232
233 void wxButton::Command (wxCommandEvent & event)
234 {
235 ProcessCommand (event);
236 }
237
238 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
239 {
240 if (!wxGetWindowFromTable(w))
241 // Widget has been deleted!
242 return;
243
244 wxButton *item = (wxButton *) clientData;
245 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
246 event.SetEventObject(item);
247 item->ProcessCommand (event);
248 }