📄 hello.c
字号:
#include <Pilot.h>
#include "hello.h"
// draws | or - depending on the data in the gadget
static void GadgetDraw(FormPtr frm, Word gadgetID)
{
RectangleType bounds;
UInt fromx, fromy, tox, toy;
Word gadgetIndex=FrmGetObjectIndex(frm, gadgetID);
VoidHand data=FrmGetGadgetData(frm, gadgetIndex);
if (data) {
WordPtr wordP=MemHandleLock(data);
FrmGetObjectBounds(frm, gadgetIndex, &bounds);
switch (*wordP) {
case 0:
fromx = bounds.topLeft.x+bounds.extent.x /2;
fromy = bounds.topLeft.y;
tox=fromx;
toy=fromy + bounds.extent.y - 1;
break;
case 1:
fromx = bounds.topLeft.x;
fromy = bounds.topLeft.y+bounds.extent.y /2;
tox=fromx+bounds.extent.x -1;
toy=fromy ;
break;
default:
fromx=tox=bounds.topLeft.x;
fromy=toy=bounds.topLeft.y;
break;
}
MemHandleUnlock(data);
WinEraseRectangle(&bounds, 0);
WinDrawLine(fromx,fromy,tox,toy);
}
}
// it'll work like a button: Invert when you tap in it.
// Stay inverted while you stay in the button. Leave the button, uninvert,
// let go outside,nothing happens; let go inside, data changes/redraws
static void GadgetTap(FormPtr frm, Word gadgetID, EventPtr event)
{
Word gadgetIndex = FrmGetObjectIndex(frm, gadgetID);
VoidHand data = FrmGetGadgetData(frm, gadgetIndex);
SWord x,y;
Boolean penDown;
RectangleType bounds;
Boolean wasInBounds = true;
if (data)
{
FrmGetObjectBounds(frm, gadgetIndex, &bounds);
WinInvertRectangle(&bounds, 0);
do
{
Boolean nowInBounds;
PenGetPoint (&x, &y, &penDown);
nowInBounds = RctPtInRectangle(x, y, &bounds);
if (nowInBounds !=wasInBounds)
{
WinInvertRectangle(&bounds, 0);
wasInBounds = nowInBounds;
}
} while (penDown);
if (wasInBounds)
{
WordPtr wPtr = MemHandleLock(data);
*wPtr = !(*wPtr);
MemHandleUnlock(data);
// GadgetDraw will erase--we don't need to invert
GadgetDraw(frm, gadgetID);
} // else gadget is already uninverted
}
}
static Boolean HelloFormEventHandler(EventPtr event)
{
Boolean handled = false;
switch (event->eType) {
case ctlSelectEvent:
FrmCustomAlert(cmdButton, "aa", "bb", NULL);
handled=true;
break;
case frmOpenEvent:
FrmDrawForm(FrmGetActiveForm());
handled = true;
break;
case menuEvent:
if(event->data.menu.itemID==cmdMenu_1)
SndPlaySystemSound(sndInfo);
else
SndPlaySystemSound(sndStartUp);
handled=true;
break;
case penDownEvent:
{
FormPtr frm=FrmGetActiveForm();
Word gadgetIndex = FrmGetObjectIndex(frm, cmdGadget);
RectangleType bounds;
FrmGetObjectBounds(frm, gadgetIndex, &bounds);
if (RctPtInRectangle (event->screenX, event->screenY, &bounds)) {
GadgetTap(frm, cmdGadget, event);
handled = true;
}
}
break;
}
return (handled);
}
static Boolean ApplicationHandleEvent(EventPtr event)
{
FormPtr form;
VoidHand h;
Word formID;
Boolean handled = false;
if (event->eType == frmLoadEvent) {
formID = event->data.frmLoad.formID;
form = FrmInitForm(formID);
FrmSetActiveForm(form);
switch (formID) {
case formID_hello:
form=FrmGetActiveForm();
h=MemHandleNew(sizeof(Word));
if (h) {
* (Word *) MemHandleLock(h)=1;
MemHandleUnlock(h);
FrmSetGadgetData(form, FrmGetObjectIndex(form, cmdGadget),h);
}
// Draw the form.
FrmDrawForm(form);
GadgetDraw(form, cmdGadget);
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)
{
VoidHand h;
FormPtr frm=FrmGetActiveForm();
h=FrmGetGadgetData(frm, FrmGetObjectIndex(frm, cmdGadget));
if (h)
MemHandleFree(h);
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 + -