xref: /haiku/src/libs/alm/Tab.cpp (revision c761a8a6d40be981d18141bfc6eae56b168bcf52)
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 
15 
16 struct XTab::BALMLayoutList {
17 	BALMLayoutList(BALMLayout* _layout, BALMLayoutList* _next = NULL)
18 		:
19 		next(_next),
20 		layout(_layout)
21 	{
22 	}
23 
24 	~BALMLayoutList()
25 	{
26 		delete next;
27 	}
28 
29 	bool HasLayout(BALMLayout* search)
30 	{
31 		if (layout == search)
32 			return true;
33 		return next ? next->HasLayout(search) : false;
34 	}
35 
36 	BALMLayoutList* Remove(BALMLayout* remove)
37 	{
38 		if (layout == remove) {
39 			BALMLayoutList* _next = next;
40 			delete this;
41 			return _next;
42 		}
43 		if (next)
44 			next = next->Remove(remove);
45 		return this;
46 	}
47 
48 	BALMLayoutList*		next;
49 	BALMLayout*			layout;
50 };
51 
52 
53 XTab::XTab(BALMLayout* layout)
54 	:
55 	Variable(layout->Solver()),
56 	fLayouts(new BALMLayoutList(layout))
57 {
58 }
59 
60 
61 XTab::~XTab()
62 {
63 	BALMLayoutList* layouts = fLayouts;
64 	while (layouts) {
65 		layouts->layout->fXTabList.RemoveItem(this);
66 		layouts = layouts->next;
67 	}
68 	delete fLayouts;
69 }
70 
71 
72 bool
73 XTab::IsInLayout(BALMLayout* layout)
74 {
75 	return fLayouts->HasLayout(layout);
76 }
77 
78 
79 bool
80 XTab::AddedToLayout(BALMLayout* layout)
81 {
82 	BALMLayoutList* newHead = new (nothrow) BALMLayoutList(layout, fLayouts);
83 	if (newHead == NULL)
84 		return false;
85 	fLayouts = newHead;
86 	return true;
87 }
88 
89 
90 void
91 XTab::LayoutLeaving(BALMLayout* layout)
92 {
93 	fLayouts = fLayouts->Remove(layout);
94 }
95 
96 
97 bool
98 XTab::IsSuitableFor(BALMLayout* layout)
99 {
100 	return (fLayouts->layout->Solver() == layout->Solver());
101 }
102 
103 
104 YTab::YTab(BALMLayout* layout)
105 	:
106 	Variable(layout->Solver()),
107 	fLayouts(new XTab::BALMLayoutList(layout))
108 {
109 }
110 
111 
112 YTab::~YTab()
113 {
114 	XTab::BALMLayoutList* layouts = fLayouts;
115 	while (layouts) {
116 		layouts->layout->fYTabList.RemoveItem(this);
117 		layouts = layouts->next;
118 	}
119 	delete fLayouts;
120 }
121 
122 
123 bool
124 YTab::IsInLayout(BALMLayout* layout)
125 {
126 	return fLayouts->HasLayout(layout);
127 }
128 
129 
130 bool
131 YTab::AddedToLayout(BALMLayout* layout)
132 {
133 	XTab::BALMLayoutList* newHead
134 		= new (nothrow) XTab::BALMLayoutList(layout, fLayouts);
135 	if (newHead == NULL)
136 		return false;
137 	fLayouts = newHead;
138 	return true;
139 }
140 
141 
142 void
143 YTab::LayoutLeaving(BALMLayout* layout)
144 {
145 	fLayouts = fLayouts->Remove(layout);
146 }
147 
148 
149 bool
150 YTab::IsSuitableFor(BALMLayout* layout)
151 {
152 	return (fLayouts->layout->Solver() == layout->Solver());
153 }
154