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 wxAutoNSAutoreleasePool pool;
187 if(!CocoaTakeFocus()) return;
188 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
189 CocoaSetPenForNSBezierPath(m_pen,bezpath);
191 [m_brush.GetNSColor() set];
195 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
197 wxAutoNSAutoreleasePool pool;
198 if(!CocoaTakeFocus()) return;
199 NSBezierPath *bezpath = [NSBezierPath bezierPath];
200 [bezpath moveToPoint:NSMakePoint(x1,y1)];
201 [bezpath lineToPoint:NSMakePoint(x2,y2)];
203 CocoaSetPenForNSBezierPath(m_pen,bezpath);
207 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
209 wxAutoNSAutoreleasePool pool;
210 // FIXME: Cache this so it can be used for DoDrawText
211 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
212 NSAttributedString *attributedString = [[NSAttributedString alloc]
213 initWithString:wxNSStringWithWxString(text.c_str())];
214 [sm_cocoaNSTextStorage setAttributedString:attributedString];
215 [attributedString release];
217 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
218 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
220 *x=(int)usedRect.size.width;
222 *y=(int)usedRect.size.height;
229 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
231 wxAutoNSAutoreleasePool pool;
232 if(!CocoaTakeFocus()) return;
233 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
234 NSAttributedString *attributedString = [[NSAttributedString alloc]
235 initWithString:wxNSStringWithWxString(text.c_str())];
236 [sm_cocoaNSTextStorage setAttributedString:attributedString];
237 [attributedString release];
239 // Set the color (and later font) attributes
240 NSColor *fgColor = m_textForegroundColour.GetNSColor();
241 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
243 fgColor = [NSColor clearColor];
245 bgColor = [NSColor clearColor];
246 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
247 fgColor, NSForegroundColorAttributeName,
248 bgColor, NSBackgroundColorAttributeName,
250 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
253 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
254 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
255 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
256 // there is no length or we don't start at zero
257 if(!glyphRange.length)
259 wxASSERT_MSG(glyphRange.location==0,wxT("glyphRange must begin at zero"));
261 NSAffineTransform *transform = [NSAffineTransform transform];
262 [transform translateXBy:x yBy:y];
264 NSAffineTransform *flipTransform = [NSAffineTransform transform];
266 y' = 0x + -1y + window's height
268 NSAffineTransformStruct matrix = {
271 , 0, usedRect.size.height
273 [flipTransform setTransformStruct: matrix];
275 NSGraphicsContext *context = [NSGraphicsContext currentContext];
276 [context saveGraphicsState];
278 [flipTransform concat];
280 // Draw+fill a rectangle so we can see where the shit is supposed to be.
281 wxLogTrace(wxTRACE_COCOA,wxT("(%f,%f) (%fx%f)"),usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
282 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
283 [[NSColor blackColor] set];
285 [[NSColor blueColor] set];
289 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
290 layoutLocation.x = 0.0;
291 layoutLocation.y *= -1.0;
292 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
293 if(m_backgroundMode==wxSOLID)
294 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
295 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
297 [context restoreGraphicsState];
300 // wxDCBase functions
301 int wxDCBase::DeviceToLogicalX(int x) const
306 int wxDCBase::DeviceToLogicalY(int y) const
311 int wxDCBase::DeviceToLogicalXRel(int x) const
316 int wxDCBase::DeviceToLogicalYRel(int y) const
321 int wxDCBase::LogicalToDeviceX(int x) const
326 int wxDCBase::LogicalToDeviceY(int y) const
331 int wxDCBase::LogicalToDeviceXRel(int x) const
336 int wxDCBase::LogicalToDeviceYRel(int y) const
341 ///////////////////////////////////////////////////////////////////////////
342 // cut here, the rest is stubs
343 ///////////////////////////////////////////////////////////////////////////
345 //-----------------------------------------------------------------------------
347 //-----------------------------------------------------------------------------
349 #define mm2inches 0.0393700787402
350 #define inches2mm 25.4
351 #define mm2twips 56.6929133859
352 #define twips2mm 0.0176388888889
353 #define mm2pt 2.83464566929
354 #define pt2mm 0.352777777778
356 //-----------------------------------------------------------------------------
358 //-----------------------------------------------------------------------------
360 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
364 void wxDC::DoDrawPoint( int x, int y )
368 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
372 void wxDC::DoDrawLines( int, wxPoint *, int, int )
376 int wxDC::GetDepth() const
381 wxSize wxDC::GetPPI() const
386 bool wxDC::CanGetTextExtent() const
391 wxCoord wxDC::GetCharHeight() const
396 wxCoord wxDC::GetCharWidth() const
401 bool wxDC::CanDrawBitmap() const
406 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
411 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
415 void wxDC::SetPen(const wxPen& pen)
420 void wxDC::SetBrush(const wxBrush& brush)
425 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
429 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
433 void wxDC::DestroyClippingRegion()
437 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
441 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
445 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
449 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
453 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
455 wxAutoNSAutoreleasePool pool;
456 if(!CocoaTakeFocus()) return;
461 // Draw a rect so we can see where it's supposed to be
462 wxLogTrace(wxTRACE_COCOA,wxT("image at (%d,%d) size %dx%d"),x,y,bmp.GetWidth(),bmp.GetHeight());
463 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
464 [[NSColor blackColor] set];
466 [[NSColor blueColor] set];
470 NSAffineTransform *transform = [NSAffineTransform transform];
471 [transform translateXBy:x yBy:y];
473 NSAffineTransform *flipTransform = [NSAffineTransform transform];
475 y' = 0x + -1y + window's height
477 NSAffineTransformStruct matrix = {
482 [flipTransform setTransformStruct: matrix];
484 NSGraphicsContext *context = [NSGraphicsContext currentContext];
485 [context saveGraphicsState];
487 [flipTransform concat];
489 NSImage *nsimage = [bmp.GetNSImage(useMask) retain];
491 [nsimage drawAtPoint: NSMakePoint(0,0)
492 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
493 operation: NSCompositeSourceOver
497 [context restoreGraphicsState];
500 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
505 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
510 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)
512 if(!CocoaTakeFocus()) return false;
513 if(!source) return false;
514 return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height,
515 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
518 bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
519 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
520 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
525 void wxDC::DoGetSize( int* width, int* height ) const
527 *width = m_maxX-m_minX;
528 *height = m_maxY-m_minY;
531 void wxDC::DoGetSizeMM( int* width, int* height ) const
538 void wxDC::SetTextForeground( const wxColour &col )
541 m_textForegroundColour = col;
544 void wxDC::SetTextBackground( const wxColour &col )
547 m_textBackgroundColour = col;
554 void wxDC::SetBackground(const wxBrush& brush)
556 m_backgroundBrush = brush;
559 void wxDC::SetPalette(const wxPalette&)
563 void wxDC::SetLogicalFunction(int)
568 void wxDC::SetMapMode( int mode )
582 SetLogicalScale( 1.0, 1.0 );
585 if (mode != wxMM_TEXT)
590 void wxDC::SetUserScale( double x, double y )
592 // allow negative ? -> no
595 ComputeScaleAndOrigin();
598 void wxDC::SetLogicalScale( double x, double y )
603 ComputeScaleAndOrigin();
606 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
608 m_logicalOriginX = x * m_signX; // is this still correct ?
609 m_logicalOriginY = y * m_signY;
610 ComputeScaleAndOrigin();
613 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
615 ComputeScaleAndOrigin();
618 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
620 m_signX = (xLeftRight ? 1 : -1);
621 m_signY = (yBottomUp ? -1 : 1);
622 ComputeScaleAndOrigin();
625 void wxDC::ComputeScaleAndOrigin(void)
627 // CMB: copy scale to see if it changes
628 double origScaleX = m_scaleX;
629 double origScaleY = m_scaleY;
631 m_scaleX = m_logicalScaleX * m_userScaleX;
632 m_scaleY = m_logicalScaleY * m_userScaleY;
634 // CMB: if scale has changed call SetPen to recalulate the line width
635 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
637 // this is a bit artificial, but we need to force wxDC to think
638 // the pen has changed
639 wxPen* pen = & GetPen();