// thisday.c // // This file implements the "On This Day in..." window's menus, controls and events. // // by John D. de Boer #include "twist.h" #include "twindow.h" extern TimeGlobs Time; extern GlobalOpts Opt; extern void *Sel,*Was; extern IBNibRef Nib; int THISWINDS; // the number of "This Day" windows currently open window *ThisWind[MAXTHISWINDS]; // the "This Day in History" windows //string inHistory; // phrase "in History" from resource // Copy static void CopyThis(ThisInfo *D) { OSStatus err; ScrapRef scrap; string S; if (!D) return; S=cat3(5,TitleString(D),"\r\r",ThisDayString(D)); err=ClearCurrentScrap(); if (err!=noErr) return; err=GetCurrentScrap(&scrap); err=PutScrapFlavor(scrap,'TEXT',0,length(S),S); old(S); if (err!=noErr) return; } // Menus for the "This Day" window static MenuRef ThisMenu,MonthMenu,DateMenu; static void InitThisMenus(void) { OSStatus err; err=CreateMenuFromNib(Nib,CFSTR("this"),&ThisMenu); err=CreateMenuFromNib(Nib,CFSTR("month"),&MonthMenu); err=CreateMenuFromNib(Nib,CFSTR("date"),&DateMenu); } static void SetUpThisMenus(void) { SetUpFileMenu(); InsertMenu(ThisMenu,0); SetUpSizeMenu(); InsertMenu(MonthMenu,0); InsertMenu(DateMenu,0); SetUpWindowMenu(); } static int DaysInMonth[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; static void AdjustThisMenus(window *W) { int i,MAX; ThisInfo *D; if (!W) return; D=W->appdata; if (!D) return; AdjustFileMenu(W); AdjustSizeMenu(W); for (i=0;i<12;i++) CheckMenuItem(MonthMenu,i + 1,i==D->month); for (i=0;i<31;i++) CheckMenuItem(DateMenu, i + 1,i==D->date); if (!range(D->month,12)) return; MAX=DaysInMonth[D->month]; itemenable(DateMenu,31,MAX>=31); itemenable(DateMenu,30,MAX>=30); } static bool MonthIndex(UInt32 C, int *INDEX) { int j; j= C - 'Mo01'; if (j>8) { j-=256; j+=10; } if (!range(j,12)) return 0; if (INDEX) *INDEX=j; return 1; } static bool DateIndex(UInt32 C, int *INDEX) { int i,j; j= C - 'Da01'; for (i=0;i<3;i++) if (j > (i * 10) + 8) { j-=256; j+=10; } if (!range(j,31)) return 0; if (INDEX) *INDEX=j; return 1; } static bool ThisCommand(window *W, UInt32 C) { int index; ThisInfo *D; if (!W) return 0; D=W->appdata; if (!D) return 0; switch (C) { case 'copy': CopyThis(D); return 1; } if (MonthIndex(C,&index)) { D->month=index; D->date=lsr(D->date,DaysInMonth[D->month] - 1); DoThisDayCalcs(W); redraw(W); return 1; } if (DateIndex(C,&index)) { D->date=index; DoThisDayCalcs(W); redraw(W); return 1; } return FileCommand(W,C); } // Contextual Menus static bool ThisPopup(window *W, point Pt) { if (!Sel) return 0; if (VoidIsPers(Sel)) return PersonPopup(W,Pt,1,1,1,1,Sel); if (VoidIsEvent(Sel)) return EventPopup(W,Pt,1,1,1,Sel); return 0; } // Controls static ControlActionUPP ThisUPP; // control action procedure for vertical scroll bar static pascal void thisscroll(ControlRef C, short PART) { int OLD,NEW; window *W; ThisInfo *D; if (!C) return; W=controlswindow(C); if (!W) return; D=W->appdata; if (!D) return; if (PART==kControlIndicatorPart) { redraw(W); return; } OLD=GetControlValue(C); NEW= OLD + partincrement(PART,1,D->fit); SetControlValue(C,NEW); NEW=GetControlValue(C); if (NEW==OLD) return; redraw(W); } static void ThisControlsInit(window *W) { if (!W) return; givevertscroll(W); SetControlAction(W->vsb,ThisUPP); SetThisScrolls(W); } static bool ThisContent(mouseclick *M) { window *W; long CONTEXTTIME; if (!M) return 0; W=M->wind; if (!W) return 0; CONTEXTTIME= TickCount() + GetDblTime(); Was=Sel; Sel=ThisHit(M); if (Sel!=Was) RedisplayAll(); if (M->right && Sel) return ThisPopup(W,M->point); //if (M->cmnd) { if (Sel) SwitchToInfo(); else if (Was) RedisplayAll(); return 1; } while (stilldown(M)) if (Sel && TickCount()>CONTEXTTIME) return ThisPopup(W,M->point); return 1; } /* static bool ThisDouble(mouseclick *M) { if (!M) return 0; Was=Sel; Sel=ThisHit(M); if (Sel && Sel==Was) { SwitchToTimeLine(); return 1; } return ThisContent(M); }*/ static bool ThisKey(keystroke *K) { window *W; ThisInfo *D; if (!K) return 0; W=K->wind; if (!W) return 0; D=W->appdata; if (!D) return 0; switch (K->c) { case 28: D->date--; if (D->date<0) { D->month=mod(D->month - 1,12); D->date= DaysInMonth[D->month] - 1; } DoThisDayCalcs(W); redraw(W); return 1; case 29: D->date++; if (D->date>=DaysInMonth[D->month]) { D->month=mod(D->month + 1,12); D->date=0; } DoThisDayCalcs(W); redraw(W); return 1; } return 0; } static void ThisGrow(window *W) { if (!W) return; SetThisScrolls(W); } // "This Day" windows void InitThisViews(void) { THISWINDS=0; //inHistory=resstring(203); ThisUPP=NewControlActionUPP(&thisscroll); InitThisMenus(); } static int IndexOfWindow(window *W) { int i; for (i=0;iappdata; EmptyThisInfo(D); if (D->item) release(D->item); old(D); oldwindow(W); } static void CloseThisWindow(window *W) { int i; if (!W) return; i=IndexOfWindow(W); if (i<0) return; OldThisWindow(W); ThisWind[i]=ThisWind[--THISWINDS]; } void CloseAllThis(void) { while (THISWINDS>0) CloseThisWindow(ThisWind[0]); } static window *NewThisWindow(void) { window *W; ThisInfo *D; W=customwindow("This Day in History",300,200,1); if (!W) return NULL; stackwindow(W,StackableWindows()); D=W->appdata=pointer(sizeof(ThisInfo)); D->month=Time.month; D->date=Time.day; D->num=0; D->item=(ThisItem ***)handle(0); DoThisDayCalcs(W); ThisControlsInit(W); installmenus(W,NULL,&SetUpThisMenus,&AdjustThisMenus,&ThisCommand); installgrow(W,&ThisGrow,NULL); installpaint(W,&DrawThisDay); installclick(W,&ThisContent,&CloseThisWindow,&DrawThisDay); //installdouble(W,&ThisDouble); installkey(W,&ThisKey); installprint(W,NULL,&PrintThisDay); return W; } void AddThisWindow(void) { window *W; if (THISWINDS>=MAXTHISWINDS) return; W=NewThisWindow(); if (!W) return; ThisWind[THISWINDS++]=W; revealwindow(W); redraw(W); }