#include "stdafx.h"
#include "TabOrder.h"

TabOrder::TabOrder()
{

}

TabOrder::~TabOrder()
{

}

void TabOrder::Add(SkinnableWindow& wnd)
{
	windows.push_back(&wnd);
}

void TabOrder::Commit()
{
	SkinnableWindow * wnd0 = NULL, * wnd1 = NULL;
	std::list<SkinnableWindow*>::iterator i;
	int j,maxj=0;

	// first find max
	for(i=windows.begin();i!=windows.end();i++){
		wnd0 = *i;
		if(wnd0->taborder>maxj) maxj = wnd0->taborder;
	}

	wnd0 = NULL;
	for(j=0;j<maxj+1;j++){

		// find the window associated with this tab order
		if(wnd1)
			wnd0 = wnd1;
		wnd1 = NULL;
		for(i=windows.begin();i!=windows.end();i++){
			wnd1 = *i;
			if(wnd1->taborder==j)
				break;
		}

		if(wnd0 && wnd1){
			wnd0->SetTabTo(wnd1->GetTabToTarget());
			wnd1->SetTabFrom(wnd0->GetTabFromTarget());
		}
	}
}

