Added nested TableView KeyboardFocus support
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / table-view / table-view-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/table-view/table-view-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <sstream>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/public-api/size-negotiation/relayout-container.h>
28 #include <dali/integration-api/debug.h>
29
30 using namespace Dali;
31
32 namespace
33 {
34 /**
35  * @brief Should the tableview fit around the given actor
36  *
37  * @param[in] actor The child actor to test against
38  * @param[dimension] The dimension to test against
39  */
40 bool FitToChild( Actor actor, Dimension::Type dimension )
41 {
42   return actor.GetResizePolicy( dimension ) != ResizePolicy::FILL_TO_PARENT && actor.GetRelayoutSize( dimension ) > 0.0f;
43 }
44
45 #if defined(DEBUG_ENABLED)
46 // debugging support, very useful when new features are added or bugs are hunted down
47 // currently not called from code so compiler will optimize these away, kept here for future debugging
48
49 #define TABLEVIEW_TAG "DALI Toolkit::TableView "
50 #define TV_LOG(fmt, args...) Debug::LogMessage(Debug::DebugInfo, TABLEVIEW_TAG fmt, ## args)
51 //#define TABLEVIEW_DEBUG 1
52
53 #if defined(TABLEVIEW_DEBUG)
54 void PrintArray( Array2d<Dali::Toolkit::Internal::TableView::CellData>& array )
55 {
56   TV_LOG( "Array2d<CellData> size [%d,%d] \n", array.GetRows(), array.GetColumns() );
57   // print values
58   for( unsigned int i = 0; i < array.GetRows(); ++i )
59   {
60     for( unsigned int j = 0; j < array.GetColumns(); ++j )
61     {
62       Dali::Toolkit::Internal::TableView::CellData data = array[i][j];
63       char actor = ' ';
64       std::string actorName;
65       if( data.actor )
66       {
67         actor = 'A';
68         actorName = data.actor.GetName();
69       }
70       TV_LOG("Array[%d,%d]=%c %s %d,%d,%d,%d  ", i, j, actor, actorName.c_str(),
71           data.position.rowIndex, data.position.columnIndex,
72           data.position.rowSpan, data.position.columnSpan );
73     }
74     TV_LOG( "\n" );
75   }
76 }
77
78 // debugging support, very useful when new features are added or bugs are hunted down
79 // currently not called from code so compiler will optimize these away, kept here for future debugging
80 void PrintArray( Array2d<Size>& array )
81 {
82   TV_LOG( "Array2d<Size> size [%d,%d] \n", array.GetRows(), array.GetColumns() );
83   // print values
84   for( unsigned int i = 0; i < array.GetRows(); ++i )
85   {
86     for( unsigned int j = 0; j < array.GetColumns(); ++j )
87     {
88       TV_LOG( "Array[%d,%d]=%.2f,%.2f ", i, j, array[i][j].width, array[i][j].height );
89     }
90     TV_LOG( "\n" );
91   }
92 }
93 // debugging support, very useful when new features are added or bugs are hunted down
94 // currently not called from code so compiler will optimize these away, kept here for future debugging
95 void PrintVector( std::vector<float>& array )
96 {
97   TV_LOG( "vector, size [%d]\n", array.size() );
98   // print values
99   for( unsigned int i = 0; i < array.size(); ++i )
100   {
101     TV_LOG( "vector[%d]=%.2f ", i, array[i] );
102   }
103   TV_LOG( "\n" );
104 }
105 #endif // defined(TABLEVIEW_DEBUG)
106 #endif // defined(DEBUG_ENABLED)
107
108 } // namespace
109
110 namespace Dali
111 {
112
113 namespace Toolkit
114 {
115
116 namespace Internal
117 {
118
119 namespace
120 {
121
122 // Type registration
123 BaseHandle Create()
124 {
125   return Toolkit::TableView::New( 0, 0 );
126 }
127
128 // Setup properties, signals and actions using the type-registry.
129 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TableView, Toolkit::Control, Create );
130
131 DALI_PROPERTY_REGISTRATION( Toolkit, TableView, "rows",           INTEGER, ROWS           )
132 DALI_PROPERTY_REGISTRATION( Toolkit, TableView, "columns",        INTEGER, COLUMNS        )
133 DALI_PROPERTY_REGISTRATION( Toolkit, TableView, "cellPadding",    VECTOR2, CELL_PADDING   )
134 DALI_PROPERTY_REGISTRATION( Toolkit, TableView, "layoutRows",     MAP,     LAYOUT_ROWS    )
135 DALI_PROPERTY_REGISTRATION( Toolkit, TableView, "layoutColumns",  MAP,     LAYOUT_COLUMNS )
136 DALI_CHILD_PROPERTY_REGISTRATION( Toolkit, TableView, "cellIndex",                VECTOR2, CELL_INDEX                )
137 DALI_CHILD_PROPERTY_REGISTRATION( Toolkit, TableView, "rowSpan",                  FLOAT,   ROW_SPAN                  )
138 DALI_CHILD_PROPERTY_REGISTRATION( Toolkit, TableView, "columnSpan",               FLOAT,   COLUMN_SPAN               )
139 DALI_CHILD_PROPERTY_REGISTRATION( Toolkit, TableView, "cellHorizontalAlignment",  STRING,  CELL_HORIZONTAL_ALIGNMENT )
140 DALI_CHILD_PROPERTY_REGISTRATION( Toolkit, TableView, "cellVerticalAlignment",    STRING,  CELL_VERTICAL_ALIGNMENT   )
141
142 DALI_TYPE_REGISTRATION_END()
143
144 const Scripting::StringEnum LAYOUT_POLICY_STRING_TABLE[] =
145 {
146  { "fixed",    Toolkit::TableView::FIXED    },
147  { "relative", Toolkit::TableView::RELATIVE },
148  { "fill",     Toolkit::TableView::FILL     },
149  { "fit",      Toolkit::TableView::FIT      }
150 };
151 const unsigned int LAYOUT_POLICY_STRING_TABLE_COUNT = sizeof(LAYOUT_POLICY_STRING_TABLE) / sizeof( LAYOUT_POLICY_STRING_TABLE[0] );
152
153 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
154 {
155   {"left",   HorizontalAlignment::LEFT},
156   {"center", HorizontalAlignment::CENTER},
157   {"right",  HorizontalAlignment::RIGHT}
158 };
159 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof(HORIZONTAL_ALIGNMENT_STRING_TABLE) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
160
161 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
162 {
163   {"top",    VerticalAlignment::TOP},
164   {"center", VerticalAlignment::CENTER},
165   {"bottom", VerticalAlignment::BOTTOM}
166 };
167 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof(VERTICAL_ALIGNMENT_STRING_TABLE) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
168
169 } // Unnamed namespace
170
171 Toolkit::TableView TableView::New( unsigned int initialRows, unsigned int initialColumns )
172 {
173   // Create the implementation, temporarily owned by this handle on stack
174   IntrusivePtr< TableView > impl = new TableView( initialRows, initialColumns );
175
176   // Pass ownership to CustomActor handle
177   Toolkit::TableView handle( *impl );
178
179   // Second-phase init of the implementation
180   // This can only be done after the CustomActor connection has been made...
181   impl->Initialize();
182
183   return handle;
184 }
185
186 bool TableView::AddChild( Actor& child, const Toolkit::TableView::CellPosition& position )
187 {
188   // check that the child is valid
189   DALI_ASSERT_ALWAYS( child );
190
191   // if child is already parented, we adopt it
192   child.Unparent();
193
194   // check if we need to expand our data array
195   if( position.rowIndex >= mCellData.GetRows() )
196   {
197     // only adding new rows
198     ResizeContainers( position.rowIndex + 1, mCellData.GetColumns() );
199   }
200
201   if( position.columnIndex >= mCellData.GetColumns() )
202   {
203     // only adding new columns
204     ResizeContainers( mCellData.GetRows(), position.columnIndex + 1 );
205   }
206
207   // check if there already is something in this cell
208   if( mCellData[ position.rowIndex ][ position.columnIndex ].actor )
209   {
210     return false; // cannot share a cell, it would complicate all logic and not bring much benefit
211   }
212
213   RelayoutingLock lock( *this );
214   // adopt the child
215   Self().Add( child );
216
217   // if child spans multiple rows of columns
218   if( ( position.rowSpan > 1 ) && ( position.rowIndex + position.rowSpan > mCellData.GetRows() ) )
219   {
220     // increase table size for the full span, only increasing rows
221     ResizeContainers( position.rowIndex + position.rowSpan, mCellData.GetColumns() );
222   }
223
224   if( ( position.columnSpan > 1 ) && ( position.columnIndex + position.columnSpan > mCellData.GetColumns() ) )
225   {
226     // increase table size for the full span, only increasing columns
227     ResizeContainers( mCellData.GetRows(), position.columnIndex + position.columnSpan );
228   }
229
230   // Fill in all cells that need the data
231   CellData data;
232   data.actor = child;
233   data.position = position;
234
235   for( unsigned int row = position.rowIndex; row < ( position.rowIndex + position.rowSpan ); ++row )
236   {
237     // store same information to all cells, this way we can identify
238     // if a cell is the prime location of an actor or a spanned one
239     for( unsigned int column = position.columnIndex; column < ( position.columnIndex + position.columnSpan ); ++column )
240     {
241       // store same information to all cells, this way we can identify
242       // if a cell is the prime location of an actor or a spanned one
243       mCellData[ row ][ column ] = data;
244     }
245   }
246
247   // Relayout the whole table
248   if( mRowData[position.rowIndex].sizePolicy == Toolkit::TableView::FIT && position.rowSpan == 1 )
249   {
250     mRowDirty = true;
251   }
252   if( mColumnData[position.columnIndex].sizePolicy == Toolkit::TableView::FIT && position.columnSpan == 1 )
253   {
254     mColumnDirty = true;
255   }
256
257   RelayoutRequest();
258
259   return true;    // Addition successful
260 }
261
262 Actor TableView::GetChildAt( const Toolkit::TableView::CellPosition& position )
263 {
264   if( ( position.rowIndex < mCellData.GetRows() ) && ( position.columnIndex < mCellData.GetColumns() ) )
265   {
266     return mCellData[ position.rowIndex ][ position.columnIndex ].actor;
267   }
268
269   // Return an empty handle
270   return Actor();
271 }
272
273 Actor TableView::RemoveChildAt( const Toolkit::TableView::CellPosition& position )
274 {
275   // get the child handle
276   Actor child = GetChildAt( position );
277   // if no real actor there, nothing else to be done
278   if( child )
279   {
280     RelayoutingLock lock( *this );
281     // Remove the child, this will trigger a call to OnChildRemove
282     Self().Remove( child );
283
284     // relayout the table only if instances were found
285     if( RemoveAllInstances( child ) )
286     {
287       if( mRowData[position.rowIndex].sizePolicy == Toolkit::TableView::FIT )
288       {
289         mRowDirty = true;
290       }
291       if( mColumnData[position.columnIndex].sizePolicy == Toolkit::TableView::FIT )
292       {
293         mColumnDirty = true;
294       }
295       RelayoutRequest();
296     }
297   }
298   // return the child back to caller
299   return child;
300 }
301
302 bool TableView::FindChildPosition( const Actor& child, Toolkit::TableView::CellPosition& positionOut )
303 {
304   // Only find valid child actors
305   if( child )
306   {
307     // Walk through the layout data
308     const unsigned int rowCount = mCellData.GetRows();
309     const unsigned int columnCount = mCellData.GetColumns();
310
311     for( unsigned int row = 0; row < rowCount; ++row )
312     {
313       for( unsigned int column = 0; column < columnCount; ++column )
314       {
315         if( mCellData[ row ][ column ].actor == child )
316         {
317           positionOut = mCellData[ row ][ column ].position;
318           return true;
319         }
320       }
321     }
322   }
323
324   return false;
325 }
326
327 void TableView::InsertRow( unsigned int rowIndex )
328 {
329   RelayoutingLock lock( *this );
330
331   mCellData.InsertRow( rowIndex );
332
333   // Need to update the cell infos for the items that moved
334   const unsigned int rowCount = mCellData.GetRows();
335   const unsigned int columnCount = mCellData.GetColumns();
336
337   for( unsigned int row = 0; row < rowCount; ++row )
338   {
339     for( unsigned int column = 0; column < columnCount; ++column )
340     {
341       Toolkit::TableView::CellPosition& position = mCellData[ row ][ column ].position;
342
343       // If cell is spanning and above and spans to inserted row
344       if( ( position.rowSpan > 1 ) && ( position.rowIndex <= rowIndex ) &&
345           ( position.rowIndex + position.rowSpan > rowIndex ) )
346       {
347         // Increment span
348         position.rowSpan++;
349
350         // Copy cell to occupy the new column
351         mCellData[ rowIndex ][ column ] = mCellData[ row ][ column ];
352       }
353       else if( row > rowIndex )   // If below of inserted row, increase row index
354       {
355         // Increment index
356         position.rowIndex++;
357       }
358     }
359   }
360
361   // Expand row data array
362   mRowData.Insert( mRowData.Begin() + rowIndex, RowColumnData() );
363
364   // Sizes may have changed, so relayout
365   mRowDirty = true;
366   RelayoutRequest();
367 }
368
369 void TableView::DeleteRow( unsigned int rowIndex )
370 {
371   std::vector< Actor > ignored;
372   DeleteRow( rowIndex, ignored );
373 }
374
375 void TableView::DeleteRow( unsigned int rowIndex, std::vector<Actor>& removed )
376 {
377   RelayoutingLock lock( *this );
378
379   // Delete the row
380   std::vector< CellData > lost;
381   mCellData.DeleteRow( rowIndex, lost );
382
383   // Need to update the cell infos for the items that moved
384   const unsigned int rowCount = mCellData.GetRows();
385   const unsigned int columnCount = mCellData.GetColumns();
386
387   for( unsigned int row = 0; row < rowCount; ++row )
388   {
389     for( unsigned int column = 0; column < columnCount; ++column )
390     {
391       Toolkit::TableView::CellPosition& position = mCellData[ row ][ column ].position;
392
393       // If cell is spanning and above and spans to deleted row
394       if( ( position.rowSpan > 1 ) && ( position.rowIndex <= rowIndex ) &&
395           ( position.rowIndex + position.rowSpan > rowIndex ) )
396       {
397         // Decrement span
398         if( position.rowSpan > 1 )
399         {
400           position.rowSpan--;
401         }
402       }
403       else if( row >= rowIndex )    // If below of or at the inserted row, decrease row index
404       {
405         // Decrement index
406         if( position.rowIndex > 0 )
407         {
408           position.rowIndex--;
409         }
410       }
411     }
412   }
413
414   // 1 row removed, 0 columns
415   RemoveAndGetLostActors( lost, removed, 1u, 0u );
416
417   // Contract row data array
418   mRowData.Erase( mRowData.Begin() + rowIndex );
419
420   // Sizes may have changed, so relayout
421   mRowDirty = true;
422   // it is possible that the deletion of row leads to remove of child which might further lead to the change of FIT column
423   mColumnDirty = true;
424
425   RelayoutRequest();
426 }
427
428 void TableView::InsertColumn( unsigned int columnIndex )
429 {
430   RelayoutingLock lock( *this );
431
432   // Insert the new column
433   mCellData.InsertColumn( columnIndex );
434
435   // Need to update the cell infos for the items that moved
436   const unsigned int rowCount = mCellData.GetRows();
437   const unsigned int columnCount = mCellData.GetColumns();
438
439   for( unsigned int row = 0; row < rowCount; ++row )
440   {
441     for( unsigned int column = 0; column < columnCount; ++column )
442     {
443       Toolkit::TableView::CellPosition& position = mCellData[ row ][ column ].position;
444
445       // If cell is spanning and left side and spans to inserted column
446       if( ( position.columnSpan > 1 ) && ( position.columnIndex <= columnIndex ) &&
447           ( position.columnIndex + position.columnSpan > columnIndex ) )
448       {
449         // Increment span
450         position.columnSpan++;
451
452         // Copy cell to occupy the new column
453         mCellData[ row ][ columnIndex ] = mCellData[ row ][ column ];
454       }
455       else if( column > columnIndex )   // If on the right side of inserted column, increase column index
456       {
457         // Increment index
458         position.columnIndex++;
459       }
460     }
461   }
462
463   // Expand column data array
464   mColumnData.Insert( mColumnData.Begin() + columnIndex, RowColumnData() );
465
466   // Sizes may have changed so relayout
467   mColumnDirty = true;
468   RelayoutRequest();
469 }
470
471 void TableView::DeleteColumn( unsigned int columnIndex )
472 {
473   std::vector< Actor > ignored;
474   DeleteColumn( columnIndex, ignored );
475 }
476
477 void TableView::DeleteColumn( unsigned int columnIndex, std::vector<Actor>& removed )
478 {
479   RelayoutingLock lock( *this );
480
481   // Remove the column
482   std::vector< CellData > lost;
483   mCellData.DeleteColumn( columnIndex, lost );
484
485   // Need to update the cell infos for the items that moved
486   const unsigned int rowCount = mCellData.GetRows();
487   const unsigned int columnCount = mCellData.GetColumns();
488
489   for( unsigned int row = 0; row < rowCount; ++row )
490   {
491     for( unsigned int column = 0; column < columnCount; ++column )
492     {
493       Toolkit::TableView::CellPosition& position = mCellData[ row ][ column ].position;
494
495       // If cell is spanning and left side and spans to inserted column
496       if( ( position.columnSpan > 1 ) && ( position.columnIndex <= columnIndex ) &&
497           ( position.columnIndex + position.columnSpan > columnIndex ) )
498       {
499         // Decrement span
500         if( position.columnSpan > 1 )
501         {
502           position.columnSpan--;
503         }
504       }
505       else if( column >= columnIndex )    // If on the right side of or at the inserted column, decrease column index
506       {
507         // Decrement index
508         if( position.columnIndex > 0 )
509         {
510           position.columnIndex--;
511         }
512       }
513     }
514   }
515
516   // 0 rows, 1 column removed
517   RemoveAndGetLostActors( lost, removed, 0u, 1u );
518
519   // Contract column data array
520   mColumnData.Erase( mColumnData.Begin() + columnIndex );
521
522   // Size may have changed so relayout
523   mColumnDirty = true;
524   // it is possible that the deletion of column leads to remove of child which might further lead to the change of FIT row
525   mRowDirty = true;
526
527   RelayoutRequest();
528 }
529
530 void TableView::Resize( unsigned int rows, unsigned int columns )
531 {
532   std::vector< Actor > ignored;
533   Resize( rows, columns, ignored );
534 }
535
536 void TableView::Resize( unsigned int rows, unsigned int columns, std::vector<Actor>& removed )
537 {
538   RelayoutingLock lock( *this );
539
540   unsigned int oldRows = GetRows();
541   unsigned int oldColumns = GetColumns();
542
543   // Resize data array
544   std::vector< CellData > lost;
545   ResizeContainers( rows, columns, lost );
546
547   // Calculate if we lost rows
548   unsigned int rowsRemoved = 0;
549   unsigned int newRows = GetRows();
550
551   if( oldRows < newRows )
552   {
553     rowsRemoved = newRows - oldRows;
554   }
555
556   // Calculate if we lost columns
557   unsigned int columnsRemoved = 0;
558   unsigned int newColumns = GetColumns();
559   if( oldColumns < newColumns )
560   {
561     rowsRemoved = newColumns - oldColumns;
562   }
563
564   RemoveAndGetLostActors( lost, removed, rowsRemoved, columnsRemoved );
565
566   // Sizes may have changed so request a relayout
567   mRowDirty = true;
568   mColumnDirty = true;
569   RelayoutRequest();
570 }
571
572 void TableView::SetCellPadding( Size padding )
573 {
574   // If padding really changed
575   if( padding != mPadding )
576   {
577     mPadding = padding;
578
579     RelayoutRequest();
580   }
581 }
582
583 Size TableView::GetCellPadding()
584 {
585   return mPadding;
586 }
587
588 void TableView::SetFitHeight( unsigned int rowIndex )
589 {
590   DALI_ASSERT_ALWAYS( rowIndex < mRowData.Size() );
591
592   if( mRowData[ rowIndex ].sizePolicy != Toolkit::TableView::FIT )
593   {
594     mRowData[ rowIndex ].sizePolicy = Toolkit::TableView::FIT;
595
596     mRowDirty = true;
597     RelayoutRequest();
598   }
599 }
600
601 bool TableView::IsFitHeight( unsigned int rowIndex ) const
602 {
603   DALI_ASSERT_ALWAYS( rowIndex < mRowData.Size() );
604
605   return mRowData[ rowIndex ].sizePolicy == Toolkit::TableView::FIT;
606 }
607
608 void TableView::SetFitWidth( unsigned int columnIndex )
609 {
610   DALI_ASSERT_ALWAYS( columnIndex < mColumnData.Size() );
611
612   if( mColumnData[ columnIndex ].sizePolicy != Toolkit::TableView::FIT )
613   {
614     mColumnData[ columnIndex ].sizePolicy = Toolkit::TableView::FIT;
615
616     mColumnDirty = true;
617     RelayoutRequest();
618   }
619 }
620
621 bool TableView::IsFitWidth( unsigned int columnIndex ) const
622 {
623   DALI_ASSERT_ALWAYS( columnIndex < mColumnData.Size() );
624
625   return mColumnData[ columnIndex ].sizePolicy == Toolkit::TableView::FIT;
626 }
627
628 void TableView::SetFixedHeight( unsigned int rowIndex, float height )
629 {
630   DALI_ASSERT_ALWAYS( rowIndex < mRowData.Size() );
631
632   RowColumnData& data = mRowData[ rowIndex ];
633   data.size = height;
634   data.sizePolicy = Toolkit::TableView::FIXED;
635
636   mRowDirty = true;
637   RelayoutRequest();
638 }
639
640 float TableView::GetFixedHeight( unsigned int rowIndex ) const
641 {
642   DALI_ASSERT_ALWAYS( rowIndex < mRowData.Size() );
643
644   return mRowData[ rowIndex ].size;
645 }
646
647 void TableView::SetFixedWidth( unsigned int columnIndex, float width )
648 {
649   DALI_ASSERT_ALWAYS( columnIndex < mColumnData.Size() );
650
651   RowColumnData& data = mColumnData[ columnIndex ];
652   data.size = width;
653   data.sizePolicy = Toolkit::TableView::FIXED;
654
655   mColumnDirty = true;
656   RelayoutRequest();
657 }
658
659 float TableView::GetFixedWidth( unsigned int columnIndex ) const
660 {
661   DALI_ASSERT_ALWAYS( columnIndex < mColumnData.Size() );
662
663   return mColumnData[ columnIndex ].size;
664 }
665
666 void TableView::SetRelativeHeight( unsigned int rowIndex, float heightPercentage )
667 {
668   DALI_ASSERT_ALWAYS( rowIndex < mRowData.Size() );
669
670   RowColumnData& data = mRowData[ rowIndex ];
671   data.fillRatio = heightPercentage;
672   data.sizePolicy = Toolkit::TableView::RELATIVE;
673
674   mRowDirty = true;
675   RelayoutRequest();
676 }
677
678 float TableView::GetRelativeHeight( unsigned int rowIndex ) const
679 {
680   DALI_ASSERT_ALWAYS( rowIndex < mRowData.Size() );
681
682   return mRowData[ rowIndex ].fillRatio;
683 }
684
685 void TableView::SetRelativeWidth( unsigned int columnIndex, float widthPercentage )
686 {
687   DALI_ASSERT_ALWAYS( columnIndex < mColumnData.Size() );
688
689   RowColumnData& data = mColumnData[ columnIndex ];
690   data.fillRatio = widthPercentage;
691   data.sizePolicy = Toolkit::TableView::RELATIVE;
692
693   mColumnDirty = true;
694   RelayoutRequest();
695 }
696
697 float TableView::GetRelativeWidth( unsigned int columnIndex ) const
698 {
699   DALI_ASSERT_ALWAYS( columnIndex < mColumnData.Size() );
700
701   return mColumnData[ columnIndex ].fillRatio;
702 }
703
704 void TableView::OnCalculateRelayoutSize( Dimension::Type dimension )
705 {
706   if( (dimension & Dimension::WIDTH) && mColumnDirty )
707   {
708     /*
709      * FIXED and FIT have size in pixel
710      * Nothing to do with FIXED, as its value is assigned by user and will not get changed
711      *
712      * Need to update the size for FIT column here
713      */
714     CalculateFitSizes( mColumnData, Dimension::WIDTH );
715
716     /* RELATIVE and FILL have size in ratio
717      * Their size in pixel is not available until we get the negotiated size for the whole table
718      * Nothing to do with RELATIVE, as its ratio is assigned by user and will not get changed
719      *
720      * Need to update the ratio for FILL column here
721      */
722     CalculateFillSizes( mColumnData );
723
724     mFixedTotals.width = CalculateTotalFixedSize( mColumnData );
725   }
726
727   if( (dimension & Dimension::HEIGHT) && mRowDirty )
728   {
729     // refer to the comment above
730     CalculateFitSizes( mRowData, Dimension::HEIGHT );
731
732     // refer to the comment above
733     CalculateFillSizes( mRowData );
734
735     mFixedTotals.height = CalculateTotalFixedSize( mRowData );
736   }
737 }
738
739 void TableView::OnLayoutNegotiated( float size, Dimension::Type dimension )
740 {
741   // Update the column sizes
742   if( (dimension & Dimension::WIDTH) && mColumnDirty )
743   {
744     float remainingSize = size - mFixedTotals.width;
745     if( remainingSize < 0.0f )
746     {
747       remainingSize = 0.0f;
748     }
749
750     // update every column position in ColumnData array
751     float cumulatedWidth = 0.0f;
752     for( unsigned int column = 0, columnCount = mCellData.GetColumns(); column < columnCount; ++column )
753     {
754       if( mColumnData[ column ].sizePolicy == Toolkit::TableView::FILL ||  mColumnData[ column ].sizePolicy == Toolkit::TableView::RELATIVE)
755       {
756         mColumnData[ column ].size = mColumnData[ column ].fillRatio * remainingSize;
757       }
758
759       cumulatedWidth += mColumnData[ column ].size;
760       mColumnData[column].position = cumulatedWidth;
761     }
762
763     mColumnDirty = false;
764   }
765
766   // Update the row sizes
767   if( (dimension & Dimension::HEIGHT) && mRowDirty )
768   {
769     float remainingSize = size - mFixedTotals.height;
770     if( remainingSize < 0.0f )
771     {
772       remainingSize = 0.0f;
773     }
774
775     // update every row position in RowData array
776     float cumulatedHeight = 0.0f;
777     for( unsigned int row = 0, rowCount = mCellData.GetRows(); row < rowCount; ++row )
778     {
779       if( mRowData[ row ].sizePolicy == Toolkit::TableView::FILL ||  mRowData[ row ].sizePolicy == Toolkit::TableView::RELATIVE)
780       {
781         mRowData[ row ].size = mRowData[ row ].fillRatio * remainingSize;
782       }
783
784       cumulatedHeight += mRowData[ row ].size;
785       mRowData[row].position = cumulatedHeight;
786     }
787
788     mRowDirty = false;
789   }
790 }
791
792 void TableView::OnSizeSet( const Vector3& size )
793 {
794   // If this table view is size negotiated by another actor or control, then the
795   // rows and columns must be recalculated or the new size will not take effect.
796   mRowDirty = mColumnDirty = true;
797   RelayoutRequest();
798 }
799
800 void TableView::OnRelayout( const Vector2& size, RelayoutContainer& container )
801 {
802   // Go through the layout data
803   for( unsigned int row = 0, rowCount = mCellData.GetRows(); row < rowCount; ++row )
804   {
805     for( unsigned int column = 0, columnCount = mCellData.GetColumns(); column < columnCount; ++column )
806     {
807       CellData& cellData= mCellData[ row ][ column ];
808       Actor& actor = cellData.actor;
809       const Toolkit::TableView::CellPosition position = cellData.position;
810
811       // If there is an actor and this is the main cell of the actor.
812       // An actor can be in multiple cells if its row or column span is more than 1.
813       // We however must lay out each actor only once.
814       if( actor &&  position.rowIndex == row && position.columnIndex == column )
815       {
816         // Anchor actor to top left of the cell
817         actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
818         actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
819
820         Padding padding;
821         actor.GetPadding( padding );
822
823         float left = column > 0 ? mColumnData[column-1].position : 0.f;
824         float right = mColumnData[column+position.columnSpan-1].position;
825         float top = row > 0 ? mRowData[row-1].position : 0.f;
826         float bottom = mRowData[row+position.rowSpan-1].position;
827
828         if( cellData.horizontalAlignment == HorizontalAlignment::LEFT )
829         {
830           actor.SetX( left + mPadding.width + padding.left );
831         }
832         else if( cellData.horizontalAlignment ==  HorizontalAlignment::RIGHT )
833         {
834           actor.SetX( right - mPadding.width - padding.right - actor.GetRelayoutSize( Dimension::WIDTH ) );
835         }
836         else //if( cellData.horizontalAlignment ==  HorizontalAlignment::CENTER )
837         {
838           actor.SetX( (left + right + padding.left - padding.right - actor.GetRelayoutSize( Dimension::WIDTH )) * 0.5f );
839         }
840
841         if( cellData.verticalAlignment == VerticalAlignment::TOP )
842         {
843           actor.SetY( top + mPadding.height + padding.top );
844         }
845         else if( cellData.verticalAlignment == VerticalAlignment::BOTTOM )
846         {
847           actor.SetY( bottom - mPadding.height - padding.bottom -  actor.GetRelayoutSize( Dimension::HEIGHT ) );
848         }
849         else //if( cellData.verticalAlignment = VerticalAlignment::CENTER )
850         {
851           actor.SetY( (top + bottom + padding.top - padding.bottom - actor.GetRelayoutSize( Dimension::HEIGHT )) * 0.5f );
852         }
853       }
854     }
855   }
856 }
857
858 unsigned int TableView::GetRows()
859 {
860   return mCellData.GetRows();
861 }
862
863 unsigned int TableView::GetColumns()
864 {
865   return mCellData.GetColumns();
866 }
867
868 void TableView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
869 {
870   Toolkit::TableView tableView = Toolkit::TableView::DownCast( Dali::BaseHandle( object ) );
871
872   if( tableView )
873   {
874     TableView& tableViewImpl( GetImpl( tableView ) );
875     switch( index )
876     {
877       case Toolkit::TableView::Property::ROWS:
878       {
879         int rows = 0;
880         if( value.Get( rows ) && rows >= 0 )
881         {
882           if( static_cast<unsigned int>(rows) != tableViewImpl.GetRows() )
883           {
884             tableViewImpl.Resize( rows, tableViewImpl.GetColumns() );
885           }
886         }
887         break;
888       }
889       case Toolkit::TableView::Property::COLUMNS:
890       {
891         int columns = 0;
892         if( value.Get( columns ) && columns >= 0 )
893         {
894           if( static_cast<unsigned int>( columns ) != tableViewImpl.GetColumns() )
895           {
896             tableViewImpl.Resize( tableViewImpl.GetRows(), value.Get<int>() );
897           }
898         }
899         break;
900       }
901       case Toolkit::TableView::Property::CELL_PADDING:
902       {
903         tableViewImpl.SetCellPadding( value.Get<Vector2>() );
904         break;
905       }
906       case Toolkit::TableView::Property::LAYOUT_ROWS:
907       {
908         SetHeightOrWidthProperty( tableViewImpl, &TableView::SetFixedHeight, &TableView::SetRelativeHeight, &TableView::SetFitHeight, value );
909         break;
910       }
911       case Toolkit::TableView::Property::LAYOUT_COLUMNS:
912       {
913         SetHeightOrWidthProperty( tableViewImpl, &TableView::SetFixedWidth, &TableView::SetRelativeWidth, &TableView::SetFitWidth, value );
914         break;
915       }
916     }
917   }
918 }
919
920 Property::Value TableView::GetProperty( BaseObject* object, Property::Index index )
921 {
922   Property::Value value;
923
924   Toolkit::TableView tableView = Toolkit::TableView::DownCast( Dali::BaseHandle( object ) );
925
926   if( tableView )
927   {
928     TableView& tableViewImpl( GetImpl( tableView ) );
929     switch( index )
930     {
931       case Toolkit::TableView::Property::ROWS:
932       {
933         value = static_cast<int>( tableViewImpl.GetRows() );
934         break;
935       }
936       case Toolkit::TableView::Property::COLUMNS:
937       {
938         value = static_cast<int>( tableViewImpl.GetColumns() );
939         break;
940       }
941       case Toolkit::TableView::Property::CELL_PADDING:
942       {
943         value = tableViewImpl.GetCellPadding();
944         break;
945       }
946       case Toolkit::TableView::Property::LAYOUT_ROWS:
947       {
948         value = tableViewImpl.GetRowHeightsPropertyValue();
949         break;
950       }
951       case Toolkit::TableView::Property::LAYOUT_COLUMNS:
952       {
953         value = tableViewImpl.GetColumnWidthsPropertyValue();
954         break;
955       }
956     }
957   }
958
959   return value;
960 }
961
962 void TableView::OnChildAdd( Actor& child )
963 {
964   Control::OnChildAdd( child );
965
966   if( mLayoutingChild )
967   {
968     // we're in the middle of laying out children so no point doing anything here
969     return;
970   }
971
972   // Check child properties on actor to decide its position inside the table
973   HorizontalAlignment::Type horizontalAlignment = HorizontalAlignment::LEFT;
974   VerticalAlignment::Type verticalAlignment = VerticalAlignment::TOP;
975
976   if( child.GetPropertyType( Toolkit::TableView::ChildProperty::CELL_HORIZONTAL_ALIGNMENT ) != Property::NONE )
977   {
978     std::string value = child.GetProperty( Toolkit::TableView::ChildProperty::CELL_HORIZONTAL_ALIGNMENT ).Get<std::string >();
979     Scripting::GetEnumeration< HorizontalAlignment::Type >( value.c_str(),
980                                                             HORIZONTAL_ALIGNMENT_STRING_TABLE,
981                                                             HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
982                                                             horizontalAlignment );
983   }
984
985   if( child.GetPropertyType( Toolkit::TableView::ChildProperty::CELL_VERTICAL_ALIGNMENT ) != Property::NONE )
986   {
987     std::string value = child.GetProperty( Toolkit::TableView::ChildProperty::CELL_VERTICAL_ALIGNMENT ).Get<std::string >();
988     Scripting::GetEnumeration< VerticalAlignment::Type >( value.c_str(),
989                                                           VERTICAL_ALIGNMENT_STRING_TABLE,
990                                                           VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
991                                                           verticalAlignment );
992   }
993
994   Toolkit::TableView::CellPosition cellPosition;
995   if( child.GetPropertyType( Toolkit::TableView::ChildProperty::ROW_SPAN ) != Property::NONE )
996   {
997     cellPosition.rowSpan = static_cast<unsigned int>( child.GetProperty( Toolkit::TableView::ChildProperty::ROW_SPAN ).Get<float>() );
998   }
999
1000   if( child.GetPropertyType( Toolkit::TableView::ChildProperty::COLUMN_SPAN ) != Property::NONE )
1001   {
1002     cellPosition.columnSpan = static_cast<unsigned int>( child.GetProperty( Toolkit::TableView::ChildProperty::COLUMN_SPAN ).Get<float>() );
1003   }
1004
1005   if( child.GetPropertyType( Toolkit::TableView::ChildProperty::CELL_INDEX ) != Property::NONE )
1006   {
1007     Vector2 indices = child.GetProperty( Toolkit::TableView::ChildProperty::CELL_INDEX ).Get<Vector2 >();
1008     cellPosition.rowIndex = static_cast<unsigned int>( indices.x );
1009     cellPosition.columnIndex = static_cast<unsigned int>( indices.y );
1010
1011     AddChild( child, cellPosition );
1012     SetCellAlignment(cellPosition, horizontalAlignment, verticalAlignment);
1013
1014     // Do not continue
1015     return;
1016   }
1017
1018   // Find the first available cell to store the actor in
1019   const unsigned int rowCount = mCellData.GetRows();
1020   const unsigned int columnCount = mCellData.GetColumns();
1021   for( unsigned int row = 0; row < rowCount; ++row )
1022   {
1023     for( unsigned int column = 0; column < columnCount; ++column )
1024     {
1025       if( !(mCellData[ row ][ column ].actor) )
1026       {
1027         // Put the actor in the cell
1028         CellData data;
1029         data.actor = child;
1030         data.position.columnIndex = column;
1031         data.position.rowIndex = row;
1032         data.horizontalAlignment = horizontalAlignment;
1033         data.verticalAlignment = verticalAlignment;
1034         mCellData[ row ][ column ] = data;
1035
1036         // Don't continue
1037         RelayoutRequest();
1038         return;
1039       }
1040     }
1041   }
1042
1043   // No empty cells, so increase size of the table
1044   unsigned int newColumnCount = ( columnCount > 0 ) ? columnCount : 1;
1045   ResizeContainers( rowCount + 1, newColumnCount );
1046
1047   // Put the actor in the first cell of the new row
1048   CellData data;
1049   data.actor = child;
1050   data.position.rowIndex = rowCount;
1051   data.position.columnIndex = 0;
1052   data.horizontalAlignment = horizontalAlignment;
1053   data.verticalAlignment = verticalAlignment;
1054   mCellData[ rowCount ][ 0 ] = data;
1055   RelayoutRequest();
1056 }
1057
1058 void TableView::OnChildRemove( Actor& child )
1059 {
1060   // dont process if we're in the middle of bigger operation like delete row, column or resize
1061   if( !mLayoutingChild )
1062   {
1063     // relayout the table only if instances were found
1064     if( RemoveAllInstances( child ) )
1065     {
1066       RelayoutRequest();
1067     }
1068   }
1069
1070   Control::OnChildRemove( child );
1071 }
1072
1073 TableView::TableView( unsigned int initialRows, unsigned int initialColumns )
1074 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
1075   mCellData( initialRows, initialColumns ),
1076   mLayoutingChild( false ),
1077   mRowDirty( true ),     // Force recalculation first time
1078   mColumnDirty( true )
1079 {
1080   mCurrentColumn = 0;
1081   mCurrentRow = 0;
1082   SetKeyboardNavigationSupport( true );
1083   ResizeContainers( initialRows, initialColumns );
1084 }
1085
1086 void TableView::OnInitialize()
1087 {
1088   // Make self as keyboard focusable and focus group
1089   Actor self = Self();
1090   self.SetKeyboardFocusable(true);
1091   SetAsKeyboardFocusGroup(true);
1092 }
1093
1094 void TableView::ResizeContainers( unsigned int rows, unsigned int columns )
1095 {
1096   std::vector<CellData> ignored;
1097   ResizeContainers( rows, columns, ignored );
1098 }
1099
1100 void TableView::ResizeContainers( unsigned int rows, unsigned int columns, std::vector<CellData>& removed )
1101 {
1102   // Resize cell data
1103   mCellData.Resize( rows, columns, removed );
1104
1105   // We don't care if these go smaller, data will be regenerated or is not needed anymore
1106   mRowData.Resize( rows );
1107   mColumnData.Resize( columns );
1108 }
1109
1110 void TableView::RemoveAndGetLostActors( const std::vector<CellData>& lost, std::vector<Actor>& removed,
1111                                         unsigned int rowsRemoved, unsigned int columnsRemoved )
1112 {
1113   // iterate through all lost cells
1114   std::vector< CellData >::const_iterator iter = lost.begin();
1115   for( ; iter != lost.end(); ++iter )
1116   {
1117     // if it is a valid actor
1118     if( (*iter).actor )
1119     {
1120       // is this actor still somewhere else in the table
1121       Toolkit::TableView::CellPosition position;
1122       if( FindChildPosition( (*iter).actor, position ) )
1123       {
1124         // it must be spanning multiple cells, position contains the top left most one
1125         // check if position is left of the removed location
1126         if( position.columnIndex < (*iter).position.columnIndex )
1127         {
1128           // if column span is greater than 1
1129           if( mCellData[ position.rowIndex ][ position.columnIndex ].position.columnSpan > 1 )
1130           {
1131             // decrease column span
1132             mCellData[ position.rowIndex ][ position.columnIndex ].position.columnSpan -= columnsRemoved;
1133           }
1134         }
1135         // check if position is left of the removed location
1136         if( position.rowIndex < (*iter).position.rowIndex )
1137         {
1138           // if row span is greater than 1
1139           if( mCellData[ position.rowIndex ][ position.columnIndex ].position.rowSpan > 1 )
1140           {
1141             // decrease row span
1142             mCellData[ position.rowIndex ][ position.columnIndex ].position.rowSpan -= rowsRemoved;
1143           }
1144         }
1145       }
1146       else
1147       {
1148         // this actor is gone for good
1149         // add actor to removed container
1150         removed.push_back( (*iter).actor );
1151         // we dont want the child actor anymore
1152         Self().Remove( (*iter).actor );
1153       }
1154     }
1155   }
1156 }
1157
1158 bool TableView::RemoveAllInstances( const Actor& child )
1159 {
1160   bool found = false;
1161   // walk through the layout data
1162   const unsigned int rowCount = mCellData.GetRows();
1163   const unsigned int columnCount = mCellData.GetColumns();
1164   for( unsigned int row = 0; row < rowCount; ++row )
1165   {
1166     for( unsigned int column = 0; column < columnCount; ++column )
1167     {
1168       if( mCellData[ row ][ column ].actor == child )
1169       {
1170         // clear the cell, NOTE that the cell might be spanning multiple cells
1171         mCellData[ row ][ column ] = CellData();
1172         found = true;
1173       }
1174     }
1175   }
1176   return found;
1177 }
1178
1179 void TableView::SetHeightOrWidthProperty(TableView& tableViewImpl,
1180                                          void(TableView::*funcFixed)(unsigned int, float),
1181                                          void(TableView::*funcRelative)(unsigned int, float),
1182                                          void(TableView::*funcFit)(unsigned int),
1183                                          const Property::Value& value )
1184 {
1185   Property::Map* map = value.GetMap();
1186   if( map )
1187   {
1188     unsigned int index(0);
1189     for ( unsigned int i = 0, count = map->Count(); i < count; ++i )
1190     {
1191       Property::Value& item = map->GetValue(i);
1192       Property::Map* childMap = item.GetMap();
1193
1194       std::istringstream( map->GetKey(i) ) >> index;
1195       if( childMap )
1196       {
1197         Property::Value* policy = childMap->Find( "policy" );
1198         Property::Value* childMapValue = childMap->Find( "value" );
1199         if( policy && childMapValue )
1200         {
1201           std::string policyValue;
1202           policy->Get( policyValue );
1203           Toolkit::TableView::LayoutPolicy policy;
1204           if( Scripting::GetEnumeration< Toolkit::TableView::LayoutPolicy >( policyValue.c_str(),
1205                                                                              LAYOUT_POLICY_STRING_TABLE,
1206                                                                              LAYOUT_POLICY_STRING_TABLE_COUNT,
1207                                                                              policy ) )
1208           {
1209             if( policy == Toolkit::TableView::FIXED  )
1210             {
1211               (tableViewImpl.*funcFixed)( index, childMapValue->Get<float>() );
1212             }
1213             else if( policy == Toolkit::TableView::RELATIVE )
1214             {
1215               (tableViewImpl.*funcRelative)( index, childMapValue->Get<float>() );
1216             }
1217             else if( policy == Toolkit::TableView::FIT )
1218             {
1219               (tableViewImpl.*funcFit)( index );
1220             }
1221             // do nothing for FILL policy
1222           }
1223         }
1224       }
1225     }
1226   }
1227 }
1228
1229 Property::Value TableView::GetRowHeightsPropertyValue()
1230 {
1231   Property::Map map;
1232   GetMapPropertyValue( mRowData, map);
1233   return Property::Value(map);
1234 }
1235
1236 Property::Value TableView::GetColumnWidthsPropertyValue()
1237 {
1238   Property::Map map;
1239   GetMapPropertyValue( mColumnData, map);
1240   return Property::Value(map);
1241 }
1242
1243 void TableView::GetMapPropertyValue( const RowColumnArray& data, Property::Map& map )
1244 {
1245   const char* fixedPolicy = Scripting::GetEnumerationName< Toolkit::TableView::LayoutPolicy >( Toolkit::TableView::FIXED,
1246                                                                                                LAYOUT_POLICY_STRING_TABLE,
1247                                                                                                LAYOUT_POLICY_STRING_TABLE_COUNT );
1248   const char* relativePolicy = Scripting::GetEnumerationName< Toolkit::TableView::LayoutPolicy >( Toolkit::TableView::RELATIVE,
1249                                                                                                   LAYOUT_POLICY_STRING_TABLE,
1250                                                                                                   LAYOUT_POLICY_STRING_TABLE_COUNT );
1251   const char* fillPolicy = Scripting::GetEnumerationName< Toolkit::TableView::LayoutPolicy >( Toolkit::TableView::FILL,
1252                                                                                               LAYOUT_POLICY_STRING_TABLE,
1253                                                                                               LAYOUT_POLICY_STRING_TABLE_COUNT );
1254   const char* fitPolicy = Scripting::GetEnumerationName< Toolkit::TableView::LayoutPolicy >( Toolkit::TableView::FIT,
1255                                                                                              LAYOUT_POLICY_STRING_TABLE,
1256                                                                                              LAYOUT_POLICY_STRING_TABLE_COUNT );
1257
1258   const RowColumnArray::SizeType count = data.Size();
1259   for( RowColumnArray::SizeType i = 0; i < count; i++ )
1260   {
1261     const RowColumnData& dataInstance = data[ i ];
1262
1263     Property::Map item;
1264     switch( dataInstance.sizePolicy )
1265     {
1266       case Toolkit::TableView::FIXED:
1267       {
1268         item[ "policy" ] = fixedPolicy;
1269         item[ "value" ] = dataInstance.size;
1270         break;
1271       }
1272       case Toolkit::TableView::RELATIVE:
1273       {
1274         item[ "policy" ] = relativePolicy;
1275         item[ "value" ] = dataInstance.fillRatio;
1276         break;
1277       }
1278       case Toolkit::TableView::FIT:
1279       {
1280         item[ "policy" ] = fitPolicy;
1281         item[ "value" ] = 0.f;
1282         break;
1283       }
1284       case Toolkit::TableView::FILL:
1285       default:
1286       {
1287         item[ "policy" ] = fillPolicy;
1288         item[ "value" ] = 0.f;
1289         break;
1290       }
1291     }
1292     std::ostringstream ss;
1293     ss << i;
1294     map[ ss.str() ] = item;
1295   }
1296 }
1297
1298 TableView::~TableView()
1299 {
1300   // nothing to do
1301 }
1302
1303 Actor TableView::GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1304 {
1305   Actor nextFocusableActor;
1306
1307   if ( !currentFocusedActor )
1308   {
1309     // Nothing is currently focused, so the child in the first cell should be focused.
1310     nextFocusableActor = GetChildAt(Toolkit::TableView::CellPosition(0, 0));
1311   }
1312   else
1313   {
1314     Toolkit::TableView::CellPosition position;
1315     if( FindChildPosition( currentFocusedActor, position ) )
1316     {
1317       // The current focused actor is a child of TableView
1318       bool focusLost = false;
1319       int currentRow = position.rowIndex;
1320       int currentColumn = position.columnIndex;
1321       int numberOfColumns = GetColumns();
1322       int numberOfRows = GetRows();
1323
1324       bool lastCell = false;
1325       Actor nextValidActor;
1326
1327       switch ( direction )
1328       {
1329         case Toolkit::Control::KeyboardFocus::LEFT:
1330         {
1331           do
1332           {
1333             if(--currentColumn < 0)
1334             {
1335               currentColumn = numberOfColumns - 1;
1336               if(--currentRow < 0)
1337               {
1338                 lastCell = true;
1339                 currentRow = loopEnabled ? numberOfRows - 1 : 0;
1340                 focusLost = (currentRow == 0);
1341               }
1342             }
1343             nextValidActor = GetChildAt(Toolkit::TableView::CellPosition(currentRow, currentColumn));
1344           } while ( !nextValidActor && !lastCell );
1345           break;
1346         }
1347         case Toolkit::Control::KeyboardFocus::RIGHT:
1348         {
1349           do
1350           {
1351             if(++currentColumn > numberOfColumns - 1)
1352             {
1353               currentColumn = 0;
1354               if(++currentRow > numberOfRows - 1)
1355               {
1356                 lastCell = true;
1357                 currentRow = loopEnabled ? 0 : numberOfRows - 1;
1358                 focusLost = (currentRow == numberOfRows - 1);
1359               }
1360             }
1361             nextValidActor = GetChildAt(Toolkit::TableView::CellPosition(currentRow, currentColumn));
1362           } while ( !nextValidActor && !lastCell );
1363           break;
1364         }
1365         case Toolkit::Control::KeyboardFocus::UP:
1366         {
1367           do
1368           {
1369             if(--currentRow < 0)
1370             {
1371               lastCell = true;
1372               currentRow = loopEnabled ? numberOfRows - 1 : 0;
1373               focusLost = (currentRow == 0);
1374             }
1375             nextValidActor = GetChildAt(Toolkit::TableView::CellPosition(currentRow, currentColumn));
1376           } while ( !nextValidActor && !lastCell );
1377           break;
1378         }
1379         case Toolkit::Control::KeyboardFocus::DOWN:
1380
1381         {
1382           do
1383           {
1384             if(++currentRow > numberOfRows - 1)
1385             {
1386               lastCell = true;
1387               currentRow = loopEnabled ? 0 : numberOfRows - 1;
1388               focusLost = (currentRow == numberOfRows - 1);
1389             }
1390             nextValidActor = GetChildAt(Toolkit::TableView::CellPosition(currentRow, currentColumn));
1391           } while ( !nextValidActor && !lastCell );
1392           break;
1393         }
1394         default:
1395         {
1396           break;
1397         }
1398       }
1399
1400       // Move the focus if we haven't lost it.
1401       if(!focusLost)
1402       {
1403         // Save the new focus cell positions of TableView.
1404         mCurrentColumn = currentColumn;
1405         mCurrentRow = currentRow;
1406
1407         nextFocusableActor = GetChildAt(Toolkit::TableView::CellPosition(currentRow, currentColumn));
1408       }
1409     }
1410     else
1411     {
1412       // The current focused actor is not within TableView.
1413       // This means that the TableView has gained the Focus again.
1414
1415       unsigned int numberOfColumns = GetColumns();
1416       unsigned int numberOfRows = GetRows();
1417
1418       if( (mCurrentRow != 0 && mCurrentColumn != 0) && // Last saved cell was not the first cell
1419           (mCurrentRow != numberOfRows - 1 && mCurrentColumn != numberOfColumns - 1) ) // Last saved cell was not the last cell
1420       {
1421         // This condition handles the cases when parent TableView gained the focus again after the child layout
1422         // container (i.e. TableView) has no more items (i.e. actors) to be focused on in a given direction.
1423
1424         // Move the focus to next cell towards the given direction in a TableView if the last saved cell was not the first or last cell.
1425         nextFocusableActor = GetNextKeyboardFocusableActor(GetChildAt(Toolkit::TableView::CellPosition(mCurrentRow, mCurrentColumn)), direction, loopEnabled);
1426       }
1427       else
1428       {
1429         // Otherwise, move the focus to either the first or the last cell according to the given direction.
1430         if(direction == Toolkit::Control::KeyboardFocus::LEFT || direction == Toolkit::Control::KeyboardFocus::UP)
1431         {
1432           nextFocusableActor = GetChildAt(Toolkit::TableView::CellPosition(numberOfRows - 1, numberOfColumns - 1));
1433         }
1434         else
1435         {
1436           nextFocusableActor = GetChildAt(Toolkit::TableView::CellPosition(0, 0));
1437         }
1438       }
1439     }
1440   }
1441
1442   return nextFocusableActor;
1443 }
1444
1445 Vector3 TableView::GetNaturalSize()
1446 {
1447   // Natural size is the size of all fixed cell widths or heights. This ignores cells with relative heights.
1448   return Vector3( mFixedTotals.width, mFixedTotals.height, 1.0f );
1449 }
1450
1451 float TableView::CalculateChildSize( const Actor& child, Dimension::Type dimension )
1452 {
1453   Toolkit::TableView::CellPosition position;
1454   if( FindChildPosition( child, position) )
1455   {
1456     switch( dimension )
1457     {
1458       case Dimension::WIDTH:
1459       {
1460         float cellSize = 0.0f;
1461         cellSize = mColumnData[position.columnIndex+position.columnSpan-1].position
1462                  - (position.columnIndex > 0 ? mColumnData[position.columnIndex-1].position : 0.f)
1463                  - mPadding.width * 2.0f;
1464
1465         if( cellSize < 0.0f )
1466         {
1467           cellSize = 0.0f;
1468         }
1469
1470         return cellSize;
1471       }
1472
1473       case Dimension::HEIGHT:
1474       {
1475         float cellSize = 0.0f;
1476
1477         cellSize = mRowData[position.rowIndex+position.rowSpan-1].position
1478                  - (position.rowIndex > 0 ? mRowData[position.rowIndex-1].position : 0.f)
1479                  - mPadding.height * 2.0f;
1480
1481         if( cellSize < 0.0f )
1482         {
1483           cellSize = 0.0f;
1484         }
1485
1486         return cellSize;
1487       }
1488       default:
1489       {
1490         return 0.0f;
1491       }
1492     }
1493   }
1494
1495   return 0.0f;    // Child not found
1496 }
1497
1498 bool TableView::RelayoutDependentOnChildren( Dimension::Type dimension )
1499 {
1500   if ( Control::RelayoutDependentOnChildren( dimension ) )
1501   {
1502     return true;
1503   }
1504
1505   return FindFit( mRowData ) || FindFit( mColumnData );
1506 }
1507
1508 void TableView::SetCellAlignment( Toolkit::TableView::CellPosition position, HorizontalAlignment::Type horizontal, VerticalAlignment::Type vertical )
1509 {
1510   // Check if we need to expand our data array
1511   if( position.rowIndex >= mCellData.GetRows() )
1512   {
1513     // Only adding new rows
1514     ResizeContainers( position.rowIndex + 1, mCellData.GetColumns() );
1515   }
1516
1517   if( position.columnIndex >= mCellData.GetColumns() )
1518   {
1519     // Only adding new columns
1520     ResizeContainers( mCellData.GetRows(), position.columnIndex + 1 );
1521   }
1522
1523   // Set the alignment of the cell
1524   CellData& data = mCellData[ position.rowIndex ][ position.columnIndex ];
1525   data.horizontalAlignment = horizontal;
1526   data.verticalAlignment = vertical;
1527 }
1528
1529 void TableView::CalculateFillSizes( RowColumnArray& data )
1530 {
1531   // First pass: Count number of fill entries and calculate used relative space
1532   Dali::Vector< RowColumnData* > fillData;
1533   float relativeTotal = 0.0f;
1534
1535   const unsigned int dataCount = data.Size();
1536
1537   for( unsigned int i = 0; i < dataCount; ++i )
1538   {
1539     RowColumnData& dataInstance = data[ i ];
1540
1541     if( dataInstance.sizePolicy == Toolkit::TableView::RELATIVE )
1542     {
1543       relativeTotal += dataInstance.fillRatio;
1544     }
1545     else if(dataInstance.sizePolicy == Toolkit::TableView::FILL)
1546     {
1547       fillData.PushBack( &dataInstance );
1548     }
1549   }
1550
1551   // Second pass: Distribute remaining relative space
1552   const unsigned int fillCount = fillData.Size();
1553   if( fillCount > 0 )
1554   {
1555     if( relativeTotal > 1.0f )
1556     {
1557       relativeTotal = 1.0f;
1558     }
1559
1560     const float evenFillRatio = (1.0f - relativeTotal ) / fillCount;
1561
1562     for( unsigned int i = 0; i < fillCount; ++i )
1563     {
1564       fillData[ i ]->fillRatio = evenFillRatio;
1565     }
1566   }
1567 }
1568
1569 float TableView::CalculateTotalFixedSize( const RowColumnArray& data )
1570 {
1571   float totalSize = 0.0f;
1572
1573   const unsigned int dataCount = data.Size();
1574
1575   for( unsigned int i = 0; i < dataCount; ++i )
1576   {
1577     const RowColumnData& dataInstance = data[ i ];
1578
1579     switch( dataInstance.sizePolicy )
1580     {
1581       // we have absolute size to FIXED and FIT column/row and relative size for RELATIVE and FILL column/row
1582       case Toolkit::TableView::FIXED:
1583       case Toolkit::TableView::FIT:
1584       {
1585         totalSize += dataInstance.size;
1586         break;
1587       }
1588
1589       default:
1590       {
1591         break;
1592       }
1593     }
1594   }
1595
1596   return totalSize;
1597 }
1598
1599 Vector2 TableView::GetCellPadding( Dimension::Type dimension )
1600 {
1601   switch( dimension )
1602   {
1603     case Dimension::WIDTH:
1604     {
1605       return Vector2( mPadding.x, mPadding.x );
1606     }
1607     case Dimension::HEIGHT:
1608     {
1609       return Vector2( mPadding.y, mPadding.y );
1610     }
1611     default:
1612     {
1613       break;
1614     }
1615   }
1616
1617   return Vector2();
1618 }
1619
1620 void TableView::CalculateFitSizes( RowColumnArray& data, Dimension::Type dimension )
1621 {
1622   Vector2 cellPadding = GetCellPadding( dimension );
1623
1624   const unsigned int dataCount = data.Size();
1625
1626   for( unsigned int i = 0; i < dataCount; ++i )
1627   {
1628     RowColumnData& dataInstance = data[ i ];
1629
1630     if( dataInstance.sizePolicy == Toolkit::TableView::FIT )
1631     {
1632       // Find the size of the biggest actor in the row or column
1633       float maxActorHeight = 0.0f;
1634
1635       unsigned int fitCount = ( dimension == Dimension::WIDTH ) ? mCellData.GetRows() : mCellData.GetColumns();
1636
1637       for( unsigned int j = 0; j < fitCount; ++j )
1638       {
1639         unsigned int row = ( dimension == Dimension::WIDTH ) ? j : i;
1640         unsigned int column = ( dimension == Dimension::WIDTH ) ? i : j;
1641         DALI_ASSERT_DEBUG( row < mCellData.GetRows() );
1642         DALI_ASSERT_DEBUG( column < mCellData.GetColumns() );
1643
1644         const CellData& cellData = mCellData[ row ][ column ];
1645         const Actor& actor = cellData.actor;
1646         if( actor )
1647         {
1648           if( FitToChild( actor, dimension ) && ( dimension == Dimension::WIDTH ) ? ( cellData.position.columnSpan == 1 ) : ( cellData.position.rowSpan == 1 )  )
1649           {
1650             maxActorHeight = std::max( maxActorHeight, actor.GetRelayoutSize( dimension ) + cellPadding.x + cellPadding.y );
1651           }
1652         }
1653       }
1654
1655       dataInstance.size = maxActorHeight;
1656     }
1657   }
1658 }
1659
1660 bool TableView::FindFit( const RowColumnArray& data )
1661 {
1662   for( unsigned int i = 0, count = data.Size(); i < count; ++i )
1663   {
1664     if( data[ i ].sizePolicy == Toolkit::TableView::FIT )
1665     {
1666       return true;
1667     }
1668   }
1669
1670   return false;
1671 }
1672
1673 } // namespace Internal
1674
1675 } // namespace Toolkit
1676
1677 } // namespace Dali