1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /* AbiWord
4  * Copyright (C) 2000 AbiSource, Inc.
5  * Copyright (C) 2003 Hubert Figuiere
6  * Copyright (C) 2005 Francis Franklin
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  */
23 
24 #include <stdlib.h>
25 
26 #include "ut_string.h"
27 #include "ut_assert.h"
28 #include "ut_debugmsg.h"
29 
30 #include "xap_CocoaDialog_Utilities.h"
31 
32 #include "xap_App.h"
33 #include "xap_CocoaApp.h"
34 #include "xap_CocoaFrame.h"
35 
36 #include "ap_Strings.h"
37 #include "ap_Dialog_Id.h"
38 #include "ap_Dialog_New.h"
39 #include "ap_CocoaDialog_New.h"
40 
41 #include "xap_Dlg_FileOpenSaveAs.h"
42 #include "ie_imp.h"
43 
44 /*************************************************************************/
45 
static_constructor(XAP_DialogFactory * pFactory,XAP_Dialog_Id dlgid)46 XAP_Dialog * AP_CocoaDialog_New::static_constructor(XAP_DialogFactory * pFactory,
47 												   XAP_Dialog_Id dlgid)
48 {
49 	AP_CocoaDialog_New * p = new AP_CocoaDialog_New(pFactory, dlgid);
50 	return p;
51 }
52 
AP_CocoaDialog_New(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id dlgid)53 AP_CocoaDialog_New::AP_CocoaDialog_New(XAP_DialogFactory * pDlgFactory,
54 										 XAP_Dialog_Id dlgid)
55 	: AP_Dialog_New(pDlgFactory, dlgid), m_pFrame(0)
56 {
57 }
58 
~AP_CocoaDialog_New(void)59 AP_CocoaDialog_New::~AP_CocoaDialog_New(void)
60 {
61 }
62 
runModal(XAP_Frame * pFrame)63 void AP_CocoaDialog_New::runModal(XAP_Frame * pFrame)
64 {
65 	m_pFrame = pFrame;
66 	m_dlg = [[AP_CocoaDialog_NewController alloc] initFromNib];
67 	[m_dlg setXAPOwner:this];
68 	NSWindow* win = [m_dlg window];
69 
70 	// Populate the window's data items
71 //	_populateWindowData();
72 
73 	[NSApp runModalForWindow:win];
74 
75 //	_storeWindowData();
76 	[m_dlg close];
77 	[m_dlg release];
78 	m_dlg = nil;
79 	m_pFrame = NULL;
80 }
81 
82 /*************************************************************************/
83 /*************************************************************************/
84 
event_Ok()85 void AP_CocoaDialog_New::event_Ok ()
86 {
87 	setAnswer (AP_Dialog_New::a_OK);
88 
89 	if ([m_dlg existingBtnState])
90 	{
91 		setOpenType(AP_Dialog_New::open_Existing);
92 	}
93 	else
94 	{
95 		setFileName([[m_dlg newBtnState] UTF8String]);
96 		setOpenType(AP_Dialog_New::open_Template);
97 	}
98 
99 	[NSApp stopModal];
100 }
101 
event_Cancel()102 void AP_CocoaDialog_New::event_Cancel ()
103 {
104 	setAnswer (AP_Dialog_New::a_CANCEL);
105 	[NSApp stopModal];
106 }
107 
event_ToggleOpenExisting()108 void AP_CocoaDialog_New::event_ToggleOpenExisting ()
109 {
110 	XAP_Dialog_Id dlgid = XAP_DIALOG_ID_FILE_OPEN;
111 
112 	XAP_DialogFactory * pDialogFactory
113 		= (XAP_DialogFactory *) XAP_App::getApp()->getDialogFactory();
114 
115 	XAP_Dialog_FileOpenSaveAs * pDialog
116 		= (XAP_Dialog_FileOpenSaveAs *)(pDialogFactory->requestDialog(dlgid));
117 	UT_ASSERT(pDialog);
118 
119 	pDialog->setCurrentPathname(0);
120 	pDialog->setSuggestFilename(false);
121 
122 	UT_uint32 filterCount = IE_Imp::getImporterCount();
123 	const char ** szDescList = (const char **) UT_calloc(filterCount + 1,
124 													  sizeof(char *));
125 	const char ** szSuffixList = (const char **) UT_calloc(filterCount + 1,
126 														sizeof(char *));
127 	IEFileType * nTypeList = (IEFileType *) UT_calloc(filterCount + 1,
128 												   sizeof(IEFileType));
129 	UT_uint32 k = 0;
130 
131 	while (IE_Imp::enumerateDlgLabels(k, &szDescList[k],
132 									  &szSuffixList[k], &nTypeList[k]))
133 			k++;
134 
135 	pDialog->setFileTypeList(szDescList, szSuffixList,
136 							 (const UT_sint32 *) nTypeList);
137 
138 	pDialog->setDefaultFileType(IE_Imp::fileTypeForSuffix(".abw"));
139 
140 	pDialog->runModal(m_pFrame);
141 
142 	XAP_Dialog_FileOpenSaveAs::tAnswer ans = pDialog->getAnswer();
143 	bool bOK = (ans == XAP_Dialog_FileOpenSaveAs::a_OK);
144 
145 	if (bOK)
146 	{
147 		const char * szResultPathname = pDialog->getPathname();
148 		if (szResultPathname && *szResultPathname)
149 		{
150 			// update the entry box
151 			[m_dlg setFileName:[NSString stringWithUTF8String:szResultPathname]];
152 			setFileName (szResultPathname);
153 		}
154 
155 		[m_dlg setExistingBtnState:YES];
156 	}
157 
158 	FREEP(szDescList);
159 	FREEP(szSuffixList);
160 	FREEP(nTypeList);
161 
162 	pDialogFactory->releaseDialog(pDialog);
163 }
164 
event_ToggleStartNew()165 void AP_CocoaDialog_New::event_ToggleStartNew ()
166 {
167 	// nada
168 }
169 
170 /*************************************************************************/
171 /*************************************************************************/
172 
173 @implementation AP_CocoaDialog_NewController
174 
175 - (void)dealloc
176 {
177 	[m_templates release];
178 	[_dataSource release];
179 	[super dealloc];
180 }
181 
182 
183 - (id)initFromNib
184 {
185 	if(![super initWithWindowNibName:@"ap_CocoaDialog_New"]) {
186 		return nil;
187 	}
188 	m_templates = [[NSMutableArray alloc] init];
189 	return self;
190 }
191 
192 
193 - (void)setXAPOwner:(XAP_Dialog *)owner
194 {
195 	_xap = dynamic_cast<AP_CocoaDialog_New*>(owner);
196 	UT_ASSERT (_xap);
197 }
198 
199 - (void)discardXAP
200 {
201 	_xap = nil;
202 }
203 
204 - (void)windowDidLoad
205 {
206 	const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
207 
208 	LocalizeControl([self window], pSS, AP_STRING_ID_DLG_NEW_Title);
209 	LocalizeControl(_okBtn, pSS, XAP_STRING_ID_DLG_OK);
210 	LocalizeControl(_cancelBtn, pSS, XAP_STRING_ID_DLG_Cancel);
211 	LocalizeControl(_chooseFileBtn, pSS, AP_STRING_ID_DLG_NEW_Choose);
212 	LocalizeControl(_createNewBtn, pSS, AP_STRING_ID_DLG_NEW_Create);
213 	LocalizeControl(_openBtn, pSS, AP_STRING_ID_DLG_NEW_Open);
214 	[self synchronizeGUI:_createNewBtn];	// TODO check what is the default
215 
216 	_dataSource = [[XAP_StringListDataSource alloc] init];
217 	NSMutableArray *templateDirs = [[NSMutableArray alloc] init];
218 
219 	[templateDirs addObject:[NSString stringWithFormat:@"%s/templates/", XAP_App::getApp()->getUserPrivateDirectory()]];
220 	[templateDirs addObject:[NSString stringWithFormat:@"%s/templates/", XAP_App::getApp()->getAbiSuiteLibDir()]];
221 	[templateDirs addObject:[NSString stringWithFormat:@"%s/templates/", [[[NSBundle mainBundle] resourcePath] UTF8String]]];
222 
223 	NSEnumerator* e = [templateDirs objectEnumerator];
224     while(NSString *dirPath = [e nextObject])
225     {
226         NSDirectoryEnumerator* dirEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirPath];
227         while(NSString *file = [dirEnumerator nextObject])
228         {
229 			if([[file pathExtension] isEqualToString:@"awt"]) {
230                 [_dataSource addString:file];
231                 [m_templates addObject:[dirPath stringByAppendingPathComponent:file]];
232             }
233         }
234     }
235 
236 	[templateDirs release];
237 
238 	[_templateList setDataSource:_dataSource];
239 	[_templateList setDelegate:self];
240 }
241 
242 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
243 {
244 	UT_UNUSED(aNotification);
245 	if ([_templateList selectedRow] < 0) // I don't think this happens
246 		[self synchronizeGUI:_openBtn];
247 	else
248 		[self synchronizeGUI:_createNewBtn];
249 }
250 
251 - (IBAction)cancelAction:(id)sender
252 {
253 	UT_UNUSED(sender);
254 	_xap->event_Cancel();
255 }
256 
257 - (IBAction)radioButtonAction:(id)sender
258 {
259 	[self synchronizeGUI:sender];
260 }
261 
262 - (IBAction)chooseAction:(id)sender
263 {
264 	UT_UNUSED(sender);
265 	_xap->event_ToggleOpenExisting();
266 }
267 
268 - (IBAction)okAction:(id)sender
269 {
270 	UT_UNUSED(sender);
271 	_xap->event_Ok();
272 }
273 
274 - (void)synchronizeGUI:(NSControl*)control
275 {
276 	enum { NONE, NEW, OPEN } selected;
277 
278 	if (control == _createNewBtn) {
279 		selected = NEW;
280 	}
281 	else if (control == _openBtn) {
282 		selected = OPEN;
283 	}
284 	else {
285 		selected = NONE;
286 	}
287 	switch (selected) {
288 	case NEW:
289 		[_createNewBtn setState:NSOnState];
290 		[_templateList setEnabled:YES];
291 		[_openBtn setState:NSOffState];
292 		[_documentNameData setEnabled:NO];
293 		[_chooseFileBtn setEnabled:NO];
294 		break;
295 	case OPEN:
296 		[_createNewBtn setState:NSOffState];
297 		[_templateList setEnabled:NO];
298 		[_openBtn setState:NSOnState];
299 		[_documentNameData setEnabled:YES];
300 		[_chooseFileBtn setEnabled:YES];
301 		break;
302 	default:
303 		break;
304 	}
305 }
306 
307 
308 - (BOOL)existingBtnState
309 {
310 	return ([_openBtn state] == NSOnState);
311 }
312 
313 - (void)setExistingBtnState:(BOOL)state
314 {
315 	if (state) {
316 		[_openBtn setState:NSOnState];
317 	}
318 	else {
319 		[_openBtn setState:NSOffState];
320 	}
321 }
322 
323 - (NSString *)newBtnState
324 {
325 	NSString * path = 0;
326 
327 	if ([_createNewBtn state] == NSOnState) {
328 		int index = [_templateList selectedRow];
329 		if (index < 0) {
330 			UT_DEBUGMSG(("AP_CocoaDialog_NewController -newBtnState: no template selection from list?\n"));
331 		}
332 		else {
333 			path = (NSString *) [m_templates objectAtIndex:((unsigned) index)];
334 			UT_DEBUGMSG(("AP_CocoaDialog_NewController -newBtnState: template \"%s\" selected from list.\n", [path UTF8String]));
335 		}
336 	}
337 	return path;
338 }
339 
340 - (void)setFileName:(NSString*)name
341 {
342 	[_documentNameData setStringValue:name];
343 }
344 
345 @end
346