]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4bb6408c JS |
13 | #pragma implementation "button.h" |
14 | #endif | |
15 | ||
1248b41f MB |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
bcd055ae JJ |
19 | #ifdef __VMS |
20 | #define XtDisplay XTDISPLAY | |
21 | #endif | |
22 | ||
f6045f99 GD |
23 | #include "wx/defs.h" |
24 | ||
4bb6408c JS |
25 | #include "wx/button.h" |
26 | ||
338dd992 JJ |
27 | #ifdef __VMS__ |
28 | #pragma message disable nosimpint | |
29 | #endif | |
02e8b2f9 JS |
30 | #include <Xm/PushBG.h> |
31 | #include <Xm/PushB.h> | |
338dd992 JJ |
32 | #ifdef __VMS__ |
33 | #pragma message enable nosimpint | |
34 | #endif | |
02e8b2f9 | 35 | |
5f7bcb48 | 36 | #include "wx/stockitem.h" |
02e8b2f9 | 37 | #include "wx/motif/private.h" |
e04b7e8e | 38 | #include "wx/sysopt.h" |
02e8b2f9 JS |
39 | |
40 | void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr); | |
41 | ||
4bb6408c | 42 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
4bb6408c | 43 | |
e04b7e8e MB |
44 | #define MIN_WIDTH 78 |
45 | #define MIN_LARGE_HEIGHT 30 | |
46 | ||
4bb6408c JS |
47 | // Button |
48 | ||
5f7bcb48 | 49 | bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& lbl, |
2d120f83 JS |
50 | const wxPoint& pos, |
51 | const wxSize& size, long style, | |
52 | const wxValidator& validator, | |
53 | const wxString& name) | |
4bb6408c | 54 | { |
5f7bcb48 VS |
55 | wxString label(lbl); |
56 | if (label.empty() && wxIsStockID(id)) | |
57 | label = wxGetStockLabel(id); | |
58 | ||
458ca8c1 MB |
59 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) |
60 | return false; | |
dfe1eee3 | 61 | |
89c7e962 | 62 | wxString label1(wxStripMenuCodes(label)); |
458ca8c1 | 63 | wxXmString text( label1 ); |
dfe1eee3 | 64 | |
02e8b2f9 | 65 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
dfe1eee3 | 66 | |
02e8b2f9 | 67 | /* |
2d120f83 JS |
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 | */ | |
02e8b2f9 | 76 | m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button", |
2d120f83 JS |
77 | xmPushButtonWidgetClass, |
78 | parentWidget, | |
73608949 | 79 | wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)), |
458ca8c1 | 80 | XmNlabelString, text(), |
84fb430b | 81 | XmNrecomputeSize, False, |
458ca8c1 MB |
82 | // See comment for wxButton::SetDefault |
83 | // XmNdefaultButtonShadowThickness, 1, | |
2d120f83 | 84 | NULL); |
dfe1eee3 | 85 | |
458ca8c1 MB |
86 | XtAddCallback ((Widget) m_mainWidget, |
87 | XmNactivateCallback, (XtCallbackProc) wxButtonCallback, | |
88 | (XtPointer) this); | |
dfe1eee3 | 89 | |
458ca8c1 MB |
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); | |
dfe1eee3 | 96 | |
0d57be45 | 97 | ChangeBackgroundColour(); |
dfe1eee3 | 98 | |
e04b7e8e | 99 | return true; |
4bb6408c JS |
100 | } |
101 | ||
458ca8c1 MB |
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 | ||
84fb430b MB |
116 | // this can't currently be done, because user code that calls SetDefault |
117 | // will break otherwise | |
118 | #if 0 | |
458ca8c1 MB |
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 ); | |
84fb430b | 125 | #endif |
4e025aa2 | 126 | InvalidateBestSize(); |
458ca8c1 MB |
127 | } |
128 | ||
129 | ||
4bb6408c JS |
130 | void wxButton::SetDefault() |
131 | { | |
dfe1eee3 | 132 | wxWindow *parent = GetParent(); |
b0ee47ff | 133 | if ( parent ) |
d5f005cc | 134 | parent->SetDefaultItem(this); |
dfe1eee3 | 135 | |
458ca8c1 MB |
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 | ||
ac32ba44 | 143 | for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst (); |
fd304d98 | 144 | node; node = node->GetNext ()) |
02e8b2f9 | 145 | { |
fd304d98 MB |
146 | wxWindow *win = node->GetData (); |
147 | wxButton *item = wxDynamicCast(win, wxButton); | |
148 | if (item) | |
458ca8c1 MB |
149 | item->SetDefaultShadowThicknessAndResize(); |
150 | } | |
151 | ||
152 | XtVaSetValues ((Widget) parent->GetMainWidget(), | |
153 | XmNdefaultButton, (Widget) GetMainWidget(), | |
154 | NULL); | |
4bb6408c JS |
155 | } |
156 | ||
e04b7e8e MB |
157 | static inline bool wxMotifLargeButtons() |
158 | { | |
159 | return wxSystemOptions::HasOption("motif.largebuttons") | |
160 | && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0; | |
161 | } | |
162 | ||
4d194d63 MB |
163 | /* static */ |
164 | wxSize wxButton::GetDefaultSize() | |
165 | { | |
166 | // TODO: check font size as in wxMSW ? MB | |
84fb430b | 167 | // Note: this is the button size (text + margin + shadow + defaultBorder) |
e04b7e8e | 168 | return wxSize( MIN_WIDTH, MIN_LARGE_HEIGHT ); |
458ca8c1 MB |
169 | } |
170 | ||
171 | wxSize wxButton::DoGetBestSize() const | |
e04b7e8e MB |
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 | ||
4e025aa2 MB |
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 | ||
e04b7e8e | 199 | wxSize wxButton::OldGetBestSize() const |
458ca8c1 MB |
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 ) ); | |
4e025aa2 | 216 | |
458ca8c1 MB |
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; | |
4d194d63 MB |
238 | } |
239 | ||
4bb6408c JS |
240 | void wxButton::Command (wxCommandEvent & event) |
241 | { | |
242 | ProcessCommand (event); | |
243 | } | |
244 | ||
f9e02ac7 | 245 | void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) |
02e8b2f9 | 246 | { |
2d120f83 JS |
247 | if (!wxGetWindowFromTable(w)) |
248 | // Widget has been deleted! | |
249 | return; | |
dfe1eee3 | 250 | |
2d120f83 JS |
251 | wxButton *item = (wxButton *) clientData; |
252 | wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId()); | |
253 | event.SetEventObject(item); | |
254 | item->ProcessCommand (event); | |
02e8b2f9 | 255 | } |