⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datepref.c

📁 我的Palm OS 5 SDK zhCN_PIMApps代码。 使用codewarrior 开发环境
💻 C
📖 第 1 页 / 共 2 页
字号:
	// Format the time into a string.
	TimeToAscii (hour, 0, TimeFormat, str);
	len = StrLen (str);
	
	// Draw a frame around the time.
	WinDrawRectangleFrame (simpleFrame, &r);
	
	WinEraseRectangle (&r, 0);
	
	// Draw the time.
	curFont = FntSetFont (boldFont);
	x = r.topLeft.x + (r.extent.x - FntCharsWidth (str, len)) - 3;
	y = r.topLeft.y + ((r.extent.y - FntLineHeight ()) / 2);
	WinDrawChars (str, len, x, y);
	FntSetFont (curFont);
}


/***********************************************************************
 *
 * FUNCTION:    PreferencesApply
 *
 * DESCRIPTION: This routine applies the changes made in the Preferences Dialog.
 *
 * PARAMETERS:  nothing
 *
 * RETURNED:    nothing
 *
 * REVISION HISTORY:
 *			Name	Date		Description
 *			----	----		-----------
 *			art	8/3/95	Initial Revision
 *			vmk	12/9/97	Set alarm sound and repeat/interval to Datebook globals
 *			vmk	12/9/97	Added a call to save app's preferences
 *
 ***********************************************************************/
static UInt16 PreferencesApply (UInt16 dayStartHour, UInt16 dayEndHour)
{	
	UInt16		updateCode = 0;
	ListPtr		lst;
	FieldPtr 	fld;
	ControlPtr	ctl;
	Boolean		updateAlarms;
		
	if ((dayStartHour != DayStartHour) || (dayEndHour != DayEndHour))
		{
		DayStartHour = dayStartHour;
		DayEndHour = dayEndHour;
		updateCode = updateDisplayOptsChanged;
		}

	// Get the alarm preset settings.
	ctl = GetObjectPtr (PreferAlarmCheckbox);
	if (CtlGetValue (ctl))
		{
		fld = GetObjectPtr (PreferAlarmField);
		AlarmPreset.advance = StrAToI (FldGetTextPtr (fld));

		lst = GetObjectPtr (PreferAlarmList);		
		AlarmPreset.advanceUnit = (AlarmUnitType) LstGetSelection (lst);
		}
	else
		AlarmPreset.advance = -1;			// no alarm is set

	
	// If the sound, or nag information is changed, all alarms posted
	// to the attention manager will need to be udpated after the prefs
	// are saved.
	updateAlarms = ((AlarmSoundRepeatCount != PrefSoundRepeatCount) ||
		 			(AlarmSoundRepeatInterval != PrefSoundRepeatInterval) ||
		 			(AlarmSoundUniqueRecID != PrefSoundUniqueRecID) );

	// Get the alarm sound and interval settings
	AlarmSoundRepeatCount = PrefSoundRepeatCount;
	AlarmSoundRepeatInterval = PrefSoundRepeatInterval;
	AlarmSoundUniqueRecID = PrefSoundUniqueRecID;
	
	// Save app's preferences -- this is needed in case alarm settings
	// have been changed and an alarm occurs before we leave the app, since
	// the alarm notification handlers cannot rely on globals and always get
	// fresh settings from app's preferences.
	DatebookSavePrefs ();
	
	if (updateAlarms)
		UpdatePostedAlarms(SoundRepeatChanged);

	return (updateCode);
}


/***********************************************************************
 *
 * FUNCTION:    PreferencesInit
 *
 * DESCRIPTION: This routine initializes the Preferences Dialog.  
 *
 * PARAMETERS:  nothing
 *
 * RETURNED:    nothing
 *
 * REVISION HISTORY:
 *			Name		Date		Description
 *			----		----		-----------
 *			art		08/03/95	Initial Revision
 *			trev		08/14/97	Added MIDI support for alarms
 *       frigino	08/20/97	Modified sound name trigger initialization
 *			frigino	08/21/97	Removed typecasts in MapToPosition
 *			vmk		12/09/97	Check gMidiListH for null before locking
 *			kwk		07/07/99	Use default sound name str to find default entry.
 *
 ***********************************************************************/
