]> git.saurik.com Git - wxWidgets.git/blame - src/motif/button.cpp
debug check for infinite loop in InitializeClasses() added
[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
338dd992
JJ
20#ifdef __VMS__
21#pragma message disable nosimpint
22#endif
02e8b2f9
JS
23#include <Xm/PushBG.h>
24#include <Xm/PushB.h>
338dd992
JJ
25#ifdef __VMS__
26#pragma message enable nosimpint
27#endif
02e8b2f9
JS
28
29#include "wx/motif/private.h"
30
31void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
32
4bb6408c
JS
33#if !USE_SHARED_LIBRARY
34IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
35#endif
36
37// Button
38
39bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
40 const wxPoint& pos,
41 const wxSize& size, long style,
42 const wxValidator& validator,
43 const wxString& name)
4bb6408c
JS
44{
45 SetName(name);
46 SetValidator(validator);
47 m_windowStyle = style;
0d57be45
JS
48 m_backgroundColour = parent->GetBackgroundColour();
49 m_foregroundColour = parent->GetForegroundColour();
da175b2c 50 m_font = parent->GetFont();
dfe1eee3 51
4bb6408c 52 parent->AddChild((wxButton *)this);
dfe1eee3 53
4bb6408c
JS
54 if (id == -1)
55 m_windowId = NewControlId();
56 else
57 m_windowId = id;
dfe1eee3 58
89c7e962 59 wxString label1(wxStripMenuCodes(label));
dfe1eee3 60
89c7e962 61 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
02e8b2f9 62 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 63
da175b2c 64 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
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,
78 XmNfontList, fontList,
79 XmNlabelString, text,
80 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
81 NULL);
dfe1eee3 82
02e8b2f9 83 XmStringFree (text);
dfe1eee3 84
02e8b2f9 85 XtAddCallback ((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
2d120f83 86 (XtPointer) this);
dfe1eee3 87
02e8b2f9 88 SetCanAddEventHandler(TRUE);
8e877c19
RR
89
90 int x = 0; int y = 0;
91 wxFont new_font( parent->GetFont() );
92 GetTextExtent( label1, &x, &y, (int*)NULL, (int*)NULL, &new_font );
93
94 wxSize newSize = size;
95 if (newSize.x == -1) newSize.x = 30+x;
96 if (newSize.y == -1) newSize.y = 27+y;
97 SetSize( newSize.x, newSize.y );
98
99 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, newSize.x, newSize.y);
dfe1eee3 100
0d57be45 101 ChangeBackgroundColour();
dfe1eee3 102
02e8b2f9 103 return TRUE;
4bb6408c
JS
104}
105
106void wxButton::SetDefault()
107{
dfe1eee3 108 wxWindow *parent = GetParent();
95814e49 109 wxPanel *panel = wxDynamicCast(parent, wxPanel);
dfe1eee3
VZ
110 if ( panel )
111 panel->SetDefaultItem(this);
112
2d120f83
JS
113 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
114 // Unfortunately, buttons are now mis-aligned. We try to correct this
115 // now -- setting this ressource to 1 for each button in the same row.
116 // Because it's very hard to find wxButton in the same row,
117 // correction is straighforward: we set resource for all wxButton
118 // in this parent (but not sub panels)
119 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
02e8b2f9 120 {
2d120f83
JS
121 wxButton *item = (wxButton *) node->Data ();
122 if (item->IsKindOf(CLASSINFO(wxButton)))
123 {
124 bool managed = XtIsManaged((Widget) item->GetMainWidget());
125 if (managed)
126 XtUnmanageChild ((Widget) item->GetMainWidget());
dfe1eee3 127
2d120f83
JS
128 XtVaSetValues ((Widget) item->GetMainWidget(),
129 XmNdefaultButtonShadowThickness, 1,
130 NULL);
dfe1eee3 131
2d120f83
JS
132 if (managed)
133 XtManageChild ((Widget) item->GetMainWidget());
134 }
dfe1eee3
VZ
135 } // while
136
2d120f83
JS
137 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
138 XtVaSetValues ((Widget) parent->GetMainWidget(), XmNdefaultButton, (Widget) GetMainWidget(), NULL);
4bb6408c
JS
139}
140
4d194d63
MB
141/* static */
142wxSize wxButton::GetDefaultSize()
143{
144 // TODO: check font size as in wxMSW ? MB
145 //
146 return wxSize(80,26);
147}
148
4bb6408c
JS
149void wxButton::Command (wxCommandEvent & event)
150{
151 ProcessCommand (event);
152}
153
f9e02ac7 154void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 155{
2d120f83
JS
156 if (!wxGetWindowFromTable(w))
157 // Widget has been deleted!
158 return;
dfe1eee3 159
2d120f83
JS
160 wxButton *item = (wxButton *) clientData;
161 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
162 event.SetEventObject(item);
163 item->ProcessCommand (event);
02e8b2f9 164}
0d57be45 165
4b5f3fe6 166void wxButton::ChangeFont(bool keepOriginalSize)
0d57be45 167{
4b5f3fe6 168 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
169}
170
171void wxButton::ChangeBackgroundColour()
172{
321db4b6 173 DoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
0d57be45
JS
174}
175
176void wxButton::ChangeForegroundColour()
177{
321db4b6 178 wxWindow::ChangeForegroundColour();
0d57be45
JS
179}
180