xref: /haiku/src/kits/tracker/VirtualDirectoryWindow.cpp (revision 95c9effd68127df2dce202d5e254a7c86560010a)
1 /*
2  * Copyright 2013-2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		John Scipione, jscipione@gmail.com
7  *		Ingo Weinhold, ingo_weinhold@gmx.de
8  */
9 /*
10 Open Tracker License
11 
12 Terms and Conditions
13 
14 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
15 
16 Permission is hereby granted, free of charge, to any person obtaining a copy of
17 this software and associated documentation files (the "Software"), to deal in
18 the Software without restriction, including without limitation the rights to
19 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
20 of the Software, and to permit persons to whom the Software is furnished to do
21 so, subject to the following conditions:
22 
23 The above copyright notice and this permission notice applies to all licensees
24 and shall be included in all copies or substantial portions of the Software.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
30 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
31 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 
33 Except as contained in this notice, the name of Be Incorporated shall not be
34 used in advertising or otherwise to promote the sale, use or other dealings in
35 this Software without prior written authorization from Be Incorporated.
36 
37 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
38 of Be Incorporated in the United States and other countries. Other brand product
39 names are registered trademarks or trademarks of their respective holders.
40 All rights reserved.
41 */
42 
43 
44 #include "VirtualDirectoryWindow.h"
45 
46 #include <Catalog.h>
47 #include <Locale.h>
48 
49 #include <AutoLocker.h>
50 
51 #include "Commands.h"
52 #include "VirtualDirectoryManager.h"
53 #include "VirtualDirectoryPoseView.h"
54 
55 
56 #undef B_TRANSLATION_CONTEXT
57 #define B_TRANSLATION_CONTEXT "VirtualDirectoryWindow"
58 
59 
60 namespace BPrivate {
61 
62 //	#pragma mark - VirtualDirectoryWindow
63 
64 
65 VirtualDirectoryWindow::VirtualDirectoryWindow(LockingList<BWindow>* windowList,
66 	uint32 containerWindowFlags, window_look look, window_feel feel,
67 	uint32 flags, uint32 workspace)
68 	:
69 	BContainerWindow(windowList, containerWindowFlags, look, feel, flags,
70 		workspace)
71 {
72 }
73 
74 
75 void
76 VirtualDirectoryWindow::CreatePoseView(Model* model)
77 {
78 	fPoseView = NewPoseView(model, kListMode);
79 	if (fPoseView == NULL)
80 		return;
81 
82 	fBorderedView->GroupLayout()->AddView(fPoseView);
83 	fBorderedView->EnableBorderHighlight(false);
84 	fBorderedView->GroupLayout()->SetInsets(0, 0, 1, 1);
85 }
86 
87 
88 BPoseView*
89 VirtualDirectoryWindow::NewPoseView(Model* model, uint32 viewMode)
90 {
91 	// If the model (or rather the entry_ref to it) came from another
92 	// application, it may refer to a subdirectory we cannot use directly. The
93 	// manager resolves the given refs to new ones, if necessary.
94 	VirtualDirectoryManager* manager = VirtualDirectoryManager::Instance();
95 	if (manager == NULL)
96 		return NULL;
97 
98 	{
99 		AutoLocker<VirtualDirectoryManager> managerLocker(manager);
100 		BStringList directoryPaths;
101 		node_ref nodeRef;
102 		entry_ref entryRef;
103 		if (manager->ResolveDirectoryPaths(*model->NodeRef(),
104 				*model->EntryRef(), directoryPaths, &nodeRef, &entryRef)
105 				!= B_OK) {
106 			return NULL;
107 		}
108 
109 		if (nodeRef != *model->NodeRef()) {
110 			// Indeed a new file. Create a new model.
111 			Model* newModel = new(std::nothrow) Model(&entryRef);
112 			if (newModel == NULL || newModel->InitCheck() != B_OK) {
113 				delete newModel;
114 				return NULL;
115 			}
116 
117 			delete model;
118 			model = newModel;
119 		}
120 	}
121 
122 	return new VirtualDirectoryPoseView(model);
123 }
124 
125 
126 VirtualDirectoryPoseView*
127 VirtualDirectoryWindow::PoseView() const
128 {
129 	return static_cast<VirtualDirectoryPoseView*>(fPoseView);
130 }
131 
132 
133 void
134 VirtualDirectoryWindow::AddWindowMenu(BMenu* menu)
135 {
136 	BMenuItem* item;
137 	BMessage* message;
138 
139 	BMenu* listViewMenu = new BMenu(B_TRANSLATE("List view"));
140 
141 	message = new BMessage(kListMode);
142 	message->AddInt32("icon_size", B_MINI_ICON);
143 	item = new BMenuItem(listViewMenu, message);
144 	item->SetShortcut('3', B_COMMAND_KEY);
145 	item->SetTarget(PoseView());
146 	menu->AddItem(item);
147 
148 	message = new BMessage(kListMode);
149 	message->AddInt32("icon_size", B_MINI_ICON);
150 	item = new BMenuItem(B_TRANSLATE("Mini"), message);
151 	item->SetTarget(PoseView());
152 	listViewMenu->AddItem(item);
153 
154 	message = new BMessage(kListMode);
155 	message->AddInt32("icon_size", B_LARGE_ICON);
156 	item = new BMenuItem(B_TRANSLATE("Large"), message);
157 	item->SetTarget(PoseView());
158 	listViewMenu->AddItem(item);
159 
160 	listViewMenu->SetTargetForItems(PoseView());
161 
162 	menu->AddSeparatorItem();
163 
164 	item = new BMenuItem(B_TRANSLATE("Resize to fit"),
165 		new BMessage(kResizeToFit), 'Y');
166 	item->SetTarget(this);
167 	menu->AddItem(item);
168 
169 	item = new BMenuItem(B_TRANSLATE("Select" B_UTF8_ELLIPSIS),
170 		new BMessage(kShowSelectionWindow), 'A', B_SHIFT_KEY);
171 	item->SetTarget(PoseView());
172 	menu->AddItem(item);
173 
174 	item = new BMenuItem(B_TRANSLATE("Select all"),
175 		new BMessage(B_SELECT_ALL), 'A');
176 	item->SetTarget(PoseView());
177 	menu->AddItem(item);
178 
179 	item = new BMenuItem(B_TRANSLATE("Invert selection"),
180 		new BMessage(kInvertSelection), 'S');
181 	item->SetTarget(PoseView());
182 	menu->AddItem(item);
183 
184 	item = new BMenuItem(B_TRANSLATE("Open parent"),
185 		new BMessage(kOpenParentDir), B_UP_ARROW);
186 	item->SetTarget(PoseView());
187 	menu->AddItem(item);
188 
189 	item = new BMenuItem(B_TRANSLATE("Close"),
190 		new BMessage(B_QUIT_REQUESTED), 'W');
191 	item->SetTarget(this);
192 	menu->AddItem(item);
193 }
194 
195 
196 void
197 VirtualDirectoryWindow::AddWindowContextMenus(BMenu* menu)
198 {
199 	BMenuItem* resizeItem = new BMenuItem(B_TRANSLATE("Resize to fit"),
200 		new BMessage(kResizeToFit), 'Y');
201 	menu->AddItem(resizeItem);
202 	menu->AddItem(new BMenuItem(B_TRANSLATE("Select" B_UTF8_ELLIPSIS),
203 		new BMessage(kShowSelectionWindow), 'A', B_SHIFT_KEY));
204 	menu->AddItem(new BMenuItem(B_TRANSLATE("Select all"),
205 		new BMessage(B_SELECT_ALL), 'A'));
206 	BMenuItem* closeItem = new BMenuItem(B_TRANSLATE("Close"),
207 		new BMessage(B_QUIT_REQUESTED), 'W');
208 	menu->AddItem(closeItem);
209 	// target items as needed
210 	menu->SetTargetForItems(PoseView());
211 	closeItem->SetTarget(this);
212 	resizeItem->SetTarget(this);
213 }
214 
215 } // namespace BPrivate
216