]> git.saurik.com Git - wxWidgets.git/blame - src/motif/button.cpp
Someone need to make the clientdata be void* instead of this char*
[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
16#include "wx/button.h"
89c7e962 17#include "wx/utils.h"
dfe1eee3 18#include "wx/panel.h"
4bb6408c 19
02e8b2f9
JS
20#include <Xm/PushBG.h>
21#include <Xm/PushB.h>
22
23#include "wx/motif/private.h"
24
25void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
26
4bb6408c
JS
27#if !USE_SHARED_LIBRARY
28IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
29#endif
30
31// Button
32
33bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
34 const wxPoint& pos,
35 const wxSize& size, long style,
36 const wxValidator& validator,
37 const wxString& name)
4bb6408c
JS
38{
39 SetName(name);
40 SetValidator(validator);
41 m_windowStyle = style;
0d57be45
JS
42 m_backgroundColour = parent->GetBackgroundColour();
43 m_foregroundColour = parent->GetForegroundColour();
da175b2c 44 m_font = parent->GetFont();
dfe1eee3 45
4bb6408c 46 parent->AddChild((wxButton *)this);
dfe1eee3 47
4bb6408c
JS
48 if (id == -1)
49 m_windowId = NewControlId();
50 else
51 m_windowId = id;
dfe1eee3 52
89c7e962 53 wxString label1(wxStripMenuCodes(label));
dfe1eee3 54
89c7e962 55 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
02e8b2f9 56 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 57
da175b2c 58 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
dfe1eee3 59
02e8b2f9 60 /*
2d120f83
JS
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 */
02e8b2f9 69 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
2d120f83
JS
70 xmPushButtonWidgetClass,
71 parentWidget,
72 XmNfontList, fontList,
73 XmNlabelString, text,
74 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
75 NULL);
dfe1eee3 76
02e8b2f9 77 XmStringFree (text);
dfe1eee3 78
02e8b2f9 79 XtAddCallback ((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
2d120f83 80 (XtPointer) this);
dfe1eee3 81
02e8b2f9
JS
82 SetCanAddEventHandler(TRUE);
83 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
dfe1eee3 84
0d57be45 85 ChangeBackgroundColour();
dfe1eee3 86
02e8b2f9 87 return TRUE;
4bb6408c
JS
88}
89
90void wxButton::SetDefault()
91{
dfe1eee3
VZ
92 wxWindow *parent = GetParent();
93 wxPanel *panel = wxDynamicCast(panel, wxPanel);
94 if ( panel )
95 panel->SetDefaultItem(this);
96
2d120f83
JS
97 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
98 // Unfortunately, buttons are now mis-aligned. We try to correct this
99 // now -- setting this ressource to 1 for each button in the same row.
100 // Because it's very hard to find wxButton in the same row,
101 // correction is straighforward: we set resource for all wxButton
102 // in this parent (but not sub panels)
103 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
02e8b2f9 104 {
2d120f83
JS
105 wxButton *item = (wxButton *) node->Data ();
106 if (item->IsKindOf(CLASSINFO(wxButton)))
107 {
108 bool managed = XtIsManaged((Widget) item->GetMainWidget());
109 if (managed)
110 XtUnmanageChild ((Widget) item->GetMainWidget());
dfe1eee3 111
2d120f83
JS
112 XtVaSetValues ((Widget) item->GetMainWidget(),
113 XmNdefaultButtonShadowThickness, 1,
114 NULL);
dfe1eee3 115
2d120f83
JS
116 if (managed)
117 XtManageChild ((Widget) item->GetMainWidget());
118 }
dfe1eee3
VZ
119 } // while
120
2d120f83
JS
121 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
122 XtVaSetValues ((Widget) parent->GetMainWidget(), XmNdefaultButton, (Widget) GetMainWidget(), NULL);
4bb6408c
JS
123}
124
125void wxButton::Command (wxCommandEvent & event)
126{
127 ProcessCommand (event);
128}
129
f9e02ac7 130void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 131{
2d120f83
JS
132 if (!wxGetWindowFromTable(w))
133 // Widget has been deleted!
134 return;
dfe1eee3 135
2d120f83
JS
136 wxButton *item = (wxButton *) clientData;
137 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
138 event.SetEventObject(item);
139 item->ProcessCommand (event);
02e8b2f9 140}
0d57be45 141
4b5f3fe6 142void wxButton::ChangeFont(bool keepOriginalSize)
0d57be45 143{
4b5f3fe6 144 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
145}
146
147void wxButton::ChangeBackgroundColour()
148{
321db4b6 149 DoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
0d57be45
JS
150}
151
152void wxButton::ChangeForegroundColour()
153{
321db4b6 154 wxWindow::ChangeForegroundColour();
0d57be45
JS
155}
156