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