]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/dc.mm
Remove check for other wxPaintDCs. It should be done in wxWindow instead.
[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
891d0563
DE
15#import <AppKit/NSBezierPath.h>
16#import <AppKit/NSTextStorage.h>
17#import <AppKit/NSLayoutManager.h>
18#import <AppKit/NSTextContainer.h>
19#import <AppKit/NSGraphicsContext.h>
20#import <AppKit/NSAffineTransform.h>
21#import <AppKit/NSColor.h>
22#import <AppKit/NSTypeSetter.h>
23
a24aff65 24IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
891d0563
DE
25wxDC *wxDC::sm_focusedDC = NULL;
26WX_NSTextStorage wxDC::sm_cocoaNSTextStorage = nil;
27WX_NSLayoutManager wxDC::sm_cocoaNSLayoutManager = nil;
28WX_NSTextContainer wxDC::sm_cocoaNSTextContainer = nil;
29
30void wxDC::CocoaInitializeTextSystem()
31{
32 wxASSERT_MSG(!sm_cocoaNSTextStorage && !sm_cocoaNSLayoutManager && !sm_cocoaNSTextContainer,"Text system already initalized! BAD PROGRAMMER!");
33
34 sm_cocoaNSTextStorage = [[NSTextStorage alloc] init];
35
36 sm_cocoaNSLayoutManager = [[NSLayoutManager alloc] init];
37 [sm_cocoaNSTextStorage addLayoutManager:sm_cocoaNSLayoutManager];
38 // NSTextStorage retains NSLayoutManager, but so do we
39 // [sm_cocoaNSLayoutManager release]; [sm_cocoaNSLayoutManager retain];
40
41 // NOTE: initWithContainerSize is the designated initializer, but the
42 // Apple CircleView sample gets away with just calling init, which
43 // is all we really need for our purposes.
44 sm_cocoaNSTextContainer = [[NSTextContainer alloc] init];
45 [sm_cocoaNSLayoutManager addTextContainer:sm_cocoaNSTextContainer];
46 // NSLayoutManager retains NSTextContainer, but so do we
47 // [sm_cocoaNSTextContainer release]; [sm_cocoaNSTextContainer retain];
48}
49
50void wxDC::CocoaShutdownTextSystem()
51{
52 [sm_cocoaNSTextContainer release]; sm_cocoaNSTextContainer = nil;
53 [sm_cocoaNSLayoutManager release]; sm_cocoaNSLayoutManager = nil;
54 [sm_cocoaNSTextStorage release]; sm_cocoaNSTextStorage = nil;
55}
56
57wxDC::wxDC(void)
58{
59}
60
61wxDC::~wxDC(void)
62{
63}
64
65void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
66{
c3b0c2c3
DE
67 NSGraphicsContext *context = [NSGraphicsContext currentContext];
68 [context saveGraphicsState];
69
891d0563 70 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(x,y,width,height)];
c3b0c2c3 71 [m_textForegroundColour.GetNSColor() set];
891d0563 72 [bezpath stroke];
c3b0c2c3
DE
73 [m_brush.GetNSColor() set];
74 [bezpath fill];
75
76 [context restoreGraphicsState];
891d0563
DE
77}
78
79void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
80{
c3b0c2c3
DE
81 NSGraphicsContext *context = [NSGraphicsContext currentContext];
82 [context saveGraphicsState];
83
891d0563
DE
84 NSBezierPath *bezpath = [NSBezierPath bezierPath];
85 [bezpath moveToPoint:NSMakePoint(x1,y1)];
86 [bezpath lineToPoint:NSMakePoint(x2,y2)];
c3b0c2c3
DE
87
88 [m_textForegroundColour.GetNSColor() set];
891d0563 89 [bezpath stroke];
c3b0c2c3
DE
90
91 [context restoreGraphicsState];
891d0563
DE
92}
93
94void wxDC::DoGetTextExtent(const wxString& text, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *theFont) const
95{
96// FIXME: Cache this so it can be used for DoDrawText
97 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
98 NSAttributedString *attributedString = [[NSAttributedString alloc]
99 initWithString:[NSString stringWithCString:text.c_str()]];
100 [sm_cocoaNSTextStorage setAttributedString:attributedString];
101 [attributedString release];
102
103 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
104 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
105 if(x)
dc483f61 106 *x=(int)usedRect.size.width;
891d0563 107 if(y)
dc483f61 108 *y=(int)usedRect.size.height;
891d0563
DE
109 if(descent)
110 *descent=0;
111 if(externalLeading)
112 *externalLeading=0;
113}
114
115void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
116{
117 wxASSERT_MSG(sm_cocoaNSTextStorage && sm_cocoaNSLayoutManager && sm_cocoaNSTextContainer, "Text system has not been initialized. BAD PROGRAMMER!");
118 NSAttributedString *attributedString = [[NSAttributedString alloc]
119 initWithString:[NSString stringWithCString:text.c_str()]];
120 [sm_cocoaNSTextStorage setAttributedString:attributedString];
121 [attributedString release];
122
123 NSRange glyphRange = [sm_cocoaNSLayoutManager glyphRangeForTextContainer:sm_cocoaNSTextContainer];
124 NSRect usedRect = [sm_cocoaNSLayoutManager usedRectForTextContainer:sm_cocoaNSTextContainer];
13fc3db4
DE
125 // NOTE: We'll crash trying to get the location of glyphAtIndex:0 if
126 // there is no length or we don't start at zero
127 if(!glyphRange.length)
128 return;
129 wxASSERT_MSG(glyphRange.location==0,"glyphRange must begin at zero");
891d0563
DE
130
131 NSAffineTransform *transform = [NSAffineTransform transform];
132 [transform translateXBy:x yBy:y];
133
134 NSAffineTransform *flipTransform = [NSAffineTransform transform];
135 /* x' = 1x + 0y + 0
136 y' = 0x + -1y + window's height
137 */
138 NSAffineTransformStruct matrix = {
139 1, 0
140 , 0, -1
141 , 0, usedRect.size.height
142 };
143 [flipTransform setTransformStruct: matrix];
144
145 NSGraphicsContext *context = [NSGraphicsContext currentContext];
146 [context saveGraphicsState];
147 [transform concat];
148 [flipTransform concat];
c3b0c2c3 149 #if 0
891d0563
DE
150 // Draw+fill a rectangle so we can see where the shit is supposed to be.
151 wxLogDebug("(%f,%f) (%fx%f)",usedRect.origin.x,usedRect.origin.y,usedRect.size.width,usedRect.size.height);
152 NSBezierPath *bezpath = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,usedRect.size.width,usedRect.size.height)];
153 [[NSColor blackColor] set];
154 [bezpath stroke];
155 [[NSColor blueColor] set];
156 [bezpath fill];
c3b0c2c3 157 #endif
891d0563
DE
158
159 NSPoint layoutLocation = [sm_cocoaNSLayoutManager locationForGlyphAtIndex:0];
160 layoutLocation.x = 0.0;
161 layoutLocation.y *= -1.0;
162 layoutLocation.y += [[sm_cocoaNSLayoutManager typesetter] baselineOffsetInLayoutManager:sm_cocoaNSLayoutManager glyphIndex:0];
163 // NOTE: That's NSMakePoint, not NSMakePint (working on that though)
c3b0c2c3 164 [m_textForegroundColour.GetNSColor() set];
891d0563
DE
165 [sm_cocoaNSLayoutManager drawGlyphsForGlyphRange:glyphRange atPoint:layoutLocation];
166
167 [context restoreGraphicsState];
168}
169
2894667f
DE
170// wxDCBase functions
171int wxDCBase::DeviceToLogicalX(int x) const
172{
173 return x;
174}
175
176int wxDCBase::DeviceToLogicalY(int y) const
177{
178 return y;
179}
180
181int wxDCBase::LogicalToDeviceX(int x) const
182{
183 return x;
184}
185
186int wxDCBase::LogicalToDeviceY(int y) const
187{
188 return y;
189}
190
891d0563
DE
191///////////////////////////////////////////////////////////////////////////
192// cut here, the rest is stubs
193///////////////////////////////////////////////////////////////////////////
a24aff65
DE
194
195//-----------------------------------------------------------------------------
196// constants
197//-----------------------------------------------------------------------------
198
199#define mm2inches 0.0393700787402
200#define inches2mm 25.4
201#define mm2twips 56.6929133859
202#define twips2mm 0.0176388888889
203#define mm2pt 2.83464566929
204#define pt2mm 0.352777777778
205
206//-----------------------------------------------------------------------------
207// wxDC
208//-----------------------------------------------------------------------------
209
a24aff65
DE
210void wxDC::DoDrawIcon( const wxIcon &WXUNUSED(icon), int WXUNUSED(x), int WXUNUSED(y) )
211{
212};
213
214void wxDC::DoDrawPoint( int x, int y )
215{
216};
217
218void wxDC::DoDrawPolygon( int, wxPoint *, int, int, int)
219{
220};
221
222void wxDC::DoDrawLines( int, wxPoint *, int, int )
223{
224}
225
a24aff65
DE
226int wxDC::GetDepth() const
227{
228 return 0;
229}
230
231wxSize wxDC::GetPPI() const
232{
233 return wxSize(0,0);
234}
235
236bool wxDC::CanGetTextExtent() const
237{
238 return false;
239}
240
a24aff65
DE
241wxCoord wxDC::GetCharHeight() const
242{
243 return 0;
244}
245
246wxCoord wxDC::GetCharWidth() const
247{
248 return 0;
249}
250
251bool wxDC::CanDrawBitmap() const
252{
253 return false;
254}
255
256bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
257{
258 return false;
259}
260
261void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
262{
263}
264
265void wxDC::SetPen(const wxPen& pen)
266{
267}
268
269void wxDC::SetBrush(const wxBrush& brush)
270{
c3b0c2c3 271 m_brush = brush;
a24aff65
DE
272}
273
274void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
275{
276}
277
278void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
279{
280}
281
282void wxDC::DestroyClippingRegion()
283{
284}
285
286void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
287{
288}
289
290void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
291{
292}
293
294void wxDC::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h, double sa, double ea)
295{
296}
297
a24aff65
DE
298void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
299{
300}
301
302void wxDC::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
303{
304}
305
306bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
307{
308 return false;
309}
310
311void wxDC::DoCrossHair(wxCoord x, wxCoord y)
312{
313}
314
a24aff65
DE
315
316bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop, bool useMask , wxCoord xsrcMask, wxCoord ysrcMask)
317{
318 return false;
319}
320
321void wxDC::DoGetSize( int* width, int* height ) const
322{
323 *width = m_maxX-m_minX;
324 *height = m_maxY-m_minY;
325};
326
327void wxDC::DoGetSizeMM( int* width, int* height ) const
328{
329 int w = 0;
330 int h = 0;
331 GetSize( &w, &h );
332};
333
334void wxDC::SetTextForeground( const wxColour &col )
335{
336 if (!Ok()) return;
337 m_textForegroundColour = col;
338};
339
340void wxDC::SetTextBackground( const wxColour &col )
341{
342 if (!Ok()) return;
343 m_textBackgroundColour = col;
344};
345
346void wxDC::Clear()
347{
348}
349
350void wxDC::SetBackground(const wxBrush&)
351{
352}
353
354void wxDC::SetPalette(const wxPalette&)
355{
356}
357
358void wxDC::SetLogicalFunction(int)
359{
360}
361
362
363void wxDC::SetMapMode( int mode )
364{
365 switch (mode)
366 {
367 case wxMM_TWIPS:
368 break;
369 case wxMM_POINTS:
370 break;
371 case wxMM_METRIC:
372 break;
373 case wxMM_LOMETRIC:
374 break;
375 default:
376 case wxMM_TEXT:
377 SetLogicalScale( 1.0, 1.0 );
378 break;
379 };
380 if (mode != wxMM_TEXT)
381 {
382 };
383};
384
385void wxDC::SetUserScale( double x, double y )
386{
387 // allow negative ? -> no
388 m_userScaleX = x;
389 m_userScaleY = y;
390 ComputeScaleAndOrigin();
391};
392
393void wxDC::SetLogicalScale( double x, double y )
394{
395 // allow negative ?
396 m_logicalScaleX = x;
397 m_logicalScaleY = y;
398 ComputeScaleAndOrigin();
399};
400
401void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
402{
403 m_logicalOriginX = x * m_signX; // is this still correct ?
404 m_logicalOriginY = y * m_signY;
405 ComputeScaleAndOrigin();
406};
407
408void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
409{
410 ComputeScaleAndOrigin();
411};
412
413void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
414{
415 m_signX = (xLeftRight ? 1 : -1);
416 m_signY = (yBottomUp ? -1 : 1);
417 ComputeScaleAndOrigin();
418};
419
420void wxDC::ComputeScaleAndOrigin(void)
421{
422 // CMB: copy scale to see if it changes
423 double origScaleX = m_scaleX;
424 double origScaleY = m_scaleY;
425
426 m_scaleX = m_logicalScaleX * m_userScaleX;
427 m_scaleY = m_logicalScaleY * m_userScaleY;
428
429 // CMB: if scale has changed call SetPen to recalulate the line width
430 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
431 {
432 // this is a bit artificial, but we need to force wxDC to think
433 // the pen has changed
434 wxPen* pen = & GetPen();
435 wxPen tempPen;
436 m_pen = tempPen;
437 SetPen(* pen);
438 }
439};
440