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, 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 #include <stdlib.h>
24 #include "ut_string.h"
25 #include "ut_assert.h"
26 #include "ut_debugmsg.h"
27 
28 // This header defines some functions for Cocoa dialogs,
29 // like centering them, measuring them, etc.
30 #include "xap_CocoaDialog_Utilities.h"
31 
32 #include "gr_CocoaCairoGraphics.h"
33 
34 #include "xap_App.h"
35 #include "xap_CocoaApp.h"
36 #include "xap_CocoaFrame.h"
37 
38 #include "xap_Dialog_Id.h"
39 #include "xap_Dlg_Zoom.h"
40 #include "xap_CocoaDlg_Zoom.h"
41 #include "xav_View.h"
42 
43 /*****************************************************************/
44 
static_constructor(XAP_DialogFactory * pFactory,XAP_Dialog_Id dlgid)45 XAP_Dialog * XAP_CocoaDialog_Zoom::static_constructor(XAP_DialogFactory * pFactory, XAP_Dialog_Id dlgid)
46 {
47 	XAP_CocoaDialog_Zoom * p = new XAP_CocoaDialog_Zoom(pFactory, dlgid);
48 	return p;
49 }
50 
XAP_CocoaDialog_Zoom(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id dlgid)51 XAP_CocoaDialog_Zoom::XAP_CocoaDialog_Zoom(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id dlgid) :
52 	XAP_Dialog_Zoom(pDlgFactory, dlgid)
53 {
54 	//
55 }
56 
~XAP_CocoaDialog_Zoom(void)57 XAP_CocoaDialog_Zoom::~XAP_CocoaDialog_Zoom(void)
58 {
59 }
60 
61 /*****************************************************************/
62 
runModal(XAP_Frame * pFrame)63 void XAP_CocoaDialog_Zoom::runModal(XAP_Frame * pFrame)
64 {
65 	UT_ASSERT(pFrame);
66 	if (!pFrame)
67 		return;
68 
69 	m_pFrame = pFrame;
70 
71 	m_dlg = [[XAP_CocoaDlg_ZoomController alloc] initFromNib];
72 
73 	[m_dlg setXAPOwner:this];
74 
75 	NSWindow * window = [m_dlg window];
76 
77 	_populateWindowData();
78 
79 	[NSApp runModalForWindow:window];
80 
81 	_storeWindowData();
82 
83 	[m_dlg close];
84 	[m_dlg release];
85 	m_dlg = nil;
86 
87 	m_pFrame = NULL;
88 }
89 
90 
event_Close(void)91 void XAP_CocoaDialog_Zoom::event_Close(void)
92 {
93 	m_answer = XAP_Dialog_Zoom::a_CANCEL;
94 	[NSApp stopModal];
95 }
96 
event_Radio200Clicked(void)97 void XAP_CocoaDialog_Zoom::event_Radio200Clicked(void)
98 {
99 	setZoomType(XAP_Frame::z_200);
100 
101 	[m_dlg setPercentValue:((int) getZoomPercent())];
102 	[m_dlg _enablePercentSpin:NO];
103 
104 	_updatePreviewZoomPercent(getZoomPercent());
105 }
106 
event_Radio100Clicked(void)107 void XAP_CocoaDialog_Zoom::event_Radio100Clicked(void)
108 {
109 	setZoomType(XAP_Frame::z_100);
110 
111 	[m_dlg setPercentValue:((int) getZoomPercent())];
112 	[m_dlg _enablePercentSpin:NO];
113 
114 	_updatePreviewZoomPercent(getZoomPercent());
115 }
116 
event_Radio75Clicked(void)117 void XAP_CocoaDialog_Zoom::event_Radio75Clicked(void)
118 {
119 	setZoomType(XAP_Frame::z_75);
120 
121 	[m_dlg setPercentValue:((int) getZoomPercent())];
122 	[m_dlg _enablePercentSpin:NO];
123 
124 	_updatePreviewZoomPercent(getZoomPercent());
125 }
126 
event_RadioPageWidthClicked(void)127 void XAP_CocoaDialog_Zoom::event_RadioPageWidthClicked(void)
128 {
129 	setZoomType(XAP_Frame::z_PAGEWIDTH);
130 
131 	[m_dlg setPercentValue:((int) getZoomPercent())];
132 	[m_dlg _enablePercentSpin:NO];
133 
134 	_updatePreviewZoomPercent(getZoomPercent());
135 }
136 
event_RadioWholePageClicked(void)137 void XAP_CocoaDialog_Zoom::event_RadioWholePageClicked(void)
138 {
139 	setZoomType(XAP_Frame::z_WHOLEPAGE);
140 
141 	[m_dlg setPercentValue:((int) getZoomPercent())];
142 	[m_dlg _enablePercentSpin:NO];
143 
144 	_updatePreviewZoomPercent(getZoomPercent());
145 }
146 
event_RadioPercentClicked(void)147 void XAP_CocoaDialog_Zoom::event_RadioPercentClicked(void)
148 {
149 	setZoomType(XAP_Frame::z_PERCENT);
150 
151 	[m_dlg _enablePercentSpin:YES];
152 }
153 
event_SpinPercentChanged(void)154 void XAP_CocoaDialog_Zoom::event_SpinPercentChanged(void)
155 {
156 	setZoomPercent((UT_uint32) [m_dlg percentValue]);
157 
158 	[m_dlg setPercentValue:((int) getZoomPercent())];
159 
160 	_updatePreviewZoomPercent(getZoomPercent());
161 }
162 
163 /*****************************************************************/
164 
_populateWindowData(void)165 void XAP_CocoaDialog_Zoom::_populateWindowData(void)
166 {
167 	// The callbacks for these radio buttons aren't always
168 	// called when the dialog is being constructed, so we have to
169 	// set the widget's value, then manually enable/disable
170 	// the spin button.
171 
172 	// enable the right button
173 	[m_dlg _enablePercentSpin:NO];
174 	XAP_Frame::tZoomType zoomType = getZoomType();
175 	switch(zoomType)
176 	{
177 	case XAP_Frame::z_200:
178 		_updatePreviewZoomPercent(200);
179 		break;
180 	case XAP_Frame::z_100:
181 		_updatePreviewZoomPercent(100);
182 		break;
183 	case XAP_Frame::z_75:
184 		_updatePreviewZoomPercent(75);
185 		break;
186 	case XAP_Frame::z_PAGEWIDTH:
187 		break;
188 	case XAP_Frame::z_WHOLEPAGE:
189 		break;
190 	case XAP_Frame::z_PERCENT:
191 		[m_dlg _enablePercentSpin:YES];
192 		_updatePreviewZoomPercent(getZoomPercent());
193 		break;
194 	default:
195 		// if they haven't set anything yet, default to the 100% radio item
196 		zoomType = XAP_Frame::z_100;
197 	}
198 	[[m_dlg zoomMatrix] selectCellWithTag:(int)zoomType];
199 	[m_dlg setPercentValue:((int) getZoomPercent())];
200 }
201 
_storeWindowData(void)202 void XAP_CocoaDialog_Zoom::_storeWindowData(void)
203 {
204 	m_zoomType = (XAP_Frame::tZoomType)[[[m_dlg zoomMatrix] selectedCell] tag];
205 	// store away percentage; the base class decides if it's important when
206 	// the caller requests the percent
207 	m_zoomPercent = [m_dlg percentValue];
208 }
209 
210 
211 //****************************************************************
212 
213 @implementation XAP_CocoaDlg_ZoomController
214 
215 - (id) initFromNib
216 {
217 	return [super initWithWindowNibName:@"xap_CocoaDlg_Zoom"];
218 }
219 
220 
221 - (void)setXAPOwner:(XAP_Dialog*)owner
222 {
223 	_xap = dynamic_cast<XAP_CocoaDialog_Zoom*>(owner);
224 	UT_ASSERT(_xap);
225 }
226 
227 - (void)discardXAP
228 {
229 	_xap = nil;
230 }
231 
232 - (void)windowDidLoad
233 {
234 	const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
235 
236 	LocalizeControl ([self window],	pSS, XAP_STRING_ID_DLG_Zoom_ZoomTitle);
237 
238 	LocalizeControl (_closeBtn,	pSS, XAP_STRING_ID_DLG_Close);
239 
240 	LocalizeControl (_zoomBox,		pSS, XAP_STRING_ID_DLG_Zoom_RadioFrameCaption);
241 
242 	LocalizeControl (_zoom200Btn,	pSS, XAP_STRING_ID_DLG_Zoom_200);
243 	LocalizeControl (_zoom100Btn,	pSS, XAP_STRING_ID_DLG_Zoom_100);
244 	LocalizeControl (_zoom75Btn,	pSS, XAP_STRING_ID_DLG_Zoom_75);
245 	LocalizeControl (_pageWidthBtn,	pSS, XAP_STRING_ID_DLG_Zoom_PageWidth);
246 	LocalizeControl (_wholePageBtn,	pSS, XAP_STRING_ID_DLG_Zoom_WholePage);
247 	LocalizeControl (_percentBtn,	pSS, XAP_STRING_ID_DLG_Zoom_Percent);
248 
249 	[_zoom200Btn	setTag:(int)XAP_Frame::z_200];
250 	[_zoom100Btn	setTag:(int)XAP_Frame::z_100];
251 	[_zoom75Btn		setTag:(int)XAP_Frame::z_75];
252 	[_pageWidthBtn	setTag:(int)XAP_Frame::z_PAGEWIDTH];
253 	[_wholePageBtn	setTag:(int)XAP_Frame::z_WHOLEPAGE];
254 	[_percentBtn	setTag:(int)XAP_Frame::z_PERCENT];
255 
256 	[_percentStepper setMinValue:((double) XAP_DLG_ZOOM_MINIMUM_ZOOM)];
257 	[_percentStepper setMaxValue:((double) XAP_DLG_ZOOM_MAXIMUM_ZOOM)];
258 }
259 
260 - (IBAction)closeAction:(id)sender
261 {
262 	UT_UNUSED(sender);
263 	_xap->event_Close();
264 }
265 
266 
267 - (IBAction)zoom200Action:(id)sender
268 {
269 	UT_UNUSED(sender);
270 	_xap->event_Radio200Clicked();
271 }
272 
273 - (IBAction)zoom100Action:(id)sender
274 {
275 	UT_UNUSED(sender);
276 	_xap->event_Radio100Clicked();
277 }
278 
279 - (IBAction)zoom75Action:(id)sender
280 {
281 	UT_UNUSED(sender);
282 	_xap->event_Radio75Clicked();
283 }
284 
285 - (IBAction)zoomPageWidthAction:(id)sender
286 {
287 	UT_UNUSED(sender);
288 	_xap->event_RadioPageWidthClicked();
289 }
290 
291 - (IBAction)zoomWholePageAction:(id)sender
292 {
293 	UT_UNUSED(sender);
294 	_xap->event_RadioWholePageClicked();
295 }
296 
297 - (IBAction)zoomPercentAction:(id)sender
298 {
299 	UT_UNUSED(sender);
300 	_xap->event_RadioPercentClicked();
301 }
302 
303 - (IBAction)stepperAction:(id)sender
304 {
305 	[_percentField setIntValue:[sender intValue]];
306 
307 	_xap->event_SpinPercentChanged();
308 }
309 
310 - (IBAction)zoomChangedAction:(id)sender
311 {
312 	UT_UNUSED(sender);
313 	int percent = [_percentField intValue];
314 	percent = (percent < XAP_DLG_ZOOM_MINIMUM_ZOOM) ? XAP_DLG_ZOOM_MINIMUM_ZOOM :
315 			 ((percent > XAP_DLG_ZOOM_MAXIMUM_ZOOM) ? XAP_DLG_ZOOM_MAXIMUM_ZOOM : percent);
316 	[self setPercentValue:percent];
317 
318 	_xap->event_SpinPercentChanged();
319 }
320 
321 - (NSMatrix*)zoomMatrix
322 {
323 	return _zoomMatrix;
324 }
325 
326 - (void)setPercentValue:(int)value
327 {
328 	[_percentField   setIntValue:value];
329 	[_percentStepper setIntValue:value];
330 }
331 
332 - (int)percentValue
333 {
334 	return [_percentField intValue];
335 }
336 
337 - (void)_enablePercentSpin:(BOOL)enable
338 {
339 	[_percentStepper setEnabled:enable];
340 	[_percentField   setEnabled:enable];
341 }
342 
343 @end
344 
345