1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dc.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
15 #import <AppKit/NSBezierPath.h>
16 #import <AppKit/NSTextStorage.h>
17 #import <AppKit/NSLayoutManager.h>
18 #import <AppKit/NSTextContainer.h>
19 #import <AppKit/NSGraphicsContext.h>
20 #import <AppKit/NSAffineTransform.h>
21 #import <AppKit/NSColor.h>
22 #import <AppKit/NSTypeSetter.h>
24 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
25 wxDC *wxDC::sm_focusedDC = NULL;
26 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
27 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
28 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
30 void wxDC::CocoaInitializeTextSystem()
32 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
34 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
36 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
37 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
38 // NSTextStorage retains NSLayoutManager, but so do we
39 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
41 // NOTE: initWithContainerSize is the designated initializer, but the
42 // Apple CircleView sample gets away with just calling init, which
43 // is all we really need for our purposes.
44 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
45 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
46 // NSLayoutManager retains NSTextContainer, but so do we
47 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
50 void wxDC::CocoaShutdownTextSystem()
52 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
53 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
54 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
65 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
67 NSGraphicsContext *context = [NSGraphicsContext currentContext];
68 [context saveGraphicsState];
70 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
71 [m_textForegroundColour.GetNSColor() set];
73 [m_brush.GetNSColor() set];
76 [context restoreGraphicsState];
79 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
81 NSGraphicsContext *context = [NSGraphicsContext currentContext];
82 [context saveGraphicsState];
84 NSBezierPath *bezpath = [NSBezierPath bezierPath];
85 [bezpath moveToPoint:NSMakePoint(x1,y1)];
86 [bezpath lineToPoint:NSMakePoint(x2,y2)];
88 [m_textForegroundColour.GetNSColor() set];
91 [context restoreGraphicsState];
94 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
96 // FIXME: Cache this so it can be used for DoDrawText
97 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
98 NSAttributedString *attributedString = [[NSAttributedString alloc]
99 initWithString:[NSString stringWithCString:text.c_str()]];
100 [sm_cocoaNSTextStorage setAttributedString:attributedString];
101 [attributedString release];
103 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
104 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
106 *x=(int)usedRect.size.width;
108 *y=(int)usedRect.size.height;
115 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
117 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
118 NSAttributedString *attributedString = [[NSAttributedString alloc]
119 initWithString:[NSString stringWithCString:text.c_str()]];
120 [sm_cocoaNSTextStorage setAttributedString:attributedString];
121 [attributedString release];
123 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
124 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
125 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
126 // there is no length or we don't start at zero
127 if(!glyphRange.length)
129 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
131 NSAffineTransform *transform = [NSAffineTransform transform];
132 [transform translateXBy:x yBy:y];
134 NSAffineTransform *flipTransform = [NSAffineTransform transform];
136 y' = 0x + -1y + window's height
138 NSAffineTransformStruct matrix = {
141 , 0, usedRect.size.height
143 [flipTransform setTransformStruct: matrix];
145 NSGraphicsContext *context = [NSGraphicsContext currentContext];
146 [context saveGraphicsState];
148 [flipTransform concat];
150 // Draw+fill a rectangle so we can see where the shit is supposed to be.
151 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
152 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
153 [[NSColor blackColor] set];
155 [[NSColor blueColor] set];
159 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
160 layoutLocation.x = 0.0;
161 layoutLocation.y *= -1.0;
162 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
163 // NOTE: That's NSMakePoint, not NSMakePint (working on that though)
164 [m_textForegroundColour.GetNSColor() set];
165 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
167 [context restoreGraphicsState];
170 // wxDCBase functions
171 int wxDCBase::DeviceToLogicalX(int x) const
176 int wxDCBase::DeviceToLogicalY(int y) const
181 int wxDCBase::LogicalToDeviceX(int x) const
186 int wxDCBase::LogicalToDeviceY(int y) const
191 ///////////////////////////////////////////////////////////////////////////
192 // cut here, the rest is stubs
193 ///////////////////////////////////////////////////////////////////////////
195 //-----------------------------------------------------------------------------
197 //-----------------------------------------------------------------------------
199 #define mm2inches 0.0393700787402
200 #define inches2mm 25.4
201 #define mm2twips 56.6929133859
202 #define twips2mm 0.0176388888889
203 #define mm2pt 2.83464566929
204 #define pt2mm 0.352777777778
206 //-----------------------------------------------------------------------------
208 //-----------------------------------------------------------------------------
210 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
214 void wxDC::DoDrawPoint( int x, int y )
218 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
222 void wxDC::DoDrawLines( int, wxPoint *, int, int )
226 int wxDC::GetDepth() const
231 wxSize wxDC::GetPPI() const
236 bool wxDC::CanGetTextExtent() const
241 wxCoord wxDC::GetCharHeight() const
246 wxCoord wxDC::GetCharWidth() const
251 bool wxDC::CanDrawBitmap() const
256 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
261 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
265 void wxDC::SetPen(const wxPen& pen)
269 void wxDC::SetBrush(const wxBrush& brush)
274 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
278 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
282 void wxDC::DestroyClippingRegion()
286 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
290 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
294 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
298 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
302 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
306 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
311 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
316 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)
321 void wxDC::DoGetSize( int* width, int* height ) const
323 *width = m_maxX-m_minX;
324 *height = m_maxY-m_minY;
327 void wxDC::DoGetSizeMM( int* width, int* height ) const
334 void wxDC::SetTextForeground( const wxColour &col )
337 m_textForegroundColour = col;
340 void wxDC::SetTextBackground( const wxColour &col )
343 m_textBackgroundColour = col;
350 void wxDC::SetBackground(const wxBrush&)
354 void wxDC::SetPalette(const wxPalette&)
358 void wxDC::SetLogicalFunction(int)
363 void wxDC::SetMapMode( int mode )
377 SetLogicalScale( 1.0, 1.0 );
380 if (mode != wxMM_TEXT)
385 void wxDC::SetUserScale( double x, double y )
387 // allow negative ? -> no
390 ComputeScaleAndOrigin();
393 void wxDC::SetLogicalScale( double x, double y )
398 ComputeScaleAndOrigin();
401 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
403 m_logicalOriginX = x * m_signX; // is this still correct ?
404 m_logicalOriginY = y * m_signY;
405 ComputeScaleAndOrigin();
408 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
410 ComputeScaleAndOrigin();
413 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
415 m_signX = (xLeftRight ? 1 : -1);
416 m_signY = (yBottomUp ? -1 : 1);
417 ComputeScaleAndOrigin();
420 void wxDC::ComputeScaleAndOrigin(void)
422 // CMB: copy scale to see if it changes
423 double origScaleX = m_scaleX;
424 double origScaleY = m_scaleY;
426 m_scaleX = m_logicalScaleX * m_userScaleX;
427 m_scaleY = m_logicalScaleY * m_userScaleY;
429 // CMB: if scale has changed call SetPen to recalulate the line width
430 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
432 // this is a bit artificial, but we need to force wxDC to think
433 // the pen has changed
434 wxPen* pen = & GetPen();