Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUi_GridLayoutImpl.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_GridLayoutImpl.cpp
19  * @brief       This is the implementation file for _GridLayoutImpl class.
20  *
21  * This file contains the implementation of _GridLayoutImpl class.
22  */
23
24 #include "FUi_GridLayoutImpl.h"
25
26 namespace Tizen { namespace Ui
27 {
28
29 // _GridLayoutImpl implementation
30 _GridLayoutImpl::_GridLayoutImpl(GridLayout* pPublicLayout, _Layout::TableLayout* pCoreLayout)
31         : _LayoutImpl(pPublicLayout, pCoreLayout)
32         , __maxRow(0)
33         , __maxColumn(0)
34 {
35 }
36
37 _GridLayoutImpl::~_GridLayoutImpl()
38 {
39 }
40
41 Tizen::Ui::LayoutType
42 _GridLayoutImpl::GetLayoutType(void) const
43 {
44         return LAYOUT_GRID;
45 }
46
47 _GridLayoutImpl*
48 _GridLayoutImpl::CreateGridLayoutImplN(GridLayout* pPublicLayout, int maxRow, int maxColumn)
49 {
50         ClearLastResult();
51
52         _Layout::TableLayout* pCoreLayout = null;
53         _GridLayoutImpl* pImplLayout = null;
54         result r = E_SUCCESS;
55
56         pCoreLayout = _Layout::TableLayout::CreateTableLayoutN();
57         r = GetLastResult();
58         SysTryReturn(NID_UI, pCoreLayout != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
59
60         pImplLayout = new (std::nothrow) _GridLayoutImpl(pPublicLayout, pCoreLayout);
61         r = CheckConstruction(pCoreLayout, pImplLayout);
62         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
63
64         r = pImplLayout->Construct(maxRow, maxColumn);
65         if (r != E_SUCCESS)
66         {
67                 delete pImplLayout;
68                 SysTryReturn(NID_UI, false, null, r, "[%s] Propagating.", GetErrorMessage(r));
69         }
70
71         return pImplLayout;
72 }
73
74 const char*
75 _GridLayoutImpl::GetPublicClassName(void) const
76 {
77         return "Tizen::Ui::GridLayout";
78 }
79
80 const GridLayout&
81 _GridLayoutImpl::GetPublic(void) const
82 {
83         return static_cast <const GridLayout&>(_LayoutImpl::GetPublic());
84 }
85
86 GridLayout&
87 _GridLayoutImpl::GetPublic(void)
88 {
89         return static_cast <GridLayout&>(_LayoutImpl::GetPublic());
90 }
91
92 const _Layout::TableLayout&
93 _GridLayoutImpl::GetCore(void) const
94 {
95         return static_cast <const _Layout::TableLayout&>(_LayoutImpl::GetCore());
96 }
97
98 _Layout::TableLayout&
99 _GridLayoutImpl::GetCore(void)
100 {
101         return static_cast <_Layout::TableLayout&>(_LayoutImpl::GetCore());
102 }
103
104 const _GridLayoutImpl*
105 _GridLayoutImpl::GetInstance(const GridLayout& layout)
106 {
107         return static_cast<const _GridLayoutImpl*>(_LayoutImpl::GetInstance(layout));
108 }
109
110 _GridLayoutImpl*
111 _GridLayoutImpl::GetInstance(GridLayout& layout)
112 {
113         return static_cast<_GridLayoutImpl*>(_LayoutImpl::GetInstance(layout));
114 }
115
116 result
117 _GridLayoutImpl::Construct(int maxRow, int maxColumn)
118 {
119         ClearLastResult();
120
121         result r = GetCore().CreateTable(maxRow, maxColumn);
122         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
123
124         __maxRow = maxRow;
125         __maxColumn = maxColumn;
126
127         return E_SUCCESS;
128 }
129
130 result
131 _GridLayoutImpl::SetColumnStretchable(int columnIndex, bool stretchable)
132 {
133         ClearLastResult();
134
135         return GetCore().SetColumnStretchable(columnIndex, stretchable);
136 }
137
138 result
139 _GridLayoutImpl::SetColumnShrinkable(int columnIndex, bool shrinkable)
140 {
141         ClearLastResult();
142
143         return GetCore().SetColumnShrinkable(columnIndex, shrinkable);
144 }
145
146 result
147 _GridLayoutImpl::SetColumnCollapsed(int columnIndex, bool collapsed)
148 {
149         ClearLastResult();
150
151         return GetCore().SetColumnCollapsed(columnIndex, collapsed);
152 }
153
154 result
155 _GridLayoutImpl::SetAllColumnsStretchable(bool stretchable)
156 {
157         ClearLastResult();
158
159         result r = E_SUCCESS;
160         bool* pOldStretchable = new (std::nothrow) bool[__maxColumn];
161         SysTryReturn(NID_UI, pOldStretchable != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
162         memset(pOldStretchable, 0, sizeof(bool) * __maxColumn);
163
164         int col = 0;
165         for (col = 0; col < __maxColumn; col++)
166         {
167                 pOldStretchable[col] = GetCore().GetColumnStretchable(col);
168                 r = GetLastResult();
169                 if (r != E_SUCCESS)
170                 {
171                         SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] max column is invalid value.");
172                         goto CATCH;
173                 }
174
175                 r = GetCore().SetColumnStretchable(col, stretchable);
176                 if (r != E_SUCCESS)
177                 {
178                         SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] max column is invalid value.");
179                         goto CATCH;
180                 }
181         }
182
183         delete[] pOldStretchable;
184
185         return r;
186
187 CATCH:
188         for (int colIndex = 0; colIndex < col; colIndex++)
189         {
190                 GetCore().SetColumnStretchable(colIndex, pOldStretchable[colIndex]);
191         }
192
193         delete[] pOldStretchable;
194
195         return r;
196 }
197
198 result
199 _GridLayoutImpl::SetAllColumnsShrinkable(bool shrinkable)
200 {
201         ClearLastResult();
202
203         result r = E_SUCCESS;
204         bool* pOldShrinkable = new (std::nothrow) bool[__maxColumn];
205         SysTryReturn(NID_UI, pOldShrinkable != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
206         memset(pOldShrinkable, 0, sizeof(bool) * __maxColumn);
207
208         int col = 0;
209         for (col = 0; col < __maxColumn; col++)
210         {
211                 pOldShrinkable[col] = GetCore().GetColumnShrinkable(col);
212                 r = GetLastResult();
213                 if (r != E_SUCCESS)
214                 {
215                         SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] max column is invalid value.");
216                         goto CATCH;
217                 }
218
219                 r = GetCore().SetColumnShrinkable(col, shrinkable);
220                 if (r != E_SUCCESS)
221                 {
222                         SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] max column is invalid value.");
223                         goto CATCH;
224                 }
225         }
226
227         delete[] pOldShrinkable;
228
229         return r;
230
231 CATCH:
232         for (int colIndex = 0; colIndex < col; colIndex++)
233         {
234                 GetCore().SetColumnShrinkable(colIndex, pOldShrinkable[colIndex]);
235         }
236
237         delete[] pOldShrinkable;
238
239         return r;
240 }
241
242 result
243 _GridLayoutImpl::SetColumnSpacing(int columnIndex, int space)
244 {
245         ClearLastResult();
246
247         return GetCore().SetColumnSpacing(columnIndex, space);
248 }
249
250 result
251 _GridLayoutImpl::SetRowStretchable(int rowIndex, bool stretchable)
252 {
253         ClearLastResult();
254
255         return GetCore().SetRowStretchable(rowIndex, stretchable);
256 }
257
258 result
259 _GridLayoutImpl::SetRowShrinkable(int rowIndex, bool shrinkable)
260 {
261         ClearLastResult();
262
263         return GetCore().SetRowShrinkable(rowIndex, shrinkable);
264 }
265
266 result
267 _GridLayoutImpl::SetRowCollapsed(int rowIndex, bool collapsed)
268 {
269         ClearLastResult();
270
271         return GetCore().SetRowCollapsed(rowIndex, collapsed);
272 }
273
274 result
275 _GridLayoutImpl::SetAllRowsStretchable(bool stretchable)
276 {
277         ClearLastResult();
278
279         result r = E_SUCCESS;
280         bool* pOldStretchable = new (std::nothrow) bool[__maxRow];
281         SysTryReturn(NID_UI, pOldStretchable != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
282         memset(pOldStretchable, 0, sizeof(bool) * __maxRow);
283
284
285         int row = 0;
286         for (row = 0; row < __maxRow; row++)
287         {
288                 pOldStretchable[row] = GetCore().GetRowStretchable(row);
289                 r = GetLastResult();
290                 if (r != E_SUCCESS)
291                 {
292                         SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] max row is invalid value.");
293                         goto CATCH;
294                 }
295
296                 r = GetCore().SetRowStretchable(row, stretchable);
297                 if (r != E_SUCCESS)
298                 {
299                         SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] max row is invalid value.");
300                         goto CATCH;
301                 }
302         }
303
304         delete[] pOldStretchable;
305
306         return r;
307
308 CATCH:
309         for (int rowIndex = 0; rowIndex < row; rowIndex++)
310         {
311                 GetCore().SetRowStretchable(rowIndex, pOldStretchable[rowIndex]);
312         }
313
314         delete[] pOldStretchable;
315
316         return r;
317 }
318
319 result
320 _GridLayoutImpl::SetAllRowsShrinkable(bool shrinkable)
321 {
322         ClearLastResult();
323
324         result r = E_SUCCESS;
325         bool* pOldShrinkable = new (std::nothrow) bool[__maxRow];
326         SysTryReturn(NID_UI, pOldShrinkable != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
327         memset(pOldShrinkable, 0, sizeof(bool) * __maxRow);
328
329         int row = 0;
330         for (row = 0; row < __maxRow; row++)
331         {
332                 pOldShrinkable[row] = GetCore().GetRowShrinkable(row);
333                 r = GetLastResult();
334                 if (r != E_SUCCESS)
335                 {
336                         SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] max row is invalid value.");
337                         goto CATCH;
338                 }
339
340                 r = GetCore().SetRowShrinkable(row, shrinkable);
341                 if (r != E_SUCCESS)
342                 {
343                         SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] max row is invalid value.");
344                         goto CATCH;
345                 }
346         }
347
348         delete[] pOldShrinkable;
349
350         return r;
351
352 CATCH:
353         for (int rowIndex = 0; rowIndex < row; rowIndex++)
354         {
355                 GetCore().SetRowShrinkable(rowIndex, pOldShrinkable[rowIndex]);
356         }
357         delete[] pOldShrinkable;
358
359         return r;
360 }
361
362 result
363 _GridLayoutImpl::SetRowSpacing(int rowIndex, int space)
364 {
365         ClearLastResult();
366
367         return GetCore().SetRowSpacing(rowIndex, space);
368 }
369
370 result
371 _GridLayoutImpl::SetPosition(_ControlImpl& control, int row, int column, int rowSpan, int columnSpan)
372 {
373         ClearLastResult();
374
375         _Layout::LayoutItem& layoutItem = control.GetLayoutContainer();
376
377         result r = E_SUCCESS;
378         int rowSize = 1;
379         int colSize = 1;
380
381         SysTryReturn(NID_UI, GetCore().ItemExists(layoutItem), E_INVALID_ARG, E_INVALID_ARG,
382                                 "[E_INVALID_ARG] The control is not belong to layout");
383
384         SysTryReturn(NID_UI, row >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
385                                 "[E_OUT_OF_RANGE] Negative input argument : row(%d)", row);
386         SysTryReturn(NID_UI, row < __maxRow, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
387                                 "[E_OUT_OF_RANGE]  Input Argument row over the max row : row(%d), maxRow(%d)", row, __maxRow);
388         SysTryReturn(NID_UI, column >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
389                                 "[E_OUT_OF_RANGE] Negative input argument : column(%d)", column);
390         SysTryReturn(NID_UI, column < __maxColumn, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
391                                 "[E_OUT_OF_RANGE]  Input Argument column over the max column : column(%d), maxColumn(%d)", column, __maxColumn);
392
393         SysTryReturn(NID_UI, rowSpan >= 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The row span is negative value.");
394         SysTryReturn(NID_UI, columnSpan >= 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The column span is negative value.");
395
396         SysTryReturn(NID_UI, (row + rowSpan - 1) < __maxRow, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Out of range.");
397         SysTryReturn(NID_UI, (column + columnSpan - 1) < __maxColumn, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
398                                 "[E_OUT_OF_RANGE] Out of range.");
399
400         r = GetCore().GetMergeSize(row, column, rowSize, colSize);
401         SysTryReturn(NID_UI, (r == E_SUCCESS), r, r,
402                                 "The specified span tries to include a cell which is already included in another span.");
403
404         _Layout::LayoutItem* prevItem = GetCore().GetItem(row, column);
405         if (prevItem)
406         {
407                 if (prevItem != &layoutItem)
408                 {
409                         r = GetCore().SwapItemPosition(layoutItem, *prevItem);
410                 }
411         }
412         else
413         {
414                 r = GetCore().SetItemPosition(layoutItem, row, column);
415         }
416         GetCore().SetFillCell(row, column, false, false);
417
418         if (r != E_SUCCESS)
419         {
420                 return r;
421         }
422
423         if (rowSpan != rowSize || columnSpan != colSize)
424         {
425                 for (int y = 0; y < rowSpan; y++)
426                 {
427                         for (int x = 0; x < columnSpan; x++)
428                         {
429                                 r = GetCore().Unmerge(row + y, column + x);
430                                 if (r == E_SYSTEM)
431                                 {
432                                         return r;
433                                 }
434                         }
435                 }
436
437                 r = GetCore().Merge(row, column, row + rowSpan - 1, column + columnSpan - 1);
438                 GetCore().SetFillCell(row, column, true, true);
439         }
440
441         return r;
442 }
443
444 int
445 _GridLayoutImpl::GetRowCount() const
446 {
447         return __maxRow;
448 }
449
450 int
451 _GridLayoutImpl::GetColumnCount() const
452 {
453         return __maxColumn;
454 }
455
456 }} // Tizen::Ui