]> git.saurik.com Git - wxWidgets.git/blob - src/motif/button.cpp
don't reset the selection after event was vetoed if there is no old selection
[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
61 wxXmString text( GetLabelText(label) );
62
63 Widget parentWidget = (Widget) parent->GetClientWidget();
64
65 /*
66 * Patch Note (important)
67 * There is no major reason to put a defaultButtonThickness here.
68 * Not requesting it give the ability to put wxButton with a spacing
69 * as small as requested. However, if some button become a DefaultButton,
70 * other buttons are no more aligned -- This is why we set
71 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
72 * in the ::SetDefaultButton method.
73 */
74 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
75 xmPushButtonWidgetClass,
76 parentWidget,
77 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
78 XmNlabelString, text(),
79 XmNrecomputeSize, False,
80 // See comment for wxButton::SetDefault
81 // XmNdefaultButtonShadowThickness, 1,
82 NULL);
83
84 XtAddCallback ((Widget) m_mainWidget,
85 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
86 (XtPointer) this);
87
88 wxSize best = GetBestSize();
89 if( size.x != -1 ) best.x = size.x;
90 if( size.y != -1 ) best.y = size.y;
91
92 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
93 pos.x, pos.y, best.x, best.y);
94
95 ChangeBackgroundColour();
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 void wxButton::SetDefault()
129 {
130 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
131 if ( tlw )
132 tlw->SetDefaultItem(this);
133
134 // We initially do not set XmNdefaultShadowThickness, to have
135 // small buttons. Unfortunately, buttons are now mis-aligned. We
136 // try to correct this now -- setting this ressource to 1 for each
137 // button in the same row. Because it's very hard to find
138 // wxButton in the same row, correction is straighforward: we set
139 // resource for all wxButton in this parent (but not sub panels)
140
141 wxWindow *parent = GetParent();
142 for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst ();
143 node; node = node->GetNext ())
144 {
145 wxWindow *win = node->GetData ();
146 wxButton *item = wxDynamicCast(win, wxButton);
147 if (item)
148 item->SetDefaultShadowThicknessAndResize();
149 }
150
151 XtVaSetValues ((Widget) parent->GetMainWidget(),
152 XmNdefaultButton, (Widget) GetMainWidget(),
153 NULL);
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 }