xref: /haiku/src/add-ons/print/transports/print_to_file/FileSelector.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
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 Haiku 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_CLOSE_ON_ESCAPE, B_CURRENT_WORKSPACE)
41 {
42 	fExitSem = create_sem(0, "FileSelector");
43 	fResult = B_ERROR;
44 	fSavePanel = NULL;
45 }
46 
47 
48 FileSelector::~FileSelector()
49 {
50 	delete fSavePanel;
51 	delete_sem(fExitSem);
52 }
53 
54 
55 bool
56 FileSelector::QuitRequested()
57 {
58 	release_sem(fExitSem);
59 	return BWindow::QuitRequested();
60 }
61 
62 
63 void
64 FileSelector::MessageReceived(BMessage * msg)
65 {
66 	switch (msg->what) {
67 		case START_MSG:
68 		{
69 			BMessenger messenger(this);
70 			fSavePanel = new BFilePanel(B_SAVE_PANEL,
71 							&messenger, NULL, 0, false);
72 
73 			fSavePanel->Window()->SetWorkspaces(B_CURRENT_WORKSPACE);
74 			fSavePanel->Show();
75 			break;
76 		}
77 		case B_SAVE_REQUESTED:
78 		{
79 			entry_ref dir;
80 
81 			if (msg->FindRef("directory", &dir) == B_OK) {
82 				const char* name;
83 
84 				BDirectory bdir(&dir);
85 				if (msg->FindString("name", &name) == B_OK) {
86 					if (name != NULL)
87 						fResult = fEntry.SetTo(&bdir, name);
88 				};
89 			};
90 
91 			release_sem(fExitSem);
92 			break;
93 		};
94 
95 		case B_CANCEL:
96 			release_sem(fExitSem);
97 			break;
98 
99 		default:
100 			inherited::MessageReceived(msg);
101 			break;
102 	};
103 }
104 
105 
106 status_t
107 FileSelector::Go(entry_ref* ref)
108 {
109 	MoveTo(300,300);
110 	Hide();
111 	Show();
112 	PostMessage(START_MSG);
113 	acquire_sem(fExitSem);
114 
115 	// cache result to avoid memory access of deleted window object
116 	// after Quit().
117 	status_t result = fResult;
118 	if (result == B_OK && ref)
119 		result = fEntry.GetRef(ref);
120 
121 	Lock();
122 	Quit();
123 
124 	return result;
125 }
126 
127