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