xref: /haiku/src/kits/tracker/FilePanel.cpp (revision 002f37b0cca92e4cf72857c72ac95db5a8b09615)
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 
38 #include <sys/resource.h>
39 
40 #include <BeBuild.h>
41 #include <Debug.h>
42 #include <FilePanel.h>
43 #include <Looper.h>
44 #include <Screen.h>
45 #include <Window.h>
46 
47 #include "AutoLock.h"
48 #include "Commands.h"
49 #include "FilePanelPriv.h"
50 
51 
52 // prototypes for some private kernel calls that will some day be public
53 #ifndef _IMPEXP_ROOT
54 #	define _IMPEXP_ROOT
55 #endif
56 #ifndef _IMPEXP_TRACKER
57 #	define _IMPEXP_TRACKER
58 #endif
59 
60 // these two calls are deprecated
61 extern _IMPEXP_TRACKER void run_open_panel();
62 extern _IMPEXP_TRACKER void run_save_panel();
63 
64 
65 void
66 run_open_panel()
67 {
68 	(new TFilePanel())->Show();
69 }
70 
71 
72 void
73 run_save_panel()
74 {
75 	(new TFilePanel(B_SAVE_PANEL))->Show();
76 }
77 
78 
79 //	#pragma mark -
80 
81 
82 BFilePanel::BFilePanel(file_panel_mode mode, BMessenger* target,
83 	const entry_ref* ref, uint32 nodeFlavors, bool multipleSelection,
84 	BMessage* message, BRefFilter* filter, bool modal,
85 	bool hideWhenDone)
86 {
87 	// boost file descriptor limit so file panels in other apps don't have
88 	// problems
89 	struct rlimit rl;
90 	rl.rlim_cur = 512;
91 	rl.rlim_max = RLIM_SAVED_MAX;
92 	setrlimit(RLIMIT_NOFILE, &rl);
93 
94 	BEntry startDir(ref);
95 	fWindow = new TFilePanel(mode, target, &startDir, nodeFlavors,
96 		multipleSelection, message, filter, 0, B_DOCUMENT_WINDOW_LOOK,
97 		modal ? B_MODAL_APP_WINDOW_FEEL : B_NORMAL_WINDOW_FEEL,
98 		hideWhenDone);
99 
100 	static_cast<TFilePanel*>(fWindow)->SetClientObject(this);
101 
102 	fWindow->SetIsFilePanel(true);
103 }
104 
105 
106 BFilePanel::~BFilePanel()
107 {
108 	if (fWindow->Lock())
109 		fWindow->Quit();
110 }
111 
112 
113 void
114 BFilePanel::Show()
115 {
116 	AutoLock<BWindow> lock(fWindow);
117 	if (!lock)
118 		return;
119 
120 	// if the window is already showing, don't jerk the workspaces around,
121 	// just pull it to us
122 	uint32 workspace = 1UL << (uint32)current_workspace();
123 	uint32 windowWorkspaces = fWindow->Workspaces();
124 	if (!(windowWorkspaces & workspace))
125 		// window in a different workspace, reopen in current
126 		fWindow->SetWorkspaces(workspace);
127 
128 	// Position the file panel like an alert
129 	BWindow* parent =
130 		dynamic_cast<BWindow*>(BLooper::LooperForThread(find_thread(NULL)));
131 	const BRect frame = parent != NULL ? parent->Frame()
132 		: BScreen(fWindow).Frame();
133 	fWindow->MoveTo(dynamic_cast<BWindow*>(fWindow)->AlertPosition(frame));
134 
135 	if (!IsShowing())
136 		fWindow->Show();
137 
138 	fWindow->Activate();
139 }
140 
141 
142 void
143 BFilePanel::Hide()
144 {
145 	AutoLock<BWindow> lock(fWindow);
146 	if (!lock)
147 		return;
148 
149 	if (!fWindow->IsHidden())
150 		fWindow->QuitRequested();
151 }
152 
153 
154 bool
155 BFilePanel::IsShowing() const
156 {
157 	AutoLock<BWindow> lock(fWindow);
158 	if (!lock)
159 		return false;
160 
161 	return !fWindow->IsHidden();
162 }
163 
164 
165 void
166 BFilePanel::SendMessage(const BMessenger* messenger, BMessage* message)
167 {
168 	messenger->SendMessage(message);
169 }
170 
171 
172 file_panel_mode
173 BFilePanel::PanelMode() const
174 {
175 	AutoLock<BWindow> lock(fWindow);
176 	if (!lock)
177 		return B_OPEN_PANEL;
178 
179 	if (static_cast<TFilePanel*>(fWindow)->IsSavePanel())
180 		return B_SAVE_PANEL;
181 
182 	return B_OPEN_PANEL;
183 }
184 
185 
186 BMessenger
187 BFilePanel::Messenger() const
188 {
189 	BMessenger target;
190 
191 	AutoLock<BWindow> lock(fWindow);
192 	if (!lock)
193 		return target;
194 
195 	return *static_cast<TFilePanel*>(fWindow)->Target();
196 }
197 
198 
199 void
200 BFilePanel::SetTarget(BMessenger target)
201 {
202 	AutoLock<BWindow> lock(fWindow);
203 	if (!lock)
204 		return;
205 
206 	static_cast<TFilePanel*>(fWindow)->SetTarget(target);
207 }
208 
209 
210 void
211 BFilePanel::SetMessage(BMessage* message)
212 {
213 	AutoLock<BWindow> lock(fWindow);
214 	if (!lock)
215 		return;
216 
217 	static_cast<TFilePanel*>(fWindow)->SetMessage(message);
218 }
219 
220 
221 void
222 BFilePanel::Refresh()
223 {
224 	AutoLock<BWindow> lock(fWindow);
225 	if (!lock)
226 		return;
227 
228 	static_cast<TFilePanel*>(fWindow)->Refresh();
229 }
230 
231 
232 BRefFilter*
233 BFilePanel::RefFilter() const
234 {
235 	AutoLock<BWindow> lock(fWindow);
236 	if (!lock)
237 		return 0;
238 
239 	return static_cast<TFilePanel*>(fWindow)->Filter();
240 }
241 
242 
243 void
244 BFilePanel::SetRefFilter(BRefFilter* filter)
245 {
246 	AutoLock<BWindow> lock(fWindow);
247 	if (!lock)
248 		return;
249 
250 	static_cast<TFilePanel*>(fWindow)->SetRefFilter(filter);
251 }
252 
253 
254 void
255 BFilePanel::SetButtonLabel(file_panel_button button, const char* text)
256 {
257 	AutoLock<BWindow> lock(fWindow);
258 	if (!lock)
259 		return;
260 
261 	static_cast<TFilePanel*>(fWindow)->SetButtonLabel(button, text);
262 }
263 
264 
265 void
266 BFilePanel::GetPanelDirectory(entry_ref* ref) const
267 {
268 	AutoLock<BWindow> lock(fWindow);
269 	if (!lock)
270 		return;
271 
272 	*ref = *static_cast<TFilePanel*>(fWindow)->TargetModel()->EntryRef();
273 }
274 
275 
276 void
277 BFilePanel::SetSaveText(const char* text)
278 {
279 	AutoLock<BWindow> lock(fWindow);
280 	if (!lock)
281 		return;
282 
283 	static_cast<TFilePanel*>(fWindow)->SetSaveText(text);
284 }
285 
286 
287 void
288 BFilePanel::SetPanelDirectory(const entry_ref* ref)
289 {
290 	AutoLock<BWindow> lock(fWindow);
291 	if (!lock)
292 		return;
293 
294 	static_cast<TFilePanel*>(fWindow)->SetTo(ref);
295 }
296 
297 
298 void
299 BFilePanel::SetPanelDirectory(const char* path)
300 {
301 	entry_ref ref;
302 	status_t err = get_ref_for_path(path, &ref);
303 	if (err < B_OK)
304 	  return;
305 
306 	AutoLock<BWindow> lock(fWindow);
307 	if (!lock)
308 		return;
309 
310 	static_cast<TFilePanel*>(fWindow)->SetTo(&ref);
311 }
312 
313 
314 void
315 BFilePanel::SetPanelDirectory(const BEntry* entry)
316 {
317 	entry_ref ref;
318 
319 	if (entry && entry->GetRef(&ref) == B_OK)
320 		SetPanelDirectory(&ref);
321 }
322 
323 
324 void
325 BFilePanel::SetPanelDirectory(const BDirectory* dir)
326 {
327 	BEntry	entry;
328 
329 	if (dir && (dir->GetEntry(&entry) == B_OK))
330 		SetPanelDirectory(&entry);
331 }
332 
333 
334 BWindow*
335 BFilePanel::Window() const
336 {
337 	return fWindow;
338 }
339 
340 
341 void
342 BFilePanel::Rewind()
343 {
344 	AutoLock<BWindow> lock(fWindow);
345 	if (!lock)
346 		return;
347 
348 	static_cast<TFilePanel*>(fWindow)->Rewind();
349 }
350 
351 
352 status_t
353 BFilePanel::GetNextSelectedRef(entry_ref* ref)
354 {
355 	AutoLock<BWindow> lock(fWindow);
356 	if (!lock)
357 		return B_ERROR;
358 
359 	return static_cast<TFilePanel*>(fWindow)->GetNextEntryRef(ref);
360 
361 }
362 
363 
364 void
365 BFilePanel::SetHideWhenDone(bool on)
366 {
367 	AutoLock<BWindow> lock(fWindow);
368 	if (!lock)
369 		return;
370 
371 	static_cast<TFilePanel*>(fWindow)->SetHideWhenDone(on);
372 }
373 
374 
375 bool
376 BFilePanel::HidesWhenDone(void) const
377 {
378 	AutoLock<BWindow> lock(fWindow);
379 	if (!lock)
380 		return false;
381 
382 	return static_cast<TFilePanel*>(fWindow)->HidesWhenDone();
383 }
384 
385 
386 void
387 BFilePanel::WasHidden()
388 {
389 	// hook function
390 }
391 
392 
393 void
394 BFilePanel::SelectionChanged()
395 {
396 	// hook function
397 }
398