xref: /haiku/src/apps/activitymonitor/ActivityWindow.cpp (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
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 
6 
7 #include "ActivityWindow.h"
8 
9 #include <stdio.h>
10 
11 #include <Application.h>
12 #include <File.h>
13 #include <FindDirectory.h>
14 #include <Menu.h>
15 #include <MenuBar.h>
16 #include <MenuItem.h>
17 #include <Path.h>
18 #include <Roster.h>
19 
20 #include "ActivityMonitor.h"
21 #include "ActivityView.h"
22 
23 
24 ActivityWindow::ActivityWindow()
25 	: BWindow(BRect(100, 100, 500, 250), "ActivityMonitor", B_TITLED_WINDOW,
26 		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE)
27 {
28 	BMessage settings;
29 	_LoadSettings(settings);
30 
31 	BRect frame;
32 	if (settings.FindRect("window frame", &frame) == B_OK) {
33 		MoveTo(frame.LeftTop());
34 		ResizeTo(frame.Width(), frame.Height());
35 		frame.OffsetTo(B_ORIGIN);
36 	} else
37 		frame = Bounds();
38 
39 	// create GUI
40 
41 	BMenuBar* menuBar = new BMenuBar(Bounds(), "menu");
42 	AddChild(menuBar);
43 
44 	frame.top = menuBar->Frame().bottom;
45 
46 	BView* top = new BView(frame, NULL, B_FOLLOW_ALL, B_WILL_DRAW);
47 	top->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
48 	AddChild(top);
49 
50 	fActivityView = new ActivityView(top->Bounds().InsetByCopy(10, 10),
51 		"ActivityMonitor", settings, B_FOLLOW_ALL);
52 	top->AddChild(fActivityView);
53 
54 	// add menu
55 
56 	// "File" menu
57 	BMenu* menu = new BMenu("File");
58 	BMenuItem* item;
59 
60 	menu->AddItem(item = new BMenuItem("About ActivityMonitor" B_UTF8_ELLIPSIS,
61 		new BMessage(B_ABOUT_REQUESTED)));
62 	menu->AddSeparatorItem();
63 
64 	menu->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q'));
65 	menu->SetTargetForItems(this);
66 	item->SetTarget(be_app);
67 	menuBar->AddItem(menu);
68 }
69 
70 
71 ActivityWindow::~ActivityWindow()
72 {
73 }
74 
75 
76 status_t
77 ActivityWindow::_OpenSettings(BFile& file, uint32 mode)
78 {
79 	BPath path;
80 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
81 		return B_ERROR;
82 
83 	path.Append("ActivityMonitor settings");
84 
85 	return file.SetTo(path.Path(), mode);
86 }
87 
88 
89 status_t
90 ActivityWindow::_LoadSettings(BMessage& settings)
91 {
92 	BFile file;
93 	status_t status = _OpenSettings(file, B_READ_ONLY);
94 	if (status < B_OK)
95 		return status;
96 
97 	return settings.Unflatten(&file);
98 }
99 
100 
101 status_t
102 ActivityWindow::_SaveSettings()
103 {
104 	BFile file;
105 	status_t status = _OpenSettings(file, B_WRITE_ONLY | B_CREATE_FILE
106 		| B_ERASE_FILE);
107 	if (status < B_OK)
108 		return status;
109 
110 	BMessage settings('actm');
111 	status = settings.AddRect("window frame", Frame());
112 	if (status == B_OK)
113 		status = settings.Flatten(&file);
114 
115 	return status;
116 }
117 
118 
119 void
120 ActivityWindow::_MessageDropped(BMessage* message)
121 {
122 	entry_ref ref;
123 	if (message->FindRef("refs", &ref) != B_OK) {
124 		// TODO: If app, then launch it, and add ActivityView for this one?
125 	}
126 }
127 
128 
129 void
130 ActivityWindow::MessageReceived(BMessage* message)
131 {
132 	if (message->WasDropped()) {
133 		_MessageDropped(message);
134 		return;
135 	}
136 
137 	switch (message->what) {
138 		case B_REFS_RECEIVED:
139 		case B_SIMPLE_DATA:
140 			_MessageDropped(message);
141 			break;
142 
143 		default:
144 			BWindow::MessageReceived(message);
145 			break;
146 	}
147 }
148 
149 
150 bool
151 ActivityWindow::QuitRequested()
152 {
153 	_SaveSettings();
154 	be_app->PostMessage(B_QUIT_REQUESTED);
155 	return true;
156 }
157