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