102be5353SAxel Dörfler/*
202be5353SAxel DörflerOpen Tracker License
302be5353SAxel Dörfler
402be5353SAxel DörflerTerms and Conditions
502be5353SAxel Dörfler
602be5353SAxel DörflerCopyright (c) 1991-2000, Be Incorporated. All rights reserved.
702be5353SAxel Dörfler
802be5353SAxel DörflerPermission is hereby granted, free of charge, to any person obtaining a copy of
902be5353SAxel Dörflerthis software and associated documentation files (the "Software"), to deal in
1002be5353SAxel Dörflerthe Software without restriction, including without limitation the rights to
1102be5353SAxel Dörfleruse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1202be5353SAxel Dörflerof the Software, and to permit persons to whom the Software is furnished to do
1302be5353SAxel Dörflerso, subject to the following conditions:
1402be5353SAxel Dörfler
1502be5353SAxel DörflerThe above copyright notice and this permission notice applies to all licensees
1602be5353SAxel Dörflerand shall be included in all copies or substantial portions of the Software.
1702be5353SAxel Dörfler
1802be5353SAxel DörflerTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1902be5353SAxel DörflerIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
2002be5353SAxel DörflerFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2102be5353SAxel DörflerBE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2202be5353SAxel DörflerAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
2302be5353SAxel DörflerWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2402be5353SAxel Dörfler
2502be5353SAxel DörflerExcept as contained in this notice, the name of Be Incorporated shall not be
2602be5353SAxel Dörflerused in advertising or otherwise to promote the sale, use or other dealings in
2702be5353SAxel Dörflerthis Software without prior written authorization from Be Incorporated.
2802be5353SAxel Dörfler
2902be5353SAxel DörflerTracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
3002be5353SAxel Dörflerof Be Incorporated in the United States and other countries. Other brand product
3102be5353SAxel Dörflernames are registered trademarks or trademarks of their respective holders.
3202be5353SAxel DörflerAll rights reserved.
3302be5353SAxel Dörfler*/
3402be5353SAxel Dörfler
3502be5353SAxel Dörfler// Implementation for the public FilePanel object.
3602be5353SAxel Dörfler
37b05aa8b5SJohn Scipione
3866684552SIngo Weinhold#include <sys/resource.h>
3966684552SIngo Weinhold
40a615ec8fSAxel Dörfler#include <BeBuild.h>
4102be5353SAxel Dörfler#include <Debug.h>
4202be5353SAxel Dörfler#include <FilePanel.h>
436723d9fcSJohn Scipione#include <Looper.h>
446723d9fcSJohn Scipione#include <Screen.h>
456723d9fcSJohn Scipione#include <Window.h>
4602be5353SAxel Dörfler
4702be5353SAxel Dörfler#include "AutoLock.h"
4802be5353SAxel Dörfler#include "Commands.h"
4902be5353SAxel Dörfler#include "FilePanelPriv.h"
5002be5353SAxel Dörfler
51a615ec8fSAxel Dörfler
5202be5353SAxel Dörfler// prototypes for some private kernel calls that will some day be public
53f6e4cbb9SAxel Dörfler#ifndef _IMPEXP_ROOT
54a615ec8fSAxel Dörfler#	define _IMPEXP_ROOT
55f6e4cbb9SAxel Dörfler#endif
5602be5353SAxel Dörfler
5702be5353SAxel Dörfler
5808575837SJohn Scipione//	#pragma mark - BFilePanel
59a615ec8fSAxel Dörfler
60a615ec8fSAxel Dörfler
61b05aa8b5SJohn ScipioneBFilePanel::BFilePanel(file_panel_mode mode, BMessenger* target,
62b05aa8b5SJohn Scipione	const entry_ref* ref, uint32 nodeFlavors, bool multipleSelection,
63b05aa8b5SJohn Scipione	BMessage* message, BRefFilter* filter, bool modal,
6402be5353SAxel Dörfler	bool hideWhenDone)
6502be5353SAxel Dörfler{
6602be5353SAxel Dörfler	// boost file descriptor limit so file panels in other apps don't have
6702be5353SAxel Dörfler	// problems
6866684552SIngo Weinhold	struct rlimit rl;
6966684552SIngo Weinhold	rl.rlim_cur = 512;
7066684552SIngo Weinhold	rl.rlim_max = RLIM_SAVED_MAX;
7166684552SIngo Weinhold	setrlimit(RLIMIT_NOFILE, &rl);
7266684552SIngo Weinhold
7302be5353SAxel Dörfler	BEntry startDir(ref);
7466684552SIngo Weinhold	fWindow = new TFilePanel(mode, target, &startDir, nodeFlavors,
7502be5353SAxel Dörfler		multipleSelection, message, filter, 0, B_DOCUMENT_WINDOW_LOOK,
7602be5353SAxel Dörfler		modal ? B_MODAL_APP_WINDOW_FEEL : B_NORMAL_WINDOW_FEEL,
7702be5353SAxel Dörfler		hideWhenDone);
7802be5353SAxel Dörfler
79b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetClientObject(this);
8002be5353SAxel Dörfler
8102be5353SAxel Dörfler	fWindow->SetIsFilePanel(true);
8202be5353SAxel Dörfler}
8302be5353SAxel Dörfler
84b05aa8b5SJohn Scipione
8502be5353SAxel DörflerBFilePanel::~BFilePanel()
8602be5353SAxel Dörfler{
8702be5353SAxel Dörfler	if (fWindow->Lock())
8802be5353SAxel Dörfler		fWindow->Quit();
8902be5353SAxel Dörfler}
9002be5353SAxel Dörfler
91b05aa8b5SJohn Scipione
9202be5353SAxel Dörflervoid
9302be5353SAxel DörflerBFilePanel::Show()
9402be5353SAxel Dörfler{
9502be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
9602be5353SAxel Dörfler	if (!lock)
9702be5353SAxel Dörfler		return;
9802be5353SAxel Dörfler
9902be5353SAxel Dörfler	// if the window is already showing, don't jerk the workspaces around,
10002be5353SAxel Dörfler	// just pull it to us
10102be5353SAxel Dörfler	uint32 workspace = 1UL << (uint32)current_workspace();
10202be5353SAxel Dörfler	uint32 windowWorkspaces = fWindow->Workspaces();
10354e2dd72SJohn Scipione	if (!(windowWorkspaces & workspace)) {
10402be5353SAxel Dörfler		// window in a different workspace, reopen in current
10502be5353SAxel Dörfler		fWindow->SetWorkspaces(workspace);
10654e2dd72SJohn Scipione	}
10702be5353SAxel Dörfler
1086723d9fcSJohn Scipione	// Position the file panel like an alert
10954e2dd72SJohn Scipione	BWindow* parent = dynamic_cast<BWindow*>(
11054e2dd72SJohn Scipione		BLooper::LooperForThread(find_thread(NULL)));
1116723d9fcSJohn Scipione	const BRect frame = parent != NULL ? parent->Frame()
1126723d9fcSJohn Scipione		: BScreen(fWindow).Frame();
1136723d9fcSJohn Scipione
114e47063d2SJohn Scipione	fWindow->MoveTo(fWindow->AlertPosition(frame));
11566684552SIngo Weinhold	if (!IsShowing())
11602be5353SAxel Dörfler		fWindow->Show();
11702be5353SAxel Dörfler
11866684552SIngo Weinhold	fWindow->Activate();
11902be5353SAxel Dörfler}
12002be5353SAxel Dörfler
121b05aa8b5SJohn Scipione
12202be5353SAxel Dörflervoid
12302be5353SAxel DörflerBFilePanel::Hide()
12402be5353SAxel Dörfler{
12502be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
12602be5353SAxel Dörfler	if (!lock)
12702be5353SAxel Dörfler		return;
12802be5353SAxel Dörfler
12902be5353SAxel Dörfler	if (!fWindow->IsHidden())
13002be5353SAxel Dörfler		fWindow->QuitRequested();
13102be5353SAxel Dörfler}
13202be5353SAxel Dörfler
133b05aa8b5SJohn Scipione
13402be5353SAxel Dörflerbool
13502be5353SAxel DörflerBFilePanel::IsShowing() const
13602be5353SAxel Dörfler{
13702be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
13802be5353SAxel Dörfler	if (!lock)
13902be5353SAxel Dörfler		return false;
14002be5353SAxel Dörfler
14102be5353SAxel Dörfler	return !fWindow->IsHidden();
14202be5353SAxel Dörfler}
14302be5353SAxel Dörfler
14402be5353SAxel Dörfler
14502be5353SAxel Dörflervoid
146b05aa8b5SJohn ScipioneBFilePanel::SendMessage(const BMessenger* messenger, BMessage* message)
14702be5353SAxel Dörfler{
14802be5353SAxel Dörfler	messenger->SendMessage(message);
14902be5353SAxel Dörfler}
15002be5353SAxel Dörfler
151b05aa8b5SJohn Scipione
15202be5353SAxel Dörflerfile_panel_mode
15302be5353SAxel DörflerBFilePanel::PanelMode() const
15402be5353SAxel Dörfler{
15502be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
15602be5353SAxel Dörfler	if (!lock)
15702be5353SAxel Dörfler		return B_OPEN_PANEL;
15802be5353SAxel Dörfler
159b05aa8b5SJohn Scipione	if (static_cast<TFilePanel*>(fWindow)->IsSavePanel())
16002be5353SAxel Dörfler		return B_SAVE_PANEL;
16102be5353SAxel Dörfler
16202be5353SAxel Dörfler	return B_OPEN_PANEL;
16302be5353SAxel Dörfler}
16402be5353SAxel Dörfler
165b05aa8b5SJohn Scipione
16602be5353SAxel DörflerBMessenger
16702be5353SAxel DörflerBFilePanel::Messenger() const
16802be5353SAxel Dörfler{
16902be5353SAxel Dörfler	BMessenger target;
17002be5353SAxel Dörfler
17102be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
17202be5353SAxel Dörfler	if (!lock)
17302be5353SAxel Dörfler		return target;
17402be5353SAxel Dörfler
175b05aa8b5SJohn Scipione	return *static_cast<TFilePanel*>(fWindow)->Target();
17602be5353SAxel Dörfler}
17702be5353SAxel Dörfler
178b05aa8b5SJohn Scipione
17902be5353SAxel Dörflervoid
18002be5353SAxel DörflerBFilePanel::SetTarget(BMessenger target)
18102be5353SAxel Dörfler{
18202be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
18302be5353SAxel Dörfler	if (!lock)
18402be5353SAxel Dörfler		return;
18502be5353SAxel Dörfler
186b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetTarget(target);
18702be5353SAxel Dörfler}
18802be5353SAxel Dörfler
189b05aa8b5SJohn Scipione
19002be5353SAxel Dörflervoid
191b05aa8b5SJohn ScipioneBFilePanel::SetMessage(BMessage* message)
19202be5353SAxel Dörfler{
19302be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
19402be5353SAxel Dörfler	if (!lock)
19502be5353SAxel Dörfler		return;
19602be5353SAxel Dörfler
197b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetMessage(message);
19802be5353SAxel Dörfler}
19902be5353SAxel Dörfler
200b05aa8b5SJohn Scipione
20102be5353SAxel Dörflervoid
20202be5353SAxel DörflerBFilePanel::Refresh()
20302be5353SAxel Dörfler{
20402be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
20502be5353SAxel Dörfler	if (!lock)
20602be5353SAxel Dörfler		return;
20766684552SIngo Weinhold
208b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->Refresh();
20902be5353SAxel Dörfler}
21002be5353SAxel Dörfler
211b05aa8b5SJohn Scipione
212b05aa8b5SJohn ScipioneBRefFilter*
21302be5353SAxel DörflerBFilePanel::RefFilter() const
21402be5353SAxel Dörfler{
21502be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
21602be5353SAxel Dörfler	if (!lock)
21702be5353SAxel Dörfler		return 0;
21802be5353SAxel Dörfler
219b05aa8b5SJohn Scipione	return static_cast<TFilePanel*>(fWindow)->Filter();
22002be5353SAxel Dörfler}
22102be5353SAxel Dörfler
222b05aa8b5SJohn Scipione
22302be5353SAxel Dörflervoid
224b05aa8b5SJohn ScipioneBFilePanel::SetRefFilter(BRefFilter* filter)
22502be5353SAxel Dörfler{
22602be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
22702be5353SAxel Dörfler	if (!lock)
22802be5353SAxel Dörfler		return;
22902be5353SAxel Dörfler
230b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetRefFilter(filter);
23102be5353SAxel Dörfler}
23202be5353SAxel Dörfler
233b05aa8b5SJohn Scipione
23402be5353SAxel Dörflervoid
235b05aa8b5SJohn ScipioneBFilePanel::SetButtonLabel(file_panel_button button, const char* text)
23602be5353SAxel Dörfler{
23702be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
23802be5353SAxel Dörfler	if (!lock)
23902be5353SAxel Dörfler		return;
24066684552SIngo Weinhold
241b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetButtonLabel(button, text);
24202be5353SAxel Dörfler}
24302be5353SAxel Dörfler
244b05aa8b5SJohn Scipione
245529cd177SAdrien Destuguesvoid
246529cd177SAdrien DestuguesBFilePanel::SetNodeFlavors(uint32 flavors)
247529cd177SAdrien Destugues{
248529cd177SAdrien Destugues	AutoLock<BWindow> lock(fWindow);
249529cd177SAdrien Destugues	if (!lock)
250529cd177SAdrien Destugues		return;
251529cd177SAdrien Destugues
252529cd177SAdrien Destugues	static_cast<TFilePanel*>(fWindow)->SetNodeFlavors(flavors);
253529cd177SAdrien Destugues}
254529cd177SAdrien Destugues
255529cd177SAdrien Destugues
25602be5353SAxel Dörflervoid
257b05aa8b5SJohn ScipioneBFilePanel::GetPanelDirectory(entry_ref* ref) const
25802be5353SAxel Dörfler{
25902be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
26002be5353SAxel Dörfler	if (!lock)
26102be5353SAxel Dörfler		return;
26202be5353SAxel Dörfler
263b05aa8b5SJohn Scipione	*ref = *static_cast<TFilePanel*>(fWindow)->TargetModel()->EntryRef();
26402be5353SAxel Dörfler}
26502be5353SAxel Dörfler
266b05aa8b5SJohn Scipione
26702be5353SAxel Dörflervoid
268b05aa8b5SJohn ScipioneBFilePanel::SetSaveText(const char* text)
26902be5353SAxel Dörfler{
27002be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
27102be5353SAxel Dörfler	if (!lock)
27202be5353SAxel Dörfler		return;
27302be5353SAxel Dörfler
274b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetSaveText(text);
27502be5353SAxel Dörfler}
27602be5353SAxel Dörfler
277b05aa8b5SJohn Scipione
27802be5353SAxel Dörflervoid
279b05aa8b5SJohn ScipioneBFilePanel::SetPanelDirectory(const entry_ref* ref)
28002be5353SAxel Dörfler{
28102be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
28202be5353SAxel Dörfler	if (!lock)
28302be5353SAxel Dörfler		return;
28402be5353SAxel Dörfler
285b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetTo(ref);
28602be5353SAxel Dörfler}
28702be5353SAxel Dörfler
288b05aa8b5SJohn Scipione
28902be5353SAxel Dörflervoid
290b05aa8b5SJohn ScipioneBFilePanel::SetPanelDirectory(const char* path)
29102be5353SAxel Dörfler{
29202be5353SAxel Dörfler	entry_ref ref;
29302be5353SAxel Dörfler	status_t err = get_ref_for_path(path, &ref);
29466684552SIngo Weinhold	if (err < B_OK)
29502be5353SAxel Dörfler	  return;
29602be5353SAxel Dörfler
29702be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
29802be5353SAxel Dörfler	if (!lock)
29902be5353SAxel Dörfler		return;
30002be5353SAxel Dörfler
301b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetTo(&ref);
30202be5353SAxel Dörfler}
30302be5353SAxel Dörfler
304b05aa8b5SJohn Scipione
30502be5353SAxel Dörflervoid
306b05aa8b5SJohn ScipioneBFilePanel::SetPanelDirectory(const BEntry* entry)
30702be5353SAxel Dörfler{
30802be5353SAxel Dörfler	entry_ref ref;
30902be5353SAxel Dörfler
31002be5353SAxel Dörfler	if (entry && entry->GetRef(&ref) == B_OK)
31102be5353SAxel Dörfler		SetPanelDirectory(&ref);
31202be5353SAxel Dörfler}
31302be5353SAxel Dörfler
314b05aa8b5SJohn Scipione
31502be5353SAxel Dörflervoid
316b05aa8b5SJohn ScipioneBFilePanel::SetPanelDirectory(const BDirectory* dir)
31702be5353SAxel Dörfler{
31802be5353SAxel Dörfler	BEntry	entry;
31902be5353SAxel Dörfler
32002be5353SAxel Dörfler	if (dir && (dir->GetEntry(&entry) == B_OK))
32102be5353SAxel Dörfler		SetPanelDirectory(&entry);
32202be5353SAxel Dörfler}
32302be5353SAxel Dörfler
324b05aa8b5SJohn Scipione
325b05aa8b5SJohn ScipioneBWindow*
32602be5353SAxel DörflerBFilePanel::Window() const
32702be5353SAxel Dörfler{
32802be5353SAxel Dörfler	return fWindow;
32902be5353SAxel Dörfler}
33002be5353SAxel Dörfler
331b05aa8b5SJohn Scipione
33202be5353SAxel Dörflervoid
33302be5353SAxel DörflerBFilePanel::Rewind()
33402be5353SAxel Dörfler{
33502be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
33602be5353SAxel Dörfler	if (!lock)
33702be5353SAxel Dörfler		return;
33802be5353SAxel Dörfler
339b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->Rewind();
34002be5353SAxel Dörfler}
34102be5353SAxel Dörfler
342b05aa8b5SJohn Scipione
34302be5353SAxel Dörflerstatus_t
344b05aa8b5SJohn ScipioneBFilePanel::GetNextSelectedRef(entry_ref* ref)
34502be5353SAxel Dörfler{
34602be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
34702be5353SAxel Dörfler	if (!lock)
34802be5353SAxel Dörfler		return B_ERROR;
34966684552SIngo Weinhold
350b05aa8b5SJohn Scipione	return static_cast<TFilePanel*>(fWindow)->GetNextEntryRef(ref);
35102be5353SAxel Dörfler
35202be5353SAxel Dörfler}
35302be5353SAxel Dörfler
35402be5353SAxel Dörfler
35502be5353SAxel Dörflervoid
35602be5353SAxel DörflerBFilePanel::SetHideWhenDone(bool on)
35702be5353SAxel Dörfler{
35802be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
35902be5353SAxel Dörfler	if (!lock)
36002be5353SAxel Dörfler		return;
36166684552SIngo Weinhold
362b05aa8b5SJohn Scipione	static_cast<TFilePanel*>(fWindow)->SetHideWhenDone(on);
36302be5353SAxel Dörfler}
36402be5353SAxel Dörfler
365b05aa8b5SJohn Scipione
36602be5353SAxel Dörflerbool
36702be5353SAxel DörflerBFilePanel::HidesWhenDone(void) const
36802be5353SAxel Dörfler{
36902be5353SAxel Dörfler	AutoLock<BWindow> lock(fWindow);
37002be5353SAxel Dörfler	if (!lock)
37102be5353SAxel Dörfler		return false;
37202be5353SAxel Dörfler
373b05aa8b5SJohn Scipione	return static_cast<TFilePanel*>(fWindow)->HidesWhenDone();
37402be5353SAxel Dörfler}
37502be5353SAxel Dörfler
376b05aa8b5SJohn Scipione
37702be5353SAxel Dörflervoid
37802be5353SAxel DörflerBFilePanel::WasHidden()
37902be5353SAxel Dörfler{
38002be5353SAxel Dörfler	// hook function
38102be5353SAxel Dörfler}
38202be5353SAxel Dörfler
383b05aa8b5SJohn Scipione
38402be5353SAxel Dörflervoid
38502be5353SAxel DörflerBFilePanel::SelectionChanged()
38602be5353SAxel Dörfler{
38702be5353SAxel Dörfler	// hook function
38802be5353SAxel Dörfler}
389