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