// twimage.c // // This file implements the Image windows, which pop up when an Info window is opened, if the // item has an associated image. // // by John D. de Boer #include "twist.h" #include "twindow.h" extern GlobalOpts Opt; extern void *Sel,*Was; int IMAGEWINDS; // the number of picture windows currently open window *ImageWind[MAXIMAGEWINDS]; // the picture windows //static string Title; // window title from resource // Drawing content static void DrawTheImage(window *W, ImageInfo *D, rect R) { point P; if (!W || !D || !D->pict) return; drawpicture(W,D->pict,R); P=middle(R); if (D->cap) textcentre(W,P.x,bottom(R) - 5 - Opt.size,D->cap); } static void DrawImage(window *W) { ImageInfo *D; rect R; if (!W) return; D=W->appdata; if (!D || !D->pict) return; R=picturerect(D->pict); alignrect(&R,0,top(W->page)); offsetrect(&R,-GetControlValue(W->hsb),+GetControlValue(W->vsb)); DrawTheImage(W,D,R); } static short ImagePages(window *W, rect R) { return 1; } static void PrintImage(window *W, short PAGE) { ImageInfo *D; rect R; float F; if (!W) return; D=W->appdata; if (!D || !D->pict) return; R=picturerect(D->pict); F= width(W->page) / width(R); if (F<1.0) scalerect(&R,F); F= height(W->page) / height(R); if (F<1.0) scalerect(&R,F); centrerect(&R,middle(W->page)); DrawTheImage(W,D,R); } static bool ImagePDFSize(window *W, rect *R) { ImageInfo *D; if (!W || !R) return 0; D=W->appdata; if (!D || !D->pict) return 0; *R=picturerect(D->pict); return 1; } static void ImagePDFDraw(window *W) { ImageInfo *D; if (!W) return; D=W->appdata; if (!D || !D->pict) return; DrawTheImage(W,D,W->page); } // aspect ratio! // Menus static void SetUpImageMenus(void) { SetUpFileMenu(); SetUpWindowMenu(); } static bool ImageCommand(window *W, UInt32 C) { return FileCommand(W,C); } // Controls static ControlActionUPP ImageUPP; // control action procedure for scroll bars static pascal void imagescroll(ControlRef C, short PART) { int OLD,NEW; window *W; if (!C) return; W=controlswindow(C); if (!W) return; if (PART==kControlIndicatorPart) { redraw(W); return; } OLD=GetControlValue(C); NEW= OLD + partincrement(PART,32,256); SetControlValue(C,NEW); NEW=GetControlValue(C); if (NEW==OLD) return; redraw(W); } static void SetImageScrolls(window *W) { ImageInfo *D; rect R; int MAX; if (!W) return; D=W->appdata; if (!D || !D->pict) return; R=picturerect(D->pict); MAX= width(R) - width(W->page); setscrollfrac(W->hsb,0,MAX); MAX= height(R) - height(W->page); setscrollfrac(W->vsb,0,MAX); } static void ImageControlsInit(window *W) { givescrolls(W); SetControlAction(W->vsb,ImageUPP); SetControlAction(W->hsb,ImageUPP); SetImageScrolls(W); } static bool ImageWheel(window *W, short DX, short DY) { ControlRef C; int OLD,NEW; bool Did; if (!W) return 0; Did=0; C=W->hsb; if (DX && C) { OLD=GetControlValue(C); SetControlValue(C,OLD - (3 * DX)); NEW=GetControlValue(C); if (NEW!=OLD) Did=1; } C=W->vsb; if (DY && C) { OLD=GetControlValue(C); SetControlValue(C,OLD - (3 * DY)); NEW=GetControlValue(C); if (NEW!=OLD) Did=1; } redraw(W); return Did; } // Window events static bool ImageContent(mouseclick *M) { window *W; ImageInfo *D; if (!M) return 0; W=M->wind; if (!W) return 0; D=W->appdata; if (!D) return 0; Was=Sel; Sel=D->obj; if (Sel!=Was) RedisplayAll(); //if (M->right && Sel) return ImagePopup(W,M->point); if (M->cmnd) SwitchToInfo(); //else if (Sel!=Was) RedisplayAll(); return 1; } static void ImageGrow(window *W) { if (!W) return; SetImageScrolls(W); } // "Image" windows void InitImageViews(void) { IMAGEWINDS=0; //Title=resstring(206); ImageUPP=NewControlActionUPP(&imagescroll); } static int ImageIndex(window *W) { int i; for (i=0;iappdata; if (!D) continue; if (D->obj==V) return W; } return NULL; } static void OldImageWindow(window *W) { ImageInfo *D; if (!W) return; D=W->appdata; if (!D) return; if (D->pict) oldpicture(D->pict); old(D); oldwindow(W); } static void CloseImageWindow(window *W) { int i; if (!W) return; i=ImageIndex(W); if (i<0) return; OldImageWindow(W); ImageWind[i]=ImageWind[--IMAGEWINDS]; } void CloseAllImages(void) { while (IMAGEWINDS>0) CloseImageWindow(ImageWind[0]); } static window *NewImageWindow(void *V, Point Pt) { rect R; window *W; ImageInfo *D; Person *P; Event *E; char *Name,*Cap; //FSSpec *FSS; FSRef *FSR; //OSErr err; if (!V) return NULL; FSR=NULL; Name=NULL; Cap=NULL; if (VoidIsPers(V)) { P=(Person *)V; FSR=P->pict; Name=P->surname; Cap=P->cap; } else if (VoidIsEvent(V)) { E=(Event *)V; FSR=E->pict; Name=E->name; Cap=E->cap; } if (!FSR || !Name) return NULL; //err=FSpMakeFSRef(FSS,&FSR); if (err) { beep(); return NULL; } D=pointer(sizeof(ImageInfo)); D->pict=filepicture(FSR); if (!D->pict) return NULL; D->obj=V; D->cap= Cap ? copyof(Cap) : NULL; R=picturerect(D->pict); Name=cat(0,"Image: ",Name); W=customwindow(Name,width(R) + SCROLLTHICK,height(R) + SCROLLTHICK,1); old(Name); if (!W || !W->wmw) return NULL; W->appdata=D; MacMoveWindow(W->wmw,Pt.h,Pt.v,0); ImageControlsInit(W); installmenus(W,NULL,&SetUpImageMenus,&AdjustFileMenu,&ImageCommand); installgrow(W,&ImageGrow,NULL); installpaint(W,&DrawImage); installclick(W,&ImageContent,&CloseImageWindow,&DrawImage); installwheel(W,&ImageWheel); installprint(W,&ImagePages,&PrintImage); installpict(W,&ImagePDFSize,&ImagePDFDraw); revealwindow(W); return W; } static void AddImageWindow(void *V, Point Pt) { window *W; if (!V) return; if (IMAGEWINDS>=MAXIMAGEWINDS) return; W=NewImageWindow(V,Pt); if (!W) return; ImageWind[IMAGEWINDS++]=W; revealwindow(W); redraw(W); } void OpenImage(void *V, Point Pt) { window *W; W=ImageWindOf(V); if (W) revealwindow(W); else AddImageWindow(V,Pt); }