static void PreferencesInit (void)
{
	FormPtr		formP;
	Char *		label;
	Char *		textP;
	MemHandle		textH;
	FieldPtr		fld;
	ControlPtr	ctl;
	ListPtr		listP;
	UInt16			item;
	UInt16			i;
	UInt16			fldIndex;
	SndMidiListItemType*	midiListP;
	Char *		defaultName;
	
	formP = FrmGetActiveForm ();

	// Set the alarm preset values.
	if (AlarmPreset.advance != apptNoAlarm)
		{
		// Turn the preset checkbox on.
		CtlSetValue (GetObjectPtr (PreferAlarmCheckbox), true);
		
		// Set the alarm advance value.
		fld = GetObjectPtr (PreferAlarmField);
		textH = MemHandleNew (maxAdvanceFieldLen);
		textP = MemHandleLock (textH);
		StrIToA (textP, AlarmPreset.advance);
		MemPtrUnlock (textP);
		FldSetTextHandle (fld, textH);
		fldIndex = FrmGetObjectIndex (formP, PreferAlarmField);
		FrmShowObject (formP, fldIndex);
		FrmSetFocus (formP, fldIndex);
	
		// Set the alarm advance unit of measure (minutes, hours, or days).
		listP = GetObjectPtr (PreferAlarmList);		
		LstSetSelection (listP, AlarmPreset.advanceUnit);
		label = LstGetSelectionText (listP, AlarmPreset.advanceUnit);

		ctl = GetObjectPtr (PreferAlarmUnitTrigger);
		CtlSetLabel (ctl, label);
		FrmShowObject (formP, FrmGetObjectIndex (formP, PreferAlarmUnitTrigger));
		}
	
	// Set the Remind Me trigger and list
	listP = FrmGetObjectPtr (formP, FrmGetObjectIndex (formP, PreferRemindMeList));

	//	Convert the preference setting to it's UI list position:
	item = MapToPosition (RepeatCountMappings, PrefSoundRepeatCount,
								 numRepeats, defaultRepeatsLevel);
	LstSetSelection (listP, item);
	CtlSetLabel (FrmGetObjectPtr (formP, FrmGetObjectIndex (formP, PreferRemindMeTrigger)),
											LstGetSelectionText (listP, item));

	// Set the Play Every trigger and list
	listP = FrmGetObjectPtr (formP, FrmGetObjectIndex (formP, PreferPlayEveryList));

	//	Convert the preference setting to it's UI list position:
	item = MapToPosition (RepeatIntervalMappings,
								PrefSoundRepeatInterval / minutesInSeconds,
								numRepeats, defaultPlayEveryLevel);
	LstSetSelection (listP, item);
	CtlSetLabel (FrmGetObjectPtr (formP, FrmGetObjectIndex (formP, PreferPlayEveryTrigger)),
											LstGetSelectionText (listP, item));
	
	listP = FrmGetObjectPtr(formP, FrmGetObjectIndex(formP, PreferAlarmSoundList));

	CreateMidiPickList(formP, FrmGetObjectIndex(formP, PreferAlarmSoundList), MidiPickListDrawItem);

	// Traverse MIDI pick list and find the item whose unique ID matches our
	// saved unique ID and use its index as our list selection index. If we
	// don't find a match, then we want to use the MIDI sound that corresponds
	// to our default sound name; if that's not found, use item 0.

	// Default to first sound in list
	item = 0;

	// Lock MIDI sound list
	if ( gMidiListH )
		{
		midiListP = MemHandleLock(gMidiListH);
		defaultName = MemHandleLock(DmGetResource(strRsc, defaultAlarmSoundNameID));

		// Iterate through each item and get its unique ID
		for (i = 0; i < gMidiCount; i++)
			{
			if (midiListP[i].uniqueRecID == PrefSoundUniqueRecID)
				{
				item = i;
				break;		// exit for loop
				}
			else if (StrCompare(midiListP[i].name, defaultName) == 0)
				{
				item = i;
				}
			}
		
		MemPtrUnlock(defaultName);
		
		// Set the list selection
		LstSetSelection (listP, item);

		// Init the sound trigger label
		// Create a new ptr to hold the label
		soundTriggerLabelP = MemPtrNew(soundTriggerLabelLen);
		// Check for mem failure
		ErrFatalDisplayIf(soundTriggerLabelP == NULL, "Out of memory");
		// Set the trigger label
		SetSoundLabel(formP, midiListP[item].name);

		// Unlock MIDI sound list
		MemPtrUnlock(midiListP);
		}

	PreferencesUpdateScrollers ();
}


