// {{{ the iWear header
/**
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 * In particular this file is part of the Framework Input Library
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework 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 General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
// }}}

#ifndef __MOUSEMOVEMENTTRACKER_H
#define __MOUSEMOVEMENTTRACKER_H

#ifndef __FUNCTOR_H
#include <iwear/functor.h>
#endif

#ifndef __MOUSEINPUTDATA_H
#include <iwear_input/mouseinputdata.h>
#endif

#ifndef __WIDGETDATA_H
#include <iwear_output/widgetdata.h>
#endif

#include <X11/Xlib.h>
#include <string>
#include <iwear/i18n.h>

using namespace std;
using namespace iwear;
using namespace iwear::output;
using namespace iwear::input;

namespace iwear{
    namespace uiservices{

	// Forward Declarations (namespace iwear::uiservices)
	class MouseInputDaemon;


enum MOUSE_POSITION_1D{
    MOUSE_POSITION_FAR_LEFT,
    MOUSE_POSITION_LEFT,
    MOUSE_POSITION_CENTER,
    MOUSE_POSITION_RIGHT,
    MOUSE_POSITION_FAR_RIGHT,
    num_MENU_POSITION_1D
};


inline string to_string(MOUSE_POSITION_1D mouse_position)
{
    switch(mouse_position){
        case MOUSE_POSITION_FAR_LEFT : {
	    return (string(i18n::trans("Nav")) 
		    + string(" [ <<<<<< |        ]"));
	}
        case MOUSE_POSITION_LEFT : {
	    return (string(i18n::trans("Nav")) 
		    + string(" [    <<< |        ]"));
	}
        case MOUSE_POSITION_CENTER : {
	    return (string(i18n::trans("Nav")) 
		    + string(" [        |        ]"));
        }
        case MOUSE_POSITION_RIGHT : {
	    return (string(i18n::trans("Nav")) 
		    + string(" [        | >>>    ]"));
	}
        case MOUSE_POSITION_FAR_RIGHT : {
	    return (string(i18n::trans("Nav")) 
		    + string(" [        | >>>>>> ]"));
        }
        default : {
	    return (string(i18n::trans("Nav")) 
		    + string(" [        ?        ]"));
	}
    }
}


class MouseMovementTracker
    : public Functor<MouseInputData>
{

public:

    MouseMovementTracker(WidgetData* widget)
	: widget(widget),
	  screen_width(0),
	  position(MOUSE_POSITION_CENTER)
    {
	Display* display = XOpenDisplay(NULL);
	this->screen_width = XDisplayWidth(display,0);
	XCloseDisplay(display);	
    }

    virtual ~MouseMovementTracker( void ){}

    inline virtual void operator()(const MouseInputData& mouse_input_data)
    {
	MOUSE_POSITION_1D old_position = this->position;

	if(mouse_input_data.x < floor(1.0*(this->screen_width/6.0))){
	    this->position = MOUSE_POSITION_FAR_LEFT;
	}else if(mouse_input_data.x < floor(2.0*(this->screen_width/6.0))){
	    this->position = MOUSE_POSITION_LEFT;
	}else if(mouse_input_data.x < floor(4.0*(this->screen_width/6.0))){
	    this->position = MOUSE_POSITION_CENTER;
	}else if(mouse_input_data.x < floor(5.0*(this->screen_width/6.0))){
	    this->position = MOUSE_POSITION_RIGHT;
	}else{
	    this->position = MOUSE_POSITION_FAR_RIGHT;
	}

	if(old_position != this->position){
	    this->widget->set_content(to_string(this->position));
	}
    }
    
protected:

    WidgetData* widget;

    int32_t screen_width;

    MOUSE_POSITION_1D position;

};

    } // namespace uiservices
} // namespace iwear


#endif // __MOUSEMOVEMENTTRACKER_H


