FileSelector.cpp revision bdee0a3f
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{
4252a38012Sejakowatz	m_exit_sem 		= create_sem(0, "FileSelector");
4352a38012Sejakowatz	m_result 		= B_ERROR;
4452a38012Sejakowatz	m_save_panel 	= NULL;
4552a38012Sejakowatz}
4652a38012Sejakowatz
4752a38012SejakowatzFileSelector::~FileSelector()
4852a38012Sejakowatz{
4952a38012Sejakowatz	delete m_save_panel;
5052a38012Sejakowatz	delete_sem(m_exit_sem);
5152a38012Sejakowatz}
5252a38012Sejakowatz
5352a38012Sejakowatz
5452a38012Sejakowatzbool FileSelector::QuitRequested()
5552a38012Sejakowatz{
5652a38012Sejakowatz	release_sem(m_exit_sem);
5752a38012Sejakowatz	return true;
5852a38012Sejakowatz}
5952a38012Sejakowatz
6052a38012Sejakowatz
6152a38012Sejakowatzvoid FileSelector::MessageReceived(BMessage * msg)
6252a38012Sejakowatz{
6352a38012Sejakowatz	switch (msg->what)
6452a38012Sejakowatz		{
6552a38012Sejakowatz		case START_MSG:
66bdee0a3fSStefano Ceccherini			{
67bdee0a3fSStefano Ceccherini			BMessenger messenger(this);
6852a38012Sejakowatz			m_save_panel = new BFilePanel(B_SAVE_PANEL,
69bdee0a3fSStefano Ceccherini							&messenger, NULL, 0, false);
7052a38012Sejakowatz
7152a38012Sejakowatz			m_save_panel->Window()->SetWorkspaces(B_CURRENT_WORKSPACE);
7252a38012Sejakowatz			m_save_panel->Show();
7352a38012Sejakowatz			break;
74bdee0a3fSStefano Ceccherini			}
7552a38012Sejakowatz		case B_SAVE_REQUESTED:
7652a38012Sejakowatz			{
7752a38012Sejakowatz			entry_ref 		dir;
7852a38012Sejakowatz
7952a38012Sejakowatz			if ( msg->FindRef("directory", &dir) == B_OK)
8052a38012Sejakowatz				{
8152a38012Sejakowatz				const char *	name;
8252a38012Sejakowatz
8352a38012Sejakowatz				BDirectory bdir(&dir);
8452a38012Sejakowatz				if ( msg->FindString("name", &name) == B_OK)
8552a38012Sejakowatz					{
8652a38012Sejakowatz					if ( name != NULL )
8752a38012Sejakowatz						m_result = m_entry.SetTo(&bdir, name);
8852a38012Sejakowatz					};
8952a38012Sejakowatz				};
9052a38012Sejakowatz
9152a38012Sejakowatz			release_sem(m_exit_sem);
9252a38012Sejakowatz			break;
9352a38012Sejakowatz			};
9452a38012Sejakowatz
9552a38012Sejakowatz		case B_CANCEL:
9652a38012Sejakowatz			release_sem(m_exit_sem);
9752a38012Sejakowatz			break;
9852a38012Sejakowatz
9952a38012Sejakowatz		default:
10052a38012Sejakowatz			inherited::MessageReceived(msg);
10152a38012Sejakowatz			break;
10252a38012Sejakowatz		};
10352a38012Sejakowatz}
10452a38012Sejakowatz
10552a38012Sejakowatz
10652a38012Sejakowatzstatus_t FileSelector::Go(entry_ref * ref)
10752a38012Sejakowatz{
10852a38012Sejakowatz	MoveTo(300,300);
10952a38012Sejakowatz	Hide();
11052a38012Sejakowatz	Show();
11152a38012Sejakowatz	PostMessage(START_MSG);
11252a38012Sejakowatz	acquire_sem(m_exit_sem);
11352a38012Sejakowatz
11466486a1bSMichael Pfeiffer	// cache result to avoid memory access of deleted window object
11566486a1bSMichael Pfeiffer	// after Quit().
11666486a1bSMichael Pfeiffer	status_t result = m_result;
11766486a1bSMichael Pfeiffer	if ( result == B_OK && ref)
11866486a1bSMichael Pfeiffer		result = m_entry.GetRef(ref);
11952a38012Sejakowatz
12052a38012Sejakowatz	Lock();
12152a38012Sejakowatz	Quit();
12252a38012Sejakowatz
12366486a1bSMichael Pfeiffer	return result;
12452a38012Sejakowatz}
12552a38012Sejakowatz
12652a38012Sejakowatz
12752a38012Sejakowatz
128