#include "hush.h"
// The drawing_canvas responds to press, motion and release events
class drawing_canvas : public canvas {
public:
drawing_canvas( char* path ) : canvas(path) {
geometry(200,100);
bind(this); // become sensitive
dragging = 0;
}
void press( event& ) { dragging = 1; }
void motion( event& e) {
if (dragging) circle(e.x(),e.y(),1,"-fill black");
}
void release( event& ) { dragging = 0; }
protected:
int dragging;
};
// To initialize the application
class application : public session {
public:
application( int argc, char* argv[] )
: session(argc,argv,"draw") {}
void main() {
canvas* c = new drawing_canvas(".draw");
c->pack();
tk->pack(".quit");
}
};
int main (int argc, char* argv[]) {
session* s = new application(argc,argv);
return s->run();
}
class widget : public handler {
public:
...
void bind(class handler* h) { ... }
...
};
interface event {
int type(); // X event type
char* name(); // type as string
int x();
int y();
int button(int i = 0); // ButtonPress
int buttonup(int i = 0); // ButtonRelease
int motion(); // MotionNotify
int keyevent(); // KeyPress or KeyRelease
int buttonevent(int i = 0); // ButtonPress or ButtonRelease
int keycode();
void trace(); // prints event information
void* rawevent(); // delivers raw X event
};
interface handler {
virtual event* dispatch(event* e);
virtual int operator()();
virtual void press( event& ) { }
virtual void release( event& ) { }
virtual void keypress( event& ) { }
virtual void keyrelease( event& ) { }
virtual void motion( event& ) { }
virtual void enter( event& ) { }
virtual void leave( event& ) { }
virtual void other( event& ) { }
protected:
event* _event;
kit* tk;
};
event* handler::dispatch( event* e ) {
_event = e;
int res = this->operator()();
return (res != OK) ? _event : 0;
}
int handler::operator()() {
event& e = * _event; fetch event
if ( e.type() == ButtonPress ) press(e);
else if ( e.type() == ButtonRelease ) release(e);
else if ( e.type() == KeyPress ) keypress(e);
else if ( e.type() == KeyRelease ) keyrelease(e);
else if ( e.type() == MotionNotify ) motion(e);
else if ( e.type() == EnterNotify ) enter(e);
else if ( e.type() == LeaveNotify ) leave(e);
else other(e);
return OK;
}