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