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