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