diff --git a/src/apps/clock/cl_view.cpp b/src/apps/clock/cl_view.cpp index 41da730579..3d3980c075 100644 --- a/src/apps/clock/cl_view.cpp +++ b/src/apps/clock/cl_view.cpp @@ -10,22 +10,22 @@ */ #define DEBUG 1 -#include -#include - -#include -#include "clock.h" -#include "cl_view.h" #include #include +#include +#include +#include -#include -#include -#include #include +#include +#include +#include #include #include +#include +#include "clock.h" +#include "cl_view.h" /* ---------------------------------------------------------------- */ TOffscreenView::TOffscreenView(BRect frame, char *name, short mRadius, @@ -50,55 +50,36 @@ TOffscreenView::TOffscreenView(BRect frame, char *name, short mRadius, //+ error = get_ref_for_path("/boot/apps/Clock", &ref); error = be_roster->FindApp(app_signature, &ref); - + printf("be_roster->FindApp() returned %s\n", strerror(error)); if (error == B_NO_ERROR) { - BFile file(&ref, O_RDONLY); - if (file.InitCheck() == B_NO_ERROR) { - BResources rsrcs(&file); - error = 0; - + BFile file(&ref, O_RDONLY); + error = file.InitCheck(); + if (error == B_NO_ERROR) { + BResources rsrcs(&file); + for (loop = 0; loop <= 8; loop++) { if ((picH = rsrcs.FindResource('PICT', loop+4, &len))) { - fClockFace[loop] = new BBitmap(theRect,B_COLOR_8_BIT); + fClockFace[loop] = new BBitmap(theRect, B_COLOR_8_BIT); fClockFace[loop]->SetBits(picH,len,0,B_COLOR_8_BIT); free(picH); - } else { - error = 1; } } theRect.Set(0,0,15,15); if ((picH = rsrcs.FindResource('MICN', "center", &len))) { - fCenter = new BBitmap(theRect,B_COLOR_8_BIT); + fCenter = new BBitmap(theRect, B_COLOR_8_BIT); fCenter->SetBits(picH,len,0,B_COLOR_8_BIT); free(picH); - } else { - error = 1; } theRect.Set(0,0,2,2); if ((picH = rsrcs.FindResource('PICT', 13, &len))) { - fInner = new BBitmap(theRect,B_COLOR_8_BIT); + fInner = new BBitmap(theRect, B_COLOR_8_BIT); fInner->SetBits(picH,len,0,B_COLOR_8_BIT); free(picH); - } else { - error = 1; - } - - if (error) { - // ??? yikes. This is terrible. No good way to signal error - // from here. How do we properly cleanup up here? Need to - // make sure the app_server side also gets cleaned up! - - BAlert *alert = new BAlert("Error", "Clock: Could not find necessary resources.", "Quit"); - alert->Go(); - delete this; - exit(1); } } - } else { - exit(1); - } + } fMinutesRadius = mRadius; fHoursRadius = hRadius; @@ -139,74 +120,66 @@ TOffscreenView::TOffscreenView(BRect frame, char *name, short mRadius, } } -/* ---------------------------------------------------------------- */ -void TOffscreenView::NextFace() +void +TOffscreenView::NextFace() { fFace++; if (fFace > 8) fFace = 1; }; -/* ---------------------------------------------------------------- */ -void TOffscreenView::AttachedToWindow() +void +TOffscreenView::AttachedToWindow() { SetFontSize(18); SetFont(be_plain_font); } -/* ---------------------------------------------------------------- */ -void TOffscreenView::DrawX() -{ -//BRect bound; -short hours; - +void +TOffscreenView::DrawX() +{ ASSERT(Window()); if (Window()->Lock()) { - DrawBitmap(fClockFace[fFace],BPoint(0,0)); + if (fClockFace != NULL) + DrawBitmap(fClockFace[fFace], BPoint(0, 0)); // // Draw hands // - SetHighColor(0,0,0); - hours = fHours; + SetHighColor(0, 0, 0); + int32 hours = fHours; if (hours >= 12) hours -= 12; hours *= 5; hours += (fMinutes / 12); - StrokeLine(BPoint(fOffset,fOffset),fHourPoints[hours]); + StrokeLine(BPoint(fOffset, fOffset), fHourPoints[hours]); SetDrawingMode(B_OP_OVER); - DrawBitmap(fCenter,BPoint(fOffset-3,fOffset-3)); + if (fCenter != NULL) + DrawBitmap(fCenter, BPoint(fOffset - 3, fOffset - 3)); SetDrawingMode(B_OP_COPY); - StrokeLine(BPoint(fOffset,fOffset),fMinutePoints[fMinutes]); - SetHighColor(180,180,180); + StrokeLine(BPoint(fOffset, fOffset), fMinutePoints[fMinutes]); + SetHighColor(180, 180, 180); if (fShowSeconds) - StrokeLine(BPoint(fOffset,fOffset),fMinutePoints[fSeconds]); - DrawBitmap(fInner,BPoint(fOffset-1,fOffset-1)); + StrokeLine(BPoint(fOffset, fOffset), fMinutePoints[fSeconds]); + if (fInner != NULL) + DrawBitmap(fInner, BPoint(fOffset - 1, fOffset - 1)); Sync(); Window()->Unlock(); } } -/* ---------------------------------------------------------------- */ TOffscreenView::~TOffscreenView() { - short counter; - - for (counter = 0; counter <= 8; counter++) + for (int32 counter = 0; counter <= 8; counter++) delete fClockFace[counter]; }; -/* ---------------------------------------------------------------- */ -/* ---------------------------------------------------------------- */ -/* ---------------------------------------------------------------- */ - -#include /* * Onscreen view object @@ -227,35 +200,34 @@ TOnscreenView::TOnscreenView(BRect rect, char *title, AddChild(dw); } -void TOnscreenView::InitObject(BRect rect, short mRadius, short hRadius, + +void +TOnscreenView::InitObject(BRect rect, short mRadius, short hRadius, short offset, long face, bool show) { fmRadius = mRadius; fhRadius = hRadius; fOffset = offset; fRect = rect; - OffscreenView = NULL; - Offscreen = NULL; + fOffscreenView = NULL; + fOffscreen = NULL; -#if 1 - OffscreenView = new TOffscreenView(rect, "freqd",mRadius,hRadius, - offset, face, show); - Offscreen = new BBitmap(rect, B_COLOR_8_BIT, TRUE); - Offscreen->Lock(); - Offscreen->AddChild(OffscreenView); - Offscreen->Unlock(); - OffscreenView->DrawX(); -#endif + fOffscreenView = new TOffscreenView(rect, "freqd",mRadius,hRadius, offset, face, show); + fOffscreen = new BBitmap(rect, B_COLOR_8_BIT, true); + if (fOffscreen != NULL && fOffscreen->Lock()) { + fOffscreen->AddChild(fOffscreenView); + fOffscreen->Unlock(); + + fOffscreenView->DrawX(); + } } -/* ---------------------------------------------------------------- */ TOnscreenView::~TOnscreenView() { - delete Offscreen; + delete fOffscreen; } -/* ---------------------------------------------------------------- */ TOnscreenView::TOnscreenView(BMessage *data) : BView(data) @@ -265,142 +237,138 @@ TOnscreenView::TOnscreenView(BMessage *data) data->FindInt32("face"), data->FindBool("seconds")); } -/* ---------------------------------------------------------------- */ -status_t TOnscreenView::Archive(BMessage *data, bool deep) const +status_t +TOnscreenView::Archive(BMessage *data, bool deep) const { inherited::Archive(data, deep); data->AddString("add_on", app_signature); //+ data->AddString("add_on_path", "/boot/apps/Clock"); data->AddRect("bounds", Bounds()); - data->AddInt32("mRadius", OffscreenView->fMinutesRadius); - data->AddInt32("hRadius", OffscreenView->fHoursRadius); - data->AddInt32("offset", OffscreenView->fOffset); - data->AddBool("seconds", OffscreenView->fShowSeconds); - data->AddInt32("face", OffscreenView->fFace); + data->AddInt32("mRadius", fOffscreenView->fMinutesRadius); + data->AddInt32("hRadius", fOffscreenView->fHoursRadius); + data->AddInt32("offset", fOffscreenView->fOffset); + data->AddBool("seconds", fOffscreenView->fShowSeconds); + data->AddInt32("face", fOffscreenView->fFace); return 0; } -/* ---------------------------------------------------------------- */ -BArchivable *TOnscreenView::Instantiate(BMessage *data) +BArchivable * +TOnscreenView::Instantiate(BMessage *data) { if (!validate_instantiation(data, "TOnscreenView")) return NULL; return new TOnscreenView(data); } -/* ---------------------------------------------------------------- */ -void TOnscreenView::AttachedToWindow() +void +TOnscreenView::AttachedToWindow() { //+ PRINT(("InitData m=%d, h=%d, offset=%d\n", fmRadius, fhRadius, fOffset)); //+ PRINT_OBJECT(fRect); } -/* ---------------------------------------------------------------- */ -void TOnscreenView::Pulse() +void +TOnscreenView::Pulse() { short hours,minutes,seconds; struct tm *loctime; time_t current; - ASSERT(Offscreen); - ASSERT(OffscreenView); + ASSERT(fOffscreen); + ASSERT(fOffscreenView); current = time(0); loctime = localtime(¤t); hours = loctime->tm_hour; minutes = loctime->tm_min; seconds = loctime->tm_sec; - if ((OffscreenView->fShowSeconds && (seconds != OffscreenView->fSeconds)) || - (minutes != OffscreenView->fMinutes)) { - OffscreenView->fHours = hours; - OffscreenView->fMinutes = minutes; - OffscreenView->fSeconds = seconds; + if ((fOffscreenView->fShowSeconds && (seconds != fOffscreenView->fSeconds)) || + (minutes != fOffscreenView->fMinutes)) { + fOffscreenView->fHours = hours; + fOffscreenView->fMinutes = minutes; + fOffscreenView->fSeconds = seconds; BRect b = Bounds(); b.InsetBy(12,12); Draw(b); } } -/* ---------------------------------------------------------------- */ -void TOnscreenView::UseFace( short face ) +void +TOnscreenView::UseFace( short face ) { - OffscreenView->fFace = face; + fOffscreenView->fFace = face; BRect b = Bounds(); b.InsetBy(12,12); Draw(b); } -/* ---------------------------------------------------------------- */ -void TOnscreenView::ShowSecs( bool secs ) +void +TOnscreenView::ShowSecs( bool secs ) { - OffscreenView->fShowSeconds = secs; + fOffscreenView->fShowSeconds = secs; BRect b = Bounds(); b.InsetBy(12,12); Invalidate(b); } -/* ---------------------------------------------------------------- */ -short TOnscreenView::ReturnFace( void ) +short +TOnscreenView::ReturnFace( void ) { - return(OffscreenView->fFace); + return(fOffscreenView->fFace); } -/* ---------------------------------------------------------------- */ -short TOnscreenView::ReturnSeconds( void ) +short +TOnscreenView::ReturnSeconds( void ) { - return(OffscreenView->fShowSeconds); + return(fOffscreenView->fShowSeconds); } -/* ---------------------------------------------------------------- */ -void TOnscreenView::Draw(BRect rect) +void +TOnscreenView::Draw(BRect rect) { - time_t current; - ASSERT(Offscreen); - ASSERT(OffscreenView); + ASSERT(fOffscreen); + ASSERT(fOffscreenView); - bool b = Offscreen->Lock(); - ASSERT(b); - OffscreenView->DrawX(); // Composite the clock offscreen... - DrawBitmap(Offscreen, rect, rect); - Offscreen->Unlock(); - - current = time(0); + if (fOffscreen->Lock()) { + fOffscreenView->DrawX(); // Composite the clock offscreen... + DrawBitmap(fOffscreen, rect, rect); + fOffscreen->Unlock(); + } }; -/* ---------------------------------------------------------------- */ -void TOnscreenView::MouseDown( BPoint point ) +void +TOnscreenView::MouseDown( BPoint point ) { BPoint cursor; ulong buttons; BRect bounds = Bounds(); GetMouse(&cursor,&buttons); - if (buttons & 0x2) { - OffscreenView->fShowSeconds = !OffscreenView->fShowSeconds; + if (buttons & B_SECONDARY_MOUSE_BUTTON) { + fOffscreenView->fShowSeconds = !fOffscreenView->fShowSeconds; be_app->PostMessage(SHOW_SECONDS); bounds.InsetBy(12,12); - Draw(bounds); + Invalidate(bounds); } else { - OffscreenView->NextFace(); - Draw(bounds); - BView *c = ChildAt(0); - if (c) - c->Draw(c->Bounds()); + fOffscreenView->NextFace(); + Invalidate(bounds); + BView *child = ChildAt(0); + if (child) + child->Invalidate(); } }; -/* ---------------------------------------------------------------- */ void TOnscreenView::MessageReceived(BMessage *msg) diff --git a/src/apps/clock/cl_view.h b/src/apps/clock/cl_view.h index c6fe6b1342..779c72a903 100644 --- a/src/apps/clock/cl_view.h +++ b/src/apps/clock/cl_view.h @@ -11,44 +11,36 @@ #ifndef _CL_VIEW_H_ #define _CL_VIEW_H_ -#ifndef _APPLICATION_H #include -#endif -#ifndef _WINDOW_H -#include -#endif -#ifndef _VIEW_H -#include -#endif -#ifndef _BITMAP_H #include -#endif +#include +#include #include class TOffscreenView : public BView { public: - TOffscreenView(BRect frame, char *name, short mRadius, + TOffscreenView(BRect frame, char *name, short mRadius, short hRadius, short offset, long face, bool show); -virtual ~TOffscreenView(); -virtual void AttachedToWindow(); -virtual void DrawX(); + virtual ~TOffscreenView(); + virtual void AttachedToWindow(); + virtual void DrawX(); void NextFace(); -BBitmap *fClockFace[9]; -BBitmap *fCenter; -BBitmap *fInner; -short fFace; -BPoint fMinutePoints[60]; -BPoint fHourPoints[60]; -short fMinutesRadius; -short fHoursRadius; -short fOffset; -short fHours; -short fMinutes; -short fSeconds; -bool fShowSeconds; + BBitmap *fClockFace[9]; + BBitmap *fCenter; + BBitmap *fInner; + short fFace; + BPoint fMinutePoints[60]; + BPoint fHourPoints[60]; + short fMinutesRadius; + short fHoursRadius; + short fOffset; + short fHours; + short fMinutes; + short fSeconds; + bool fShowSeconds; }; @@ -80,12 +72,12 @@ private: typedef BView inherited; -BBitmap *Offscreen; -TOffscreenView *OffscreenView; -short fmRadius; -short fhRadius; -short fOffset; -BRect fRect; + BBitmap *fOffscreen; + TOffscreenView *fOffscreenView; + short fmRadius; + short fhRadius; + short fOffset; + BRect fRect; }; #endif