//	Zinc Application Framework - M_CURSOR.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#include <z_cursor.hpp>
#include <z_scrdsp.hpp>
#include <z_string.hpp> // For strcmp().
#define ZAF_CURSOR_INFO
#include "gbl_def.cpp"

// Warning: Cursor functions in Motif are applied at the widget level, not
// through the event manager.  Therefore, cursor state functions in Motif 
// only work for the current widget and are lost once focus changes to 
// another object.

// ----- ZafCursor ----------------------------------------------------------

ZafCursor::ZafCursor(ZafDeviceState _state, ZafDeviceImage _imageType) :
	ZafDevice(E_CURSOR, _state), imageType(_imageType)
{
	// Check the image information.
	ImageAllocate();
	SetStringID(className);
	SetNumberID((ZafNumberID)classID);

	// Initialize the cursor information.
	position.column = position.line = 0;
	offset.column = offset.line = 0;
	installed = true;
}

ZafCursor::~ZafCursor(void)
{
	// Check the image information.
	ImageFree();
}

ZafEventType ZafCursor::Event(const ZafEventStruct &event)
{
	// Switch on the event type.
	ZafEventType ccode = event.type;
	switch (ccode)
	{
	case D_OFF:
	case D_ON:
		// Change the cursor state for the current object.
		{
		deviceState = ccode;
		Window window;
		int revert;
		XGetInputFocus(((ZafScreenDisplay *)display)->xDisplay, &window, &revert);
		Widget widget = window ? XtWindowToWidget(((ZafScreenDisplay *)display)->xDisplay, window) : 0;
		if (widget)
			XtVaSetValues(widget, XmNcursorPositionVisible, deviceState == D_ON, 0);
		}
		break;

	case D_STATE:
		return (deviceState);

	case DC_INSERT:
	case DC_OVERSTRIKE:
		SetImageType(ccode);
		break;
	}

	// Return the ccode.
	return (ccode);
}

void ZafCursor::ImageAllocate(void)
{
	// No cursor images need to be allocated.
}

void ZafCursor::ImageFree(bool )
{
	// No cursor images need to be freed.
}

bool ZafCursor::IsA(ZafClassID compareID) const
{
	return ((compareID == classID) ? true : ZafDevice::IsA(compareID));
}

bool ZafCursor::IsA(ZafClassName compareName) const
{
	return (!strcmp(compareName, className) ? true : ZafDevice::IsA(compareName));
}

void ZafCursor::Poll(void)
{
	// Motif does not allow cursor polling.
}

int ZafCursor::SetBlinkRate(int tBlinkRate)
{
	// Set the blink rate for the current object.
	blinkRate = tBlinkRate;
	Window window;
	int revert;
	XGetInputFocus(((ZafScreenDisplay *)display)->xDisplay, &window, &revert);
	Widget widget = window ? XtWindowToWidget(((ZafScreenDisplay *)display)->xDisplay, window) : 0;
	if (widget)
		XtVaSetValues(widget, XmNblinkRate, blinkRate, 0);
	return (blinkRate);
}

ZafDeviceImage ZafCursor::SetImageType(ZafDeviceImage tImageType)
{
	// Motif does not allow the user to change the cursor image
	// from the cursor device.
	imageType = tImageType;
	return (imageType);
}

