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