1 /*
2 * This file is part of a wxSmith plugin for Code::Blocks Studio
3 * Copyright (C) 2007 Bartlomiej Swiecki
4 *
5 * wxSmith is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * wxSmith and this file is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with wxSmith; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 * Ron Collins
20 * rcoll@theriver.com
21 * 4-Feb-2010
22 *
23 */
24
25 #include "wxsSpeedButton.h"
26
27 //------------------------------------------------------------------------------
28
29 namespace
30 {
31
32 // Loading images from xpm files
33
34 #include "SpeedButton16.xpm"
35 #include "SpeedButton32.xpm"
36
37 wxsRegisterItem<wxsSpeedButton> Reg(
38 _T("wxSpeedButton"), // Class name
39 wxsTWidget, // Item type
40 _T("wxWindows"), // License
41 _T("Ron Collins"), // Author
42 _T("rcoll@theriver.com"), // Author's email
43 _T(""), // Item's homepage
44 _T("Contrib"), // Category in palette
45 90, // Priority in palette
46 _T("SpeedButton"), // Base part of names for new items
47 wxsCPP, // List of coding languages supported by this item
48 1, 0, // Version
49 wxBitmap(SpeedButton32_xpm), // 32x32 bitmap
50 wxBitmap(SpeedButton16_xpm), // 16x16 bitmap
51 false); // We do not allow this item inside XRC files
52
53 WXS_ST_BEGIN(wxsSpeedButtonStyles,_T("wxBORDER_NONE|wxTAB_TRAVERSAL"))
54 WXS_ST_CATEGORY("wxSpeedButton")
55 WXS_ST(wxBU_LEFT)
56 WXS_ST(wxBU_TOP)
57 WXS_ST(wxBU_RIGHT)
58 WXS_ST(wxBU_BOTTOM)
59 WXS_ST_DEFAULTS()
60 WXS_ST_END()
61
62 WXS_EV_BEGIN(wxsSpeedButtonEvents)
63 WXS_EVI(EVT_COMMAND_LEFT_CLICK, wxEVT_COMMAND_LEFT_CLICK, wxCommandEvent, LeftClick)
64 WXS_EVI(EVT_COMMAND_RIGHT_CLICK, wxEVT_COMMAND_RIGHT_CLICK, wxCommandEvent, RightClick)
65 WXS_EV_END()
66 }
67
68 //------------------------------------------------------------------------------
69
wxsSpeedButton(wxsItemResData * Data)70 wxsSpeedButton::wxsSpeedButton(wxsItemResData* Data):
71 wxsWidget(
72 Data,
73 &Reg.Info,
74 wxsSpeedButtonEvents,
75 wxsSpeedButtonStyles)
76 {
77
78 // start with a clean slate
79
80 mLabel = _("");
81 mGlyphCount = 0;
82 mMargin = 2;
83 mUseInclude = true;
84 mGroupIndex = 0;
85 mAllowAllUp = true;
86 mUserData = 0;
87 mButtonType = 1;
88 mButtonDown = false;
89
90 }
91
92 //------------------------------------------------------------------------------
93
OnBuildCreatingCode()94 void wxsSpeedButton::OnBuildCreatingCode() {
95 int n;
96 wxString inc;
97 wxString vname; // name of this var
98 wxString bname; // name of wxBitmap for the button
99 wxString ss, tt;
100
101 // valid language?
102
103 if (GetLanguage() != wxsCPP) wxsCodeMarks::Unknown(_T("wxsSpeedButton::OnBuildCreatingCode"),GetLanguage());
104
105 // who we are
106
107 vname = GetVarName();
108 bname = vname + _("_BMP");
109
110 // include files
111
112 AddHeader(_("<wxSpeedButton.h>"), GetInfo().ClassName, 0);
113
114 // create the bitmap used for button images
115
116 BuildBitmap();
117
118 // fix the group index depending upon the button type
119
120 if (mButtonType == 0) n = 0;
121 else if (mButtonType == 1) n = -1;
122 else if (mButtonType == 2) n = -2;
123 else if (mButtonType == 3) n = mGroupIndex;
124 else n = -1;
125
126 // create the button
127
128 Codef(_T("%s = new wxSpeedButton(%W, %I, %t, %s, %d, %d, %d, %b, %P, %S, %T, %V, %N);\n"),
129 vname.wx_str(), mLabel.wx_str(), bname.wx_str(), mGlyphCount, mMargin, n, mAllowAllUp);
130 BuildSetupWindowCode();
131
132 // and individual button settings
133
134 if (mButtonDown) Codef(_T("%s->SetDown(true);\n"), vname.wx_str());
135 Codef(_T("%s->SetUserData(%d);\n"), vname.wx_str(), static_cast<int>(mUserData));
136 }
137
138 //------------------------------------------------------------------------------
139 // if no bitmap is selected, make a NULL bitmap
140 // if art-provider is selected, make a bitmap from that
141 // if an XPM file is selected, and user selected to #include the XPM file,
142 // then #include the file and make a bitmap from the data
143 // else it must be a file was selected, so make a bitmap from the file
144
BuildBitmap(void)145 void wxsSpeedButton::BuildBitmap(void) {
146 wxString s,v;
147
148 // make a name for the bitmap
149
150 v = GetVarName() + _("_BMP");
151
152 // no image
153
154 if ((mGlyph.Id.IsEmpty()) && (mGlyph.FileName.IsEmpty())) {
155 Codef(_T("wxBitmap %s = wxNullBitmap;\n"), v.wx_str());
156 }
157
158 // art-provider image
159
160 else if (! mGlyph.Id.IsEmpty()) {
161 Codef(_T("wxBitmap %s(%i);\n"), v.wx_str(), &mGlyph);
162 }
163
164 // is it an XPM and do we want to #include it?
165
166 else if ((IsImageXPM(mGlyph)) && (mUseInclude)) {
167 s = mGlyph.FileName;
168 s.Replace(_("\\"), _("/"), true);
169 s = _T("\"") + s + _T("\"");
170 AddHeader(s, GetInfo().ClassName, 0);
171
172 s = GetXPMName(mGlyph);
173 Codef(_T("wxBitmap %s(%s);\n"), v.wx_str(), s.wx_str());
174 }
175
176 // else a normal image file
177
178 else if (! mGlyph.FileName.IsEmpty()) {
179 s = mGlyph.FileName;
180 s.Replace(_("\\"), _("/"), true);
181
182 Codef(_T("wxBitmap %s(%t, wxBITMAP_TYPE_ANY);\n"), v.wx_str(), s.wx_str());
183 }
184
185 // an unknown and unexpected set of conditions
186
187 else {
188 Codef(_T("wxBitmap %s = wxNullBitmap;\n"), v.wx_str());
189 };
190 }
191
192 //------------------------------------------------------------------------------
193 // did the user specify a XPM image file?
194
IsImageXPM(wxsBitmapData & inData)195 bool wxsSpeedButton::IsImageXPM(wxsBitmapData &inData) {
196 wxString s;
197
198 // a special case of no image at all
199
200 if (inData.IsEmpty()) return false;
201
202 // or built-in wxWidgets art
203
204 inData.Id.Trim();
205 if (! inData.Id.IsEmpty()) return false;
206
207 // no filename given?
208
209 inData.FileName.Trim();
210 if (inData.FileName.IsEmpty()) return false;
211
212 // file must actually exist
213
214 if (! wxFileName::FileExists(inData.FileName)) return false;
215
216 // last 4 chars of filename should be ".XPM"
217
218 s = inData.FileName.Right(4);
219 s.MakeUpper();
220 if (s == _T(".XPM")) return true;
221
222 // must be something else
223
224 return false;
225 }
226
227 //------------------------------------------------------------------------------
228 // if an image data buffer specifies an XPM file, then return the name
229 // of the static char array defined by the file
230
GetXPMName(wxsBitmapData & inData)231 wxString wxsSpeedButton::GetXPMName(wxsBitmapData &inData) {
232 wxFileInputStream input( inData.FileName );
233 wxTextInputStream txt( input );
234
235
236 // this will be our return value
237
238 wxString v = _T("");
239
240 // read until EOF, keeping the last data name we find
241
242 while (! input.Eof()) {
243
244 // next line
245
246 wxString s = txt.ReadLine();
247 //std::cout << "line " << i++ << ") " << s.mb_str() << std::endl;
248
249 // the "static" keyword is optional under some compilers
250
251 s.Trim(false);
252 int n = s.Find(_T("static"));
253 if (n == 0) s.erase(0, 6);
254
255 // but the "char" and "*" are required
256
257 s.Trim(false);
258 n = s.Find(_T("char"));
259 if (n != 0) continue;
260 s.erase(0, 4);
261
262 s.Trim(false);
263 n = s.Find(_T("*"));
264 if (n != 0) continue;
265 s.erase(0, 1);
266
267 // everything up to the "[" is the name of an array
268
269 s.Trim(false);
270 n = s.Find(_T("["));
271 if (n == wxNOT_FOUND) n = s.Len();
272
273 v = s.Left(n);
274 };
275
276 // done with file
277
278 // txt.Close();
279
280 // done
281
282 return v;
283 }
284
285
286
287
288 //------------------------------------------------------------------------------
289
OnBuildPreview(wxWindow * Parent,long Flags)290 wxObject* wxsSpeedButton::OnBuildPreview(wxWindow* Parent, long Flags) {
291 int n;
292 wxSpeedButton *sb;
293 wxBitmap bmp;
294
295 // make bitmap
296
297 bmp = mGlyph.GetPreview(wxDefaultSize);
298
299 // fix the group index depending upon the button type
300
301 if (mButtonType == 0) n = 0;
302 else if (mButtonType == 1) n = -1;
303 else if (mButtonType == 2) n = -2;
304 else if (mButtonType == 3) n = mGroupIndex;
305 else n = -1;
306
307 // make a button
308
309 sb = new wxSpeedButton(Parent, GetId(), mLabel, bmp, mGlyphCount,
310 mMargin, n, mAllowAllUp, Pos(Parent), Size(Parent), Style(),
311 wxDefaultValidator, _T("SpeedButton"));
312 if (sb == NULL) return NULL;
313 SetupWindow(sb, Flags);
314
315 if (mButtonDown) sb->SetDown(mButtonDown);
316
317 // done
318
319 return sb;
320 }
321
322 //------------------------------------------------------------------------------
323
OnEnumWidgetProperties(cb_unused long Flags)324 void wxsSpeedButton::OnEnumWidgetProperties(cb_unused long Flags) {
325 static const long TypeValues[] = { 0, 1, 2, 3, 0};
326 static const wxChar* TypeNames[] = {_T("Simple Button"), _T("Toggle Button"), _T("Group By Parent"), _T("Group By Index"), 0};
327
328 WXS_STRING(wxsSpeedButton, mLabel, _("Label"), _T("label"), _T(""), false);
329 WXS_BITMAP(wxsSpeedButton, mGlyph, _("Glyph"), _T("glyph"), _T("wxART_OTHER"));
330 WXS_LONG( wxsSpeedButton, mGlyphCount, _("Glyph Count"), _T("glyphcount"), 0)
331 WXS_BOOL( wxsSpeedButton, mUseInclude, _("Use XPM As #include"), _T("useinclude"), true);
332 WXS_LONG( wxsSpeedButton, mMargin, _("Margin Space"), _T("margin"), 2)
333 WXS_BOOL( wxsSpeedButton, mButtonDown, _("Button DOWN"), _("buttondown"), false);
334 WXS_ENUM( wxsSpeedButton, mButtonType, _("Button Type"), _T("buttontype"), TypeValues, TypeNames, 1);
335 WXS_LONG( wxsSpeedButton, mGroupIndex, _("Group Index"), _T("groupindex"), 0)
336 WXS_BOOL( wxsSpeedButton, mAllowAllUp, _("Allow All Up"), _T("allowallup"), true);
337 WXS_LONG( wxsSpeedButton, mUserData, _("User Data"), _T("userdata"), 0)
338
339 };
340
341
342
343