/***********************************************************************
 *
 * FUNCTION:    SetSoundLabel
 *
 * DESCRIPTION: Sets the sound trigger label, using truncation
 *
 * PARAMETERS:  formP - the form ptr
 *              labelP - ptr to original label text
 *
 * RETURNED:    nothing
 *
 * REVISION HISTORY:
 *			Name		Date		Description
 *			----		----		-----------
 *			frigino	8/18/97	Initial Revision
 *
 ***********************************************************************/

static void SetSoundLabel(FormPtr formP, const char* labelP)
{
	ControlPtr	triggerP;
	UInt16			triggerIdx;

	// Copy the label, winUp to the max into the ptr
	StrNCopy(soundTriggerLabelP, labelP, soundTriggerLabelLen);
	// Terminate string at max len
	soundTriggerLabelP[soundTriggerLabelLen - 1] = '\0';
	// Get trigger idx
	triggerIdx = FrmGetObjectIndex(formP, PreferAlarmSoundTrigger);
	// Get trigger control ptr
	triggerP = FrmGetObjectPtr(formP, triggerIdx);
	// Use category routines to truncate it
	CategoryTruncateName(soundTriggerLabelP, ResLoadConstant(soundTriggerLabelWidth));
	// Set the label
	CtlSetLabel(triggerP, soundTriggerLabelP);
}


/***********************************************************************
 *
 * FUNCTION:    PreferencesHandleEvent
 *
 * DESCRIPTION: This routine is the event handler for the "Preferences
 *              Dialog Box".
 *
 * PARAMETERS:  event  - a pointer to an EventType structure
 *
 * RETURNED:    true if the event was handled and should not be passed
 *              to a higher level handler.
 *
 * REVISION HISTORY:
 *			Name		Date		Description
 *			----		----		-----------
 *			art		8/3/95	Initial Revision
 *			trev		8/14/97	Added MIDI support for alarms
 *			frigino	9/9/97	Added PlayAlarmSound call when selecting new
 *									alarm sound from popup list
 *			vmk		12/9/97	Added initialization of alarm prefs in frmOpenEvent
 *			gap		10/15/99	Added added handling of frmUpdateEvent
 *
 ***********************************************************************/
