]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dc.mm
Use the new wx/cocoa/autorelease.h header.
[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 #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>
23
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;
29
30 void wxDC::CocoaInitializeTextSystem()
31 {
32 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
33
34 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
35
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];
40
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];
48 }
49
50 void wxDC::CocoaShutdownTextSystem()
51 {
52 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
53 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
54 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
55 }
56
57 wxDC::wxDC(void)
58 {
59 }
60
61 wxDC::~wxDC(void)
62 {
63 }
64
65 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
66 {
67 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
68 [m_textForegroundColour.GetNSColor() set];
69 [bezpath stroke];
70 [m_brush.GetNSColor() set];
71 [bezpath fill];
72 }
73
74 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
75 {
76 NSBezierPath *bezpath = [NSBezierPath bezierPath];
77 [bezpath moveToPoint:NSMakePoint(x1,y1)];
78 [bezpath lineToPoint:NSMakePoint(x2,y2)];
79
80 [m_textForegroundColour.GetNSColor() set];
81 [bezpath stroke];
82 }
83
84 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
85 {
86 // FIXME: Cache this so it can be used for DoDrawText
87 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
88 NSAttributedString *attributedString = [[NSAttributedString alloc]
89 initWithString:[NSString stringWithCString:text.c_str()]];
90 [sm_cocoaNSTextStorage setAttributedString:attributedString];
91 [attributedString release];
92
93 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
94 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
95 if(x)
96 *x=(int)usedRect.size.width;
97 if(y)
98 *y=(int)usedRect.size.height;
99 if(descent)
100 *descent=0;
101 if(externalLeading)
102 *externalLeading=0;
103 }
104
105 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
106 {
107 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
108 NSAttributedString *attributedString = [[NSAttributedString alloc]
109 initWithString:[NSString stringWithCString:text.c_str()]];
110 [sm_cocoaNSTextStorage setAttributedString:attributedString];
111 [attributedString release];
112
113 // Set the color (and later font) attributes
114 NSColor *fgColor = m_textForegroundColour.GetNSColor();
115 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
116 if(!fgColor)
117 fgColor = [NSColor clearColor];
118 if(!bgColor)
119 bgColor = [NSColor clearColor];
120 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
121 fgColor, NSForegroundColorAttributeName,
122 bgColor, NSBackgroundColorAttributeName,
123 nil];
124 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
125 [attrDict release];
126
127 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
128 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
129 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
130 // there is no length or we don't start at zero
131 if(!glyphRange.length)
132 return;
133 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
134
135 NSAffineTransform *transform = [NSAffineTransform transform];
136 [transform translateXBy:x yBy:y];
137
138 NSAffineTransform *flipTransform = [NSAffineTransform transform];
139 /* x' = 1x + 0y + 0
140 y' = 0x + -1y + window's height
141 */
142 NSAffineTransformStruct matrix = {
143 1, 0
144 , 0, -1
145 , 0, usedRect.size.height
146 };
147 [flipTransform setTransformStruct: matrix];
148
149 NSGraphicsContext *context = [NSGraphicsContext currentContext];
150 [context saveGraphicsState];
151 [transform concat];
152 [flipTransform concat];
153 #if 0
154 // Draw+fill a rectangle so we can see where the shit is supposed to be.
155 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
156 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
157 [[NSColor blackColor] set];
158 [bezpath stroke];
159 [[NSColor blueColor] set];
160 [bezpath fill];
161 #endif
162
163 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
164 layoutLocation.x = 0.0;
165 layoutLocation.y *= -1.0;
166 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
167 if(m_backgroundMode==wxSOLID)
168 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
169 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
170
171 [context restoreGraphicsState];
172 }
173
174 // wxDCBase functions
175 int wxDCBase::DeviceToLogicalX(int x) const
176 {
177 return x;
178 }
179
180 int wxDCBase::DeviceToLogicalY(int y) const
181 {
182 return y;
183 }
184
185 int wxDCBase::LogicalToDeviceX(int x) const
186 {
187 return x;
188 }
189
190 int wxDCBase::LogicalToDeviceY(int y) const
191 {
192 return y;
193 }
194
195 ///////////////////////////////////////////////////////////////////////////
196 // cut here, the rest is stubs
197 ///////////////////////////////////////////////////////////////////////////
198
199 //-----------------------------------------------------------------------------
200 // constants
201 //-----------------------------------------------------------------------------
202
203 #define mm2inches 0.0393700787402
204 #define inches2mm 25.4
205 #define mm2twips 56.6929133859
206 #define twips2mm 0.0176388888889
207 #define mm2pt 2.83464566929
208 #define pt2mm 0.352777777778
209
210 //-----------------------------------------------------------------------------
211 // wxDC
212 //-----------------------------------------------------------------------------
213
214 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
215 {
216 };
217
218 void wxDC::DoDrawPoint( int x, int y )
219 {
220 };
221
222 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
223 {
224 };
225
226 void wxDC::DoDrawLines( int, wxPoint *, int, int )
227 {
228 }
229
230 int wxDC::GetDepth() const
231 {
232 return 0;
233 }
234
235 wxSize wxDC::GetPPI() const
236 {
237 return wxSize(0,0);
238 }
239
240 bool wxDC::CanGetTextExtent() const
241 {
242 return false;
243 }
244
245 wxCoord wxDC::GetCharHeight() const
246 {
247 return 0;
248 }
249
250 wxCoord wxDC::GetCharWidth() const
251 {
252 return 0;
253 }
254
255 bool wxDC::CanDrawBitmap() const
256 {
257 return false;
258 }
259
260 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
261 {
262 return false;
263 }
264
265 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
266 {
267 }
268
269 void wxDC::SetPen(const wxPen& pen)
270 {
271 }
272
273 void wxDC::SetBrush(const wxBrush& brush)
274 {
275 m_brush = brush;
276 }
277
278 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
279 {
280 }
281
282 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
283 {
284 }
285
286 void wxDC::DestroyClippingRegion()
287 {
288 }
289
290 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
291 {
292 }
293
294 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
295 {
296 }
297
298 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
299 {
300 }
301
302 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
303 {
304 }
305
306 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
307 {
308 }
309
310 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
311 {
312 return false;
313 }
314
315 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
316 {
317 }
318
319
320 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 {
322 return false;
323 }
324
325 void wxDC::DoGetSize( int* width, int* height ) const
326 {
327 *width = m_maxX-m_minX;
328 *height = m_maxY-m_minY;
329 };
330
331 void wxDC::DoGetSizeMM( int* width, int* height ) const
332 {
333 int w = 0;
334 int h = 0;
335 GetSize( &w, &h );
336 };
337
338 void wxDC::SetTextForeground( const wxColour &col )
339 {
340 if (!Ok()) return;
341 m_textForegroundColour = col;
342 };
343
344 void wxDC::SetTextBackground( const wxColour &col )
345 {
346 if (!Ok()) return;
347 m_textBackgroundColour = col;
348 };
349
350 void wxDC::Clear()
351 {
352 }
353
354 void wxDC::SetBackground(const wxBrush& brush)
355 {
356 m_backgroundBrush = brush;
357 }
358
359 void wxDC::SetPalette(const wxPalette&)
360 {
361 }
362
363 void wxDC::SetLogicalFunction(int)
364 {
365 }
366
367
368 void wxDC::SetMapMode( int mode )
369 {
370 switch (mode)
371 {
372 case wxMM_TWIPS:
373 break;
374 case wxMM_POINTS:
375 break;
376 case wxMM_METRIC:
377 break;
378 case wxMM_LOMETRIC:
379 break;
380 default:
381 case wxMM_TEXT:
382 SetLogicalScale( 1.0, 1.0 );
383 break;
384 };
385 if (mode != wxMM_TEXT)
386 {
387 };
388 };
389
390 void wxDC::SetUserScale( double x, double y )
391 {
392 // allow negative ? -> no
393 m_userScaleX = x;
394 m_userScaleY = y;
395 ComputeScaleAndOrigin();
396 };
397
398 void wxDC::SetLogicalScale( double x, double y )
399 {
400 // allow negative ?
401 m_logicalScaleX = x;
402 m_logicalScaleY = y;
403 ComputeScaleAndOrigin();
404 };
405
406 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
407 {
408 m_logicalOriginX = x * m_signX; // is this still correct ?
409 m_logicalOriginY = y * m_signY;
410 ComputeScaleAndOrigin();
411 };
412
413 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
414 {
415 ComputeScaleAndOrigin();
416 };
417
418 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
419 {
420 m_signX = (xLeftRight ? 1 : -1);
421 m_signY = (yBottomUp ? -1 : 1);
422 ComputeScaleAndOrigin();
423 };
424
425 void wxDC::ComputeScaleAndOrigin(void)
426 {
427 // CMB: copy scale to see if it changes
428 double origScaleX = m_scaleX;
429 double origScaleY = m_scaleY;
430
431 m_scaleX = m_logicalScaleX * m_userScaleX;
432 m_scaleY = m_logicalScaleY * m_userScaleY;
433
434 // CMB: if scale has changed call SetPen to recalulate the line width
435 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
436 {
437 // this is a bit artificial, but we need to force wxDC to think
438 // the pen has changed
439 wxPen* pen = & GetPen();
440 wxPen tempPen;
441 m_pen = tempPen;
442 SetPen(* pen);
443 }
444 };
445