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