New makefiles for GTK and Motif, using configure but not
[wxWidgets.git] / src / motif / button.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "button.h"
14 #endif
15
16 #include "wx/button.h"
17 #include "wx/utils.h"
18 #include "wx/panel.h"
19
20 #include <Xm/PushBG.h>
21 #include <Xm/PushB.h>
22
23 #include "wx/motif/private.h"
24
25 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
26
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
29 #endif
30
31 // Button
32
33 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
34 const wxPoint& pos,
35 const wxSize& size, long style,
36 const wxValidator& validator,
37 const wxString& name)
38 {
39 SetName(name);
40 SetValidator(validator);
41 m_windowStyle = style;
42 m_backgroundColour = parent->GetBackgroundColour();
43 m_foregroundColour = parent->GetForegroundColour();
44 m_font = parent->GetFont();
45
46 parent->AddChild((wxButton *)this);
47
48 if (id == -1)
49 m_windowId = NewControlId();
50 else
51 m_windowId = id;
52
53 wxString label1(wxStripMenuCodes(label));
54
55 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
56 Widget parentWidget = (Widget) parent->GetClientWidget();
57
58 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
59
60 /*
61 * Patch Note (important)
62 * There is no major reason to put a defaultButtonThickness here.
63 * Not requesting it give the ability to put wxButton with a spacing
64 * as small as requested. However, if some button become a DefaultButton,
65 * other buttons are no more aligned -- This is why we set
66 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
67 * in the ::SetDefaultButton method.
68 */
69 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
70 xmPushButtonWidgetClass,
71 parentWidget,
72 XmNfontList, fontList,
73 XmNlabelString, text,
74 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
75 NULL);
76
77 XmStringFree (text);
78
79 XtAddCallback ((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
80 (XtPointer) this);
81
82 SetCanAddEventHandler(TRUE);
83
84 int x = 0; int y = 0;
85 wxFont new_font( parent->GetFont() );
86 GetTextExtent( label1, &x, &y, (int*)NULL, (int*)NULL, &new_font );
87
88 wxSize newSize = size;
89 if (newSize.x == -1) newSize.x = 30+x;
90 if (newSize.y == -1) newSize.y = 27+y;
91 SetSize( newSize.x, newSize.y );
92
93 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, newSize.x, newSize.y);
94
95 ChangeBackgroundColour();
96
97 return TRUE;
98 }
99
100 void wxButton::SetDefault()
101 {
102 wxWindow *parent = GetParent();
103 wxPanel *panel = wxDynamicCast(panel, wxPanel);
104 if ( panel )
105 panel->SetDefaultItem(this);
106
107 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
108 // Unfortunately, buttons are now mis-aligned. We try to correct this
109 // now -- setting this ressource to 1 for each button in the same row.
110 // Because it's very hard to find wxButton in the same row,
111 // correction is straighforward: we set resource for all wxButton
112 // in this parent (but not sub panels)
113 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
114 {
115 wxButton *item = (wxButton *) node->Data ();
116 if (item->IsKindOf(CLASSINFO(wxButton)))
117 {
118 bool managed = XtIsManaged((Widget) item->GetMainWidget());
119 if (managed)
120 XtUnmanageChild ((Widget) item->GetMainWidget());
121
122 XtVaSetValues ((Widget) item->GetMainWidget(),
123 XmNdefaultButtonShadowThickness, 1,
124 NULL);
125
126 if (managed)
127 XtManageChild ((Widget) item->GetMainWidget());
128 }
129 } // while
130
131 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
132 XtVaSetValues ((Widget) parent->GetMainWidget(), XmNdefaultButton, (Widget) GetMainWidget(), NULL);
133 }
134
135 /* static */
136 wxSize wxButton::GetDefaultSize()
137 {
138 // TODO: check font size as in wxMSW ? MB
139 //
140 return wxSize(80,26);
141 }
142
143 void wxButton::Command (wxCommandEvent & event)
144 {
145 ProcessCommand (event);
146 }
147
148 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
149 {
150 if (!wxGetWindowFromTable(w))
151 // Widget has been deleted!
152 return;
153
154 wxButton *item = (wxButton *) clientData;
155 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
156 event.SetEventObject(item);
157 item->ProcessCommand (event);
158 }
159
160 void wxButton::ChangeFont(bool keepOriginalSize)
161 {
162 wxWindow::ChangeFont(keepOriginalSize);
163 }
164
165 void wxButton::ChangeBackgroundColour()
166 {
167 DoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
168 }
169
170 void wxButton::ChangeForegroundColour()
171 {
172 wxWindow::ChangeForegroundColour();
173 }
174