xref: /haiku/src/add-ons/print/transports/print_to_file/FileSelector.cpp (revision d9cebac2b77547b7064f22497514eecd2d047160)
1 /*****************************************************************************/
2 // Print to file transport add-on.
3 //
4 // Author
5 //   Philippe Houdoin
6 //
7 // This application and all source files used in its construction, except
8 // where noted, are licensed under the MIT License, and have been written
9 // and are:
10 //
11 // Copyright (c) 2001,2002 OpenBeOS Project
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 /*****************************************************************************/
31 
32 #include <stdio.h>
33 
34 #include <InterfaceKit.h>
35 
36 #include "FileSelector.h"
37 
38 FileSelector::FileSelector(void)
39 	: BWindow(BRect(0,0,320,160), "printtofile", B_TITLED_WINDOW,
40 	B_NOT_ZOOMABLE, B_CURRENT_WORKSPACE)
41 {
42 	m_exit_sem 		= create_sem(0, "FileSelector");
43 	m_result 		= B_ERROR;
44 	m_save_panel 	= NULL;
45 }
46 
47 FileSelector::~FileSelector()
48 {
49 	delete m_save_panel;
50 	delete_sem(m_exit_sem);
51 }
52 
53 
54 bool FileSelector::QuitRequested()
55 {
56 	release_sem(m_exit_sem);
57 	return true;
58 }
59 
60 
61 void FileSelector::MessageReceived(BMessage * msg)
62 {
63 	switch (msg->what)
64 		{
65 		case START_MSG:
66 			m_save_panel = new BFilePanel(B_SAVE_PANEL,
67 							new BMessenger(this), NULL, 0, false);
68 
69 			m_save_panel->Window()->SetWorkspaces(B_CURRENT_WORKSPACE);
70 			m_save_panel->Show();
71 			break;
72 
73 		case B_SAVE_REQUESTED:
74 			{
75 			entry_ref 		dir;
76 
77 			if ( msg->FindRef("directory", &dir) == B_OK)
78 				{
79 				const char *	name;
80 
81 				BDirectory bdir(&dir);
82 				if ( msg->FindString("name", &name) == B_OK)
83 					{
84 					if ( name != NULL )
85 						m_result = m_entry.SetTo(&bdir, name);
86 					};
87 				};
88 
89 			release_sem(m_exit_sem);
90 			break;
91 			};
92 
93 		case B_CANCEL:
94 			release_sem(m_exit_sem);
95 			break;
96 
97 		default:
98 			inherited::MessageReceived(msg);
99 			break;
100 		};
101 }
102 
103 
104 status_t FileSelector::Go(entry_ref * ref)
105 {
106 	MoveTo(300,300);
107 	Hide();
108 	Show();
109 	PostMessage(START_MSG);
110 	acquire_sem(m_exit_sem);
111 
112 	// cache result to avoid memory access of deleted window object
113 	// after Quit().
114 	status_t result = m_result;
115 	if ( result == B_OK && ref)
116 		result = m_entry.GetRef(ref);
117 
118 	Lock();
119 	Quit();
120 
121 	return result;
122 }
123 
124 
125 
126