#include "polytool.h" extern polygons **TP; // The polygons extern polyselect Sel; // selected vertex or midpoint static window *Wind; // The window // Saving to disk static bool SaveData(void) { // 0: user cancelled or failure; 1: success bool R; file F; R=saveas("Save File As","Untitled Polygons",'PYTL','TEXT',&F); if (!R) return 0; setpath(&F); writetextpolys(TP); closefile(&F); (*TP)->changed=0; return 1; } static void SaveSelection(void) { bool R; file F; if (!range(Sel.poly,(*TP)->num)) { beep(); return; } R=saveas("Save File As","Selection.txt",'ttxt','TEXT',&F); if (!R) return; setpath(&F); writetextpoly((*TP)->poly[Sel.poly]); closefile(&F); } static void DoTEXTDoc(file *F) { char *S,*T; bool R; polygon **P; if (!F) return; setpath(F); S=filetocharpointer(); R=parsetextpolys(S,TP); if (!R) { T=S; P=parsetextpoly(&T); if (P) insertpoly(TP,P); else error("File format error"); } old(S); closefile(F); redraw(Wind); } static void OpenTEXTDoc(FSRef *FSR) { file F; if (openfile(FSR/*,'TEXT'*/,&F)) DoTEXTDoc(&F); } static void OpenTEXTDialog(void) { file F; if (openexistingfile('TEXT',&F)) DoTEXTDoc(&F); } // Miscellaneous events static void DoGrow(window *W) { if (!W) return; resizeproj(W->proj,W->page); } static void DoQuit(window *W) { int R; if (!(*TP)->changed) quit(); R=0;//R=savechanges("Save Changes to Polygons?"); if (R==-1) return; if (R==1) { bool Q; Q=SaveData(); if (Q==0) return; } quit(); } // Menu stuff static Handle MenuBar; static void NamePolygon(polygons **Py, int POLY) { polygon **P; char *S; if (!Py || !*Py || !range(POLY,(*Py)->num)) return; P=(*Py)->poly[POLY]; S=copyof((*P)->name); modaltextentry("Name of Polygon:",&S); namepolygon(Py,POLY,S); old(S); } static void MenuInit(void) { OSStatus err; IBNibRef Nib; err=CreateNibReference(CFSTR("polytool"),&Nib); err=CreateMenuBarFromNib(Nib,CFSTR("polytool"),&MenuBar); DisposeNibReference(Nib); } static void AdjustMenus(window *W) { bool P; P=range(Sel.poly,(*TP)->num); allowitem(W,'expt',P); allowitem(W,'name',P); allowitem(W,'rvrs',P); allowitem(W,'clsd',P); checkitem(W,'clsd',P && !polyisopen(TP,Sel.poly)); allowitem(W,'focu',P); } static bool DoCommand(window *W, UInt32 C) { projection *P; switch (C) { case 'abou': beep(); return 1; case 'quit': quit(); return 1; case 'open': OpenTEXTDialog(); return 1; case 'svas': SaveData(); redraw(Wind); return 1; case 'expt': SaveSelection(); redraw(Wind); return 1; case 'name': NamePolygon(TP,Sel.poly); redraw(W); return 1; case 'rvrs': reversepoly(TP,Sel.poly,&(Sel.pt)); redraw(W); return 1; case 'clsd': changepolyopenness(TP,Sel.poly); redraw(W); return 1; case 'focu': if (!W) return 1; P=W->proj; if (!P) return 1; lookatpoly(P,TP,Sel.poly,Sel.pt); projtoscroll(W); redraw(W); return 1; } return 0; } // Functions that handle keystroke events static bool DoKey(keystroke *K) { if (projectionkey(K)) return 1; if (K->c==8 && Sel.poly>=0 && Sel.pt>=0 && K->wind) { removecorner(TP,Sel.poly,Sel.pt); redraw(K->wind); return 1; } return 0; } // Main event loop static void InitGUI(void) { initmacintosh(); initreferenceframe("PolyTool Polygon Editor"); installcore(&OpenTEXTDialog,NULL,&OpenTEXTDoc,NULL); Wind=standardwindow("Polygon Tool"); Wind->erase=0; Wind->proj=newprojection(0.0,0.0,0.0,200.0,Wind->page); MenuInit(); giveglobecontrols(Wind,0,15,0); scrolltoproj(Wind); installmenus(Wind,MenuBar,NULL,&AdjustMenus,&DoCommand); installpaint(Wind,&Display); installgrow(Wind,&DoGrow,NULL); installclick(Wind,&DoContent,&DoQuit,&Display); installkey(Wind,&DoKey); installwheel(Wind,&MapWheel); } int main(void) { InitGUI(); Sel.poly=-1; TP=newpolys(); revealwindow(Wind); RunApplicationEventLoop(); return 0; }