1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dc.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/string.h"
23 #import <AppKit/NSBezierPath.h>
24 #import <AppKit/NSTextStorage.h>
25 #import <AppKit/NSLayoutManager.h>
26 #import <AppKit/NSTextContainer.h>
27 #import <AppKit/NSGraphicsContext.h>
28 #import <AppKit/NSAffineTransform.h>
29 #import <AppKit/NSColor.h>
30 #import <AppKit/NSTypesetter.h>
31 #import <AppKit/NSImage.h>
33 #include "wx/math.h" //math constants
34 #include "wx/listimpl.cpp"
35 WX_DEFINE_LIST(wxCocoaDCStack);
37 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
38 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
39 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
40 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
41 wxCocoaDCStack wxDC::sm_cocoaDCStack;
43 inline void CocoaSetPenForNSBezierPath(wxPen &pen, NSBezierPath *bezpath)
45 [pen.GetNSColor() set];
47 [bezpath setLineDash:pattern count:pen.GetCocoaLineDash(&pattern) phase:0.0];
48 [bezpath setLineWidth:pen.GetWidth()];
52 [bezpath setLineJoinStyle:NSBevelLineJoinStyle];
55 [bezpath setLineJoinStyle:NSRoundLineJoinStyle];
58 [bezpath setLineJoinStyle:NSMiterLineJoinStyle];
64 [bezpath setLineCapStyle:NSRoundLineCapStyle];
66 case wxCAP_PROJECTING:
67 [bezpath setLineCapStyle:NSSquareLineCapStyle];
70 [bezpath setLineCapStyle:NSButtLineCapStyle];
75 void wxDC::CocoaInitializeTextSystem()
77 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,wxT("Text system already initalized! BAD PROGRAMMER!"));
79 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
81 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
82 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
83 // NSTextStorage retains NSLayoutManager, but so do we
84 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
86 // NOTE: initWithContainerSize is the designated initializer, but the
87 // Apple CircleView sample gets away with just calling init, which
88 // is all we really need for our purposes.
89 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
90 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
91 // NSLayoutManager retains NSTextContainer, but so do we
92 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
95 void wxDC::CocoaShutdownTextSystem()
97 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
98 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
99 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
102 void wxDC::CocoaUnwindStackAndLoseFocus()
104 wxCocoaDCStack::compatibility_iterator ourNode=sm_cocoaDCStack.Find(this);
107 wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst();
108 for(;node!=ourNode; node=sm_cocoaDCStack.GetFirst())
110 wxDC *dc = node->GetData();
113 if(!dc->CocoaUnlockFocus())
115 wxFAIL_MSG(wxT("Unable to unlock focus on higher-level DC!"));
117 sm_cocoaDCStack.Erase(node);
119 wxASSERT(node==ourNode);
120 wxASSERT(ourNode->GetData() == this);
121 ourNode->GetData()->CocoaUnlockFocus();
122 sm_cocoaDCStack.Erase(ourNode);
126 bool wxDC::CocoaUnwindStackAndTakeFocus()
128 wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst();
129 for(;node;node = sm_cocoaDCStack.GetFirst())
131 wxDC *dc = node->GetData();
133 // If we're on the stack, then it's unwound enough and we have focus
136 // If unable to unlockFocus (e.g. wxPaintDC) stop here
137 if(!dc->CocoaUnlockFocus())
139 sm_cocoaDCStack.Erase(node);
141 return CocoaLockFocus();
146 m_cocoaWxToBoundsTransform = nil;
147 m_pen = *wxBLACK_PEN;
152 [m_cocoaWxToBoundsTransform release];
155 bool wxDC::CocoaLockFocus()
160 bool wxDC::CocoaUnlockFocus()
165 /*static*/ WX_NSAffineTransform wxDC::CocoaGetWxToBoundsTransform(bool isFlipped, float height)
167 NSAffineTransform *transform = nil;
168 // This transform flips the graphics since wxDC uses top-left origin
171 // The transform is auto released
172 transform = [NSAffineTransform transform];
174 y' = 0x + -1y + window's height
176 NSAffineTransformStruct matrix = {
181 [transform setTransformStruct: matrix];
186 void wxDC::CocoaApplyTransformations()
188 [m_cocoaWxToBoundsTransform concat];
189 // TODO: Apply device/logical/user position/scaling transformations
192 void wxDC::CocoaUnapplyTransformations()
194 // NOTE: You *must* call this with focus held.
195 // Undo all transforms so we're back in true Cocoa coords with
196 // no scaling or flipping.
197 NSAffineTransform *invertTransform;
198 invertTransform = [m_cocoaWxToBoundsTransform copy];
199 [invertTransform invert];
200 [invertTransform concat];
201 [invertTransform release];
204 bool wxDC::CocoaGetBounds(void *rectData)
206 // We don't know what we are so we can't return anything.
210 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
212 wxAutoNSAutoreleasePool pool;
213 if(!CocoaTakeFocus()) return;
214 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
215 CocoaSetPenForNSBezierPath(m_pen,bezpath);
217 [m_brush.GetNSColor() set];
221 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
223 wxAutoNSAutoreleasePool pool;
224 if(!CocoaTakeFocus()) return;
225 NSBezierPath *bezpath = [NSBezierPath bezierPath];
226 [bezpath moveToPoint:NSMakePoint(x1,y1)];
227 [bezpath lineToPoint:NSMakePoint(x2,y2)];
229 CocoaSetPenForNSBezierPath(m_pen,bezpath);
233 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
235 wxAutoNSAutoreleasePool pool;
236 // FIXME: Cache this so it can be used for DoDrawText
237 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
238 NSAttributedString *attributedString = [[NSAttributedString alloc]
239 initWithString:wxNSStringWithWxString(text.c_str())];
240 [sm_cocoaNSTextStorage setAttributedString:attributedString];
241 [attributedString release];
243 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
244 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
246 *x=(int)usedRect.size.width;
248 *y=(int)usedRect.size.height;
255 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
257 wxAutoNSAutoreleasePool pool;
258 if(!CocoaTakeFocus()) return;
259 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
260 NSAttributedString *attributedString = [[NSAttributedString alloc]
261 initWithString:wxNSStringWithWxString(text.c_str())];
262 [sm_cocoaNSTextStorage setAttributedString:attributedString];
263 [attributedString release];
265 // Set the color (and later font) attributes
266 NSColor *fgColor = m_textForegroundColour.GetNSColor();
267 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
269 fgColor = [NSColor clearColor];
271 bgColor = [NSColor clearColor];
272 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
273 fgColor, NSForegroundColorAttributeName,
274 bgColor, NSBackgroundColorAttributeName,
276 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
279 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
280 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
281 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
282 // there is no length or we don't start at zero
283 if(!glyphRange.length)
285 wxASSERT_MSG(glyphRange.location==0,wxT("glyphRange must begin at zero"));
287 NSAffineTransform *transform = [NSAffineTransform transform];
288 [transform translateXBy:x yBy:y];
290 NSAffineTransform *flipTransform = [NSAffineTransform transform];
292 y' = 0x + -1y + window's height
294 NSAffineTransformStruct matrix = {
297 , 0, usedRect.size.height
299 [flipTransform setTransformStruct: matrix];
301 NSGraphicsContext *context = [NSGraphicsContext currentContext];
302 [context saveGraphicsState];
304 [flipTransform concat];
306 // Draw+fill a rectangle so we can see where the shit is supposed to be.
307 wxLogTrace(wxTRACE_COCOA,wxT("(%f,%f) (%fx%f)"),usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
308 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
309 [[NSColor blackColor] set];
311 [[NSColor blueColor] set];
315 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
316 layoutLocation.x = 0.0;
317 layoutLocation.y *= -1.0;
318 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
319 if(m_backgroundMode==wxSOLID)
320 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
321 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
323 [context restoreGraphicsState];
326 // wxDCBase functions
327 int wxDCBase::DeviceToLogicalX(int x) const
332 int wxDCBase::DeviceToLogicalY(int y) const
337 int wxDCBase::DeviceToLogicalXRel(int x) const
342 int wxDCBase::DeviceToLogicalYRel(int y) const
347 int wxDCBase::LogicalToDeviceX(int x) const
352 int wxDCBase::LogicalToDeviceY(int y) const
357 int wxDCBase::LogicalToDeviceXRel(int x) const
362 int wxDCBase::LogicalToDeviceYRel(int y) const
367 ///////////////////////////////////////////////////////////////////////////
368 // cut here, the rest is stubs
369 ///////////////////////////////////////////////////////////////////////////
371 //-----------------------------------------------------------------------------
373 //-----------------------------------------------------------------------------
375 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
379 void wxDC::DoDrawPoint( int x, int y )
383 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
387 void wxDC::DoDrawLines( int, wxPoint *, int, int )
391 int wxDC::GetDepth() const
396 wxSize wxDC::GetPPI() const
401 bool wxDC::CanGetTextExtent() const
406 wxCoord wxDC::GetCharHeight() const
411 wxCoord wxDC::GetCharWidth() const
416 bool wxDC::CanDrawBitmap() const
421 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
426 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
430 void wxDC::SetPen(const wxPen& pen)
435 void wxDC::SetBrush(const wxBrush& brush)
440 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
444 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
448 void wxDC::DestroyClippingRegion()
452 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
456 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
460 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
464 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
468 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
470 wxAutoNSAutoreleasePool pool;
471 if(!CocoaTakeFocus()) return;
476 // Draw a rect so we can see where it's supposed to be
477 wxLogTrace(wxTRACE_COCOA,wxT("image at (%d,%d) size %dx%d"),x,y,bmp.GetWidth(),bmp.GetHeight());
478 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
479 [[NSColor blackColor] set];
481 [[NSColor blueColor] set];
485 NSAffineTransform *transform = [NSAffineTransform transform];
486 [transform translateXBy:x yBy:y];
488 NSAffineTransform *flipTransform = [NSAffineTransform transform];
490 y' = 0x + -1y + window's height
492 NSAffineTransformStruct matrix = {
497 [flipTransform setTransformStruct: matrix];
499 NSGraphicsContext *context = [NSGraphicsContext currentContext];
500 [context saveGraphicsState];
502 [flipTransform concat];
504 NSImage *nsimage = [bmp.GetNSImage(useMask) retain];
506 [nsimage drawAtPoint: NSMakePoint(0,0)
507 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
508 operation: NSCompositeSourceOver
512 [context restoreGraphicsState];
515 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
520 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
525 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop, bool useMask , wxCoord xsrcMask, wxCoord ysrcMask)
527 if(!CocoaTakeFocus()) return false;
528 if(!source) return false;
529 return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height,
530 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
533 bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
534 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
535 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
540 void wxDC::DoGetSize( int* width, int* height ) const
542 *width = m_maxX-m_minX;
543 *height = m_maxY-m_minY;
546 void wxDC::DoGetSizeMM( int* width, int* height ) const
553 void wxDC::SetTextForeground( const wxColour &col )
556 m_textForegroundColour = col;
559 void wxDC::SetTextBackground( const wxColour &col )
562 m_textBackgroundColour = col;
567 if(!CocoaTakeFocus()) return;
570 if(!CocoaGetBounds(&boundsRect)) return;
572 NSGraphicsContext *context = [NSGraphicsContext currentContext];
573 [context saveGraphicsState];
575 // Undo all transforms so when we draw our bounds rect we
576 // really overwrite our bounds rect.
577 CocoaUnapplyTransformations();
579 [m_backgroundBrush.GetNSColor() set];
580 [NSBezierPath fillRect:boundsRect];
582 [context restoreGraphicsState];
585 void wxDC::SetBackground(const wxBrush& brush)
587 m_backgroundBrush = brush;
590 void wxDC::SetPalette(const wxPalette&)
594 void wxDC::SetLogicalFunction(int)
599 void wxDC::SetMapMode( int mode )
613 SetLogicalScale( 1.0, 1.0 );
616 if (mode != wxMM_TEXT)
621 void wxDC::SetUserScale( double x, double y )
623 // allow negative ? -> no
626 ComputeScaleAndOrigin();
629 void wxDC::SetLogicalScale( double x, double y )
634 ComputeScaleAndOrigin();
637 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
639 m_logicalOriginX = x * m_signX; // is this still correct ?
640 m_logicalOriginY = y * m_signY;
641 ComputeScaleAndOrigin();
644 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
646 ComputeScaleAndOrigin();
649 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
651 m_signX = (xLeftRight ? 1 : -1);
652 m_signY = (yBottomUp ? -1 : 1);
653 ComputeScaleAndOrigin();
656 void wxDC::ComputeScaleAndOrigin(void)
658 // CMB: copy scale to see if it changes
659 double origScaleX = m_scaleX;
660 double origScaleY = m_scaleY;
662 m_scaleX = m_logicalScaleX * m_userScaleX;
663 m_scaleY = m_logicalScaleY * m_userScaleY;
665 // CMB: if scale has changed call SetPen to recalulate the line width
666 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
668 // this is a bit artificial, but we need to force wxDC to think
669 // the pen has changed
670 const wxPen* pen = & GetPen();