1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /* AbiWord
4  * Copyright (C) 1998 AbiSource, Inc.
5  * Copyright (C) 2001-2003 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 
24 #import <Cocoa/Cocoa.h>
25 
26 #import "ut_types.h"
27 #import "ut_debugmsg.h"
28 #import "ut_assert.h"
29 #import "gr_CocoaCairoGraphics.h"
30 #import "ev_CocoaToolbar.h"
31 #import "ev_CocoaMouse.h"
32 
33 #import "xav_View.h"
34 #import "xap_CocoaCompat.h"
35 #import "xap_CocoaApp.h"
36 #import "xap_CocoaTextView.h"
37 #import "xap_CocoaToolbarWindow.h"
38 
39 #import "ap_FrameData.h"
40 #import "ap_CocoaFrame.h"
41 #import "ap_CocoaFrameImpl.h"
42 #import "ap_CocoaTopRuler.h"
43 #import "ap_CocoaLeftRuler.h"
44 #import "ap_CocoaStatusBar.h"
45 
46 /*!
47 	custom wrapper for NSScrollers.
48  */
49 @interface XAP_NSScroller : NSScroller
50 {
51 }
52 -(id)initWithFrame:(NSRect)frame andController:(NSWindowController*)controller vertical:(BOOL)vertical;
53 
54 @end
55 
56 /*****************************************************************/
AP_CocoaFrameImpl(AP_CocoaFrame * pCocoaFrame)57 AP_CocoaFrameImpl::AP_CocoaFrameImpl(AP_CocoaFrame *pCocoaFrame)
58 	: XAP_CocoaFrameImpl (pCocoaFrame),
59 		m_hScrollbar(nil),
60 		m_vScrollbar(nil),
61 		m_docAreaGRView(nil),
62 		m_HMinScroll(0),
63 		m_HMaxScroll(0),
64 		m_HCurrentScroll(0),
65 		m_VMinScroll(0),
66 		m_VMaxScroll(0),
67 		m_VCurrentScroll(0)
68 {
69 }
70 
createInstance(XAP_Frame * pFrame)71 XAP_FrameImpl * AP_CocoaFrameImpl::createInstance(XAP_Frame *pFrame)
72 {
73 	return new AP_CocoaFrameImpl(static_cast<AP_CocoaFrame*>(pFrame));
74 }
75 
_setHScrollValue(UT_sint32 value)76 void AP_CocoaFrameImpl::_setHScrollValue(UT_sint32 value)
77 {
78 	if (m_HCurrentScroll != value) {
79 		m_HCurrentScroll = value;
80 		if (m_HCurrentScroll < m_HMinScroll) {
81 			m_HCurrentScroll = m_HMinScroll;
82 		}
83 		if (m_HCurrentScroll > m_HMaxScroll) {
84 			m_HCurrentScroll = m_HMaxScroll;
85 		}
86 		UT_DEBUGMSG(("_setHScrollValue : %d\n", value));
87 		_setHScrollbarValues();
88 	}
89 }
90 
_setHScrollMin(UT_sint32 value)91 void AP_CocoaFrameImpl::_setHScrollMin(UT_sint32 value)
92 {
93 	if (m_HMinScroll != value) {
94 		m_HMinScroll = value;
95 		if (m_HCurrentScroll < m_HMinScroll) {
96 			m_HCurrentScroll = m_HMinScroll;
97 		}
98 		UT_DEBUGMSG(("_setHScrollMin : %d\n", value));
99 		_setHScrollbarValues();
100 	}
101 }
102 
_setHScrollMax(UT_sint32 value)103 void AP_CocoaFrameImpl::_setHScrollMax(UT_sint32 value)
104 {
105 	if (m_HMaxScroll != value) {
106 		m_HMaxScroll = value;
107 		if (m_HCurrentScroll > m_HMaxScroll) {
108 			m_HCurrentScroll = m_HMaxScroll;
109 		}
110 		UT_DEBUGMSG(("_setHScrollMax : %d\n", value));
111 		_setHScrollbarValues();
112 	}
113 }
114 
_setHVisible(UT_sint32 value)115 void AP_CocoaFrameImpl::_setHVisible(UT_sint32 value)
116 {
117 	if (m_HVisible != value) {
118 		m_HVisible = value;
119 		_setHScrollbarValues();
120 	}
121 }
122 
_setVScrollValue(UT_sint32 value)123 void AP_CocoaFrameImpl::_setVScrollValue(UT_sint32 value)
124 {
125 	if (m_VCurrentScroll != value) {
126 		m_VCurrentScroll = value;
127 		if (m_VCurrentScroll < m_VMinScroll) {
128 			m_VCurrentScroll = m_VMinScroll;
129 		}
130 		if (m_VCurrentScroll > m_VMaxScroll) {
131 			m_VCurrentScroll = m_VMaxScroll;
132 		}
133 		UT_DEBUGMSG(("_setVScrollValue : %d\n", value));
134 		_setVScrollbarValues();
135 	}
136 }
137 
_setVScrollMin(UT_sint32 value)138 void AP_CocoaFrameImpl::_setVScrollMin(UT_sint32 value)
139 {
140 	if (m_VMinScroll != value) {
141 		m_VMinScroll = value;
142 		if (m_VCurrentScroll < m_VMinScroll) {
143 			m_VCurrentScroll = m_VMinScroll;
144 		}
145 		UT_DEBUGMSG(("_setVScrollMin : %d\n", value));
146 		_setVScrollbarValues();
147 	}
148 }
149 
_setVScrollMax(UT_sint32 value)150 void AP_CocoaFrameImpl::_setVScrollMax(UT_sint32 value)
151 {
152 	if (m_VMaxScroll != value) {
153 		m_VMaxScroll = value;
154 		if (m_VCurrentScroll > m_VMaxScroll) {
155 			m_VCurrentScroll = m_VMaxScroll;
156 		}
157 		UT_DEBUGMSG(("_setVScrollMax : %d\n", value));
158 		_setVScrollbarValues();
159 	}
160 }
161 
_setVVisible(UT_sint32 value)162 void AP_CocoaFrameImpl::_setVVisible(UT_sint32 value)
163 {
164 	if (m_VVisible != value) {
165 		m_VVisible = value;
166 		_setVScrollbarValues();
167 	}
168 }
169 
_setHScrollbarValues()170 void AP_CocoaFrameImpl::_setHScrollbarValues()
171 {
172 	float value;
173 	CGFloat knob;
174 	if (m_HMaxScroll == 0) {
175 		value = 0.0;
176 	}
177 	else {
178 		value = ((float)m_HCurrentScroll / (float)m_HMaxScroll);
179 	}
180 	if (m_HMaxScroll == 0) {
181 		knob = 1.0;
182 	}
183 	else {
184 		knob = ((CGFloat)m_HVisible / (CGFloat)(m_HVisible + m_HMaxScroll));
185 	}
186 	UT_DEBUGMSG(("_setHScrollbarValues(), max = %d, current = %d, visible = %d\n", m_HMaxScroll, m_HCurrentScroll, m_HVisible));
187 	UT_DEBUGMSG(("_setHScrollbarValues(), value = %f, knob = %f\n", value, knob));
188 	if (knob >= 1.0) {
189 		[m_hScrollbar setEnabled:NO];
190 	}
191 	else {
192 		[m_hScrollbar setEnabled:YES];
193 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
194 		[m_hScrollbar setKnobProportion:knob];
195 		[m_hScrollbar setDoubleValue:value];
196 #else
197 		[m_hScrollbar setFloatValue:value knobProportion:knob];
198 #endif
199 	}
200 	[m_hScrollbar setNeedsDisplay:YES];
201 	[[(AP_CocoaFrameController*)_getController() getHRuler] setNeedsDisplay:YES];
202 }
203 
204 
_setVScrollbarValues()205 void AP_CocoaFrameImpl::_setVScrollbarValues()
206 {
207 	float value;
208 	CGFloat knob;
209 	if (m_VMaxScroll == 0) {
210 		value = 0.0;
211 	}
212 	else {
213 		value = ((float)m_VCurrentScroll / (float)m_VMaxScroll);
214 	}
215 	if (m_VMaxScroll == 0) {
216 		knob = 1.0;
217 	}
218 	else {
219 		knob = ((CGFloat)m_VVisible / (CGFloat)(m_VVisible + m_VMaxScroll));
220 	}
221 	UT_DEBUGMSG(("_setVScrollbarValues(), max = %d, current = %d, visible = %d\n", m_VMaxScroll, m_VCurrentScroll, m_VVisible));
222 	UT_DEBUGMSG(("_setVScrollbarValues(), value = %f, knob = %f\n", value, knob));
223 	if (knob >= 1.0) {
224 		[m_vScrollbar setEnabled:NO];
225 	}
226 	else {
227 		[m_vScrollbar setEnabled:YES];
228 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
229 		[m_hScrollbar setKnobProportion:knob];
230 		[m_hScrollbar setDoubleValue:value];
231 #else
232 		[m_hScrollbar setFloatValue:value knobProportion:knob];
233 #endif
234 	}
235 	[m_vScrollbar setNeedsDisplay:YES];
236 	// [[(_getController()) getVRuler] setNeedsDisplay:YES]; // ??
237 }
238 
239 
_scrollAction(id sender)240 void AP_CocoaFrameImpl::_scrollAction(id sender)
241 {
242 	float newValue = [sender floatValue];
243 	NSScrollerPart part = [sender hitPart];
244 	AV_View * pView = getFrame()->getCurrentView();
245 	GR_Graphics * pGr = pView->getGraphics();
246 
247 	UT_DEBUGMSG(("newValue = %f\n", newValue));
248 	switch (part) {
249 	case NSScrollerNoPart:
250 		UT_DEBUGMSG(("No Scroll\n"));
251 		return;
252 		break;
253     case NSScrollerDecrementPage:
254 		UT_DEBUGMSG(("NSScrollerDecrementPage\n"));
255 		if (sender == m_vScrollbar) {
256 			_setVScrollValue(m_VCurrentScroll - m_VVisible);
257 		}
258 		else {
259 			_setHScrollValue(m_HCurrentScroll - m_HVisible);
260 		}
261 		break;
262     case NSScrollerIncrementPage:
263 		UT_DEBUGMSG(("NSScrollerIncrementPage\n"));
264 		if (sender == m_vScrollbar) {
265 			_setVScrollValue(m_VCurrentScroll + m_VVisible);
266 		}
267 		else {
268 			_setHScrollValue(m_HCurrentScroll + m_HVisible);
269 		}
270 		break;
271     case NSScrollerDecrementLine:
272 		UT_DEBUGMSG(("NSScrollerDecrementLine\n"));
273 		if (sender == m_vScrollbar) {
274 			_setVScrollValue(m_VCurrentScroll - pGr->tlu(20));
275 		}
276 		else {
277 			_setHScrollValue(m_HCurrentScroll - pGr->tlu(20));
278 		}
279 		break;
280     case NSScrollerIncrementLine:
281 		UT_DEBUGMSG(("NSScrollerIncrementLine\n"));
282 		if (sender == m_vScrollbar) {
283 			_setVScrollValue(m_VCurrentScroll + pGr->tlu(20));
284 		}
285 		else {
286 			_setHScrollValue(m_HCurrentScroll + pGr->tlu(20));
287 		}
288 		break;
289     case NSScrollerKnob	:
290     case NSScrollerKnobSlot	:
291 		UT_DEBUGMSG(("NSScrollerKnob\n"));
292 		if (sender == m_vScrollbar) {
293 			m_VCurrentScroll = lrintf(newValue * m_VMaxScroll);
294 			_setVScrollbarValues();
295 		}
296 		else {
297 			m_HCurrentScroll = lrintf(newValue * m_HMaxScroll);
298 			_setHScrollbarValues();
299 		}
300 		break;
301 	default:
302 		break;
303 	}
304 	if (sender == m_vScrollbar) {
305 		pView->sendVerticalScrollEvent(m_VCurrentScroll);
306 	}
307 	else {
308 		pView->sendHorizontalScrollEvent(m_HCurrentScroll);
309 	}
310 }
311 
312 
_showTopRulerNSView(void)313 void AP_CocoaFrameImpl::_showTopRulerNSView(void)
314 {
315 	XAP_CocoaFrameController* ctrl = _getController();
316 	UT_ASSERT([ctrl isKindOfClass:[XAP_CocoaFrameController class]]);
317 	AP_CocoaFrameController* apCtrl = (AP_CocoaFrameController*)ctrl;
318 	XAP_CocoaNSView* ruler = [apCtrl getHRuler];
319 	XAP_CocoaNSView* vRuler = [apCtrl getVRuler];
320 	NSView* mainView = [ctrl getMainView];
321 
322 	UT_ASSERT([ruler superview] == nil);
323 	// make sure it is not visible
324 	if ([ruler superview]) {
325 		NSLog(@"AP_CocoaFrameImpl::_showTopRulerNSView attempted to show already visible ruler");
326 		return;
327 	}
328 
329 	UT_ASSERT(mainView);
330 	NSRect mainFrame = [mainView frame];
331 	NSRect rulerFrame = [ruler frame];
332 	mainFrame.size.height -= rulerFrame.size.height;
333 	[mainView setFrame:mainFrame];
334 	rulerFrame.origin.y = mainFrame.origin.y + mainFrame.size.height;
335 	rulerFrame.size.width = mainFrame.size.width + mainFrame.origin.x;
336 	[ruler setFrame:rulerFrame];
337 	if ([vRuler superview]) {
338 		NSRect vRulerFrame = [vRuler frame];
339 		vRulerFrame.size.height = mainFrame.size.height;
340 		[vRuler setFrame:vRulerFrame];
341 	}
342 	[[mainView superview] addSubview:ruler];
343 	[ruler release];	/* when we hide the ruler we retain the object before the removeFromSuperview */
344 }
345 
346 
_hideTopRulerNSView(void)347 void AP_CocoaFrameImpl::_hideTopRulerNSView(void)
348 {
349 	XAP_CocoaFrameController* ctrl = _getController();
350 	UT_ASSERT([ctrl isKindOfClass:[XAP_CocoaFrameController class]]);
351 	AP_CocoaFrameController* apCtrl = (AP_CocoaFrameController*)ctrl;
352 	XAP_CocoaNSView* ruler = [apCtrl getHRuler];
353 	XAP_CocoaNSView* vRuler = [apCtrl getVRuler];
354 	NSView* mainView = [ctrl getMainView];
355 
356 	UT_ASSERT([ruler superview]);
357 	// make sure it is not visible
358 	if ([ruler superview] == nil) {
359 		NSLog(@"AP_CocoaFrameImpl::_hideTopRulerNSView attempted to hide already hidden ruler");
360 		return;
361 	}
362 
363 	UT_ASSERT(mainView);
364 	NSRect mainFrame = [mainView frame];
365 	NSRect rulerFrame = [ruler frame];
366 	mainFrame.size.height += rulerFrame.size.height;
367 	[mainView setFrame:mainFrame];
368 	if ([vRuler superview]) {
369 		NSRect vRulerFrame = [vRuler frame];
370 		vRulerFrame.size.height = mainFrame.size.height;
371 		[vRuler setFrame:vRulerFrame];
372 	}
373 //	rulerFrame.origin.y = mainFrame.origin.y + mainFrame.size.height + rulerFrame.size.height;
374 //	[ruler setFrame:rulerFrame];
375 	[ruler retain];
376 	[ruler removeFromSuperview];
377 }
378 
379 
_showLeftRulerNSView(void)380 void AP_CocoaFrameImpl::_showLeftRulerNSView(void)
381 {
382 	XAP_CocoaFrameController* ctrl = _getController();
383 	UT_ASSERT([ctrl isKindOfClass:[XAP_CocoaFrameController class]]);
384 	AP_CocoaFrameController* apCtrl = (AP_CocoaFrameController*)ctrl;
385 	XAP_CocoaNSView* ruler = [apCtrl getVRuler];
386 	NSView* mainView = [ctrl getMainView];
387 
388 	UT_ASSERT([ruler superview] == nil);
389 	// make sure it is not visible
390 	if ([ruler superview]) {
391 		NSLog(@"AP_CocoaFrameImpl::_showLeftRulerNSView attempted to show already visible ruler");
392 		return;
393 	}
394 
395 	UT_ASSERT(mainView);
396 	NSRect mainFrame = [mainView frame];
397 	NSRect rulerFrame = [ruler frame];
398 	mainFrame.size.width -= rulerFrame.size.width;
399 	mainFrame.origin.x = rulerFrame.size.width;
400 	[mainView setFrame:mainFrame];
401 	rulerFrame.origin.x = 0;
402 	rulerFrame.origin.y = mainFrame.origin.y;
403 	rulerFrame.size.height = mainFrame.size.height;
404 	[ruler setFrame:rulerFrame];
405 	[[mainView superview] addSubview:ruler];
406 	[ruler release];	/* when we hide the ruler we retain the object before the removeFromSuperview */
407 }
408 
409 
_hideLeftRulerNSView(void)410 void AP_CocoaFrameImpl::_hideLeftRulerNSView(void)
411 {
412 	XAP_CocoaFrameController* ctrl = _getController();
413 	UT_ASSERT([ctrl isKindOfClass:[XAP_CocoaFrameController class]]);
414 	AP_CocoaFrameController* apCtrl = (AP_CocoaFrameController*)ctrl;
415 	XAP_CocoaNSView* ruler = [apCtrl getVRuler];
416 	NSView* mainView = [ctrl getMainView];
417 
418 	UT_ASSERT([ruler superview]);
419 	// make sure it is not visible
420 	if ([ruler superview] == nil) {
421 		NSLog(@"AP_CocoaFrameImpl::_hideLeftRulerNSView attempted to hide already hidden ruler");
422 		return;
423 	}
424 
425 	UT_ASSERT(mainView);
426 	NSRect mainFrame = [mainView frame];
427 	NSRect rulerFrame = [ruler frame];
428 	mainFrame.size.width += mainFrame.origin.x;
429 	mainFrame.origin.x = 0;
430 	[mainView setFrame:mainFrame];
431 	[ruler retain];
432 	[ruler removeFromSuperview];
433 }
434 
_createDocView(GR_Graphics * & pG)435 void AP_CocoaFrameImpl::_createDocView(GR_Graphics* &pG)
436 {
437 	XAP_Frame*	pFrame = getFrame();
438 	NSView*		docArea = [_getController() getMainView];
439 
440     NSArray* docAreaSubviews = [[docArea subviews] copy];
441     if ([docAreaSubviews count] > 0) {
442 		[docAreaSubviews makeObjectsPerformSelector:@selector(removeFromSuperviewWithoutNeedingDisplay)];
443         m_hScrollbar = NULL;
444         m_vScrollbar = NULL;
445         m_docAreaGRView = NULL;
446     }
447 	[docAreaSubviews release];
448 
449 	NSRect frame = [docArea bounds];
450 	NSRect controlFrame;
451 
452 	/* vertical scrollbar */
453 	controlFrame.origin.y = [NSScroller scrollerWidth];
454 	controlFrame.size.width = [NSScroller scrollerWidth];
455 	controlFrame.size.height = frame.size.height - controlFrame.origin.y;
456 	controlFrame.origin.x = frame.size.width - controlFrame.size.width;
457 	m_vScrollbar = [[XAP_NSScroller alloc] initWithFrame:controlFrame andController:_getController()
458 						vertical:YES];
459 	[docArea addSubview:m_vScrollbar];
460 	[m_vScrollbar setEnabled:YES];
461 	[m_vScrollbar release];
462 
463 	/* horizontal scrollbar */
464 	controlFrame.origin.x = 0;
465 	controlFrame.origin.y = 0;
466 	controlFrame.size.height = [NSScroller scrollerWidth];
467 	controlFrame.size.width = frame.size.width - controlFrame.size.height;
468 	m_hScrollbar = [[XAP_NSScroller alloc] initWithFrame:controlFrame andController:_getController()
469 						vertical:NO];
470 	[docArea addSubview:m_hScrollbar];
471 	[m_hScrollbar setEnabled:YES];
472 	[m_hScrollbar release];
473 
474 	/* doc view */
475 	controlFrame.origin.x = 0;
476 	controlFrame.origin.y = [NSScroller scrollerWidth];
477 	controlFrame.size.height = frame.size.height - controlFrame.origin.y;
478 	controlFrame.size.width = frame.size.width - [NSScroller scrollerWidth];
479 	m_docAreaGRView = [[XAP_CocoaTextView alloc] initWith:pFrame andFrame:controlFrame];
480 	[docArea addSubview:m_docAreaGRView];
481 	[m_docAreaGRView setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
482 	[m_docAreaGRView setEventDelegate:[[[AP_DocViewDelegate alloc] init] autorelease]];
483 	[_getController() setTextView:m_docAreaGRView];
484 	[[_getController() window] makeFirstResponder:m_docAreaGRView];
485 	[m_docAreaGRView release];
486 
487 	GR_CocoaCairoAllocInfo ai(m_docAreaGRView);
488 	pG = (GR_CocoaCairoGraphics*)XAP_App::getApp()->newGraphics(ai);
489 
490 	static_cast<GR_CocoaCairoGraphics *>(pG)->_setUpdateCallback (&_graphicsUpdateCB, (void *)pFrame);
491 }
492 
493 
_bindToolbars(AV_View * pView)494 void AP_CocoaFrameImpl::_bindToolbars(AV_View * pView)
495 {
496 	int nrToolbars = m_vecToolbarLayoutNames.getItemCount();
497 	for (int k = 0; k < nrToolbars; k++)
498 	{
499 		// TODO Toolbars are a frame-level item, but a view-listener is
500 		// TODO a view-level item.  I've bound the toolbar-view-listeners
501 		// TODO to the current view within this frame and have code in the
502 		// TODO toolbar to allow the view-listener to be rebound to a different
503 		// TODO view.  in the future, when we have support for multiple views
504 		// TODO in the frame (think splitter windows), we will need to have
505 		// TODO a loop like this to help change the focus when the current
506 		// TODO view changes.
507 
508 		EV_CocoaToolbar * pCocoaToolbar = (EV_CocoaToolbar *)m_vecToolbars.getNthItem(k);
509 		pCocoaToolbar->bindListenerToView(pView);
510 	}
511 }
512 
513 // Does the initial show/hide of statusbar (based on the user prefs).
514 // Idem.
_showOrHideStatusbar()515 void AP_CocoaFrameImpl::_showOrHideStatusbar()
516 {
517 	XAP_Frame*	pFrame = getFrame();
518 	bool bShowStatusBar = static_cast<AP_FrameData*> (pFrame->getFrameData())->m_bShowStatusBar;
519 	static_cast<AP_CocoaFrame *>(pFrame)->toggleStatusBar(bShowStatusBar);
520 }
521 
522 // Does the initial show/hide of toolbars (based on the user prefs).
523 // This is needed because toggleBar is called only when the user
524 // (un)checks the show {Stantandard,Format,Extra} toolbar checkbox,
525 // and thus we have to manually call this function at startup.
_showOrHideToolbars()526 void AP_CocoaFrameImpl::_showOrHideToolbars()
527 {
528 	XAP_Frame*	pFrame = getFrame();
529 	bool *bShowBar = static_cast<AP_FrameData*> (pFrame->getFrameData())->m_bShowBar;
530 	UT_uint32 cnt = m_vecToolbarLayoutNames.getItemCount();
531 
532 	XAP_CocoaToolbarWindow_Controller* tlbr = [XAP_CocoaToolbarWindow_Controller sharedToolbar];
533 	[tlbr lock];
534 	for (UT_uint32 i = 0; i < cnt; i++)
535 	{
536 		// TODO: The two next lines are here to bind the EV_Toolbar to the
537 		// AP_FrameData, but their correct place are next to the toolbar creation (JCA)
538 		EV_CocoaToolbar * pCocoaToolbar = static_cast<EV_CocoaToolbar *> (m_vecToolbars.getNthItem(i));
539 		static_cast<AP_FrameData*> (pFrame->getFrameData())->m_pToolbar[i] = pCocoaToolbar;
540 		static_cast<AP_CocoaFrame *>(pFrame)->toggleBar(i, bShowBar[i]);
541 	}
542 	[tlbr unlock];
543 	[tlbr redisplayToolbars:_getController()];
544 }
545 
546 
547 /*!
548  * Refills the framedata class with pointers to the current toolbars. We
549  * need to do this after a toolbar icon and been dragged and dropped.
550  */
_refillToolbarsInFrameData(void)551 void AP_CocoaFrameImpl::_refillToolbarsInFrameData(void)
552 {
553 	XAP_Frame*	pFrame = getFrame();
554 	UT_uint32 cnt = m_vecToolbarLayoutNames.getItemCount();
555 
556 	for (UT_uint32 i = 0; i < cnt; i++)
557 	{
558 		EV_CocoaToolbar * pCocoaToolbar = static_cast<EV_CocoaToolbar *> (m_vecToolbars.getNthItem(i));
559 		static_cast<AP_FrameData*> (pFrame->getFrameData())->m_pToolbar[i] = pCocoaToolbar;
560 	}
561 }
562 
_createDocumentWindow()563 void AP_CocoaFrameImpl::_createDocumentWindow()
564 {
565 	XAP_Frame*	pFrame = getFrame();
566 	AP_FrameData* pData = static_cast<AP_FrameData*> (pFrame->getFrameData());
567 	bool bShowRulers = pData->m_bShowRuler;
568 
569 	// create the rulers
570 	AP_CocoaTopRuler * pCocoaTopRuler = NULL;
571 	AP_CocoaLeftRuler * pCocoaLeftRuler = NULL;
572 
573 	if ( bShowRulers )
574 	{
575 		pCocoaTopRuler = new AP_CocoaTopRuler(pFrame);
576 		UT_ASSERT(pCocoaTopRuler);
577 
578 		if (pData->m_pViewMode == VIEW_PRINT) {
579 		    pCocoaLeftRuler = new AP_CocoaLeftRuler(pFrame);
580 		    UT_ASSERT(pCocoaLeftRuler);
581 		    pCocoaLeftRuler->createWidget();
582 		    // get the width from the left ruler and stuff it into the top ruler.
583 		    pCocoaTopRuler->setOffsetLeftRuler(pCocoaLeftRuler->getWidth());
584 		}
585 		else {
586 			_hideLeftRulerNSView();
587 		    pCocoaTopRuler->setOffsetLeftRuler(0);
588 		}
589 	}
590 	else {
591 		_hideTopRulerNSView();
592 		_hideLeftRulerNSView();
593 	}
594 
595 	pData->m_pTopRuler = pCocoaTopRuler;
596 	pData->m_pLeftRuler = pCocoaLeftRuler;
597 }
598 
_createStatusBarWindow(XAP_CocoaNSStatusBar *)599 void AP_CocoaFrameImpl::_createStatusBarWindow(XAP_CocoaNSStatusBar * /*statusBar*/)
600 {
601 	XAP_Frame*	pFrame = getFrame();
602 
603 	UT_DEBUGMSG (("AP_CocoaFrame::_createStatusBarWindow ()\n"));
604 	// TODO: pass the NSView instead of the whole frame
605 	AP_CocoaStatusBar * pCocoaStatusBar = new AP_CocoaStatusBar(pFrame);
606 	UT_ASSERT(pCocoaStatusBar);
607 
608 	static_cast<AP_FrameData *>(pFrame->getFrameData())->m_pStatusBar = pCocoaStatusBar;
609 	pCocoaStatusBar->createWidget();
610 }
611 
_setWindowIcon()612 void AP_CocoaFrameImpl::_setWindowIcon()
613 {
614 	// this is NOT needed. Just need to the the title.
615 }
616 
_getNibName()617 NSString *	AP_CocoaFrameImpl::_getNibName ()
618 {
619 	return @"ap_CocoaFrame";
620 }
621 
622 /*!
623 	Create and intialize the controller.
624  */
_createController()625 XAP_CocoaFrameController *AP_CocoaFrameImpl::_createController()
626 {
627 	UT_DEBUGMSG (("AP_CocoaFrame::_createController()\n"));
628 	return [[AP_CocoaFrameController createFrom:this] retain];
629 }
630 
631 
632 
_graphicsUpdateCB(NSRect * aRect,GR_CocoaCairoGraphics * pG,void * param)633 bool AP_CocoaFrameImpl::_graphicsUpdateCB(NSRect * aRect, GR_CocoaCairoGraphics *pG, void* param)
634 {
635 	// a static function
636 	AP_CocoaFrame * pCocoaFrame = static_cast<AP_CocoaFrame *>(param);
637 	if (!pCocoaFrame)
638 		return false;
639 	FV_View * pView = static_cast<FV_View *>(pCocoaFrame->getCurrentView());
640 
641 	UT_DEBUGMSG(("AP_CocoaFrameImpl::_graphicsUpdateCB()\n"));
642 	if(pView != NULL) {
643 		UT_Rect rClip;
644 		rClip.left = (UT_sint32)rint(pG->tluD(aRect->origin.x));
645 		rClip.top = (UT_sint32)rint(pG->tluD(aRect->origin.y));
646 		rClip.width = (UT_sint32)rint(pG->tluD(aRect->size.width));
647 		rClip.height = (UT_sint32)rint(pG->tluD(aRect->size.height));
648 		UT_DEBUGMSG(("Cocoa in frame expose painting area:  left=%d, top=%d, width=%d, height=%d\n", rClip.left, rClip.top, rClip.width, rClip.height));
649 		pView->draw(&rClip);
650 	}
651 	else
652 		return false;
653 	return true;
654 }
655 
656 
giveFocus()657 void AP_CocoaFrameImpl::giveFocus()
658 {
659 	[[_getController() window] makeFirstResponder:m_docAreaGRView];
660 }
661 /* Objective-C section */
662 
663 @implementation AP_CocoaFrameController
664 + (XAP_CocoaFrameController*)createFrom:(XAP_CocoaFrameImpl *)frame
665 {
666 	UT_DEBUGMSG (("Cocoa: @AP_CocoaFrameController createFrom:frame\n"));
667 	AP_CocoaFrameController *obj = [[AP_CocoaFrameController alloc] initWith:frame];
668 	return [obj autorelease];
669 }
670 
671 - (id)initWith:(XAP_CocoaFrameImpl *)frame
672 {
673 	UT_DEBUGMSG (("Cocoa: @AP_CocoaFrameController initWith:frame\n"));
674 	return [super initWith:frame];
675 }
676 
677 
678 - (IBAction)rulerClick:(id)sender
679 {
680 	UT_UNUSED(sender);
681 	UT_DEBUGMSG(("ruler action"));
682 }
683 
684 - (XAP_CocoaNSView *)getVRuler
685 {
686 	return vRuler;
687 }
688 
689 - (XAP_CocoaNSView *)getHRuler
690 {
691 	return hRuler;
692 }
693 
694 - (IBAction)scrollAction:(id)sender
695 {
696 	static_cast<AP_CocoaFrameImpl*>(m_frame)->_scrollAction(sender);
697 }
698 
699 @end
700 
701 
702 @implementation XAP_NSScroller
703 
704 -(id)initWithFrame:(NSRect)frame andController:(NSWindowController*)controller vertical:(BOOL)vertical
705 {
706 	if(![super initWithFrame:frame]) {
707 		return nil;
708 	}
709 	UT_DEBUGMSG (("x = %f, y = %f, w = %f, h = %f\n", frame.origin.x, frame.origin.y,
710 			frame.size.width, frame.size.height));
711 	if (vertical) {
712 		UT_DEBUGMSG(("Is vertical\n"));
713 		[self setAutoresizingMask:(NSViewMinXMargin |  NSViewHeightSizable)];
714 	}
715 	else {
716 		UT_DEBUGMSG(("Is horizontal\n"));
717 		[self setAutoresizingMask:(NSViewMaxYMargin |  NSViewWidthSizable)];
718 	}
719 	[self setTarget:controller];
720 	[self setAction:@selector(scrollAction:)];
721 	return self;
722 }
723 
724 @end
725 
726 
727 @implementation AP_DocViewDelegate
728 
729 - (void)mouseDown:(NSEvent *)theEvent from:(id)sender
730 {
731 //  	pFrame->setTimeOfLastEvent([theEvent timestamp]);
732 	XAP_Frame* pFrame = [(XAP_CocoaNSView*)sender xapFrame];
733 	AV_View * pView = pFrame->getCurrentView();
734 	EV_CocoaMouse * pCocoaMouse = (EV_CocoaMouse *)pFrame->getMouse();
735 
736 	if (pView)
737 		pCocoaMouse->mouseClick (pView, theEvent, sender);
738 }
739 
740 - (void)mouseDragged:(NSEvent *)theEvent from:(id)sender
741 {
742 //  	pFrame->setTimeOfLastEvent([theEvent timestamp]);
743 	XAP_Frame* pFrame = [(XAP_CocoaNSView*)sender xapFrame];
744 	AV_View * pView = pFrame->getCurrentView();
745 	EV_CocoaMouse * pCocoaMouse =(EV_CocoaMouse *)pFrame->getMouse();
746 
747 	if (pView)
748 		pCocoaMouse->mouseMotion (pView, theEvent, sender);
749 }
750 
751 - (void)mouseUp:(NSEvent *)theEvent from:(id)sender
752 {
753 //  	pFrame->setTimeOfLastEvent([theEvent timestamp]);
754 	XAP_Frame* pFrame = [(XAP_CocoaNSView*)sender xapFrame];
755 	AV_View * pView = pFrame->getCurrentView();
756 	EV_CocoaMouse * pCocoaMouse = (EV_CocoaMouse *)pFrame->getMouse();
757 
758 	if (pView)
759 		pCocoaMouse->mouseUp (pView, theEvent, sender);
760 }
761 
762 @end
763 
764