xref: /haiku/src/kits/tracker/FilePanel.cpp (revision fef6144999c2fa611f59ee6ffe6dd7999501385c)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 // Implementation for the public FilePanel object.
36 
37 #include <Debug.h>
38 #include <FilePanel.h>
39 
40 #include "AutoLock.h"
41 #include "Commands.h"
42 #include "FilePanelPriv.h"
43 
44 // prototypes for some private kernel calls that will some day be public
45 #if B_BEOS_VERSION_DANO
46 #define _IMPEXP_ROOT
47 #endif
48 extern "C" _IMPEXP_ROOT int _kset_fd_limit_(int num);
49 #if B_BEOS_VERSION_DANO
50 #undef _IMPEXP_ROOT
51 #endif
52 
53 void
54 run_open_panel()
55 {
56 	(new TFilePanel())->Show();
57 }
58 
59 void
60 run_save_panel()
61 {
62 	(new TFilePanel(B_SAVE_PANEL))->Show();
63 }
64 
65 
66 BFilePanel::BFilePanel(file_panel_mode mode, BMessenger *target,
67 	const entry_ref *ref, uint32 nodeFlavors, bool multipleSelection,
68 	BMessage *message, BRefFilter *filter, bool modal,
69 	bool hideWhenDone)
70 {
71 	// boost file descriptor limit so file panels in other apps don't have
72 	// problems
73 	_kset_fd_limit_ (512);
74 	BEntry startDir(ref);
75 	fWindow = new TFilePanel(mode, target, &startDir, nodeFlavors,
76 		multipleSelection, message, filter, 0, B_DOCUMENT_WINDOW_LOOK,
77 		modal ? B_MODAL_APP_WINDOW_FEEL : B_NORMAL_WINDOW_FEEL,
78 		hideWhenDone);
79 
80 	static_cast<TFilePanel *>(fWindow)->SetClientObject(this);
81 
82 	fWindow->SetIsFilePanel(true);
83 }
84 
85 BFilePanel::~BFilePanel()
86 {
87 	if (fWindow->Lock())
88 		fWindow->Quit();
89 }
90 
91 void
92 BFilePanel::Show()
93 {
94 	AutoLock<BWindow> lock(fWindow);
95 	if (!lock)
96 		return;
97 
98 	// if the window is already showing, don't jerk the workspaces around,
99 	// just pull it to us
100 	uint32 workspace = 1UL << (uint32)current_workspace();
101 	uint32 windowWorkspaces = fWindow->Workspaces();
102 	if (!(windowWorkspaces & workspace))
103 		// window in a different workspace, reopen in current
104 		fWindow->SetWorkspaces(workspace);
105 
106 	if (!IsShowing())
107 		fWindow->Show();
108 
109 	fWindow->Activate();
110 }
111 
112 void
113 BFilePanel::Hide()
114 {
115 	AutoLock<BWindow> lock(fWindow);
116 	if (!lock)
117 		return;
118 
119 	if (!fWindow->IsHidden())
120 		fWindow->QuitRequested();
121 }
122 
123 bool
124 BFilePanel::IsShowing() const
125 {
126 	AutoLock<BWindow> lock(fWindow);
127 	if (!lock)
128 		return false;
129 
130 	return !fWindow->IsHidden();
131 }
132 
133 
134 void
135 BFilePanel::SendMessage(const BMessenger *messenger, BMessage *message)
136 {
137 	messenger->SendMessage(message);
138 }
139 
140 file_panel_mode
141 BFilePanel::PanelMode() const
142 {
143 	AutoLock<BWindow> lock(fWindow);
144 	if (!lock)
145 		return B_OPEN_PANEL;
146 
147 	if (static_cast<TFilePanel *>(fWindow)->IsSavePanel())
148 		return B_SAVE_PANEL;
149 
150 	return B_OPEN_PANEL;
151 }
152 
153 BMessenger
154 BFilePanel::Messenger() const
155 {
156 	BMessenger target;
157 
158 	AutoLock<BWindow> lock(fWindow);
159 	if (!lock)
160 		return target;
161 
162 	return *static_cast<TFilePanel *>(fWindow)->Target();
163 }
164 
165 void
166 BFilePanel::SetTarget(BMessenger target)
167 {
168 	AutoLock<BWindow> lock(fWindow);
169 	if (!lock)
170 		return;
171 
172 	static_cast<TFilePanel *>(fWindow)->SetTarget(target);
173 }
174 
175 void
176 BFilePanel::SetMessage(BMessage *message)
177 {
178 	AutoLock<BWindow> lock(fWindow);
179 	if (!lock)
180 		return;
181 
182 	static_cast<TFilePanel *>(fWindow)->SetMessage(message);
183 }
184 
185 void
186 BFilePanel::Refresh()
187 {
188 	AutoLock<BWindow> lock(fWindow);
189 	if (!lock)
190 		return;
191 
192 	static_cast<TFilePanel *>(fWindow)->Refresh();
193 }
194 
195 BRefFilter *
196 BFilePanel::RefFilter() const
197 {
198 	AutoLock<BWindow> lock(fWindow);
199 	if (!lock)
200 		return 0;
201 
202 	return static_cast<TFilePanel *>(fWindow)->Filter();
203 }
204 
205 void
206 BFilePanel::SetRefFilter(BRefFilter *filter)
207 {
208 	AutoLock<BWindow> lock(fWindow);
209 	if (!lock)
210 		return;
211 
212 	static_cast<TFilePanel *>(fWindow)->SetRefFilter(filter);
213 }
214 
215 void
216 BFilePanel::SetButtonLabel(file_panel_button button, const char *text)
217 {
218 	AutoLock<BWindow> lock(fWindow);
219 	if (!lock)
220 		return;
221 
222 	static_cast<TFilePanel *>(fWindow)->SetButtonLabel(button, text);
223 }
224 
225 void
226 BFilePanel::GetPanelDirectory(entry_ref *ref) const
227 {
228 	AutoLock<BWindow> lock(fWindow);
229 	if (!lock)
230 		return;
231 
232 	*ref = *static_cast<TFilePanel *>(fWindow)->TargetModel()->EntryRef();
233 }
234 
235 void
236 BFilePanel::SetSaveText(const char *text)
237 {
238 	AutoLock<BWindow> lock(fWindow);
239 	if (!lock)
240 		return;
241 
242 	static_cast<TFilePanel *>(fWindow)->SetSaveText(text);
243 }
244 
245 void
246 BFilePanel::SetPanelDirectory(const entry_ref *ref)
247 {
248 	AutoLock<BWindow> lock(fWindow);
249 	if (!lock)
250 		return;
251 
252 	static_cast<TFilePanel *>(fWindow)->SetTo(ref);
253 }
254 
255 void
256 BFilePanel::SetPanelDirectory(const char *path)
257 {
258 	entry_ref ref;
259 	status_t err = get_ref_for_path(path, &ref);
260 	if (err < B_OK)
261 	  return;
262 
263 	AutoLock<BWindow> lock(fWindow);
264 	if (!lock)
265 		return;
266 
267 	static_cast<TFilePanel *>(fWindow)->SetTo(&ref);
268 }
269 
270 void
271 BFilePanel::SetPanelDirectory(const BEntry *entry)
272 {
273 	entry_ref ref;
274 
275 	if (entry && entry->GetRef(&ref) == B_OK)
276 		SetPanelDirectory(&ref);
277 }
278 
279 void
280 BFilePanel::SetPanelDirectory(const BDirectory *dir)
281 {
282 	BEntry	entry;
283 
284 	if (dir && (dir->GetEntry(&entry) == B_OK))
285 		SetPanelDirectory(&entry);
286 }
287 
288 BWindow *
289 BFilePanel::Window() const
290 {
291 	return fWindow;
292 }
293 
294 void
295 BFilePanel::Rewind()
296 {
297 	AutoLock<BWindow> lock(fWindow);
298 	if (!lock)
299 		return;
300 
301 	static_cast<TFilePanel *>(fWindow)->Rewind();
302 }
303 
304 status_t
305 BFilePanel::GetNextSelectedRef(entry_ref *ref)
306 {
307 	AutoLock<BWindow> lock(fWindow);
308 	if (!lock)
309 		return B_ERROR;
310 
311 	return static_cast<TFilePanel *>(fWindow)->GetNextEntryRef(ref);
312 
313 }
314 
315 
316 void
317 BFilePanel::SetHideWhenDone(bool on)
318 {
319 	AutoLock<BWindow> lock(fWindow);
320 	if (!lock)
321 		return;
322 
323 	static_cast<TFilePanel *>(fWindow)->SetHideWhenDone(on);
324 }
325 
326 bool
327 BFilePanel::HidesWhenDone(void) const
328 {
329 	AutoLock<BWindow> lock(fWindow);
330 	if (!lock)
331 		return false;
332 
333 	return static_cast<TFilePanel *>(fWindow)->HidesWhenDone();
334 }
335 
336 void
337 BFilePanel::WasHidden()
338 {
339 	// hook function
340 }
341 
342 void
343 BFilePanel::SelectionChanged()
344 {
345 	// hook function
346 }
347 
348