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