xref: /haiku/src/apps/diskprobe/OpenWindow.cpp (revision 67bce78b48ed6d01b5a8eef89f5694c372b7e0a1)
1 /*
2 ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the OpenBeOS License.
4 */
5 
6 
7 #include "OpenWindow.h"
8 #include "DiskProbe.h"
9 
10 #include <Application.h>
11 #include <Screen.h>
12 #include <MenuField.h>
13 #include <PopUpMenu.h>
14 #include <MenuItem.h>
15 #include <Button.h>
16 #include <Directory.h>
17 #include <Entry.h>
18 #include <Path.h>
19 
20 
21 static const uint32 kMsgProbeFile = 'prDv';
22 static const uint32 kMsgProbeDevice = 'prFl';
23 static const uint32 kMsgCancel = 'Canc';
24 
25 
26 OpenWindow::OpenWindow()
27 	: BWindow(BRect(0, 0, 350, 100), "DiskProbe", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
28 {
29 	BView *view = new BView(Bounds(), B_EMPTY_STRING, B_FOLLOW_ALL, B_WILL_DRAW);
30 	view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
31 	AddChild(view);
32 
33 	fDevicesMenu = new BPopUpMenu("devices");
34 	CollectDevices(fDevicesMenu);
35 	if (BMenuItem *item = fDevicesMenu->ItemAt(0))
36 		item->SetMarked(true);
37 
38 	BRect rect = Bounds().InsetByCopy(8, 8);
39 	BMenuField *field = new BMenuField(rect, "devices", "Examine Device:", fDevicesMenu);
40 	field->SetDivider(field->StringWidth(field->Label()) + 8);
41 	field->ResizeToPreferred();
42 	view->AddChild(field);
43 
44 	BButton *button = new BButton(BRect(10, 10, 20, 20), "file", "Probe Device", new BMessage(kMsgProbeDevice));
45 	button->ResizeToPreferred();
46 	rect = button->Bounds();
47 	button->MoveTo(Bounds().Width() - 8 - rect.Width(), Bounds().Height() - 8 - rect.Height());
48 	view->AddChild(button);
49 
50 	// MakeDefault() may change the size and location of the button
51 	rect = button->Frame();
52 	button->MakeDefault(true);
53 
54 	button = new BButton(rect, "file", "Probe File" B_UTF8_ELLIPSIS, new BMessage(kMsgProbeFile));
55 	button->ResizeToPreferred();
56 	button->MoveBy(-button->Bounds().Width() - 8, 0);
57 	view->AddChild(button);
58 
59 	button = new BButton(button->Frame(), "cancel", "Cancel", new BMessage(kMsgCancel));
60 	button->ResizeToPreferred();
61 	button->MoveBy(-button->Bounds().Width() - 8, 0);
62 	view->AddChild(button);
63 
64 	BScreen screen;
65 	MoveTo((screen.Frame().Width() - Frame().Width()) / 2,
66 		(screen.Frame().Height() - Frame().Height()) / 2);
67 }
68 
69 
70 OpenWindow::~OpenWindow()
71 {
72 }
73 
74 
75 void
76 OpenWindow::MessageReceived(BMessage *message)
77 {
78 	switch (message->what) {
79 		case kMsgProbeDevice: {
80 			BMenuItem *item = fDevicesMenu->FindMarked();
81 			if (item == NULL)
82 				break;
83 
84 			be_app_messenger.SendMessage(item->Message());
85 			PostMessage(B_QUIT_REQUESTED);
86 			break;
87 		}
88 
89 		case kMsgProbeFile:
90 			be_app_messenger.SendMessage(kMsgOpenFilePanel);
91 			PostMessage(B_QUIT_REQUESTED);
92 			break;
93 
94 		case kMsgCancel:
95 			if (QuitRequested())
96 				Quit();
97 			break;
98 
99 		case B_SIMPLE_DATA: {
100 			// if it's a file drop, open it
101 			entry_ref ref;
102 			if (message->FindRef("refs", 0, &ref) == B_OK) {
103 				BMessage openMessage(*message);
104 				openMessage.what = B_REFS_RECEIVED;
105 
106 				be_app_messenger.SendMessage(&openMessage);
107 				PostMessage(B_QUIT_REQUESTED);
108 			}
109 			break;
110 		}
111 
112 		default:
113 			BWindow::MessageReceived(message);
114 			break;
115 	}
116 }
117 
118 
119 bool
120 OpenWindow::QuitRequested()
121 {
122 	be_app_messenger.SendMessage(kMsgOpenWindowClosed);
123 	return true;
124 }
125 
126 
127 void
128 OpenWindow::CollectDevices(BMenu *menu, BEntry *startEntry)
129 {
130 	BDirectory directory;
131 	if (startEntry != NULL)
132 		directory.SetTo(startEntry);
133 	else
134 		directory.SetTo("/dev/disk");
135 
136 	BEntry entry;
137 	while (directory.GetNextEntry(&entry) == B_OK) {
138 		if (entry.IsDirectory()) {
139 			CollectDevices(menu, &entry);
140 			continue;
141 		}
142 
143 		entry_ref ref;
144 		if (entry.GetRef(&ref) != B_OK)
145 			continue;
146 
147 		BPath path;
148 		if (entry.GetPath(&path) != B_OK)
149 			continue;
150 
151 		BMessage *message = new BMessage(B_REFS_RECEIVED);
152 		message->AddRef("refs", &ref);
153 
154 		menu->AddItem(new BMenuItem(path.Path(), message));
155 	}
156 }
157 
158