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"
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_cocoaWxToBoundsTransform = nil;
144 m_pen = *wxBLACK_PEN;
151 bool wxDC::CocoaLockFocus()
156 bool wxDC::CocoaUnlockFocus()
161 /*static*/ WX_NSAffineTransform wxDC::CocoaGetWxToBoundsTransform(bool isFlipped, float height)
163 NSAffineTransform *transform = nil;
164 // This transform flips the graphics since wxDC uses top-left origin
167 // The transform is auto released
168 transform = [NSAffineTransform transform];
170 y' = 0x + -1y + window's height
172 NSAffineTransformStruct matrix = {
177 [transform setTransformStruct: matrix];
182 void wxDC::CocoaApplyTransformations()
184 [m_cocoaWxToBoundsTransform concat];
185 // TODO: Apply device/logical/user position/scaling transformations
188 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
190 wxAutoNSAutoreleasePool pool;
191 if(!CocoaTakeFocus()) return;
192 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
193 CocoaSetPenForNSBezierPath(m_pen,bezpath);
195 [m_brush.GetNSColor() set];
199 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
201 wxAutoNSAutoreleasePool pool;
202 if(!CocoaTakeFocus()) return;
203 NSBezierPath *bezpath = [NSBezierPath bezierPath];
204 [bezpath moveToPoint:NSMakePoint(x1,y1)];
205 [bezpath lineToPoint:NSMakePoint(x2,y2)];
207 CocoaSetPenForNSBezierPath(m_pen,bezpath);
211 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
213 wxAutoNSAutoreleasePool pool;
214 // FIXME: Cache this so it can be used for DoDrawText
215 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
216 NSAttributedString *attributedString = [[NSAttributedString alloc]
217 initWithString:wxNSStringWithWxString(text.c_str())];
218 [sm_cocoaNSTextStorage setAttributedString:attributedString];
219 [attributedString release];
221 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
222 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
224 *x=(int)usedRect.size.width;
226 *y=(int)usedRect.size.height;
233 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
235 wxAutoNSAutoreleasePool pool;
236 if(!CocoaTakeFocus()) return;
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 // Set the color (and later font) attributes
244 NSColor *fgColor = m_textForegroundColour.GetNSColor();
245 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
247 fgColor = [NSColor clearColor];
249 bgColor = [NSColor clearColor];
250 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
251 fgColor, NSForegroundColorAttributeName,
252 bgColor, NSBackgroundColorAttributeName,
254 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
257 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
258 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
259 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
260 // there is no length or we don't start at zero
261 if(!glyphRange.length)
263 wxASSERT_MSG(glyphRange.location==0,wxT("glyphRange must begin at zero"));
265 NSAffineTransform *transform = [NSAffineTransform transform];
266 [transform translateXBy:x yBy:y];
268 NSAffineTransform *flipTransform = [NSAffineTransform transform];
270 y' = 0x + -1y + window's height
272 NSAffineTransformStruct matrix = {
275 , 0, usedRect.size.height
277 [flipTransform setTransformStruct: matrix];
279 NSGraphicsContext *context = [NSGraphicsContext currentContext];
280 [context saveGraphicsState];
282 [flipTransform concat];
284 // Draw+fill a rectangle so we can see where the shit is supposed to be.
285 wxLogTrace(wxTRACE_COCOA,wxT("(%f,%f) (%fx%f)"),usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
286 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
287 [[NSColor blackColor] set];
289 [[NSColor blueColor] set];
293 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
294 layoutLocation.x = 0.0;
295 layoutLocation.y *= -1.0;
296 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
297 if(m_backgroundMode==wxSOLID)
298 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
299 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
301 [context restoreGraphicsState];
304 // wxDCBase functions
305 int wxDCBase::DeviceToLogicalX(int x) const
310 int wxDCBase::DeviceToLogicalY(int y) const
315 int wxDCBase::DeviceToLogicalXRel(int x) const
320 int wxDCBase::DeviceToLogicalYRel(int y) const
325 int wxDCBase::LogicalToDeviceX(int x) const
330 int wxDCBase::LogicalToDeviceY(int y) const
335 int wxDCBase::LogicalToDeviceXRel(int x) const
340 int wxDCBase::LogicalToDeviceYRel(int y) const
345 ///////////////////////////////////////////////////////////////////////////
346 // cut here, the rest is stubs
347 ///////////////////////////////////////////////////////////////////////////
349 //-----------------------------------------------------------------------------
351 //-----------------------------------------------------------------------------
353 #define mm2inches 0.0393700787402
354 #define inches2mm 25.4
355 #define mm2twips 56.6929133859
356 #define twips2mm 0.0176388888889
357 #define mm2pt 2.83464566929
358 #define pt2mm 0.352777777778
360 //-----------------------------------------------------------------------------
362 //-----------------------------------------------------------------------------
364 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
368 void wxDC::DoDrawPoint( int x, int y )
372 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
376 void wxDC::DoDrawLines( int, wxPoint *, int, int )
380 int wxDC::GetDepth() const
385 wxSize wxDC::GetPPI() const
390 bool wxDC::CanGetTextExtent() const
395 wxCoord wxDC::GetCharHeight() const
400 wxCoord wxDC::GetCharWidth() const
405 bool wxDC::CanDrawBitmap() const
410 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
415 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
419 void wxDC::SetPen(const wxPen& pen)
424 void wxDC::SetBrush(const wxBrush& brush)
429 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
433 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
437 void wxDC::DestroyClippingRegion()
441 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
445 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
449 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
453 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
457 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
459 wxAutoNSAutoreleasePool pool;
460 if(!CocoaTakeFocus()) return;
465 // Draw a rect so we can see where it's supposed to be
466 wxLogTrace(wxTRACE_COCOA,wxT("image at (%d,%d) size %dx%d"),x,y,bmp.GetWidth(),bmp.GetHeight());
467 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
468 [[NSColor blackColor] set];
470 [[NSColor blueColor] set];
474 NSAffineTransform *transform = [NSAffineTransform transform];
475 [transform translateXBy:x yBy:y];
477 NSAffineTransform *flipTransform = [NSAffineTransform transform];
479 y' = 0x + -1y + window's height
481 NSAffineTransformStruct matrix = {
486 [flipTransform setTransformStruct: matrix];
488 NSGraphicsContext *context = [NSGraphicsContext currentContext];
489 [context saveGraphicsState];
491 [flipTransform concat];
493 NSImage *nsimage = [bmp.GetNSImage(useMask) retain];
495 [nsimage drawAtPoint: NSMakePoint(0,0)
496 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
497 operation: NSCompositeSourceOver
501 [context restoreGraphicsState];
504 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
509 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
514 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)
516 if(!CocoaTakeFocus()) return false;
517 if(!source) return false;
518 return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height,
519 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
522 bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
523 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
524 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
529 void wxDC::DoGetSize( int* width, int* height ) const
531 *width = m_maxX-m_minX;
532 *height = m_maxY-m_minY;
535 void wxDC::DoGetSizeMM( int* width, int* height ) const
542 void wxDC::SetTextForeground( const wxColour &col )
545 m_textForegroundColour = col;
548 void wxDC::SetTextBackground( const wxColour &col )
551 m_textBackgroundColour = col;
558 void wxDC::SetBackground(const wxBrush& brush)
560 m_backgroundBrush = brush;
563 void wxDC::SetPalette(const wxPalette&)
567 void wxDC::SetLogicalFunction(int)
572 void wxDC::SetMapMode( int mode )
586 SetLogicalScale( 1.0, 1.0 );
589 if (mode != wxMM_TEXT)
594 void wxDC::SetUserScale( double x, double y )
596 // allow negative ? -> no
599 ComputeScaleAndOrigin();
602 void wxDC::SetLogicalScale( double x, double y )
607 ComputeScaleAndOrigin();
610 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
612 m_logicalOriginX = x * m_signX; // is this still correct ?
613 m_logicalOriginY = y * m_signY;
614 ComputeScaleAndOrigin();
617 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
619 ComputeScaleAndOrigin();
622 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
624 m_signX = (xLeftRight ? 1 : -1);
625 m_signY = (yBottomUp ? -1 : 1);
626 ComputeScaleAndOrigin();
629 void wxDC::ComputeScaleAndOrigin(void)
631 // CMB: copy scale to see if it changes
632 double origScaleX = m_scaleX;
633 double origScaleY = m_scaleY;
635 m_scaleX = m_logicalScaleX * m_userScaleX;
636 m_scaleY = m_logicalScaleY * m_userScaleY;
638 // CMB: if scale has changed call SetPen to recalulate the line width
639 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
641 // this is a bit artificial, but we need to force wxDC to think
642 // the pen has changed
643 const wxPen* pen = & GetPen();