~ubuntu-branches/ubuntu/feisty/openbabel/feisty

« back to all changes in this revision

Viewing changes to windows/VC8/OpenBabelGUI/optswx.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2006-05-14 19:46:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060514194601-h3j1wovvc42cigxl
Tags: 2.0.1-1
* New upstream release. (Closes: #341628)
* debian/patches/04_zipstream_fix.diff: Removed, applied upstream.
* debian/rules (DEB_MAKE_CHECK_TARGET): Readded.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
optswx.cpp -  Constructs wxWidgets Option controls from description text
 
3
 
 
4
Copyright (C) 2006 by Chris Morley
 
5
 
 
6
This file is part of the Open Babel project.
 
7
For more information, see <http://openbabel.sourceforge.net/>
 
8
 
 
9
This program is free software; you can redistribute it and/or modify
 
10
it under the terms of the GNU General Public License as published by
 
11
the Free Software Foundation version 2 of the License.
 
12
 
 
13
This program is distributed in the hope that it will be useful,
 
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
GNU General Public License for more details.
 
17
***********************************************************************/
 
18
#include "babelconfig.h"
 
19
#include "stdwx.h"
 
20
#include <sstream>
 
21
#include <utility>
 
22
#include "optswx.h"
 
23
 
 
24
////////////////////////////////////////////////////
 
25
DynOptionswx::~DynOptionswx()
 
26
{ Clear();}
 
27
 
 
28
void DynOptionswx::Clear()
 
29
{
 
30
        OMapType::iterator itr;
 
31
        for(itr=OptionMap.begin();itr!=OptionMap.end();++itr)
 
32
        {
 
33
                sizer->Detach(itr->second);
 
34
                (itr->second)->Destroy();
 
35
        }
 
36
        OptionMap.clear();
 
37
        std::vector<wxSizer*>::iterator itrs;
 
38
        for(itrs=Sizers.begin();itrs!=Sizers.end();++itrs)
 
39
        {
 
40
                sizer->Detach(*itrs);
 
41
                delete *itrs;
 
42
        }
 
43
        Sizers.clear();
 
44
}
 
45
 
 
46
bool DynOptionswx::Construct(const char* OptionsText, const char* StartText, int MultiCharFilter)
 
47
{
 
48
        //Looks for "options" (case-insensitive)
 
49
        //If StartText is not NULL it must precede "options" on the same line and 
 
50
        //not be part of a longer word
 
51
        const int ONE=1;
 
52
        const int FOUR=4;
 
53
        bool NextIsRadio=false;
 
54
        char* pNewStr = new char[strlen(OptionsText)+1];
 
55
        strcpy(pNewStr,OptionsText); //Make a working copy 
 
56
        char* p = pNewStr;
 
57
        
 
58
        bool OptionsFound=false;
 
59
        char* lineend = NULL;
 
60
        
 
61
        if(StartText)
 
62
        {
 
63
                do
 
64
                {
 
65
                        p=strcasestr(p,StartText);//locate StartText
 
66
                        if(!p) break;
 
67
                        p += strlen(StartText);
 
68
                }while(isalpha(*p));//next char is not a letter or number
 
69
                if(p)
 
70
                        lineend=strchr(p,'\n');
 
71
        }
 
72
        if(p && (p=strcasestr(p,"option")) && (!lineend  || p<lineend))
 
73
        {
 
74
                OptionsFound=true;
 
75
 
 
76
                p = strchr(p,'\n')+1; //options start on next line              
 
77
                while(p && *p && *p!='\n') //loop for all options until blank line
 
78
                {
 
79
                        int ProvideEditCtl=0;
 
80
                        while(*p && !isalnum(*(p++))); //skip space and punctuation
 
81
                        if(!(*p--)) break;
 
82
                        wxString oname;
 
83
                        while(isalnum(*p))
 
84
                                oname += *p++;
 
85
 
 
86
                        //Filter on whether option is single or multicharacter
 
87
                        //MultiCharFilter == 0 Display all options
 
88
                        //MultiCharFilter == 1 Display only single char options
 
89
                        //MultiCharFilter == 2 Display only multi char options
 
90
                        if((MultiCharFilter==1 && oname.size()>1)  
 
91
                                || (MultiCharFilter==2 && oname.size()==1))  
 
92
                        {
 
93
                                p =strchr(p,'\n');
 
94
                                if(p) ++p; //to next line
 
95
                                continue;
 
96
                        }
 
97
 
 
98
                        while(isspace(*p++));//skip white space
 
99
                        while(ispunct(*(--p)) && *p != '-')
 
100
                        {
 
101
                                //If first non-white space character after option char is punctuation(except for '-')
 
102
                                //Provide an edit control rather than a checkbox
 
103
                                ++ProvideEditCtl;
 
104
                                char endch=0;
 
105
                                switch(*p++)
 
106
                                {
 
107
                                case '\"':
 
108
                                case '\'':
 
109
                                        endch=*(p-1); break;
 
110
                                case '<':
 
111
                                        endch='>'; break;
 
112
                                case '[':
 
113
                                        endch= ']'; break;
 
114
                                case '(':
 
115
                                        endch=')'; break;
 
116
                                }
 
117
                                if(endch)
 
118
                                {
 
119
                                        char* ptemp = strchr(p, endch);
 
120
                                        if(ptemp)
 
121
                                                p=ptemp+1;
 
122
                                }
 
123
                                while(isspace(*p++));//skip white space
 
124
                                if(!(*p)) break;
 
125
                        }
 
126
                        p--; //so not to lose first char of caption
 
127
 
 
128
                        while(!isalnum(*(++p))); //skip space and punctuation
 
129
                        if(!(*p)) break;
 
130
                        char* pCaption = p-1;
 
131
                        p =strchr(p,'\n');
 
132
                        if(p)
 
133
                                *p++ ='\0'; //mark end of this option's text
 
134
 
 
135
                        if(ProvideEditCtl)
 
136
                        {
 
137
                                //First the caption
 
138
                                char* pdef;
 
139
                                char* pdefWord=NULL;
 
140
                                if(pdef=strcasestr(pCaption,"default"))
 
141
                                {
 
142
                                        //Put the next word in the editbox
 
143
                                        char* tok=strtok(pdef," :-\t</\"\'");
 
144
                                        pdefWord = strtok(NULL," :-</\t;,\"\'>");
 
145
 
 
146
                                        //delete caption after default or after <default etc
 
147
                                        *pdef='\0';
 
148
                                        while(isspace(*(--pdef)));
 
149
                                        if(!strpbrk(pdef,"([<{-;")) pdef++ ;
 
150
                                        *pdef='\0';
 
151
                                }
 
152
                                wxStaticText* pEdCaption = new wxStaticText(parent,wxID_STATIC,pCaption);
 
153
                                OptionMap.push_back(std::make_pair(wxString(),pEdCaption));//string is empty for a caption: not an option
 
154
 
 
155
                                //Edit boxes for multicharacter options are larger
 
156
                                const int EDWIDTH = oname.size()>1? 60 : 40;
 
157
                                wxTextCtrl* pEd;
 
158
 
 
159
                                //Make a large edit box on the next line if last char is of the caption is :
 
160
                                bool BigEdit =(pCaption[strlen(pCaption)-1] == ':'); 
 
161
                                if(BigEdit)
 
162
                                {
 
163
                                        sizer->Add(pEdCaption,0,wxEXPAND|wxTOP,FOUR);
 
164
                                        while(ProvideEditCtl--)
 
165
                                        {
 
166
                                                pEd = new wxTextCtrl(parent,wxID_ANY,wxEmptyString,
 
167
                                                                wxDefaultPosition,wxSize(EDWIDTH,16));
 
168
                                                OptionMap.push_back(std::make_pair(oname,pEd));
 
169
                                                if(ProvideEditCtl)
 
170
                                                        oname = ' ' + oname;//editboxes except the first have name preceded by one or more spaces
 
171
                                        }
 
172
                                        sizer->Add(pEd,0,wxEXPAND|wxTOP,ONE);
 
173
                                }
 
174
                                else
 
175
                                {
 
176
                                        wxBoxSizer* pEdSizer = new wxBoxSizer(wxHORIZONTAL);
 
177
                                        while(ProvideEditCtl--)
 
178
                                        {
 
179
                                                pEd = new wxTextCtrl(parent,wxID_ANY,wxEmptyString,
 
180
                                                                wxDefaultPosition,wxSize(EDWIDTH,16));
 
181
                                                OptionMap.push_back(std::make_pair(oname,pEd));
 
182
                                                if(ProvideEditCtl)
 
183
                                                        oname = ' ' + oname;//editboxes except the first have name preceded by one or more spaces
 
184
                                                pEdSizer->Add(pEd,0);
 
185
                                        }
 
186
                                        pEdSizer->Add(pEdCaption,1,wxLEFT|wxALIGN_CENTER_VERTICAL,FOUR);
 
187
                                        sizer->Add(pEdSizer,0,wxEXPAND|wxTOP,FOUR);
 
188
                                        Sizers.push_back(pEdSizer);
 
189
                                }
 
190
                                pEd->AppendText(pdefWord);
 
191
                        }
 
192
                        else
 
193
                        {
 
194
                                // Checkbox, or radio button ("or" is the last word in the caption && no letters after " or")
 
195
                                wxControl* pChk;
 
196
                                
 
197
                                //First see if should be checked
 
198
                                //If 'default appears in caption set the checkbox unless it is followed
 
199
                                //by one of 'no' 'not' or 'none'
 
200
                                char* pdef=strcasestr(pCaption,"default");
 
201
                                bool SetChk=(pdef!=NULL);
 
202
                                if(SetChk)
 
203
                                        strcasecmp(pdef,"no") && !strcasecmp(pdef,"not") && strcasecmp(pdef,"none");
 
204
 
 
205
                                char* por=strcasestr(pCaption," or");
 
206
                                bool HasOr = (por && !strpbrk(por+3,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));
 
207
 
 
208
                                if(NextIsRadio || HasOr)
 
209
                                {
 
210
                                        unsigned int style = NextIsRadio ? 0 : wxRB_GROUP; //Style only on first radiobutton
 
211
                                        pChk = new wxRadioButton(parent,wxID_ANY,pCaption,
 
212
                                                                wxDefaultPosition, wxDefaultSize, style);
 
213
                                        NextIsRadio = HasOr;
 
214
                                        ((wxRadioButton*)pChk)->SetValue(SetChk);
 
215
                                }
 
216
                                else
 
217
                                {
 
218
                                        pChk = new wxCheckBox(parent,wxID_ANY,pCaption);
 
219
                                        ((wxCheckBox*)pChk)->SetValue(SetChk);
 
220
                                }
 
221
                        
 
222
                                OptionMap.push_back(std::make_pair(oname,pChk));
 
223
                                sizer->Add(pChk,0,wxEXPAND|wxTOP,FOUR);
 
224
                        }
 
225
                }
 
226
        }
 
227
        if(OptionsFound)
 
228
        {
 
229
                wxStaticLine* pLine = new wxStaticLine(parent);
 
230
                sizer->Add(pLine,0,wxTOP|wxEXPAND,FOUR);
 
231
                OptionMap.push_back(std::make_pair(wxString(),pLine));//empty string
 
232
        }
 
233
        delete [] pNewStr;
 
234
        return OptionsFound;
 
235
}
 
236
 
 
237
//////////////////////////////////////////////////////////
 
238
 
 
239
 
 
240
int DynOptionswx::SetOptions(OpenBabel::OBConversion& Conv, OpenBabel::OBConversion::Option_type opttyp)
 
241
{
 
242
        //Now sets options directly in OBConversion
 
243
        int count=0;
 
244
        OMapType::iterator itr;
 
245
        for (itr = OptionMap.begin(); itr != OptionMap.end(); ++itr)
 
246
        {
 
247
                if(itr->first.empty()) continue; //just a caption or a line
 
248
                wxCheckBox* pChk = dynamic_cast<wxCheckBox*> (itr->second);
 
249
                if(pChk)
 
250
                {       
 
251
                        if(pChk->IsChecked())
 
252
                        {
 
253
                                Conv.AddOption(itr->first, opttyp);
 
254
                                ++count;
 
255
                        }
 
256
                }
 
257
                else
 
258
                {
 
259
                        wxRadioButton* pRadio = dynamic_cast<wxRadioButton*> (itr->second);
 
260
                        if(pRadio)
 
261
                        {
 
262
                                if(pRadio->GetValue())
 
263
                                {
 
264
                                        Conv.AddOption(itr->first, opttyp);
 
265
                                        ++count;
 
266
                                }
 
267
                        }
 
268
                        else
 
269
                        {
 
270
                                wxTextCtrl* pText = dynamic_cast<wxTextCtrl*> (itr->second);
 
271
                                if(pText)
 
272
                                {
 
273
                                        wxString txt = pText->GetValue();
 
274
                                        if(txt.IsEmpty()) continue;
 
275
                                        wxString oname = itr->first;
 
276
 
 
277
                                        //Get the contents of subsequent editboxes 
 
278
                                        OMapType::iterator itr2 = itr;
 
279
                                        while(++itr2!= OptionMap.end())
 
280
                                        {
 
281
                                                if(itr2->first[0]!=' ') //subsequent editboxes have the name preceded by a space
 
282
                                                        break;
 
283
                                                txt = txt + ' ' + static_cast<wxTextCtrl*>(itr2->second)->GetValue();
 
284
                                                ++itr;
 
285
                                        }
 
286
                                        Conv.AddOption(oname, opttyp, txt);
 
287
                                        ++count;
 
288
                                }
 
289
                        }
 
290
                }
 
291
        }
 
292
        return count;
 
293
}
 
294
 
 
295
//////////////////////////////////////////////////////////
 
296
char* DynOptionswx::strcasestr(const char* haystack, const char* needle)
 
297
{
 
298
        //Adapted from http://primates.ximian.com/~fejj/strlib.c
 
299
        register unsigned char *h, *n, *hc, *nc;
 
300
        size_t needlelen = strlen (needle);
 
301
        if (needlelen == 0)
 
302
                return (char *) haystack;
 
303
 
 
304
        h=(unsigned char *)haystack;
 
305
        n=(unsigned char *)needle;
 
306
        while (*(h + needlelen - 1))
 
307
        {
 
308
                if (tolower(*h) == tolower(*n))
 
309
                {
 
310
                        for (hc = h + 1, nc = n + 1; *hc && *nc; hc++, nc++)
 
311
                                if (tolower(*hc) != tolower(*nc))
 
312
                                        break;
 
313
                        
 
314
                        if (!*nc)
 
315
                                return (char*) h;
 
316
                }               
 
317
                h++;
 
318
        }
 
319
        return NULL;
 
320
}