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

📄 hello.c

📁 CD_PALM程序设计入门
💻 C
字号:
#include <Pilot.h>
#include "hello.h"



static FieldPtr SetFieldTextFromHandle(Word fieldID, Handle txtH)
{
    Handle          oldTxtH;
    FormPtr         frm=FrmGetActiveForm();
    FieldPtr        fldP;
    

    // get the field and the field's current text handle.
    fldP =FrmGetObjectPtr(frm,FrmGetObjectIndex(frm,fieldID));
    ErrNonFatalDisplayIf(!fldP, "missing field");

     oldTxtH = FldGetTextHandle(fldP);

    // set the field's text to the new text.
    FldSetTextHandle(fldP, txtH);
    FldDrawField(fldP);

    // free the handle AFTER we call FldSetTextHandle().
    if (oldTxtH)
           MemHandleFree( (VoidHand) oldTxtH );

    return fldP;

}

// Allocates new handle and copies incoming string
static FieldPtr SetFieldTextFromStr(Word fieldID, CharPtr strP)
{
    Handle      txtH;

    // get some space in which to stash the string
    txtH = (Handle) MemHandleNew(StrLen(strP)+1);
    if (!txtH)
       return NULL;

    // copy the string to the locked handle.
    StrCopy(MemHandleLock( (VoidHand) txtH),strP);

    // unlock the string handle.
    MemHandleUnlock( (VoidHand) txtH);

    // set the field to the handle
    return SetFieldTextFromHandle(fieldID, txtH);
}

static void ClearFieldText(Word fieldID)
{
        SetFieldTextFromHandle(fieldID, NULL);
}



// Updating the scrollbar based on the insertion point
static void UpdateScrollbar (void)
{
    FormPtr      frm = FrmGetActiveForm();
    ScrollBarPtr scroll;
    FieldPtr     field;
    Word         currentPosition;
    Word         textHeight;
    Word         fieldHeight;
    Word         maxValue;

    field = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm,cmdField));
    FldGetScrollValues(field,&currentPosition,&textHeight,&fieldHeight);

    // if the field is 3 lines, and the text height is 4 lines
    // then we can scroll so that the first line is at the top
    // (scroll position 0) or so the second line is at the top
    // (scroll position 1). These two values are enough to see
    // the entire text.
    if (textHeight > fieldHeight)
    {
        maxValue=textHeight-fieldHeight;
//        SetFieldTextFromStr(showField, "111");
        SndPlaySystemSound(sndInfo);
    }
    else if (currentPosition)
    {
        maxValue=currentPosition;
//        SetFieldTextFromStr(showField, "222");
        SndPlaySystemSound(sndInfo);
    }
    else
    {
        SndPlaySystemSound(sndInfo);
//        SetFieldTextFromStr(showField, "333");
        currentPosition=0;
        maxValue=1 ;  // if 0, no scrollbar
    }
    
 
    scroll=FrmGetObjectPtr(frm,FrmGetObjectIndex(frm,cmdScrollbar));

    // on a page scroll, want to overlap by one line (to provide context)
    SclSetScrollBar(scroll, currentPosition , 0, maxValue, fieldHeight-1);

}

static void MainViewInit(void)
{
    UpdateScrollbar();
    // Draw the form.
    FrmDrawForm(FrmGetActiveForm());
}

static void ScrollLines(int numLinesToScroll, Boolean redraw)
{
    FormPtr       frm=FrmGetActiveForm();
    FieldPtr      field;
    CharPtr       s;

    field = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm,cmdField));
    if (numLinesToScroll < 0)
          FldScrollField(field, -numLinesToScroll, up);
    else
          FldScrollField(field, numLinesToScroll, down);

    // if there are blank lines at the end and we scroll up, FldScrollField
    // makes the blank lines disappear. Therefore, we've got to update
    // the scrollbar
     
    if ((FldGetNumberOfBlankLines(field) && numLinesToScroll < 0 ) || redraw)
    {
       UpdateScrollbar();
    }    
}

static void PageScroll(DirectionType direction)
{
    FormPtr       frm=FrmGetActiveForm();
    FieldPtr      field;

    field=FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, cmdField));
    if (FldScrollable(field, direction))
    {
       int linesToScroll = FldGetVisibleLines(field)-1;
       if (direction==up)
          linesToScroll = -linesToScroll;
       ScrollLines(linesToScroll,true);
    }
}


static Boolean HelloFormEventHandler(EventPtr event)
{
	Boolean handled = false;

	switch (event->eType) {

      case ctlSelectEvent:
        FrmCustomAlert(cmdButton, "aa", "bb", NULL);
        handled=true;
        break;

		case frmOpenEvent:
         MainViewInit();
			// FrmDrawForm(FrmGetActiveForm());
			handled = true;
			break;

      case menuEvent:
         if(event->data.menu.itemID==cmdMenu_1)
             SndPlaySystemSound(sndInfo);
         else
             SndPlaySystemSound(sndStartUp);
         handled=true;
         break;

      // case sclExitEvent:
         // MainViewInit();
         // handled =true;
         // break;
      case sclRepeatEvent:
         ScrollLines(event->data.sclRepeat.newValue -
                          event->data.sclRepeat.value, false);
         break;


      case fldChangedEvent:
         UpdateScrollbar();
         handled=true;
         break;


      case ctlRepeatEvent:
  //       SetFieldTextFromStr(showField, "aaabbb");
         if (event->data.ctlRepeat.controlID == UpRepeatButton)
         {
            PageScroll(up);
  //          SetFieldTextFromStr(showField, "aaa");
            handled=true;
         } 
         else if (event->data.ctlRepeat.controlID == DownRepeatButton)
         {
            PageScroll(down);
 //           SetFieldTextFromStr(showField, "bbb");
            handled=true;
         }
         break;

	}

	return (handled);
}

static Boolean ApplicationHandleEvent(EventPtr event)
{
	FormPtr form;
	Word    formID;
	Boolean handled = false;

	if (event->eType == frmLoadEvent) {
		formID = event->data.frmLoad.formID;
		form   = FrmInitForm(formID);

		FrmSetActiveForm(form);

		switch (formID)  {
			case formID_hello:
				FrmSetEventHandler(form, HelloFormEventHandler);
				break;
		}

		handled = true;
	}

	return (handled);
}

static void StartApplication()
{
	FrmGotoForm(formID_hello);
}

static void EventLoop(void)
{
	short			err;
	int			formID;
	FormPtr		form;
	EventType		event;

	do {
		EvtGetEvent(&event, evtWaitForever);

		if (SysHandleEvent(&event)) continue;

		if (MenuHandleEvent(NULL, &event, &err))	continue;

		if (ApplicationHandleEvent(&event)) continue;

		FrmDispatchEvent(&event);

	} while (event.eType != appStopEvent);
}

static void StopApplication(void)
{
	FrmCloseAllForms();
}

DWord PilotMain (Word cmd, Ptr cmdPBP, Word launchFlags)
{
	if (cmd ==  sysAppLaunchCmdNormalLaunch) {
		StartApplication();
		EventLoop();
		StopApplication();
	}

	return (0);
}

⌨️ 快捷键说明

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