1/*
2 * Copyright 2008, Axel D��rfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef ACTIVITY_VIEW_H
6#define ACTIVITY_VIEW_H
7
8
9#include <map>
10
11#include <Locker.h>
12#include <Message.h>
13#include <ObjectList.h>
14#include <View.h>
15
16#include "CircularBuffer.h"
17#include "DataSource.h"
18
19
20class BBitmap;
21class BMessageRunner;
22class Scale;
23class SystemInfoHandler;
24class ViewHistory;
25struct data_item;
26
27class DataHistory {
28public:
29						DataHistory(bigtime_t memorize, bigtime_t interval);
30						~DataHistory();
31
32			void		AddValue(bigtime_t time, int64 value);
33
34			int64		ValueAt(bigtime_t time);
35			int64		MaximumValue() const;
36			int64		MinimumValue() const;
37			bigtime_t	Start() const;
38			bigtime_t	End() const;
39
40			void		SetRefreshInterval(bigtime_t interval);
41			void		SetScale(Scale* scale);
42
43private:
44	CircularBuffer<data_item> fBuffer;
45	int64				fMinimumValue;
46	int64				fMaximumValue;
47	bigtime_t			fRefreshInterval;
48	int32				fLastIndex;
49	Scale*				fScale;
50};
51
52
53class ActivityView : public BView {
54public:
55						ActivityView(BRect frame, const char* name,
56							const BMessage* settings, uint32 resizingMode);
57						ActivityView(const char* name,
58							const BMessage* settings);
59						ActivityView(BMessage* archive);
60	virtual				~ActivityView();
61
62	virtual status_t	Archive(BMessage* into, bool deep = true) const;
63	static	BArchivable* Instantiate(BMessage* archive);
64
65			status_t	SaveState(BMessage& state) const;
66
67#ifdef __HAIKU__
68			BLayoutItem* CreateHistoryLayoutItem();
69			BLayoutItem* CreateLegendLayoutItem();
70#endif
71
72			DataSource*	FindDataSource(const DataSource* source);
73			status_t	AddDataSource(const DataSource* source,
74							const BMessage* state = NULL);
75			status_t	RemoveDataSource(const DataSource* source);
76			void		RemoveAllDataSources();
77
78			bigtime_t	RefreshInterval() const
79							{ return atomic_get64((int64*)&fRefreshInterval); }
80
81protected:
82	virtual	void		AttachedToWindow();
83	virtual	void		DetachedFromWindow();
84
85#ifdef __HAIKU__
86	virtual	BSize		MinSize();
87#endif
88
89	virtual void		FrameResized(float width, float height);
90	virtual void		MouseDown(BPoint where);
91	virtual void		MouseUp(BPoint where);
92	virtual void		MouseMoved(BPoint where, uint32 transit,
93							const BMessage* dragMessage);
94
95	virtual void		MessageReceived(BMessage* message);
96
97	virtual void		Draw(BRect updateRect);
98
99private:
100			void		_Init(const BMessage* settings);
101			::Scale*	_ScaleFor(scale_type type);
102			void		_Refresh();
103	static	status_t	_RefreshThread(void* self);
104			void		_UpdateOffscreenBitmap();
105			BView*		_OffscreenView();
106			void		_UpdateFrame();
107			BRect		_HistoryFrame() const;
108			float		_LegendHeight() const;
109			BRect		_LegendFrame() const;
110			BRect		_LegendFrameAt(BRect frame, int32 index) const;
111			BRect		_LegendColorFrameAt(BRect frame, int32 index) const;
112			float		_PositionForValue(DataSource* source,
113							DataHistory* values, int64 value);
114			void		_DrawHistory();
115			void		_UpdateResolution(int32 resolution,
116							bool broadcast = true);
117
118private:
119	class HistoryLayoutItem;
120	class LegendLayoutItem;
121
122	friend class HistoryLayoutItem;
123	friend class LegendLayoutItem;
124
125	rgb_color			fHistoryBackgroundColor;
126	rgb_color			fLegendBackgroundColor;
127	BBitmap*			fOffscreen;
128#ifdef __HAIKU__
129	BLayoutItem*		fHistoryLayoutItem;
130	BLayoutItem*		fLegendLayoutItem;
131#endif
132
133	mutable BLocker		fSourcesLock;
134	BObjectList<DataSource> fSources;
135	BObjectList<DataHistory> fValues;
136	BObjectList<ViewHistory> fViewValues;
137	thread_id			fRefreshThread;
138	sem_id				fRefreshSem;
139	bigtime_t			fRefreshInterval;
140	bigtime_t			fLastRefresh;
141	int32				fDrawResolution;
142	bool				fShowLegend;
143	bool				fZooming;
144	BPoint				fZoomPoint;
145	int32				fOriginalResolution;
146	SystemInfoHandler*	fSystemInfoHandler;
147	std::map<scale_type, ::Scale*> fScales;
148};
149
150#endif	// ACTIVITY_VIEW_H
151