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: 8704 $
19 * $Id: wxsstringproperty.cpp 8704 2012-12-23 20:32:03Z mortenmacfly $
20 * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/plugins/contrib/wxSmith/properties/wxsstringproperty.cpp $
21 */
22 
23 #include "wxsstringproperty.h"
24 
25 #include <globals.h>
26 #include <prep.h>
27 
28 // Helper macro for fetching variable
29 #define VALUE   wxsVARIABLE(Object,Offset,wxString)
30 
31 // TODO: Fix \n handling in property editor
32 
33 
wxsStringProperty(const wxString & PGName,const wxString & DataName,long _Offset,bool _IsLongString,bool _XmlStoreEmpty,const wxString & _Default,int Priority)34 wxsStringProperty::wxsStringProperty(const wxString& PGName, const wxString& DataName,long _Offset,bool _IsLongString,bool _XmlStoreEmpty,const wxString& _Default,int Priority):
35     wxsProperty(PGName,DataName,Priority),
36     Offset(_Offset),
37     IsLongString(_IsLongString),
38     XmlStoreEmpty(_XmlStoreEmpty),
39     Default(_Default)
40 {}
41 
42 
PGCreate(wxsPropertyContainer * Object,wxPropertyGridManager * Grid,wxPGId Parent)43 void wxsStringProperty::PGCreate(wxsPropertyContainer* Object,wxPropertyGridManager* Grid,wxPGId Parent)
44 {
45     wxString Fixed = VALUE;
46     Fixed.Replace(_T("\n"),_T("\\n"));
47     wxPGId Id;
48     if ( IsLongString )
49     {
50         Id = Grid->AppendIn(Parent,NEW_IN_WXPG14X wxLongStringProperty(GetPGName(),wxPG_LABEL,Fixed));
51     }
52     else
53     {
54         Id = Grid->AppendIn(Parent,NEW_IN_WXPG14X wxStringProperty(GetPGName(),wxPG_LABEL,Fixed));
55     }
56     PGRegister(Object,Grid,Id);
57 }
58 
PGRead(cb_unused wxsPropertyContainer * Object,wxPropertyGridManager * Grid,wxPGId Id,cb_unused long Index)59 bool wxsStringProperty::PGRead(cb_unused wxsPropertyContainer* Object,
60                                wxPropertyGridManager* Grid,wxPGId Id,
61                                cb_unused long Index)
62 {
63     VALUE = Grid->GetPropertyValue(Id).GetString();
64     VALUE.Replace(_T("\\n"),_T("\n"));
65     return true;
66 }
67 
PGWrite(cb_unused wxsPropertyContainer * Object,wxPropertyGridManager * Grid,wxPGId Id,cb_unused long Index)68 bool wxsStringProperty::PGWrite(cb_unused wxsPropertyContainer* Object,
69                                 wxPropertyGridManager* Grid,wxPGId Id,
70                                 cb_unused long Index)
71 {
72     wxString Fixed = VALUE;
73     Fixed.Replace(_T("\n"),_T("\\n"));
74     Grid->SetPropertyValue(Id,Fixed);
75     return true;
76 }
77 
XmlRead(cb_unused wxsPropertyContainer * Object,TiXmlElement * Element)78 bool wxsStringProperty::XmlRead(cb_unused wxsPropertyContainer* Object,
79                                 TiXmlElement* Element)
80 {
81     if ( !Element )
82     {
83         VALUE.Clear();
84         return false;
85     }
86     // TODO: Use proper encoding
87     wxString Base = cbC2U(Element->GetText());
88     wxString Result;
89     for ( const wxChar* Ch = Base.c_str(); *Ch; Ch++ )
90     {
91         if ( *Ch == _T('_') )
92         {
93             if ( *++Ch == _T('_') )
94             {
95                 Result << _T('_');
96             }
97             else
98             {
99                 Result << _T('&') << *Ch;
100             }
101         }
102         else if ( *Ch == _T('\\') )
103         {
104             switch ( *++Ch )
105             {
106                 case _T('n'):  Result << _T('\n'); break;
107                 case _T('r'):  Result << _T('\r'); break;
108                 case _T('t'):  Result << _T('\t'); break;
109                 case _T('\\'): Result << _T('\\'); break;
110                 default: Result << _T('\\') << *Ch; break;
111             }
112         }
113         else
114         {
115             Result << *Ch;
116         }
117     }
118     VALUE = Result;
119     return true;
120 }
121 
XmlWrite(cb_unused wxsPropertyContainer * Object,TiXmlElement * Element)122 bool wxsStringProperty::XmlWrite(cb_unused wxsPropertyContainer* Object,
123                                  TiXmlElement* Element)
124 {
125     if ( XmlStoreEmpty || (VALUE != Default) )
126     {
127         wxString Base = VALUE;
128         wxString Result;
129         for ( const wxChar* Ch = Base.c_str(); *Ch; Ch++ )
130         {
131             switch ( *Ch )
132             {
133                 case _T('_'):  Result << _T("__"); break;       // TODO: This is NOT compatible with xrc file when there's no version entry or version is less than 2.3.0.1
134                 //case _T('&'):  Result << _T('_');  break;     // We could leave this to be translated into &amp; but this looks nicer ;)
135                 case _T('\\'): Result << _T("\\\\"); break;
136                 // We could handle \n and \r here too but this is not necessary since XRC loading
137                 // routines also handle \n and \r chars
138                 default:       Result << *Ch;
139             }
140         }
141         // TODO: Use proper encoding
142         Element->InsertEndChild(TiXmlText(cbU2C(Result)));
143         return true;
144     }
145     return false;
146 }
147 
PropStreamRead(cb_unused wxsPropertyContainer * Object,wxsPropertyStream * Stream)148 bool wxsStringProperty::PropStreamRead(cb_unused wxsPropertyContainer* Object,
149                                        wxsPropertyStream* Stream)
150 {
151     return Stream->GetString(GetDataName(),VALUE,Default);
152 }
153 
PropStreamWrite(cb_unused wxsPropertyContainer * Object,wxsPropertyStream * Stream)154 bool wxsStringProperty::PropStreamWrite(cb_unused wxsPropertyContainer* Object,
155                                         wxsPropertyStream* Stream)
156 {
157     return Stream->PutString(GetDataName(),VALUE,Default);
158 }
159