Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / layout / FUi_LayoutProxyList.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FUi_LayoutProxyList.cpp
19  * @brief       This is the implementation file for ProxyList class.
20  *
21  * This file contains the implementation of ProxyList class.
22  */
23
24 #include <new>
25 #include <FBaseSysLog.h>
26 #include "FUi_LayoutRelativeLayout.h"
27 #include "FUi_LayoutProxyList.h"
28 #include "FUi_LayoutLayoutItemProxy.h"
29 #include "FUi_LayoutLayoutItemInfo.h"
30
31 namespace Tizen { namespace Ui { namespace _Layout
32 {
33
34 ProxyList::ProxyList(void)
35         : __lastIndex(-1)
36 {
37 }
38
39 ProxyList::~ProxyList(void)
40 {
41 }
42
43 ProxyListNode*
44 ProxyList::GetFirstNode(void) const
45 {
46         return dynamic_cast <ProxyListNode*>(LinkedList::GetFirstNode());
47 }
48
49 ProxyListNode*
50 ProxyList::GetLastNode(void) const
51 {
52         return dynamic_cast <ProxyListNode*>(LinkedList::GetLastNode());
53 }
54
55 ProxyListNode*
56 ProxyList::GetNextNode(const LinkedListNode& node) const
57 {
58         return dynamic_cast <ProxyListNode*>(LinkedList::GetNextNode(node));
59 }
60
61 ProxyListNode*
62 ProxyList::GetPrevNode(const LinkedListNode& node) const
63 {
64         return dynamic_cast <ProxyListNode*>(LinkedList::GetPrevNode(node));
65 }
66
67 ProxyListNode*
68 ProxyList::GetNode(int index) const
69 {
70         ProxyListNode* pNode = SearchNode(index);
71
72         return pNode;
73 }
74
75 ProxyListNode*
76 ProxyList::GetNode(const LayoutItem& item) const
77 {
78         LinkedListNode* pCurNode = GetFirstNode();
79         LinkedListNode* pNextNode = pCurNode;
80
81         while (pCurNode)
82         {
83                 pNextNode = GetNextNode(*pCurNode);
84                 ProxyListNode* pProxyNode = dynamic_cast <ProxyListNode*>(pCurNode);
85                 if (pProxyNode == null)
86                 {
87                         return null;
88                 }
89                 LayoutItemProxy* pItemProxy = pProxyNode->GetItemProxy();
90                 if (pItemProxy == null)
91                 {
92                         return null;
93                 }
94
95                 if (pItemProxy->GetItem() == &item)
96                 {
97                         return pProxyNode;
98                 }
99
100                 pCurNode = pNextNode;
101         }
102         return null;
103 }
104
105 int
106 ProxyList::GetIndex(ProxyListNode& node) const
107 {
108         LayoutItemProxy* pItemProxy = node.GetItemProxy();
109         if (pItemProxy == null)
110         {
111                 SysAssert(false);
112                 return -1;
113         }
114         return pItemProxy->GetIndex();
115 }
116
117 LayoutItemProxy*
118 ProxyList::GetItemProxy(const LayoutItem& item) const
119 {
120         LinkedListNode* pCurNode = GetFirstNode();
121         LinkedListNode* pNextNode = pCurNode;
122
123         while (pCurNode)
124         {
125                 pNextNode = GetNextNode(*pCurNode);
126                 ProxyListNode* pProxyNode = dynamic_cast <ProxyListNode*>(pCurNode);
127                 if (pProxyNode == null)
128                 {
129                         return null;
130                 }
131                 LayoutItemProxy* pItemProxy = pProxyNode->GetItemProxy();
132                 if (pItemProxy == null)
133                 {
134                         return null;
135                 }
136
137                 if (pItemProxy->GetItem() == &item)
138                 {
139                         return pItemProxy;
140                 }
141
142                 pCurNode = pNextNode;
143         }
144         return null;
145 }
146
147 ProxyListNode*
148 ProxyList::SearchNode(int index) const
149 {
150         ProxyListNode* pCurNode = GetFirstNode();
151         ProxyListNode* pNextNode = pCurNode;
152
153         while (pCurNode)
154         {
155                 pNextNode = GetNextNode(*pCurNode);
156                 LayoutItemProxy* pItemProxy = pCurNode->GetItemProxy();
157                 if (pItemProxy == null)
158                 {
159                         return null;
160                 }
161
162                 if (pItemProxy->GetIndex() == index)
163                 {
164                         return pCurNode;
165                 }
166
167                 pCurNode = pNextNode;
168         }
169         return null;
170 }
171
172 ProxyListNode*
173 ProxyList::SearchNode(const LayoutItemProxy& itemProxy) const
174 {
175         ProxyListNode* pCurNode = GetFirstNode();
176         ProxyListNode* pNextNode = pCurNode;
177
178         while (pCurNode)
179         {
180                 pNextNode = GetNextNode(*pCurNode);
181                 LayoutItemProxy* pCurItemProxy = pCurNode->GetItemProxy();
182                 if (pCurItemProxy == null)
183                 {
184                         return null;
185                 }
186
187                 if (pCurItemProxy == &itemProxy)
188                 {
189                         return pCurNode;
190                 }
191
192                 pCurNode = pNextNode;
193         }
194         return null;
195 }
196
197 void
198 ProxyList::RefreshIndex(void)
199 {
200         ProxyListNode* pCurNode = GetFirstNode();
201         ProxyListNode* pNextNode = pCurNode;
202
203         int bakLastIndex = __lastIndex;
204         __lastIndex = -1;
205
206         while (pCurNode)
207         {
208                 pNextNode = GetNextNode(*pCurNode);
209                 LayoutItemProxy* pCurItemProxy = pCurNode->GetItemProxy();
210
211                 pCurItemProxy->SetIndex(++__lastIndex);
212
213                 pCurNode = pNextNode;
214         }
215
216         SysAssert(bakLastIndex == __lastIndex);
217 }
218
219 ProxyListNode*
220 ProxyList::AddNode(LayoutItemProxy& addProxy)
221 {
222         if (SearchNode(addProxy) != null)
223         {
224                 SysLog(NID_UI, "SearchNode() is failed.");
225                 return null;
226         }
227         ProxyListNode* pAddNode = new (std::nothrow) ProxyListNode();
228         SysTryReturn(NID_UI, pAddNode != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to add to ProxyList.");
229
230         pAddNode->Create(addProxy);
231
232         if (OnAddNode(*pAddNode) != E_SUCCESS)
233         {
234                 SysLog(NID_UI, "OnAddNode() is failed.");
235                 delete pAddNode;
236                 return null;
237         }
238         if (InsertIntoLeft(*_pEndNode, *pAddNode) != E_SUCCESS)
239         {
240                 SysLog(NID_UI, "InsertIntoLeft() is failed.");
241                 delete pAddNode;
242                 return null;
243         }
244
245         pAddNode->GetItemProxy()->SetIndex(++__lastIndex);
246
247         return pAddNode;
248 }
249
250 result
251 ProxyList::RemoveNode(LinkedListNode& removeNode)
252 {
253         ProxyListNode* pProxyNode = dynamic_cast <ProxyListNode*>(&removeNode);
254         result re = E_SYSTEM;
255         re = OnRemoveNode(*pProxyNode);
256         if (re != E_SUCCESS)
257         {
258                 SysLog(NID_UI, "OnRemoveNode() is failed.");
259                 SysAssert(false);
260                 return re;
261         }
262
263         re = LinkedList::RemoveNode(removeNode);
264         if (re != E_SUCCESS)
265         {
266                 SysLog(NID_UI, "RemoveNode() is failed.");
267         }
268
269         __lastIndex--;
270
271         return re;
272 }
273
274 result
275 ProxyList::RemoveNode(int index)
276 {
277         ProxyListNode* pRemoveNode = SearchNode(index);
278
279         result re = E_SYSTEM;
280         re = RemoveNode(*pRemoveNode);
281
282         return re;
283 }
284
285 result
286 ProxyList::RemoveNode(LayoutItemProxy& removeItem)
287 {
288         ProxyListNode* pRemoveNode = SearchNode(removeItem);
289
290         return RemoveNode(*pRemoveNode);
291         //int index = GetIndex(*removeNode);
292
293         //return RemoveNode(index);
294 }
295
296 result
297 ProxyList::OnAddNode(ProxyListNode& addNode)
298 {
299         return E_SUCCESS;
300 }
301
302 result
303 ProxyList::OnRemoveNode(ProxyListNode& removeNode)
304 {
305         return E_SUCCESS;
306 }
307
308 int
309 ProxyList::GetNodeCount(void) const
310 {
311         return __lastIndex + 1;
312 }
313
314 void
315 ProxyList::RemoveAllNode(void)
316 {
317         ProxyListNode* pCurNode = GetFirstNode();
318         ProxyListNode* pNextNode = pCurNode;
319         LayoutItemProxy* pItemProxy = null;
320
321         while (pCurNode != null)
322         {
323                 pNextNode = GetNextNode(*pCurNode);
324                 pItemProxy = pCurNode->GetItemProxy();
325                 SysAssert(pItemProxy != null);
326                 pItemProxy->SetParentContainer(null);
327                 delete pItemProxy;
328
329                 if (RemoveNode(*pCurNode) != E_SUCCESS)
330                 {
331                         SysAssert(false);
332                 }
333                 pCurNode = pNextNode;
334         }
335 }
336
337 //
338 // Absolute layout
339 //
340 AbsoluteProxyList::AbsoluteProxyList(void)
341 {
342 }
343
344 AbsoluteProxyList::~AbsoluteProxyList(void)
345 {
346         RemoveAllNode();
347 }
348
349 result
350 AbsoluteProxyList::OnAddNode(ProxyListNode& addNode)
351 {
352         return E_SUCCESS;
353 }
354
355 result
356 AbsoluteProxyList::OnRemoveNode(ProxyListNode& removeNode)
357 {
358         return E_SUCCESS;
359 }
360
361 //
362 // Relative layout
363 //
364 RelativeProxyList::RelativeProxyList()
365         : __pRelativeLayout(null)
366 {
367 }
368
369 RelativeProxyList::~RelativeProxyList()
370 {
371         RemoveAllNode();
372 }
373
374 result
375 RelativeProxyList::OnAddNode(ProxyListNode& addNode)
376 {
377         RelativeItemInfo* pItemInfo = new (std::nothrow) RelativeItemInfo();
378         if (pItemInfo == null)
379         {
380                 return E_OUT_OF_MEMORY;
381         }
382
383         addNode.SetItemInfo(pItemInfo);
384
385         return E_SUCCESS;
386 }
387
388 result
389 RelativeProxyList::OnRemoveNode(ProxyListNode& removeNode)
390 {
391         RelativeItemInfo* pItemInfo = dynamic_cast <RelativeItemInfo*>(removeNode.GetItemInfo());
392         if (pItemInfo == null)
393         {
394                 return E_INVALID_ARG;
395         }
396         ProxyListNode* pCurNode = GetFirstNode();
397         while (pCurNode != null)
398         {
399                 __pRelativeLayout->DeleteTargetInfo(*pCurNode, removeNode);
400                 pCurNode = GetNextNode(*pCurNode);
401         }
402         delete pItemInfo;
403         removeNode.SetItemInfo(null);
404
405         return E_SUCCESS;
406 }
407
408 int
409 RelativeProxyList::GetReferenceCount(ProxyListNode& node) const
410 {
411         RelativeItemInfo* pItemInfo = dynamic_cast <RelativeItemInfo*>(node.GetItemInfo());
412         if (pItemInfo == null)
413         {
414                 return 0;
415         }
416
417         return pItemInfo->__refCount;
418 }
419
420 void
421 RelativeProxyList::SetRelativeLayout(RelativeLayout* pLayout)
422 {
423         __pRelativeLayout = pLayout;
424 }
425
426 //
427 // Table layout
428 //
429 TableProxyList::TableProxyList()
430 {
431 }
432
433 TableProxyList::~TableProxyList()
434 {
435         RemoveAllNode();
436 }
437
438 result
439 TableProxyList::OnAddNode(ProxyListNode& addNode)
440 {
441         TableItemInfo* pItemInfo = new (std::nothrow) TableItemInfo();
442         if (pItemInfo == null)
443         {
444                 return E_OUT_OF_MEMORY;
445         }
446         addNode.SetItemInfo(pItemInfo);
447
448         return E_SUCCESS;
449 }
450
451 result
452 TableProxyList::OnRemoveNode(ProxyListNode& removeNode)
453 {
454         TableItemInfo* pItemInfo = dynamic_cast <TableItemInfo*>(removeNode.GetItemInfo());
455         if (pItemInfo == null)
456         {
457                 return E_INVALID_STATE;
458         }
459
460         delete pItemInfo;
461         removeNode.SetItemInfo(null);
462
463         return E_SUCCESS;
464 }
465
466 //
467 // Linear layout
468 //
469 LinearProxyList::LinearProxyList(void)
470 {
471 }
472
473 LinearProxyList::~LinearProxyList(void)
474 {
475         RemoveAllNode();
476 }
477
478 result
479 LinearProxyList::OnAddNode(ProxyListNode& addNode)
480 {
481         LinearItemInfo* pItemInfo = new (std::nothrow) LinearItemInfo();
482         if (pItemInfo == null)
483         {
484                 return E_OUT_OF_MEMORY;
485         }
486         addNode.SetItemInfo(pItemInfo);
487
488         return E_SUCCESS;
489 }
490
491 result
492 LinearProxyList::OnRemoveNode(ProxyListNode& removeNode)
493 {
494         LinearItemInfo* pItemInfo = dynamic_cast <LinearItemInfo*>(removeNode.GetItemInfo());
495         if (pItemInfo == null)
496         {
497                 return E_INVALID_STATE;
498         }
499         delete pItemInfo;
500
501         return E_SUCCESS;
502 }
503
504 }}} // Tizen::Ui::_Layout