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>
26 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
27 wxDC *wxDC::sm_focusedDC = NULL;
28 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
29 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
30 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
32 void wxDC::CocoaInitializeTextSystem()
34 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
36 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
38 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
39 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
40 // NSTextStorage retains NSLayoutManager, but so do we
41 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
43 // NOTE: initWithContainerSize is the designated initializer, but the
44 // Apple CircleView sample gets away with just calling init, which
45 // is all we really need for our purposes.
46 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
47 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
48 // NSLayoutManager retains NSTextContainer, but so do we
49 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
52 void wxDC::CocoaShutdownTextSystem()
54 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
55 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
56 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
67 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
69 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
70 [m_textForegroundColour.GetNSColor() set];
72 [m_brush.GetNSColor() set];
76 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
78 NSBezierPath *bezpath = [NSBezierPath bezierPath];
79 [bezpath moveToPoint:NSMakePoint(x1,y1)];
80 [bezpath lineToPoint:NSMakePoint(x2,y2)];
82 [m_textForegroundColour.GetNSColor() set];
86 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
88 wxAutoNSAutoreleasePool pool;
89 // FIXME: Cache this so it can be used for DoDrawText
90 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
91 NSAttributedString *attributedString = [[NSAttributedString alloc]
92 initWithString:[NSString stringWithCString:text.c_str()]];
93 [sm_cocoaNSTextStorage setAttributedString:attributedString];
94 [attributedString release];
96 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
97 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
99 *x=(int)usedRect.size.width;
101 *y=(int)usedRect.size.height;
108 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
110 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
111 NSAttributedString *attributedString = [[NSAttributedString alloc]
112 initWithString:[NSString stringWithCString:text.c_str()]];
113 [sm_cocoaNSTextStorage setAttributedString:attributedString];
114 [attributedString release];
116 // Set the color (and later font) attributes
117 NSColor *fgColor = m_textForegroundColour.GetNSColor();
118 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
120 fgColor = [NSColor clearColor];
122 bgColor = [NSColor clearColor];
123 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
124 fgColor, NSForegroundColorAttributeName,
125 bgColor, NSBackgroundColorAttributeName,
127 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
130 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
131 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
132 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
133 // there is no length or we don't start at zero
134 if(!glyphRange.length)
136 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
138 NSAffineTransform *transform = [NSAffineTransform transform];
139 [transform translateXBy:x yBy:y];
141 NSAffineTransform *flipTransform = [NSAffineTransform transform];
143 y' = 0x + -1y + window's height
145 NSAffineTransformStruct matrix = {
148 , 0, usedRect.size.height
150 [flipTransform setTransformStruct: matrix];
152 NSGraphicsContext *context = [NSGraphicsContext currentContext];
153 [context saveGraphicsState];
155 [flipTransform concat];
157 // Draw+fill a rectangle so we can see where the shit is supposed to be.
158 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
159 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
160 [[NSColor blackColor] set];
162 [[NSColor blueColor] set];
166 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
167 layoutLocation.x = 0.0;
168 layoutLocation.y *= -1.0;
169 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
170 if(m_backgroundMode==wxSOLID)
171 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
172 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
174 [context restoreGraphicsState];
177 // wxDCBase functions
178 int wxDCBase::DeviceToLogicalX(int x) const
183 int wxDCBase::DeviceToLogicalY(int y) const
188 int wxDCBase::LogicalToDeviceX(int x) const
193 int wxDCBase::LogicalToDeviceY(int y) const
198 ///////////////////////////////////////////////////////////////////////////
199 // cut here, the rest is stubs
200 ///////////////////////////////////////////////////////////////////////////
202 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
206 #define mm2inches 0.0393700787402
207 #define inches2mm 25.4
208 #define mm2twips 56.6929133859
209 #define twips2mm 0.0176388888889
210 #define mm2pt 2.83464566929
211 #define pt2mm 0.352777777778
213 //-----------------------------------------------------------------------------
215 //-----------------------------------------------------------------------------
217 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
221 void wxDC::DoDrawPoint( int x, int y )
225 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
229 void wxDC::DoDrawLines( int, wxPoint *, int, int )
233 int wxDC::GetDepth() const
238 wxSize wxDC::GetPPI() const
243 bool wxDC::CanGetTextExtent() const
248 wxCoord wxDC::GetCharHeight() const
253 wxCoord wxDC::GetCharWidth() const
258 bool wxDC::CanDrawBitmap() const
263 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
268 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
272 void wxDC::SetPen(const wxPen& pen)
276 void wxDC::SetBrush(const wxBrush& brush)
281 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
285 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
289 void wxDC::DestroyClippingRegion()
293 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
297 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
301 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
305 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
309 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
313 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
318 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
323 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)
328 void wxDC::DoGetSize( int* width, int* height ) const
330 *width = m_maxX-m_minX;
331 *height = m_maxY-m_minY;
334 void wxDC::DoGetSizeMM( int* width, int* height ) const
341 void wxDC::SetTextForeground( const wxColour &col )
344 m_textForegroundColour = col;
347 void wxDC::SetTextBackground( const wxColour &col )
350 m_textBackgroundColour = col;
357 void wxDC::SetBackground(const wxBrush& brush)
359 m_backgroundBrush = brush;
362 void wxDC::SetPalette(const wxPalette&)
366 void wxDC::SetLogicalFunction(int)
371 void wxDC::SetMapMode( int mode )
385 SetLogicalScale( 1.0, 1.0 );
388 if (mode != wxMM_TEXT)
393 void wxDC::SetUserScale( double x, double y )
395 // allow negative ? -> no
398 ComputeScaleAndOrigin();
401 void wxDC::SetLogicalScale( double x, double y )
406 ComputeScaleAndOrigin();
409 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
411 m_logicalOriginX = x * m_signX; // is this still correct ?
412 m_logicalOriginY = y * m_signY;
413 ComputeScaleAndOrigin();
416 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
418 ComputeScaleAndOrigin();
421 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
423 m_signX = (xLeftRight ? 1 : -1);
424 m_signY = (yBottomUp ? -1 : 1);
425 ComputeScaleAndOrigin();
428 void wxDC::ComputeScaleAndOrigin(void)
430 // CMB: copy scale to see if it changes
431 double origScaleX = m_scaleX;
432 double origScaleY = m_scaleY;
434 m_scaleX = m_logicalScaleX * m_userScaleX;
435 m_scaleY = m_logicalScaleY * m_userScaleY;
437 // CMB: if scale has changed call SetPen to recalulate the line width
438 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
440 // this is a bit artificial, but we need to force wxDC to think
441 // the pen has changed
442 wxPen* pen = & GetPen();