#include "split4.hpp"

// Create a basic window class to add the splitters.
SplitterWindow::SplitterWindow(ZafIChar *name, ZafObjectPersistence &persist) : 
	ZafWindow(name, persist)
{
	// Create the vertical list that will be added dynamically.
	list = new ZafVtList(0, 0, 0, 0);
	list->Add(new ZafScrollBar(ZAF_VERTICAL_SCROLL));
	list->SetBordered(true);
	ZafIChar buff[30];
	for (int i = 0; i < 15; i++)
	{
		sprintf(buff, ZAF_ITEXT("String %i"), i + 1);
		list->Add(new ZafString(0, 0, 0, new ZafStringData(buff)));
	}

	// Create the dynamic splitter.  This will be added at run time.
	dynamicSplitter = new ZafSplitter(ZAF_VERTICAL_SPLITTER, 15, false);
	text1 = GetObject(ZAF_ITEXT("TEXT1"));
	text2 = GetObject(ZAF_ITEXT("TEXT2"));

	// Get a pointer to my already existing splitter.
	splitter = DynamicPtrCast(GetObject(ZAF_ITEXT("SPLITTER")), ZafSplitter);

	zafWindowManager->Center(this);
}

SplitterWindow::~SplitterWindow()
{
	// If the dynamic objects aren't attached they need to be deleted.
	if (dynamicSplitter && !dynamicSplitter->parent)
		delete dynamicSplitter;

	if (list && !list->parent)
		delete list;
}

ZafEventType SplitterWindow::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = event.type;
	switch(ccode)
	{
	case ADD_SPLITTER:
		{
		if (Index(dynamicSplitter) == -1)
		{
			// Turn off updating till the mods are done.
			SetAutomaticUpdate(false);

			// Add the splitter and update the pane objects for
			// all the splitters.
			Add(dynamicSplitter);
			Add(list);
			splitter->SetPreviousPaneObject(dynamicSplitter);
			dynamicSplitter->SetPreviousPaneObject(list);
			dynamicSplitter->SetNextPaneObject(text1);

			// Let the window paint itself now that we're done.
			SetAutomaticUpdate(true);
		}
		}
		break;

	case REMOVE_SPLITTER:
		{
		if (Index(dynamicSplitter) != -1)
		{
			// Turn off updating till the mods are done.
			SetAutomaticUpdate(false);

			// Remove the dynamic splitter and list, then
			// reset the existing splitter.
			Subtract(dynamicSplitter);
			Subtract(list);
			splitter->SetPreviousPaneObject(text1);

			// Let the window paint itself now that we're done.
			SetAutomaticUpdate(true);
		}
		}
		break;

	default:
		ccode = ZafWindow::Event(event);
	}
	return (ccode);
}
