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