/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CScrollBar.cpp -  description
                             -------------------
    begin                : 29 December 2004
    copyright            : (C) Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#if defined(WIN32)
#pragma warning (disable: 4786)
#endif

#include <FL/Fl.h>
#include <FL/fl_draw.h>
#include <sptk3/CScrollBar.h>
#include <sptk3/CThemes.h>

//#include <stdio.h>

using namespace sptk;

int CScrollBar::handle(int event) {
	int area;  // area of scrollbar
	int X=x(), Y=y(), W=w(), H=h();

   // adjust slider area to be inside the arrow buttons:
	if (horizontal()) {
		if (W >= 3*H) {X += H; W -= 2*H;}
	} else {
		if (H >= 3*W) {Y += W; H -= 2*W;}
	}

   // which widget part is highlighted?
	int relx;
	int ww;
	if (horizontal()) {
		relx = Fl::event_x()-X;
		ww = W;
	} else {
		relx = Fl::event_y()-Y;
		ww = H;
	}
	if (relx < 0) area = 1;
	else if (relx >= ww) area = 2;
	else {
		int S = int(slider_size()*ww+.5);
		int T = (horizontal() ? H : W)/2+1;
		if (S < T) S = T;
		double val =
			(maximum()-minimum()) ? (value()-minimum())/(maximum()-minimum()) : 0.5;
		int sliderx;
		if (val >= 1.0) sliderx = ww-S;
		else if (val <= 0.0) sliderx = 0;
		else sliderx = int(val*(ww-S)+.5);
		if (Fl::event_button() == FL_MIDDLE_MOUSE) area = 8;
		else if (relx < sliderx) area = 5;
		else if (relx >= sliderx+S) area = 6;
		else area = 8;
	}

	switch (event) {
		case FL_ENTER:
		case FL_LEAVE:
			break;
		case FL_RELEASE:
			m_pushed = 0;
			break;
		case FL_PUSH:
			if (m_pushed) break;
			if (area != 8) m_pushed = area;
			break;
	}
	return Fl_Scrollbar::handle(event);
}

void CScrollBar::draw() {
	// Use the theme support to draw the scroll bars 
	CThemes::drawScrollBar(x(),y(),w(),h(),(float)minimum(),(float)maximum(),(float)value(),slider_size(),m_pushed,active_r()!=0);
}

void CScrollBar::preferredSize(int& w,int& h) {
	if (!CThemes::sizeScrollBar(w,h)) {
		if (horizontal()) {
		// Horizontal scroll bar
			h = 16;
		} else {
		// Vertical scroll bar
			w = 16;
		}
	}
}
