tableview.monkey2 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. Namespace mojox
  2. #rem monkeydoc The TableView class.
  3. #end
  4. Class TableView Extends ScrollView
  5. #rem monkeydoc Creates a new table view.
  6. #end
  7. Method New()
  8. Style=GetStyle( "TableView" )
  9. _docker=New DockingView
  10. ContentView=_docker
  11. End
  12. Method New( columns:Int,rows:Int=0 )
  13. Self.New()
  14. Columns=columns
  15. Rows=rows
  16. End
  17. Method New( columns:String[] )
  18. Self.New()
  19. For Local column:=Eachin columns
  20. AddColumn( column )
  21. Next
  22. End
  23. Property Columns:Int()
  24. Return _cols.Length
  25. Setter( columns:Int )
  26. DebugAssert( columns>=0 )
  27. If columns=_cols.Length Return
  28. Local n:=_cols.Length
  29. For Local i:=columns Until n
  30. _docker.RemoveView( _cols[i] )
  31. Next
  32. _cols.Resize( columns )
  33. For Local i:=n Until columns
  34. _cols[i]=New TableColumn( "",Null,_rowSizes )
  35. _docker.AddView( _cols[i],"left" )
  36. Next
  37. RequestRender()
  38. End
  39. Property Rows:Int()
  40. Return _numRows
  41. Setter( rows:Int )
  42. DebugAssert( rows>=0 )
  43. If rows=_numRows Return
  44. _numRows=rows
  45. _rowSizes.Resize( _numRows )
  46. For Local col:=Eachin _cols
  47. col._rows.Resize( _numRows )
  48. Next
  49. RequestRender()
  50. End
  51. #rem monkeydoc Adds a column.
  52. Returns index of new column.
  53. @return Index of new column.
  54. #end
  55. Method AddColumn:Int( text:String="",icon:Image=Null,size:String="",draggable:Bool=False )
  56. Local col:=New TableColumn( text,icon,_rowSizes )
  57. _docker.AddView( col,"left",size,draggable )
  58. _cols.Push( col )
  59. Return _cols.Length-1
  60. End
  61. #rem monkeydoc Adds rows.
  62. Returns index of first row added.
  63. @return Index of first row added.
  64. #end
  65. Method AddRows:Int( num:Int )
  66. _numRows+=num
  67. _rowSizes.Resize( _numRows )
  68. For Local col:=Eachin _cols
  69. col.Rows.Resize( _numRows )
  70. Next
  71. Return _numRows-num
  72. End
  73. #rem monkeydoc Removes all rows.
  74. #end
  75. Method RemoveAllRows()
  76. For Local col:=Eachin _cols
  77. For Local row:=0 Until _numRows
  78. col.SetView( row,Null )
  79. Next
  80. col.Rows.Clear()
  81. Next
  82. _rowSizes.Clear()
  83. _numRows=0
  84. End
  85. #rem monkeydoc Removes all rows and columns.
  86. #end
  87. Method RemoveAll()
  88. RemoveAllRows()
  89. _docker.RemoveAllViews()
  90. _cols.Clear()
  91. End
  92. #rem monkeydoc Gets the view at a cell location.
  93. #end
  94. Operator[]:View( col:Int,row:Int )
  95. Assert( col>=0 And col<_cols.Length And row>=0 And row<_numRows )
  96. Return _cols[col].Rows[row]
  97. End
  98. #rem monkeydoc Sets the view at a cell location.
  99. #end
  100. Operator[]=( col:Int,row:Int,view:View )
  101. Assert( col>=0 And col<_cols.Length And row>=0 And row<_numRows )
  102. _cols[col].SetView( row,view )
  103. End
  104. #rem monkeydoc Number of columns.
  105. Deprecated! Use [[Columns]] property instead.
  106. #end
  107. Property NumColumns:Int()
  108. Return _cols.Length
  109. End
  110. #rem monkeydoc Number of rows.
  111. Deprecated! Use [[Rows]] property instead.
  112. #end
  113. Property NumRows:Int()
  114. Return _numRows
  115. End
  116. Protected
  117. Method OnMeasure:Vec2i() Override
  118. Local size:=Super.OnMeasure()
  119. Local h:=0
  120. For Local i:=0 Until _rowSizes.Length
  121. h+=_rowSizes[i]
  122. Next
  123. size.y=Max( h,size.y )
  124. Return size
  125. End
  126. Method OnLayout() Override
  127. Super.OnLayout()
  128. For Local i:=0 Until _rowSizes.Length
  129. ' _rowSizes[i]=0
  130. Next
  131. End
  132. Private
  133. Class TableColumn Extends View
  134. Method New( text:String,icon:Image,rowSizes:Stack<Int> )
  135. Style=GetStyle( "TableColumn" )
  136. _rowSizes=rowSizes
  137. _header=New Label( text,icon )
  138. _header.Style=GetStyle( "TableHeader" )
  139. AddChildView( _header )
  140. _rows.Resize( _rowSizes.Length )
  141. End
  142. Property Rows:Stack<View>()
  143. Return _rows
  144. End
  145. Method SetView( row:Int,view:View )
  146. If _rows[row] RemoveChildView( _rows[row] )
  147. _rows[row]=view
  148. If _rows[row] AddChildView( _rows[row] )
  149. End
  150. Protected
  151. Method OnMeasure:Vec2i() Override
  152. Local size:=_header.LayoutSize
  153. For Local i:=0 Until _rows.Length
  154. Local view:=_rows[i]
  155. If Not view Continue
  156. _rowSizes[i]=Max( _rowSizes[i],view.LayoutSize.y )
  157. size.x=Max( size.x,view.LayoutSize.x )
  158. size.y+=view.LayoutSize.y
  159. Next
  160. Return size
  161. End
  162. Method OnLayout() Override
  163. _header.Frame=New Recti( 0,0,Width,_header.LayoutSize.y )
  164. Local y:=_header.LayoutSize.y
  165. For Local i:=0 Until _rows.Length
  166. Local view:=_rows[i]
  167. Local y2:=y+_rowSizes[i]
  168. If view view.Frame=New Recti( 0,y,Width,y2 )
  169. y=y2
  170. Next
  171. End
  172. Private
  173. Field _header:Label
  174. Field _rowSizes:Stack<Int>
  175. Field _rows:=New Stack<View>
  176. End
  177. Field _docker:DockingView
  178. Field _numRows:Int
  179. Field _rowSizes:=New Stack<Int>
  180. Field _cols:=New Stack<TableColumn>
  181. End