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"
19 #include "wx/cocoa/string.h"
21 #import <AppKit/NSBezierPath.h>
22 #import <AppKit/NSTextStorage.h>
23 #import <AppKit/NSLayoutManager.h>
24 #import <AppKit/NSTextContainer.h>
25 #import <AppKit/NSGraphicsContext.h>
26 #import <AppKit/NSAffineTransform.h>
27 #import <AppKit/NSColor.h>
28 #import <AppKit/NSTypesetter.h>
29 #import <AppKit/NSImage.h>
31 #include <wx/listimpl.cpp>
32 WX_DEFINE_LIST(wxCocoaDCStack);
34 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
35 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
36 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
37 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
38 wxCocoaDCStack wxDC::sm_cocoaDCStack;
40 inline void CocoaSetPenForNSBezierPath(wxPen &pen, NSBezierPath *bezpath)
42 [pen.GetNSColor() set];
44 [bezpath setLineDash:pattern count:pen.GetCocoaLineDash(&pattern) phase:0.0];
45 [bezpath setLineWidth:pen.GetWidth()];
49 [bezpath setLineJoinStyle:NSBevelLineJoinStyle];
52 [bezpath setLineJoinStyle:NSRoundLineJoinStyle];
55 [bezpath setLineJoinStyle:NSMiterLineJoinStyle];
61 [bezpath setLineCapStyle:NSRoundLineCapStyle];
63 case wxCAP_PROJECTING:
64 [bezpath setLineCapStyle:NSSquareLineCapStyle];
67 [bezpath setLineCapStyle:NSButtLineCapStyle];
72 void wxDC::CocoaInitializeTextSystem()
74 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,wxT("Text system already initalized! BAD PROGRAMMER!"));
76 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
78 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
79 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
80 // NSTextStorage retains NSLayoutManager, but so do we
81 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
83 // NOTE: initWithContainerSize is the designated initializer, but the
84 // Apple CircleView sample gets away with just calling init, which
85 // is all we really need for our purposes.
86 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
87 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
88 // NSLayoutManager retains NSTextContainer, but so do we
89 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
92 void wxDC::CocoaShutdownTextSystem()
94 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
95 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
96 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
99 void wxDC::CocoaUnwindStackAndLoseFocus()
101 wxCocoaDCStack::compatibility_iterator ourNode=sm_cocoaDCStack.Find(this);
104 wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst();
105 for(;node!=ourNode; node=sm_cocoaDCStack.GetFirst())
107 wxDC *dc = node->GetData();
110 if(!dc->CocoaUnlockFocus())
112 wxFAIL_MSG(wxT("Unable to unlock focus on higher-level DC!"));
114 sm_cocoaDCStack.Erase(node);
116 wxASSERT(node==ourNode);
117 wxASSERT(ourNode->GetData() == this);
118 ourNode->GetData()->CocoaUnlockFocus();
119 sm_cocoaDCStack.Erase(ourNode);
123 bool wxDC::CocoaUnwindStackAndTakeFocus()
125 wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst();
126 for(;node;node = sm_cocoaDCStack.GetFirst())
128 wxDC *dc = node->GetData();
130 // If we're on the stack, then it's unwound enough and we have focus
133 // If unable to unlockFocus (e.g. wxPaintDC) stop here
134 if(!dc->CocoaUnlockFocus())
136 sm_cocoaDCStack.Erase(node);
138 return CocoaLockFocus();
143 m_cocoaFlipped = false;
145 m_pen = *wxBLACK_PEN;
152 bool wxDC::CocoaLockFocus()
157 bool wxDC::CocoaUnlockFocus()
162 void wxDC::CocoaApplyTransformations()
164 // This transform flips the graphics since wxDC uses top-left origin
167 // The transform is auto released
168 NSAffineTransform *transform = [NSAffineTransform transform];
170 y' = 0x + -1y + window's height
172 NSAffineTransformStruct matrix = {
177 [transform setTransformStruct: matrix];
178 // Apply the transform
181 // TODO: Apply scaling transformation
184 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
186 if(!CocoaTakeFocus()) return;
187 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
188 CocoaSetPenForNSBezierPath(m_pen,bezpath);
190 [m_brush.GetNSColor() set];
194 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
196 if(!CocoaTakeFocus()) return;
197 NSBezierPath *bezpath = [NSBezierPath bezierPath];
198 [bezpath moveToPoint:NSMakePoint(x1,y1)];
199 [bezpath lineToPoint:NSMakePoint(x2,y2)];
201 CocoaSetPenForNSBezierPath(m_pen,bezpath);
205 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
207 wxAutoNSAutoreleasePool pool;
208 // FIXME: Cache this so it can be used for DoDrawText
209 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
210 NSAttributedString *attributedString = [[NSAttributedString alloc]
211 initWithString:wxNSStringWithWxString(text.c_str())];
212 [sm_cocoaNSTextStorage setAttributedString:attributedString];
213 [attributedString release];
215 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
216 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
218 *x=(int)usedRect.size.width;
220 *y=(int)usedRect.size.height;
227 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
229 if(!CocoaTakeFocus()) return;
230 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
231 NSAttributedString *attributedString = [[NSAttributedString alloc]
232 initWithString:wxNSStringWithWxString(text.c_str())];
233 [sm_cocoaNSTextStorage setAttributedString:attributedString];
234 [attributedString release];
236 // Set the color (and later font) attributes
237 NSColor *fgColor = m_textForegroundColour.GetNSColor();
238 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
240 fgColor = [NSColor clearColor];
242 bgColor = [NSColor clearColor];
243 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
244 fgColor, NSForegroundColorAttributeName,
245 bgColor, NSBackgroundColorAttributeName,
247 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
250 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
251 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
252 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
253 // there is no length or we don't start at zero
254 if(!glyphRange.length)
256 wxASSERT_MSG(glyphRange.location==0,wxT("glyphRange must begin at zero"));
258 NSAffineTransform *transform = [NSAffineTransform transform];
259 [transform translateXBy:x yBy:y];
261 NSAffineTransform *flipTransform = [NSAffineTransform transform];
263 y' = 0x + -1y + window's height
265 NSAffineTransformStruct matrix = {
268 , 0, usedRect.size.height
270 [flipTransform setTransformStruct: matrix];
272 NSGraphicsContext *context = [NSGraphicsContext currentContext];
273 [context saveGraphicsState];
275 [flipTransform concat];
277 // Draw+fill a rectangle so we can see where the shit is supposed to be.
278 wxLogTrace(wxTRACE_COCOA,wxT("(%f,%f) (%fx%f)"),usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
279 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
280 [[NSColor blackColor] set];
282 [[NSColor blueColor] set];
286 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
287 layoutLocation.x = 0.0;
288 layoutLocation.y *= -1.0;
289 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
290 if(m_backgroundMode==wxSOLID)
291 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
292 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
294 [context restoreGraphicsState];
297 // wxDCBase functions
298 int wxDCBase::DeviceToLogicalX(int x) const
303 int wxDCBase::DeviceToLogicalY(int y) const
308 int wxDCBase::DeviceToLogicalXRel(int x) const
313 int wxDCBase::DeviceToLogicalYRel(int y) const
318 int wxDCBase::LogicalToDeviceX(int x) const
323 int wxDCBase::LogicalToDeviceY(int y) const
328 int wxDCBase::LogicalToDeviceXRel(int x) const
333 int wxDCBase::LogicalToDeviceYRel(int y) const
338 ///////////////////////////////////////////////////////////////////////////
339 // cut here, the rest is stubs
340 ///////////////////////////////////////////////////////////////////////////
342 //-----------------------------------------------------------------------------
344 //-----------------------------------------------------------------------------
346 #define mm2inches 0.0393700787402
347 #define inches2mm 25.4
348 #define mm2twips 56.6929133859
349 #define twips2mm 0.0176388888889
350 #define mm2pt 2.83464566929
351 #define pt2mm 0.352777777778
353 //-----------------------------------------------------------------------------
355 //-----------------------------------------------------------------------------
357 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
361 void wxDC::DoDrawPoint( int x, int y )
365 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
369 void wxDC::DoDrawLines( int, wxPoint *, int, int )
373 int wxDC::GetDepth() const
378 wxSize wxDC::GetPPI() const
383 bool wxDC::CanGetTextExtent() const
388 wxCoord wxDC::GetCharHeight() const
393 wxCoord wxDC::GetCharWidth() const
398 bool wxDC::CanDrawBitmap() const
403 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
408 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
412 void wxDC::SetPen(const wxPen& pen)
417 void wxDC::SetBrush(const wxBrush& brush)
422 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
426 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
430 void wxDC::DestroyClippingRegion()
434 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
438 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
442 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
446 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
450 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
452 if(!CocoaTakeFocus()) return;
457 // Draw a rect so we can see where it's supposed to be
458 wxLogTrace(wxTRACE_COCOA,wxT("image at (%d,%d) size %dx%d"),x,y,bmp.GetWidth(),bmp.GetHeight());
459 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
460 [[NSColor blackColor] set];
462 [[NSColor blueColor] set];
466 NSAffineTransform *transform = [NSAffineTransform transform];
467 [transform translateXBy:x yBy:y];
469 NSAffineTransform *flipTransform = [NSAffineTransform transform];
471 y' = 0x + -1y + window's height
473 NSAffineTransformStruct matrix = {
478 [flipTransform setTransformStruct: matrix];
480 NSGraphicsContext *context = [NSGraphicsContext currentContext];
481 [context saveGraphicsState];
483 [flipTransform concat];
485 NSImage *nsimage = [bmp.GetNSImage(useMask) retain];
487 [nsimage drawAtPoint: NSMakePoint(0,0)
488 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
489 operation: NSCompositeSourceOver
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();