]> git.saurik.com Git - wxWidgets.git/blame - src/motif/button.cpp
Regenerated some more makefiles/filelists.
[wxWidgets.git] / src / motif / button.cpp
CommitLineData
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
12#ifdef __GNUG__
13#pragma implementation "button.h"
14#endif
15
bcd055ae
JJ
16#ifdef __VMS
17#define XtDisplay XTDISPLAY
18#endif
19
f6045f99
GD
20#include "wx/defs.h"
21
4bb6408c
JS
22#include "wx/button.h"
23
338dd992
JJ
24#ifdef __VMS__
25#pragma message disable nosimpint
26#endif
02e8b2f9
JS
27#include <Xm/PushBG.h>
28#include <Xm/PushB.h>
338dd992
JJ
29#ifdef __VMS__
30#pragma message enable nosimpint
31#endif
02e8b2f9
JS
32
33#include "wx/motif/private.h"
34
35void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
36
4bb6408c 37IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
4bb6408c
JS
38
39// Button
40
41bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
42 const wxPoint& pos,
43 const wxSize& size, long style,
44 const wxValidator& validator,
45 const wxString& name)
4bb6408c 46{
458ca8c1
MB
47 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
48 return false;
dfe1eee3 49
89c7e962 50 wxString label1(wxStripMenuCodes(label));
458ca8c1 51 wxXmString text( label1 );
dfe1eee3 52
02e8b2f9 53 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 54
02e8b2f9 55 /*
2d120f83
JS
56 * Patch Note (important)
57 * There is no major reason to put a defaultButtonThickness here.
58 * Not requesting it give the ability to put wxButton with a spacing
59 * as small as requested. However, if some button become a DefaultButton,
60 * other buttons are no more aligned -- This is why we set
61 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
62 * in the ::SetDefaultButton method.
63 */
02e8b2f9 64 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
2d120f83
JS
65 xmPushButtonWidgetClass,
66 parentWidget,
da494b40 67 wxFont::GetFontTag(), m_font.GetFontType(XtDisplay(parentWidget)),
458ca8c1 68 XmNlabelString, text(),
84fb430b 69 XmNrecomputeSize, False,
458ca8c1
MB
70 // See comment for wxButton::SetDefault
71 // XmNdefaultButtonShadowThickness, 1,
2d120f83 72 NULL);
dfe1eee3 73
458ca8c1
MB
74 XtAddCallback ((Widget) m_mainWidget,
75 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
76 (XtPointer) this);
dfe1eee3 77
02e8b2f9 78 SetCanAddEventHandler(TRUE);
8e877c19 79
458ca8c1
MB
80 wxSize best = GetBestSize();
81 if( size.x != -1 ) best.x = size.x;
82 if( size.y != -1 ) best.y = size.y;
83
84 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
85 pos.x, pos.y, best.x, best.y);
dfe1eee3 86
0d57be45 87 ChangeBackgroundColour();
dfe1eee3 88
02e8b2f9 89 return TRUE;
4bb6408c
JS
90}
91
458ca8c1
MB
92void wxButton::SetDefaultShadowThicknessAndResize()
93{
94 Widget buttonWidget = (Widget)GetMainWidget();
95 bool managed = XtIsManaged( buttonWidget );
96 if( managed )
97 XtUnmanageChild( buttonWidget );
98
99 XtVaSetValues( buttonWidget,
100 XmNdefaultButtonShadowThickness, 1,
101 NULL );
102
103 if( managed )
104 XtManageChild( buttonWidget );
105
84fb430b
MB
106 // this can't currently be done, because user code that calls SetDefault
107 // will break otherwise
108#if 0
458ca8c1
MB
109 wxSize best = GetBestSize(), actual = GetSize();
110 if( best.x < actual.x ) best.x = actual.x;
111 if( best.y < actual.y ) best.y = actual.y;
112
113 if( best != actual )
114 SetSize( best );
84fb430b 115#endif
458ca8c1
MB
116}
117
118
4bb6408c
JS
119void wxButton::SetDefault()
120{
dfe1eee3 121 wxWindow *parent = GetParent();
b0ee47ff 122 if ( parent )
d5f005cc 123 parent->SetDefaultItem(this);
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
fd304d98
MB
132 for (wxWindowList::Node * node = parent->GetChildren().GetFirst ();
133 node; node = node->GetNext ())
02e8b2f9 134 {
fd304d98
MB
135 wxWindow *win = node->GetData ();
136 wxButton *item = wxDynamicCast(win, wxButton);
137 if (item)
458ca8c1
MB
138 item->SetDefaultShadowThicknessAndResize();
139 }
140
141 XtVaSetValues ((Widget) parent->GetMainWidget(),
142 XmNdefaultButton, (Widget) GetMainWidget(),
143 NULL);
4bb6408c
JS
144}
145
4d194d63
MB
146/* static */
147wxSize wxButton::GetDefaultSize()
148{
149 // TODO: check font size as in wxMSW ? MB
84fb430b
MB
150 // Note: this is the button size (text + margin + shadow + defaultBorder)
151 return wxSize(78,30);
458ca8c1
MB
152}
153
154wxSize wxButton::DoGetBestSize() const
155{
156 Dimension xmargin, ymargin, highlight, shadow, defThickness;
157
158 XtVaGetValues( (Widget)m_mainWidget,
159 XmNmarginWidth, &xmargin,
160 XmNmarginHeight, &ymargin,
161 XmNhighlightThickness, &highlight,
162 XmNshadowThickness, &shadow,
163 XmNdefaultButtonShadowThickness, &defThickness,
164 NULL );
165
166 int x = 0; int y = 0;
167 GetTextExtent( GetLabel(), &x, &y );
168
169 int margin = highlight * 2 +
170 ( defThickness ? ( ( shadow + defThickness ) * 4 ) : ( shadow * 2 ) );
171 wxSize best( x + xmargin * 2 + margin,
172 y + ymargin * 2 + margin );
173
174 // all buttons have at least the standard size unless the user explicitly
175 // wants them to be of smaller size and used wxBU_EXACTFIT style when
176 // creating the button
177 if( !HasFlag( wxBU_EXACTFIT ) )
178 {
179 wxSize def = GetDefaultSize();
180 int margin = highlight * 2 +
181 ( defThickness ? ( shadow * 4 + defThickness * 4 ) : 0 );
182 def.x += margin;
183 def.y += margin;
184
185 if( def.x > best.x )
186 best.x = def.x;
187 if( def.y > best.y )
188 best.y = def.y;
189 }
190
191 return best;
4d194d63
MB
192}
193
4bb6408c
JS
194void wxButton::Command (wxCommandEvent & event)
195{
196 ProcessCommand (event);
197}
198
f9e02ac7 199void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 200{
2d120f83
JS
201 if (!wxGetWindowFromTable(w))
202 // Widget has been deleted!
203 return;
dfe1eee3 204
2d120f83
JS
205 wxButton *item = (wxButton *) clientData;
206 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
207 event.SetEventObject(item);
208 item->ProcessCommand (event);
02e8b2f9 209}