Esempio n. 1
0
Boolean MultiCartDialog (void)
{
	OSStatus	err;
	IBNibRef	nibRef;

	multiCartDragHilite = -1;
	multiCartDialogResult = false;

	err = CreateNibReference(kMacS9XCFString, &nibRef);
	if (err == noErr)
	{
		WindowRef	window;

		err = CreateWindowFromNib(nibRef, CFSTR("MultiCart"), &window);
		if (err == noErr)
		{
			static int	index[2] = { 0, 1 };

			EventHandlerRef	wRef, cRef[2];
			EventHandlerUPP	wUPP, cUPP[2];
			EventTypeSpec	wEvent[] = { { kEventClassCommand, kEventCommandProcess      },
										 { kEventClassCommand, kEventCommandUpdateStatus } },
							cEvent[] = { { kEventClassControl, kEventControlDraw         },
										 { kEventClassControl, kEventControlDragEnter    },
										 { kEventClassControl, kEventControlDragWithin   },
										 { kEventClassControl, kEventControlDragLeave    },
										 { kEventClassControl, kEventControlDragReceive  } };
			HIViewRef		ctl, root, pane[2];
			HIViewID		cid;

			root = HIViewGetRoot(window);

			wUPP = NewEventHandlerUPP(MultiCartEventHandler);
			err = InstallWindowEventHandler(window, wUPP, GetEventTypeCount(wEvent), wEvent, (void *) window, &wRef);
			err = SetAutomaticControlDragTrackingEnabledForWindow(window, true);

			for (int i = 0; i < 2; i++)
			{
				cid.id = i;

				cid.signature = 'MPan';
				HIViewFindByID(root, cid, &pane[i]);
				cUPP[i] = NewEventHandlerUPP(MultiCartPaneEventHandler);
				err = InstallControlEventHandler(pane[i], cUPP[i], GetEventTypeCount(cEvent), cEvent, (void *) &index[i], &cRef[i]);
				err = SetControlDragTrackingEnabled(pane[i], true);

				cid.signature = 'MNAM';
				HIViewFindByID(root, cid, &ctl);
				SetStaticTextTrunc(ctl, truncEnd, false);
				if (multiCartPath[i])
				{
					CFStringRef	str;
					CFURLRef	url;

					url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, multiCartPath[i], kCFURLPOSIXPathStyle, false);
					str = CFURLCopyLastPathComponent(url);
					SetStaticTextCFString(ctl, str, false);
					CFRelease(str);
					CFRelease(url);
				}
				else
					SetStaticTextCFString(ctl, CFSTR(""), false);
			}

			MoveWindowPosition(window, kWindowMultiCart, false);
			ShowWindow(window);
			err = RunAppModalLoopForWindow(window);
			HideWindow(window);
			SaveWindowPosition(window, kWindowMultiCart);

			for (int i = 0; i < 2; i++)
			{
				err = RemoveEventHandler(cRef[i]);
				DisposeEventHandlerUPP(cUPP[i]);
			}

			err = RemoveEventHandler(wRef);
			DisposeEventHandlerUPP(wUPP);

			CFRelease(window);
		}

		DisposeNibReference(nibRef);
	}

	return (multiCartDialogResult);
}
/*****************************************************
*
* Do_NewWindow() 
*
* Purpose:  called to create a new window that has been constructed with Interface Builder
*
* Inputs:   none
*
* Returns:  OSStatus			- error code (0 == no error) 
*/
OSStatus Do_NewWindow(void)
	{
	OSStatus status;
	static IBNibRef gIBNibRef = NULL;
	WindowRef aWindowRef = NULL;
	CFStringRef theTitle = NULL;
	CFMutableStringRef theNewTitle = NULL;
	
	if (gIBNibRef == NULL)
		{
		// Create a Nib reference passing the name of the nib file (without the .nib extension)
		// CreateNibReference only searches into the application bundle.
		status = CreateNibReference(CFSTR("main"), &gIBNibRef);
		require_noerr(status, CantGetNibRef);
		}
	require(gIBNibRef != NULL, CantGetNibRef);
	
	// Create a window. "MainWindow" is the name of the window object. This name is set in 
	// InterfaceBuilder when the nib is created.
	status = CreateWindowFromNib(gIBNibRef, CFSTR("MainWindow"), &aWindowRef);
	require_noerr(status, CantCreateWindow);
	require(NULL != aWindowRef, CantCreateWindow);
	
	// Grab the title of the window and add the window count to it
	status = CopyWindowTitleAsCFString(aWindowRef, &theTitle);
	require_noerr(status, CantGetSetTitle);
	
	theNewTitle = CFStringCreateMutableCopy(NULL, 0, theTitle);
	require(NULL != theNewTitle, CantGetSetTitle);
	
	CFStringAppendFormat(theNewTitle, NULL, CFSTR(" %ld"), ++gWindowCount);
	status = SetWindowTitleWithCFString(aWindowRef, theNewTitle);
	require_noerr(status, CantGetSetTitle);
	
	// Create the custom view to be tested and embed it in our group box control 
	HIViewRef groupBox;
	status = HIViewFindByID(HIViewGetRoot(aWindowRef), kGroupBoxID, &groupBox);
	require_noerr(status, CantFindGroupBox);
	require(groupBox != NULL, CantFindGroupBox);
	
	HIViewRef customView;
	status = HICreateCustomView(NULL, &customView);
	require_noerr(status, CantCreateCustom);
	require(customView != NULL, CantCreateCustom);
	
	HIRect groupBoxBounds;
	HIViewGetBounds(groupBox, &groupBoxBounds);
	groupBoxBounds.origin.x += 20;
	groupBoxBounds.origin.y += 34;
	groupBoxBounds.size.width -= 40;
	groupBoxBounds.size.height -= 54;
	HIViewSetFrame(customView, &groupBoxBounds);
	
	HIViewSetLayoutInfo(customView, &kBindToParentLayout);
	
	status = HIViewAddSubview(groupBox, customView);
	require_noerr(status, CantAddSubview);

	HIViewSetVisible(customView, true);
	
	// Let's react to User's commands.
	EventTypeSpec eventTypeCP = {kEventClassCommand, kEventCommandProcess};
	status = InstallWindowEventHandler(aWindowRef, Handle_WindowCommandProcess, 1, &eventTypeCP, (void *)customView, NULL);
	require_noerr(status, CantInstallEventHandler);
	
	// Let's update our static text field whenever the value or hilite of our tested custom view changes
	EventTypeSpec eventTypeCVFC[] =
		{
			{kEventClassControl, kEventControlValueFieldChanged},
			{kEventClassControl, kEventControlHiliteChanged}
		};
	status = InstallControlEventHandler(customView, Handle_ControlValueFieldOrHiliteChanged, 2, eventTypeCVFC, (void *)customView, NULL);
	require_noerr(status, CantInstallEventHandler);

	// We accept only numbers in our Edit text control
	HIViewRef editText;
	HIViewFindByID(HIViewGetRoot(GetControlOwner(customView)), kSetValueEditID, &editText);

#ifdef MAC_OS_X_VERSION_10_4
	// in Tiger, only 1 event handler is necessary for the key filtering
	EventTypeSpec eventTypeTSCIR = {kEventClassTextField, kEventTextShouldChangeInRange};
	status = InstallControlEventHandler(editText, Handle_TextShouldChangeInRange, 1, &eventTypeTSCIR, (void *)editText, NULL);
	require_noerr(status, CantInstallEventHandler);
#else
	// pre-Tiger, we need a different event handler and a validation proc to handle pastes and drops.
	ControlEditTextValidationUPP textValidation = MyValidationProc;
	SetControlData(editText, kControlEntireControl, kControlEditTextValidationProcTag, sizeof(textValidation), &textValidation);

	EventTypeSpec eventTypeTIUFKE = {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent};
	status = InstallControlEventHandler(editText, Handle_TextInputEvent, 1, &eventTypeTIUFKE, (void *)editText, NULL);
	require_noerr(status, CantInstallEventHandler);
#endif

	// Finishing the custom view setup
	SetControl32BitMinimum(customView, 0);
	SetControl32BitMaximum(customView, 36);
	SetControl32BitValue(customView, 2);

	// We want window and thus our custom view to be able to respond to drops
	status = SetAutomaticControlDragTrackingEnabledForWindow(aWindowRef, true);
	require_noerr(status, SetAutomaticControlDragTrackingEnabledForWindow);

	ShowWindow(aWindowRef);
	SetWindowModified(aWindowRef, false);

SetAutomaticControlDragTrackingEnabledForWindow:
SetControlDragTrackingEnabled:
CantAddSubview:
CantCreateCustom:
CantFindGroupBox:
CantInstallEventHandler:
CantGetSetTitle:
CantAllocateWindowData:
CantCreateWindow:
CantGetNibRef:

	if (theTitle != NULL) CFRelease(theTitle);
	if (theNewTitle != NULL) CFRelease(theNewTitle);
	
	return status;
	}   // Do_NewWindow