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