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