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