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 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
28 wxDC *wxDC::sm_focusedDC = NULL;
29 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
30 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
31 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
33 void wxDC::CocoaInitializeTextSystem()
35 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
37 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
39 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
40 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
41 // NSTextStorage retains NSLayoutManager, but so do we
42 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
44 // NOTE: initWithContainerSize is the designated initializer, but the
45 // Apple CircleView sample gets away with just calling init, which
46 // is all we really need for our purposes.
47 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
48 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
49 // NSLayoutManager retains NSTextContainer, but so do we
50 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
53 void wxDC::CocoaShutdownTextSystem()
55 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
56 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
57 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
68 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
70 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
71 [m_textForegroundColour.GetNSColor() set];
73 [m_brush.GetNSColor() set];
77 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
79 NSBezierPath *bezpath = [NSBezierPath bezierPath];
80 [bezpath moveToPoint:NSMakePoint(x1,y1)];
81 [bezpath lineToPoint:NSMakePoint(x2,y2)];
83 [m_textForegroundColour.GetNSColor() set];
87 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
89 wxAutoNSAutoreleasePool pool;
90 // FIXME: Cache this so it can be used for DoDrawText
91 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
92 NSAttributedString *attributedString = [[NSAttributedString alloc]
93 initWithString:[NSString stringWithCString:text.c_str()]];
94 [sm_cocoaNSTextStorage setAttributedString:attributedString];
95 [attributedString release];
97 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
98 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
100 *x=(int)usedRect.size.width;
102 *y=(int)usedRect.size.height;
109 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
111 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
112 NSAttributedString *attributedString = [[NSAttributedString alloc]
113 initWithString:[NSString stringWithCString:text.c_str()]];
114 [sm_cocoaNSTextStorage setAttributedString:attributedString];
115 [attributedString release];
117 // Set the color (and later font) attributes
118 NSColor *fgColor = m_textForegroundColour.GetNSColor();
119 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
121 fgColor = [NSColor clearColor];
123 bgColor = [NSColor clearColor];
124 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
125 fgColor, NSForegroundColorAttributeName,
126 bgColor, NSBackgroundColorAttributeName,
128 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
131 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
132 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
133 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
134 // there is no length or we don't start at zero
135 if(!glyphRange.length)
137 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
139 NSAffineTransform *transform = [NSAffineTransform transform];
140 [transform translateXBy:x yBy:y];
142 NSAffineTransform *flipTransform = [NSAffineTransform transform];
144 y' = 0x + -1y + window's height
146 NSAffineTransformStruct matrix = {
149 , 0, usedRect.size.height
151 [flipTransform setTransformStruct: matrix];
153 NSGraphicsContext *context = [NSGraphicsContext currentContext];
154 [context saveGraphicsState];
156 [flipTransform concat];
158 // Draw+fill a rectangle so we can see where the shit is supposed to be.
159 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
160 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
161 [[NSColor blackColor] set];
163 [[NSColor blueColor] set];
167 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
168 layoutLocation.x = 0.0;
169 layoutLocation.y *= -1.0;
170 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
171 if(m_backgroundMode==wxSOLID)
172 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
173 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
175 [context restoreGraphicsState];
178 // wxDCBase functions
179 int wxDCBase::DeviceToLogicalX(int x) const
184 int wxDCBase::DeviceToLogicalY(int y) const
189 int wxDCBase::LogicalToDeviceX(int x) const
194 int wxDCBase::LogicalToDeviceY(int y) const
199 ///////////////////////////////////////////////////////////////////////////
200 // cut here, the rest is stubs
201 ///////////////////////////////////////////////////////////////////////////
203 //-----------------------------------------------------------------------------
205 //-----------------------------------------------------------------------------
207 #define mm2inches 0.0393700787402
208 #define inches2mm 25.4
209 #define mm2twips 56.6929133859
210 #define twips2mm 0.0176388888889
211 #define mm2pt 2.83464566929
212 #define pt2mm 0.352777777778
214 //-----------------------------------------------------------------------------
216 //-----------------------------------------------------------------------------
218 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
222 void wxDC::DoDrawPoint( int x, int y )
226 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
230 void wxDC::DoDrawLines( int, wxPoint *, int, int )
234 int wxDC::GetDepth() const
239 wxSize wxDC::GetPPI() const
244 bool wxDC::CanGetTextExtent() const
249 wxCoord wxDC::GetCharHeight() const
254 wxCoord wxDC::GetCharWidth() const
259 bool wxDC::CanDrawBitmap() const
264 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
269 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
273 void wxDC::SetPen(const wxPen& pen)
277 void wxDC::SetBrush(const wxBrush& brush)
282 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
286 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
290 void wxDC::DestroyClippingRegion()
294 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
298 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
302 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
306 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
310 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
316 // Draw a rect so we can see where it's supposed to be
317 wxLogDebug("image at (%d,%d) size %dx%d",x,y,bmp.GetWidth(),bmp.GetHeight());
318 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
319 [[NSColor blackColor] set];
321 [[NSColor blueColor] set];
325 NSAffineTransform *transform = [NSAffineTransform transform];
326 [transform translateXBy:x yBy:y];
328 NSAffineTransform *flipTransform = [NSAffineTransform transform];
330 y' = 0x + -1y + window's height
332 NSAffineTransformStruct matrix = {
337 [flipTransform setTransformStruct: matrix];
339 NSGraphicsContext *context = [NSGraphicsContext currentContext];
340 [context saveGraphicsState];
342 [flipTransform concat];
344 NSImage *nsimage = [[NSImage alloc]
345 initWithSize:NSMakeSize(bmp.GetWidth(), bmp.GetHeight())];
346 [nsimage addRepresentation: const_cast<wxBitmap&>(bmp).GetNSBitmapImageRep()];
347 [nsimage drawAtPoint: NSMakePoint(0,0)
348 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
349 operation: NSCompositeCopy
353 [context restoreGraphicsState];
356 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
361 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
366 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)
371 void wxDC::DoGetSize( int* width, int* height ) const
373 *width = m_maxX-m_minX;
374 *height = m_maxY-m_minY;
377 void wxDC::DoGetSizeMM( int* width, int* height ) const
384 void wxDC::SetTextForeground( const wxColour &col )
387 m_textForegroundColour = col;
390 void wxDC::SetTextBackground( const wxColour &col )
393 m_textBackgroundColour = col;
400 void wxDC::SetBackground(const wxBrush& brush)
402 m_backgroundBrush = brush;
405 void wxDC::SetPalette(const wxPalette&)
409 void wxDC::SetLogicalFunction(int)
414 void wxDC::SetMapMode( int mode )
428 SetLogicalScale( 1.0, 1.0 );
431 if (mode != wxMM_TEXT)
436 void wxDC::SetUserScale( double x, double y )
438 // allow negative ? -> no
441 ComputeScaleAndOrigin();
444 void wxDC::SetLogicalScale( double x, double y )
449 ComputeScaleAndOrigin();
452 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
454 m_logicalOriginX = x * m_signX; // is this still correct ?
455 m_logicalOriginY = y * m_signY;
456 ComputeScaleAndOrigin();
459 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
461 ComputeScaleAndOrigin();
464 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
466 m_signX = (xLeftRight ? 1 : -1);
467 m_signY = (yBottomUp ? -1 : 1);
468 ComputeScaleAndOrigin();
471 void wxDC::ComputeScaleAndOrigin(void)
473 // CMB: copy scale to see if it changes
474 double origScaleX = m_scaleX;
475 double origScaleY = m_scaleY;
477 m_scaleX = m_logicalScaleX * m_userScaleX;
478 m_scaleY = m_logicalScaleY * m_userScaleY;
480 // CMB: if scale has changed call SetPen to recalulate the line width
481 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
483 // this is a bit artificial, but we need to force wxDC to think
484 // the pen has changed
485 wxPen* pen = & GetPen();