FilePanel.cpp revision e47063d2
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#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
61extern _IMPEXP_TRACKER void run_open_panel();
62extern _IMPEXP_TRACKER void run_save_panel();
63
64
65void
66run_open_panel()
67{
68	(new TFilePanel())->Show();
69}
70
71
72void
73run_save_panel()
74{
75	(new TFilePanel(B_SAVE_PANEL))->Show();
76}
77
78
79//	#pragma mark - BFilePanel
80
81
82BFilePanel::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
106BFilePanel::~BFilePanel()
107{
108	if (fWindow->Lock())
109		fWindow->Quit();
110}
111
112
113void
114BFilePanel::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
129	// Position the file panel like an alert
130	BWindow* parent = dynamic_cast<BWindow*>(
131		BLooper::LooperForThread(find_thread(NULL)));
132	const BRect frame = parent != NULL ? parent->Frame()
133		: BScreen(fWindow).Frame();
134
135	fWindow->MoveTo(fWindow->AlertPosition(frame));
136	if (!IsShowing())
137		fWindow->Show();
138
139	fWindow->Activate();
140}
141
142
143void
144BFilePanel::Hide()
145{
146	AutoLock<BWindow> lock(fWindow);
147	if (!lock)
148		return;
149
150	if (!fWindow->IsHidden())
151		fWindow->QuitRequested();
152}
153
154
155bool
156BFilePanel::IsShowing() const
157{
158	AutoLock<BWindow> lock(fWindow);
159	if (!lock)
160		return false;
161
162	return !fWindow->IsHidden();
163}
164
165
166void
167BFilePanel::SendMessage(const BMessenger* messenger, BMessage* message)
168{
169	messenger->SendMessage(message);
170}
171
172
173file_panel_mode
174BFilePanel::PanelMode() const
175{
176	AutoLock<BWindow> lock(fWindow);
177	if (!lock)
178		return B_OPEN_PANEL;
179
180	if (static_cast<TFilePanel*>(fWindow)->IsSavePanel())
181		return B_SAVE_PANEL;
182
183	return B_OPEN_PANEL;
184}
185
186
187BMessenger
188BFilePanel::Messenger() const
189{
190	BMessenger target;
191
192	AutoLock<BWindow> lock(fWindow);
193	if (!lock)
194		return target;
195
196	return *static_cast<TFilePanel*>(fWindow)->Target();
197}
198
199
200void
201BFilePanel::SetTarget(BMessenger target)
202{
203	AutoLock<BWindow> lock(fWindow);
204	if (!lock)
205		return;
206
207	static_cast<TFilePanel*>(fWindow)->SetTarget(target);
208}
209
210
211void
212BFilePanel::SetMessage(BMessage* message)
213{
214	AutoLock<BWindow> lock(fWindow);
215	if (!lock)
216		return;
217
218	static_cast<TFilePanel*>(fWindow)->SetMessage(message);
219}
220
221
222void
223BFilePanel::Refresh()
224{
225	AutoLock<BWindow> lock(fWindow);
226	if (!lock)
227		return;
228
229	static_cast<TFilePanel*>(fWindow)->Refresh();
230}
231
232
233BRefFilter*
234BFilePanel::RefFilter() const
235{
236	AutoLock<BWindow> lock(fWindow);
237	if (!lock)
238		return 0;
239
240	return static_cast<TFilePanel*>(fWindow)->Filter();
241}
242
243
244void
245BFilePanel::SetRefFilter(BRefFilter* filter)
246{
247	AutoLock<BWindow> lock(fWindow);
248	if (!lock)
249		return;
250
251	static_cast<TFilePanel*>(fWindow)->SetRefFilter(filter);
252}
253
254
255void
256BFilePanel::SetButtonLabel(file_panel_button button, const char* text)
257{
258	AutoLock<BWindow> lock(fWindow);
259	if (!lock)
260		return;
261
262	static_cast<TFilePanel*>(fWindow)->SetButtonLabel(button, text);
263}
264
265
266void
267BFilePanel::GetPanelDirectory(entry_ref* ref) const
268{
269	AutoLock<BWindow> lock(fWindow);
270	if (!lock)
271		return;
272
273	*ref = *static_cast<TFilePanel*>(fWindow)->TargetModel()->EntryRef();
274}
275
276
277void
278BFilePanel::SetSaveText(const char* text)
279{
280	AutoLock<BWindow> lock(fWindow);
281	if (!lock)
282		return;
283
284	static_cast<TFilePanel*>(fWindow)->SetSaveText(text);
285}
286
287
288void
289BFilePanel::SetPanelDirectory(const entry_ref* ref)
290{
291	AutoLock<BWindow> lock(fWindow);
292	if (!lock)
293		return;
294
295	static_cast<TFilePanel*>(fWindow)->SetTo(ref);
296}
297
298
299void
300BFilePanel::SetPanelDirectory(const char* path)
301{
302	entry_ref ref;
303	status_t err = get_ref_for_path(path, &ref);
304	if (err < B_OK)
305	  return;
306
307	AutoLock<BWindow> lock(fWindow);
308	if (!lock)
309		return;
310
311	static_cast<TFilePanel*>(fWindow)->SetTo(&ref);
312}
313
314
315void
316BFilePanel::SetPanelDirectory(const BEntry* entry)
317{
318	entry_ref ref;
319
320	if (entry && entry->GetRef(&ref) == B_OK)
321		SetPanelDirectory(&ref);
322}
323
324
325void
326BFilePanel::SetPanelDirectory(const BDirectory* dir)
327{
328	BEntry	entry;
329
330	if (dir && (dir->GetEntry(&entry) == B_OK))
331		SetPanelDirectory(&entry);
332}
333
334
335BWindow*
336BFilePanel::Window() const
337{
338	return fWindow;
339}
340
341
342void
343BFilePanel::Rewind()
344{
345	AutoLock<BWindow> lock(fWindow);
346	if (!lock)
347		return;
348
349	static_cast<TFilePanel*>(fWindow)->Rewind();
350}
351
352
353status_t
354BFilePanel::GetNextSelectedRef(entry_ref* ref)
355{
356	AutoLock<BWindow> lock(fWindow);
357	if (!lock)
358		return B_ERROR;
359
360	return static_cast<TFilePanel*>(fWindow)->GetNextEntryRef(ref);
361
362}
363
364
365void
366BFilePanel::SetHideWhenDone(bool on)
367{
368	AutoLock<BWindow> lock(fWindow);
369	if (!lock)
370		return;
371
372	static_cast<TFilePanel*>(fWindow)->SetHideWhenDone(on);
373}
374
375
376bool
377BFilePanel::HidesWhenDone(void) const
378{
379	AutoLock<BWindow> lock(fWindow);
380	if (!lock)
381		return false;
382
383	return static_cast<TFilePanel*>(fWindow)->HidesWhenDone();
384}
385
386
387void
388BFilePanel::WasHidden()
389{
390	// hook function
391}
392
393
394void
395BFilePanel::SelectionChanged()
396{
397	// hook function
398}
399