blob: 8418fb0e720517dae2cfc0f231827c88993b4c0a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkViewInflate.h"
9#include "SkView.h"
10#include <stdio.h>
11
12SkViewInflate::SkViewInflate() : fIDs(kMinIDStrAlloc), fStrings(kMinIDStrAlloc)
13{
14}
15
16SkViewInflate::~SkViewInflate()
17{
18}
19
20void SkViewInflate::rInflate(const SkDOM& dom, const SkDOM::Node* node, SkView* parent)
21{
rmistry@google.comd6176b02012-08-23 18:14:13 +000022 const char* str = dom.findAttr(node, "id");
23 if (str)
24 fIDs.set(str, parent);
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
rmistry@google.comd6176b02012-08-23 18:14:13 +000026 const SkDOM::Node* child = dom.getFirstChild(node);
27 while (child)
28 {
29 SkView* view = this->createView(dom, child);
30 if (view)
31 {
32 this->rInflate(dom, child, view);
33 parent->attachChildToFront(view)->unref();
34 }
35 else
36 {
37 const char* name = dom.getName(child);
38 const char* target;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
halcanary96fcdcc2015-08-27 07:41:13 -070040 if (!strcmp(name, "listenTo") && (target = dom.findAttr(child, "target")) != nullptr)
rmistry@google.comd6176b02012-08-23 18:14:13 +000041 this->addIDStr(&fListenTo, parent, target);
reed@android.com8a1c16f2008-12-17 15:59:43 +000042
halcanary96fcdcc2015-08-27 07:41:13 -070043 if (!strcmp(name, "broadcastTo") && (target = dom.findAttr(child, "target")) != nullptr)
rmistry@google.comd6176b02012-08-23 18:14:13 +000044 this->addIDStr(&fBroadcastTo, parent, target);
45 }
46 child = dom.getNextSibling(child);
47 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000048
rmistry@google.comd6176b02012-08-23 18:14:13 +000049 parent->setVisibleP(true);
50 this->inflateView(parent, dom, node);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051}
52
53void SkViewInflate::inflateView(SkView* view, const SkDOM& dom, const SkDOM::Node* node)
54{
rmistry@google.comd6176b02012-08-23 18:14:13 +000055 // called after all of view's children have been instantiated.
56 // this may be overridden by a subclass, to load in layout or other helpers
57 // they should call through to us (INHERITED) before or after their patch
58 view->inflate(dom, node);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059}
60
61SkView* SkViewInflate::inflate(const SkDOM& dom, const SkDOM::Node* node, SkView* root)
62{
rmistry@google.comd6176b02012-08-23 18:14:13 +000063 fIDs.reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +000064
halcanary96fcdcc2015-08-27 07:41:13 -070065 if (root == nullptr)
rmistry@google.comd6176b02012-08-23 18:14:13 +000066 {
67 root = this->createView(dom, node);
halcanary96fcdcc2015-08-27 07:41:13 -070068 if (root == nullptr)
rmistry@google.comd6176b02012-08-23 18:14:13 +000069 {
halcanary96fcdcc2015-08-27 07:41:13 -070070 printf("createView returned nullptr on <%s>\n", dom.getName(node));
71 return nullptr;
rmistry@google.comd6176b02012-08-23 18:14:13 +000072 }
73 }
74 this->rInflate(dom, node, root);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
rmistry@google.comd6176b02012-08-23 18:14:13 +000076 // resolve listeners and broadcasters
77 {
78 SkView* target;
79 const IDStr* iter = fListenTo.begin();
80 const IDStr* stop = fListenTo.end();
81 for (; iter < stop; iter++)
82 {
83 if (fIDs.find(iter->fStr, &target))
84 target->addListenerID(iter->fView->getSinkID());
85 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 iter = fBroadcastTo.begin();
88 stop = fBroadcastTo.end();
89 for (; iter < stop; iter++)
90 {
91 if (fIDs.find(iter->fStr, &target))
92 iter->fView->addListenerID(target->getSinkID());
93 }
94 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000095
rmistry@google.comd6176b02012-08-23 18:14:13 +000096 // now that the tree is built, give everyone a shot at the ID dict
97 root->postInflate(fIDs);
98 return root;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099}
100
101SkView* SkViewInflate::inflate(const char xml[], size_t len, SkView* root)
102{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103 SkDOM dom;
104 const SkDOM::Node* node = dom.build(xml, len);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
halcanary96fcdcc2015-08-27 07:41:13 -0700106 return node ? this->inflate(dom, node, root) : nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107}
108
109SkView* SkViewInflate::findViewByID(const char id[]) const
110{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111 SkASSERT(id);
112 SkView* view;
halcanary96fcdcc2015-08-27 07:41:13 -0700113 return fIDs.find(id, &view) ? view : nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114}
115
116SkView* SkViewInflate::createView(const SkDOM& dom, const SkDOM::Node* node)
117{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118 if (!strcmp(dom.getName(node), "view"))
119 return new SkView;
halcanary96fcdcc2015-08-27 07:41:13 -0700120 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121}
122
123void SkViewInflate::addIDStr(SkTDArray<IDStr>* list, SkView* view, const char* str)
124{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000125 size_t len = strlen(str) + 1;
126 IDStr* pair = list->append();
127 pair->fView = view;
128 pair->fStr = (char*)fStrings.alloc(len, SkChunkAlloc::kThrow_AllocFailType);
129 memcpy(pair->fStr, str, len);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130}
131
132#ifdef SK_DEBUG
133void SkViewInflate::dump() const
134{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000135 const IDStr* iter = fListenTo.begin();
136 const IDStr* stop = fListenTo.end();
137 for (; iter < stop; iter++)
138 SkDebugf("inflate: listenTo(\"%s\")\n", iter->fStr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139
rmistry@google.comd6176b02012-08-23 18:14:13 +0000140 iter = fBroadcastTo.begin();
141 stop = fBroadcastTo.end();
142 for (; iter < stop; iter++)
143 SkDebugf("inflate: broadcastFrom(\"%s\")\n", iter->fStr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144}
145#endif