Put wxAutoNSAutoreleasePool in methods that may be used outside the run loop.
[wxWidgets.git] / src / cocoa / dc.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/dc.mm
3 // Purpose:     wxDC
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/04/01
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/dc.h"
13 #include "wx/log.h"
14
15 #include "wx/cocoa/autorelease.h"
16
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
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;
31
32 void wxDC::CocoaInitializeTextSystem()
33 {
34     wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized!  BAD PROGRAMMER!");
35
36     sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
37
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];
42
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];
50 }
51
52 void wxDC::CocoaShutdownTextSystem()
53 {
54     [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
55     [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
56     [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
57 }
58
59 wxDC::wxDC(void)
60 {
61 }
62
63 wxDC::~wxDC(void)
64 {
65 }
66
67 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
68 {
69     NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
70     [m_textForegroundColour.GetNSColor() set];
71     [bezpath stroke];
72     [m_brush.GetNSColor() set];
73     [bezpath fill];
74 }
75
76 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
77 {
78     NSBezierPath *bezpath = [NSBezierPath bezierPath];
79     [bezpath moveToPoint:NSMakePoint(x1,y1)];
80     [bezpath lineToPoint:NSMakePoint(x2,y2)];
81
82     [m_textForegroundColour.GetNSColor() set];
83     [bezpath stroke];
84 }
85
86 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
87 {
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];
95
96     NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
97     NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
98     if(x)
99         *x=(int)usedRect.size.width;
100     if(y)
101         *y=(int)usedRect.size.height;
102     if(descent)
103         *descent=0;
104     if(externalLeading)
105         *externalLeading=0;
106 }
107
108 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
109 {
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];
115
116     // Set the color (and later font) attributes
117     NSColor *fgColor = m_textForegroundColour.GetNSColor();
118     NSColor *bgColor = m_textBackgroundColour.GetNSColor();
119     if(!fgColor)
120         fgColor = [NSColor clearColor];
121     if(!bgColor)
122         bgColor = [NSColor clearColor];
123     NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
124             fgColor, NSForegroundColorAttributeName,
125             bgColor, NSBackgroundColorAttributeName,
126             nil];
127     [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
128     [attrDict release];
129
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)
135         return;
136     wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
137
138     NSAffineTransform *transform = [NSAffineTransform transform];
139     [transform translateXBy:x yBy:y];
140
141     NSAffineTransform *flipTransform = [NSAffineTransform transform];
142     /*  x' = 1x + 0y + 0
143         y' = 0x + -1y + window's height
144     */
145     NSAffineTransformStruct matrix = {
146         1,  0
147     ,   0, -1
148     ,   0, usedRect.size.height
149     };
150     [flipTransform setTransformStruct: matrix];
151
152     NSGraphicsContext *context = [NSGraphicsContext currentContext];
153     [context saveGraphicsState];
154     [transform concat];
155     [flipTransform concat];
156     #if 0
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];
161     [bezpath stroke];
162     [[NSColor blueColor] set];
163     [bezpath fill];
164     #endif
165
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];
173
174     [context restoreGraphicsState];
175 }
176
177 // wxDCBase functions
178 int wxDCBase::DeviceToLogicalX(int x) const
179 {
180     return x;
181 }
182
183 int wxDCBase::DeviceToLogicalY(int y) const
184 {
185     return y;
186 }
187
188 int wxDCBase::LogicalToDeviceX(int x) const
189 {
190     return x;
191 }
192
193 int wxDCBase::LogicalToDeviceY(int y) const
194 {
195     return y;
196 }
197
198 ///////////////////////////////////////////////////////////////////////////
199 // cut here, the rest is stubs
200 ///////////////////////////////////////////////////////////////////////////
201
202 //-----------------------------------------------------------------------------
203 // constants
204 //-----------------------------------------------------------------------------
205
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
212
213 //-----------------------------------------------------------------------------
214 // wxDC
215 //-----------------------------------------------------------------------------
216
217 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
218 {
219 };
220
221 void wxDC::DoDrawPoint( int x, int y ) 
222
223 };
224
225 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
226 {
227 };
228
229 void wxDC::DoDrawLines( int, wxPoint *, int, int )
230 {
231 }
232
233 int wxDC::GetDepth() const
234 {
235     return 0;
236 }
237
238 wxSize wxDC::GetPPI() const
239 {
240     return wxSize(0,0);
241 }
242
243 bool wxDC::CanGetTextExtent() const
244 {
245     return false;
246 }
247
248 wxCoord wxDC::GetCharHeight() const
249 {
250     return 0;
251 }
252
253 wxCoord wxDC::GetCharWidth() const
254 {
255     return 0;
256 }
257
258 bool wxDC::CanDrawBitmap() const
259 {
260     return false;
261 }
262
263 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
264 {
265     return false;
266 }
267
268 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
269 {
270 }
271     
272 void wxDC::SetPen(const wxPen& pen)
273 {
274 }
275
276 void wxDC::SetBrush(const wxBrush& brush)
277 {
278     m_brush = brush;
279 }
280
281 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
282 {
283 }
284
285 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
286 {
287 }
288
289 void wxDC::DestroyClippingRegion()
290 {
291 }
292
293 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
294 {
295 }
296
297 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
298 {
299 }
300
301 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
302 {
303 }
304
305 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
306 {
307 }
308
309 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
310 {
311 }
312
313 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
314 {
315     return false;
316 }
317
318 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
319 {
320 }
321
322
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)
324 {
325     return false;
326 }
327
328 void wxDC::DoGetSize( int* width, int* height ) const
329 {
330   *width = m_maxX-m_minX;
331   *height = m_maxY-m_minY;
332 };
333
334 void wxDC::DoGetSizeMM( int* width, int* height ) const
335 {
336   int w = 0;
337   int h = 0;
338   GetSize( &w, &h );
339 };
340
341 void wxDC::SetTextForeground( const wxColour &col )
342 {
343   if (!Ok()) return;
344   m_textForegroundColour = col;
345 };
346
347 void wxDC::SetTextBackground( const wxColour &col )
348 {
349   if (!Ok()) return;
350   m_textBackgroundColour = col;
351 };
352
353 void wxDC::Clear()
354 {
355 }
356
357 void wxDC::SetBackground(const wxBrush& brush)
358 {
359     m_backgroundBrush = brush;
360 }
361
362 void wxDC::SetPalette(const wxPalette&)
363 {
364 }
365
366 void wxDC::SetLogicalFunction(int)
367 {
368 }
369
370
371 void wxDC::SetMapMode( int mode )
372 {
373   switch (mode) 
374   {
375     case wxMM_TWIPS:
376       break;
377     case wxMM_POINTS:
378       break;
379     case wxMM_METRIC:
380       break;
381     case wxMM_LOMETRIC:
382       break;
383     default:
384     case wxMM_TEXT:
385       SetLogicalScale( 1.0, 1.0 );
386       break;
387   };
388   if (mode != wxMM_TEXT)
389   {
390   };
391 };
392
393 void wxDC::SetUserScale( double x, double y )
394 {
395   // allow negative ? -> no
396   m_userScaleX = x;
397   m_userScaleY = y;
398   ComputeScaleAndOrigin();
399 };
400
401 void wxDC::SetLogicalScale( double x, double y )
402 {
403   // allow negative ?
404   m_logicalScaleX = x;
405   m_logicalScaleY = y;
406   ComputeScaleAndOrigin();
407 };
408
409 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
410 {
411   m_logicalOriginX = x * m_signX;   // is this still correct ?
412   m_logicalOriginY = y * m_signY;
413   ComputeScaleAndOrigin();
414 };
415
416 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
417 {
418   ComputeScaleAndOrigin();
419 };
420
421 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
422 {
423   m_signX = (xLeftRight ?  1 : -1);
424   m_signY = (yBottomUp  ? -1 :  1);
425   ComputeScaleAndOrigin();
426 };
427
428 void wxDC::ComputeScaleAndOrigin(void)
429 {
430   // CMB: copy scale to see if it changes
431   double origScaleX = m_scaleX;
432   double origScaleY = m_scaleY;
433
434   m_scaleX = m_logicalScaleX * m_userScaleX;
435   m_scaleY = m_logicalScaleY * m_userScaleY;
436
437   // CMB: if scale has changed call SetPen to recalulate the line width 
438   if (m_scaleX != origScaleX || m_scaleY != origScaleY)
439   {
440     // this is a bit artificial, but we need to force wxDC to think
441     // the pen has changed
442     wxPen* pen = & GetPen();
443     wxPen tempPen;
444     m_pen = tempPen;
445     SetPen(* pen);
446   }
447 };
448