1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiWord
4 * Copyright (C) 2000 AbiSource, Inc.
5 * Copyright (C) 2001-2002 Hubert Figuiere
6 * Copyright (C) 2005 Francis James 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 #include <time.h>
26
27 #include "ut_assert.h"
28 #include "ut_debugmsg.h"
29 #include "ut_string.h"
30
31 #include "xap_App.h"
32 #include "xap_CocoaApp.h"
33 #include "xap_CocoaFrame.h"
34 #include "xap_Strings.h"
35
36 #include "ap_Dialog_Id.h"
37 #include "ap_Dialog_MailMerge.h"
38 #include "ap_Strings.h"
39
40 #include "ap_CocoaDialog_MailMerge.h"
41
42 /*****************************************************************/
43
static_constructor(XAP_DialogFactory * pFactory,XAP_Dialog_Id dlgid)44 XAP_Dialog * AP_CocoaDialog_MailMerge::static_constructor(XAP_DialogFactory * pFactory, XAP_Dialog_Id dlgid)
45 {
46 AP_CocoaDialog_MailMerge * p = new AP_CocoaDialog_MailMerge(pFactory, dlgid);
47 return p;
48 }
49
AP_CocoaDialog_MailMerge(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id dlgid)50 AP_CocoaDialog_MailMerge::AP_CocoaDialog_MailMerge(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id dlgid) :
51 AP_Dialog_MailMerge(pDlgFactory, dlgid),
52 m_dlg(nil)
53 {
54 //
55 }
56
~AP_CocoaDialog_MailMerge(void)57 AP_CocoaDialog_MailMerge::~AP_CocoaDialog_MailMerge(void)
58 {
59 destroy();
60 }
61
runModeless(XAP_Frame *)62 void AP_CocoaDialog_MailMerge::runModeless(XAP_Frame * /*pFrame*/)
63 {
64 m_dlg = [[AP_CocoaDialog_MailMerge_Controller alloc] initFromNib];
65 if (m_dlg)
66 {
67 [m_dlg setXAPOwner:this];
68 [m_dlg window];
69
70 // TODO
71
72 // Save dialog the ID number and pointer to the widget
73 UT_sint32 sid = (UT_sint32) getDialogId();
74 m_pApp->rememberModelessId(sid, (XAP_Dialog_Modeless *) m_pDialog);
75 XAP_Frame * pFrame = XAP_App::getApp()->getLastFocussedFrame();
76 if (pFrame)
77 {
78 setActiveFrame(pFrame);
79 init();
80 }
81
82 activate();
83 }
84 }
85
activate(void)86 void AP_CocoaDialog_MailMerge::activate(void)
87 {
88 if (m_dlg)
89 {
90 [m_dlg windowToFront];
91 [m_dlg updateAvailableFields];
92 }
93 }
94
destroy(void)95 void AP_CocoaDialog_MailMerge::destroy(void)
96 {
97 if (m_dlg)
98 {
99 [m_dlg close];
100 [m_dlg release];
101
102 modeless_cleanup();
103 }
104 m_dlg = 0;
105 }
106
eventInsert(NSString * field_name)107 void AP_CocoaDialog_MailMerge::eventInsert(NSString * field_name)
108 {
109 UT_UTF8String name([field_name UTF8String]);
110
111 setMergeField(name);
112 addClicked();
113 }
114
setFieldList()115 void AP_CocoaDialog_MailMerge::setFieldList()
116 {
117 if (m_dlg)
118 {
119 [m_dlg updateAvailableFields];
120 }
121 }
122
123 @implementation AP_CocoaDialog_MailMerge_Controller
124
125 - (id)initFromNib
126 {
127 if (![super initWithWindowNibName:@"ap_CocoaDialog_MailMerge"]) {
128 return nil;
129 }
130 _xap = NULL;
131
132 m_AvailableFields = [[NSMutableArray alloc] initWithCapacity:32];
133 if (!m_AvailableFields)
134 {
135 [self release];
136 return nil;
137 }
138 return self;
139 }
140
141 - (void)dealloc
142 {
143 if (m_AvailableFields)
144 {
145 [m_AvailableFields release];
146 m_AvailableFields = 0;
147 }
148 [super dealloc];
149 }
150
151 - (void)setXAPOwner:(XAP_Dialog *)owner
152 {
153 _xap = static_cast<AP_CocoaDialog_MailMerge *>(owner);
154 }
155
156 - (void)discardXAP
157 {
158 _xap = 0;
159 }
160
161 - (void)windowDidLoad
162 {
163 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
164
165 LocalizeControl([self window], pSS, AP_STRING_ID_DLG_MailMerge_MailMergeTitle);
166
167 LocalizeControl(oAvailableFields, pSS, AP_STRING_ID_DLG_MailMerge_AvailableFields);
168 LocalizeControl(oFieldNameCell, pSS, AP_STRING_ID_DLG_MailMerge_Insert);
169 LocalizeControl(oOpenFile, pSS, AP_STRING_ID_DLG_MailMerge_OpenFile);
170 LocalizeControl(oClose, pSS,XAP_STRING_ID_DLG_Close);
171 LocalizeControl(oInsert, pSS, AP_STRING_ID_DLG_InsertButton);
172
173 [oFieldsTable setDataSource:self];
174 [oFieldsTable setDelegate:self];
175 }
176
177 - (void)windowToFront
178 {
179 [[self window] makeKeyAndOrderFront:self];
180 [[self window] makeFirstResponder:oFieldName];
181 }
182
183 - (IBAction)aFieldsTable:(id)sender
184 {
185 UT_UNUSED(sender);
186 //
187 }
188
189 - (IBAction)aFieldName:(id)sender
190 {
191 [self aInsert:sender];
192 }
193
194 - (IBAction)aOpenFile:(id)sender
195 {
196 UT_UNUSED(sender);
197 if (_xap)
198 if (XAP_Frame * pFrame = XAP_App::getApp()->getLastFocussedFrame())
199 {
200 _xap->setActiveFrame(pFrame);
201 _xap->eventOpen();
202 }
203 }
204
205 - (IBAction)aClose:(id)sender
206 {
207 UT_UNUSED(sender);
208 if (_xap)
209 _xap->destroy();
210 }
211
212 - (IBAction)aInsert:(id)sender
213 {
214 UT_UNUSED(sender);
215 NSString * field_name = [oFieldName stringValue];
216
217 if ([field_name length])
218 if (_xap)
219 _xap->eventInsert(field_name);
220 }
221
222 - (void)updateAvailableFields
223 {
224 if (_xap)
225 {
226 [m_AvailableFields removeAllObjects];
227
228 UT_uint32 count = _xap->fieldCount();
229
230 for (UT_uint32 i = 0; i < count; i++)
231 {
232 [m_AvailableFields addObject:[NSString stringWithUTF8String:(_xap->field(i).utf8_str())]];
233 }
234 [oFieldsTable reloadData];
235 }
236 }
237
238 /* NSTableViewDataSource methods
239 */
240 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
241 {
242 UT_UNUSED(aTableView);
243 return (int) [m_AvailableFields count];
244 }
245
246 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
247 {
248 UT_UNUSED(aTableView);
249 UT_UNUSED(aTableColumn);
250 return [m_AvailableFields objectAtIndex:rowIndex];
251 }
252
253 /* NSTableView delegate methods
254 */
255 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
256 {
257 UT_UNUSED(aNotification);
258 int row = [oFieldsTable selectedRow];
259 if (row >= 0)
260 {
261 [oFieldName setStringValue:[m_AvailableFields objectAtIndex:row]];
262 }
263 else
264 {
265 [oFieldName setStringValue:@""];
266 }
267 }
268
269 - (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
270 {
271 UT_UNUSED(aTableView);
272 UT_UNUSED(aTableColumn);
273 UT_UNUSED(rowIndex);
274 [aCell setFont:[NSFont systemFontOfSize:10.0f]];
275 }
276
277 @end
278