1 // $Id: Widget.cc,v 1.31 2003/04/07 06:21:25 christof Exp $
2 /*  glade--: C++ frontend for glade (Gtk+ User Interface Builder)
3  *  Copyright (C) 1998  Christof Petig
4  *
5  *  This program 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 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program 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 this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 #include "Widget.hh"
21 #include "writers/WriterBase.hh"
22 #include <cstring>
23 
UniqueName(const std::string & prefix,const void * addr)24 static std::string UniqueName(const std::string &prefix,const void *addr)
25 {  std::string ret(prefix);
26    unsigned long val=(unsigned long)addr;
27    if (sizeof(val)<sizeof(addr))
28       std::cerr<<"Warning: Widget::UniqueName: truncating address\n";
29    unsigned int bit;
30    const int size=5;
31    for (bit=0;bit<8*(sizeof val);bit+=size)
32       ret+="QCDGHKLMJNPVWYZXqcdghklmjnpvwyzx"[(val>>bit)&((1<<size)-1)];
33       // selecting really unreadable names
34    return ret;
35 }
36 
Name() const37 const std::string Widget::Name() const throw()
38 {  if (!name.empty()) return name;
39    if (!Configuration.glade2) name=tag->getString("name");
40    else name=tag->getAttr("id");
41    if (name.empty()) name=UniqueName("Widget",tag);
42    return name;
43 }
44 
get_Child_params() const45 const ChildParamList Widget::get_Child_params() const throw()
46 {  const Tag *t=0;
47    if (Configuration.glade2) t=childtag->find("packing");
48    else t=tag->find("child");
49    if (t) return ChildParamList(*t);
50 
51    static const Tag empty_list("child");
52    return ChildParamList(empty_list);
53 }
54 
subwidgettype(const Widget & w) const55 Subwidget Widget::subwidgettype(const Widget &w) const throw()
56 {  return LookupWriter(*this).IsSubwidget(*this,w);
57 }
58 
getProperty(const std::string & name,const std::string & _default) const59 const std::string Widget::getProperty(const std::string &name, const std::string &_default) const
60 {  if (Configuration.glade2)
61    {  FOR_EACH_CONST_TAG_OF(i,*tag,"property")
62          if (i->getAttr("name")==name)
63             return i->Value();
64       return _default;
65    }
66    else return tag->getString(name,_default);
67 }
68 
hasProperty(const std::string & name) const69 bool Widget::hasProperty(const std::string &name) const
70 {  if (Configuration.glade2)
71    {  FOR_EACH_CONST_TAG_OF(i,*tag,"property")
72          if (i->getAttr("name")==name)
73             return true;
74       return false;
75    }
76    else return tag->hasTag(name);
77 }
78 
setProperty(const std::string & name,const std::string & value)79 void Widget::setProperty(const std::string &name, const std::string &value)
80 {  if (Configuration.glade2)
81    {  FOR_EACH_TAG_OF(i,*tag,"property")
82       {  if (i->getAttr("name")==name)
83          {  i->Value(value);
84             return;
85          }
86       }
87       Tag nw("property",value);
88       nw.setAttr("name",name);
89       tag->push_back(nw);
90    }
91    else tag->mark(name,value);
92 }
93 
Class() const94 const std::string Widget::Class() const throw()
95 {  if (Configuration.glade2)
96    {  if (tag->Type()=="placeholder") return "Placeholder";
97       return tag->getAttr("class");
98    }
99    else return tag->getString("class");
100 }
101 
ChildName() const102 const std::string Widget::ChildName() const throw()
103 {  return Configuration.glade2 ? (!childtag ? "" : childtag->getAttr("internal-child"))
104 			: tag->getString("child_name"); }
hasChildren() const105 bool Widget::hasChildren() const throw()
106 {  return tag->hasTag(Widget::const_contained_iterator::type); }
107 
begin() const108 Widget::const_iterator Widget::begin() const
109 {  return const_iterator(find(tag->begin(),tag->end(),Widget::const_contained_iterator::type),tag->end(),Widget::const_contained_iterator::type);
110 }
111 
begin()112 Widget::iterator Widget::begin()
113 {  assert(!is_const); // too hard?
114    return iterator(find(tag->begin(),tag->end(),Widget::const_contained_iterator::type),tag->end(),Widget::const_contained_iterator::type);
115 }
116 
get_Accels() const117 Widget::const_iterator Widget::get_Accels() const throw()
118 {  return const_iterator(find(tag->begin(),tag->end(),"accelerator"),tag->end(),"accelerator");
119 }
120 
begin_contained(InternalSelection _internal,bool debug) const121 Widget::const_contained_iterator Widget::begin_contained(InternalSelection _internal,bool debug) const
122 {  const_contained_iterator it(find(tag->begin(),tag->end(),Configuration.glade2?"child":"widget"),tag,_internal,debug);
123    return it;
124 }
125 
getParent() const126 Widget Widget::getParent() const throw(std::out_of_range)
127 {  Tag *parent_ptr=tag->parent();
128    if (!parent_ptr) throw std::out_of_range("no parent");
129    if (Configuration.glade2)
130    {  parent_ptr=parent_ptr->parent();
131       if (!parent_ptr || parent_ptr->Type()!="widget") throw std::out_of_range("no parent");
132       return Widget(parent_ptr,parent_ptr->parent());
133    }
134    return Widget(parent_ptr,parent_ptr);
135 }
136