Debian Codegraph @

src/af/xap/cocoa/xap_CocoaDlg_Language.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 1998 AbiSource, Inc.
5 * Copyright (c) 2003 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 <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include "ut_assert.h"
29 #include "ut_debugmsg.h"
30 #include "ut_string.h"
31
32 #include "xap_App.h"
33 #include "xap_CocoaDialog_Utilities.h"
34 #include "xap_CocoaDlg_Language.h"
35
36 /* *************************************************************** */
37
38 XAP_Dialog * XAP_CocoaDialog_Language::static_constructor(XAP_DialogFactory * pFactory, XAP_Dialog_Id dlgid)
39 {
40 XAP_CocoaDialog_Language * p = new XAP_CocoaDialog_Language(pFactory, dlgid);
41 return p;
42 }
43
44 XAP_CocoaDialog_Language::XAP_CocoaDialog_Language(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id dlgid) :
45 XAP_Dialog_Language(pDlgFactory, dlgid),
46 m_dlg(nil)
47 {
48 //
49 }
50
51 XAP_CocoaDialog_Language::~XAP_CocoaDialog_Language(void)
52 {
53 //
54 }
55
56 void XAP_CocoaDialog_Language::runModal(XAP_Frame * /*pFrame*/)
57 {
58 m_dlg = [[XAP_CocoaDialog_Language_Controller alloc] initFromNib];
59
60 [m_dlg setXAPOwner:this];
61
62 [NSApp runModalForWindow:[m_dlg window]];
63
64 int index = [m_dlg indexOfSelectedLanguage];
65
66 if (index >= 0)
67 {
68 m_answer = XAP_Dialog_Language::a_OK;
69
70 setMakeDocumentDefault([m_dlg applyToDocument] == YES);
71
72 _setLanguage(m_ppLanguages[index]);
73
74 m_bChangedLanguage = true;
75 }
76 else
77 {
78 m_answer = XAP_Dialog_Language::a_CANCEL;
79 }
80
81 [m_dlg close];
82 [m_dlg release];
83
84 m_dlg = nil;
85 }
86
87 /* **************************************************************************************** */
88
89 @implementation XAP_CocoaDialog_Language_Controller
90
91 - (id)initFromNib
92 {
93 if (![super initWithWindowNibName:@"xap_CocoaDlg_Language"]) {
94 return nil;
95 }
96 _xap = 0;
97
98 m_Selection = 0;
99 m_SelectionIndex = -1;
100
101 m_bApplyToDocument = NO;
102
103 m_Languages = [[NSMutableArray alloc] initWithCapacity:100];
104 return self;
105 }
106
107 - (void)dealloc
108 {
109 [m_Languages release];
110 [m_Selection release];
111 [super dealloc];
112 }
113
114 - (void)setXAPOwner:(XAP_Dialog *)owner
115 {
116 if (m_Selection)
117 {
118 [m_Selection release];
119 m_Selection = nil;
120 }
121 m_SelectionIndex = -1;
122
123 _xap = static_cast<XAP_CocoaDialog_Language *>(owner);
124 }
125
126 - (void)discardXAP
127 {
128 if (m_Selection)
129 {
130 [m_Selection release];
131 m_Selection = nil;
132 }
133 m_SelectionIndex = -1;
134
135 _xap = 0;
136 }
137
138 - (void)windowDidLoad
139 {
140 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
141
142 LocalizeControl([self window], pSS, XAP_STRING_ID_DLG_ULANG_LangTitle);
143
144 LocalizeControl(_cancelBtn, pSS, XAP_STRING_ID_DLG_Cancel);
145 LocalizeControl(_okBtn, pSS, XAP_STRING_ID_DLG_OK);
146 LocalizeControl(_selectLanguageBox, pSS, XAP_STRING_ID_DLG_ULANG_AvailableLanguages);
147
148 LocalizeControl(_documentDefaultBtn, pSS, XAP_STRING_ID_DLG_ULANG_DefaultLangChkbox);
149
150 if (_xap)
151 {
152 UT_UTF8String defaultLanguageString;
153 _xap->getDocDefaultLangDescription(defaultLanguageString);
154 [_documentCurrentLabel setStringValue:[NSString stringWithUTF8String:(defaultLanguageString.utf8_str())]];
155
156 const char * current_language = _xap->getCurrentLanguage();
157
158 int current_index = -1;
159
160 UT_uint32 count = _xap->getLanguageCount();
161
162 for (UT_uint32 i = 0; i < count; i++)
163 {
164 const char * language = _xap->getNthLanguage(i);
165
166 [m_Languages addObject:[NSString stringWithUTF8String:language]];
167
168 if (current_language && (g_ascii_strcasecmp(current_language, language) == 0))
169 {
170 current_index = (int) i;
171 }
172 }
173 [_languageTable reloadData];
174
175 if (current_index >= 0)
176 {
177 [_languageTable selectRowIndexes:[NSIndexSet indexSetWithIndex:current_index] byExtendingSelection:NO];
178 [_languageTable scrollRowToVisible:current_index];
179 }
180 }
181 }
182
183 - (NSString *)selectedLanguage
184 {
185 return m_Selection;
186 }
187
188 - (int)indexOfSelectedLanguage
189 {
190 return m_SelectionIndex;
191 }
192
193 - (BOOL)applyToDocument
194 {
195 return m_bApplyToDocument;
196 }
197
198 - (IBAction)aCancel:(id)sender
199 {
200 UT_UNUSED(sender);
201 if (m_Selection)
202 {
203 [m_Selection release];
204 m_Selection = 0;
205 }
206 m_SelectionIndex = -1;
207
208 [NSApp stopModal];
209 }
210
211 - (IBAction)aOK:(id)sender
212 {
213 UT_UNUSED(sender);
214 m_bApplyToDocument = ([_documentDefaultBtn state] == NSOnState) ? YES : NO;
215
216 [NSApp stopModal];
217 }
218
219 - (IBAction)aLanguageTable:(id)sender
220 {
221 UT_UNUSED(sender);
222 if (m_Selection)
223 {
224 [m_Selection release];
225 m_Selection = nil;
226 }
227 m_SelectionIndex = [_languageTable selectedRow];
228
229 if (m_SelectionIndex >= 0)
230 {
231 if ((unsigned) m_SelectionIndex < [m_Languages count])
232 {
233 m_Selection = [[m_Languages objectAtIndex:((unsigned) m_SelectionIndex)] retain];
234 }
235 else // huh?
236 {
237 m_SelectionIndex = -1;
238 }
239 }
240 }
241
242 /* NSTableViewDataSource methods
243 */
244 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
245 {
246 UT_UNUSED(aTableView);
247 return (int) [m_Languages count];
248 }
249
250 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
251 {
252 UT_UNUSED(aTableView);
253 UT_UNUSED(aTableColumn);
254 return [m_Languages objectAtIndex:((unsigned) rowIndex)];
255 }
256
257 @end