xref: /haiku/src/kits/tracker/FilePanel.cpp (revision 7a74a5df454197933bc6e80a542102362ee98703)
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 
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
58 extern _IMPEXP_TRACKER void run_open_panel();
59 extern _IMPEXP_TRACKER void run_save_panel();
60 
61 
62 void
63 run_open_panel()
64 {
65 	(new TFilePanel())->Show();
66 }
67 
68 
69 void
70 run_save_panel()
71 {
72 	(new TFilePanel(B_SAVE_PANEL))->Show();
73 }
74 
75 
76 //	#pragma mark -
77 
78 
79 BFilePanel::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 
103 BFilePanel::~BFilePanel()
104 {
105 	if (fWindow->Lock())
106 		fWindow->Quit();
107 }
108 
109 
110 void
111 BFilePanel::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 
132 void
133 BFilePanel::Hide()
134 {
135 	AutoLock<BWindow> lock(fWindow);
136 	if (!lock)
137 		return;
138 
139 	if (!fWindow->IsHidden())
140 		fWindow->QuitRequested();
141 }
142 
143 
144 bool
145 BFilePanel::IsShowing() const
146 {
147 	AutoLock<BWindow> lock(fWindow);
148 	if (!lock)
149 		return false;
150 
151 	return !fWindow->IsHidden();
152 }
153 
154 
155 void
156 BFilePanel::SendMessage(const BMessenger* messenger, BMessage* message)
157 {
158 	messenger->SendMessage(message);
159 }
160 
161 
162 file_panel_mode
163 BFilePanel::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 
176 BMessenger
177 BFilePanel::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 
189 void
190 BFilePanel::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 
200 void
201 BFilePanel::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 
211 void
212 BFilePanel::Refresh()
213 {
214 	AutoLock<BWindow> lock(fWindow);
215 	if (!lock)
216 		return;
217 
218 	static_cast<TFilePanel*>(fWindow)->Refresh();
219 }
220 
221 
222 BRefFilter*
223 BFilePanel::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 
233 void
234 BFilePanel::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 
244 void
245 BFilePanel::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 
255 void
256 BFilePanel::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 
266 void
267 BFilePanel::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 
277 void
278 BFilePanel::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 
288 void
289 BFilePanel::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 
304 void
305 BFilePanel::SetPanelDirectory(const BEntry* entry)
306 {
307 	entry_ref ref;
308 
309 	if (entry && entry->GetRef(&ref) == B_OK)
310 		SetPanelDirectory(&ref);
311 }
312 
313 
314 void
315 BFilePanel::SetPanelDirectory(const BDirectory* dir)
316 {
317 	BEntry	entry;
318 
319 	if (dir && (dir->GetEntry(&entry) == B_OK))
320 		SetPanelDirectory(&entry);
321 }
322 
323 
324 BWindow*
325 BFilePanel::Window() const
326 {
327 	return fWindow;
328 }
329 
330 
331 void
332 BFilePanel::Rewind()
333 {
334 	AutoLock<BWindow> lock(fWindow);
335 	if (!lock)
336 		return;
337 
338 	static_cast<TFilePanel*>(fWindow)->Rewind();
339 }
340 
341 
342 status_t
343 BFilePanel::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 
354 void
355 BFilePanel::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 
365 bool
366 BFilePanel::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 
376 void
377 BFilePanel::WasHidden()
378 {
379 	// hook function
380 }
381 
382 
383 void
384 BFilePanel::SelectionChanged()
385 {
386 	// hook function
387 }
388