// input-test.cpp

#include <stdio.h>

#include <Application.h>
#include <Message.h>
#include <Window.h>
#include <View.h>

// hack: use inline friend of BMessage to access private members
inline
int32
_get_message_target_(BMessage *message)
{
	printf("fReplyTo: port: %ld, target: %ld, team: %ld, "
		   "preferred: %d\n", message->fReplyTo.port, message->fReplyTo.target,
		   message->fReplyTo.team, message->fReplyTo.preferred);
	printf("reply required: %d\n", message->fReplyRequired);
	printf("delivered: %d\n", message->WasDelivered());
	return 0;
}

class MyView : public BView {
public:
	MyView()
		: BView(BRect(0, 0, 400, 300), "test view", B_FOLLOW_ALL, 0)
	{
	}

	virtual void MouseDown(BPoint where)
	{
		PrintMessageSource("MouseDown");
		BView::MouseDown(where);
	}

	virtual void MouseUp(BPoint where)
	{
		PrintMessageSource("MouseUp");
		BView::MouseUp(where);
	}

	virtual void MouseMoved(BPoint where, uint32 code,
							const BMessage *message)
	{
		PrintMessageSource("MouseMoved");
		BView::MouseMoved(where, code, message);
	}

	virtual void KeyDown(const char *bytes, int32 numBytes)
	{
		PrintMessageSource("KeyDown");
		BView::KeyDown(bytes, numBytes);
	}

	virtual void KeyUp(const char *bytes, int32 numBytes)
	{
		PrintMessageSource("KeyUp");
		BView::KeyUp(bytes, numBytes);
	}

	void PrintMessageSource(const char *method)
	{
		BMessage *message = Window()->CurrentMessage();
		BMessenger sender(message->ReturnAddress());
		printf("%s():\n", method);
		_get_message_target_(message);
		printf("\n");
	}
};

// main
int
main()
{
	BApplication app("application/x-vnd.bonefish.input-msg-test");
	BWindow *window = new BWindow(BRect(100, 100, 500, 400),
								  "Input Message Test", B_TITLED_WINDOW,
								  B_QUIT_ON_WINDOW_CLOSE);
	MyView *view = new MyView;
	window->AddChild(view);
	window->Show();
	window->Lock();
	view->MakeFocus();
	window->Unlock();
	app.Run();
	return 0;
}

