~ubuntu-branches/ubuntu/jaunty/eris/jaunty

« back to all changes in this revision

Viewing changes to Eris/UIFactory.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2004-11-10 11:47:09 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041110114709-ybzwmmnlvzosz3de
Tags: 1.2.1-1
* New upstream release
* Added debian/watch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef HAVE_CONFIG_H
 
2
        #include "config.h"
 
3
#endif
 
4
 
 
5
#include <Eris/UIFactory.h>
 
6
 
 
7
#include <Atlas/Objects/Entity/UIEntity.h>
 
8
#include <Atlas/Objects/Entity/Frame.h>
 
9
#include <Atlas/Objects/Entity/Slot.h>
 
10
 
 
11
#include <sigc++/object_slot.h>
 
12
 
 
13
Eris::UI::Factory::Factory(const Atlas::Objects::Entity::UIEntity& entity,
 
14
                           const std::string& base_type,
 
15
                           const IDList& parents,
 
16
                           const Atlas::Message::Element::MapType& attrs,
 
17
                           BaseGen* gen) :
 
18
        _id(entity.getId()), _id_list(parents), _gen(gen),
 
19
        _persistent(false), _refcount(1), _attrs(attrs)
 
20
{
 
21
  _id_list.push_back(_id);
 
22
  _attrs["display_status"] = entity.getDisplayStatus();
 
23
  Atlas::Objects::Root::const_iterator I, last(entity, base_type);
 
24
  for(I = entity.begin(); I != last; ++I)
 
25
    _attrs[I->first] = I->second;
 
26
}
 
27
 
 
28
Eris::UI::Element*
 
29
Eris::UI::Factory::create(IDMap& idmap) const
 
30
{
 
31
  Element* elem = _gen->create(attrs());
 
32
  for(IDList::const_iterator I = idlist().begin(); I != idlist().end(); ++I)
 
33
    idmap[*I] = elem;
 
34
  return elem;
 
35
}
 
36
 
 
37
Eris::UI::Factory*
 
38
Eris::UI::Factory::parse(const Atlas::Message::Element::MapType& map,
 
39
                         const Bindings&) const
 
40
{
 
41
  Atlas::Objects::Entity::UIEntity entity;
 
42
 
 
43
  // FIXME isn't there a better way to do this?
 
44
  Atlas::Message::Element::MapType::const_iterator I = map.begin();
 
45
  while(I != map.end())
 
46
    entity.setAttr(I->first, I->second);
 
47
 
 
48
  return new Factory(entity, "u_i_entity", idlist(), attrs(), _gen);
 
49
}
 
50
 
 
51
Eris::UI::FrameFactory::~FrameFactory()
 
52
{
 
53
  for(ChildList::iterator I = _children.begin(); I != _children.end(); ++I)
 
54
    (*I)->unref();
 
55
  if(persistent()) // original frame factory, owns BaseGen
 
56
    delete _gen;
 
57
}
 
58
 
 
59
Eris::UI::Element*
 
60
Eris::UI::FrameFactory::create(IDMap& idmap) const
 
61
{
 
62
  FrameElement* frame = _gen->create(_valign, _halign, _rel_pos, attrs());
 
63
 
 
64
  // we don't want child slots connecting to anything
 
65
  // outside this frame, so we use a local idmap
 
66
 
 
67
  IDMap local;
 
68
 
 
69
  for(IDList::const_iterator I = idlist().begin(); I != idlist().end(); ++I)
 
70
    local[*I] = frame;
 
71
 
 
72
  for(ChildList::const_iterator I = _children.begin(); I != _children.end(); ++I) {
 
73
    Element* child = (*I)->create(local);
 
74
    SlotElement* slot = dynamic_cast<SlotElement*>(child);
 
75
    if(slot)
 
76
      frame->packSlot(slot);
 
77
    else
 
78
      frame->pack(child);
 
79
  }
 
80
 
 
81
  // FIXME if one insertion fails, do they all fail?
 
82
  idmap.insert(local.begin(), local.end());
 
83
 
 
84
  return frame;
 
85
}
 
86
 
 
87
Eris::UI::Factory*
 
88
Eris::UI::FrameFactory::parse(const Atlas::Message::Element::MapType& map,
 
89
                              const Bindings& factories) const
 
90
{
 
91
  Atlas::Objects::Entity::Frame frame;
 
92
 
 
93
  // FIXME isn't there a better way to do this?
 
94
  Atlas::Message::Element::MapType::const_iterator I = map.begin();
 
95
  while(I != map.end())
 
96
    frame.setAttr(I->first, I->second);
 
97
 
 
98
  FrameFactory* factory = new FrameFactory(frame, idlist(), factories, attrs(), _gen);
 
99
 
 
100
  // if 'contains' isn't set, then we're dealing with a child
 
101
  // of Frame being used as an opaque type, so we keep the same
 
102
  // children
 
103
  if(factory->_children.empty()) {
 
104
    factory->_valign = _valign;
 
105
    factory->_halign = _halign;
 
106
    factory->_rel_pos = _rel_pos;
 
107
    factory->_children = _children;
 
108
    for(ChildList::const_iterator I = _children.begin(); I != _children.end(); ++I)
 
109
      (*I)->ref();
 
110
    return factory;
 
111
  }
 
112
 
 
113
  return factory;
 
114
}
 
115
 
 
116
Eris::UI::FrameFactory::FrameFactory(const Atlas::Objects::Entity::Frame& frame,
 
117
                                     const IDList& parents, const Bindings& factories,
 
118
                                     const Atlas::Message::Element::MapType& attrs,
 
119
                                     BaseGen* gen)
 
120
        : Factory(frame, "frame", parents, attrs, 0),
 
121
          _valign(frame.getValign()), _halign(frame.getHalign()),
 
