Window maximise/minimise


#1

Hi there,

I want our JUCE application to behave like other Mac applications, but there are some problems.

  1. When our application icon in Dock is clicked, its minimised window doesn’t expand.
  2. If its window is set to use OS native one, it doesn’t collapse on double click of its title bar.

JUCE Demo behaves samely.

To fix them, I added Carbon event handler for kEventMouseDown, kEventAppActivated and kEventAppShown to my application.
It seems to work OK, but aren’t there some better solutions?

Here is my dirty code:

void MyDocumentWindow::initializeMacEventHandler()
{
	const EventTypeSpec appEvents[] =
	{
		{ kEventClassApplication, kEventAppActivated },
		{ kEventClassApplication, kEventAppShown }
	};
	InstallApplicationEventHandler(NewEventHandlerUPP(macAppEventHandler),
		GetEventTypeCount(appEvents), appEvents, this, &macAppEventHandler);
	
	const EventTypeSpec windowEvents[] =
	{
		{ kEventClassMouse, kEventMouseDown }
	};
	InstallWindowEventHandler((WindowRef)getWindowHandle(), NewEventHandlerUPP(macWindowEventHandler),
		GetEventTypeCount(windowEvents), windowEvents, this, &macWindowEventHandler);
}

OSStatus MyDocumentWindow::macAppEventHandler(EventHandlerCallRef caller, EventRef event, void* userData)
{
	MyDocumentWindow* thisPtr = (MyDocumentWindow*)userData;
	if (GetEventClass(event))
	{
		if (GetEventKind(event) == kEventAppShown
			|| GetEventKind(event) == kEventAppActivated)
		{
			thisPtr->setMinimised(false);
		}
	}
	return eventNotHandledErr;
}

#undef Point

OSStatus MyDocumentWindow::macWindowEventHandler(EventHandlerCallRef caller, EventRef event, void* userData)
{
	MyDocumentWindow* thisPtr = (MyDocumentWindow*)userData;
	if (GetEventClass(event) == kEventClassMouse
		&& GetEventKind(event) == kEventMouseDown)
	{
		::Point where;
		GetEventParameter(event, kEventParamMouseLocation, typeQDPoint, 0, sizeof(::Point), 0, &where);
		int x = where.h;
		int y = where.v;
		thisPtr->globalPositionToRelative(x, y);
		UInt32 clickCount;
		GetEventParameter(event, kEventParamClickCount, typeUInt32, 0, sizeof(clickCount), 0, &clickCount);
		if (clickCount == 2 && y <= 0 && thisPtr->contains(x, 0))
		{
			thisPtr->setMinimised(true);
			return noErr;
		}
	}
	return eventNotHandledErr;
}