// twarn.c // // by John D. de Boer #include "twist.h" #include "twindow.h" // so that the File menu may be set up extern string FileName; extern window *WaitWind; window *WarnWind; // the "Warnings" window typedef struct { int level; // 0, 1 or 2 string msg; // the message } Message; //static string Title; // window title from resource static int WARNS, // number of warnings VSCR, // the top warning visible FIT, // the number of warnings that fit vertically in the window EXTRA; // the number that don't fit in the window static Message **Warn; // warning strings static const int WARNHEIGHT = 16; // line height // Controls static void CalcFit(void) { FIT= height(WarnWind->page) / WARNHEIGHT; EXTRA=gtr(0,WARNS - FIT); } static pascal void warnscroll(ControlRef C, short PART) { int OLD,INCR; window *W; if (!C) return; W=controlswindow(C); if (!W) return; if (PART==kControlIndicatorPart) { VSCR=GetControlValue(C); redraw(W); return; } INCR=partincrement(PART,1,FIT); VSCR=between(VSCR + INCR,0,EXTRA); OLD=GetControlValue(C); SetControlValue(C,VSCR); if (OLD==GetControlValue(C)) return; redraw(W); } static void SetWarnScroll(window *W) { if (!W || !W->vsb) return; CalcFit(); VSCR=between(VSCR,0,EXTRA); setscrollbar(W->vsb,VSCR,0,EXTRA); } static bool WarnWheel(window *W, short DX, short DY) { ControlRef C; int OLD,NEW; if (!W || !DY) return 0; C=W->vsb; if (!C) return 0; OLD=GetControlValue(C); VSCR=between(OLD - DY,0,EXTRA); SetControlValue(C,VSCR); NEW=GetControlValue(C); if (OLD==NEW) return 0; redraw(W); return 1; } // Posting a warning static void AddWarnToList(string S, int LEVEL) { Message *M; if (!S) return; fuzzyresize(Warn,WARNS + 1,sizeof(Message)); M= *Warn + (WARNS++); M->msg=S; M->level=LEVEL; } static void PurgeWarningList(void) { Message *M; while (WARNS>0) { M= *Warn + (--WARNS); old(M->msg); } } static void MessageOld(string S, int LEVEL) { if (!S) return; if (FileName) S=cat4(1,S," (",FileName,")"); else S=cat(1,S," (-)"); AddWarnToList(S,LEVEL); if (LEVEL<=0 && !windowseen(WarnWind)) return; revealbehind(WarnWind,WaitWind); SetWarnScroll(WarnWind); redraw(WarnWind); } void NoteOld(string S) { MessageOld(S,0); } void Note(const char *S) { NoteOld(copyof(S)); } void WarnOld(string S) { MessageOld(S,1); } void Warning(const char *S) { WarnOld(copyof(S)); } void DangerOld(string S) { MessageOld(S,2); } void Danger(const char *S) { DangerOld(copyof(S)); } void PurgeWarnings(void) { PurgeWarningList(); VSCR=0; SetWarnScroll(WarnWind); redraw(WarnWind); } // Menus static void SetUpWarnMenus(void) { SetUpFileMenu(); SetUpWindowMenu(); } static void AdjustWarnMenus(window *W) { if (!W) return; AdjustFileMenu(W); } // Drawing static picture WarnIcon[3]; static point RowAnchor(int ROW) { point P; P.x=5; P.y= height(WarnWind->page) - (WARNHEIGHT * (ROW - VSCR)) - 14; return P; } static void DrawOneWarning(window *W, int ROW, const Message *M) { rect R; point Pt; if (!W || !M || !M->msg) return; Pt=RowAnchor(ROW); setrect(&R,Pt.x,Pt.y - 21,32,32); if (range(M->level,3)) drawpicture(W,WarnIcon[M->level],R); drawtext(W,Pt.x + 18,Pt.y,M->msg); } static void DrawWarnings(window *W) { int i; point Pt; if (!W) return; fillindex(W,black); lock(Warn); for (i=0;ivsb,WarnUPP); VSCR=0; DoGrow(WarnWind); installmenus(WarnWind,NULL,&SetUpWarnMenus,&AdjustWarnMenus,&FileCommand); installpaint(WarnWind,&DrawWarnings); installgrow(WarnWind,&DoGrow,NULL); installclick(WarnWind,&DoWarnContent,&concealwindow,NULL); installwheel(WarnWind,&WarnWheel); WarnIcon[0]=getpicture("info"); WarnIcon[1]=getpicture("caution"); WarnIcon[2]=getpicture("stop"); WARNS=0; Warn=(Message **)handle(0); //DRW=0; }