File: optswx.cpp

package info (click to toggle)
openbabel 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 36,644 kB
  • ctags: 33,717
  • sloc: cpp: 242,528; ansic: 87,037; sh: 10,280; perl: 5,518; python: 5,156; pascal: 793; makefile: 747; cs: 392; xml: 97; ruby: 54; java: 23
file content (380 lines) | stat: -rw-r--r-- 12,937 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**********************************************************************
optswx.cpp -  Constructs wxWidgets Option controls from description text

Copyright (C) 2006 by Chris Morley

This file is part of the Open Babel project.
For more information, see <http://openbabel.sourceforge.net/>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
***********************************************************************/
#include <openbabel/babelconfig.h>
#include <stdwx.h>
#include <sstream>
#include <utility>
#include <optswx.h>

/*
The string returned by OBFormat::Description() gives information about the
format and the commandline options that can be used with it. More general
options have similar descriptions in for instance in 
OBConversion::TargetClassDescription(). These strings are parsed in 
OBGUIFrame::OnChangeFormat and the following routine so that the GUI can
present these options as checkboxes, editboxes, etc. The method was originally
designed to use the existing descriptions as far as possible, so that the
information is extracted without having to add special codes, etc. It is
fairly unobtrusive, but liable to breaking because of alterations which
had more significance than the person making then was expecting.

For format descriptions options for Read and Write are extracted with 
separate calls to DynOptionswx::Construct(). The list of options starts with
a a line containing "Options". Those for reading have "Read" or "Input" 
preceding this on the same line and those for writing have "Write", "Output"
or nothing preceding it on the same line. These are all case insensitive.
The options themselves are one to a line on subsequent lines, ending with a 
blank lineor the end of the string.

Each option has a name, which can be a single letter. Any punctuation 
preceding thename is ignored.

If the first character after the name, other than space or tab, is a 
punctuation character, then the option will be displayed as an edit box,
to be used to enter parameters.

--property <attrib> <value> add or replace a property\n

The name of this parameter, e.g. "attrib" and "value" in the example,
is not displyed in the GUI. Additional editboxes are displayed if the first
character after the next whitespace is punctuation. If it is a letter, it is
the start of the caption text which continues to the end of the line (but see
below). The caption is displyed after the edit box(es).Edit boxes for options
with multicharacter names are larger than for those with single character names.

If the last character of the line is ':' then a full-line edit box is
displayed and the caption is displayed before it.

If the text contains the word "default", the subsequent word is written into the
editbox. It will have some punctuation characters stripped from its start and end,
so that 
q <day> Starting day, default <Sunday>
 will display as an edit box containing "Sunday" and caption "Starting day".

If the first character after the option name, other than space or tab, is a
letter, then a checkbox is usually displayed.
  c  continuous output: no formatting\n

If the text contains "default", the checkbox is displayed checked and the
caption contains only the text up to this point. If the follow word is 
"no", "not or "none" the checkbox is unchecked.

A set of radio buttons are displayed, rather than a checkbox if "or" is the 
last word of the caption. Including "default" in the caption checks that button.
For example
1  output CML V1.0  or \n \
2  output CML V2.0 (default)\n \

*/
////////////////////////////////////////////////////
DynOptionswx::~DynOptionswx()
{ Clear();}

void DynOptionswx::Clear()
{
  OMapType::iterator itr;
  for(itr=OptionMap.begin();itr!=OptionMap.end();++itr)
  {
    sizer->Detach(itr->second);
    (itr->second)->Destroy();
  }
  OptionMap.clear();
  std::vector<wxSizer*>::iterator itrs;
  for(itrs=Sizers.begin();itrs!=Sizers.end();++itrs)
  {
    sizer->Detach(*itrs);
    delete *itrs;
  }
  Sizers.clear();
}

