1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dc.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/cocoa/autorelease.h"
17 #import <AppKit/NSBezierPath.h>
18 #import <AppKit/NSTextStorage.h>
19 #import <AppKit/NSLayoutManager.h>
20 #import <AppKit/NSTextContainer.h>
21 #import <AppKit/NSGraphicsContext.h>
22 #import <AppKit/NSAffineTransform.h>
23 #import <AppKit/NSColor.h>
24 #import <AppKit/NSTypeSetter.h>
25 #import <AppKit/NSImage.h>
27 #include <wx/listimpl.cpp>
28 WX_DEFINE_LIST(wxCocoaDCStack);
30 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
31 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
32 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
33 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
34 wxCocoaDCStack wxDC::sm_cocoaDCStack;
36 inline void CocoaSetPenForNSBezierPath(wxPen &pen, NSBezierPath *bezpath)
38 [pen.GetNSColor() set];
40 [bezpath setLineDash:pattern count:pen.GetCocoaLineDash(&pattern) phase:0.0];
41 [bezpath setLineWidth:pen.GetWidth()];
45 [bezpath setLineJoinStyle:NSBevelLineJoinStyle];
48 [bezpath setLineJoinStyle:NSRoundLineJoinStyle];
51 [bezpath setLineJoinStyle:NSMiterLineJoinStyle];
57 [bezpath setLineCapStyle:NSRoundLineCapStyle];
59 case wxCAP_PROJECTING:
60 [bezpath setLineCapStyle:NSSquareLineCapStyle];
63 [bezpath setLineCapStyle:NSButtLineCapStyle];
68 void wxDC::CocoaInitializeTextSystem()
70 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
72 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
74 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
75 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
76 // NSTextStorage retains NSLayoutManager, but so do we
77 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
79 // NOTE: initWithContainerSize is the designated initializer, but the
80 // Apple CircleView sample gets away with just calling init, which
81 // is all we really need for our purposes.
82 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
83 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
84 // NSLayoutManager retains NSTextContainer, but so do we
85 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
88 void wxDC::CocoaShutdownTextSystem()
90 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
91 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
92 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
95 void wxDC::CocoaUnwindStackAndLoseFocus()
97 wxCocoaDCStack::Node *ourNode=sm_cocoaDCStack.Find(this);
100 wxCocoaDCStack::Node *node=sm_cocoaDCStack.GetFirst();
101 for(;node!=ourNode; node=sm_cocoaDCStack.GetFirst())
103 wxDC *dc = node->GetData();
106 if(!dc->CocoaUnlockFocus())
108 wxFAIL_MSG("Unable to unlock focus on higher-level DC!");
110 sm_cocoaDCStack.DeleteNode(node);
112 wxASSERT(node==ourNode);
113 wxASSERT(ourNode->GetData() == this);
114 ourNode->GetData()->CocoaUnlockFocus();
115 sm_cocoaDCStack.DeleteNode(ourNode);
119 bool wxDC::CocoaUnwindStackAndTakeFocus()
121 wxCocoaDCStack::Node *node=sm_cocoaDCStack.GetFirst();
122 for(;node;node = sm_cocoaDCStack.GetFirst())
124 wxDC *dc = node->GetData();
126 // If we're on the stack, then it's unwound enough and we have focus
129 // If unable to unlockFocus (e.g. wxPaintDC) stop here
130 if(!dc->CocoaUnlockFocus())
132 sm_cocoaDCStack.DeleteNode(node);
134 return CocoaLockFocus();
139 m_cocoaFlipped = false;
141 m_pen = *wxBLACK_PEN;
148 bool wxDC::CocoaLockFocus()
153 bool wxDC::CocoaUnlockFocus()
158 void wxDC::CocoaApplyTransformations()
160 // This transform flips the graphics since wxDC uses top-left origin
163 // The transform is auto released
164 NSAffineTransform *transform = [NSAffineTransform transform];
166 y' = 0x + -1y + window's height
168 NSAffineTransformStruct matrix = {
173 [transform setTransformStruct: matrix];
174 // Apply the transform
177 // TODO: Apply scaling transformation
180 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
182 if(!CocoaTakeFocus()) return;
183 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
184 CocoaSetPenForNSBezierPath(m_pen,bezpath);
186 [m_brush.GetNSColor() set];
190 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
192 if(!CocoaTakeFocus()) return;
193 NSBezierPath *bezpath = [NSBezierPath bezierPath];
194 [bezpath moveToPoint:NSMakePoint(x1,y1)];
195 [bezpath lineToPoint:NSMakePoint(x2,y2)];
197 CocoaSetPenForNSBezierPath(m_pen,bezpath);
201 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
203 wxAutoNSAutoreleasePool pool;
204 // FIXME: Cache this so it can be used for DoDrawText
205 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
206 NSAttributedString *attributedString = [[NSAttributedString alloc]
207 initWithString:[NSString stringWithCString:text.c_str()]];
208 [sm_cocoaNSTextStorage setAttributedString:attributedString];
209 [attributedString release];
211 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
212 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
214 *x=(int)usedRect.size.width;
216 *y=(int)usedRect.size.height;
223 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
225 if(!CocoaTakeFocus()) return;
226 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
227 NSAttributedString *attributedString = [[NSAttributedString alloc]
228 initWithString:[NSString stringWithCString:text.c_str()]];
229 [sm_cocoaNSTextStorage setAttributedString:attributedString];
230 [attributedString release];
232 // Set the color (and later font) attributes
233 NSColor *fgColor = m_textForegroundColour.GetNSColor();
234 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
236 fgColor = [NSColor clearColor];
238 bgColor = [NSColor clearColor];
239 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
240 fgColor, NSForegroundColorAttributeName,
241 bgColor, NSBackgroundColorAttributeName,
243 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
246 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
247 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
248 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
249 // there is no length or we don't start at zero
250 if(!glyphRange.length)
252 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
254 NSAffineTransform *transform = [NSAffineTransform transform];
255 [transform translateXBy:x yBy:y];
257 NSAffineTransform *flipTransform = [NSAffineTransform transform];
259 y' = 0x + -1y + window's height
261 NSAffineTransformStruct matrix = {
264 , 0, usedRect.size.height
266 [flipTransform setTransformStruct: matrix];
268 NSGraphicsContext *context = [NSGraphicsContext currentContext];
269 [context saveGraphicsState];
271 [flipTransform concat];
273 // Draw+fill a rectangle so we can see where the shit is supposed to be.
274 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
275 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
276 [[NSColor blackColor] set];
278 [[NSColor blueColor] set];
282 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
283 layoutLocation.x = 0.0;
284 layoutLocation.y *= -1.0;
285 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
286 if(m_backgroundMode==wxSOLID)
287 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
288 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
290 [context restoreGraphicsState];
293 // wxDCBase functions
294 int wxDCBase::DeviceToLogicalX(int x) const
299 int wxDCBase::DeviceToLogicalY(int y) const
304 int wxDCBase::DeviceToLogicalXRel(int x) const
309 int wxDCBase::DeviceToLogicalYRel(int y) const
314 int wxDCBase::LogicalToDeviceX(int x) const
319 int wxDCBase::LogicalToDeviceY(int y) const
324 int wxDCBase::LogicalToDeviceXRel(int x) const
329 int wxDCBase::LogicalToDeviceYRel(int y) const
334 ///////////////////////////////////////////////////////////////////////////
335 // cut here, the rest is stubs
336 ///////////////////////////////////////////////////////////////////////////
338 //-----------------------------------------------------------------------------
340 //-----------------------------------------------------------------------------
342 #define mm2inches 0.0393700787402
343 #define inches2mm 25.4
344 #define mm2twips 56.6929133859
345 #define twips2mm 0.0176388888889
346 #define mm2pt 2.83464566929
347 #define pt2mm 0.352777777778
349 //-----------------------------------------------------------------------------
351 //-----------------------------------------------------------------------------
353 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
357 void wxDC::DoDrawPoint( int x, int y )
361 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
365 void wxDC::DoDrawLines( int, wxPoint *, int, int )
369 int wxDC::GetDepth() const
374 wxSize wxDC::GetPPI() const
379 bool wxDC::CanGetTextExtent() const
384 wxCoord wxDC::GetCharHeight() const
389 wxCoord wxDC::GetCharWidth() const
394 bool wxDC::CanDrawBitmap() const
399 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
404 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
408 void wxDC::SetPen(const wxPen& pen)
413 void wxDC::SetBrush(const wxBrush& brush)
418 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
422 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
426 void wxDC::DestroyClippingRegion()
430 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
434 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
438 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
442 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
446 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
448 if(!CocoaTakeFocus()) return;
453 // Draw a rect so we can see where it's supposed to be
454 wxLogDebug("image at (%d,%d) size %dx%d",x,y,bmp.GetWidth(),bmp.GetHeight());
455 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
456 [[NSColor blackColor] set];
458 [[NSColor blueColor] set];
462 NSAffineTransform *transform = [NSAffineTransform transform];
463 [transform translateXBy:x yBy:y];
465 NSAffineTransform *flipTransform = [NSAffineTransform transform];
467 y' = 0x + -1y + window's height
469 NSAffineTransformStruct matrix = {
474 [flipTransform setTransformStruct: matrix];
476 NSGraphicsContext *context = [NSGraphicsContext currentContext];
477 [context saveGraphicsState];
479 [flipTransform concat];
481 NSImage *nsimage = [[NSImage alloc]
482 initWithSize:NSMakeSize(bmp.GetWidth(), bmp.GetHeight())];
483 [nsimage addRepresentation: const_cast<wxBitmap&>(bmp).GetNSBitmapImageRep()];
484 [nsimage drawAtPoint: NSMakePoint(0,0)
485 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
486 operation: NSCompositeCopy
490 [context restoreGraphicsState];
493 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
498 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
503 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)
505 if(!CocoaTakeFocus()) return false;
506 if(!source) return false;
507 return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height,
508 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
511 bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
512 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
513 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
518 void wxDC::DoGetSize( int* width, int* height ) const
520 *width = m_maxX-m_minX;
521 *height = m_maxY-m_minY;
524 void wxDC::DoGetSizeMM( int* width, int* height ) const
531 void wxDC::SetTextForeground( const wxColour &col )
534 m_textForegroundColour = col;
537 void wxDC::SetTextBackground( const wxColour &col )
540 m_textBackgroundColour = col;
547 void wxDC::SetBackground(const wxBrush& brush)
549 m_backgroundBrush = brush;
552 void wxDC::SetPalette(const wxPalette&)
556 void wxDC::SetLogicalFunction(int)
561 void wxDC::SetMapMode( int mode )
575 SetLogicalScale( 1.0, 1.0 );
578 if (mode != wxMM_TEXT)
583 void wxDC::SetUserScale( double x, double y )
585 // allow negative ? -> no
588 ComputeScaleAndOrigin();
591 void wxDC::SetLogicalScale( double x, double y )
596 ComputeScaleAndOrigin();
599 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
601 m_logicalOriginX = x * m_signX; // is this still correct ?
602 m_logicalOriginY = y * m_signY;
603 ComputeScaleAndOrigin();
606 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
608 ComputeScaleAndOrigin();
611 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
613 m_signX = (xLeftRight ? 1 : -1);
614 m_signY = (yBottomUp ? -1 : 1);
615 ComputeScaleAndOrigin();
618 void wxDC::ComputeScaleAndOrigin(void)
620 // CMB: copy scale to see if it changes
621 double origScaleX = m_scaleX;
622 double origScaleY = m_scaleY;
624 m_scaleX = m_logicalScaleX * m_userScaleX;
625 m_scaleY = m_logicalScaleY * m_userScaleY;
627 // CMB: if scale has changed call SetPen to recalulate the line width
628 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
630 // this is a bit artificial, but we need to force wxDC to think
631 // the pen has changed
632 wxPen* pen = & GetPen();