REBOL.net

/*
 *  testwin.c - Test Active Window
 *
 *  Shows problem with SelectWindow not making the window active (the focus).
 *
 *  Created by Carl Sassenrath on Fri Apr 22 2005.
 *
 *  Compile with:
 *     gcc -o test testwin.c /system/library/frameworks/carbon.framework/carbon
 *
 *  Run from shell with:  (Use CTRL-C to quit)
 *     ./test
 *
 *  Concluding note, added Apr 27 2005:
 *
 *     We have given up. It does not appear that OSX allows this to happen
 *     without making a .app (directory). We think this is some kind of
 *     requirement connected to the DocumentWindowClass and how it obtains
 *     some of its default attributes and settings.
 */

#include 

main()
{
	WindowRef window;
	Rect bounds;
	WindowClass class = kDocumentWindowClass;
	WindowAttributes attributes = kWindowStandardDocumentAttributes;
	EventRef event;
	EventTargetRef wintarget;
	int str[2];

	printf("Testing... Click in window. Press CTRL-C to quit.\n");

	SetRect(&bounds, 100, 100, 500, 500);
	if (CreateNewWindow(class, attributes, &bounds, &window) != noErr) {
		printf("Window will not open\n");
		exit(0);
	}
	wintarget = GetWindowEventTarget(window);
	InstallStandardEventHandler(wintarget);

	ShowWindow(window);
	SelectWindow(window);
	
	// Line below can also be used... but has same problem.
	// RunApplicationEventLoop();

	str[1] = 0;
	while (true) {
		if (ReceiveNextEvent(0, NULL, kEventDurationSecond, true, &event) == noErr) {
			str[0] = GetEventClass(event);
			printf("Event: Class %s Kind %d\n", str, GetEventKind(event));
			// We omitted the SendEventToEventTarget() and ReleaseEvent()
			// calls, because they do not make a difference in the result.
			// That is, if you add them back, you still get no focus.
		} else
			printf("I'm alive.\n");
	}

}

Updated 14-Mar-2007, WIP Wiki, REBOL/Core 2.6.2.4.2   -   Copyright 2007 REBOL Technologies   -   WWW.REBOL.COM   -   Edit