#include "XNoteBk.h"
#include "XBookPg.h"
#include "xcntevnt.h"
#include "xcolor.h"
#include "xexcept.h"
#include "xres.h"
#include "xreslib.h"
#include "xbitmap.h"

MRESULT HandleDefault(XWindow * w, ULONG msg, MPARAM mp1, MPARAM mp2, BOOL & handled);
void BuildChilds(HWND dlgHandle);


MRESULT EXPENTRY bookProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
	XNoteBookPage *w = (XNoteBookPage *) WinQueryWindowPtr(hwnd, 0);

	if (w)
	{
		BOOL handeld = FALSE;
		MRESULT mr = HandleDefault(w, msg, mp1, mp2, handeld);

		if (handeld)
			return mr;
	}
	return WinDefDlgProc(hwnd, msg, mp1, mp2);
}


/*@
@class XNoteBookPage
@parent XWindow
@type overview
@symbol _
*/

/*@ 
@class XNoteBookPage
@type overview
@symbol _
@remarks XNoteBookPage is a page of XNoteBook. You can add one ore more pages to
a notebook. The behaviour of a XNoteBookPage is like any other window so you can use
all functions like DoCommand, DoControl etc.
*/


/*@ XNoteBookPage :: GetBackgroundColor ( XColor * col )
@group colors
@remarks Returns the background color.
@parameters XColor * buffer
*/
void XNoteBookPage::GetBackgroundColor(XColor * col)
{
	owner->GetBackgroundColor(col);
}


/*@ XNoteBookPage::XNoteBookPage( const XNoteBook * o, const USHORT style, const USHORT order, const char * title, const XNoteBookPage * insertBehind, const XResource * res)
@group contructors/destructors
@remarks Construct a notebook-page and adds it to a given notebook
@parameters <t 'ø' c=2>
				øXNoteBook * notebook       ønotebook which gets the page
            øUSHORT style               østyle of the page:
													<t 'ø' c=2>
                                          øBP_MAJORTAB	ø
                                          øBP_MINORTAB	ø
                                          øBP_PAGEBUTTON	ø
                                          øBP_STATUSTEXTON	ø
                                          øBP_MAJOR	ø
                                          øBP_MINOR	ø
													</t>
                                       (can be or-ed, default is BP_MAJOR|BP_STATUSTEXTON)
            øUSHORT order               øwhere to insert:
													<t 'ø' c=2>
                                          øBP_LAST	ø
                                          øBP_FIRST	ø
                                          øBP_NEXT	ø
                                          øBP_PREV	ø
                                          øBP_TOP	ø
													</t>
                                       if BP_LAST or BP_FIRST, insertBehind is ignored (default BP_LAST)
            øXNoteBookPage * insertBehind   øinsert behind this page (default is NULL)
            øXResource * resource       øIt is possibe to load a dialog from the resources to be a bookpage. If you want to do
                                       so give here a XResource which contains the id of the dialog to load and the resourcelibrary
                                       where to load the dialog from. If you have created one or more pages from resources, you
                                       should resize the notebook.
                                    	Default is NULL.
				</t>
@exceptions	If the method fails an exception of the type XException is thrown.
*/
XNoteBookPage :: XNoteBookPage(const XNoteBook * o, const USHORT style, const USHORT order, const char *title, const XNoteBookPage * insertBehind, const XResource * res)
{
	if (res)
	{
		XColor col(COL_PALEGRAY);

		SetBackgroundColor(&col);
		if ((winhandle = WinLoadDlg(o->GetHandle(), o->GetHandle(), (PFNWP) bookProc, res->GetResourceLibrary()->GetModuleHandle(), res->GetID(), NULL)) == 0)
			OOLThrow("error loading notebook-template", -10);

		WinSetWindowPtr(winhandle, 0, this);
		BuildChilds(winhandle);
	}
	else
	{
		if (WinRegisterClass(WinQueryAnchorBlock(o->GetHandle()), (PSZ) "OOL_BOOKPAGE", (PFNWP) userProc, 0, 4) == FALSE)
			OOLThrow("error registering notebook-class", -10);

		winhandle = WinCreateWindow(o->GetHandle(), (PSZ) "OOL_BOOKPAGE", NULL, 0, 0, 0, 0, 0, o->GetHandle(), HWND_TOP, 0, this, 0);
	}

	ULONG p = (insertBehind ? insertBehind->id : 0);

	owner = (XNoteBook *) o;
	id = LONGFROMMR(WinSendMsg(o->GetHandle(), BKM_INSERTPAGE, (MPARAM) p, MPFROM2SHORT(style | BKA_AUTOPAGESIZE, order)));
	WinSendMsg(o->GetHandle(), BKM_SETPAGEWINDOWHWND, MPFROMLONG(id), MPFROMHWND(winhandle));
	if (title)
		SetText(title);
}


/*@ XNoteBookPage :: SetStatusText( const char * t)
@group text functions
@remarks Set the text of the status-line (if the page has one)
@parameters char * text    the text to display
*/
void XNoteBookPage::SetStatusText(const char *t) const
{
	WinSendMsg(owner->GetHandle(), BKM_SETSTATUSLINETEXT, MPFROMLONG(id), MPFROMP(t));
}


/*@ XNoteBookPage :: SetText( const char * t)
@group text functions
@remarks Set the text of the tab
@parameters char * text    the text to display
*/
void XNoteBookPage::SetText(const char *t)
{
	WinSendMsg(owner->GetHandle(), BKM_SETTABTEXT, MPFROMLONG(id), MPFROMP(t));
}


/*@ XNoteBookPage :: SetBitmap( const XBitmap * b)
@group text functions
@remarks Set the bitmap of the tab
@parameters XBitmap * bitmap    the bitmap to display
*/
void XNoteBookPage::SetBitmap(const XBitmap * b)
{
	WinSendMsg(owner->GetHandle(), BKM_SETTABBITMAP, MPFROMLONG(id), MPFROMLONG(b->GetHandle()));
}


/*@ XNoteBookPage :: SetTop( void )
@group misc
@remarks Set the page to the top of the notebook
*/
void XNoteBookPage::SetTop(void) const
{
	WinSendMsg(owner->GetHandle(), BKM_TURNTOPAGE, MPFROMLONG(id), 0);
}


/*@ XNoteBookPage :: GetPageCount( void )
@group misc
@remarks Returns the count of minor-pages behind this page up to the next major-page
@returns SHORT numberOfPages
*/
SHORT XNoteBookPage::GetPageCount(void) const
{
	return SHORT1FROMMR(WinSendMsg(winhandle, BKM_QUERYPAGECOUNT, (MPARAM) id, (MPARAM) BKA_MAJOR));
}
