tableview.monkey2 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #rem monkeydoc Number of columns.
  13. #end
  14. Property NumColumns:Int()
  15. Return _cols.Length
  16. End
  17. #rem monkeydoc Number of rows.
  18. #end
  19. Property NumRows:Int()
  20. Return _numRows
  21. end
  22. #rem monkeydoc Adds a column.
  23. #end
  24. Method AddColumn:Int( text:String="",icon:Image=Null,size:String="",draggable:Bool=False )
  25. Local col:=New TableColumn( text,icon,_rowSizes )
  26. col.Rows.Resize( _numRows )
  27. _docker.AddView( col,"left",size,draggable )
  28. _cols.Push( col )
  29. Return _cols.Length-1
  30. End
  31. #rem monkeydoc Adds rows.
  32. #end
  33. Method AddRows( num:Int )
  34. _numRows+=num
  35. _rowSizes.Resize( _numRows )
  36. For Local col:=Eachin _cols
  37. col.Rows.Resize( _numRows )
  38. Next
  39. End
  40. #rem monkeydoc Removes all rows.
  41. #end
  42. Method RemoveAllRows()
  43. For Local col:=Eachin _cols
  44. For Local row:=0 Until _numRows
  45. col.SetView( row,Null )
  46. Next
  47. col.Rows.Clear()
  48. Next
  49. _rowSizes.Clear()
  50. _numRows=0
  51. End
  52. #rem monkeydoc Removes all rows and columns.
  53. #end
  54. Method RemoveAll()
  55. RemoveAllRows()
  56. _docker.RemoveAllViews()
  57. _cols.Clear()
  58. End
  59. #rem monkeydoc Gets the view at a cell location.
  60. #end
  61. Operator[]:View( col:Int,row:Int )
  62. Assert( col>=0 And col<_cols.Length And row>=0 And row<_numRows )
  63. Return _cols[col].Rows[row]
  64. End
  65. #rem monkeydoc Sets the view at a cell location.
  66. #end
  67. Operator[]=( col:Int,row:Int,view:View )
  68. Assert( col>=0 And col<_cols.Length And row>=0 And row<_numRows )
  69. _cols[col].SetView( row,view )
  70. End
  71. Protected
  72. Method OnMeasure:Vec2i() Override
  73. Local size:=Super.OnMeasure()
  74. Local h:=0
  75. For Local i:=0 Until _rowSizes.Length
  76. h+=_rowSizes[i]
  77. Next
  78. size.y=Max( h,size.y )
  79. Return size
  80. End
  81. Method OnLayout() Override
  82. Super.OnLayout()
  83. For Local i:=0 Until _rowSizes.Length
  84. ' _rowSizes[i]=0
  85. Next
  86. End
  87. Private
  88. Class TableColumn Extends View
  89. Method New( text:String,icon:Image,rowSizes:Stack<Int> )
  90. Style=GetStyle( "TableColumn" )
  91. _rowSizes=rowSizes
  92. _header=New Label( text,icon )
  93. _header.Style=GetStyle( "TableHeader" )
  94. AddChildView( _header )
  95. End
  96. Property Rows:Stack<View>()
  97. Return _rows
  98. End
  99. Method SetView( row:Int,view:View )
  100. If _rows[row] RemoveChildView( _rows[row] )
  101. _rows[row]=view
  102. If _rows[row] AddChildView( _rows[row] )
  103. End
  104. Protected
  105. Method OnMeasure:Vec2i() Override
  106. Local size:=_header.LayoutSize
  107. For Local i:=0 Until _rows.Length
  108. Local view:=_rows[i]
  109. If Not view Continue
  110. _rowSizes[i]=Max( _rowSizes[i],view.LayoutSize.y )
  111. size.x=Max( size.x,view.LayoutSize.x )
  112. size.y+=view.LayoutSize.y
  113. Next
  114. Return size
  115. End
  116. Method OnLayout() Override
  117. _header.Frame=New Recti( 0,0,Width,_header.LayoutSize.y )
  118. Local y:=_header.LayoutSize.y
  119. For Local i:=0 Until _rows.Length
  120. Local view:=_rows[i]
  121. Local y2:=y+_rowSizes[i]
  122. If view view.Frame=New Recti( 0,y,Width,y2 )
  123. y=y2
  124. Next
  125. End
  126. Private
  127. Field _header:Label
  128. Field _rowSizes:Stack<Int>
  129. Field _rows:=New Stack<View>
  130. End
  131. Field _docker:DockingView
  132. Field _numRows:Int
  133. Field _rowSizes:=New Stack<Int>
  134. Field _cols:=New Stack<TableColumn>
  135. End