~ubuntu-branches/ubuntu/trusty/codeblocks/trusty-proposed

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxSmith/properties/wxseditenumproperty.cpp

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng
  • Date: 2013-05-06 00:20:02 UTC
  • mfrom: (6.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130506002002-ngc7bwkewnak8fja
Tags: 12.11-3
* Upload to unstable.
* Update watch file, thanks to Bart Martens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** \file wxseditenumproperty.cpp
 
2
*
 
3
* This file is part of wxSmith plugin for Code::Blocks Studio
 
4
* Copyright (C) 2010  Gary Harris.
 
5
*
 
6
* wxSmith is free software; you can redistribute it and/or modify
 
7
* it under the terms of the GNU General Public License as published by
 
8
* the Free Software Foundation; either version 3 of the License, or
 
9
* (at your option) any later version.
 
10
*
 
11
* wxSmith is distributed in the hope that it will be useful,
 
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
14
* GNU General Public License for more details.
 
15
*
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with wxSmith. If not, see <http://www.gnu.org/licenses/>.
 
18
*
 
19
* This code was taken from the wxSmithImage plug-in, copyright Ron Collins
 
20
* and released under the GPL.
 
21
*/
 
22
 
 
23
#include "wxseditenumproperty.h"
 
24
 
 
25
#include <globals.h>
 
26
 
 
27
// Helper macro for fetching variable
 
28
#define VALUE   wxsVARIABLE(Object,Offset,wxString)
 
29
 
 
30
wxsEditEnumProperty::wxsEditEnumProperty(const wxString &PGName,
 
31
        const wxString &DataName,
 
32
        long _Offset,
 
33
        const wxChar **_Names,
 
34
        bool _UpdateEnteries,
 
35
        const wxString &_Default,
 
36
        bool _XmlStoreEmpty,
 
37
        bool _UseNamesInXml,
 
38
        int Priority):
 
39
    wxsProperty(PGName, DataName, Priority),
 
40
    Offset(_Offset),
 
41
    Default(_Default),
 
42
    XmlStoreEmpty(_XmlStoreEmpty),
 
43
    UpdateEnteries(_UpdateEnteries),
 
44
    Names(_Names),
 
45
    UseNamesInXml(_UseNamesInXml)
 
46
{
 
47
    int     i;
 
48
 
 
49
    // the calling routine only needs the returned value as a string
 
50
    // the list of long Values[] is useless to the calling routine
 
51
    // so, let us hope that we have enough Values[] here
 
52
 
 
53
    for(i = 0; i < 512; i++) Values[i] = i;
 
54
}
 
55
 
 
56
/*! \brief Register the control with a property grid.
 
57
 *
 
58
 * \param Object wxsPropertyContainer*
 
59
 * \param Grid wxPropertyGridManager*
 
60
 * \param Parent wxPGId
 
61
 * \return void
 
62
 *
 
63
 */
 
64
void wxsEditEnumProperty::PGCreate(wxsPropertyContainer *Object, wxPropertyGridManager *Grid, wxPGId Parent)
 
65
{
 
66
    wxPGChoices PGC(Names, Values);
 
67
 
 
68
    PGRegister(Object, Grid, Grid->AppendIn(Parent, NEW_IN_WXPG14X wxEditEnumProperty(GetPGName(), wxPG_LABEL, PGC, VALUE)));
 
69
}
 
70
 
 
71
/*! \brief Read a property value.
 
72
 *
 
73
 * \param Object wxsPropertyContainer*
 
74
 * \param Grid wxPropertyGridManager*
 
75
 * \param Id wxPGId
 
76
 * \param Index long
 
77
 * \return bool
 
78
 *
 
79
 * \date 27/8/10
 
80
 * Updated by Cryogen to use the item name rather than the enumerator value under wxPropertyGrid 1.4.
 
81
 */
 
82
bool wxsEditEnumProperty::PGRead(wxsPropertyContainer *Object, wxPropertyGridManager *Grid, wxPGId Id, long Index)
 
83
{
 
84
    VALUE = Grid->GetPropertyValueAsString(Id);
 
85
    VALUE.Replace(_T("\\n"), _T("\n"));
 
86
 
 
87
    return true;
 
88
}
 
89
 
 
90
/*! \brief Write a property value.
 
91
 *
 
92
 * \param Object wxsPropertyContainer*
 
93
 * \param Grid wxPropertyGridManager*
 
94
 * \param Id wxPGId
 
95
 * \param Index long
 
96
 * \return bool
 
97
 *
 
98
 */
 
99
bool wxsEditEnumProperty::PGWrite(wxsPropertyContainer *Object, wxPropertyGridManager *Grid, wxPGId Id, long Index)
 
100
{
 
101
    wxString Fixed = VALUE;
 
102
 
 
103
    Fixed.Replace(_T("\n"), _T("\\n"));
 
104
   if ( UpdateEnteries )
 
105
    {
 
106
        #if wxCHECK_VERSION(2, 9, 0)
 
107
        wxPGChoices choices = Grid->GetGrid()->GetSelection()->GetChoices();
 
108
        choices.Set(Names,Values);
 
109
        #else
 
110
        Grid->GetPropertyChoices(Id).Set(Names,Values);
 
111
        #endif
 
112
    }
 
113
    Grid->SetPropertyValue(Id, Fixed);
 
114
    return true;
 
115
}
 
116
 
 
117
/*! \brief Read XML data.
 
118
 *
 
119
 * \param Object wxsPropertyContainer*
 
120
 * \param Element TiXmlElement*
 
121
 * \return bool
 
122
 *
 
123
 */
 
124
bool wxsEditEnumProperty::XmlRead(wxsPropertyContainer *Object, TiXmlElement *Element)
 
125
{
 
126
    if(!Element)
 
127
    {
 
128
        VALUE.Clear();
 
129
        return false;
 
130
    }
 
131
    // TODO: Use proper encoding
 
132
    wxString Base = cbC2U(Element->GetText());
 
133
    wxString Result;
 
134
    for(const wxChar *Ch = Base.c_str(); *Ch; Ch++)
 
135
    {
 
136
        if(*Ch == _T('_'))
 
137
        {
 
138
            if(*++Ch == _T('_'))
 
139
            {
 
140
                Result << _T('_');
 
141
            }
 
142
            else
 
143
            {
 
144
                Result << _T('&') << *Ch;
 
145
            }
 
146
        }
 
147
        else if(*Ch == _T('\\'))
 
148
        {
 
149
            switch(*++Ch)
 
150
            {
 
151
                case _T('n'):
 
152
                    Result << _T('\n');
 
153
                    break;
 
154
                case _T('r'):
 
155
                    Result << _T('\r');
 
156
                    break;
 
157
                case _T('t'):
 
158
                    Result << _T('\t');
 
159
                    break;
 
160
                case _T('\\'):
 
161
                    Result << _T('\\');
 
162
                    break;
 
163
                default:
 
164
                    Result << _T('\\') << *Ch;
 
165
                    break;
 
166
            }
 
167
        }
 
168
        else
 
169
        {
 
170
            Result << *Ch;
 
171
        }
 
172
    }
 
173
    VALUE = Result;
 
174
    return true;
 
175
}
 
176
 
 
177
/*! \brief Write XML data.
 
178
 *
 
179
 * \param Object wxsPropertyContainer*
 
180
 * \param Element TiXmlElement*
 
181
 * \return bool
 
182
 *
 
183
 */
 
184
bool wxsEditEnumProperty::XmlWrite(wxsPropertyContainer *Object, TiXmlElement *Element)
 
185
{
 
186
    if(XmlStoreEmpty || (VALUE != Default))
 
187
    {
 
188
        wxString Base = VALUE;
 
189
        wxString Result;
 
190
        for(const wxChar *Ch = Base.c_str(); *Ch; Ch++)
 
191
        {
 
192
            switch(*Ch)
 
193
            {
 
194
                case _T('_'):
 
195
                    Result << _T("__");
 
196
                    break;       // TODO: This is NOT compatible with xrc file when there's no version entry or version is less than 2.3.0.1
 
197
                    //case _T('&'):  Result << _T('_');  break;     // We could leave this to be translated into &amp; but this looks nicer ;)
 
198
                case _T('\\'):
 
199
                    Result << _T("\\\\");
 
200
                    break;
 
201
                    // We could handle \n and \r here too but this is not necessary since XRC loading
 
202
                    // routines also handle \n and \r chars
 
203
                default:
 
204
                    Result << *Ch;
 
205
            }
 
206
        }
 
207
        // TODO: Use proper encoding
 
208
        Element->InsertEndChild(TiXmlText(cbU2C(Result)));
 
209
        return true;
 
210
    }
 
211
    return false;
 
212
}
 
213
 
 
214
/*! \brief Read from a property stream.
 
215
 *
 
216
 * \param Object wxsPropertyContainer*
 
217
 * \param Stream wxsPropertyStream*
 
218
 * \return bool
 
219
 *
 
220
 */
 
221
bool wxsEditEnumProperty::PropStreamRead(wxsPropertyContainer *Object, wxsPropertyStream *Stream)
 
222
{
 
223
    return Stream->GetString(GetDataName(), VALUE, Default);
 
224
}
 
225
 
 
226
/*! \brief Write to a property stream.
 
227
 *
 
228
 * \param Object wxsPropertyContainer*
 
229
 * \param Stream wxsPropertyStream*
 
230
 * \return bool
 
231
 *
 
232
 */
 
233
bool wxsEditEnumProperty::PropStreamWrite(wxsPropertyContainer *Object, wxsPropertyStream *Stream)
 
234
{
 
235
    return Stream->PutString(GetDataName(), VALUE, Default);
 
236
}