More Motif stuff, minor stubs correction
[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
18 #include <Xm/PushBG.h>
19 #include <Xm/PushB.h>
20
21 #include "wx/motif/private.h"
22
23 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
24
25 #if !USE_SHARED_LIBRARY
26 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
27 #endif
28
29 // Button
30
31 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
32 const wxPoint& pos,
33 const wxSize& size, long style,
34 const wxValidator& validator,
35 const wxString& name)
36 {
37 SetName(name);
38 SetValidator(validator);
39 m_windowStyle = style;
40
41 parent->AddChild((wxButton *)this);
42
43 if (id == -1)
44 m_windowId = NewControlId();
45 else
46 m_windowId = id;
47
48 char* label1 = (label.IsNull() ? "" : (char*) (const char*) label);
49
50 XmString text = XmStringCreateSimple (label1);
51 Widget parentWidget = (Widget) parent->GetClientWidget();
52
53 /*
54 * Patch Note (important)
55 * There is no major reason to put a defaultButtonThickness here.
56 * Not requesting it give the ability to put wxButton with a spacing
57 * as small as requested. However, if some button become a DefaultButton,
58 * other buttons are no more aligned -- This is why we set
59 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
60 * in the ::SetDefaultButton method.
61 */
62 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
63 xmPushButtonWidgetClass,
64 parentWidget,
65 XmNlabelString, text,
66 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
67 NULL);
68
69 XmStringFree (text);
70
71 XtAddCallback ((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
72 (XtPointer) this);
73
74
75 SetCanAddEventHandler(TRUE);
76 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
77
78 SetFont(* parent->GetFont());
79 ChangeColour(m_mainWidget);
80
81 return TRUE;
82 }
83
84 void wxButton::SetDefault()
85 {
86 wxWindow *parent = (wxWindow *)GetParent();
87 if (parent)
88 parent->SetDefaultItem(this);
89
90 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
91 // Unfortunately, buttons are now mis-aligned. We try to correct this
92 // now -- setting this ressource to 1 for each button in the same row.
93 // Because it's very hard to find wxButton in the same row,
94 // correction is straighforward: we set resource for all wxButton
95 // in this parent (but not sub panels)
96 for (wxNode * node = parent->GetChildren ()->First (); node; node = node->Next ())
97 {
98 wxButton *item = (wxButton *) node->Data ();
99 if (item->IsKindOf(CLASSINFO(wxButton)))
100 {
101 bool managed = XtIsManaged((Widget) item->GetMainWidget());
102 if (managed)
103 XtUnmanageChild ((Widget) item->GetMainWidget());
104
105 XtVaSetValues ((Widget) item->GetMainWidget(),
106 XmNdefaultButtonShadowThickness, 1,
107 NULL);
108
109 if (managed)
110 XtManageChild ((Widget) item->GetMainWidget());
111 }
112 } // while
113
114 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
115 XtVaSetValues ((Widget) parent->GetMainWidget(), XmNdefaultButton, (Widget) GetMainWidget(), NULL);
116 }
117
118 void wxButton::Command (wxCommandEvent & event)
119 {
120 ProcessCommand (event);
121 }
122
123 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr)
124 {
125 if (!wxGetWindowFromTable(w))
126 // Widget has been deleted!
127 return;
128
129 wxButton *item = (wxButton *) clientData;
130 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
131 event.SetEventObject(item);
132 item->ProcessCommand (event);
133 }