118d3e65eSAxel Dörfler/*
218d3e65eSAxel Dörfler * Copyright 2008, Axel D��rfler, axeld@pinc-software.de. All rights reserved.
318d3e65eSAxel Dörfler * Distributed under the terms of the MIT License.
418d3e65eSAxel Dörfler */
518d3e65eSAxel Dörfler#ifndef ACTIVITY_VIEW_H
618d3e65eSAxel Dörfler#define ACTIVITY_VIEW_H
718d3e65eSAxel Dörfler
818d3e65eSAxel Dörfler
951591198SAxel Dörfler#include <map>
1051591198SAxel Dörfler
11d4216445SAxel Dörfler#include <Locker.h>
12bb7cffd0SPhilippe Saint-Pierre#include <Message.h>
1318d3e65eSAxel Dörfler#include <ObjectList.h>
14d4216445SAxel Dörfler#include <View.h>
1518d3e65eSAxel Dörfler
1618d3e65eSAxel Dörfler#include "CircularBuffer.h"
1751591198SAxel Dörfler#include "DataSource.h"
1818d3e65eSAxel Dörfler
195b0cd987SJohn Scipione
203a4a2141SAxel Dörflerclass BBitmap;
2118d3e65eSAxel Dörflerclass BMessageRunner;
2251591198SAxel Dörflerclass Scale;
2374158bb4SFrançois Revolclass SystemInfoHandler;
248e26f387SAxel Dörflerclass ViewHistory;
2518d3e65eSAxel Dörflerstruct data_item;
2618d3e65eSAxel Dörfler
2718d3e65eSAxel Dörflerclass DataHistory {
2818d3e65eSAxel Dörflerpublic:
2918d3e65eSAxel Dörfler						DataHistory(bigtime_t memorize, bigtime_t interval);
3018d3e65eSAxel Dörfler						~DataHistory();
3118d3e65eSAxel Dörfler
3218d3e65eSAxel Dörfler			void		AddValue(bigtime_t time, int64 value);
3318d3e65eSAxel Dörfler
3418d3e65eSAxel Dörfler			int64		ValueAt(bigtime_t time);
3518d3e65eSAxel Dörfler			int64		MaximumValue() const;
3618d3e65eSAxel Dörfler			int64		MinimumValue() const;
3718d3e65eSAxel Dörfler			bigtime_t	Start() const;
3818d3e65eSAxel Dörfler			bigtime_t	End() const;
3918d3e65eSAxel Dörfler
4018d3e65eSAxel Dörfler			void		SetRefreshInterval(bigtime_t interval);
4151591198SAxel Dörfler			void		SetScale(Scale* scale);
4218d3e65eSAxel Dörfler
4318d3e65eSAxel Dörflerprivate:
4418d3e65eSAxel Dörfler	CircularBuffer<data_item> fBuffer;
4518d3e65eSAxel Dörfler	int64				fMinimumValue;
4618d3e65eSAxel Dörfler	int64				fMaximumValue;
4718d3e65eSAxel Dörfler	bigtime_t			fRefreshInterval;
4818d3e65eSAxel Dörfler	int32				fLastIndex;
4951591198SAxel Dörfler	Scale*				fScale;
5018d3e65eSAxel Dörfler};
5118d3e65eSAxel Dörfler
5218d3e65eSAxel Dörfler
5318d3e65eSAxel Dörflerclass ActivityView : public BView {
5418d3e65eSAxel Dörflerpublic:
5518d3e65eSAxel Dörfler						ActivityView(BRect frame, const char* name,
563a4a2141SAxel Dörfler							const BMessage* settings, uint32 resizingMode);
573a4a2141SAxel Dörfler						ActivityView(const char* name,
583a4a2141SAxel Dörfler							const BMessage* settings);
5918d3e65eSAxel Dörfler						ActivityView(BMessage* archive);
6018d3e65eSAxel Dörfler	virtual				~ActivityView();
6118d3e65eSAxel Dörfler
6218d3e65eSAxel Dörfler	virtual status_t	Archive(BMessage* into, bool deep = true) const;
6318d3e65eSAxel Dörfler	static	BArchivable* Instantiate(BMessage* archive);
6418d3e65eSAxel Dörfler
6518d3e65eSAxel Dörfler			status_t	SaveState(BMessage& state) const;
6618d3e65eSAxel Dörfler
67dc1ffc79SFrançois Revol#ifdef __HAIKU__
68065e8de0SAxel Dörfler			BLayoutItem* CreateHistoryLayoutItem();
69065e8de0SAxel Dörfler			BLayoutItem* CreateLegendLayoutItem();
70dc1ffc79SFrançois Revol#endif
71065e8de0SAxel Dörfler
723a4a2141SAxel Dörfler			DataSource*	FindDataSource(const DataSource* source);
739c89c450SAxel Dörfler			status_t	AddDataSource(const DataSource* source,
749c89c450SAxel Dörfler							const BMessage* state = NULL);
753a4a2141SAxel Dörfler			status_t	RemoveDataSource(const DataSource* source);
7618d3e65eSAxel Dörfler			void		RemoveAllDataSources();
7718d3e65eSAxel Dörfler
781bb87bd0SAxel Dörfler			bigtime_t	RefreshInterval() const
79077c84ebSPawel Dziepak							{ return atomic_get64((int64*)&fRefreshInterval); }
801bb87bd0SAxel Dörfler
8118d3e65eSAxel Dörflerprotected:
8218d3e65eSAxel Dörfler	virtual	void		AttachedToWindow();
8318d3e65eSAxel Dörfler	virtual	void		DetachedFromWindow();
8418d3e65eSAxel Dörfler
85cbd4bc95SFrançois Revol#ifdef __HAIKU__
863a4a2141SAxel Dörfler	virtual	BSize		MinSize();
87cbd4bc95SFrançois Revol#endif
883a4a2141SAxel Dörfler
8918d3e65eSAxel Dörfler	virtual void		FrameResized(float width, float height);
9018d3e65eSAxel Dörfler	virtual void		MouseDown(BPoint where);
9151591198SAxel Dörfler	virtual void		MouseUp(BPoint where);
9218d3e65eSAxel Dörfler	virtual void		MouseMoved(BPoint where, uint32 transit,
9318d3e65eSAxel Dörfler							const BMessage* dragMessage);
9418d3e65eSAxel Dörfler
9518d3e65eSAxel Dörfler	virtual void		MessageReceived(BMessage* message);
9618d3e65eSAxel Dörfler
9718d3e65eSAxel Dörfler	virtual void		Draw(BRect updateRect);
9818d3e65eSAxel Dörfler
9918d3e65eSAxel Dörflerprivate:
10018d3e65eSAxel Dörfler			void		_Init(const BMessage* settings);
10151591198SAxel Dörfler			::Scale*	_ScaleFor(scale_type type);
10218d3e65eSAxel Dörfler			void		_Refresh();
103d4216445SAxel Dörfler	static	status_t	_RefreshThread(void* self);
1049c89c450SAxel Dörfler			void		_UpdateOffscreenBitmap();
1059c89c450SAxel Dörfler			BView*		_OffscreenView();
106065e8de0SAxel Dörfler			void		_UpdateFrame();
1073a4a2141SAxel Dörfler			BRect		_HistoryFrame() const;
108065e8de0SAxel Dörfler			float		_LegendHeight() const;
1093a4a2141SAxel Dörfler			BRect		_LegendFrame() const;
1103a4a2141SAxel Dörfler			BRect		_LegendFrameAt(BRect frame, int32 index) const;
1111201d52eSAxel Dörfler			BRect		_LegendColorFrameAt(BRect frame, int32 index) const;
11218d3e65eSAxel Dörfler			float		_PositionForValue(DataSource* source,
11318d3e65eSAxel Dörfler							DataHistory* values, int64 value);
114ed1b2753SStephan Aßmus			void		_DrawHistory(bool drawBackground);
1151bb87bd0SAxel Dörfler			void		_UpdateResolution(int32 resolution,
1161bb87bd0SAxel Dörfler							bool broadcast = true);
11718d3e65eSAxel Dörfler
11851591198SAxel Dörflerprivate:
119065e8de0SAxel Dörfler	class HistoryLayoutItem;
120065e8de0SAxel Dörfler	class LegendLayoutItem;
121065e8de0SAxel Dörfler
122065e8de0SAxel Dörfler	friend class HistoryLayoutItem;
123065e8de0SAxel Dörfler	friend class LegendLayoutItem;
124065e8de0SAxel Dörfler
1259c89c450SAxel Dörfler	rgb_color			fHistoryBackgroundColor;
1269c89c450SAxel Dörfler	rgb_color			fLegendBackgroundColor;
1273a4a2141SAxel Dörfler	BBitmap*			fOffscreen;
128dc1ffc79SFrançois Revol#ifdef __HAIKU__
129065e8de0SAxel Dörfler	BLayoutItem*		fHistoryLayoutItem;
130065e8de0SAxel Dörfler	BLayoutItem*		fLegendLayoutItem;
131dc1ffc79SFrançois Revol#endif
132bb7cffd0SPhilippe Saint-Pierre
133d4216445SAxel Dörfler	mutable BLocker		fSourcesLock;
13418d3e65eSAxel Dörfler	BObjectList<DataSource> fSources;
13518d3e65eSAxel Dörfler	BObjectList<DataHistory> fValues;
1368e26f387SAxel Dörfler	BObjectList<ViewHistory> fViewValues;
137d4216445SAxel Dörfler	thread_id			fRefreshThread;
138d4216445SAxel Dörfler	sem_id				fRefreshSem;
13918d3e65eSAxel Dörfler	bigtime_t			fRefreshInterval;
14018d3e65eSAxel Dörfler	bigtime_t			fLastRefresh;
14118d3e65eSAxel Dörfler	int32				fDrawResolution;
1423a4a2141SAxel Dörfler	bool				fShowLegend;
14351591198SAxel Dörfler	bool				fZooming;
14451591198SAxel Dörfler	BPoint				fZoomPoint;
14551591198SAxel Dörfler	int32				fOriginalResolution;
14674158bb4SFrançois Revol	SystemInfoHandler*	fSystemInfoHandler;
14751591198SAxel Dörfler	std::map<scale_type, ::Scale*> fScales;
14818d3e65eSAxel Dörfler};
14918d3e65eSAxel Dörfler
15018d3e65eSAxel Dörfler#endif	// ACTIVITY_VIEW_H
151