]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dc.mm
Observe NSControlTintDidChangeNotification in the application delegate.
[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_cocoaFlipped = false;
144 m_cocoaHeight = 0.0;
145 m_pen = *wxBLACK_PEN;
146 }
147
148 wxDC::~wxDC(void)
149 {
150 }
151
152 bool wxDC::CocoaLockFocus()
153 {
154 return false;
155 }
156
157 bool wxDC::CocoaUnlockFocus()
158 {
159 return false;
160 }
161
162 void wxDC::CocoaApplyTransformations()
163 {
164 // This transform flips the graphics since wxDC uses top-left origin
165 if(!m_cocoaFlipped)
166 {
167 // The transform is auto released
168 NSAffineTransform *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, m_cocoaHeight
176 };
177 [transform setTransformStruct: matrix];
178 // Apply the transform
179 [transform concat];
180 }
181 // TODO: Apply scaling transformation
182 }
183
184 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
185 {
186 wxAutoNSAutoreleasePool pool;
187 if(!CocoaTakeFocus()) return;
188 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
189 CocoaSetPenForNSBezierPath(m_pen,bezpath);
190 [bezpath stroke];
191 [m_brush.GetNSColor() set];
192 [bezpath fill];
193 }
194
195 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
196 {
197 wxAutoNSAutoreleasePool pool;
198 if(!CocoaTakeFocus()) return;
199 NSBezierPath *bezpath = [NSBezierPath bezierPath];
200 [bezpath moveToPoint:NSMakePoint(x1,y1)];
201 [bezpath lineToPoint:NSMakePoint(x2,y2)];
202
203 CocoaSetPenForNSBezierPath(m_pen,bezpath);
204 [bezpath stroke];
205 }
206
207 void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
208 {
209 wxAutoNSAutoreleasePool pool;
210 // FIXME: Cache this so it can be used for DoDrawText
211 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
212 NSAttributedString *attributedString = [[NSAttributedString alloc]
213 initWithString:wxNSStringWithWxString(text.c_str())];
214 [sm_cocoaNSTextStorage setAttributedString:attributedString];
215 [attributedString release];
216
217 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
218 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
219 if(x)
220 *x=(int)usedRect.size.width;
221 if(y)
222 *y=(int)usedRect.size.height;
223 if(descent)
224 *descent=0;
225 if(externalLeading)
226 *externalLeading=0;
227 }
228
229 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
230 {
231 wxAutoNSAutoreleasePool pool;
232 if(!CocoaTakeFocus()) return;
233 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, wxT("Text system has not been initialized. BAD PROGRAMMER!"));
234 NSAttributedString *attributedString = [[NSAttributedString alloc]
235 initWithString:wxNSStringWithWxString(text.c_str())];
236 [sm_cocoaNSTextStorage setAttributedString:attributedString];
237 [attributedString release];
238
239 // Set the color (and later font) attributes
240 NSColor *fgColor = m_textForegroundColour.GetNSColor();
241 NSColor *bgColor = m_textBackgroundColour.GetNSColor();
242 if(!fgColor)
243 fgColor = [NSColor clearColor];
244 if(!bgColor)
245 bgColor = [NSColor clearColor];
246 NSDictionary *attrDict = [[NSDictionary alloc] initWithObjectsAndKeys:
247 fgColor, NSForegroundColorAttributeName,
248 bgColor, NSBackgroundColorAttributeName,
249 nil];
250 [sm_cocoaNSTextStorage addAttributes: attrDict range:NSMakeRange(0,[sm_cocoaNSTextStorage length])];
251 [attrDict release];
252
253 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
254 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
255 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
256 // there is no length or we don't start at zero
257 if(!glyphRange.length)
258 return;
259 wxASSERT_MSG(glyphRange.location==0,wxT("glyphRange must begin at zero"));
260
261 NSAffineTransform *transform = [NSAffineTransform transform];
262 [transform translateXBy:x yBy:y];
263
264 NSAffineTransform *flipTransform = [NSAffineTransform transform];
265 /* x' = 1x + 0y + 0
266 y' = 0x + -1y + window's height
267 */
268 NSAffineTransformStruct matrix = {
269 1, 0
270 , 0, -1
271 , 0, usedRect.size.height
272 };
273 [flipTransform setTransformStruct: matrix];
274
275 NSGraphicsContext *context = [NSGraphicsContext currentContext];
276 [context saveGraphicsState];
277 [transform concat];
278 [flipTransform concat];
279 #if 0
280 // Draw+fill a rectangle so we can see where the shit is supposed to be.
281 wxLogTrace(wxTRACE_COCOA,wxT("(%f,%f) (%fx%f)"),usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
282 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
283 [[NSColor blackColor] set];
284 [bezpath stroke];
285 [[NSColor blueColor] set];
286 [bezpath fill];
287 #endif
288
289 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
290 layoutLocation.x = 0.0;
291 layoutLocation.y *= -1.0;
292 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
293 if(m_backgroundMode==wxSOLID)
294 [sm_cocoaNSLayoutManager drawBackgroundForGlyphRange:glyphRange atPoint:NSZeroPoint];
295 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
296
297 [context restoreGraphicsState];
298 }
299
300 // wxDCBase functions
301 int wxDCBase::DeviceToLogicalX(int x) const
302 {
303 return x;
304 }
305
306 int wxDCBase::DeviceToLogicalY(int y) const
307 {
308 return y;
309 }
310
311 int wxDCBase::DeviceToLogicalXRel(int x) const
312 {
313 return x;
314 }
315
316 int wxDCBase::DeviceToLogicalYRel(int y) const
317 {
318 return y;
319 }
320
321 int wxDCBase::LogicalToDeviceX(int x) const
322 {
323 return x;
324 }
325
326 int wxDCBase::LogicalToDeviceY(int y) const
327 {
328 return y;
329 }
330
331 int wxDCBase::LogicalToDeviceXRel(int x) const
332 {
333 return x;
334 }
335
336 int wxDCBase::LogicalToDeviceYRel(int y) const
337 {
338 return y;
339 }
340
341 ///////////////////////////////////////////////////////////////////////////
342 // cut here, the rest is stubs
343 ///////////////////////////////////////////////////////////////////////////
344
345 //-----------------------------------------------------------------------------
346 // constants
347 //-----------------------------------------------------------------------------
348
349 #define mm2inches 0.0393700787402
350 #define inches2mm 25.4
351 #define mm2twips 56.6929133859
352 #define twips2mm 0.0176388888889
353 #define mm2pt 2.83464566929
354 #define pt2mm 0.352777777778
355
356 //-----------------------------------------------------------------------------
357 // wxDC
358 //-----------------------------------------------------------------------------
359
360 void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
361 {
362 };
363
364 void wxDC::DoDrawPoint( int x, int y )
365 {
366 };
367
368 void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
369 {
370 };
371
372 void wxDC::DoDrawLines( int, wxPoint *, int, int )
373 {
374 }
375
376 int wxDC::GetDepth() const
377 {
378 return 0;
379 }
380
381 wxSize wxDC::GetPPI() const
382 {
383 return wxSize(0,0);
384 }
385
386 bool wxDC::CanGetTextExtent() const
387 {
388 return false;
389 }
390
391 wxCoord wxDC::GetCharHeight() const
392 {
393 return 0;
394 }
395
396 wxCoord wxDC::GetCharWidth() const
397 {
398 return 0;
399 }
400
401 bool wxDC::CanDrawBitmap() const
402 {
403 return true;
404 }
405
406 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
407 {
408 return false;
409 }
410
411 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
412 {
413 }
414
415 void wxDC::SetPen(const wxPen& pen)
416 {
417 m_pen = pen;
418 }
419
420 void wxDC::SetBrush(const wxBrush& brush)
421 {
422 m_brush = brush;
423 }
424
425 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
426 {
427 }
428
429 void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
430 {
431 }
432
433 void wxDC::DestroyClippingRegion()
434 {
435 }
436
437 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
438 {
439 }
440
441 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
442 {
443 }
444
445 void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
446 {
447 }
448
449 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
450 {
451 }
452
453 void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
454 {
455 wxAutoNSAutoreleasePool pool;
456 if(!CocoaTakeFocus()) return;
457 if(!bmp.Ok())
458 return;
459
460 #if 0
461 // Draw a rect so we can see where it's supposed to be
462 wxLogTrace(wxTRACE_COCOA,wxT("image at (%d,%d) size %dx%d"),x,y,bmp.GetWidth(),bmp.GetHeight());
463 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,bmp.GetWidth(),bmp.GetHeight())];
464 [[NSColor blackColor] set];
465 [bezpath stroke];
466 [[NSColor blueColor] set];
467 [bezpath fill];
468 #endif // 0
469
470 NSAffineTransform *transform = [NSAffineTransform transform];
471 [transform translateXBy:x yBy:y];
472
473 NSAffineTransform *flipTransform = [NSAffineTransform transform];
474 /* x' = 1x + 0y + 0
475 y' = 0x + -1y + window's height
476 */
477 NSAffineTransformStruct matrix = {
478 1, 0
479 , 0, -1
480 , 0, bmp.GetHeight()
481 };
482 [flipTransform setTransformStruct: matrix];
483
484 NSGraphicsContext *context = [NSGraphicsContext currentContext];
485 [context saveGraphicsState];
486 [transform concat];
487 [flipTransform concat];
488
489 NSImage *nsimage = [bmp.GetNSImage(useMask) retain];
490
491 [nsimage drawAtPoint: NSMakePoint(0,0)
492 fromRect: NSMakeRect(0.0,0.0,bmp.GetWidth(),bmp.GetHeight())
493 operation: NSCompositeSourceOver
494 fraction: 1.0];
495
496 [nsimage release];
497 [context restoreGraphicsState];
498 }
499
500 bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
501 {
502 return false;
503 }
504
505 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
506 {
507 }
508
509
510 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)
511 {
512 if(!CocoaTakeFocus()) return false;
513 if(!source) return false;
514 return source->CocoaDoBlitOnFocusedDC(xdest,ydest,width,height,
515 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
516 }
517
518 bool wxDC::CocoaDoBlitOnFocusedDC(wxCoord xdest, wxCoord ydest,
519 wxCoord width, wxCoord height, wxCoord xsrc, wxCoord ysrc,
520 int logicalFunc, bool useMask, wxCoord xsrcMask, wxCoord ysrcMask)
521 {
522 return false;
523 }
524
525 void wxDC::DoGetSize( int* width, int* height ) const
526 {
527 *width = m_maxX-m_minX;
528 *height = m_maxY-m_minY;
529 };
530
531 void wxDC::DoGetSizeMM( int* width, int* height ) const
532 {
533 int w = 0;
534 int h = 0;
535 GetSize( &w, &h );
536 };
537
538 void wxDC::SetTextForeground( const wxColour &col )
539 {
540 if (!Ok()) return;
541 m_textForegroundColour = col;
542 };
543
544 void wxDC::SetTextBackground( const wxColour &col )
545 {
546 if (!Ok()) return;
547 m_textBackgroundColour = col;
548 };
549
550 void wxDC::Clear()
551 {
552 }
553
554 void wxDC::SetBackground(const wxBrush& brush)
555 {
556 m_backgroundBrush = brush;
557 }
558
559 void wxDC::SetPalette(const wxPalette&)
560 {
561 }
562
563 void wxDC::SetLogicalFunction(int)
564 {
565 }
566
567
568 void wxDC::SetMapMode( int mode )
569 {
570 switch (mode)
571 {
572 case wxMM_TWIPS:
573 break;
574 case wxMM_POINTS:
575 break;
576 case wxMM_METRIC:
577 break;
578 case wxMM_LOMETRIC:
579 break;
580 default:
581 case wxMM_TEXT:
582 SetLogicalScale( 1.0, 1.0 );
583 break;
584 };
585 if (mode != wxMM_TEXT)
586 {
587 };
588 };
589
590 void wxDC::SetUserScale( double x, double y )
591 {
592 // allow negative ? -> no
593 m_userScaleX = x;
594 m_userScaleY = y;
595 ComputeScaleAndOrigin();
596 };
597
598 void wxDC::SetLogicalScale( double x, double y )
599 {
600 // allow negative ?
601 m_logicalScaleX = x;
602 m_logicalScaleY = y;
603 ComputeScaleAndOrigin();
604 };
605
606 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
607 {
608 m_logicalOriginX = x * m_signX; // is this still correct ?
609 m_logicalOriginY = y * m_signY;
610 ComputeScaleAndOrigin();
611 };
612
613 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
614 {
615 ComputeScaleAndOrigin();
616 };
617
618 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
619 {
620 m_signX = (xLeftRight ? 1 : -1);
621 m_signY = (yBottomUp ? -1 : 1);
622 ComputeScaleAndOrigin();
623 };
624
625 void wxDC::ComputeScaleAndOrigin(void)
626 {
627 // CMB: copy scale to see if it changes
628 double origScaleX = m_scaleX;
629 double origScaleY = m_scaleY;
630
631 m_scaleX = m_logicalScaleX * m_userScaleX;
632 m_scaleY = m_logicalScaleY * m_userScaleY;
633
634 // CMB: if scale has changed call SetPen to recalulate the line width
635 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
636 {
637 // this is a bit artificial, but we need to force wxDC to think
638 // the pen has changed
639 wxPen* pen = & GetPen();
640 wxPen tempPen;
641 m_pen = tempPen;
642 SetPen(* pen);
643 }
644 };
645