122
          _rel_pos(frame.getRelPos()), _gen(gen)
 
123
{
 
124
  const Atlas::Message::Element::ListType& contains = frame.getContains();
 
125
 
 
126
  Atlas::Message::Element::ListType::const_iterator I;
 
127
 
 
128
  for(I = contains.begin(); I != contains.end(); ++I) {
 
129
    Factory* child = factories.findFactory(*I);
 
130
    if(child) {
 
131
      _children.push_back(child);
 
132
      child->ref();
 
133
    }
 
134
  }
 
135
}
 
136
 
 
137
Eris::UI::SlotFactory::SlotFactory(const Atlas::Objects::Entity::Slot& slot,
 
138
                                   const IDList& parents,
 
139
                                   const Atlas::Message::Element::MapType& attrs,
 
140
                                   BaseGen* gen)
 
141
        : Factory(slot, "slot", parents, attrs, gen), _target(slot.getTarget())
 
142
{
 
143
 
 
144
}
 
145
 
 
146
Eris::UI::Element*
 
147
Eris::UI::SlotFactory::create(IDMap& idmap) const
 
148
{
 
149
  SlotElement* slot = dynamic_cast<SlotElement*>(gen()->create(attrs()));
 
150
  assert(slot);
 
151
  Atlas::Message::Element::ListType::const_iterator T;
 
152
  for(T = _target.begin(); T != _target.end(); ++T) {
 
153
    if(!T->isString())
 
154
      continue;
 
155
    IDMap::iterator I = idmap.find(T->asString());
 
156
    if(I != idmap.end())
 
157
      I->second->PropertiesChanged.connect(SigC::slot(*slot, &SlotElement::action));
 
158
  }
 
159
  for(IDList::const_iterator I = idlist().begin(); I != idlist().end(); ++I)
 
160
    idmap[*I] = slot;
 
161
  return slot;
 
162
}
 
163
 
 
164
Eris::UI::Factory*
 
165
Eris::UI::SlotFactory::parse(const Atlas::Message::Element::MapType& map,
 
166
                             const Bindings&) const
 
167
{
 
168
  Atlas::Objects::Entity::Slot slot;
 
169
 
 
170
  // FIXME isn't there a better way to do this?
 
171
  Atlas::Message::Element::MapType::const_iterator I = map.begin();
 
172
  while(I != map.end())
 
173
    slot.setAttr(I->first, I->second);
 
174
 
 
175
  if(slot.getTarget().empty())
 
176
    return 0;
 
177
 
 
178
  return new SlotFactory(slot, idlist(), attrs(), gen());
 
179
}
 
180
 
 
181
Eris::UI::Bindings::Bindings()
 
182
{
 
183
  // FIXME bind slot factories here
 
184
}
 
185
 
 
186
Eris::UI::Bindings::~Bindings()
 
187
{
 
188
  for(FactoryMap::iterator I = _factory_map.begin(); I != _factory_map.end(); ++I)
 
189
    I->second->unref();
 
190
}
 
191
 
 
192
void
 
193
Eris::UI::Bindings::parse(const Atlas::Message::Element& obj)
 
194
{
 
195
  if(!obj.isMap())
 
196
    return;
 
197
 
 
198
  const Atlas::Message::Element::MapType& map = obj.asMap();
 
199
 
 
200
  Atlas::Message::Element::MapType::const_iterator J = map.find("display_status");
 
201
 
 
202
  if(J != map.end() && J->second.isString() && J->second.asString() == "console") {
 
203
    Atlas::Objects::Entity::UIEntity entity;
 
204
 
 
205
    // FIXME isn't there a better way to do this?
 
206
    Atlas::Message::Element::MapType::const_iterator I = map.begin();
 
207
    while(I != map.end())
 
208
      entity.setAttr(I->first, I->second);
 
209
 
 
210
    createConsoleElement(entity);
 
211
    return;
 
212
  }
 
213
 
 
214
  Atlas::Message::Element::MapType::const_iterator parents = map.find("parents");
 
215
 
 
216
  if(parents == map.end() || !parents->second.isList())
 
217
    return;
 
218
 
 
219
  J = map.find("objtype");
 
220
 
 
221
  if(J == map.end() || !J->second.isString())
 
222
    return;
 
223
 
 
224
  const std::string& objtype = J->second.asString();
 
225
 
 
226
  if(objtype != "class" && objtype != "object")
 
227
    return;
 
228
 
 
229
  Factory* factory = 0;
 
230
 
 
231
  Atlas::Message::Element::ListType::const_iterator I;
 
232
  for(I = parents->second.asList().begin(); I != parents->second.asList().end(); ++I) {
 
233
    Factory* parent = findFactory(*I);
 
234
    if(parent) {
 
235
      factory = parent->parse(obj.asMap(), *this);
 
236
      break;
 
237
    }
 
238
  }
 
239
 
 
240
  if(!factory)
 
241
    return;
 
242
 
 
243
  if(objtype == "class") {
 
244
    if(!bind(factory))
 
245
      factory->unref();
 
246
  }
 
247
  else { // objtype == "object"
 
248
    Factory::IDMap idmap;
 
249
    display(factory->create(idmap), factory->id());
 
250
    factory->unref();
 
251
  }
 
252
}
 
253
 
 
254
void
 
255
Eris::UI::Bindings::clear()
 
256
{
 
257
  FactoryMap keepers;
 
258
 
 
259
  for(FactoryMap::iterator I = _factory_map.begin(); I != _factory_map.end(); ++I) {
 
260
    if(I->second->persistent())
 
261
      keepers.insert(*I);
 
262
    else
 
263
      I->second->unref();
 
264
  }
 
265
 
 
266
  _factory_map = keepers;
 
267
}