bool DynOptionswx::Construct(const char* OptionsText, const char* StartText, int MultiCharFilter)
{
  //Looks for "options" (case-insensitive)
  //If StartText is not NULL it must precede "options" on the same line and 
  //not be part of a longer word
  const int ONE=1;
  const int FOUR=4;
  bool NextIsRadio=false;
  char* pNewStr = new char[strlen(OptionsText)+1];
  strcpy(pNewStr,OptionsText); //Make a working copy 
  char* p = pNewStr;
  
  bool OptionsFound=false;
  char* lineend = NULL;
  
  if(StartText)
  {
    do
    {
      p=strcasestr(p,StartText);//locate StartText
      if(!p) break;
      p += strlen(StartText);
    }while(isalpha(*p));//next char is not a letter or number
    if(p)
      lineend=strchr(p,'\n');
  }
  if(p && (p=strcasestr(p,"option")) && (!lineend  || p<lineend))
  {
    OptionsFound=true;

    p = strchr(p,'\n')+1; //options start on next line		
    while(p && p-1 && *p && *p!='\n') //loop for all options until blank line
    {
      int ProvideEditCtl=0;
      while(*p && !isalnum(*(p++))); //skip space and punctuation
      if(!(*p--)) break;
      wxString oname;
      while(isalnum(*p))
        oname += *p++;

      //Filter on whether option is single or multicharacter
      //MultiCharFilter == 0 Display all options
      //MultiCharFilter == 1 Display only single char options
      //MultiCharFilter == 2 Display only multi char options
      if((MultiCharFilter==1 && oname.size()>1)  
        || (MultiCharFilter==2 && oname.size()==1))  
      {
        p =strchr(p,'\n');
        if(p) ++p; //to next line
        continue;
      }

      while(*p>=0 && isspace(*p++));//skip white space
      while(ispunct(*(--p)) && *p != '-')
      {
        //If first non-white space character after option char is punctuation(except for '-')
        //Provide an edit control rather than a checkbox
        ++ProvideEditCtl;
        char endch=0;
        switch(*p++)
        {
        case '\"':
        case '\'':
          endch=*(p-1); break;
        case '<':
          endch='>'; break;
        case '[':
          endch= ']'; break;
        case '(':
          endch=')'; break;
        }
        if(endch)
        {
          char* ptemp = strchr(p, endch);
          if(ptemp)
            p=ptemp+1;
        }
        while(isspace(*p++));//skip white space
        if(!(*p)) break;
      }
      p--; //so not to lose first char of caption

      while(!isalnum(*(++p))); //skip space and punctuation
      if(!(*p)) break;
      char* pCaption = p-1;
      p =strchr(p,'\n');
      if(p)
        *p++ ='\0'; //mark end of this option's text

      if(ProvideEditCtl)
      {
        //First the caption
        char* pdef;
        char* pdefWord=NULL;
        if(pdef=strcasestr(pCaption,"default"))
        {
          //Put the next word in the editbox
          char* tok=strtok(pdef," :-\t</\"\'");
          pdefWord = strtok(NULL," :-</\t;,\"\'>");

          //delete caption after default or after <default etc
          *pdef='\0';
          while(isspace(*(--pdef)));
          if(!strpbrk(pdef,"([<{-;")) pdef++ ;
          *pdef='\0';
        }
        wxStaticText* pEdCaption = new wxStaticText(parent,wxID_STATIC,pCaption);
        OptionMap.push_back(std::make_pair(wxString(),pEdCaption));//string is empty for a caption: not an option

        //Edit boxes for multicharacter options are larger
        const int EDWIDTH = oname.size()>1? 60 : 40;
        wxTextCtrl* pEd;

        //Make a large edit box on the next line if last char is of the caption is :
        bool BigEdit =(pCaption[strlen(pCaption)-1] == ':'); 
        if(BigEdit)
        {
          sizer->Add(pEdCaption,0,wxEXPAND|wxTOP,FOUR);
          while(ProvideEditCtl--)
          {
            pEd = new wxTextCtrl(parent,wxID_ANY,wxEmptyString,
                wxDefaultPosition,wxSize(EDWIDTH,16));
            OptionMap.push_back(std::make_pair(oname,pEd));
            if(ProvideEditCtl)
              oname = ' ' + oname;//editboxes except the first have name preceded by one or more spaces
          }
          sizer->Add(pEd,0,wxEXPAND|wxTOP,ONE);
        }
        else
        {
          wxBoxSizer* pEdSizer = new wxBoxSizer(wxHORIZONTAL);
          while(ProvideEditCtl--)
          {
            pEd = new wxTextCtrl(parent,wxID_ANY,wxEmptyString,
                wxDefaultPosition,wxSize(EDWIDTH,16));
            OptionMap.push_back(std::make_pair(oname,pEd));
            if(ProvideEditCtl)
              oname = ' ' + oname;//editboxes except the first have name preceded by one or more spaces
            pEdSizer->Add(pEd,0);
          }
          pEdSizer->Add(pEdCaption,1,wxLEFT|wxALIGN_CENTER_VERTICAL,FOUR);
          sizer->Add(pEdSizer,0,wxEXPAND|wxTOP,FOUR);
          Sizers.push_back(pEdSizer);
        }
        pEd->AppendText(pdefWord);
      }
      else
      {
        // Checkbox, or radio button ("or" is the last word in the caption && no letters after " or")
        wxControl* pChk;
        
        //First see if should be checked
        //If 'default appears in caption set the checkbox unless it is followed
        //by one of 'no' 'not' or 'none'
        char* pdef=strcasestr(pCaption,"default");
        bool SetChk=(pdef!=NULL);
        if(SetChk)
          strcasecmp(pdef,"no") && !strcasecmp(pdef,"not") && strcasecmp(pdef,"none");

        char* por=strcasestr(pCaption," or");
        bool HasOr = (por && !strpbrk(por+3,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));

        if(NextIsRadio || HasOr)
        {
          unsigned int style = NextIsRadio ? 0 : wxRB_GROUP; //Style only on first radiobutton
          pChk = new wxRadioButton(parent,wxID_ANY,pCaption,
                wxDefaultPosition, wxDefaultSize, style);
          NextIsRadio = HasOr;
          ((wxRadioButton*)pChk)->SetValue(SetChk);
        }
        else
        {
          pChk = new wxCheckBox(parent,wxID_ANY,pCaption);
          ((wxCheckBox*)pChk)->SetValue(SetChk);
        }
      
        OptionMap.push_back(std::make_pair(oname,pChk));
        sizer->Add(pChk,0,wxEXPAND|wxTOP,FOUR);
      }
    }
  }
  if(OptionsFound)
  {
    wxStaticLine* pLine = new wxStaticLine(parent);
    sizer->Add(pLine,0,wxTOP|wxEXPAND,FOUR);
    OptionMap.push_back(std::make_pair(wxString(),pLine));//empty string
  }
  delete [] pNewStr;
  return OptionsFound;
}

