From: dac@cbnewsf.cb.att.com (david.a.copperman)
Subject: IV code fragment
Keywords: InputHandler
Organization: AT&T
Date: Fri, 22 Jan 1993 13:58:48 GMT

Recently Paul Shannon (pshannon@nrao.edu) posted a program that he had
written as an InterViews learning tool.  I am climbing that learning tree
myself, and am finding it extremely slow going.  Paul's code has been
very helpful, so I thought I'd follow suit and post something (much smaller)
for discussion.

Coming out of the Xt world, I'm used to the generic event handler,
which can be applied to any widget and which carries, optionally, an
application-supplied value, providing context for the routine.  So I
began to investigate a way to create a class that could be generalized
for this.  (One person suggested an extension of the Callback macro,
as an alternative to what I'v come up with.)

The code below defines a class derived from InputHandler.  While the code
here is fairly trivial, you can see that this class can be readily designed
as a base for numerous other classes corresponding to different widgets
or whatever.  I wrote it to demonstrate to myself that I could write
event handlers that would have access to the Event class, which I need for a 
so I'll know which mouse button is in play, while still controlling widget
behavior where I deemed it important.  (For example, I want to control the
mapping/unmapping of all popups myself, and while they are visible, the
button that pops them up is pushed in, and is released only when they pop down.)

In this code I create a button using WidgetKit::push_button, and retain
the result pointer to be able to call Button::press.  I've been told it's
more intuitively reasonable to derive this from the Button class than
from InputHandler, but I've been unable to derive a Button that gives
me the same look and feel as WidgetKit::push_button and that also calls
Button::press successfully.

I hope this code fragment is interesting for some, and perhaps a little
provocative.  For any IV guru reading this - are you offended by this
buttonHandler class?  Have I violated some important precept in IV
programming conventions?  I'd be interested in any and all comments.

Dave Copperman
dac@hoqaa.att.com


#include <IV-look/kit.h>
#include <InterViews/background.h>
#include <InterViews/button.h>
#include <InterViews/event.h>
#include <InterViews/layout.h>
#include <InterViews/session.h>
#include <InterViews/window.h>
#include <stdio.h>

static WidgetKit* kit;
static Style* kitstyle;
static Window* win;

class buttonHandler : public InputHandler {
private:
	Button* _button;
public:
	buttonHandler :: buttonHandler (const char*);
	void press (const Event& event);
};

buttonHandler :: buttonHandler (const char* label) :
	InputHandler (_button = kit->push_button (label, nil), kitstyle)
{
}

/*
 * Toggle button position to show explicit control.
 */
void buttonHandler :: press (const Event& event)
{
	static int up = 1;

	if (event.pointer_button () != Event::left)
		return;
	if (up) {
		_button->press (event);
		up = 0;
	}
	else {
		_button->release (event);
		up = 1;
	}
}

main (int argc, char** argv) {
	Session* session = new Session ("Himom", argc, argv);
	kit = WidgetKit::instance();
	kitstyle = kit->style ();
	const LayoutKit& layout = *LayoutKit::instance();
	buttonHandler *handler = new buttonHandler ("  Push Me  ");
	win = new ApplicationWindow(
		kit->inset_frame (
		layout.margin (handler, 10.0)
	    )
	);

	session->run_window (win);
}