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 void wxDC::CocoaInitializeTextSystem()
38 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
40 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
42 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
43 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
44 // NSTextStorage retains NSLayoutManager, but so do we
45 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
47 // NOTE: initWithContainerSize is the designated initializer, but the
48 // Apple CircleView sample gets away with just calling init, which
49 // is all we really need for our purposes.
50 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
51 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
52 // NSLayoutManager retains NSTextContainer, but so do we
53 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
56 void wxDC::CocoaShutdownTextSystem()
58 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
59 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
60 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
63 void wxDC::CocoaUnwindStackAndLoseFocus()
65 wxCocoaDCStack::Node *ourNode=sm_cocoaDCStack.Find(this);
68 wxCocoaDCStack::Node *node=sm_cocoaDCStack.GetFirst();
69 for(;node!=ourNode; node=sm_cocoaDCStack.GetFirst())
71 wxDC *dc = node->GetData();
74 if(!dc->CocoaUnlockFocus())
76 wxFAIL_MSG("Unable to unlock focus on higher-level DC!");
78 sm_cocoaDCStack.DeleteNode(node);
80 wxASSERT(node==ourNode);
81 wxASSERT(ourNode->GetData() == this);
82 ourNode->GetData()->CocoaUnlockFocus();
83 sm_cocoaDCStack.DeleteNode(ourNode);
87 bool wxDC::CocoaUnwindStackAndTakeFocus()
89 wxCocoaDCStack::Node *node=sm_cocoaDCStack.GetFirst();
90 for(;node;node = sm_cocoaDCStack.GetFirst())
92 wxDC *dc = node->GetData();
94 // If we're on the stack, then it's unwound enough and we have focus
97 // If unable to unlockFocus (e.g. wxPaintDC) stop here
98 if(!dc->CocoaUnlockFocus())
100 sm_cocoaDCStack.DeleteNode(node);
102 return CocoaLockFocus();
107 m_cocoaFlipped = false;
115 bool wxDC::CocoaLockFocus()
120 bool wxDC::CocoaUnlockFocus()
125 void wxDC::CocoaApplyTransformations()
127 // This transform flips the graphics since wxDC uses top-left origin
130 // The transform is auto released
131 NSAffineTransform *transform = [NSAffineTransform transform];
133 y' = 0x + -1y + window's height
135 NSAffineTransformStruct matrix = {
140 [transform setTransformStruct: matrix];
141 // Apply the transform
144 // TODO: Apply scaling transformation
147 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
149 if(!CocoaTakeFocus()) return;
150 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
151 [m_textForegroundColour.GetNSColor() set];
153 [m_brush.GetNSColor() set];
157 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
159 if(!CocoaTakeFocus()) return;
160 NSBezierPath *bezpath = [NSBezierPath bezierPath];
161 [bezpath moveToPoint:NSMakePoint(x1,y1)];
162 [bezpath lineToPoint:NSMakePoint(x2,y2)];
164 [m_textForegroundColour.GetNSColor() set];
168 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
170 wxAutoNSAutoreleasePool pool;
171 // FIXME: Cache this so it can be used for DoDrawText
172 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
173 NSAttributedString *attributedString = [[NSAttributedString alloc]
174 initWithString:[NSString stringWithCString:text.c_str()]];
175 [sm_cocoaNSTextStorage setAttributedString:attributedString];
176 [attributedString release];
178 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
179 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
181 *x=(int)usedRect.size.width;
183 *y=(int)usedRect.size.height;
190 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
192 if(!CocoaTakeFocus()) return;
193 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
194 NSAttributedString *attributedString = [[NSAttributedString alloc]
195 initWithString:[NSString stringWithCString:text.c_str()]];
196 [sm_cocoaNSTextStorage setAttributedString:attributedString];
197 [attributedString release];
199 // Set the color (and later font) attributes
200 NSColor *fgColor = m_textForegroundColour.GetNSColor();
201 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
203 fgColor = [NSColor clearColor];
205 bgColor = [NSColor clearColor];
206 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
207 fgColor, NSForegroundColorAttributeName,
208 bgColor, NSBackgroundColorAttributeName,
210 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
213 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
214 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
215 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
216 // there is no length or we don't start at zero
217 if(!glyphRange.length)
219 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
221 NSAffineTransform *transform = [NSAffineTransform transform];
222 [transform translateXBy:x yBy:y];
224 NSAffineTransform *flipTransform = [NSAffineTransform transform];
226 y' = 0x + -1y + window's height
228 NSAffineTransformStruct matrix = {
231 , 0, usedRect.size.height
233 [flipTransform setTransformStruct: matrix];
235 NSGraphicsContext *context = [NSGraphicsContext currentContext];
236 [context saveGraphicsState];
238 [flipTransform concat];
240 // Draw+fill a rectangle so we can see where the shit is supposed to be.
241 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
242 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
243 [[NSColor blackColor] set];
245 [[NSColor blueColor] set];
249 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
250 layoutLocation.x = 0.0;
251 layoutLocation.y *= -1.0;
252 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
253 if(m_backgroundMode==wxSOLID)
254 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
255 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
257 [context restoreGraphicsState];
260 // wxDCBase functions
261 int wxDCBase::DeviceToLogicalX(int x) const
266 int wxDCBase::DeviceToLogicalY(int y) const
271 int wxDCBase::LogicalToDeviceX(int x) const
276 int wxDCBase::LogicalToDeviceY(int y) const
281 ///////////////////////////////////////////////////////////////////////////
282 // cut here, the rest is stubs
283 ///////////////////////////////////////////////////////////////////////////
285 //-----------------------------------------------------------------------------
287 //-----------------------------------------------------------------------------
289 #define mm2inches 0.0393700787402
290 #define inches2mm 25.4
291 #define mm2twips 56.6929133859
292 #define twips2mm 0.0176388888889
293 #define mm2pt 2.83464566929
294 #define pt2mm 0.352777777778
296 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------
300 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
304 void wxDC::DoDrawPoint( int x, int y )
308 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
312 void wxDC::DoDrawLines( int, wxPoint *, int, int )
316 int wxDC::GetDepth() const
321 wxSize wxDC::GetPPI() const
326 bool wxDC::CanGetTextExtent() const
331 wxCoord wxDC::GetCharHeight() const
336 wxCoord wxDC::GetCharWidth() const
341 bool wxDC::CanDrawBitmap() const
346 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
351 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
355 void wxDC::SetPen(const wxPen& pen)
359 void wxDC::SetBrush(const wxBrush& brush)
364 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
368 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
372 void wxDC::DestroyClippingRegion()
376 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
380 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
384 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
388 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
392 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
394 if(!CocoaTakeFocus()) return;
399 // Draw a rect so we can see where it's supposed to be
400 wxLogDebug("image at (%d,%d) size %dx%d",x,y,bmp.GetWidth(),bmp.GetHeight());
401 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
402 [[NSColor blackColor] set];
404 [[NSColor blueColor] set];
408 NSAffineTransform *transform = [NSAffineTransform transform];
409 [transform translateXBy:x yBy:y];
411 NSAffineTransform *flipTransform = [NSAffineTransform transform];
413 y' = 0x + -1y + window's height
415 NSAffineTransformStruct matrix = {
420 [flipTransform setTransformStruct: matrix];
422 NSGraphicsContext *context = [NSGraphicsContext currentContext];
423 [context saveGraphicsState];
425 [flipTransform concat];
427 NSImage *nsimage = [[NSImage alloc]
428 initWithSize:NSMakeSize(bmp.GetWidth(), bmp.GetHeight())];
429 [nsimage addRepresentation: const_cast<wxBitmap&>(bmp).GetNSBitmapImageRep()];
430 [nsimage drawAtPoint: NSMakePoint(0,0)
431 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
432 operation: NSCompositeCopy
436 [context restoreGraphicsState];
439 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
444 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
449 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)
454 void wxDC::DoGetSize( int* width, int* height ) const
456 *width = m_maxX-m_minX;
457 *height = m_maxY-m_minY;
460 void wxDC::DoGetSizeMM( int* width, int* height ) const
467 void wxDC::SetTextForeground( const wxColour &col )
470 m_textForegroundColour = col;
473 void wxDC::SetTextBackground( const wxColour &col )
476 m_textBackgroundColour = col;
483 void wxDC::SetBackground(const wxBrush& brush)
485 m_backgroundBrush = brush;
488 void wxDC::SetPalette(const wxPalette&)
492 void wxDC::SetLogicalFunction(int)
497 void wxDC::SetMapMode( int mode )
511 SetLogicalScale( 1.0, 1.0 );
514 if (mode != wxMM_TEXT)
519 void wxDC::SetUserScale( double x, double y )
521 // allow negative ? -> no
524 ComputeScaleAndOrigin();
527 void wxDC::SetLogicalScale( double x, double y )
532 ComputeScaleAndOrigin();
535 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
537 m_logicalOriginX = x * m_signX; // is this still correct ?
538 m_logicalOriginY = y * m_signY;
539 ComputeScaleAndOrigin();
542 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
544 ComputeScaleAndOrigin();
547 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
549 m_signX = (xLeftRight ? 1 : -1);
550 m_signY = (yBottomUp ? -1 : 1);
551 ComputeScaleAndOrigin();
554 void wxDC::ComputeScaleAndOrigin(void)
556 // CMB: copy scale to see if it changes
557 double origScaleX = m_scaleX;
558 double origScaleY = m_scaleY;
560 m_scaleX = m_logicalScaleX * m_userScaleX;
561 m_scaleY = m_logicalScaleY * m_userScaleY;
563 // CMB: if scale has changed call SetPen to recalulate the line width
564 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
566 // this is a bit artificial, but we need to force wxDC to think
567 // the pen has changed
568 wxPen* pen = & GetPen();