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