]>
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 | |
dfe1eee3 | 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 JS |
35 | |
36 | #include "wx/motif/private.h" | |
e04b7e8e | 37 | #include "wx/sysopt.h" |
02e8b2f9 JS |
38 | |
39 | void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr); | |
40 | ||
4bb6408c | 41 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
4bb6408c | 42 | |
e04b7e8e MB |
43 | #define MIN_WIDTH 78 |
44 | #define MIN_LARGE_HEIGHT 30 | |
45 | ||
4bb6408c JS |
46 | // Button |
47 | ||
48 | bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label, | |
2d120f83 JS |
49 | const wxPoint& pos, |
50 | const wxSize& size, long style, | |
51 | const wxValidator& validator, | |
52 | const wxString& name) | |
4bb6408c | 53 | { |
458ca8c1 MB |
54 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) |
55 | return false; | |
dfe1eee3 | 56 | |
89c7e962 | 57 | wxString label1(wxStripMenuCodes(label)); |
458ca8c1 | 58 | wxXmString text( label1 ); |
dfe1eee3 | 59 | |
02e8b2f9 | 60 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
dfe1eee3 | 61 | |
02e8b2f9 | 62 | /* |
2d120f83 JS |
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 | */ | |
02e8b2f9 | 71 | m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button", |
2d120f83 JS |
72 | xmPushButtonWidgetClass, |
73 | parentWidget, | |
da494b40 | 74 | wxFont::GetFontTag(), m_font.GetFontType(XtDisplay(parentWidget)), |
458ca8c1 | 75 | XmNlabelString, text(), |
84fb430b | 76 | XmNrecomputeSize, False, |
458ca8c1 MB |
77 | // See comment for wxButton::SetDefault |
78 | // XmNdefaultButtonShadowThickness, 1, | |
2d120f83 | 79 | NULL); |
dfe1eee3 | 80 | |
458ca8c1 MB |
81 | XtAddCallback ((Widget) m_mainWidget, |
82 | XmNactivateCallback, (XtCallbackProc) wxButtonCallback, | |
83 | (XtPointer) this); | |
dfe1eee3 | 84 | |
458ca8c1 MB |
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); | |
dfe1eee3 | 91 | |
0d57be45 | 92 | ChangeBackgroundColour(); |
dfe1eee3 | 93 | |
e04b7e8e | 94 | return true; |
4bb6408c JS |
95 | } |
96 | ||
458ca8c1 MB |
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 | ||
84fb430b MB |
111 | // this can't currently be done, because user code that calls SetDefault |
112 | // will break otherwise | |
113 | #if 0 | |
458ca8c1 MB |
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 ); | |
84fb430b | 120 | #endif |
458ca8c1 MB |
121 | } |
122 | ||
123 | ||
4bb6408c JS |
124 | void wxButton::SetDefault() |
125 | { | |
dfe1eee3 | 126 | wxWindow *parent = GetParent(); |
b0ee47ff | 127 | if ( parent ) |
d5f005cc | 128 | parent->SetDefaultItem(this); |
dfe1eee3 | 129 | |
458ca8c1 MB |
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 | ||
ac32ba44 | 137 | for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst (); |
fd304d98 | 138 | node; node = node->GetNext ()) |
02e8b2f9 | 139 | { |
fd304d98 MB |
140 | wxWindow *win = node->GetData (); |
141 | wxButton *item = wxDynamicCast(win, wxButton); | |
142 | if (item) | |
458ca8c1 MB |
143 | item->SetDefaultShadowThicknessAndResize(); |
144 | } | |
145 | ||
146 | XtVaSetValues ((Widget) parent->GetMainWidget(), | |
147 | XmNdefaultButton, (Widget) GetMainWidget(), | |
148 | NULL); | |
4bb6408c JS |
149 | } |
150 | ||
e04b7e8e MB |
151 | static inline bool wxMotifLargeButtons() |
152 | { | |
153 | return wxSystemOptions::HasOption("motif.largebuttons") | |
154 | && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0; | |
155 | } | |
156 | ||
4d194d63 MB |
157 | /* static */ |
158 | wxSize wxButton::GetDefaultSize() | |
159 | { | |
160 | // TODO: check font size as in wxMSW ? MB | |
84fb430b | 161 | // Note: this is the button size (text + margin + shadow + defaultBorder) |
e04b7e8e | 162 | return wxSize( MIN_WIDTH, MIN_LARGE_HEIGHT ); |
458ca8c1 MB |
163 | } |
164 | ||
165 | wxSize wxButton::DoGetBestSize() const | |
e04b7e8e MB |
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 | |
458ca8c1 MB |
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; | |
4d194d63 MB |
218 | } |
219 | ||
4bb6408c JS |
220 | void wxButton::Command (wxCommandEvent & event) |
221 | { | |
222 | ProcessCommand (event); | |
223 | } | |
224 | ||
f9e02ac7 | 225 | void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) |
02e8b2f9 | 226 | { |
2d120f83 JS |
227 | if (!wxGetWindowFromTable(w)) |
228 | // Widget has been deleted! | |
229 | return; | |
dfe1eee3 | 230 | |
2d120f83 JS |
231 | wxButton *item = (wxButton *) clientData; |
232 | wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId()); | |
233 | event.SetEventObject(item); | |
234 | item->ProcessCommand (event); | |
02e8b2f9 | 235 | } |