1 /*
2 * This file is part of wxSmith plugin for Code::Blocks Studio
3 * Copyright (C) 2006-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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * wxSmith 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, see <http://www.gnu.org/licenses/>.
17 *
18 * $Revision: 11783 $
19 * $Id: wxsstatusbar.cpp 11783 2019-07-08 19:39:27Z fuscated $
20 * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/plugins/contrib/wxSmith/wxwidgets/defitems/wxsstatusbar.cpp $
21 */
22 
23 #include <wx/statusbr.h>
24 #include "wxsstatusbar.h"
25 #include "../wxsitemresdata.h"
26 
27 namespace
28 {
29     wxsRegisterItem<wxsStatusBar> Reg(_T("StatusBar"),wxsTTool,_T("Tools"),40);
30 
31     WXS_ST_BEGIN(wxsStatusBarStyles,_T(""))
32         WXS_ST_CATEGORY("wxStatusBar")
33         WXS_ST(wxST_SIZEGRIP)
34         WXS_ST_DEFAULTS()
35     WXS_ST_END()
36 
37     const wxChar* FieldStyles[] = { _T("wxSB_NORMAL"), _T("wxSB_FLAT"), _T("wxSB_RAISED"), 0 };
38     const long FieldStylesVal[] = { wxSB_NORMAL, wxSB_FLAT, wxSB_RAISED };
39 }
40 
wxsStatusBar(wxsItemResData * Data)41 wxsStatusBar::wxsStatusBar(wxsItemResData* Data):
42     wxsTool(Data,&Reg.Info,0,wxsStatusBarStyles,flVariable|flId|flSubclass|flExtraCode),
43     m_Fields(1)
44 {
45     UpdateArraysSize(m_Fields);
46 }
47 
OnBuildCreatingCode()48 void wxsStatusBar::OnBuildCreatingCode()
49 {
50     switch ( GetLanguage() )
51     {
52         case wxsCPP:
53         {
54             AddHeader(_T("<wx/statusbr.h>"),GetInfo().ClassName,hfInPCH);
55             Codef(_T("%C(%W, %I, %T, %N);\n"));
56             if ( m_Fields>0 )
57             {
58                 wxString WidthsVarName = GetCoderContext()->GetUniqueName(_T("__wxStatusBarWidths"));
59                 wxString StylesVarName = GetCoderContext()->GetUniqueName(_T("__wxStatusBarStyles"));
60 
61                 Codef(_T("int %v[%d] = { "),WidthsVarName.wx_str(),m_Fields);
62                 for ( int i=0; i<m_Fields; i++ )
63                 {
64                     Codef( _T("%d%s"),
65                         m_VarWidth[i]?-m_Widths[i]:m_Widths[i],
66                         i==(m_Fields-1) ? _T(" };\n") : _T(", "));
67                 }
68                 Codef(_T("int %v[%d] = { "),StylesVarName.wx_str(),m_Fields);
69                 for ( int i=0; i<m_Fields; i++ )
70                 {
71                     Codef(_T("%s%s"),
72                         m_Styles[i] == wxSB_FLAT ?   _T("wxSB_FLAT") :
73                         m_Styles[i] == wxSB_RAISED ? _T("wxSB_RAISED") :
74                                                      _T("wxSB_NORMAL"),
75                         i==(m_Fields-1) ? _T(" };\n") : _T(", "));
76                 }
77                 Codef(_T("%ASetFieldsCount(%d,%v);\n"),m_Fields,WidthsVarName.wx_str());
78                 Codef(_T("%ASetStatusStyles(%d,%v);\n"),m_Fields,StylesVarName.wx_str());
79                 Codef(_T("SetStatusBar(%O);\n"));
80             }
81             BuildSetupWindowCode();
82             break;
83         }
84 
85         case wxsUnknownLanguage: // fall-through
86         default:
87             wxsCodeMarks::Unknown(_T("wxsStatusBar::OnBuildCreatingCode"),GetLanguage());
88     }
89 }
90 
OnEnumToolProperties(cb_unused long Flags)91 void wxsStatusBar::OnEnumToolProperties(cb_unused long Flags)
92 {
93 }
94 
OnCanAddToResource(wxsItemResData * Data,bool ShowMessage)95 bool wxsStatusBar::OnCanAddToResource(wxsItemResData* Data,bool ShowMessage)
96 {
97     if ( Data->GetClassType() != _T("wxFrame") )
98     {
99         if ( ShowMessage )
100         {
101             cbMessageBox(_("wxStatusBar can be added to wxFrame only"));
102         }
103         return false;
104     }
105 
106     for ( int i=0; i<Data->GetToolsCount(); i++ )
107     {
108         if ( Data->GetTool(i)->GetClassName() == _T("wxStatusBar") )
109         {
110             if ( ShowMessage )
111             {
112                 cbMessageBox(_("Can not add two or more wxStatusBar classes\ninto one wxFrame"));
113             }
114             return false;
115         }
116     }
117 
118     return true;
119 }
120 
OnCanAddChild(cb_unused wxsItem * Item,bool ShowMessage)121         bool wxsStatusBar::OnCanAddChild(cb_unused wxsItem* Item,bool ShowMessage)
122 {
123     if ( ShowMessage )
124     {
125         cbMessageBox(_("wxsStatusBar can have no children"));
126     }
127     return false;
128 }
129 
OnCanAddToParent(cb_unused wxsParent * Item,bool ShowMessage)130 bool wxsStatusBar::OnCanAddToParent(cb_unused wxsParent* Item,bool ShowMessage)
131 {
132     if ( ShowMessage )
133     {
134         cbMessageBox(_("wxsStatusBar must be added as tool"));
135     }
136     return false;
137 }
138 
139 
OnAddExtraProperties(wxsPropertyGridManager * Grid)140 void wxsStatusBar::OnAddExtraProperties(wxsPropertyGridManager* Grid)
141 {
142     #if wxCHECK_VERSION(3, 0, 0) || wxCHECK_PROPGRID_VERSION(1, 4, 0)
143     Grid->SelectPage(0);
144     #else
145     Grid->SetTargetPage(0);
146     #endif
147     m_FieldsId = Grid->Append(NEW_IN_WXPG14X wxIntProperty(_("Fields"),wxPG_LABEL,m_Fields));
148 
149     m_FieldsId->SetAttribute(wxPG_ATTR_MIN, 1);
150     m_FieldsId->SetAttribute(wxPG_ATTR_MAX, 30);
151 
152     for ( int i=0; i<m_Fields; i++ )
153     {
154         wxPGId ParentProp = Grid->Append(NEW_IN_WXPG14X wxParentProperty(wxString::Format(_("Field %d"),i+1),wxPG_LABEL));
155         ParentProp->ChangeFlag(wxPG_PROP_READONLY, true);
156         m_WidthsIds[i] = Grid->AppendIn(ParentProp,NEW_IN_WXPG14X wxIntProperty(_("Width"),wxPG_LABEL,m_Widths[i]));
157         m_VarWidthIds[i] = Grid->AppendIn(ParentProp,NEW_IN_WXPG14X wxBoolProperty(_T("Variable width"),wxPG_LABEL,m_VarWidth[i]));
158         Grid->SetPropertyAttribute(m_VarWidthIds[i],wxPG_BOOL_USE_CHECKBOX,1L,wxPG_RECURSE);
159         m_StylesIds[i] = Grid->AppendIn(ParentProp,NEW_IN_WXPG14X wxEnumProperty(_("Style"),wxPG_LABEL,FieldStyles,FieldStylesVal,m_Styles[i]));
160         m_ParentIds[i] = ParentProp;
161     }
162 
163     wxsItem::OnAddExtraProperties(Grid);
164 }
165 
OnExtraPropertyChanged(wxsPropertyGridManager * Grid,wxPGId Id)166 void wxsStatusBar::OnExtraPropertyChanged(wxsPropertyGridManager* Grid,wxPGId Id)
167 {
168     if ( Id == m_FieldsId )
169     {
170         // Number of fields is going to change...
171         int NewFields = Grid->GetPropertyValueAsInt(Id);
172         if ( NewFields < 1 )
173         {
174             NewFields = 1;
175             Grid->SetPropertyValue(Id,NewFields);
176         }
177 
178         // Now it's time to delete / add properties for fields
179         if ( NewFields < m_Fields )
180         {
181             for ( int i=NewFields; i<m_Fields; i++ )
182             {
183                 #if wxCHECK_VERSION(3, 0, 0) || wxCHECK_PROPGRID_VERSION(1, 4, 0)
184                 Grid->DeleteProperty(m_ParentIds[i]);
185                 #else
186                 Grid->Delete(m_ParentIds[i]);
187                 #endif
188             }
189         }
190         else if ( NewFields > m_Fields )
191         {
192             // Adding new properties
193             #if wxCHECK_VERSION(3, 0, 0) || wxCHECK_PROPGRID_VERSION(1, 4, 0)
194             Grid->SelectPage(0);
195             #else
196             Grid->SetTargetPage(0);
197             #endif
198             UpdateArraysSize(NewFields);
199             for ( int i = m_Fields; i<NewFields; i++ )
200             {
201                 wxPGId ParentProp = Grid->Append(NEW_IN_WXPG14X wxParentProperty(wxString::Format(_("Field %d"),i+1),wxPG_LABEL));
202                 ParentProp->ChangeFlag(wxPG_PROP_READONLY, true);
203                 m_WidthsIds[i] = Grid->AppendIn(ParentProp,NEW_IN_WXPG14X wxIntProperty(_("Width"),wxPG_LABEL,m_Widths[i]));
204                 m_VarWidthIds[i] = Grid->AppendIn(ParentProp,NEW_IN_WXPG14X wxBoolProperty(_T("Variable width"),wxPG_LABEL,m_VarWidth[i]));
205                 Grid->SetPropertyAttribute(m_VarWidthIds[i],wxPG_BOOL_USE_CHECKBOX,1L,wxPG_RECURSE);
206                 m_StylesIds[i] = Grid->AppendIn(ParentProp,NEW_IN_WXPG14X wxEnumProperty(_("Style"),wxPG_LABEL,FieldStyles,FieldStylesVal,m_Styles[i]));
207                 m_ParentIds[i] = ParentProp;
208             }
209         }
210 
211         m_Fields = NewFields;
212         NotifyPropertyChange(true);
213         return;
214     }
215 
216     for ( int i=0; i<m_Fields; i++ )
217     {
218         if ( m_ParentIds[i]   == Id ||
219              m_WidthsIds[i]   == Id ||
220              m_StylesIds[i]   == Id ||
221              m_VarWidthIds[i] == Id )
222         {
223             m_Widths[i] = Grid->GetPropertyValueAsLong(m_WidthsIds[i]);
224             m_Styles[i] = Grid->GetPropertyValueAsLong(m_StylesIds[i]);
225             m_VarWidth[i] = Grid->GetPropertyValueAsBool(m_VarWidthIds[i]);
226             if ( m_Widths[i] < 0 )
227             {
228                 m_Widths[i] = -m_Widths[i];
229                 m_VarWidth[i] = !m_VarWidth[i];
230                 Grid->SetPropertyValue(m_WidthsIds[i],m_Widths[i]);
231                 Grid->SetPropertyValue(m_VarWidthIds[i],m_VarWidth[i]);
232             }
233             NotifyPropertyChange(true);
234             return;
235         }
236     }
237 
238     wxsItem::OnExtraPropertyChanged(Grid,Id);
239 }
240 
OnXmlRead(TiXmlElement * Element,bool IsXRC,bool IsExtra)241 bool wxsStatusBar::OnXmlRead(TiXmlElement* Element,bool IsXRC,bool IsExtra)
242 {
243     if ( IsXRC )
244     {
245         TiXmlElement* FieldsCnt = Element->FirstChildElement("fields");
246         if ( !FieldsCnt )
247         {
248             m_Fields = 1;
249         }
250         else
251         {
252             m_Fields = wxAtoi(cbC2U(FieldsCnt->GetText()));
253         }
254 
255         if ( m_Fields < 1 ) m_Fields = 1;
256         UpdateArraysSize(m_Fields);
257 
258         wxString WidthsStr;
259         wxString StylesStr;
260         if ( TiXmlElement* WidthsElem = Element->FirstChildElement("widths") )
261         {
262             WidthsStr = cbC2U(WidthsElem->GetText());
263         }
264         if ( TiXmlElement* StylesElem = Element->FirstChildElement("styles") )
265         {
266             StylesStr = cbC2U(StylesElem->GetText());
267         }
268 
269         for ( int i=0; i<m_Fields; i++ )
270         {
271             m_Widths[i] = wxAtoi(WidthsStr.BeforeFirst(_T(',')));
272             if ( WidthsStr.Find(_T(',')) != -1 )
273             {
274                 WidthsStr.Remove(0,WidthsStr.Find(_T(','))+1);
275             }
276             if ( m_Widths[i] < 0 )
277             {
278                 m_VarWidth[i] = true;
279                 m_Widths[i] = -m_Widths[i];
280             }
281             else
282             {
283                 m_VarWidth[i] = false;
284             }
285 
286             wxString Style = StylesStr.BeforeFirst(_T(','));
287             if ( Style==_T("wxSB_FLAT") )
288             {
289                 m_Styles[i] = wxSB_FLAT;
290             }
291             else if ( Style==_T("wxSB_RAISED") )
292             {
293                 m_Styles[i] = wxSB_RAISED;
294             }
295             else
296             {
297                 m_Styles[i] = wxSB_NORMAL;
298             }
299 
300             if ( StylesStr.Find(_T(',')) != -1 )
301             {
302                 StylesStr.Remove(0,StylesStr.Find(_T(','))+1);
303             }
304         }
305     }
306 
307     return wxsParent::OnXmlRead(Element,IsXRC,IsExtra);
308 }
309 
OnXmlWrite(TiXmlElement * Element,bool IsXRC,bool IsExtra)310 bool wxsStatusBar::OnXmlWrite(TiXmlElement* Element,bool IsXRC,bool IsExtra)
311 {
312     if ( IsXRC )
313     {
314         Element->InsertEndChild(TiXmlElement("fields"))->
315                  InsertEndChild(TiXmlText(cbU2C(wxString::Format(_T("%d"),m_Fields))));
316 
317         wxString Widths;
318         wxString Styles;
319         for ( int i=0; i<m_Fields; i++ )
320         {
321             if ( i>0 )
322             {
323                 Widths << _T(',');
324                 Styles << _T(',');
325             }
326 
327             Widths << wxString::Format(_T("%d"),m_VarWidth[i]?-m_Widths[i]:m_Widths[i]);
328             if ( m_Styles[i] == wxSB_FLAT )
329             {
330                 Styles << _T("wxSB_FLAT");
331             }
332             else if ( m_Styles[i] == wxSB_RAISED )
333             {
334                 Styles << _T("wxSB_RAISED");
335             }
336             else
337             {
338                 Styles << _T("wxSB_NORMAL");
339             }
340         }
341 
342         Element->InsertEndChild(TiXmlElement("widths"))->
343                  InsertEndChild(TiXmlText(cbU2C(Widths)));
344         Element->InsertEndChild(TiXmlElement("styles"))->
345                  InsertEndChild(TiXmlText(cbU2C(Styles)));
346     }
347 
348     return wxsParent::OnXmlWrite(Element,IsXRC,IsExtra);
349 }
350 
UpdateArraysSize(int Size)351 void wxsStatusBar::UpdateArraysSize(int Size)
352 {
353     m_Widths.SetCount(Size,10);
354     m_Styles.SetCount(Size,wxSB_NORMAL);
355     while ( (int)m_VarWidth.GetCount() < Size )
356     {
357         // SetCount didn't work for wxArrayBool
358         m_VarWidth.Add(true);
359     }
360     m_ParentIds.SetCount(Size);
361     m_WidthsIds.SetCount(Size);
362     m_StylesIds.SetCount(Size);
363     m_VarWidthIds.SetCount(Size);
364 }
365