Now clock doesn't quit in case it can't find its resource (due to a bug in BRoster::FindApp(), probably). It looks like crap but it's not important right now. Also restyled a bit the cl_view.h/cpp files

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15657 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2005-12-22 21:19:23 +00:00
parent bdf1da4d6e
commit 57de0ba79d
2 changed files with 129 additions and 169 deletions
+104 -136
View File
@@ -10,22 +10,22 @@
*/ */
#define DEBUG 1 #define DEBUG 1
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "clock.h"
#include "cl_view.h"
#include <float.h> #include <float.h>
#include <math.h> #include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <Debug.h>
#include <Application.h>
#include <Roster.h>
#include <Alert.h> #include <Alert.h>
#include <Application.h>
#include <Debug.h>
#include <Dragger.h>
#include <Entry.h> #include <Entry.h>
#include <Resources.h> #include <Resources.h>
#include <Roster.h>
#include "clock.h"
#include "cl_view.h"
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
TOffscreenView::TOffscreenView(BRect frame, char *name, short mRadius, 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 = get_ref_for_path("/boot/apps/Clock", &ref);
error = be_roster->FindApp(app_signature, &ref); error = be_roster->FindApp(app_signature, &ref);
printf("be_roster->FindApp() returned %s\n", strerror(error));
if (error == B_NO_ERROR) { if (error == B_NO_ERROR) {
BFile file(&ref, O_RDONLY); BFile file(&ref, O_RDONLY);
if (file.InitCheck() == B_NO_ERROR) { error = file.InitCheck();
BResources rsrcs(&file); if (error == B_NO_ERROR) {
error = 0; BResources rsrcs(&file);
for (loop = 0; loop <= 8; loop++) { for (loop = 0; loop <= 8; loop++) {
if ((picH = rsrcs.FindResource('PICT', loop+4, &len))) { 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); fClockFace[loop]->SetBits(picH,len,0,B_COLOR_8_BIT);
free(picH); free(picH);
} else {
error = 1;
} }
} }
theRect.Set(0,0,15,15); theRect.Set(0,0,15,15);
if ((picH = rsrcs.FindResource('MICN', "center", &len))) { 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); fCenter->SetBits(picH,len,0,B_COLOR_8_BIT);
free(picH); free(picH);
} else {
error = 1;
} }
theRect.Set(0,0,2,2); theRect.Set(0,0,2,2);
if ((picH = rsrcs.FindResource('PICT', 13, &len))) { 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); fInner->SetBits(picH,len,0,B_COLOR_8_BIT);
free(picH); 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; fMinutesRadius = mRadius;
fHoursRadius = hRadius; fHoursRadius = hRadius;
@@ -139,74 +120,66 @@ TOffscreenView::TOffscreenView(BRect frame, char *name, short mRadius,
} }
} }
/* ---------------------------------------------------------------- */
void TOffscreenView::NextFace() void
TOffscreenView::NextFace()
{ {
fFace++; fFace++;
if (fFace > 8) if (fFace > 8)
fFace = 1; fFace = 1;
}; };
/* ---------------------------------------------------------------- */
void TOffscreenView::AttachedToWindow() void
TOffscreenView::AttachedToWindow()
{ {
SetFontSize(18); SetFontSize(18);
SetFont(be_plain_font); SetFont(be_plain_font);
} }
/* ---------------------------------------------------------------- */
void TOffscreenView::DrawX() void
{ TOffscreenView::DrawX()
//BRect bound; {
short hours;
ASSERT(Window()); ASSERT(Window());
if (Window()->Lock()) { if (Window()->Lock()) {
DrawBitmap(fClockFace[fFace],BPoint(0,0)); if (fClockFace != NULL)
DrawBitmap(fClockFace[fFace], BPoint(0, 0));
// //
// Draw hands // Draw hands
// //
SetHighColor(0,0,0); SetHighColor(0, 0, 0);
hours = fHours; int32 hours = fHours;
if (hours >= 12) if (hours >= 12)
hours -= 12; hours -= 12;
hours *= 5; hours *= 5;
hours += (fMinutes / 12); hours += (fMinutes / 12);
StrokeLine(BPoint(fOffset,fOffset),fHourPoints[hours]); StrokeLine(BPoint(fOffset, fOffset), fHourPoints[hours]);
SetDrawingMode(B_OP_OVER); 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); SetDrawingMode(B_OP_COPY);
StrokeLine(BPoint(fOffset,fOffset),fMinutePoints[fMinutes]); StrokeLine(BPoint(fOffset, fOffset), fMinutePoints[fMinutes]);
SetHighColor(180,180,180); SetHighColor(180, 180, 180);
if (fShowSeconds) if (fShowSeconds)
StrokeLine(BPoint(fOffset,fOffset),fMinutePoints[fSeconds]); StrokeLine(BPoint(fOffset, fOffset), fMinutePoints[fSeconds]);
DrawBitmap(fInner,BPoint(fOffset-1,fOffset-1)); if (fInner != NULL)
DrawBitmap(fInner, BPoint(fOffset - 1, fOffset - 1));
Sync(); Sync();
Window()->Unlock(); Window()->Unlock();
} }
} }
/* ---------------------------------------------------------------- */
TOffscreenView::~TOffscreenView() TOffscreenView::~TOffscreenView()
{ {
short counter; for (int32 counter = 0; counter <= 8; counter++)
for (counter = 0; counter <= 8; counter++)
delete fClockFace[counter]; delete fClockFace[counter];
}; };
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */
#include <Dragger.h>
/* /*
* Onscreen view object * Onscreen view object
@@ -227,35 +200,34 @@ TOnscreenView::TOnscreenView(BRect rect, char *title,
AddChild(dw); 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) short offset, long face, bool show)
{ {
fmRadius = mRadius; fmRadius = mRadius;
fhRadius = hRadius; fhRadius = hRadius;
fOffset = offset; fOffset = offset;
fRect = rect; fRect = rect;
OffscreenView = NULL; fOffscreenView = NULL;
Offscreen = NULL; fOffscreen = NULL;
#if 1 fOffscreenView = new TOffscreenView(rect, "freqd",mRadius,hRadius, offset, face, show);
OffscreenView = new TOffscreenView(rect, "freqd",mRadius,hRadius, fOffscreen = new BBitmap(rect, B_COLOR_8_BIT, true);
offset, face, show); if (fOffscreen != NULL && fOffscreen->Lock()) {
Offscreen = new BBitmap(rect, B_COLOR_8_BIT, TRUE); fOffscreen->AddChild(fOffscreenView);
Offscreen->Lock(); fOffscreen->Unlock();
Offscreen->AddChild(OffscreenView);
Offscreen->Unlock(); fOffscreenView->DrawX();
OffscreenView->DrawX(); }
#endif
} }
/* ---------------------------------------------------------------- */
TOnscreenView::~TOnscreenView() TOnscreenView::~TOnscreenView()
{ {
delete Offscreen; delete fOffscreen;
} }
/* ---------------------------------------------------------------- */
TOnscreenView::TOnscreenView(BMessage *data) TOnscreenView::TOnscreenView(BMessage *data)
: BView(data) : BView(data)
@@ -265,142 +237,138 @@ TOnscreenView::TOnscreenView(BMessage *data)
data->FindInt32("face"), data->FindBool("seconds")); 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); inherited::Archive(data, deep);
data->AddString("add_on", app_signature); data->AddString("add_on", app_signature);
//+ data->AddString("add_on_path", "/boot/apps/Clock"); //+ data->AddString("add_on_path", "/boot/apps/Clock");
data->AddRect("bounds", Bounds()); data->AddRect("bounds", Bounds());
data->AddInt32("mRadius", OffscreenView->fMinutesRadius); data->AddInt32("mRadius", fOffscreenView->fMinutesRadius);
data->AddInt32("hRadius", OffscreenView->fHoursRadius); data->AddInt32("hRadius", fOffscreenView->fHoursRadius);
data->AddInt32("offset", OffscreenView->fOffset); data->AddInt32("offset", fOffscreenView->fOffset);
data->AddBool("seconds", OffscreenView->fShowSeconds); data->AddBool("seconds", fOffscreenView->fShowSeconds);
data->AddInt32("face", OffscreenView->fFace); data->AddInt32("face", fOffscreenView->fFace);
return 0; return 0;
} }
/* ---------------------------------------------------------------- */
BArchivable *TOnscreenView::Instantiate(BMessage *data) BArchivable *
TOnscreenView::Instantiate(BMessage *data)
{ {
if (!validate_instantiation(data, "TOnscreenView")) if (!validate_instantiation(data, "TOnscreenView"))
return NULL; return NULL;
return new TOnscreenView(data); return new TOnscreenView(data);
} }
/* ---------------------------------------------------------------- */
void TOnscreenView::AttachedToWindow() void
TOnscreenView::AttachedToWindow()
{ {
//+ PRINT(("InitData m=%d, h=%d, offset=%d\n", fmRadius, fhRadius, fOffset)); //+ PRINT(("InitData m=%d, h=%d, offset=%d\n", fmRadius, fhRadius, fOffset));
//+ PRINT_OBJECT(fRect); //+ PRINT_OBJECT(fRect);
} }
/* ---------------------------------------------------------------- */
void TOnscreenView::Pulse() void
TOnscreenView::Pulse()
{ {
short hours,minutes,seconds; short hours,minutes,seconds;
struct tm *loctime; struct tm *loctime;
time_t current; time_t current;
ASSERT(Offscreen); ASSERT(fOffscreen);
ASSERT(OffscreenView); ASSERT(fOffscreenView);
current = time(0); current = time(0);
loctime = localtime(&current); loctime = localtime(&current);
hours = loctime->tm_hour; hours = loctime->tm_hour;
minutes = loctime->tm_min; minutes = loctime->tm_min;
seconds = loctime->tm_sec; seconds = loctime->tm_sec;
if ((OffscreenView->fShowSeconds && (seconds != OffscreenView->fSeconds)) || if ((fOffscreenView->fShowSeconds && (seconds != fOffscreenView->fSeconds)) ||
(minutes != OffscreenView->fMinutes)) { (minutes != fOffscreenView->fMinutes)) {
OffscreenView->fHours = hours; fOffscreenView->fHours = hours;
OffscreenView->fMinutes = minutes; fOffscreenView->fMinutes = minutes;
OffscreenView->fSeconds = seconds; fOffscreenView->fSeconds = seconds;
BRect b = Bounds(); BRect b = Bounds();
b.InsetBy(12,12); b.InsetBy(12,12);
Draw(b); Draw(b);
} }
} }
/* ---------------------------------------------------------------- */
void TOnscreenView::UseFace( short face ) void
TOnscreenView::UseFace( short face )
{ {
OffscreenView->fFace = face; fOffscreenView->fFace = face;
BRect b = Bounds(); BRect b = Bounds();
b.InsetBy(12,12); b.InsetBy(12,12);
Draw(b); Draw(b);
} }
/* ---------------------------------------------------------------- */
void TOnscreenView::ShowSecs( bool secs ) void
TOnscreenView::ShowSecs( bool secs )
{ {
OffscreenView->fShowSeconds = secs; fOffscreenView->fShowSeconds = secs;
BRect b = Bounds(); BRect b = Bounds();
b.InsetBy(12,12); b.InsetBy(12,12);
Invalidate(b); 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(fOffscreen);
ASSERT(Offscreen); ASSERT(fOffscreenView);
ASSERT(OffscreenView);
bool b = Offscreen->Lock(); if (fOffscreen->Lock()) {
ASSERT(b); fOffscreenView->DrawX(); // Composite the clock offscreen...
OffscreenView->DrawX(); // Composite the clock offscreen... DrawBitmap(fOffscreen, rect, rect);
DrawBitmap(Offscreen, rect, rect); fOffscreen->Unlock();
Offscreen->Unlock(); }
current = time(0);
}; };
/* ---------------------------------------------------------------- */
void TOnscreenView::MouseDown( BPoint point ) void
TOnscreenView::MouseDown( BPoint point )
{ {
BPoint cursor; BPoint cursor;
ulong buttons; ulong buttons;
BRect bounds = Bounds(); BRect bounds = Bounds();
GetMouse(&cursor,&buttons); GetMouse(&cursor,&buttons);
if (buttons & 0x2) { if (buttons & B_SECONDARY_MOUSE_BUTTON) {
OffscreenView->fShowSeconds = !OffscreenView->fShowSeconds; fOffscreenView->fShowSeconds = !fOffscreenView->fShowSeconds;
be_app->PostMessage(SHOW_SECONDS); be_app->PostMessage(SHOW_SECONDS);
bounds.InsetBy(12,12); bounds.InsetBy(12,12);
Draw(bounds); Invalidate(bounds);
} else { } else {
OffscreenView->NextFace(); fOffscreenView->NextFace();
Draw(bounds); Invalidate(bounds);
BView *c = ChildAt(0); BView *child = ChildAt(0);
if (c) if (child)
c->Draw(c->Bounds()); child->Invalidate();
} }
}; };
/* ---------------------------------------------------------------- */
void void
TOnscreenView::MessageReceived(BMessage *msg) TOnscreenView::MessageReceived(BMessage *msg)
+25 -33
View File
@@ -11,44 +11,36 @@
#ifndef _CL_VIEW_H_ #ifndef _CL_VIEW_H_
#define _CL_VIEW_H_ #define _CL_VIEW_H_
#ifndef _APPLICATION_H
#include <Application.h> #include <Application.h>
#endif
#ifndef _WINDOW_H
#include <Window.h>
#endif
#ifndef _VIEW_H
#include <View.h>
#endif
#ifndef _BITMAP_H
#include <Bitmap.h> #include <Bitmap.h>
#endif #include <View.h>
#include <Window.h>
#include <time.h> #include <time.h>
class TOffscreenView : public BView { class TOffscreenView : public BView {
public: public:
TOffscreenView(BRect frame, char *name, short mRadius, TOffscreenView(BRect frame, char *name, short mRadius,
short hRadius, short offset, long face, bool show); short hRadius, short offset, long face, bool show);
virtual ~TOffscreenView(); virtual ~TOffscreenView();
virtual void AttachedToWindow(); virtual void AttachedToWindow();
virtual void DrawX(); virtual void DrawX();
void NextFace(); void NextFace();
BBitmap *fClockFace[9]; BBitmap *fClockFace[9];
BBitmap *fCenter; BBitmap *fCenter;
BBitmap *fInner; BBitmap *fInner;
short fFace; short fFace;
BPoint fMinutePoints[60]; BPoint fMinutePoints[60];
BPoint fHourPoints[60]; BPoint fHourPoints[60];
short fMinutesRadius; short fMinutesRadius;
short fHoursRadius; short fHoursRadius;
short fOffset; short fOffset;
short fHours; short fHours;
short fMinutes; short fMinutes;
short fSeconds; short fSeconds;
bool fShowSeconds; bool fShowSeconds;
}; };
@@ -80,12 +72,12 @@ private:
typedef BView inherited; typedef BView inherited;
BBitmap *Offscreen; BBitmap *fOffscreen;
TOffscreenView *OffscreenView; TOffscreenView *fOffscreenView;
short fmRadius; short fmRadius;
short fhRadius; short fhRadius;
short fOffset; short fOffset;
BRect fRect; BRect fRect;
}; };
#endif #endif