1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dc.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/cocoa/autorelease.h"
20 #import <AppKit/NSBezierPath.h>
21 #import <AppKit/NSTextStorage.h>
22 #import <AppKit/NSLayoutManager.h>
23 #import <AppKit/NSTextContainer.h>
24 #import <AppKit/NSGraphicsContext.h>
25 #import <AppKit/NSAffineTransform.h>
26 #import <AppKit/NSColor.h>
27 #import <AppKit/NSTypeSetter.h>
28 #import <AppKit/NSImage.h>
30 #include <wx/listimpl.cpp>
31 WX_DEFINE_LIST(wxCocoaDCStack);
33 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
34 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
35 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
36 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
37 wxCocoaDCStack wxDC::sm_cocoaDCStack;
39 inline void CocoaSetPenForNSBezierPath(wxPen &pen, NSBezierPath *bezpath)
41 [pen.GetNSColor() set];
43 [bezpath setLineDash:pattern count:pen.GetCocoaLineDash(&pattern) phase:0.0];
44 [bezpath setLineWidth:pen.GetWidth()];
48 [bezpath setLineJoinStyle:NSBevelLineJoinStyle];
51 [bezpath setLineJoinStyle:NSRoundLineJoinStyle];
54 [bezpath setLineJoinStyle:NSMiterLineJoinStyle];
60 [bezpath setLineCapStyle:NSRoundLineCapStyle];
62 case wxCAP_PROJECTING:
63 [bezpath setLineCapStyle:NSSquareLineCapStyle];
66 [bezpath setLineCapStyle:NSButtLineCapStyle];
71 void wxDC::CocoaInitializeTextSystem()
73 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
75 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
77 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
78 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
79 // NSTextStorage retains NSLayoutManager, but so do we
80 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
82 // NOTE: initWithContainerSize is the designated initializer, but the
83 // Apple CircleView sample gets away with just calling init, which
84 // is all we really need for our purposes.
85 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
86 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
87 // NSLayoutManager retains NSTextContainer, but so do we
88 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
91 void wxDC::CocoaShutdownTextSystem()
93 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
94 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
95 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
98 void wxDC::CocoaUnwindStackAndLoseFocus()
100 wxCocoaDCStack::Node *ourNode=sm_cocoaDCStack.Find(this);
103 wxCocoaDCStack::Node *node=sm_cocoaDCStack.GetFirst();
104 for(;node!=ourNode; node=sm_cocoaDCStack.GetFirst())
106 wxDC *dc = node->GetData();
109 if(!dc->CocoaUnlockFocus())
111 wxFAIL_MSG("Unable to unlock focus on higher-level DC!");
113 sm_cocoaDCStack.DeleteNode(node);
115 wxASSERT(node==ourNode);
116 wxASSERT(ourNode->GetData() == this);
117 ourNode->GetData()->CocoaUnlockFocus();
118 sm_cocoaDCStack.DeleteNode(ourNode);
122 bool wxDC::CocoaUnwindStackAndTakeFocus()
124 wxCocoaDCStack::Node *node=sm_cocoaDCStack.GetFirst();
125 for(;node;node = sm_cocoaDCStack.GetFirst())
127 wxDC *dc = node->GetData();
129 // If we're on the stack, then it's unwound enough and we have focus
132 // If unable to unlockFocus (e.g. wxPaintDC) stop here
133 if(!dc->CocoaUnlockFocus())
135 sm_cocoaDCStack.DeleteNode(node);
137 return CocoaLockFocus();
142 m_cocoaFlipped = false;
144 m_pen = *wxBLACK_PEN;
151 bool wxDC::CocoaLockFocus()
156 bool wxDC::CocoaUnlockFocus()
161 void wxDC::CocoaApplyTransformations()
163 // This transform flips the graphics since wxDC uses top-left origin
166 // The transform is auto released
167 NSAffineTransform *transform = [NSAffineTransform transform];
169 y' = 0x + -1y + window's height
171 NSAffineTransformStruct matrix = {
176 [transform setTransformStruct: matrix];
177 // Apply the transform
180 // TODO: Apply scaling transformation
183 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
185 if(!CocoaTakeFocus()) return;
186 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
187 CocoaSetPenForNSBezierPath(m_pen,bezpath);
189 [m_brush.GetNSColor() set];
193 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
195 if(!CocoaTakeFocus()) return;
196 NSBezierPath *bezpath = [NSBezierPath bezierPath];
197 [bezpath moveToPoint:NSMakePoint(x1,y1)];
198 [bezpath lineToPoint:NSMakePoint(x2,y2)];
200 CocoaSetPenForNSBezierPath(m_pen,bezpath);
204 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
206 wxAutoNSAutoreleasePool pool;
207 // FIXME: Cache this so it can be used for DoDrawText
208 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
209 NSAttributedString *attributedString = [[NSAttributedString alloc]
210 initWithString:[NSString stringWithCString:text.c_str()]];
211 [sm_cocoaNSTextStorage setAttributedString:attributedString];
212 [attributedString release];
214 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
215 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
217 *x=(int)usedRect.size.width;
219 *y=(int)usedRect.size.height;
226 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
228 if(!CocoaTakeFocus()) return;
229 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
230 NSAttributedString *attributedString = [[NSAttributedString alloc]
231 initWithString:[NSString stringWithCString:text.c_str()]];
232 [sm_cocoaNSTextStorage setAttributedString:attributedString];
233 [attributedString release];
235 // Set the color (and later font) attributes
236 NSColor *fgColor = m_textForegroundColour.GetNSColor();
237 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
239 fgColor = [NSColor clearColor];
241 bgColor = [NSColor clearColor];
242 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
243 fgColor, NSForegroundColorAttributeName,
244 bgColor, NSBackgroundColorAttributeName,
246 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
249 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
250 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
251 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
252 // there is no length or we don't start at zero
253 if(!glyphRange.length)
255 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
257 NSAffineTransform *transform = [NSAffineTransform transform];
258 [transform translateXBy:x yBy:y];
260 NSAffineTransform *flipTransform = [NSAffineTransform transform];
262 y' = 0x + -1y + window's height
264 NSAffineTransformStruct matrix = {
267 , 0, usedRect.size.height
269 [flipTransform setTransformStruct: matrix];
271 NSGraphicsContext *context = [NSGraphicsContext currentContext];
272 [context saveGraphicsState];
274 [flipTransform concat];
276 // Draw+fill a rectangle so we can see where the shit is supposed to be.
277 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
278 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
279 [[NSColor blackColor] set];
281 [[NSColor blueColor] set];
285 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
286 layoutLocation.x = 0.0;
287 layoutLocation.y *= -1.0;
288 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
289 if(m_backgroundMode==wxSOLID)
290 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
291 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
293 [context restoreGraphicsState];
296 // wxDCBase functions
297 int wxDCBase::DeviceToLogicalX(int x) const
302 int wxDCBase::DeviceToLogicalY(int y) const
307 int wxDCBase::DeviceToLogicalXRel(int x) const
312 int wxDCBase::DeviceToLogicalYRel(int y) const
317 int wxDCBase::LogicalToDeviceX(int x) const
322 int wxDCBase::LogicalToDeviceY(int y) const
327 int wxDCBase::LogicalToDeviceXRel(int x) const
332 int wxDCBase::LogicalToDeviceYRel(int y) const
337 ///////////////////////////////////////////////////////////////////////////
338 // cut here, the rest is stubs
339 ///////////////////////////////////////////////////////////////////////////
341 //-----------------------------------------------------------------------------
343 //-----------------------------------------------------------------------------
345 #define mm2inches 0.0393700787402
346 #define inches2mm 25.4
347 #define mm2twips 56.6929133859
348 #define twips2mm 0.0176388888889
349 #define mm2pt 2.83464566929
350 #define pt2mm 0.352777777778
352 //-----------------------------------------------------------------------------
354 //-----------------------------------------------------------------------------
356 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
360 void wxDC::DoDrawPoint( int x, int y )
364 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
368 void wxDC::DoDrawLines( int, wxPoint *, int, int )
372 int wxDC::GetDepth() const
377 wxSize wxDC::GetPPI() const
382 bool wxDC::CanGetTextExtent() const
387 wxCoord wxDC::GetCharHeight() const
392 wxCoord wxDC::GetCharWidth() const
397 bool wxDC::CanDrawBitmap() const
402 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
407 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
411 void wxDC::SetPen(const wxPen& pen)
416 void wxDC::SetBrush(const wxBrush& brush)
421 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
425 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
429 void wxDC::DestroyClippingRegion()
433 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
437 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
441 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
445 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
449 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
451 if(!CocoaTakeFocus()) return;
456 // Draw a rect so we can see where it's supposed to be
457 wxLogDebug("image at (%d,%d) size %dx%d",x,y,bmp.GetWidth(),bmp.GetHeight());
458 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
459 [[NSColor blackColor] set];
461 [[NSColor blueColor] set];
465 NSAffineTransform *transform = [NSAffineTransform transform];
466 [transform translateXBy:x yBy:y];
468 NSAffineTransform *flipTransform = [NSAffineTransform transform];
470 y' = 0x + -1y + window's height
472 NSAffineTransformStruct matrix = {
477 [flipTransform setTransformStruct: matrix];
479 NSGraphicsContext *context = [NSGraphicsContext currentContext];
480 [context saveGraphicsState];
482 [flipTransform concat];
484 NSImage *nsimage = [[NSImage alloc]
485 initWithSize:NSMakeSize(bmp.GetWidth(), bmp.GetHeight())];
486 [nsimage addRepresentation: const_cast<wxBitmap&>(bmp).GetNSBitmapImageRep()];
487 [nsimage drawAtPoint: NSMakePoint(0,0)
488 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
489 operation: NSCompositeCopy
493 [context restoreGraphicsState];
496 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
501 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
506 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)
508 if(!CocoaTakeFocus()) return false;
509 if(!source) return false;
510 return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height,
511 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
514 bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
515 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
516 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
521 void wxDC::DoGetSize( int* width, int* height ) const
523 *width = m_maxX-m_minX;
524 *height = m_maxY-m_minY;
527 void wxDC::DoGetSizeMM( int* width, int* height ) const
534 void wxDC::SetTextForeground( const wxColour &col )
537 m_textForegroundColour = col;
540 void wxDC::SetTextBackground( const wxColour &col )
543 m_textBackgroundColour = col;
550 void wxDC::SetBackground(const wxBrush& brush)
552 m_backgroundBrush = brush;
555 void wxDC::SetPalette(const wxPalette&)
559 void wxDC::SetLogicalFunction(int)
564 void wxDC::SetMapMode( int mode )
578 SetLogicalScale( 1.0, 1.0 );
581 if (mode != wxMM_TEXT)
586 void wxDC::SetUserScale( double x, double y )
588 // allow negative ? -> no
591 ComputeScaleAndOrigin();
594 void wxDC::SetLogicalScale( double x, double y )
599 ComputeScaleAndOrigin();
602 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
604 m_logicalOriginX = x * m_signX; // is this still correct ?
605 m_logicalOriginY = y * m_signY;
606 ComputeScaleAndOrigin();
609 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
611 ComputeScaleAndOrigin();
614 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
616 m_signX = (xLeftRight ? 1 : -1);
617 m_signY = (yBottomUp ? -1 : 1);
618 ComputeScaleAndOrigin();
621 void wxDC::ComputeScaleAndOrigin(void)
623 // CMB: copy scale to see if it changes
624 double origScaleX = m_scaleX;
625 double origScaleY = m_scaleY;
627 m_scaleX = m_logicalScaleX * m_userScaleX;
628 m_scaleY = m_logicalScaleY * m_userScaleY;
630 // CMB: if scale has changed call SetPen to recalulate the line width
631 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
633 // this is a bit artificial, but we need to force wxDC to think
634 // the pen has changed
635 wxPen* pen = & GetPen();