FileSelector.cpp revision 68fba17f
14092c70eSMichael Pfeiffer/*****************************************************************************/
24092c70eSMichael Pfeiffer// Print to file transport add-on.
34092c70eSMichael Pfeiffer//
44092c70eSMichael Pfeiffer// Author
54092c70eSMichael Pfeiffer//   Philippe Houdoin
64092c70eSMichael Pfeiffer//
74092c70eSMichael Pfeiffer// This application and all source files used in its construction, except
84092c70eSMichael Pfeiffer// where noted, are licensed under the MIT License, and have been written
94092c70eSMichael Pfeiffer// and are:
104092c70eSMichael Pfeiffer//
114092c70eSMichael Pfeiffer// Copyright (c) 2001,2002 OpenBeOS Project
124092c70eSMichael Pfeiffer//
134092c70eSMichael Pfeiffer// Permission is hereby granted, free of charge, to any person obtaining a
144092c70eSMichael Pfeiffer// copy of this software and associated documentation files (the "Software"),
154092c70eSMichael Pfeiffer// to deal in the Software without restriction, including without limitation
164092c70eSMichael Pfeiffer// the rights to use, copy, modify, merge, publish, distribute, sublicense,
174092c70eSMichael Pfeiffer// and/or sell copies of the Software, and to permit persons to whom the
184092c70eSMichael Pfeiffer// Software is furnished to do so, subject to the following conditions:
194092c70eSMichael Pfeiffer//
204092c70eSMichael Pfeiffer// The above copyright notice and this permission notice shall be included
214092c70eSMichael Pfeiffer// in all copies or substantial portions of the Software.
224092c70eSMichael Pfeiffer//
234092c70eSMichael Pfeiffer// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
244092c70eSMichael Pfeiffer// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
254092c70eSMichael Pfeiffer// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
264092c70eSMichael Pfeiffer// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
274092c70eSMichael Pfeiffer// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
284092c70eSMichael Pfeiffer// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
294092c70eSMichael Pfeiffer// DEALINGS IN THE SOFTWARE.
304092c70eSMichael Pfeiffer/*****************************************************************************/
314092c70eSMichael Pfeiffer
3252a38012Sejakowatz#include <stdio.h>
3352a38012Sejakowatz
3452a38012Sejakowatz#include <InterfaceKit.h>
3552a38012Sejakowatz
3652a38012Sejakowatz#include "FileSelector.h"
3752a38012Sejakowatz
3852a38012SejakowatzFileSelector::FileSelector(void)
3952a38012Sejakowatz	: BWindow(BRect(0,0,320,160), "printtofile", B_TITLED_WINDOW,
4052a38012Sejakowatz	B_NOT_ZOOMABLE, B_CURRENT_WORKSPACE)
4152a38012Sejakowatz{
4268fba17fSStefano Ceccherini	m_exit_sem = create_sem(0, "FileSelector");
4368fba17fSStefano Ceccherini	m_result = B_ERROR;
4468fba17fSStefano Ceccherini	m_save_panel = NULL;
4552a38012Sejakowatz}
4652a38012Sejakowatz
4768fba17fSStefano Ceccherini
4852a38012SejakowatzFileSelector::~FileSelector()
4952a38012Sejakowatz{
5052a38012Sejakowatz	delete m_save_panel;
5152a38012Sejakowatz	delete_sem(m_exit_sem);
5252a38012Sejakowatz}
5352a38012Sejakowatz
5452a38012Sejakowatz
5568fba17fSStefano Ceccherinibool
5668fba17fSStefano CeccheriniFileSelector::QuitRequested()
5752a38012Sejakowatz{
5852a38012Sejakowatz	release_sem(m_exit_sem);
5968fba17fSStefano Ceccherini	return BWindow::QuitRequested();
6052a38012Sejakowatz}
6152a38012Sejakowatz
6252a38012Sejakowatz
6368fba17fSStefano Ceccherinivoid
6468fba17fSStefano CeccheriniFileSelector::MessageReceived(BMessage * msg)
6552a38012Sejakowatz{
6668fba17fSStefano Ceccherini	switch (msg->what) {
6752a38012Sejakowatz		case START_MSG:
6868fba17fSStefano Ceccherini		{
69bdee0a3fSStefano Ceccherini			BMessenger messenger(this);
7052a38012Sejakowatz			m_save_panel = new BFilePanel(B_SAVE_PANEL,
71bdee0a3fSStefano Ceccherini							&messenger, NULL, 0, false);
7252a38012Sejakowatz
7352a38012Sejakowatz			m_save_panel->Window()->SetWorkspaces(B_CURRENT_WORKSPACE);
7452a38012Sejakowatz			m_save_panel->Show();
7552a38012Sejakowatz			break;
7668fba17fSStefano Ceccherini		}
7752a38012Sejakowatz		case B_SAVE_REQUESTED:
7868fba17fSStefano Ceccherini		{
7968fba17fSStefano Ceccherini			entry_ref dir;
8052a38012Sejakowatz
8168fba17fSStefano Ceccherini			if (msg->FindRef("directory", &dir) == B_OK) {
8268fba17fSStefano Ceccherini				const char* name;
8352a38012Sejakowatz
8452a38012Sejakowatz				BDirectory bdir(&dir);
8568fba17fSStefano Ceccherini				if (msg->FindString("name", &name) == B_OK) {
8668fba17fSStefano Ceccherini					if (name != NULL)
8752a38012Sejakowatz						m_result = m_entry.SetTo(&bdir, name);
8852a38012Sejakowatz				};
8968fba17fSStefano Ceccherini			};
9052a38012Sejakowatz
9152a38012Sejakowatz			release_sem(m_exit_sem);
9252a38012Sejakowatz			break;
9368fba17fSStefano Ceccherini		};
9452a38012Sejakowatz
9552a38012Sejakowatz		case B_CANCEL:
9652a38012Sejakowatz			release_sem(m_exit_sem);
9752a38012Sejakowatz			break;
9852a38012Sejakowatz
9952a38012Sejakowatz		default:
10052a38012Sejakowatz			inherited::MessageReceived(msg);
10152a38012Sejakowatz			break;
10268fba17fSStefano Ceccherini	};
10352a38012Sejakowatz}
10452a38012Sejakowatz
10552a38012Sejakowatz
10668fba17fSStefano Ceccherinistatus_t
10768fba17fSStefano CeccheriniFileSelector::Go(entry_ref* ref)
10852a38012Sejakowatz{
10952a38012Sejakowatz	MoveTo(300,300);
11052a38012Sejakowatz	Hide();
11152a38012Sejakowatz	Show();
11252a38012Sejakowatz	PostMessage(START_MSG);
11352a38012Sejakowatz	acquire_sem(m_exit_sem);
11452a38012Sejakowatz
11566486a1bSMichael Pfeiffer	// cache result to avoid memory access of deleted window object
11666486a1bSMichael Pfeiffer	// after Quit().
11766486a1bSMichael Pfeiffer	status_t result = m_result;
11868fba17fSStefano Ceccherini	if (result == B_OK && ref)
11966486a1bSMichael Pfeiffer		result = m_entry.GetRef(ref);
12052a38012Sejakowatz
12152a38012Sejakowatz	Lock();
12252a38012Sejakowatz	Quit();
12352a38012Sejakowatz
12466486a1bSMichael Pfeiffer	return result;
12552a38012Sejakowatz}
12652a38012Sejakowatz
127