| 1 |
/* AbiSource Application Framework |
| 2 |
* Copyright (C) 1998-2000 AbiSource, Inc. |
| 3 |
* Copyright (C) 2001-2004 Hubert Figuiere |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
* 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#import <Cocoa/Cocoa.h> |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <sys/stat.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include "ut_types.h" |
| 30 |
#include "ut_debugmsg.h" |
| 31 |
#include "ut_assert.h" |
| 32 |
#include "ut_files.h" |
| 33 |
#include "ut_sleep.h" |
| 34 |
#include "xap_ViewListener.h" |
| 35 |
#include "xap_CocoaApp.h" |
| 36 |
#include "xap_CocoaFrame.h" |
| 37 |
#include "xap_CocoaTimer.h" |
| 38 |
#include "ev_CocoaKeyboard.h" |
| 39 |
#include "ev_CocoaMouse.h" |
| 40 |
#include "ev_CocoaMenuBar.h" |
| 41 |
#include "ev_CocoaMenuPopup.h" |
| 42 |
#include "ev_CocoaToolbar.h" |
| 43 |
#include "ev_EditMethod.h" |
| 44 |
#include "xav_View.h" |
| 45 |
#include "fv_View.h" |
| 46 |
#include "xad_Document.h" |
| 47 |
#include "gr_CocoaCairoGraphics.h" |
| 48 |
#include "gr_Painter.h" |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
@implementation XAP_CocoaNSView |
| 53 |
- (id)initWith:(XAP_Frame *)frame andFrame:(NSRect)windowFrame |
| 54 |
{ |
| 55 |
UT_DEBUGMSG (("Cocoa: @XAP_CocoaNSView initWith:Frame\n")); |
| 56 |
UT_ASSERT (frame); |
| 57 |
if(![super initWithFrame:windowFrame]) { |
| 58 |
return nil; |
| 59 |
} |
| 60 |
m_pFrame = frame; |
| 61 |
m_pGR = NULL; |
| 62 |
if(frame) { |
| 63 |
[[NSNotificationCenter defaultCenter] addObserver:self |
| 64 |
selector:@selector(hasBeenResized:) |
| 65 |
name:NSViewFrameDidChangeNotification object:self]; |
| 66 |
} |
| 67 |
return self; |
| 68 |
} |
| 69 |
|
| 70 |
- (void)dealloc |
| 71 |
{ |
| 72 |
[_eventDelegate release]; |
| 73 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 74 |
[super dealloc]; |
| 75 |
} |
| 76 |
|
| 77 |
- (BOOL)acceptsFirstResponder |
| 78 |
{ |
| 79 |
return YES; |
| 80 |
} |
| 81 |
|
| 82 |
- (BOOL)becomeFirstResponder |
| 83 |
{ |
| 84 |
if (m_pFrame) { |
| 85 |
UT_ASSERT (m_pFrame); |
| 86 |
|
| 87 |
if (m_pFrame->getCurrentView()) { |
| 88 |
m_pFrame->getCurrentView()->focusChange(AV_FOCUS_HERE); |
| 89 |
} |
| 90 |
UT_DEBUGMSG(("became first responder!\n")); |
| 91 |
return YES; |
| 92 |
} |
| 93 |
UT_DEBUGMSG (("refused to become first responder, because gr_Graphics is not setup.\n")); |
| 94 |
return NO; |
| 95 |
} |
| 96 |
|
| 97 |
- (XAP_Frame *)xapFrame |
| 98 |
{ |
| 99 |
return m_pFrame; |
| 100 |
} |
| 101 |
|
| 102 |
- (void)setGraphics:(GR_CocoaCairoGraphics *)gr |
| 103 |
{ |
| 104 |
m_pGR = gr; |
| 105 |
} |
| 106 |
|
| 107 |
/*! |
| 108 |
Cocoa overridden method. Redraw the screen. |
| 109 |
*/ |
| 110 |
- (void)drawRect:(NSRect)aRect |
| 111 |
{ |
| 112 |
if (m_pGR) { |
| 113 |
UT_RGBColor clr; |
| 114 |
GR_Painter painter(m_pGR); |
| 115 |
|
| 116 |
GR_CocoaCairoGraphics::_utNSColorToRGBColor([NSColor redColor], clr); |
| 117 |
m_pGR->fillRect (clr, aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height); |
| 118 |
|
| 119 |
/* Because of the way we convert from local to display units for scrolling and back again for expose |
| 120 |
* events, there can be a sizeable discrepancy (in local units) between internal events and the |
| 121 |
* reported exposed area. I am therefore marking an extra 1-pixel border around the exposed display area. |
| 122 |
*/ |
| 123 |
aRect.origin.x += -1.0; |
| 124 |
aRect.origin.y += -1.0; |
| 125 |
aRect.size.width += 2.0; |
| 126 |
aRect.size.height += 2.0; |
| 127 |
|
| 128 |
// if (![self inLiveResize]) // this case handled in -hasBeenResized: below |
| 129 |
m_pGR->_callUpdateCallback(&aRect); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/*! |
| 134 |
Cocoa overridden method. |
| 135 |
|
| 136 |
\return NO. Coordinates are still upside down, but we'll reverse |
| 137 |
the offscreen instead. |
| 138 |
*/ |
| 139 |
- (BOOL)isFlipped |
| 140 |
{ |
| 141 |
return GR_CocoaCairoGraphics::_isFlipped(); |
| 142 |
} |
| 143 |
|
| 144 |
/*! |
| 145 |
Cocoa overridden method. |
| 146 |
|
| 147 |
\return NO. Not opaque. |
| 148 |
*/ |
| 149 |
- (BOOL)isOpaque |
| 150 |
{ |
| 151 |
return YES; |
| 152 |
} |
| 153 |
|
| 154 |
/* |
| 155 |
- (XAP_Frame *)_getOwnerFrame |
| 156 |
{ |
| 157 |
id controller = [[self window] delegate]; |
| 158 |
if ([controller isKindOfClass:[XAP_CocoaFrameController class]]) |
| 159 |
{ |
| 160 |
XAP_Frame* frame = [(XAP_CocoaFrameController*)controller frameImpl]->getFrame(); |
| 161 |
return frame; |
| 162 |
} |
| 163 |
NSLog (@"-[_getOwnerFrame] could find owner frame"); |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
*/ |
| 167 |
|
| 168 |
- (void)resetCursorRects |
| 169 |
{ |
| 170 |
[self addCursorRect:[self visibleRect] cursor:_cursor]; |
| 171 |
//[_cursor setOnMouseEntered:YES]; |
| 172 |
} |
| 173 |
|
| 174 |
- (void)setCursor:(NSCursor*)cursor; |
| 175 |
{ |
| 176 |
[_cursor release]; |
| 177 |
_cursor = [cursor retain]; |
| 178 |
} |
| 179 |
|
| 180 |
- (void)setEventDelegate:(NSObject <XAP_MouseEventDelegate>*)delegate |
| 181 |
{ |
| 182 |
[_eventDelegate release]; |
| 183 |
_eventDelegate = delegate; |
| 184 |
[delegate retain]; |
| 185 |
} |
| 186 |
|
| 187 |
- (void)hasBeenResized:(NSNotification*)notif |
| 188 |
{ |
| 189 |
UT_UNUSED(notif); |
| 190 |
if (m_pGR && m_pFrame) { |
| 191 |
AV_View * pView = m_pFrame->getCurrentView(); |
| 192 |
NSRect rect = [self bounds]; |
| 193 |
if (pView && !pView->isLayoutFilling()) |
| 194 |
{ |
| 195 |
pView->setWindowSize((UT_sint32)rint(rect.size.width), (UT_sint32)rint(rect.size.height)); |
| 196 |
// m_pGR->_callUpdateCallback(&rect); |
| 197 |
m_pFrame->quickZoom(); // was update zoom |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
- (NSObject <XAP_MouseEventDelegate>*)eventDelegate |
| 205 |
{ |
| 206 |
return _eventDelegate; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
- (void)mouseDown:(NSEvent *)theEvent |
| 211 |
{ |
| 212 |
if (NSInputManager * inputManager = [NSInputManager currentInputManager]) |
| 213 |
[inputManager markedTextAbandoned:[theEvent window]]; |
| 214 |
[_eventDelegate mouseDown:theEvent from:self]; |
| 215 |
} |
| 216 |
- (void)rightMouseDown:(NSEvent *)theEvent |
| 217 |
{ |
| 218 |
if (NSInputManager * inputManager = [NSInputManager currentInputManager]) |
| 219 |
[inputManager markedTextAbandoned:[theEvent window]]; |
| 220 |
[_eventDelegate mouseDown:theEvent from:self]; |
| 221 |
} |
| 222 |
- (void)otherMouseDown:(NSEvent *)theEvent |
| 223 |
{ |
| 224 |
if (NSInputManager * inputManager = [NSInputManager currentInputManager]) |
| 225 |
[inputManager markedTextAbandoned:[theEvent window]]; |
| 226 |
[_eventDelegate mouseDown:theEvent from:self]; |
| 227 |
} |
| 228 |
|
| 229 |
- (void)mouseDragged:(NSEvent *)theEvent |
| 230 |
{ |
| 231 |
[_eventDelegate mouseDragged:theEvent from:self]; |
| 232 |
} |
| 233 |
- (void)rightMouseDragged:(NSEvent *)theEvent |
| 234 |
{ |
| 235 |
[_eventDelegate mouseDragged:theEvent from:self]; |
| 236 |
} |
| 237 |
- (void)otherMouseDragged:(NSEvent *)theEvent |
| 238 |
{ |
| 239 |
[_eventDelegate mouseDragged:theEvent from:self]; |
| 240 |
} |
| 241 |
|
| 242 |
- (void)mouseUp:(NSEvent *)theEvent |
| 243 |
{ |
| 244 |
[_eventDelegate mouseUp:theEvent from:self]; |
| 245 |
} |
| 246 |
- (void)rightMouseUp:(NSEvent *)theEvent |
| 247 |
{ |
| 248 |
[_eventDelegate mouseUp:theEvent from:self]; |
| 249 |
} |
| 250 |
- (void)otherMouseUp:(NSEvent *)theEvent |
| 251 |
{ |
| 252 |
[_eventDelegate mouseUp:theEvent from:self]; |
| 253 |
} |
| 254 |
|
| 255 |
- (void)scrollWheel:(NSEvent *)theEvent |
| 256 |
{ |
| 257 |
if(!m_pFrame) { |
| 258 |
return; |
| 259 |
} |
| 260 |
AV_View *pAV_View = m_pFrame->getCurrentView (); |
| 261 |
|
| 262 |
UT_sint32 deltaX = (UT_sint32)rint([theEvent deltaX]); |
| 263 |
UT_sint32 deltaY = (UT_sint32)rint([theEvent deltaY]); |
| 264 |
|
| 265 |
if (deltaX < 0) |
| 266 |
pAV_View->cmdScroll(AV_SCROLLCMD_LINERIGHT, m_pGR->tlu(-30*deltaX)); |
| 267 |
if (deltaX > 0) |
| 268 |
pAV_View->cmdScroll(AV_SCROLLCMD_LINELEFT, m_pGR->tlu( 30*deltaX)); |
| 269 |
|
| 270 |
if (deltaY > 0) |
| 271 |
pAV_View->cmdScroll(AV_SCROLLCMD_LINEUP, m_pGR->tlu( 30*deltaY)); |
| 272 |
if (deltaY < 0) |
| 273 |
pAV_View->cmdScroll(AV_SCROLLCMD_LINEDOWN, m_pGR->tlu(-30*deltaY)); |
| 274 |
} |
| 275 |
|
| 276 |
@end |