123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- Namespace mojox
- #rem monkeydoc @hidden
- #end
- Class TabButton Extends Button
- Field CloseClicked:Void()
- Method New( text:String,icon:Image,view:View,closable:bool )
- Super.New( text,icon )
-
- Style=GetStyle( "TabButton" )
-
- _view=view
-
- If closable
- _close=New Button
- _close.Style=GetStyle( "TabClose" )
- _close.Icon=_close.Style.Icons[0]
- _close.Clicked=Lambda()
- CloseClicked()
- End
-
- AddView( _close )
- Endif
- End
-
- Property View:View()
-
- Return _view
-
- Setter( view:View )
-
- _view=view
- End
-
- Private
-
- Field _button:Button
- Field _close:Button
-
- Field _view:View
- End
- #rem monkeydoc @hidden
- #end
- Class TabBar Extends ToolBar
- Method New()
- Style=GetStyle( "TabBar" )
- Layout="fill-x"
- End
-
- End
- #rem monkeydoc The TabViewFlags enum.
- Tab view flags:
- | TabViewFlags | Description
- |:--------------|:-----------
- | ClosableTabs | Tabs can be closed.
- | DraggableTabs | Tabs can be dragged.
- #end
- Enum TabViewFlags
- ClosableTabs=1
- DraggableTabs=2
- End
- #rem monkeydoc The TabView class.
- #end
- Class TabView Extends DockingView
- #rem monkeydoc Invoked when the current tab changes.
- #end
- Field CurrentChanged:Void()
-
- #rem monkeydoc @hidden
- #end
- Field Clicked:Void()
-
- #rem monkeydoc Invoked when a tab is right clicked.
- #end
- Field RightClicked:Void()
-
- #rem monkeydoc Invoked when a tab is double clicked.
- #end
- Field DoubleClicked:Void()
-
- #rem monkeydoc Invoked when a tab is double clicked.
- #end
- Field CloseClicked:Void( index:Int )
- #rem monkeydoc Invoked when a tab is dragged.
- #end
- Field Dragged:Void()
- #rem monkeydoc Creates a new tab view.
- #end
- Method New( flags:TabViewFlags=Null )
-
- _flags=flags
-
- Style=GetStyle( "TabView" )
- Layout="fill"
- _tabBar=New TabBar
-
- AddView( _tabBar,"top" )
- End
-
- #rem monkeydoc Tab view flags.
- #end
- Property Flags:TabViewFlags()
-
- Return _flags
- End
-
- #rem monkeydoc Number of tabs
- #end
- Property NumTabs:Int()
-
- Return _tabs.Length
- End
-
- #rem monkeydoc The current index.
- #end
- Property CurrentIndex:Int()
- If _current Return TabIndex( _current.View )
- Return -1
-
- Setter( currentIndex:Int )
-
- MakeCurrent( _tabs[currentIndex],False )
- End
-
- #rem monkeydoc The current view.
- #end
- Property CurrentView:View()
-
- If _current Return _current.View
- Return Null
-
- Setter( currentView:View )
-
- MakeCurrent( _tabs[ TabIndex( currentView ) ],False )
- End
- #rem monkeydoc Gets a tab's view.
- #end
- Method TabView:View( index:Int )
- Assert( index>=0 And index<_tabs.Length,"Tab index out of range" )
-
- Return _tabs[index].View
- End
- #rem monkeydoc Gets a tab's index.
- #end
- Method TabIndex:Int( view:View )
-
- For Local i:=0 Until _tabs.Length
- If _tabs[i].View=view Return i
- Next
- Return -1
- End
- #rem monkeydoc Adds a tab.
- #end
- Method AddTab:Int( text:String,view:View,makeCurrent:Bool=False )
-
- Return AddTab( text,Null,view,makeCurrent )
- End
- Method AddTab:Int( text:String,icon:Image,view:View,makeCurrent:Bool=False )
-
- Assert( TabIndex( view )=-1,"View has already been added to TabView" )
-
- Local tab:=New TabButton( text,icon,view,_flags & TabViewFlags.ClosableTabs )
-
- tab.Clicked=Lambda()
-
- MakeCurrent( tab,True )
- Clicked()
- End
-
- tab.RightClicked=Lambda()
-
- MakeCurrent( tab,True )
-
- RightClicked()
- End
-
- tab.DoubleClicked=Lambda()
-
- MakeCurrent( tab,True )
-
- DoubleClicked()
- End
-
- tab.Dragged+=Lambda( v:Vec2i )
-
- If Not (_flags & TabViewFlags.DraggableTabs) return
- Local mx:=_tabBar.MouseLocation.x
- If mx<0 Return
-
- Local w:=tab.Bounds.Width
- Local x:=0,i:=_tabs.Length
- For Local j:=0 Until i
- If mx<x+w
- i=j
- Exit
- Endif
-
- If tab=_tabs[j] Continue
-
- x+=_tabs[j].Bounds.Width
- If mx<x Return
- Next
-
- Local i2:=_tabs.FindIndex( tab )
- If i=i2 Return
- If i>i2 i-=1
-
- _tabs.Erase( i2 )
- _tabs.Insert( i,tab )
-
- _tabBar.RemoveAllViews()
- For Local view:=Eachin _tabs
- _tabBar.AddView( view )
- Next
- RequestRender()
-
- Dragged()
- End
-
- tab.CloseClicked=Lambda()
-
- CloseClicked( TabIndex( tab.View ) )
- End
-
- Local index:=_tabs.Length
- _tabBar.AddView( tab )
- _tabs.Push( tab )
-
- If makeCurrent CurrentIndex=index
- Return index
- End
-
- #rem monkeydoc Removes a tab.
- #end
- Method RemoveTab( index:Int )
-
- If _current=_tabs[index]
- _current.Selected=False
- _current=Null
- ContentView=Null
- Endif
-
- _tabBar.RemoveView( _tabs[index] )
- _tabs.Erase( index )
- End
-
- Method RemoveTab( view:View )
-
- RemoveTab( TabIndex( view ) )
- End
-
- Method SetTabView( index:Int,view:View )
-
- _tabs[index].View=view
-
- If _tabs[index]=_current ContentView=view
- End
-
- Method SetTabView( view:View,newView:View )
- SetTabView( TabIndex( view ),newView )
- End
-
- #rem monkeydoc Sets a tab's text.
- #end
- Method SetTabText( index:Int,text:String )
-
- _tabs[index].Text=text
- End
-
- Method SetTabText( view:View,text:String )
-
- SetTabText( TabIndex( view ),text )
- End
-
- #rem monkeydoc Sets a tab's icon.
- #end
- Method SetTabIcon( index:Int,icon:Image )
-
- _tabs[index].Icon=icon
- End
-
- Method SetTabIcon( view:View,icon:Image )
-
- SetTabIcon( TabIndex( view ),icon )
- End
-
- Private
-
- Field _flags:TabViewFlags
-
- Field _tabBar:TabBar
-
- Field _tabs:=New Stack<TabButton>
-
- Field _current:TabButton
-
- Method MakeCurrent( tab:TabButton,notify:Bool )
-
- If tab=_current Return
-
- If _current _current.Selected=False
-
- ContentView=tab.View
-
- _current=tab
-
- If _current _current.Selected=True
-
- If notify CurrentChanged()
- End
-
- End
|