xref: /haiku/src/kits/tracker/VirtualDirectoryWindow.cpp (revision 445d4fd926c569e7b9ae28017da86280aaecbae2)
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 
138 	item = new BMenuItem(B_TRANSLATE("Resize to fit"),
139 		new BMessage(kResizeToFit), 'Y');
140 	item->SetTarget(this);
141 	menu->AddItem(item);
142 
143 	item = new BMenuItem(B_TRANSLATE("Select" B_UTF8_ELLIPSIS),
144 		new BMessage(kShowSelectionWindow), 'A', B_SHIFT_KEY);
145 	item->SetTarget(PoseView());
146 	menu->AddItem(item);
147 
148 	item = new BMenuItem(B_TRANSLATE("Select all"),
149 		new BMessage(B_SELECT_ALL), 'A');
150 	item->SetTarget(this);
151 	menu->AddItem(item);
152 
153 	item = new BMenuItem(B_TRANSLATE("Invert selection"),
154 		new BMessage(kInvertSelection), 'S');
155 	item->SetTarget(PoseView());
156 	menu->AddItem(item);
157 
158 	item = new BMenuItem(B_TRANSLATE("Open parent"),
159 		new BMessage(kOpenParentDir), B_UP_ARROW);
160 	item->SetTarget(PoseView());
161 	menu->AddItem(item);
162 
163 	item = new BMenuItem(B_TRANSLATE("Close"),
164 		new BMessage(B_QUIT_REQUESTED), 'W');
165 	item->SetTarget(this);
166 	menu->AddItem(item);
167 }
168 
169 
170 void
171 VirtualDirectoryWindow::AddWindowContextMenus(BMenu* menu)
172 {
173 	BMenuItem* resizeItem = new BMenuItem(B_TRANSLATE("Resize to fit"),
174 		new BMessage(kResizeToFit), 'Y');
175 	menu->AddItem(resizeItem);
176 	menu->AddItem(new BMenuItem(B_TRANSLATE("Select" B_UTF8_ELLIPSIS),
177 		new BMessage(kShowSelectionWindow), 'A', B_SHIFT_KEY));
178 	menu->AddItem(new BMenuItem(B_TRANSLATE("Select all"),
179 		new BMessage(B_SELECT_ALL), 'A'));
180 	BMenuItem* closeItem = new BMenuItem(B_TRANSLATE("Close"),
181 		new BMessage(B_QUIT_REQUESTED), 'W');
182 	menu->AddItem(closeItem);
183 	// target items as needed
184 	menu->SetTargetForItems(PoseView());
185 	closeItem->SetTarget(this);
186 	resizeItem->SetTarget(this);
187 }
188 
189 } // namespace BPrivate
190