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