| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- //
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <TurboBadger/tb_widgets.h>
- #include <TurboBadger/tb_widgets_common.h>
- #include <TurboBadger/tb_tab_container.h>
- #include "UI.h"
- #include "UIEvents.h"
- #include "UITabContainer.h"
- #include "UILayout.h"
- #include "UIButton.h"
- using namespace tb;
- namespace Atomic
- {
- UITabContainer::UITabContainer(Context* context, bool createWidget) : UIWidget(context, false)
- {
- if (createWidget)
- {
- widget_ = new TBTabContainer();
- widget_->SetDelegate(this);
- GetSubsystem<UI>()->WrapWidget(this, widget_);
- }
- }
- UITabContainer::~UITabContainer()
- {
- }
- UIWidget* UITabContainer::GetCurrentPageWidget()
- {
- if (!widget_)
- return 0;
- TBWidget* w = ((TBTabContainer*)widget_)->GetCurrentPageWidget();
- return GetSubsystem<UI>()->WrapWidget(w);
- }
- int UITabContainer::GetNumPages()
- {
- if (!widget_)
- return 0;
- return ((TBTabContainer*)widget_)->GetNumPages();
- }
- UILayout* UITabContainer::GetTabLayout()
- {
- if (!widget_)
- return 0;
- TBLayout* layout = ((TBTabContainer*)widget_)->GetTabLayout();
- if (!layout)
- return 0;
- UI* ui = GetSubsystem<UI>();
- return (UILayout*) ui->WrapWidget(layout);
- }
- void UITabContainer::SetCurrentPage(int page)
- {
- if (!widget_)
- return;
- ((TBTabContainer*)widget_)->SetCurrentPage(page);
- }
- bool UITabContainer::OnEvent(const tb::TBWidgetEvent &ev)
- {
- return UIWidget::OnEvent(ev);
- }
- /// returns the current page number
- int UITabContainer::GetCurrentPage()
- {
- if (!widget_)
- return 0;
- return ((TBTabContainer*)widget_)->GetCurrentPage();
- }
- /// deletes a tab + page, returns true if successful
- bool UITabContainer::DeletePage( int page )
- {
- if (!widget_ || page < 0 || page > GetNumPages() - 1)
- return false;
- UILayout *uil = GetTabLayout();
- if (uil)
- {
- UIWidget* mytab = NULL;
- int nn=0;
- for (UIWidget *child = uil->GetFirstChild(); child; child = child->GetNext())
- if (nn++ == page)
- mytab = child;
- if (mytab)
- {
- mytab->UnsubscribeFromAllEvents();
- uil->RemoveChild( mytab, true );
- }
- }
- UIWidget *pages = GetContentRoot();
- if (pages)
- {
- UIWidget* mypage = NULL;
- int nn=0;
- for (UIWidget *child = pages->GetFirstChild(); child; child = child->GetNext())
- if (nn++ == page)
- mypage = child;
- if (mypage)
- {
- mypage->UnsubscribeFromAllEvents();
- pages->RemoveChild( mypage, true );
- }
- }
-
- Invalidate();
- // tab container "feature", can not set it to the page number that was removed.
- int num = 0;
- if ( page - 1 > 0 ) num = page - 1;
- SetCurrentPage(num);
- return true;
- }
- /// adds a tab + page from layout file
- void UITabContainer::AddTabPageFile ( const String &tabname, const String &layoutFile, bool selecttab )
- {
- UIButton* button = new UIButton(context_);
- button->SetText(tabname);
- button->SetId(tabname);
- UILayout *uil = GetTabLayout();
- if (uil && button)
- uil->AddChild(button);
- UILayout* layout = new UILayout(context_);
- layout->SetAxis(UI_AXIS_Y);
- layout->SetLayoutSize(UI_LAYOUT_SIZE_AVAILABLE);
- layout->SetLayoutPosition(UI_LAYOUT_POSITION_GRAVITY);
- layout->SetLayoutDistribution(UI_LAYOUT_DISTRIBUTION_AVAILABLE);
- layout->Load (layoutFile);
- UIWidget *pages = GetContentRoot();
- if (pages && layout)
- pages->AddChild(layout);
- Invalidate();
- if (selecttab)
- SetCurrentPage( GetNumPages() -1 );
- }
- /// adds a tab + page widget(s)
- void UITabContainer::AddTabPageWidget ( const String &tabname, UIWidget *widget, bool selecttab )
- {
- UIButton* button = new UIButton(context_);
- button->SetText(tabname);
- button->SetId(tabname);
- UILayout *uil = GetTabLayout();
- if (uil && button)
- uil->AddChild(button);
- UILayout* layout = new UILayout(context_);
- layout->SetAxis(UI_AXIS_Y);
- layout->SetLayoutSize(UI_LAYOUT_SIZE_AVAILABLE);
- layout->SetLayoutPosition(UI_LAYOUT_POSITION_GRAVITY);
- layout->SetLayoutDistribution(UI_LAYOUT_DISTRIBUTION_AVAILABLE);
- layout->AddChild(widget);
- UIWidget *pages = GetContentRoot();
- if (pages && layout)
- pages->AddChild(layout);
- Invalidate();
- if (selecttab)
- SetCurrentPage( GetNumPages() -1 );
- }
- /// undocks the page into a window with the tab name, and removes the tab
- void UITabContainer::UndockPage ( int page )
- {
- if (!widget_)
- return;
- ((TBTabContainer*)widget_)->UndockPage(page);
- // tab container "feature", can not set it to the page number that was removed.
- int num = 0;
- if ( page - 1 > 0 ) num = page - 1;
- SetCurrentPage(num);
- }
- /// docks content from a UIDockWindow with specified title
- bool UITabContainer::DockWindow ( String windowTitle )
- {
- if (!widget_)
- return false;
- bool done = ((TBTabContainer*)widget_)->DockFromWindow(windowTitle.CString());
- if (done)
- SetCurrentPage( GetNumPages() -1 );
- return done;
- }
- }
|