]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dc.mm
da2d539def056d9822a5a940d008e7dc3a0f9c74
[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: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/dc.h"
16 #endif //WX_PRECOMP
17
18 #include "wx/cocoa/autorelease.h"
19 #include "wx/cocoa/string.h"
20
21 #import <AppKit/NSBezierPath.h>
22 #import <AppKit/NSTextStorage.h>
23 #import <AppKit/NSLayoutManager.h>
24 #import <AppKit/NSTextContainer.h>
25 #import <AppKit/NSGraphicsContext.h>
26 #import <AppKit/NSAffineTransform.h>
27 #import <AppKit/NSColor.h>
28 #import <AppKit/NSTypesetter.h>
29 #import <AppKit/NSImage.h>
30
31 #include "wx/listimpl.cpp"
32 WX_DEFINE_LIST(wxCocoaDCStack);
33
34 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
35 WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
36 WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
37 WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
38 wxCocoaDCStack wxDC::sm_cocoaDCStack;
39
40 inline void CocoaSetPenForNSBezierPath(wxPen &pen, NSBezierPath *bezpath)
41 {
42 [pen.GetNSColor() set];
43 const float *pattern;
44 [bezpath setLineDash:pattern count:pen.GetCocoaLineDash(&pattern) phase:0.0];
45 [bezpath setLineWidth:pen.GetWidth()];
46 switch(pen.GetJoin())
47 {
48 case wxJOIN_BEVEL:
49 [bezpath setLineJoinStyle:NSBevelLineJoinStyle];
50 break;
51 case wxJOIN_ROUND:
52 [bezpath setLineJoinStyle:NSRoundLineJoinStyle];
53 break;
54 case wxJOIN_MITER:
55 [bezpath setLineJoinStyle:NSMiterLineJoinStyle];
56 break;
57 }
58 switch(pen.GetCap())
59 {
60 case wxCAP_ROUND:
61 [bezpath setLineCapStyle:NSRoundLineCapStyle];
62 break;
63 case wxCAP_PROJECTING:
64 [bezpath setLineCapStyle:NSSquareLineCapStyle];
65 break;
66 case wxCAP_BUTT:
67 [bezpath setLineCapStyle:NSButtLineCapStyle];
68 break;
69 }
70 }
71
72 void wxDC::CocoaInitializeTextSystem()
73 {
74 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,wxT("Text system already initalized! BAD PROGRAMMER!"));
75
76 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
77
78 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
79 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
80 // NSTextStorage retains NSLayoutManager, but so do we
81 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
82
83 // NOTE: initWithContainerSize is the designated initializer, but the
84 // Apple CircleView sample gets away with just calling init, which
85 // is all we really need for our purposes.
86 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
87 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
88 // NSLayoutManager retains NSTextContainer, but so do we
89 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
90 }
91
92 void wxDC::CocoaShutdownTextSystem()
93 {
94 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
95 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
96 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
97 }
98
99 void wxDC::CocoaUnwindStackAndLoseFocus()
100 {
101 wxCocoaDCStack::compatibility_iterator ourNode=sm_cocoaDCStack.Find(this);
102 if(ourNode)
103 {
104 wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst();
105 for(;node!=ourNode; node=sm_cocoaDCStack.GetFirst())
106 {
107 wxDC *dc = node->GetData();
108 wxASSERT(dc);
109 wxASSERT(dc!=this);
110 if(!dc->CocoaUnlockFocus())
111 {
112 wxFAIL_MSG(wxT("Unable to unlock focus on higher-level DC!"));
113 }
114 sm_cocoaDCStack.Erase(node);
115 }
116 wxASSERT(node==ourNode);
117 wxASSERT(ourNode->GetData() == this);
118 ourNode->GetData()->CocoaUnlockFocus();
119 sm_cocoaDCStack.Erase(ourNode);
120 }
121 }
122
123 bool wxDC::CocoaUnwindStackAndTakeFocus()
124 {
125 wxCocoaDCStack::compatibility_iterator node=sm_cocoaDCStack.GetFirst();
126 for(;node;node = sm_cocoaDCStack.GetFirst())
127 {
128 wxDC *dc = node->GetData();
129 wxASSERT(dc);
130 // If we're on the stack, then it's unwound enough and we have focus
131 if(dc==this)
132 return true;
133 // If unable to unlockFocus (e.g. wxPaintDC) stop here
134 if(!dc->CocoaUnlockFocus())
135 break;
136 sm_cocoaDCStack.Erase(node);
137 }
138 return CocoaLockFocus();
139 }
140
141 wxDC::wxDC(void)
142 {
143 m_cocoaWxToBoundsTransform = nil;
144 m_pen = *wxBLACK_PEN;
145 }
146
147 wxDC::~wxDC(void)
148 {
149 }
150
151 bool wxDC::CocoaLockFocus()
152 {
153 return false;
154 }
155
156 bool wxDC::CocoaUnlockFocus()
157 {
158 return false;
159 }
160
161 /*static*/ WX_NSAffineTransform wxDC::CocoaGetWxToBoundsTransform(bool isFlipped, float height)
162 {
163 NSAffineTransform *transform = nil;
164 // This transform flips the graphics since wxDC uses top-left origin
165 if(!isFlipped)
166 {
167 // The transform is auto released
168 transform = [NSAffineTransform transform];
169 /* x' = 1x + 0y + 0
170 y' = 0x + -1y + window's height
171 */
172 NSAffineTransformStruct matrix = {
173 1, 0
174 , 0, -1
175 , 0, height
176 };
177 [transform setTransformStruct: matrix];
178 }
179 return transform;
180 }
181
182 void wxDC::CocoaApplyTransformations()
183 {
184 [m_cocoaWxToBoundsTransform concat];
185 // TODO: Apply device/logical/user position/scaling transformations
186 }
187
188 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
189 {
190 wxAutoNSAutoreleasePool pool;
191 if(!CocoaTakeFocus()) return;
192 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
193 CocoaSetPenForNSBezierPath(m_pen,bezpath);
194 [bezpath stroke];
195 [m_brush.GetNSColor() set];
196 [bezpath fill];
197 }
198
199 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
200 {
201 wxAutoNSAutoreleasePool pool;
202 if(!CocoaTakeFocus()) return;
203 NSBezierPath *bezpath = [NSBezierPath bezierPath];
204 [bezpath moveToPoint:NSMakePoint(x1,y1)];
205 [bezpath lineToPoint:NSMakePoint(x2,y2)];
206
207 CocoaSetPenForNSBezierPath(m_pen,bezpath);
208 [bezpath stroke];
209 }
210
211 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
212 {
213 wxAutoNSAutoreleasePool pool;
214 // FIXME: Cache this so it can be used for DoDrawText
215 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
216 NSAttributedString *attributedString = [[NSAttributedString alloc]
217 initWithString:wxNSStringWithWxString(text.c_str())];
218 [sm_cocoaNSTextStorage setAttributedString:attributedString];
219 [attributedString release];
220
221 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
222 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
223 if(x)
224 *x=(int)usedRect.size.width;
225 if(y)
226 *y=(int)usedRect.size.height;
227 if(descent)
228 *descent=0;
229 if(externalLeading)
230 *externalLeading=0;
231 }
232
233 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
234 {
235 wxAutoNSAutoreleasePool pool;
236 if(!CocoaTakeFocus()) return;
237 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
238 NSAttributedString *attributedString = [[NSAttributedString alloc]
239 initWithString:wxNSStringWithWxString(text.c_str())];
240 [sm_cocoaNSTextStorage setAttributedString:attributedString];
241 [attributedString release];
242
243 // Set the color (and later font) attributes
244 NSColor *fgColor = m_textForegroundColour.GetNSColor();
245 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
246 if(!fgColor)
247 fgColor = [NSColor clearColor];
248 if(!bgColor)
249 bgColor = [NSColor clearColor];
250 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
251 fgColor, NSForegroundColorAttributeName,
252 bgColor, NSBackgroundColorAttributeName,
253 nil];
254 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
255 [attrDict release];
256
257 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
258 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
259 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
260 // there is no length or we don't start at zero
261 if(!glyphRange.length)
262 return;
263 wxASSERT_MSG(glyphRange.location==0,wxT("glyphRange must begin at zero"));
264
265 NSAffineTransform *transform = [NSAffineTransform transform];
266 [transform translateXBy:x yBy:y];
267
268 NSAffineTransform *flipTransform = [NSAffineTransform transform];
269 /* x' = 1x + 0y + 0
270 y' = 0x + -1y + window's height
271 */
272 NSAffineTransformStruct matrix = {
273 1, 0
274 , 0, -1
275 , 0, usedRect.size.height
276 };
277 [flipTransform setTransformStruct: matrix];
278
279 NSGraphicsContext *context = [NSGraphicsContext currentContext];
280 [context saveGraphicsState];
281 [transform concat];
282 [flipTransform concat];
283 #if 0
284 // Draw+fill a rectangle so we can see where the shit is supposed to be.
285 wxLogTrace(wxTRACE_COCOA,wxT("(%f,%f) (%fx%f)"),usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
286 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
287 [[NSColor blackColor] set];
288 [bezpath stroke];
289 [[NSColor blueColor] set];
290 [bezpath fill];
291 #endif
292
293 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
294 layoutLocation.x = 0.0;
295 layoutLocation.y *= -1.0;
296 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
297 if(m_backgroundMode==wxSOLID)
298 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
299 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
300
301 [context restoreGraphicsState];
302 }
303
304 // wxDCBase functions
305 int wxDCBase::DeviceToLogicalX(int x) const
306 {
307 return x;
308 }
309
310 int wxDCBase::DeviceToLogicalY(int y) const
311 {
312 return y;
313 }
314
315 int wxDCBase::DeviceToLogicalXRel(int x) const
316 {
317 return x;
318 }
319
320 int wxDCBase::DeviceToLogicalYRel(int y) const
321 {
322 return y;
323 }
324
325 int wxDCBase::LogicalToDeviceX(int x) const
326 {
327 return x;
328 }
329
330 int wxDCBase::LogicalToDeviceY(int y) const
331 {
332 return y;
333 }
334
335 int wxDCBase::LogicalToDeviceXRel(int x) const
336 {
337 return x;
338 }
339
340 int wxDCBase::LogicalToDeviceYRel(int y) const
341 {
342 return y;
343 }
344
345 ///////////////////////////////////////////////////////////////////////////
346 // cut here, the rest is stubs
347 ///////////////////////////////////////////////////////////////////////////
348
349 //-----------------------------------------------------------------------------
350 // constants
351 //-----------------------------------------------------------------------------
352
353 #define mm2inches 0.0393700787402
354 #define inches2mm 25.4
355 #define mm2twips 56.6929133859
356 #define twips2mm 0.0176388888889
357 #define mm2pt 2.83464566929
358 #define pt2mm 0.352777777778
359
360 //-----------------------------------------------------------------------------
361 // wxDC
362 //-----------------------------------------------------------------------------
363
364 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
365 {
366 };
367
368 void wxDC::DoDrawPoint( int x, int y )
369 {
370 };
371
372 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
373 {
374 };
375
376 void wxDC::DoDrawLines( int, wxPoint *, int, int )
377 {
378 }
379
380 int wxDC::GetDepth() const
381 {
382 return 0;
383 }
384
385 wxSize wxDC::GetPPI() const
386 {
387 return wxSize(0,0);
388 }
389
390 bool wxDC::CanGetTextExtent() const
391 {
392 return false;
393 }
394
395 wxCoord wxDC::GetCharHeight() const
396 {
397 return 0;
398 }
399
400 wxCoord wxDC::GetCharWidth() const
401 {
402 return 0;
403 }
404
405 bool wxDC::CanDrawBitmap() const
406 {
407 return true;
408 }
409
410 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
411 {
412 return false;
413 }
414
415 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
416 {
417 }
418
419 void wxDC::SetPen(const wxPen& pen)
420 {
421 m_pen = pen;
422 }
423
424 void wxDC::SetBrush(const wxBrush& brush)
425 {
426 m_brush = brush;
427 }
428
429 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
430 {
431 }
432
433 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
434 {
435 }
436
437 void wxDC::DestroyClippingRegion()
438 {
439 }
440
441 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
442 {
443 }
444
445 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
446 {
447 }
448
449 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
450 {
451 }
452
453 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
454 {
455 }
456
457 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
458 {
459 wxAutoNSAutoreleasePool pool;
460 if(!CocoaTakeFocus()) return;
461 if(!bmp.Ok())
462 return;
463
464 #if 0
465 // Draw a rect so we can see where it's supposed to be
466 wxLogTrace(wxTRACE_COCOA,wxT("image at (%d,%d) size %dx%d"),x,y,bmp.GetWidth(),bmp.GetHeight());
467 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
468 [[NSColor blackColor] set];
469 [bezpath stroke];
470 [[NSColor blueColor] set];
471 [bezpath fill];
472 #endif // 0
473
474 NSAffineTransform *transform = [NSAffineTransform transform];
475 [transform translateXBy:x yBy:y];
476
477 NSAffineTransform *flipTransform = [NSAffineTransform transform];
478 /* x' = 1x + 0y + 0
479 y' = 0x + -1y + window's height
480 */
481 NSAffineTransformStruct matrix = {
482 1, 0
483 , 0, -1
484 , 0, bmp.GetHeight()
485 };
486 [flipTransform setTransformStruct: matrix];
487
488 NSGraphicsContext *context = [NSGraphicsContext currentContext];
489 [context saveGraphicsState];
490 [transform concat];
491 [flipTransform concat];
492
493 NSImage *nsimage = [bmp.GetNSImage(useMask) retain];
494
495 [nsimage drawAtPoint: NSMakePoint(0,0)
496 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
497 operation: NSCompositeSourceOver
498 fraction: 1.0];
499
500 [nsimage release];
501 [context restoreGraphicsState];
502 }
503
504 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
505 {
506 return false;
507 }
508
509 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
510 {
511 }
512
513
514 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)
515 {
516 if(!CocoaTakeFocus()) return false;
517 if(!source) return false;
518 return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height,
519 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
520 }
521
522 bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
523 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
524 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
525 {
526 return false;
527 }
528
529 void wxDC::DoGetSize( int* width, int* height ) const
530 {
531 *width = m_maxX-m_minX;
532 *height = m_maxY-m_minY;
533 };
534
535 void wxDC::DoGetSizeMM( int* width, int* height ) const
536 {
537 int w = 0;
538 int h = 0;
539 GetSize( &w, &h );
540 };
541
542 void wxDC::SetTextForeground( const wxColour &col )
543 {
544 if (!Ok()) return;
545 m_textForegroundColour = col;
546 };
547
548 void wxDC::SetTextBackground( const wxColour &col )
549 {
550 if (!Ok()) return;
551 m_textBackgroundColour = col;
552 };
553
554 void wxDC::Clear()
555 {
556 }
557
558 void wxDC::SetBackground(const wxBrush& brush)
559 {
560 m_backgroundBrush = brush;
561 }
562
563 void wxDC::SetPalette(const wxPalette&)
564 {
565 }
566
567 void wxDC::SetLogicalFunction(int)
568 {
569 }
570
571
572 void wxDC::SetMapMode( int mode )
573 {
574 switch (mode)
575 {
576 case wxMM_TWIPS:
577 break;
578 case wxMM_POINTS:
579 break;
580 case wxMM_METRIC:
581 break;
582 case wxMM_LOMETRIC:
583 break;
584 default:
585 case wxMM_TEXT:
586 SetLogicalScale( 1.0, 1.0 );
587 break;
588 };
589 if (mode != wxMM_TEXT)
590 {
591 };
592 };
593
594 void wxDC::SetUserScale( double x, double y )
595 {
596 // allow negative ? -> no
597 m_userScaleX = x;
598 m_userScaleY = y;
599 ComputeScaleAndOrigin();
600 };
601
602 void wxDC::SetLogicalScale( double x, double y )
603 {
604 // allow negative ?
605 m_logicalScaleX = x;
606 m_logicalScaleY = y;
607 ComputeScaleAndOrigin();
608 };
609
610 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
611 {
612 m_logicalOriginX = x * m_signX; // is this still correct ?
613 m_logicalOriginY = y * m_signY;
614 ComputeScaleAndOrigin();
615 };
616
617 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
618 {
619 ComputeScaleAndOrigin();
620 };
621
622 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
623 {
624 m_signX = (xLeftRight ? 1 : -1);
625 m_signY = (yBottomUp ? -1 : 1);
626 ComputeScaleAndOrigin();
627 };
628
629 void wxDC::ComputeScaleAndOrigin(void)
630 {
631 // CMB: copy scale to see if it changes
632 double origScaleX = m_scaleX;
633 double origScaleY = m_scaleY;
634
635 m_scaleX = m_logicalScaleX * m_userScaleX;
636 m_scaleY = m_logicalScaleY * m_userScaleY;
637
638 // CMB: if scale has changed call SetPen to recalulate the line width
639 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
640 {
641 // this is a bit artificial, but we need to force wxDC to think
642 // the pen has changed
643 const wxPen* pen = & GetPen();
644 wxPen tempPen;
645 m_pen = tempPen;
646 SetPen(* pen);
647 }
648 };
649