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