"Fossies" - the Fresh Open Source Software Archive

Member "ivtools-ivtools-2.0.4/src/AttrGlyph/attredit.c" (9 Oct 2020, 6725 Bytes) of package /linux/misc/ivtools-ivtools-2.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "attredit.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 1996,1999 Vectaport Inc.
    3  *
    4  * Permission to use, copy, modify, distribute, and sell this software and
    5  * its documentation for any purpose is hereby granted without fee, provided
    6  * that the above copyright notice appear in all copies and that both that
    7  * copyright notice and this permission notice appear in supporting
    8  * documentation, and that the names of the copyright holders not be used in
    9  * advertising or publicity pertaining to distribution of the software
   10  * without specific, written prior permission.  The copyright holders make
   11  * no representations about the suitability of this software for any purpose.
   12  * It is provided "as is" without express or implied warranty.
   13  *
   14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
   15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
   16  * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
   17  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
   18  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
   19  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
   20  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   21  * 
   22  */
   23 
   24 #include <Attribute/aliterator.h>
   25 #include <Attribute/attribute.h>
   26 #include <Attribute/attrlist.h>
   27 #include <Attribute/attrvalue.h>
   28 #include <Attribute/lexscan.h>
   29 #include <Attribute/paramlist.h>
   30 
   31 #include <AttrGlyph/attredit.h>
   32 
   33 #include <InterViews/action.h>
   34 #include <InterViews/background.h>
   35 #include <InterViews/font.h>
   36 #include <InterViews/layout.h>
   37 #include <InterViews/style.h>
   38 #include <IV-look/dialogs.h>
   39 #include <IV-look/kit.h>
   40 #include <OS/string.h>
   41 
   42 #include <IVGlyph/textedit.h>
   43 #include <IVGlyph/textview.h>
   44 
   45 #include <ctype.h>
   46 #include <strstream>
   47 #include <string.h>
   48 #if __GNUC__==2 && __GNUC_MINOR__<=7
   49 #else
   50 #define STL_VECTOR
   51 #include <vector>
   52 #endif
   53 
   54 using std::vector;
   55 
   56 declareActionCallback(AttributeListEditor)
   57 implementActionCallback(AttributeListEditor)
   58 declareFieldEditorCallback(AttributeListEditor)
   59 implementFieldEditorCallback(AttributeListEditor)
   60 
   61 AttributeListEditor::AttributeListEditor(AttributeList* al)
   62 : Patch(nil) {
   63     _list = al;
   64     Resource::ref(_list);
   65     DialogKit& dk = *DialogKit::instance();
   66     WidgetKit& wk = *WidgetKit::instance();
   67     _namefe = dk.field_editor("", wk.style(), 
   68                   new FieldEditorCallback(AttributeListEditor)
   69                   (this, &AttributeListEditor::fe_add, 
   70                    &AttributeListEditor::fe_clr));
   71     _valfe = dk.field_editor("", wk.style(),
   72                  new FieldEditorCallback(AttributeListEditor)
   73                  (this, &AttributeListEditor::fe_add, 
   74                   &AttributeListEditor::fe_clr));
   75     Style* s = new Style(wk.style());
   76     s->attribute("rows", "10");
   77     s->attribute("columns", "30");
   78     _ete = new EivTextEditor(s, false);
   79     _ete->ref();
   80     _ete->textview()->disable_caret();
   81     build();
   82 }
   83 
   84 AttributeListEditor::~AttributeListEditor() {
   85 //    _ete->unref();
   86   Resource::unref(_list);
   87 }
   88 
   89 AttributeList* AttributeListEditor::list() {
   90     return _list;
   91 }
   92 
   93 void AttributeListEditor::add() {
   94     const String* txt = _namefe->text();
   95     if (txt->length() > 0) {
   96     char* buf = new char[strlen(_valfe->text()->string())+2];
   97     sprintf(buf, "%s\n", _valfe->text()->string());
   98     while(isspace(*buf) && *buf!='\0') buf++;
   99     int negate = *buf=='-';
  100     int skip = negate || *buf=='+';
  101     AttributeValue* av = ParamList::lexscan()->get_attrval(buf+skip, strlen(buf+skip));
  102     if(negate) av->negate();
  103     _list->add_attr(txt->string(), av);
  104     update_text(true);
  105     }
  106 }
  107 
  108 void AttributeListEditor::remove() {
  109     const String* txt = _namefe->text();
  110     if (txt->length() > 0) {
  111     Attribute* attr;
  112     if (attr = _list->GetAttr(txt->string())) {
  113         _list->Remove(attr);
  114         update_text(true);
  115     }
  116     }
  117 }
  118 
  119 void AttributeListEditor::update_text(boolean update) {
  120     ALIterator i;
  121 #ifndef STL_VECTOR
  122     char buf[1024];
  123     memset(buf, 0, 1024);
  124 #else
  125     vector <char> vbuf;
  126 #endif
  127     for (_list->First(i); !_list->Done(i); _list->Next(i)) {
  128     Attribute* attr = _list->GetAttr(i);
  129     const char* name = attr->Name();
  130     int namelen = name ? strlen(name) : 0;
  131     if (name)
  132 #ifndef STL_VECTOR
  133         strcat(buf, name);
  134 #else
  135     {
  136         const char* namep = name;
  137         while (*namep) { vbuf.push_back(*namep++); }
  138     }
  139 #endif
  140     int n;
  141     for (n = 15; n > namelen-1; n--)
  142 #ifndef STL_VECTOR
  143         strcat(buf, " ");
  144 #else
  145         vbuf.push_back(' ');
  146 #endif
  147 #ifndef STL_VECTOR
  148     strcat(buf, " ");
  149 #else
  150         vbuf.push_back(' ');
  151 #endif
  152     std::strstream valstr;
  153     valstr << *attr->Value() << '\0';
  154     const char* val = valstr.str();
  155     int vallen = val ? strlen(val) : 0;
  156     if (val)
  157 #ifndef STL_VECTOR
  158         strcat(buf, val);
  159 #else
  160     {
  161         const char* valp = val;
  162         while (*valp)  { vbuf.push_back(*valp++); }
  163     }
  164 #endif
  165     for (n = 15; n > vallen; n--)
  166 #ifndef STL_VECTOR
  167         strcat(buf, " ");
  168     strcat(buf, "\n");
  169 #else
  170             vbuf.push_back(' ');
  171     vbuf.push_back('\n');
  172 #endif
  173 
  174     }
  175 #ifndef STL_VECTOR
  176     _ete->text(buf, update);
  177 #else
  178     vbuf.push_back('\0');
  179     _ete->text(&vbuf[0], update);
  180 #endif
  181 }
  182 
  183 void AttributeListEditor::build() {
  184     DialogKit& dk = *DialogKit::instance();
  185     WidgetKit& wk = *WidgetKit::instance();
  186     const LayoutKit& lk = *LayoutKit::instance();
  187     PolyGlyph* _mainglyph = lk.vbox();
  188 //    _mainglyph->append(lk.hcenter(wk.label(_lab)));
  189     Glyph* glu = lk.vspace(5);
  190     PolyGlyph* _namebox = lk.vbox();
  191     PolyGlyph* _valbox = lk.vbox();
  192     InputHandler* ih = new InputHandler(nil, wk.style());
  193     Coord wid = wk.font()->width("MMMMMMMMMMMMMMM", 15);
  194 
  195     Action* addaction = new ActionCallback(AttributeListEditor)(
  196     this, &AttributeListEditor::add
  197     );
  198     Button* addbutton = wk.push_button("Add", addaction);
  199     Action* remaction = new ActionCallback(AttributeListEditor)(
  200     this, &AttributeListEditor::remove
  201     );
  202     Button* rembutton = wk.push_button("Remove", remaction);
  203     _mainglyph->append(
  204     lk.hcenter(
  205         lk.hbox(
  206         lk.vcenter(addbutton),
  207         lk.hspace(10),
  208         lk.vcenter(rembutton)
  209         )
  210     )
  211     );
  212     _mainglyph->append(lk.vspace(10));
  213     _mainglyph->append(
  214     lk.hcenter(
  215         lk.hbox(
  216             lk.vcenter(lk.hfixed(wk.label("name"), wid)),
  217         lk.hspace(10),
  218             lk.vcenter(lk.hfixed(wk.label("value"), wid))
  219         )
  220     )
  221     );
  222     _mainglyph->append(lk.vspace(2));
  223     _mainglyph->append(
  224     lk.hcenter(
  225         lk.hbox(
  226         lk.vcenter(lk.hfixed(_namefe, wid)),
  227         lk.hspace(10),
  228         lk.vcenter(lk.hfixed(_valfe, wid))
  229         )
  230     )
  231     );
  232     _mainglyph->append(lk.vspace(15));
  233     update_text(false);
  234     _mainglyph->append(lk.hcenter(lk.hspace(300)));
  235     _mainglyph->append(lk.hcenter(_ete));
  236     ih->body(wk.outset_frame(lk.margin(_mainglyph, 10)));
  237     body(ih);
  238 }
  239 
  240 void AttributeListEditor::fe_clr(FieldEditor* fe) {
  241     fe->field("");
  242 }