Debian Codegraph @

src/af/xap/cocoa/xap_CocoaApp.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) 2001, 2003-2004, 2009 Hubert Figuiere
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA.
21 */
22
23 #import <Cocoa/Cocoa.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <unistd.h>
30
31 #include <sys/stat.h>
32
33 #include <glib.h>
34
35 #include "ut_debugmsg.h"
36 #include "ut_path.h"
37 #include "ut_string.h"
38 #include "ut_uuid.h"
39
40 #include "xap_Strings.h"
41 #include "xap_Args.h"
42 #include "xap_CocoaApp.h"
43 #include "xap_FakeClipboard.h"
44 #include "xap_CocoaFrame.h"
45 #include "xap_CocoaFrameImpl.h"
46 #include "xap_CocoaToolbar_Icons.h"
47 #include "xap_Cocoa_TB_CFactory.h"
48 #include "xap_Prefs.h"
49 #include "xap_CocoaEncodingManager.h"
50 #include "gr_CocoaCairoGraphics.h"
51 #include "ev_CocoaMenuBar.h"
52
53 /*****************************************************************/
54
55 XAP_CocoaApp::XAP_CocoaApp(const char * szAppName)
56 : XAP_App(szAppName),
57 m_dialogFactory(this),
58 m_controlFactory(),
59 m_pCocoaMenu(NULL),
60 m_szMenuLayoutName(NULL),
61 m_szMenuLabelSetName(NULL)
62 {
63 m_pCocoaToolbarIcons = 0;
64
65 [NSApplication sharedApplication];
66 [NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
67
68 _setAbiSuiteLibDir();
69
70 // set some generic window sizes and positions
71 m_geometry.x = 1;
72 m_geometry.y = 1;
73 m_geometry.width = 600;
74 m_geometry.height = 600;
75
76 // by default, which will be applied if the user does NOT
77 // specify a --geometry argument, we only want to obey the
78 // size (which is set above), not a position.
79 m_geometry.flags = GEOMETRY_FLAG_SIZE;
80
81 // create an instance of UT_UUIDGenerator or appropriate derrived class
82 _setUUIDGenerator(new UT_UUIDGenerator());
83
84 // register graphics allocator
85 GR_GraphicsFactory * pGF = getGraphicsFactory();
86 UT_ASSERT( pGF );
87
88 if(pGF)
89 {
90 UT_DebugOnly<bool> bSuccess = pGF->registerClass(GR_CocoaCairoGraphics::graphicsAllocator,
91 GR_CocoaCairoGraphics::graphicsDescriptor,
92 GR_CocoaCairoGraphics::s_getClassId());
93
94 // we are in deep trouble if this did not succeed
95 UT_ASSERT( bSuccess );
96 pGF->registerAsDefault(GR_CocoaCairoGraphics::s_getClassId(), true);
97 pGF->registerAsDefault(GR_CocoaCairoGraphics::s_getClassId(), false);
98 }
99
100 }
101
102 XAP_CocoaApp::~XAP_CocoaApp()
103 {
104 DELETEP(m_pCocoaToolbarIcons);
105 FREEP(m_szMenuLayoutName);
106 FREEP(m_szMenuLabelSetName);
107 }
108
109 /*!
110 Returns the GUI string encoding.
111 */
112 const char * XAP_CocoaApp::getDefaultEncoding () const
113 {
114 return "UTF-8";
115 }
116
117 bool XAP_CocoaApp::initialize(const char * szKeyBindingsKey, const char * szKeyBindingsDefaultValue)
118 {
119 if (!g_thread_supported ()) {
120 g_thread_init(NULL);
121 }
122
123 // let our base class do it's thing.
124
125 XAP_App::initialize(szKeyBindingsKey, szKeyBindingsDefaultValue);
126
127 /*******************************/
128
129 // load the font stuff from the font directory
130
131 if (!_loadFonts())
132 return false;
133
134 /*******************************/
135
136 // load only one copy of the platform-specific icons.
137
138 m_pCocoaToolbarIcons = new XAP_CocoaToolbar_Icons();
139
140 // do any thing we need here...
141
142
143
144 return true;
145 }
146
147 void XAP_CocoaApp::reallyExit()
148 {
149 [NSApp stop:NSApp];
150 }
151
152 void XAP_CocoaApp::notifyFrameCountChange()
153 {
154 XAP_App::notifyFrameCountChange();
155 }
156
157 UT_sint32 XAP_CocoaApp::makeDirectory(const char * szPath, const UT_sint32 mode ) const
158 {
159 return mkdir(szPath, mode);
160 }
161
162 XAP_App::BidiSupportType XAP_CocoaApp::theOSHasBidiSupport() const
163 {
164 return BIDI_SUPPORT_FULL;
165 }
166
167 XAP_DialogFactory * XAP_CocoaApp::getDialogFactory()
168 {
169 return &m_dialogFactory;
170 }
171
172 XAP_Toolbar_ControlFactory * XAP_CocoaApp::getControlFactory()
173 {
174 return &m_controlFactory;
175 }
176
177 void XAP_CocoaApp::setGeometry(int x, int y, UT_uint32 width, UT_uint32 height,
178 windowGeometryFlags flags)
179 {
180 // TODO : do some range checking?
181 m_geometry.x = x;
182 m_geometry.y = y;
183 m_geometry.width = width;
184 m_geometry.height = height;
185 m_geometry.flags = flags;
186 }
187
188 void XAP_CocoaApp::getGeometry(int * x, int * y, UT_uint32 * width,
189 UT_uint32 * height, windowGeometryFlags * flags)
190 {
191 UT_ASSERT(x && y && width && height);
192 *x = m_geometry.x;
193 *y = m_geometry.y;
194 *width = m_geometry.width;
195 *height = m_geometry.height;
196 *flags = m_geometry.flags;
197 }
198
199 const char * XAP_CocoaApp::getUserPrivateDirectory()
200 {
201 static const char * szAbiDir = "Library/Application Support/AbiSuite";
202
203 static char upd_buffer[PATH_MAX];
204 static char * upd_cache = 0;
205 if (upd_cache) return upd_cache; // points to the static buffer
206
207 const char * szHome = getenv("HOME");
208 if (!szHome || !*szHome) {
209 szHome = ".";
210 }
211
212 if (strlen(szHome)+strlen(szAbiDir)+2 >= PATH_MAX)
213 return NULL;
214
215 strcpy(upd_buffer,szHome);
216 strcat(upd_buffer,"/");
217 strcat(upd_buffer,szAbiDir);
218
219 upd_cache = upd_buffer;
220 return upd_cache;
221 }
222
223 bool XAP_CocoaApp::findAbiSuiteBundleFile(std::string & path, const char * filename, const char * subdir) // checks only bundle
224 {
225 if (!filename)
226 {
227 return false;
228 }
229
230 bool bFound = false;
231
232 // get Bundle resource directory and use that.
233 NSString * resDir = [[NSBundle mainBundle] resourcePath];
234 if (resDir)
235 {
236 path = [resDir UTF8String];
237 if (subdir)
238 {
239 path += "/";
240 path += subdir;
241 }
242 path += "/";
243 path += filename;
244 xxx_UT_DEBUGMSG(("XAP_CocoaApp::findAbiSuiteBundleFile(\"%s\",\"%s\",\"%s\")\n",path.c_str(),filename,subdir));
245 bFound = UT_isRegularFile(path.c_str());
246 }
247 return bFound;
248 }
249
250 bool XAP_CocoaApp::findAbiSuiteLibFile(std::string & path, const char * filename, const char * subdir)
251 {
252 if (!filename)
253 {
254 return false;
255 }
256 if (XAP_App::findAbiSuiteLibFile(path, filename, subdir))
257 {
258 return true;
259 }
260 return findAbiSuiteBundleFile(path, filename, subdir);
261 }
262
263 bool XAP_CocoaApp::findAbiSuiteAppFile(std::string & path, const char * filename, const char * subdir)
264 {
265 if (!filename)
266 {
267 return false;
268 }
269 if (XAP_App::findAbiSuiteLibFile(path, filename, subdir))
270 {
271 return true;
272 }
273 return findAbiSuiteBundleFile(path, filename, subdir);
274 }
275
276 bool XAP_CocoaApp::_loadFonts()
277 {
278 return true;
279 }
280
281 void XAP_CocoaApp::_setAbiSuiteLibDir()
282 {
283 XAP_App::_setAbiSuiteLibDir("/Library/Application Support/AbiSuite");
284 #if 0
285 // TODO Change that to use Bundle path instead. Probably by using FJF code.
286 char buf[PATH_MAX];
287 // char buf2[PATH_MAX]; // not used?
288
289 // see if a command line option [-lib <AbiSuiteLibraryDirectory>] was given
290
291 int kLimit = m_pArgs->m_argc;
292 int nFirstArg = 1; // Cocoa puts the program name in argv[0], so [1] is the first argument
293 int k;
294
295 for (k = nFirstArg; k < kLimit; ++k)
296 if ((*m_pArgs->m_argv[k] == '-') && (g_ascii_strcasecmp(m_pArgs->m_argv[k],"-lib")==0) && (k+1 < kLimit))
297 {
298 strcpy(buf,m_pArgs->m_argv[k+1]);
299 int len = strlen(buf);
300 if (buf[len-1]=='/') // trim trailing slash
301 buf[len-1] = 0;
302 XAP_App::_setAbiSuiteLibDir(buf);
303 return;
304 }
305
306 // if not, see if ABIWORD_DATADIR was set in the environment
307
308 const char * sz = getenv("ABIWORD_DATADIR");
309 if (sz && *sz)
310 {
311 strcpy(buf,sz);
312 char * p = buf;
313 int len = strlen(p);
314 if ( (p[0]=='"') && (p[len-1]=='"') )
315 {
316 // trim leading and trailing DQUOTES
317 p[len-1]=0;
318 p++;
319 len -= 2;
320 }
321 if (p[len-1]=='/') // trim trailing slash
322 p[len-1] = 0;
323 XAP_App::_setAbiSuiteLibDir(p);
324 return;
325 }
326
327 // get Bundle resource directory and use that.
328 NSString* resDir = [[NSBundle mainBundle] resourcePath];
329 if (resDir != nil) {
330 XAP_App::_setAbiSuiteLibDir([resDir UTF8String]);
331 return;
332 }
333
334 // otherwise, use the hard-coded value
335 // this is unlikely to happen
336 UT_DEBUGMSG(("Couldn't get resource bundle directory."));
337 XAP_App::_setAbiSuiteLibDir(getAbiSuiteHome());
338 return;
339 #endif
340 }
341
342 //////////////////////////////////////////////////////////////////
343 //////////////////////////////////////////////////////////////////
344
345 // causes memory corruption!
346 void XAP_CocoaApp::setTimeOfLastEvent(NSTimeInterval timestamp)
347 {
348 // assert until memory corruption fixed
349 UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
350 m_eventTime = timestamp;
351 }
352
353
354 XAP_Frame * XAP_CocoaApp::_getFrontFrame(void)
355 {
356 XAP_Frame* myFrame = NULL;
357 NSArray* array = [NSApp orderedWindows];
358
359 NSEnumerator* e = [array objectEnumerator];
360 while(NSWindow *win = [e nextObject])
361 {
362 id ctrl = [win delegate];
363 if ([ctrl isKindOfClass:[XAP_CocoaFrameController class]])
364 {
365 myFrame = [(XAP_CocoaFrameController*)ctrl frameImpl]->getFrame();
366 UT_ASSERT(myFrame);
367 return myFrame;
368 }
369 }
370 UT_DEBUGMSG(("Could not find Frame\n"));
371 return myFrame;
372 }
373
374 const char* XAP_CocoaApp::_findNearestFont(const char* pszFontFamily,
375 const char* /*pszFontStyle*/,
376 const char* /*pszFontVariant*/,
377 const char* /*pszFontWeight*/,
378 const char* /*pszFontStretch*/,
379 const char* /*pszFontSize*/,
380 const char * /*pszLang*/)
381 {
382 return pszFontFamily;
383 }