Boolean PreferencesHandleEvent (EventType * event)
{
	UInt16 updateCode;
	FormPtr frm = 0;
	Boolean handled = false;
	SndMidiListItemType*	listP;
	UInt16 item;

	if (event->eType == ctlSelectEvent)
		{
		switch (event->data.ctlSelect.controlID)
			{
			case PreferOkButton:
				updateCode = PreferencesApply (PrefDayStartHour, PrefDayEndHour);
				FrmReturnToForm (0);
				if (updateCode)
					FrmUpdateForm (FrmGetFormId (FrmGetActiveForm()), updateCode);
				handled = true;
				// Free all data before we return to underlying form
				FreeAll();
				break;

			case PreferCancelButton:
				FrmReturnToForm (0);
				handled = true;
				// Free all data before we return to underlying form
				FreeAll();
				break;
			
			case PreferAlarmCheckbox:
				PreferencesAlarmPresetOnOff (event->data.ctlSelect.on);
				handled = true;
				break;
			}
		}

	else if (event->eType == ctlRepeatEvent)
		{
		switch (event->data.ctlRepeat.controlID)
			{
			case PreferStartDownButton:
				PrefDayStartHour--;
				PreferencesDrawTime (PrefDayStartHour, true);
				break;

			case PreferStartUpButton:
				PrefDayStartHour++;
				PreferencesDrawTime (PrefDayStartHour, true);
				if (PrefDayEndHour < PrefDayStartHour)
					{
					PrefDayEndHour = PrefDayStartHour;
					PreferencesDrawTime (PrefDayEndHour, false);
					}
				break;

			case PreferEndDownButton:
				PrefDayEndHour--;
				if (PrefDayEndHour < PrefDayStartHour)
					{
					PrefDayStartHour = PrefDayEndHour;
					PreferencesDrawTime (PrefDayStartHour, true);
					}
				PreferencesDrawTime (PrefDayEndHour, false);				
				break;

			case PreferEndUpButton:
				PrefDayEndHour++;
				PreferencesDrawTime (PrefDayEndHour, false);
				break;
			}
		PreferencesUpdateScrollers ();
		}

	else if ((event->eType == keyDownEvent)
	&& (!EvtKeydownIsVirtual(event)))
		{
		WChar chr = event->data.keyDown.chr;
		if (TxtCharIsDigit (chr) || TxtCharIsCntrl (chr))
			{
			// Redirect numeric input to the Alarm Preset input field
			FldHandleEvent (GetObjectPtr (PreferAlarmField), event);
			}
		handled = true;
		}

	else if (event->eType == frmOpenEvent)
		{
		PrefDayStartHour = DayStartHour;
		PrefDayEndHour = DayEndHour;
		PrefSoundRepeatCount = AlarmSoundRepeatCount;
		PrefSoundRepeatInterval = AlarmSoundRepeatInterval;
		PrefSoundUniqueRecID = AlarmSoundUniqueRecID;

		frm = FrmGetActiveForm ();
		PreferencesInit ();
		FrmDrawForm (frm);
		PreferencesDrawTime (DayStartHour, true);
		PreferencesDrawTime (DayEndHour, false);

		handled = true;
		}
		
	else if (event->eType == frmUpdateEvent)
		{
		frm = FrmGetActiveForm ();
		FrmDrawForm (frm);
		PreferencesDrawTime (DayStartHour, true);
		PreferencesDrawTime (DayEndHour, false);
		handled = true;
		}
		
	else if (event->eType == popSelectEvent)
		{
		switch (event->data.popSelect.listID)
			{
			case PreferRemindMeList:
				item = event->data.popSelect.selection;
				
				PrefSoundRepeatCount = RepeatCountMappings[item];
				
				break;
			case PreferPlayEveryList:
				item = event->data.popSelect.selection;
				
				PrefSoundRepeatInterval = RepeatIntervalMappings[item] * minutesInSeconds;
				
				break;	
			case PreferAlarmSoundList:
				// Get new selected item
				item = event->data.popSelect.selection;
				// Get active form
				frm = FrmGetActiveForm ();
				// Lock MIDI list
				listP = MemHandleLock(gMidiListH);
				// Save alarm sound unique rec ID
				PrefSoundUniqueRecID = listP[item].uniqueRecID;
				// Erase control
				CtlEraseControl (event->data.popSelect.controlP);
				// Set new trigger label
				SetSoundLabel(frm, listP[item].name);
				// Redraw control
				CtlDrawControl (event->data.popSelect.controlP);
				// Unlock MIDI list
				MemPtrUnlock(listP);
				// Play new alarm sound
				PlayAlarmSound (PrefSoundUniqueRecID);
				// Mark event as handled
				handled = true;
				break;
			}
		}	

	else if (event->eType == frmCloseEvent)
		{
		// Free all data before we return to underlying form
		FreeAll();
		}

	return (handled);
}


/***********************************************************************
 *
 * FUNCTION:    FreeAll
 *
 * DESCRIPTION: Frees all data allocated for the duration of the dialog
 *
 * PARAMETERS:  none
 *
 * RETURNED:    none
 *
 * REVISION HISTORY:
 *			Name		Date		Description
 *			----		----		-----------
 *			frigino	8/20/97	Initial Revision
 *			vmk		12/9/97	Check soundTriggerLabelP for null before deleting
 *
 ***********************************************************************/
static void FreeAll(void)
{
	// Free the MIDI pick list
	FreeMidiPickList();

	// Free the sound trigger label placeholder
	if ( soundTriggerLabelP )
		{
		MemPtrFree(soundTriggerLabelP);
		soundTriggerLabelP = NULL;
		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -