/////////////////////////////////////////////////////////////////////////////
// Name:        rescrollw.cpp
// Purpose:     wxReDirectScrolledWindow implementation
// Author:      Hans Van Leemputten
// Modified by:
// Created:     19/Apr/2001
// RCS-ID:      $Id: rescrolw.cpp,v 1.2 2001/04/23 21:55:34 vlabella Exp $
// Copyright:   (c) Hans Van Leemputten
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#ifdef __GNUG__
    #pragma implementation "rescrolw.cpp"
#endif

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif


#include "wx/wspp/rescrolw.h"
#include "wx/wspp/pagectrl.h"

#ifdef __WXMSW__
    #include "windows.h"
#endif

#ifdef __WXMOTIF__
// For wxRETAINED implementation
#ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
               //This code switches off the compiler warnings
# pragma message disable nosimpint
#endif
#include <Xm/Xm.h>
#ifdef __VMS__
# pragma message enable nosimpint
#endif
#endif

// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------

BEGIN_EVENT_TABLE(wxReDirectScrolledWindow, wxScrolledWindow)
    /*
	 *	EVT_SCROLLWIN
	 */
END_EVENT_TABLE()

IMPLEMENT_DYNAMIC_CLASS(wxReDirectScrolledWindow, wxScrolledWindow)

// ============================================================================
// implementation
// ============================================================================

// ----------------------------------------------------------------------------
// wxReDirectScrolledWindow creation
// ----------------------------------------------------------------------------
wxReDirectScrolledWindow::wxReDirectScrolledWindow()
{
    m_ScrollController = (wxWindow*) NULL;
}

bool wxReDirectScrolledWindow::Create(wxWindow *parent,
                              wxWindowID id,
                              const wxPoint& pos,
                              const wxSize& size,
                              long style,
                              const wxString& name)
{
    if (parent && KnownController(parent))
		m_ScrollController = parent;
	else
		m_ScrollController = this;

    return wxScrolledWindow::Create(parent, id, pos, size, style, name);
}
// ----------------------------------------------------------------------------
wxReDirectScrolledWindow::~wxReDirectScrolledWindow()
{
	// All done by ~wxScrolledWindow!
}

// ----------------------------------------------------------------------------
// Controller window handling
// ----------------------------------------------------------------------------
void wxReDirectScrolledWindow::SetControlWindow( wxWindow *target )
{
    wxASSERT_MSG( target, wxT("Controller window must not be NULL") );
    m_ScrollController = target;

	// TODO:: ADD code here to force a update get a correct scrollbar view.
}

// ----------------------------------------------------------------------------
// New scrolling implementation, Redirect it all :-)
// ----------------------------------------------------------------------------
void wxReDirectScrolledWindow::SetScrollbar(int orientation, int position, int thumbSize, int range, bool refresh)
{
	SetCaller();

	if (m_ScrollController == this)
		wxScrolledWindow::SetScrollbar(orientation, position, thumbSize, range, refresh);
	else
	{
		m_ScrollController->SetScrollbar(orientation, position, thumbSize, range, refresh);

		if (CallResult() == false)
			wxScrolledWindow::SetScrollbar(orientation, position, thumbSize, range, refresh);
	}
}
// ----------------------------------------------------------------------------
void wxReDirectScrolledWindow::SetScrollPos(int orientation, int pos, bool refresh)
{
	SetCaller();

	if (m_ScrollController == this)
		wxScrolledWindow::SetScrollPos(orientation, pos, refresh);
	else
	{
		m_ScrollController->SetScrollPos(orientation, pos, refresh);

		if (CallResult() == false)
			wxScrolledWindow::SetScrollPos(orientation, pos, refresh);
	}
}
// ----------------------------------------------------------------------------
int wxReDirectScrolledWindow::GetScrollThumb(int orientation)
{
	int result;

	SetCaller();

	if (m_ScrollController == this)
		result = wxScrolledWindow::GetScrollThumb(orientation);
	else
	{
		result = m_ScrollController->GetScrollThumb(orientation);

		if (CallResult() == false)
			result = wxScrolledWindow::GetScrollThumb(orientation);
	}

	return result;
}
// ----------------------------------------------------------------------------
int wxReDirectScrolledWindow::GetScrollPos(int orientation)
{
	int result;

	SetCaller();

	if (m_ScrollController == this)
		result = wxScrolledWindow::GetScrollPos(orientation);
	else
	{
		result = m_ScrollController->GetScrollPos(orientation);

		if (CallResult() == false)
			result = wxScrolledWindow::GetScrollPos(orientation);
	}

	return result;
}
// ----------------------------------------------------------------------------
int wxReDirectScrolledWindow::GetScrollRange(int orientation)
{
	int result;

	SetCaller();

	if (m_ScrollController == this)
		result = wxScrolledWindow::GetScrollRange(orientation);
	else
	{
		result = m_ScrollController->GetScrollRange(orientation);

		if (CallResult() == false)
			result = wxScrolledWindow::GetScrollRange(orientation);
	}

	return result;
}

// ----------------------------------------------------------------------------
// Implement controller handeling.
// ----------------------------------------------------------------------------
bool wxReDirectScrolledWindow::KnownController(wxWindow *parent)
{
	wxASSERT(parent); // DBG::

	return parent->IsKindOf(CLASSINFO(wxPagingCtrl));

	// TODO Extend the return to newly supported control classes.
}
// ----------------------------------------------------------------------------
bool wxReDirectScrolledWindow::CallResult()
{
	if (KnownController(m_ScrollController))
	{
		wxPagingCtrl *tmp_wxPagingCtrl = wxDynamicCast(m_ScrollController, wxPagingCtrl);
		if (tmp_wxPagingCtrl)
			return tmp_wxPagingCtrl->ResultLastScrollAction();

		// TODO Add new supported control classes here!

		wxASSERT_MSG(NULL, wxT("The class is missing a supported if()!!!"));
		return false;
	}
	else
		return true; // There is no way to check if it was oke.
}
// ----------------------------------------------------------------------------
void wxReDirectScrolledWindow::SetCaller()
{
	if (KnownController(m_ScrollController))
	{
		wxPagingCtrl *tmp_wxPagingCtrl = wxDynamicCast(m_ScrollController, wxPagingCtrl);
		if (tmp_wxPagingCtrl)
		{
			tmp_wxPagingCtrl->SetNextCaller(this);
			return;
		}

		// TODO Add new supported control classes here!
	}
}

// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------