//////////////////////////////////////////////////////////


int DynOptionswx::SetOptions(OpenBabel::OBConversion& Conv, OpenBabel::OBConversion::Option_type opttyp)
{
  //Now sets options directly in OBConversion
  int count=0;
  OMapType::iterator itr;
  for (itr = OptionMap.begin(); itr != OptionMap.end(); ++itr)
  {
    if(itr->first.empty()) continue; //just a caption or a line
    wxCheckBox* pChk = dynamic_cast<wxCheckBox*> (itr->second);
    if(pChk)
    {	
      if(pChk->IsChecked())
      {
        Conv.AddOption(itr->first, opttyp);
        ++count;
      }
    }
    else
    {
      wxRadioButton* pRadio = dynamic_cast<wxRadioButton*> (itr->second);
      if(pRadio)
      {
        if(pRadio->GetValue())
        {
          Conv.AddOption(itr->first, opttyp);
          ++count;
        }
      }
      else
      {
        wxTextCtrl* pText = dynamic_cast<wxTextCtrl*> (itr->second);
        if(pText)
        {
          wxString txt = pText->GetValue();
          if(txt.IsEmpty()) continue;
          wxString oname = itr->first;

          //Get the contents of subsequent editboxes 
          OMapType::iterator itr2 = itr;
          while(++itr2!= OptionMap.end())
          {
            if(itr2->first[0]!=' ') //subsequent editboxes have the name preceded by a space
              break;
            txt = txt + ' ' + static_cast<wxTextCtrl*>(itr2->second)->GetValue();
            ++itr;
          }
          Conv.AddOption(oname, opttyp, txt);
          ++count;
        }
      }
    }
  }
  return count;
}

//////////////////////////////////////////////////////////
char* DynOptionswx::strcasestr(const char* haystack, const char* needle)
{
  //Adapted from http://primates.ximian.com/~fejj/strlib.c
  register unsigned char *h, *n, *hc, *nc;
  size_t needlelen = strlen (needle);
  if (needlelen == 0)
    return (char *) haystack;

  h=(unsigned char *)haystack;
  n=(unsigned char *)needle;
  while (*(h + needlelen - 1))
  {
    if (tolower(*h) == tolower(*n))
    {
      for (hc = h + 1, nc = n + 1; *hc && *nc; hc++, nc++)
        if (tolower(*hc) != tolower(*nc))
          break;
      
      if (!*nc)
        return (char*) h;
    }		
    h++;
  }
  return NULL;
}