1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /* AbiWord
4  * Copyright (C) 2005 Martin Sevior
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301 USA.
20  */
21 
22 #include "ut_string.h"
23 #include "ut_assert.h"
24 #include "ut_debugmsg.h"
25 
26 #include "xap_App.h"
27 #include "xap_CocoaApp.h"
28 #include "xap_CocoaWidget.h"
29 #include "xap_Frame.h"
30 
31 #include "ap_Dialog_Id.h"
32 #include "ap_Strings.h"
33 
34 #include "ap_CocoaDialog_Latex.h"
35 
static_constructor(XAP_DialogFactory * pFactory,XAP_Dialog_Id dlgid)36 XAP_Dialog * AP_CocoaDialog_Latex::static_constructor(XAP_DialogFactory * pFactory, XAP_Dialog_Id dlgid)
37 {
38 	return new AP_CocoaDialog_Latex(pFactory,dlgid);
39 }
40 
AP_CocoaDialog_Latex(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id dlgid)41 AP_CocoaDialog_Latex::AP_CocoaDialog_Latex(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id dlgid) :
42 		 AP_Dialog_Latex(pDlgFactory,dlgid)
43 {
44 	// ...
45 }
46 
~AP_CocoaDialog_Latex(void)47 AP_CocoaDialog_Latex::~AP_CocoaDialog_Latex(void)
48 {
49 	// ...
50 }
51 
runModeless(XAP_Frame *)52 void AP_CocoaDialog_Latex::runModeless(XAP_Frame * /*pFrame*/)
53 {
54 	m_dlg = [[AP_CocoaDialog_LatexController alloc] initFromNib];
55 
56 	[m_dlg setXAPOwner:this];
57 
58 	[m_dlg window]; // just to make sure the nib is loaded.
59 
60 	/* Save dialog the ID number and pointer to the widget
61 	 */
62 	UT_sint32 sid = static_cast<UT_sint32>(getDialogId());
63 	m_pApp->rememberModelessId (sid, (XAP_Dialog_Modeless *) m_pDialog);
64 
65 	activate();
66 }
67 
activate(void)68 void  AP_CocoaDialog_Latex::activate(void)
69 {
70 	if (m_dlg)
71 		{
72 			[[m_dlg window] orderFront:m_dlg];
73 		}
74 }
75 
notifyActiveFrame(XAP_Frame *)76 void AP_CocoaDialog_Latex::notifyActiveFrame(XAP_Frame */*pFrame*/)
77 {
78 	// ...
79 }
80 
event_Insert(void)81 void AP_CocoaDialog_Latex::event_Insert(void)
82 {
83 	getLatexFromGUI();
84 
85 	if (convertLatexToMathML())
86 		{
87 			insertIntoDoc();
88 		}
89 }
90 
event_Close(void)91 void AP_CocoaDialog_Latex::event_Close(void)
92 {
93 	destroy();
94 }
95 
destroy(void)96 void AP_CocoaDialog_Latex::destroy(void)
97 {
98 	m_answer = AP_Dialog_Latex::a_CANCEL;
99 
100 	modeless_cleanup();
101 
102 	[m_dlg close];
103 	[m_dlg release];
104 	m_dlg = nil;
105 }
106 
setLatexInGUI(void)107 void AP_CocoaDialog_Latex::setLatexInGUI(void)
108 {
109 	if (m_dlg)
110 		{
111 			UT_UTF8String latex;
112 
113 			getLatex(latex);
114 
115 			[m_dlg setEditorText:[NSString stringWithUTF8String:(latex.utf8_str())]];
116 		}
117 }
118 
getLatexFromGUI(void)119 bool AP_CocoaDialog_Latex::getLatexFromGUI(void)
120 {
121 	bool bOkay = false;
122 
123 	if (m_dlg)
124 		if (NSString * editorText = [m_dlg editorText])
125 			if ([editorText length])
126 				{
127 					bOkay = true;
128 
129 					UT_UTF8String latex([editorText UTF8String]);
130 
131 					setLatex(latex);
132 				}
133 	return bOkay;
134 }
135 
136 /*****************************************************************/
137 
138 @implementation AP_CocoaDialog_LatexController
139 
140 - (id)initFromNib
141 {
142 	if (![super initWithWindowNibName:@"ap_CocoaDialog_Latex"]) {
143 		return nil;
144 	}
145 	return self;
146 }
147 
148 - (void)dealloc
149 {
150 	// ...
151 	[super dealloc];
152 }
153 
154 - (void)setXAPOwner:(XAP_Dialog *)owner
155 {
156 	_xap = static_cast<AP_CocoaDialog_Latex *>(owner);
157 }
158 
159 - (void)discardXAP
160 {
161 	_xap = nil;
162 }
163 
164 - (void)windowDidLoad
165 {
166 	if (_xap)
167 		{
168 			const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
169 
170 			LocalizeControl([self window],	pSS, AP_STRING_ID_DLG_Latex_LatexTitle);
171 
172 			LocalizeControl(oClose,			pSS, AP_STRING_ID_DLG_CloseButton);
173 			LocalizeControl(oInsert,		pSS, AP_STRING_ID_DLG_InsertButton);
174 
175 			LocalizeControl(oHeadingText,	pSS, AP_STRING_ID_DLG_Latex_LatexEquation);
176 			LocalizeControl(oExampleText,	pSS, AP_STRING_ID_DLG_Latex_Example);
177 		}
178 }
179 
180 - (void)windowWillClose:(NSNotification *)aNotification
181 {
182 	UT_UNUSED(aNotification);
183 	if (_xap)
184 		_xap->event_Close();
185 }
186 
187 - (IBAction)aClose:(id)sender
188 {
189 	UT_UNUSED(sender);
190 	if (_xap)
191 		_xap->event_Close();
192 }
193 
194 - (IBAction)aInsert:(id)sender
195 {
196 	UT_UNUSED(sender);
197 	if (_xap)
198 		_xap->event_Insert();
199 }
200 
201 - (void)setEditorText:(NSString *)text
202 {
203     [oEditor setString:text];
204 }
205 
206 - (NSString *)editorText
207 {
208 	return [oEditor string];
209 }
210 
211 @end
212