xref: /haiku/src/apps/cortex/DormantNodeView/DormantNodeView.cpp (revision 541ff51a6ef4c47f8ab105ba6ff895cdbba83aca)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // DormantNodeView.cpp
33 
34 #include "DormantNodeView.h"
35 // DormantNodeView
36 #include "DormantNodeWindow.h"
37 #include "DormantNodeListItem.h"
38 // InfoWindow
39 #include "InfoWindowManager.h"
40 
41 // Interface Kit
42 #include <Deskbar.h>
43 #include <Region.h>
44 #include <Screen.h>
45 #include <ScrollBar.h>
46 // Media Kit
47 #include <MediaRoster.h>
48 // Storage Kit
49 #include <Mime.h>
50 
51 __USE_CORTEX_NAMESPACE
52 
53 #include <Debug.h>
54 #define D_ALLOC(x) //PRINT (x)		// ctor/dtor
55 #define D_HOOK(x) //PRINT (x)		// BListView impl.
56 #define D_MESSAGE(x) //PRINT (x)	// MessageReceived()
57 #define D_INTERNAL(x) //PRINT (x)	// internal operations
58 
59 // -------------------------------------------------------- //
60 // ctor/dtor (public)
61 // -------------------------------------------------------- //
62 
63 DormantNodeView::DormantNodeView(
64 	BRect frame,
65 	const char *name,
66 	uint32 resizeMode)
67 	: BListView(frame, name, B_SINGLE_SELECTION_LIST, resizeMode),
68 	  m_lastItemUnder(0) {
69 	D_ALLOC(("DormantNodeView::DormantNodeView()\n"));
70 
71 }
72 
73 DormantNodeView::~DormantNodeView() {
74 	D_ALLOC(("DormantNodeView::~DormantNodeView()\n"));
75 
76 }
77 
78 // -------------------------------------------------------- //
79 // BListView impl. (public)
80 // -------------------------------------------------------- //
81 
82 void DormantNodeView::AttachedToWindow() {
83 	D_HOOK(("DormantNodeView::AttachedToWindow()\n"));
84 
85 	// populate the list
86 	_populateList();
87 
88 	// Start watching the MediaRoster for flavor changes
89 	BMediaRoster *roster = BMediaRoster::CurrentRoster();
90 	if (roster) {
91 		BMessenger messenger(this, Window());
92 		roster->StartWatching(messenger, B_MEDIA_FLAVORS_CHANGED);
93 	}
94 }
95 
96 void DormantNodeView::DetachedFromWindow() {
97 	D_HOOK(("DormantNodeView::DetachedFromWindow()\n"));
98 
99 	// delete the lists contents
100 	_freeList();
101 
102 	// Stop watching the MediaRoster for flavor changes
103 	BMediaRoster *roster = BMediaRoster::CurrentRoster();
104 	if (roster) {
105 		BMessenger messenger(this, Window());
106 		roster->StopWatching(messenger, B_MEDIA_FLAVORS_CHANGED);
107 	}
108 }
109 
110 void DormantNodeView::GetPreferredSize(
111 	float* width,
112 	float* height) {
113 	D_HOOK(("DormantNodeView::GetPreferredSize()\n"));
114 
115 	// calculate the accumulated size of all list items
116 	*width = 0;
117 	*height = 0;
118 	for (int32 i = 0; i < CountItems(); i++) {
119 		DormantNodeListItem *item;
120 		item = dynamic_cast<DormantNodeListItem *>(ItemAt(i));
121 		if (item) {
122 			BRect r = item->getRealFrame(be_plain_font);
123 			if (r.Width() > *width) {
124 				*width = r.Width();
125 			}
126 			*height += r.Height() + 1.0;
127 		}
128 	}
129 }
130 
131 void DormantNodeView::MessageReceived(
132 	BMessage *message) {
133 	D_MESSAGE(("DormantNodeView::MessageReceived()\n"));
134 
135 	switch (message->what) {
136 		case B_MEDIA_FLAVORS_CHANGED: {
137 			D_MESSAGE((" -> B_MEDIA_FLAVORS_CHANGED\n"));
138 
139 			// init & re-populate the list
140 			int32 addOnID = 0;
141 			if (message->FindInt32("be:addon_id", &addOnID) != B_OK) {
142 				D_MESSAGE((" -> messages doesn't contain 'be:addon_id'!\n"));
143 				return;
144 			}
145 			_updateList(addOnID);
146 			break;
147 		}
148 		case InfoWindowManager::M_INFO_WINDOW_REQUESTED: {
149 			D_MESSAGE((" -> InfoWindowManager::M_INFO_WINDOW_REQUESTED)\n"));
150 
151 			DormantNodeListItem *item;
152 			item = dynamic_cast<DormantNodeListItem *>(ItemAt(CurrentSelection()));
153 			if (item) {
154 				InfoWindowManager *manager = InfoWindowManager::Instance();
155 				if (manager && manager->Lock()) {
156 					manager->openWindowFor(item->info());
157 					manager->Unlock();
158 				}
159 			}
160 			break;
161 		}
162 		default: {
163 			_inherited::MessageReceived(message);
164 		}
165 	}
166 }
167 
168 void DormantNodeView::MouseDown(
169 	BPoint point) {
170 	D_HOOK(("DormantNodeView::MouseDown()\n"));
171 
172 	BMessage* message = Window()->CurrentMessage();
173 	int32 buttons = message->FindInt32("buttons");
174 	if (buttons == B_SECONDARY_MOUSE_BUTTON) {
175 		int32 index;
176 		if ((index = IndexOf(point)) >= 0) {
177 			DormantNodeListItem *item = dynamic_cast<DormantNodeListItem *>(ItemAt(index));
178 			if (item) {
179 				Select(index);
180 				BRect r = item->getRealFrame(be_plain_font);
181 				if (r.Contains(point)) {
182 					item->showContextMenu(point, this);
183 				}
184 			}
185 		}
186 	}
187 
188 	_inherited::MouseDown(point);
189 }
190 
191 void DormantNodeView::MouseMoved(
192 	BPoint point,
193 	uint32 transit,
194 	const BMessage *message) {
195 	D_HOOK(("DormantNodeView::MouseMoved()\n"));
196 
197 	int32 index;
198 	if (!message && ((index = IndexOf(point)) >= 0)) {
199 		DormantNodeListItem *item = dynamic_cast<DormantNodeListItem *>(ItemAt(index));
200 		DormantNodeListItem *last = dynamic_cast<DormantNodeListItem *>(m_lastItemUnder);
201 		BRect r = item->getRealFrame(be_plain_font);
202 		if (item && r.Contains(point)) {
203 			if (item != last) {
204 				if (last)
205 					last->MouseOver(this, point, B_EXITED_VIEW);
206 				item->MouseOver(this, point, B_ENTERED_VIEW);
207 				m_lastItemUnder = item;
208 			}
209 			else {
210 				item->MouseOver(this, point, B_INSIDE_VIEW);
211 			}
212 		}
213 		else if (last) {
214 			last->MouseOver(this, point, B_EXITED_VIEW);
215 		}
216 	}
217 
218 	_inherited::MouseMoved(point, transit, message);
219 }
220 
221 bool DormantNodeView::InitiateDrag(
222 	BPoint point,
223 	int32 index,
224 	bool wasSelected) {
225 	D_HOOK(("DormantNodeView::InitiateDrag()\n"));
226 
227 	DormantNodeListItem *item = dynamic_cast<DormantNodeListItem *>(ItemAt(CurrentSelection()));
228 	if (item) {
229 		BMessage dragMsg(M_INSTANTIATE_NODE);
230 		dragMsg.AddData("which", B_RAW_TYPE,
231 						reinterpret_cast<const void *>(&item->info()),
232 						sizeof(item->info()));
233 		point -= ItemFrame(index).LeftTop();
234 		DragMessage(&dragMsg, item->getDragBitmap(), B_OP_ALPHA, point);
235 		return true;
236 	}
237 	return false;
238 }
239 
240 // -------------------------------------------------------- //
241 // internal operations (private)
242 // -------------------------------------------------------- //
243 
244 void DormantNodeView::_populateList() {
245 	D_INTERNAL(("DormantNodeView::_populateList()\n"));
246 
247 	// init the resizable node-info buffer
248 	BMediaRoster *roster = BMediaRoster::CurrentRoster();
249 	const int32 bufferInc = 64;
250 	int32 bufferSize = bufferInc;
251 	dormant_node_info *infoBuffer = new dormant_node_info[bufferSize];
252 	int32 numNodes;
253 
254 	// fill the buffer
255 	while (true) {
256 		numNodes = bufferSize;
257 		status_t error = roster->GetDormantNodes(infoBuffer, &numNodes);
258 		if (error) {
259 			return;
260 		}
261 		if (numNodes < bufferSize) {
262 			break;
263 		}
264 
265 		// reallocate buffer & try again
266 		delete [] infoBuffer;
267 		bufferSize += bufferInc;
268 		infoBuffer = new dormant_node_info[bufferSize];
269 	}
270 
271 	// populate the list
272 	for (int32 i = 0; i < numNodes; i++) {
273 		DormantNodeListItem *item = new DormantNodeListItem(infoBuffer[i]);
274 		AddItem(item);
275 	}
276 	SortItems(compareName);
277 }
278 
279 void DormantNodeView::_freeList() {
280 	D_HOOK(("DormantNodeView::_freeList()\n"));
281 
282 	// remove and delete all items in the list
283 	while (CountItems() > 0) {
284 		BListItem *item = ItemAt(0);
285 		if (RemoveItem(item)) {
286 			delete item;
287 		}
288 	}
289 }
290 
291 void DormantNodeView::_updateList(
292 	int32 addOnID) {
293 	D_INTERNAL(("DormantNodeView::_updateList(%ld)\n", addOnID));
294 
295 	// init the resizable node-info buffer
296 	BMediaRoster *roster = BMediaRoster::CurrentRoster();
297 	const int32 bufferInc = 64;
298 	int32 bufferSize = bufferInc;
299 	dormant_node_info *infoBuffer = new dormant_node_info[bufferSize];
300 	int32 numNodes;
301 
302 	// fill the buffer
303 	while (true) {
304 		numNodes = bufferSize;
305 		status_t error = roster->GetDormantNodes(infoBuffer, &numNodes);
306 		if (error) {
307 			return;
308 		}
309 		if (numNodes < bufferSize) {
310 			break;
311 		}
312 
313 		// reallocate buffer & try again
314 		delete [] infoBuffer;
315 		bufferSize += bufferInc;
316 		infoBuffer = new dormant_node_info[bufferSize];
317 	}
318 
319 	// sort the list by add-on id to avoid multiple searches through
320 	// the list
321 	SortItems(compareAddOnID);
322 
323 	// Remove all nodes managed by this add-on
324 	int32 start;
325 	for (start = 0; start < CountItems(); start++) {
326 		DormantNodeListItem *item = dynamic_cast<DormantNodeListItem *>(ItemAt(start));
327 		if (item && (item->info().addon == addOnID)) {
328 			break;
329 		}
330 	}
331 	int32 count = 0;
332 	for (int32 i = start; start < CountItems(); i++) {
333 		DormantNodeListItem *item = dynamic_cast<DormantNodeListItem *>(ItemAt(i));
334 		if (!item || (item->info().addon != addOnID)) {
335 			break;
336 		}
337 		count++;
338 	}
339 	RemoveItems(start, count);
340 
341 	// add the items
342 	for (int32 i = 0; i < numNodes; i++) {
343 		if (infoBuffer[i].addon != addOnID) {
344 			continue;
345 		}
346 		AddItem(new DormantNodeListItem(infoBuffer[i]));
347 	}
348 
349 	SortItems(compareName);
350 }
351 
352 // END -- DormantNodeView.cpp --
353