PalmOS 6 compilation fixes (patch 1849757)
[wxWidgets.git] / src / common / dcbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dcbase.cpp
3 // Purpose: generic methods of the wxDC Class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 05/25/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/dc.h"
28 #include "wx/dcclient.h"
29 #include "wx/dcmemory.h"
30 #include "wx/dcscreen.h"
31 #include "wx/dcprint.h"
32 #include "wx/prntbase.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/math.h"
36 #include "wx/module.h"
37 #endif
38
39 #ifdef __WXMSW__
40 #include "wx/msw/dcclient.h"
41 #include "wx/msw/dcmemory.h"
42 #include "wx/msw/dcscreen.h"
43 #endif
44
45 #ifdef __WXGTK__
46 #include "wx/gtk/dcclient.h"
47 #include "wx/gtk/dcmemory.h"
48 #include "wx/gtk/dcscreen.h"
49 #endif
50
51 #ifdef __WXMAC__
52 #include "wx/mac/dcclient.h"
53 #include "wx/mac/dcmemory.h"
54 #include "wx/mac/dcscreen.h"
55 #endif
56
57 #ifdef __WXCOCOA__
58 #include "wx/cocoa/dcclient.h"
59 #include "wx/cocoa/dcmemory.h"
60 #include "wx/cocoa/dcscreen.h"
61 #endif
62
63 #ifdef __WXMOTIF__
64 #include "wx/motif/dcclient.h"
65 #include "wx/motif/dcmemory.h"
66 #include "wx/motif/dcscreen.h"
67 #endif
68
69 #ifdef __WXX11__
70 #include "wx/x11/dcclient.h"
71 #include "wx/x11/dcmemory.h"
72 #include "wx/x11/dcscreen.h"
73 #endif
74
75 #ifdef __WXDFB__
76 #include "wx/dfb/dcclient.h"
77 #include "wx/dfb/dcmemory.h"
78 #include "wx/dfb/dcscreen.h"
79 #endif
80
81 #ifdef __WXPALMOS__
82 #include "wx/palmos/dcclient.h"
83 #include "wx/palmos/dcmemory.h"
84 #include "wx/palmos/dcscreen.h"
85 #endif
86
87 //----------------------------------------------------------------------------
88 // wxDCFactory
89 //----------------------------------------------------------------------------
90
91 wxDCFactory *wxDCFactory::m_factory = NULL;
92
93 void wxDCFactory::Set(wxDCFactory *factory)
94 {
95 delete m_factory;
96
97 m_factory = factory;
98 }
99
100 wxDCFactory *wxDCFactory::Get()
101 {
102 if ( !m_factory )
103 m_factory = new wxNativeDCFactory;
104
105 return m_factory;
106 }
107
108 class wxDCFactoryCleanupModule : public wxModule
109 {
110 public:
111 virtual bool OnInit() { return true; }
112 virtual void OnExit() { wxDCFactory::Set(NULL); }
113
114 private:
115 DECLARE_DYNAMIC_CLASS(wxDCFactoryCleanupModule)
116 };
117
118 IMPLEMENT_DYNAMIC_CLASS(wxDCFactoryCleanupModule, wxModule)
119
120 //-----------------------------------------------------------------------------
121 // wxNativeDCFactory
122 //-----------------------------------------------------------------------------
123
124 wxDCImpl* wxNativeDCFactory::CreateWindowDC( wxWindowDC *owner )
125 {
126 return new wxWindowDCImpl( owner );
127 }
128
129 wxDCImpl* wxNativeDCFactory::CreateWindowDC( wxWindowDC *owner, wxWindow *window )
130 {
131 return new wxWindowDCImpl( owner, window );
132 }
133
134 wxDCImpl* wxNativeDCFactory::CreateClientDC( wxClientDC *owner )
135 {
136 return new wxClientDCImpl( owner );
137 }
138
139 wxDCImpl* wxNativeDCFactory::CreateClientDC( wxClientDC *owner, wxWindow *window )
140 {
141 return new wxClientDCImpl( owner, window );
142 }
143
144 wxDCImpl* wxNativeDCFactory::CreatePaintDC( wxPaintDC *owner )
145 {
146 return new wxPaintDCImpl( owner );
147 }
148
149 wxDCImpl* wxNativeDCFactory::CreatePaintDC( wxPaintDC *owner, wxWindow *window )
150 {
151 return new wxPaintDCImpl( owner, window );
152 }
153
154 wxDCImpl* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC *owner )
155 {
156 return new wxMemoryDCImpl( owner );
157 }
158
159 wxDCImpl* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC *owner, wxBitmap &bitmap )
160 {
161 return new wxMemoryDCImpl( owner, bitmap );
162 }
163
164 wxDCImpl* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC *owner, wxDC *dc )
165 {
166 return new wxMemoryDCImpl( owner, dc );
167 }
168
169 wxDCImpl* wxNativeDCFactory::CreateScreenDC( wxScreenDC *owner )
170 {
171 return new wxScreenDCImpl( owner );
172 }
173
174 #if wxUSE_PRINTING_ARCHITECTURE
175 wxDCImpl *wxNativeDCFactory::CreatePrinterDC( wxPrinterDC *owner, const wxPrintData &data )
176 {
177 wxPrintFactory *factory = wxPrintFactory::GetFactory();
178 return factory->CreatePrinterDCImpl( owner, data );
179 }
180 #endif
181
182 //-----------------------------------------------------------------------------
183 // wxWindowDC
184 //-----------------------------------------------------------------------------
185
186 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC, wxDC)
187
188 wxWindowDC::wxWindowDC(wxWindow *win)
189 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win))
190 {
191 }
192
193 //-----------------------------------------------------------------------------
194 // wxClientDC
195 //-----------------------------------------------------------------------------
196
197 IMPLEMENT_ABSTRACT_CLASS(wxClientDC, wxWindowDC)
198
199 wxClientDC::wxClientDC(wxWindow *win)
200 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win))
201 {
202 }
203
204 //-----------------------------------------------------------------------------
205 // wxMemoryDC
206 //-----------------------------------------------------------------------------
207
208 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC)
209
210 wxMemoryDC::wxMemoryDC()
211 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
212 {
213 }
214
215 wxMemoryDC::wxMemoryDC(wxBitmap& bitmap)
216 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap))
217 {
218 }
219
220 wxMemoryDC::wxMemoryDC(wxDC *dc)
221 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc))
222 {
223 }
224
225 void wxMemoryDC::SelectObject(wxBitmap& bmp)
226 {
227 // make sure that the given wxBitmap is not sharing its data with other
228 // wxBitmap instances as its contents will be modified by any drawing
229 // operation done on this DC
230 if (bmp.IsOk())
231 bmp.UnShare();
232
233 GetImpl()->DoSelect(bmp);
234 }
235
236 void wxMemoryDC::SelectObjectAsSource(const wxBitmap& bmp)
237 {
238 GetImpl()->DoSelect(bmp);
239 }
240
241 const wxBitmap& wxMemoryDC::GetSelectedBitmap() const
242 {
243 return GetImpl()->GetSelectedBitmap();
244 }
245
246 wxBitmap& wxMemoryDC::GetSelectedBitmap()
247 {
248 return GetImpl()->GetSelectedBitmap();
249 }
250
251
252 //-----------------------------------------------------------------------------
253 // wxPaintDC
254 //-----------------------------------------------------------------------------
255
256 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC, wxClientDC)
257
258 wxPaintDC::wxPaintDC(wxWindow *win)
259 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win))
260 {
261 }
262
263 //-----------------------------------------------------------------------------
264 // wxScreenDC
265 //-----------------------------------------------------------------------------
266
267 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC, wxWindowDC)
268
269 wxScreenDC::wxScreenDC()
270 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
271 {
272 }
273
274 //-----------------------------------------------------------------------------
275 // wxPrinterDC
276 //-----------------------------------------------------------------------------
277
278 #if wxUSE_PRINTING_ARCHITECTURE
279
280 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC, wxDC)
281
282 wxPrinterDC::wxPrinterDC()
283 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
284 {
285 }
286
287 wxPrinterDC::wxPrinterDC(const wxPrintData& data)
288 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data))
289 {
290 }
291
292 wxRect wxPrinterDC::GetPaperRect()
293 {
294 return GetImpl()->GetPaperRect();
295 }
296
297 int wxPrinterDC::GetResolution()
298 {
299 return GetImpl()->GetResolution();
300 }
301
302 #endif // wxUSE_PRINTING_ARCHITECTURE
303
304 //-----------------------------------------------------------------------------
305 // wxDCImpl
306 //-----------------------------------------------------------------------------
307
308 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl, wxObject)
309
310 wxDCImpl::wxDCImpl( wxDC *owner )
311 : m_window(NULL)
312 , m_colour(wxColourDisplay())
313 , m_ok(true)
314 , m_clipping(false)
315 , m_isInteractive(0)
316 , m_isBBoxValid(false)
317 , m_logicalOriginX(0), m_logicalOriginY(0)
318 , m_deviceOriginX(0), m_deviceOriginY(0)
319 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
320 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
321 , m_userScaleX(1.0), m_userScaleY(1.0)
322 , m_scaleX(1.0), m_scaleY(1.0)
323 , m_signX(1), m_signY(1)
324 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
325 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
326 , m_logicalFunction(wxCOPY)
327 , m_backgroundMode(wxTRANSPARENT)
328 , m_mappingMode(wxMM_TEXT)
329 , m_pen()
330 , m_brush()
331 , m_backgroundBrush(*wxTRANSPARENT_BRUSH)
332 , m_textForegroundColour(*wxBLACK)
333 , m_textBackgroundColour(*wxWHITE)
334 , m_font()
335 #if wxUSE_PALETTE
336 , m_palette()
337 , m_hasCustomPalette(false)
338 #endif // wxUSE_PALETTE
339 {
340 m_owner = owner;
341
342 m_mm_to_pix_x = (double)wxGetDisplaySize().GetWidth() /
343 (double)wxGetDisplaySizeMM().GetWidth();
344 m_mm_to_pix_y = (double)wxGetDisplaySize().GetHeight() /
345 (double)wxGetDisplaySizeMM().GetHeight();
346
347 ResetBoundingBox();
348 ResetClipping();
349 }
350
351 wxDCImpl::~wxDCImpl()
352 {
353 }
354
355 // ----------------------------------------------------------------------------
356 // coordinate conversions and transforms
357 // ----------------------------------------------------------------------------
358
359 wxCoord wxDCImpl::DeviceToLogicalX(wxCoord x) const
360 {
361 return wxRound((double)(x - m_deviceOriginX - m_deviceLocalOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
362 }
363
364 wxCoord wxDCImpl::DeviceToLogicalY(wxCoord y) const
365 {
366 return wxRound((double)(y - m_deviceOriginY - m_deviceLocalOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
367 }
368
369 wxCoord wxDCImpl::DeviceToLogicalXRel(wxCoord x) const
370 {
371 return wxRound((double)(x) / m_scaleX);
372 }
373
374 wxCoord wxDCImpl::DeviceToLogicalYRel(wxCoord y) const
375 {
376 return wxRound((double)(y) / m_scaleY);
377 }
378
379 wxCoord wxDCImpl::LogicalToDeviceX(wxCoord x) const
380 {
381 return wxRound((double)(x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX * m_signY + m_deviceLocalOriginX;
382 }
383
384 wxCoord wxDCImpl::LogicalToDeviceY(wxCoord y) const
385 {
386 return wxRound((double)(y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY * m_signY + m_deviceLocalOriginY;
387 }
388
389 wxCoord wxDCImpl::LogicalToDeviceXRel(wxCoord x) const
390 {
391 return wxRound((double)(x) * m_scaleX);
392 }
393
394 wxCoord wxDCImpl::LogicalToDeviceYRel(wxCoord y) const
395 {
396 return wxRound((double)(y) * m_scaleY);
397 }
398
399 void wxDCImpl::ComputeScaleAndOrigin()
400 {
401 m_scaleX = m_logicalScaleX * m_userScaleX;
402 m_scaleY = m_logicalScaleY * m_userScaleY;
403 }
404
405 void wxDCImpl::SetMapMode( int mode )
406 {
407 switch (mode)
408 {
409 case wxMM_TWIPS:
410 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
411 break;
412 case wxMM_POINTS:
413 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
414 break;
415 case wxMM_METRIC:
416 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
417 break;
418 case wxMM_LOMETRIC:
419 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
420 break;
421 default:
422 case wxMM_TEXT:
423 SetLogicalScale( 1.0, 1.0 );
424 break;
425 }
426 m_mappingMode = mode;
427 }
428
429 void wxDCImpl::SetUserScale( double x, double y )
430 {
431 // allow negative ? -> no
432 m_userScaleX = x;
433 m_userScaleY = y;
434 ComputeScaleAndOrigin();
435 }
436
437 void wxDCImpl::SetLogicalScale( double x, double y )
438 {
439 // allow negative ?
440 m_logicalScaleX = x;
441 m_logicalScaleY = y;
442 ComputeScaleAndOrigin();
443 }
444
445 void wxDCImpl::SetLogicalOrigin( wxCoord x, wxCoord y )
446 {
447 m_logicalOriginX = x * m_signX;
448 m_logicalOriginY = y * m_signY;
449 ComputeScaleAndOrigin();
450 }
451
452 void wxDCImpl::SetDeviceOrigin( wxCoord x, wxCoord y )
453 {
454 m_deviceOriginX = x;
455 m_deviceOriginY = y;
456 ComputeScaleAndOrigin();
457 }
458
459 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x, wxCoord y )
460 {
461 m_deviceLocalOriginX = x;
462 m_deviceLocalOriginY = y;
463 ComputeScaleAndOrigin();
464 }
465
466 void wxDCImpl::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
467 {
468 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
469 // wxWidgets 2.9: no longer override it
470 m_signX = (xLeftRight ? 1 : -1);
471 m_signY = (yBottomUp ? -1 : 1);
472 ComputeScaleAndOrigin();
473 }
474
475
476 // Each element of the widths array will be the width of the string up to and
477 // including the corresponding character in text. This is the generic
478 // implementation, the port-specific classes should do this with native APIs
479 // if available and if faster. Note: pango_layout_index_to_pos is much slower
480 // than calling GetTextExtent!!
481
482 #define FWC_SIZE 256
483
484 class FontWidthCache
485 {
486 public:
487 FontWidthCache() : m_scaleX(1), m_widths(NULL) { }
488 ~FontWidthCache() { delete []m_widths; }
489
490 void Reset()
491 {
492 if (!m_widths)
493 m_widths = new int[FWC_SIZE];
494
495 memset(m_widths, 0, sizeof(int)*FWC_SIZE);
496 }
497
498 wxFont m_font;
499 double m_scaleX;
500 int *m_widths;
501 };
502
503 static FontWidthCache s_fontWidthCache;
504
505 bool wxDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
506 {
507 int totalWidth = 0;
508
509 const size_t len = text.length();
510 widths.Empty();
511 widths.Add(0, len);
512
513 // reset the cache if font or horizontal scale have changed
514 if ( !s_fontWidthCache.m_widths ||
515 !wxIsSameDouble(s_fontWidthCache.m_scaleX, m_scaleX) ||
516 (s_fontWidthCache.m_font != GetFont()) )
517 {
518 s_fontWidthCache.Reset();
519 s_fontWidthCache.m_font = GetFont();
520 s_fontWidthCache.m_scaleX = m_scaleX;
521 }
522
523 // Calculate the position of each character based on the widths of
524 // the previous characters
525 int w, h;
526 for ( size_t i = 0; i < len; i++ )
527 {
528 const wxChar c = text[i];
529 unsigned int c_int = (unsigned int)c;
530
531 if ((c_int < FWC_SIZE) && (s_fontWidthCache.m_widths[c_int] != 0))
532 {
533 w = s_fontWidthCache.m_widths[c_int];
534 }
535 else
536 {
537 DoGetTextExtent(c, &w, &h);
538 if (c_int < FWC_SIZE)
539 s_fontWidthCache.m_widths[c_int] = w;
540 }
541
542 totalWidth += w;
543 widths[i] = totalWidth;
544 }
545
546 return true;
547 }
548
549 void wxDCImpl::GetMultiLineTextExtent(const wxString& text,
550 wxCoord *x,
551 wxCoord *y,
552 wxCoord *h,
553 const wxFont *font) const
554 {
555 wxCoord widthTextMax = 0, widthLine,
556 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
557
558 wxString curLine;
559 for ( wxString::const_iterator pc = text.begin(); ; ++pc )
560 {
561 if ( pc == text.end() || *pc == _T('\n') )
562 {
563 if ( curLine.empty() )
564 {
565 // we can't use GetTextExtent - it will return 0 for both width
566 // and height and an empty line should count in height
567 // calculation
568
569 // assume that this line has the same height as the previous
570 // one
571 if ( !heightLineDefault )
572 heightLineDefault = heightLine;
573
574 if ( !heightLineDefault )
575 {
576 // but we don't know it yet - choose something reasonable
577 DoGetTextExtent(_T("W"), NULL, &heightLineDefault,
578 NULL, NULL, font);
579 }
580
581 heightTextTotal += heightLineDefault;
582 }
583 else
584 {
585 DoGetTextExtent(curLine, &widthLine, &heightLine,
586 NULL, NULL, font);
587 if ( widthLine > widthTextMax )
588 widthTextMax = widthLine;
589 heightTextTotal += heightLine;
590 }
591
592 if ( pc == text.end() )
593 {
594 break;
595 }
596 else // '\n'
597 {
598 curLine.clear();
599 }
600 }
601 else
602 {
603 curLine += *pc;
604 }
605 }
606
607 if ( x )
608 *x = widthTextMax;
609 if ( y )
610 *y = heightTextTotal;
611 if ( h )
612 *h = heightLine;
613 }
614
615 void wxDCImpl::DoDrawCheckMark(wxCoord x1, wxCoord y1,
616 wxCoord width, wxCoord height)
617 {
618 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
619
620 wxCoord x2 = x1 + width,
621 y2 = y1 + height;
622
623 // the pen width is calibrated to give 3 for width == height == 10
624 wxDCPenChanger pen( *m_owner, wxPen(GetTextForeground(), (width + height + 1)/7));
625
626 // we're drawing a scaled version of wx/generic/tick.xpm here
627 wxCoord x3 = x1 + (4*width) / 10, // x of the tick bottom
628 y3 = y1 + height / 2; // y of the left tick branch
629 DoDrawLine(x1, y3, x3, y2);
630 DoDrawLine(x3, y2, x2, y1);
631
632 CalcBoundingBox(x1, y1);
633 CalcBoundingBox(x2, y2);
634 }
635
636 bool
637 wxDCImpl::DoStretchBlit(wxCoord xdest, wxCoord ydest,
638 wxCoord dstWidth, wxCoord dstHeight,
639 wxDC *source,
640 wxCoord xsrc, wxCoord ysrc,
641 wxCoord srcWidth, wxCoord srcHeight,
642 int rop,
643 bool useMask,
644 wxCoord xsrcMask,
645 wxCoord ysrcMask)
646 {
647 wxCHECK_MSG( srcWidth && srcHeight && dstWidth && dstHeight, false,
648 _T("invalid blit size") );
649
650 // emulate the stretching by modifying the DC scale
651 double xscale = (double)srcWidth/dstWidth,
652 yscale = (double)srcHeight/dstHeight;
653
654 double xscaleOld, yscaleOld;
655 GetUserScale(&xscaleOld, &yscaleOld);
656 SetUserScale(xscaleOld/xscale, yscaleOld/yscale);
657
658 bool rc = DoBlit(wxCoord(xdest*xscale), wxCoord(ydest*yscale),
659 wxCoord(dstWidth*xscale), wxCoord(dstHeight*yscale),
660 source,
661 xsrc, ysrc, rop, useMask, xsrcMask, ysrcMask);
662
663 SetUserScale(xscaleOld, yscaleOld);
664
665 return rc;
666 }
667
668 void wxDCImpl::DrawLines(const wxPointList *list, wxCoord xoffset, wxCoord yoffset)
669 {
670 int n = list->GetCount();
671 wxPoint *points = new wxPoint[n];
672
673 int i = 0;
674 for ( wxPointList::compatibility_iterator node = list->GetFirst(); node; node = node->GetNext(), i++ )
675 {
676 wxPoint *point = node->GetData();
677 points[i].x = point->x;
678 points[i].y = point->y;
679 }
680
681 DoDrawLines(n, points, xoffset, yoffset);
682
683 delete [] points;
684 }
685
686 void wxDCImpl::DrawPolygon(const wxPointList *list,
687 wxCoord xoffset, wxCoord yoffset,
688 int fillStyle)
689 {
690 int n = list->GetCount();
691 wxPoint *points = new wxPoint[n];
692
693 int i = 0;
694 for ( wxPointList::compatibility_iterator node = list->GetFirst(); node; node = node->GetNext(), i++ )
695 {
696 wxPoint *point = node->GetData();
697 points[i].x = point->x;
698 points[i].y = point->y;
699 }
700
701 DoDrawPolygon(n, points, xoffset, yoffset, fillStyle);
702
703 delete [] points;
704 }
705
706 void
707 wxDCImpl::DoDrawPolyPolygon(int n,
708 int count[],
709 wxPoint points[],
710 wxCoord xoffset, wxCoord yoffset,
711 int fillStyle)
712 {
713 if ( n == 1 )
714 {
715 DoDrawPolygon(count[0], points, xoffset, yoffset, fillStyle);
716 return;
717 }
718
719 int i, j, lastOfs;
720 wxPoint* pts;
721 wxPen pen;
722
723 for (i = j = lastOfs = 0; i < n; i++)
724 {
725 lastOfs = j;
726 j += count[i];
727 }
728 pts = new wxPoint[j+n-1];
729 for (i = 0; i < j; i++)
730 pts[i] = points[i];
731 for (i = 2; i <= n; i++)
732 {
733 lastOfs -= count[n-i];
734 pts[j++] = pts[lastOfs];
735 }
736
737 pen = GetPen();
738 SetPen(wxPen(*wxBLACK, 0, wxTRANSPARENT));
739 DoDrawPolygon(j, pts, xoffset, yoffset, fillStyle);
740 SetPen(pen);
741 for (i = j = 0; i < n; i++)
742 {
743 DoDrawLines(count[i], pts+j, xoffset, yoffset);
744 j += count[i];
745 }
746 delete[] pts;
747 }
748
749 #if wxUSE_SPLINES
750
751 void wxDCImpl::DoDrawSpline(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord x3, wxCoord y3)
752 {
753 wxPointList point_list;
754
755 wxPoint *point1 = new wxPoint;
756 point1->x = x1; point1->y = y1;
757 point_list.Append( point1 );
758
759 wxPoint *point2 = new wxPoint;
760 point2->x = x2; point2->y = y2;
761 point_list.Append( point2 );
762
763 wxPoint *point3 = new wxPoint;
764 point3->x = x3; point3->y = y3;
765 point_list.Append( point3 );
766
767 DoDrawSpline(&point_list);
768
769 for( wxPointList::compatibility_iterator node = point_list.GetFirst(); node; node = node->GetNext() )
770 {
771 wxPoint *p = node->GetData();
772 delete p;
773 }
774 }
775
776 void wxDCImpl::DoDrawSpline(int n, wxPoint points[])
777 {
778 wxPointList list;
779 for (int i =0; i < n; i++)
780 list.Append( &points[i] );
781
782 DoDrawSpline(&list);
783 }
784
785 // ----------------------------------- spline code ----------------------------------------
786
787 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
788 double a3, double b3, double a4, double b4);
789 void wx_clear_stack();
790 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
791 double *y3, double *x4, double *y4);
792 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
793 double x4, double y4);
794 static bool wx_spline_add_point(double x, double y);
795 static void wx_spline_draw_point_array(wxDC *dc);
796
797 wxPointList wx_spline_point_list;
798
799 #define half(z1, z2) ((z1+z2)/2.0)
800 #define THRESHOLD 5
801
802 /* iterative version */
803
804 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
805 double b4)
806 {
807 register double xmid, ymid;
808 double x1, y1, x2, y2, x3, y3, x4, y4;
809
810 wx_clear_stack();
811 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
812
813 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
814 xmid = (double)half(x2, x3);
815 ymid = (double)half(y2, y3);
816 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
817 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
818 wx_spline_add_point( x1, y1 );
819 wx_spline_add_point( xmid, ymid );
820 } else {
821 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
822 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
823 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
824 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
825 }
826 }
827 }
828
829 /* utilities used by spline drawing routines */
830
831 typedef struct wx_spline_stack_struct {
832 double x1, y1, x2, y2, x3, y3, x4, y4;
833 } Stack;
834
835 #define SPLINE_STACK_DEPTH 20
836 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
837 static Stack *wx_stack_top;
838 static int wx_stack_count;
839
840 void wx_clear_stack()
841 {
842 wx_stack_top = wx_spline_stack;
843 wx_stack_count = 0;
844 }
845
846 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
847 {
848 wx_stack_top->x1 = x1;
849 wx_stack_top->y1 = y1;
850 wx_stack_top->x2 = x2;
851 wx_stack_top->y2 = y2;
852 wx_stack_top->x3 = x3;
853 wx_stack_top->y3 = y3;
854 wx_stack_top->x4 = x4;
855 wx_stack_top->y4 = y4;
856 wx_stack_top++;
857 wx_stack_count++;
858 }
859
860 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
861 double *x3, double *y3, double *x4, double *y4)
862 {
863 if (wx_stack_count == 0)
864 return (0);
865 wx_stack_top--;
866 wx_stack_count--;
867 *x1 = wx_stack_top->x1;
868 *y1 = wx_stack_top->y1;
869 *x2 = wx_stack_top->x2;
870 *y2 = wx_stack_top->y2;
871 *x3 = wx_stack_top->x3;
872 *y3 = wx_stack_top->y3;
873 *x4 = wx_stack_top->x4;
874 *y4 = wx_stack_top->y4;
875 return (1);
876 }
877
878 static bool wx_spline_add_point(double x, double y)
879 {
880 wxPoint *point = new wxPoint( wxRound(x), wxRound(y) );
881 wx_spline_point_list.Append(point );
882 return true;
883 }
884
885 static void wx_spline_draw_point_array(wxDC *dc)
886 {
887 dc->DrawLines(&wx_spline_point_list, 0, 0 );
888 wxPointList::compatibility_iterator node = wx_spline_point_list.GetFirst();
889 while (node)
890 {
891 wxPoint *point = node->GetData();
892 delete point;
893 wx_spline_point_list.Erase(node);
894 node = wx_spline_point_list.GetFirst();
895 }
896 }
897
898 void wxDCImpl::DoDrawSpline( const wxPointList *points )
899 {
900 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
901
902 wxPoint *p;
903 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
904 double x1, y1, x2, y2;
905
906 wxPointList::compatibility_iterator node = points->GetFirst();
907 if (!node)
908 // empty list
909 return;
910
911 p = (wxPoint *)node->GetData();
912
913 x1 = p->x;
914 y1 = p->y;
915
916 node = node->GetNext();
917 p = node->GetData();
918
919 x2 = p->x;
920 y2 = p->y;
921 cx1 = (double)((x1 + x2) / 2);
922 cy1 = (double)((y1 + y2) / 2);
923 cx2 = (double)((cx1 + x2) / 2);
924 cy2 = (double)((cy1 + y2) / 2);
925
926 wx_spline_add_point(x1, y1);
927
928 while ((node = node->GetNext())
929 #if !wxUSE_STL
930 != NULL
931 #endif // !wxUSE_STL
932 )
933 {
934 p = node->GetData();
935 x1 = x2;
936 y1 = y2;
937 x2 = p->x;
938 y2 = p->y;
939 cx4 = (double)(x1 + x2) / 2;
940 cy4 = (double)(y1 + y2) / 2;
941 cx3 = (double)(x1 + cx4) / 2;
942 cy3 = (double)(y1 + cy4) / 2;
943
944 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
945
946 cx1 = cx4;
947 cy1 = cy4;
948 cx2 = (double)(cx1 + x2) / 2;
949 cy2 = (double)(cy1 + y2) / 2;
950 }
951
952 wx_spline_add_point( cx1, cy1 );
953 wx_spline_add_point( x2, y2 );
954
955 wx_spline_draw_point_array( m_owner );
956 }
957
958 #endif // wxUSE_SPLINES
959
960
961
962 void wxDCImpl::DoGradientFillLinear(const wxRect& rect,
963 const wxColour& initialColour,
964 const wxColour& destColour,
965 wxDirection nDirection)
966 {
967 // save old pen
968 wxPen oldPen = m_pen;
969 wxBrush oldBrush = m_brush;
970
971 wxUint8 nR1 = initialColour.Red();
972 wxUint8 nG1 = initialColour.Green();
973 wxUint8 nB1 = initialColour.Blue();
974 wxUint8 nR2 = destColour.Red();
975 wxUint8 nG2 = destColour.Green();
976 wxUint8 nB2 = destColour.Blue();
977 wxUint8 nR, nG, nB;
978
979 if ( nDirection == wxEAST || nDirection == wxWEST )
980 {
981 wxInt32 x = rect.GetWidth();
982 wxInt32 w = x; // width of area to shade
983 wxInt32 xDelta = w/256; // height of one shade bend
984 if (xDelta < 1)
985 xDelta = 1;
986
987 while (x >= xDelta)
988 {
989 x -= xDelta;
990 if (nR1 > nR2)
991 nR = nR1 - (nR1-nR2)*(w-x)/w;
992 else
993 nR = nR1 + (nR2-nR1)*(w-x)/w;
994
995 if (nG1 > nG2)
996 nG = nG1 - (nG1-nG2)*(w-x)/w;
997 else
998 nG = nG1 + (nG2-nG1)*(w-x)/w;
999
1000 if (nB1 > nB2)
1001 nB = nB1 - (nB1-nB2)*(w-x)/w;
1002 else
1003 nB = nB1 + (nB2-nB1)*(w-x)/w;
1004
1005 wxColour colour(nR,nG,nB);
1006 SetPen(wxPen(colour, 1, wxSOLID));
1007 SetBrush(wxBrush(colour));
1008 if(nDirection == wxEAST)
1009 DoDrawRectangle(rect.GetRight()-x-xDelta+1, rect.GetTop(),
1010 xDelta, rect.GetHeight());
1011 else //nDirection == wxWEST
1012 DoDrawRectangle(rect.GetLeft()+x, rect.GetTop(),
1013 xDelta, rect.GetHeight());
1014 }
1015 }
1016 else // nDirection == wxNORTH || nDirection == wxSOUTH
1017 {
1018 wxInt32 y = rect.GetHeight();
1019 wxInt32 w = y; // height of area to shade
1020 wxInt32 yDelta = w/255; // height of one shade bend
1021 if (yDelta < 1)
1022 yDelta = 1;
1023
1024 while (y > 0)
1025 {
1026 y -= yDelta;
1027 if (nR1 > nR2)
1028 nR = nR1 - (nR1-nR2)*(w-y)/w;
1029 else
1030 nR = nR1 + (nR2-nR1)*(w-y)/w;
1031
1032 if (nG1 > nG2)
1033 nG = nG1 - (nG1-nG2)*(w-y)/w;
1034 else
1035 nG = nG1 + (nG2-nG1)*(w-y)/w;
1036
1037 if (nB1 > nB2)
1038 nB = nB1 - (nB1-nB2)*(w-y)/w;
1039 else
1040 nB = nB1 + (nB2-nB1)*(w-y)/w;
1041
1042 wxColour colour(nR,nG,nB);
1043 SetPen(wxPen(colour, 1, wxSOLID));
1044 SetBrush(wxBrush(colour));
1045 if(nDirection == wxNORTH)
1046 DoDrawRectangle(rect.GetLeft(), rect.GetTop()+y,
1047 rect.GetWidth(), yDelta);
1048 else //nDirection == wxSOUTH
1049 DoDrawRectangle(rect.GetLeft(), rect.GetBottom()-y-yDelta+1,
1050 rect.GetWidth(), yDelta);
1051 }
1052 }
1053
1054 SetPen(oldPen);
1055 SetBrush(oldBrush);
1056 }
1057
1058 void wxDCImpl::DoGradientFillConcentric(const wxRect& rect,
1059 const wxColour& initialColour,
1060 const wxColour& destColour,
1061 const wxPoint& circleCenter)
1062 {
1063 //save the old pen color
1064 wxColour oldPenColour = m_pen.GetColour();
1065
1066 wxUint8 nR1 = destColour.Red();
1067 wxUint8 nG1 = destColour.Green();
1068 wxUint8 nB1 = destColour.Blue();
1069 wxUint8 nR2 = initialColour.Red();
1070 wxUint8 nG2 = initialColour.Green();
1071 wxUint8 nB2 = initialColour.Blue();
1072 wxUint8 nR, nG, nB;
1073
1074
1075 //Radius
1076 wxInt32 cx = rect.GetWidth() / 2;
1077 wxInt32 cy = rect.GetHeight() / 2;
1078 wxInt32 nRadius;
1079 if (cx < cy)
1080 nRadius = cx;
1081 else
1082 nRadius = cy;
1083
1084 //Offset of circle
1085 wxInt32 nCircleOffX = circleCenter.x - (rect.GetWidth() / 2);
1086 wxInt32 nCircleOffY = circleCenter.y - (rect.GetHeight() / 2);
1087
1088 for ( wxInt32 x = 0; x < rect.GetWidth(); x++ )
1089 {
1090 for ( wxInt32 y = 0; y < rect.GetHeight(); y++ )
1091 {
1092 //get color difference
1093 wxInt32 nGradient = ((nRadius -
1094 (wxInt32)sqrt(
1095 pow((double)(x - cx - nCircleOffX), 2) +
1096 pow((double)(y - cy - nCircleOffY), 2)
1097 )) * 100) / nRadius;
1098
1099 //normalize Gradient
1100 if (nGradient < 0 )
1101 nGradient = 0;
1102
1103 //get dest colors
1104 nR = (wxUint8)(nR1 + ((nR2 - nR1) * nGradient / 100));
1105 nG = (wxUint8)(nG1 + ((nG2 - nG1) * nGradient / 100));
1106 nB = (wxUint8)(nB1 + ((nB2 - nB1) * nGradient / 100));
1107
1108 //set the pixel
1109 m_pen.SetColour(wxColour(nR,nG,nB));
1110 DoDrawPoint(x + rect.GetLeft(), y + rect.GetTop());
1111 }
1112 }
1113 //return old pen color
1114 m_pen.SetColour(oldPenColour);
1115 }
1116
1117 //-----------------------------------------------------------------------------
1118 // wxDC
1119 //-----------------------------------------------------------------------------
1120
1121 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
1122
1123 void wxDC::DrawLabel(const wxString& text,
1124 const wxBitmap& bitmap,
1125 const wxRect& rect,
1126 int alignment,
1127 int indexAccel,
1128 wxRect *rectBounding)
1129 {
1130 // find the text position
1131 wxCoord widthText, heightText, heightLine;
1132 GetMultiLineTextExtent(text, &widthText, &heightText, &heightLine);
1133
1134 wxCoord width, height;
1135 if ( bitmap.Ok() )
1136 {
1137 width = widthText + bitmap.GetWidth();
1138 height = bitmap.GetHeight();
1139 }
1140 else // no bitmap
1141 {
1142 width = widthText;
1143 height = heightText;
1144 }
1145
1146 wxCoord x, y;
1147 if ( alignment & wxALIGN_RIGHT )
1148 {
1149 x = rect.GetRight() - width;
1150 }
1151 else if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1152 {
1153 x = (rect.GetLeft() + rect.GetRight() + 1 - width) / 2;
1154 }
1155 else // alignment & wxALIGN_LEFT
1156 {
1157 x = rect.GetLeft();
1158 }
1159
1160 if ( alignment & wxALIGN_BOTTOM )
1161 {
1162 y = rect.GetBottom() - height;
1163 }
1164 else if ( alignment & wxALIGN_CENTRE_VERTICAL )
1165 {
1166 y = (rect.GetTop() + rect.GetBottom() + 1 - height) / 2;
1167 }
1168 else // alignment & wxALIGN_TOP
1169 {
1170 y = rect.GetTop();
1171 }
1172
1173 // draw the bitmap first
1174 wxCoord x0 = x,
1175 y0 = y,
1176 width0 = width;
1177 if ( bitmap.Ok() )
1178 {
1179 DrawBitmap(bitmap, x, y, true /* use mask */);
1180
1181 wxCoord offset = bitmap.GetWidth() + 4;
1182 x += offset;
1183 width -= offset;
1184
1185 y += (height - heightText) / 2;
1186 }
1187
1188 // we will draw the underscore under the accel char later
1189 wxCoord startUnderscore = 0,
1190 endUnderscore = 0,
1191 yUnderscore = 0;
1192
1193 // split the string into lines and draw each of them separately
1194 wxString curLine;
1195 for ( wxString::const_iterator pc = text.begin(); ; ++pc )
1196 {
1197 if ( *pc == _T('\n') || pc == text.end() )
1198 {
1199 int xRealStart = x; // init it here to avoid compielr warnings
1200
1201 if ( !curLine.empty() )
1202 {
1203 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1204 // wxALIGN_LEFT is 0
1205 if ( alignment & (wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL) )
1206 {
1207 wxCoord widthLine;
1208 GetTextExtent(curLine, &widthLine, NULL);
1209
1210 if ( alignment & wxALIGN_RIGHT )
1211 {
1212 xRealStart += width - widthLine;
1213 }
1214 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1215 {
1216 xRealStart += (width - widthLine) / 2;
1217 }
1218 }
1219 //else: left aligned, nothing to do
1220
1221 DrawText(curLine, xRealStart, y);
1222 }
1223
1224 y += heightLine;
1225
1226 // do we have underscore in this line? we can check yUnderscore
1227 // because it is set below to just y + heightLine if we do
1228 if ( y == yUnderscore )
1229 {
1230 // adjust the horz positions to account for the shift
1231 startUnderscore += xRealStart;
1232 endUnderscore += xRealStart;
1233 }
1234
1235 if ( pc == text.end() )
1236 break;
1237
1238 curLine.clear();
1239 }
1240 else // not end of line
1241 {
1242 if ( pc - text.begin() == indexAccel )
1243 {
1244 // remeber to draw underscore here
1245 GetTextExtent(curLine, &startUnderscore, NULL);
1246 curLine += *pc;
1247 GetTextExtent(curLine, &endUnderscore, NULL);
1248
1249 yUnderscore = y + heightLine;
1250 }
1251 else
1252 {
1253 curLine += *pc;
1254 }
1255 }
1256 }
1257
1258 // draw the underscore if found
1259 if ( startUnderscore != endUnderscore )
1260 {
1261 // it should be of the same colour as text
1262 SetPen(wxPen(GetTextForeground(), 0, wxSOLID));
1263
1264 yUnderscore--;
1265
1266 DrawLine(startUnderscore, yUnderscore, endUnderscore, yUnderscore);
1267 }
1268
1269 // return bounding rect if requested
1270 if ( rectBounding )
1271 {
1272 *rectBounding = wxRect(x, y - heightText, widthText, heightText);
1273 }
1274
1275 CalcBoundingBox(x0, y0);
1276 CalcBoundingBox(x0 + width0, y0 + height);
1277 }
1278
1279 #if WXWIN_COMPATIBILITY_2_8
1280 // for compatibility with the old code when wxCoord was long everywhere
1281 void wxDC::GetTextExtent(const wxString& string,
1282 long *x, long *y,
1283 long *descent,
1284 long *externalLeading,
1285 const wxFont *theFont) const
1286 {
1287 wxCoord x2, y2, descent2, externalLeading2;
1288 m_pimpl->DoGetTextExtent(string, &x2, &y2,
1289 &descent2, &externalLeading2,
1290 theFont);
1291 if ( x )
1292 *x = x2;
1293 if ( y )
1294 *y = y2;
1295 if ( descent )
1296 *descent = descent2;
1297 if ( externalLeading )
1298 *externalLeading = externalLeading2;
1299 }
1300
1301 void wxDC::GetLogicalOrigin(long *x, long *y) const
1302 {
1303 wxCoord x2, y2;
1304 m_pimpl->DoGetLogicalOrigin(&x2, &y2);
1305 if ( x )
1306 *x = x2;
1307 if ( y )
1308 *y = y2;
1309 }
1310
1311 void wxDC::GetDeviceOrigin(long *x, long *y) const
1312 {
1313 wxCoord x2, y2;
1314 m_pimpl->DoGetDeviceOrigin(&x2, &y2);
1315 if ( x )
1316 *x = x2;
1317 if ( y )
1318 *y = y2;
1319 }
1320
1321 void wxDC::GetClippingBox(long *x, long *y, long *w, long *h) const
1322 {
1323 wxCoord xx,yy,ww,hh;
1324 m_pimpl->DoGetClippingBox(&xx, &yy, &ww, &hh);
1325 if (x) *x = xx;
1326 if (y) *y = yy;
1327 if (w) *w = ww;
1328 if (h) *h = hh;
1329 }
1330
1331 #endif // WXWIN_COMPATIBILITY_2_8