xref: /haiku/src/libs/alm/Tab.cpp (revision 3af608ada59d12d73608a045de66d425eedf7003)
1 /*
2  * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <Tab.h>
8 
9 
10 #include <ALMLayout.h>
11 
12 
13 using std::nothrow;
14 using BALM::TabBase;
15 
16 
17 struct TabBase::BALMLayoutList {
18 	BALMLayoutList(BALMLayout* _layout, BALMLayoutList* _next = NULL)
19 		:
20 		next(_next),
21 		layout(_layout)
22 	{
23 	}
24 
25 	~BALMLayoutList()
26 	{
27 		delete next;
28 	}
29 
30 	bool HasLayout(BALMLayout* search)
31 	{
32 		if (layout == search)
33 			return true;
34 		return next ? next->HasLayout(search) : false;
35 	}
36 
37 	BALMLayoutList* Remove(BALMLayout* remove)
38 	{
39 		if (layout == remove) {
40 			BALMLayoutList* _next = next;
41 			delete this;
42 			return _next;
43 		}
44 		if (next)
45 			next = next->Remove(remove);
46 		return this;
47 	}
48 
49 	BALMLayoutList*		next;
50 	BALMLayout*			layout;
51 };
52 
53 
54 TabBase::TabBase()
55 	:
56 	fLayouts(NULL)
57 {
58 }
59 
60 
61 TabBase::TabBase(BMessage* archive)
62 	:
63 	BArchivable(BUnarchiver::PrepareArchive(archive)),
64 	fLayouts(NULL)
65 {
66 	BUnarchiver(archive).Finish(B_OK);
67 }
68 
69 
70 TabBase::~TabBase()
71 {
72 }
73 
74 
75 bool
76 TabBase::IsInLayout(BALMLayout* layout)
77 {
78 	return fLayouts ? fLayouts->HasLayout(layout) : false;
79 }
80 
81 
82 bool
83 TabBase::AddedToLayout(BALMLayout* layout)
84 {
85 	BALMLayoutList* newHead = new (nothrow) BALMLayoutList(layout, fLayouts);
86 	if (newHead == NULL)
87 		return false;
88 	fLayouts = newHead;
89 	return true;
90 }
91 
92 
93 void
94 TabBase::LayoutLeaving(BALMLayout* layout)
95 {
96 	fLayouts = fLayouts->Remove(layout);
97 }
98 
99 
100 bool
101 TabBase::IsSuitableFor(BALMLayout* layout)
102 {
103 	return (fLayouts->layout->Solver() == layout->Solver());
104 }
105 
106 
107 XTab::XTab(BALMLayout* layout)
108 	:
109 	Variable(layout->Solver())
110 {
111 	AddedToLayout(layout);
112 }
113 
114 
115 XTab::XTab(BMessage* archive)
116 	:
117 	Variable(NULL),
118 	TabBase(BUnarchiver::PrepareArchive(archive))
119 {
120 	BUnarchiver(archive).Finish(B_OK);
121 }
122 
123 
124 XTab::~XTab()
125 {
126 	TabBase::BALMLayoutList* layouts = fLayouts;
127 	while (layouts) {
128 		layouts->layout->fXTabList.RemoveItem(this);
129 		layouts = layouts->next;
130 	}
131 	delete fLayouts;
132 }
133 
134 
135 BArchivable*
136 XTab::Instantiate(BMessage* archive)
137 {
138 	if (validate_instantiation(archive, "BALM::XTab"))
139 		return new XTab(archive);
140 	return NULL;
141 }
142 
143 
144 YTab::YTab(BALMLayout* layout)
145 	:
146 	Variable(layout->Solver())
147 {
148 	AddedToLayout(layout);
149 }
150 
151 
152 YTab::YTab(BMessage* archive)
153 	:
154 	Variable(NULL),
155 	TabBase(BUnarchiver::PrepareArchive(archive))
156 {
157 	BUnarchiver(archive).Finish(B_OK);
158 }
159 
160 
161 YTab::~YTab()
162 {
163 	TabBase::BALMLayoutList* layouts = fLayouts;
164 	while (layouts) {
165 		layouts->layout->fYTabList.RemoveItem(this);
166 		layouts = layouts->next;
167 	}
168 	delete fLayouts;
169 }
170 
171 
172 BArchivable*
173 YTab::Instantiate(BMessage* archive)
174 {
175 	if (validate_instantiation(archive, "BALM::YTab"))
176 		return new YTab(archive);
177 	return NULL;
178 }
179