157014d1fSczeidler/*
257014d1fSczeidler * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
357014d1fSczeidler * Distributed under the terms of the MIT License.
457014d1fSczeidler */
5f0307e76SAlex Wilson
6f0307e76SAlex Wilson
757014d1fSczeidler#include <Tab.h>
857014d1fSczeidler
9a25ffa4fSAlex Wilson
1057014d1fSczeidler#include <ALMLayout.h>
1157014d1fSczeidler
1257014d1fSczeidler
13c761a8a6SAlex Wilsonusing std::nothrow;
14708a298eSAlex Wilsonusing BALM::TabBase;
15c761a8a6SAlex Wilson
16c761a8a6SAlex Wilson
17708a298eSAlex Wilsonstruct TabBase::BALMLayoutList {
18c761a8a6SAlex Wilson	BALMLayoutList(BALMLayout* _layout, BALMLayoutList* _next = NULL)
19c761a8a6SAlex Wilson		:
20c761a8a6SAlex Wilson		next(_next),
21c761a8a6SAlex Wilson		layout(_layout)
22c761a8a6SAlex Wilson	{
23c761a8a6SAlex Wilson	}
24c761a8a6SAlex Wilson
25c761a8a6SAlex Wilson	~BALMLayoutList()
26c761a8a6SAlex Wilson	{
27c761a8a6SAlex Wilson		delete next;
28c761a8a6SAlex Wilson	}
29c761a8a6SAlex Wilson
30c761a8a6SAlex Wilson	bool HasLayout(BALMLayout* search)
31c761a8a6SAlex Wilson	{
32c761a8a6SAlex Wilson		if (layout == search)
33c761a8a6SAlex Wilson			return true;
34c761a8a6SAlex Wilson		return next ? next->HasLayout(search) : false;
35c761a8a6SAlex Wilson	}
36c761a8a6SAlex Wilson
37c761a8a6SAlex Wilson	BALMLayoutList* Remove(BALMLayout* remove)
38c761a8a6SAlex Wilson	{
39c761a8a6SAlex Wilson		if (layout == remove) {
40c761a8a6SAlex Wilson			BALMLayoutList* _next = next;
41c761a8a6SAlex Wilson			delete this;
42c761a8a6SAlex Wilson			return _next;
43c761a8a6SAlex Wilson		}
44c761a8a6SAlex Wilson		if (next)
45c761a8a6SAlex Wilson			next = next->Remove(remove);
46c761a8a6SAlex Wilson		return this;
47c761a8a6SAlex Wilson	}
48c761a8a6SAlex Wilson
49c761a8a6SAlex Wilson	BALMLayoutList*		next;
50c761a8a6SAlex Wilson	BALMLayout*			layout;
51c761a8a6SAlex Wilson};
52c761a8a6SAlex Wilson
53c761a8a6SAlex Wilson
54708a298eSAlex WilsonTabBase::TabBase()
5557014d1fSczeidler	:
56708a298eSAlex Wilson	fLayouts(NULL)
5757014d1fSczeidler{
5857014d1fSczeidler}
5957014d1fSczeidler
6057014d1fSczeidler
6135bf0b5cSAlex WilsonTabBase::TabBase(BMessage* archive)
6235bf0b5cSAlex Wilson	:
63c8b24e3eSAlex Wilson	BArchivable(BUnarchiver::PrepareArchive(archive)),
64c8b24e3eSAlex Wilson	fLayouts(NULL)
6535bf0b5cSAlex Wilson{
6635bf0b5cSAlex Wilson	BUnarchiver(archive).Finish(B_OK);
6735bf0b5cSAlex Wilson}
6835bf0b5cSAlex Wilson
6935bf0b5cSAlex Wilson
70708a298eSAlex WilsonTabBase::~TabBase()
7157014d1fSczeidler{
72c761a8a6SAlex Wilson}
73c761a8a6SAlex Wilson
74c761a8a6SAlex Wilson
75c761a8a6SAlex Wilsonbool
76708a298eSAlex WilsonTabBase::IsInLayout(BALMLayout* layout)
77c761a8a6SAlex Wilson{
78f9c58743SAlex Wilson	return fLayouts ? fLayouts->HasLayout(layout) : false;
79c761a8a6SAlex Wilson}
80c761a8a6SAlex Wilson
81c761a8a6SAlex Wilson
82c761a8a6SAlex Wilsonbool
83708a298eSAlex WilsonTabBase::AddedToLayout(BALMLayout* layout)
84c761a8a6SAlex Wilson{
85c761a8a6SAlex Wilson	BALMLayoutList* newHead = new (nothrow) BALMLayoutList(layout, fLayouts);
86c761a8a6SAlex Wilson	if (newHead == NULL)
87c761a8a6SAlex Wilson		return false;
88c761a8a6SAlex Wilson	fLayouts = newHead;
89c761a8a6SAlex Wilson	return true;
90c761a8a6SAlex Wilson}
91c761a8a6SAlex Wilson
92c761a8a6SAlex Wilson
93c761a8a6SAlex Wilsonvoid
94708a298eSAlex WilsonTabBase::LayoutLeaving(BALMLayout* layout)
95c761a8a6SAlex Wilson{
96c761a8a6SAlex Wilson	fLayouts = fLayouts->Remove(layout);
97c761a8a6SAlex Wilson}
98c761a8a6SAlex Wilson
99c761a8a6SAlex Wilson
100c761a8a6SAlex Wilsonbool
101708a298eSAlex WilsonTabBase::IsSuitableFor(BALMLayout* layout)
102c761a8a6SAlex Wilson{
103c761a8a6SAlex Wilson	return (fLayouts->layout->Solver() == layout->Solver());
10457014d1fSczeidler}
10557014d1fSczeidler
10657014d1fSczeidler
107708a298eSAlex WilsonXTab::XTab(BALMLayout* layout)
10857014d1fSczeidler	:
109708a298eSAlex Wilson	Variable(layout->Solver())
11057014d1fSczeidler{
111708a298eSAlex Wilson	AddedToLayout(layout);
11257014d1fSczeidler}
11357014d1fSczeidler
11457014d1fSczeidler
11535bf0b5cSAlex WilsonXTab::XTab(BMessage* archive)
11635bf0b5cSAlex Wilson	:
11735bf0b5cSAlex Wilson	Variable(NULL),
11835bf0b5cSAlex Wilson	TabBase(BUnarchiver::PrepareArchive(archive))
11935bf0b5cSAlex Wilson{
12035bf0b5cSAlex Wilson	BUnarchiver(archive).Finish(B_OK);
12135bf0b5cSAlex Wilson}
12235bf0b5cSAlex Wilson
12335bf0b5cSAlex Wilson
124708a298eSAlex WilsonXTab::~XTab()
12557014d1fSczeidler{
126708a298eSAlex Wilson	TabBase::BALMLayoutList* layouts = fLayouts;
127c761a8a6SAlex Wilson	while (layouts) {
128708a298eSAlex Wilson		layouts->layout->fXTabList.RemoveItem(this);
129c761a8a6SAlex Wilson		layouts = layouts->next;
130c761a8a6SAlex Wilson	}
131c761a8a6SAlex Wilson	delete fLayouts;
132c761a8a6SAlex Wilson}
133c761a8a6SAlex Wilson
134c761a8a6SAlex Wilson
13535bf0b5cSAlex WilsonBArchivable*
13635bf0b5cSAlex WilsonXTab::Instantiate(BMessage* archive)
13735bf0b5cSAlex Wilson{
13835bf0b5cSAlex Wilson	if (validate_instantiation(archive, "BALM::XTab"))
13935bf0b5cSAlex Wilson		return new XTab(archive);
14035bf0b5cSAlex Wilson	return NULL;
14135bf0b5cSAlex Wilson}
14235bf0b5cSAlex Wilson
14335bf0b5cSAlex Wilson
144708a298eSAlex WilsonYTab::YTab(BALMLayout* layout)
145708a298eSAlex Wilson	:
146708a298eSAlex Wilson	Variable(layout->Solver())
147c761a8a6SAlex Wilson{
148708a298eSAlex Wilson	AddedToLayout(layout);
149c761a8a6SAlex Wilson}
150c761a8a6SAlex Wilson
151c761a8a6SAlex Wilson
15235bf0b5cSAlex WilsonYTab::YTab(BMessage* archive)
15335bf0b5cSAlex Wilson	:
15435bf0b5cSAlex Wilson	Variable(NULL),
15535bf0b5cSAlex Wilson	TabBase(BUnarchiver::PrepareArchive(archive))
15635bf0b5cSAlex Wilson{
15735bf0b5cSAlex Wilson	BUnarchiver(archive).Finish(B_OK);
15835bf0b5cSAlex Wilson}
15935bf0b5cSAlex Wilson
16035bf0b5cSAlex Wilson
161708a298eSAlex WilsonYTab::~YTab()
162c761a8a6SAlex Wilson{
163708a298eSAlex Wilson	TabBase::BALMLayoutList* layouts = fLayouts;
164708a298eSAlex Wilson	while (layouts) {
165708a298eSAlex Wilson		layouts->layout->fYTabList.RemoveItem(this);
166708a298eSAlex Wilson		layouts = layouts->next;
167708a298eSAlex Wilson	}
168708a298eSAlex Wilson	delete fLayouts;
16957014d1fSczeidler}
17035bf0b5cSAlex Wilson
17135bf0b5cSAlex Wilson
17235bf0b5cSAlex WilsonBArchivable*
17335bf0b5cSAlex WilsonYTab::Instantiate(BMessage* archive)
17435bf0b5cSAlex Wilson{
17535bf0b5cSAlex Wilson	if (validate_instantiation(archive, "BALM::YTab"))
17635bf0b5cSAlex Wilson		return new YTab(archive);
17735bf0b5cSAlex Wilson	return NULL;
17835bf0b5cSAlex Wilson}
179