]> git.saurik.com Git - wxWidgets.git/blame - src/common/cmndata.cpp
fix window rectangle computation in Centre(wxCENTER_ON_SCREEN)
[wxWidgets.git] / src / common / cmndata.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/common/cmndata.cpp
c801d85f
KB
3// Purpose: Common GDI data
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
8826f46f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
8826f46f 24 #pragma hdrstop
c801d85f
KB
25#endif
26
e4db172a
WS
27#include "wx/cmndata.h"
28
c801d85f 29#ifndef WX_PRECOMP
57bd4c60
WS
30 #if defined(__WXMSW__)
31 #include "wx/msw/wrapcdlg.h"
32 #endif // MSW
8826f46f
VZ
33 #include <stdio.h>
34 #include "wx/string.h"
35 #include "wx/utils.h"
36 #include "wx/app.h"
e4db172a 37 #include "wx/log.h"
dd05139a 38 #include "wx/gdicmn.h"
c801d85f
KB
39#endif
40
166fbaa4 41#include "wx/tokenzr.h"
8850cbd3
RR
42#include "wx/prntbase.h"
43#include "wx/printdlg.h"
8826f46f 44
dbc65e27
VZ
45#if wxUSE_FONTDLG
46 #include "wx/fontdlg.h"
47#endif // wxUSE_FONTDLG
48
88ac883a 49#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 50
57bd4c60 51#include "wx/paper.h"
179e085f 52
746d7582 53
57bd4c60
WS
54IMPLEMENT_DYNAMIC_CLASS(wxPrintData, wxObject)
55IMPLEMENT_DYNAMIC_CLASS(wxPrintDialogData, wxObject)
56IMPLEMENT_DYNAMIC_CLASS(wxPageSetupDialogData, wxObject)
57
58#endif // wxUSE_PRINTING_ARCHITECTURE
cd0b1709 59
57bd4c60
WS
60IMPLEMENT_DYNAMIC_CLASS(wxFontData, wxObject)
61IMPLEMENT_DYNAMIC_CLASS(wxColourData, wxObject)
c801d85f 62
8826f46f
VZ
63// ============================================================================
64// implementation
65// ============================================================================
66
67// ----------------------------------------------------------------------------
68// wxColourData
69// ----------------------------------------------------------------------------
c801d85f 70
8bbe427f 71wxColourData::wxColourData()
c801d85f 72{
393c836c 73 m_chooseFull = false;
ae500232 74 m_dataColour.Set(0,0,0);
393c836c 75 // m_custColours are wxNullColours initially
7bcb11d3 76}
c801d85f 77
7bcb11d3 78wxColourData::wxColourData(const wxColourData& data)
5cd0202b 79 : wxObject()
7bcb11d3
JS
80{
81 (*this) = data;
8bbe427f 82}
c801d85f 83
8bbe427f 84wxColourData::~wxColourData()
c801d85f
KB
85{
86}
87
cebf2fec 88void wxColourData::SetCustomColour(int i, const wxColour& colour)
c801d85f 89{
559843f0 90 wxCHECK_RET( i >= 0 && i < NUM_CUSTOM, _T("custom colour index out of range") );
8826f46f 91
ae500232 92 m_custColours[i] = colour;
c801d85f
KB
93}
94
95wxColour wxColourData::GetCustomColour(int i)
96{
559843f0 97 wxCHECK_MSG( i >= 0 && i < NUM_CUSTOM, wxColour(0,0,0),
393c836c 98 _T("custom colour index out of range") );
8826f46f 99
ae500232 100 return m_custColours[i];
c801d85f
KB
101}
102
5cd0202b 103wxColourData& wxColourData::operator=(const wxColourData& data)
c801d85f 104{
559843f0 105 for ( int i = 0; i < NUM_CUSTOM; i++)
ae500232 106 m_custColours[i] = data.m_custColours[i];
8826f46f 107
e6ef9ea4 108 m_dataColour = data.m_dataColour;
ae500232 109 m_chooseFull = data.m_chooseFull;
5cd0202b
VZ
110
111 return *this;
c801d85f
KB
112}
113
e6ef9ea4
VZ
114// ----------------------------------------------------------------------------
115// [de]serialization
116// ----------------------------------------------------------------------------
117
118// separator used between different fields
119static const char wxCOL_DATA_SEP = ',';
120
121wxString wxColourData::ToString() const
122{
123 wxString str(m_chooseFull ? '1' : '0');
124
559843f0 125 for ( int i = 0; i < NUM_CUSTOM; i++ )
e6ef9ea4
VZ
126 {
127 str += wxCOL_DATA_SEP;
128
129 const wxColour& clr = m_custColours[i];
130 if ( clr.IsOk() )
131 str += clr.GetAsString(wxC2S_HTML_SYNTAX);
132 }
133
134 return str;
135}
136
137bool wxColourData::FromString(const wxString& str)
138{
166fbaa4
PC
139 wxStringTokenizer tokenizer(str, wxCOL_DATA_SEP);
140 wxString token = tokenizer.GetNextToken();
141 m_chooseFull = token == '1';
142 bool success = m_chooseFull || token == '0';
143 for (int i = 0; success && i < NUM_CUSTOM; i++)
e6ef9ea4 144 {
166fbaa4
PC
145 token = tokenizer.GetNextToken();
146 if (token.empty())
147 m_custColours[i] = wxNullColour;
148 else
149 success = m_custColours[i].Set(token);
e6ef9ea4 150 }
166fbaa4 151 return success;
e6ef9ea4
VZ
152}
153
8826f46f
VZ
154// ----------------------------------------------------------------------------
155// Font data
156// ----------------------------------------------------------------------------
c801d85f 157
8bbe427f 158wxFontData::wxFontData()
c801d85f 159{
7bcb11d3 160 // Intialize colour to black.
ae500232 161 m_fontColour = wxNullColour;
8826f46f 162
c9d59ee7
WS
163 m_showHelp = false;
164 m_allowSymbols = true;
165 m_enableEffects = true;
ae500232
JS
166 m_minSize = 0;
167 m_maxSize = 0;
c801d85f 168
7beba2fc 169 m_encoding = wxFONTENCODING_SYSTEM;
c801d85f
KB
170}
171
8bbe427f 172wxFontData::~wxFontData()
c801d85f
KB
173{
174}
175
dbc65e27
VZ
176#if wxUSE_FONTDLG
177
178wxFontDialogBase::~wxFontDialogBase()
179{
180}
181
182#endif // wxUSE_FONTDLG
183
88ac883a 184#if wxUSE_PRINTING_ARCHITECTURE
8826f46f
VZ
185// ----------------------------------------------------------------------------
186// Print data
187// ----------------------------------------------------------------------------
c801d85f 188
8bbe427f 189wxPrintData::wxPrintData()
c801d85f 190{
60090256 191 m_bin = wxPRINTBIN_DEFAULT;
781609f2 192 m_media = wxPRINTMEDIA_DEFAULT;
6038ec8e 193 m_printMode = wxPRINT_MODE_PRINTER;
7bcb11d3 194 m_printOrientation = wxPORTRAIT;
c7c6e54b 195 m_printOrientationReversed = false;
7bcb11d3 196 m_printNoCopies = 1;
c9d59ee7 197 m_printCollate = false;
8826f46f 198
7bcb11d3 199 // New, 24/3/99
b494c48b 200 m_printerName = wxEmptyString;
c9d59ee7 201 m_colour = true;
7bcb11d3
JS
202 m_duplexMode = wxDUPLEX_SIMPLEX;
203 m_printQuality = wxPRINT_QUALITY_HIGH;
d5eaa19f
VZ
204
205 // we intentionally don't initialize paper id and size at all, like this
206 // the default system settings will be used for them
207 m_paperId = wxPAPER_NONE;
208 m_paperSize = wxDefaultSize;
7bcb11d3 209
aa96f01c
RR
210 m_privData = NULL;
211 m_privDataLen = 0;
660296aa 212
8850cbd3 213 m_nativeData = wxPrintFactory::GetFactory()->CreatePrintNativeData();
7bcb11d3
JS
214}
215
216wxPrintData::wxPrintData(const wxPrintData& printData)
5e472c1f 217 : wxObject()
7bcb11d3 218{
7b8a9da3 219 m_nativeData = NULL;
7cc52cd8 220 m_privData = NULL;
7bcb11d3 221 (*this) = printData;
c801d85f
KB
222}
223
aa96f01c
RR
224void wxPrintData::SetPrivData( char *privData, int len )
225{
226 if (m_privData)
227 {
228 delete [] m_privData;
229 m_privData = NULL;
230 }
231 m_privDataLen = len;
232 if (m_privDataLen > 0)
233 {
234 m_privData = new char[m_privDataLen];
235 memcpy( m_privData, privData, m_privDataLen );
236 }
237}
238
8bbe427f 239wxPrintData::~wxPrintData()
c801d85f 240{
8850cbd3 241 m_nativeData->m_ref--;
4055ed82 242 if (m_nativeData->m_ref == 0)
8850cbd3 243 delete m_nativeData;
660296aa 244
aa96f01c
RR
245 if (m_privData)
246 delete [] m_privData;
c801d85f
KB
247}
248
51abe921
SC
249void wxPrintData::ConvertToNative()
250{
53fa663a 251 m_nativeData->TransferFrom( *this ) ;
51abe921
SC
252}
253
254void wxPrintData::ConvertFromNative()
255{
53fa663a 256 m_nativeData->TransferTo( *this ) ;
53fa663a 257}
51abe921 258
7bcb11d3
JS
259void wxPrintData::operator=(const wxPrintData& data)
260{
261 m_printNoCopies = data.m_printNoCopies;
262 m_printCollate = data.m_printCollate;
263 m_printOrientation = data.m_printOrientation;
c7c6e54b 264 m_printOrientationReversed = data.m_printOrientationReversed;
7bcb11d3
JS
265 m_printerName = data.m_printerName;
266 m_colour = data.m_colour;
267 m_duplexMode = data.m_duplexMode;
268 m_printQuality = data.m_printQuality;
269 m_paperId = data.m_paperId;
270 m_paperSize = data.m_paperSize;
60090256 271 m_bin = data.m_bin;
781609f2 272 m_media = data.m_media;
6038ec8e 273 m_printMode = data.m_printMode;
4055ed82 274 m_filename = data.m_filename;
aac85509 275
4055ed82 276 // UnRef old m_nativeData
7b8a9da3
RD
277 if (m_nativeData)
278 {
279 m_nativeData->m_ref--;
4055ed82 280 if (m_nativeData->m_ref == 0)
7b8a9da3
RD
281 delete m_nativeData;
282 }
aac85509 283 // Set Ref new one
8850cbd3
RR
284 m_nativeData = data.GetNativeData();
285 m_nativeData->m_ref++;
4055ed82 286
aa96f01c
RR
287 if (m_privData)
288 {
289 delete [] m_privData;
290 m_privData = NULL;
291 }
292 m_privDataLen = data.GetPrivDataLen();
293 if (m_privDataLen > 0)
294 {
295 m_privData = new char[m_privDataLen];
296 memcpy( m_privData, data.GetPrivData(), m_privDataLen );
297 }
7bcb11d3
JS
298}
299
58cf0491 300// Is this data OK for showing the print dialog?
b7cacb43 301bool wxPrintData::IsOk() const
58cf0491 302{
fd64de59 303 m_nativeData->TransferFrom( *this );
8850cbd3
RR
304
305 return m_nativeData->Ok();
58cf0491 306}
8826f46f
VZ
307
308// ----------------------------------------------------------------------------
309// Print dialog data
310// ----------------------------------------------------------------------------
7bcb11d3
JS
311
312wxPrintDialogData::wxPrintDialogData()
313{
7bcb11d3
JS
314 m_printFromPage = 0;
315 m_printToPage = 0;
316 m_printMinPage = 0;
317 m_printMaxPage = 0;
318 m_printNoCopies = 1;
c9d59ee7
WS
319 m_printAllPages = false;
320 m_printCollate = false;
321 m_printToFile = false;
322 m_printSelection = false;
323 m_printEnableSelection = false;
324 m_printEnablePageNumbers = true;
4055ed82 325
9f1316dc
RR
326 wxPrintFactory* factory = wxPrintFactory::GetFactory();
327 m_printEnablePrintToFile = ! factory->HasOwnPrintToFile();
4055ed82 328
c9d59ee7 329 m_printEnableHelp = false;
7bcb11d3
JS
330}
331
332wxPrintDialogData::wxPrintDialogData(const wxPrintDialogData& dialogData)
5e472c1f 333 : wxObject()
7bcb11d3
JS
334{
335 (*this) = dialogData;
336}
337
338wxPrintDialogData::wxPrintDialogData(const wxPrintData& printData)
339{
d2b354f9 340 m_printFromPage = 1;
7bcb11d3 341 m_printToPage = 0;
d2b354f9
JS
342 m_printMinPage = 1;
343 m_printMaxPage = 9999;
7bcb11d3 344 m_printNoCopies = 1;
c9d59ee7
WS
345 m_printAllPages = false;
346 m_printCollate = false;
347 m_printToFile = false;
348 m_printSelection = false;
349 m_printEnableSelection = false;
350 m_printEnablePageNumbers = true;
351 m_printEnablePrintToFile = true;
352 m_printEnableHelp = false;
7bcb11d3
JS
353 m_printData = printData;
354}
355
356wxPrintDialogData::~wxPrintDialogData()
357{
7bcb11d3
JS
358}
359
7bcb11d3 360void wxPrintDialogData::operator=(const wxPrintDialogData& data)
c801d85f 361{
7bcb11d3
JS
362 m_printFromPage = data.m_printFromPage;
363 m_printToPage = data.m_printToPage;
364 m_printMinPage = data.m_printMinPage;
365 m_printMaxPage = data.m_printMaxPage;
366 m_printNoCopies = data.m_printNoCopies;
367 m_printAllPages = data.m_printAllPages;
368 m_printCollate = data.m_printCollate;
369 m_printToFile = data.m_printToFile;
5360828d 370 m_printSelection = data.m_printSelection;
7bcb11d3
JS
371 m_printEnableSelection = data.m_printEnableSelection;
372 m_printEnablePageNumbers = data.m_printEnablePageNumbers;
373 m_printEnableHelp = data.m_printEnableHelp;
374 m_printEnablePrintToFile = data.m_printEnablePrintToFile;
7bcb11d3
JS
375 m_printData = data.m_printData;
376}
377
378void wxPrintDialogData::operator=(const wxPrintData& data)
379{
380 m_printData = data;
c801d85f
KB
381}
382
8826f46f
VZ
383// ----------------------------------------------------------------------------
384// wxPageSetupDialogData
385// ----------------------------------------------------------------------------
c801d85f 386
7bcb11d3 387wxPageSetupDialogData::wxPageSetupDialogData()
c801d85f 388{
c47addef 389 m_paperSize = wxSize(0,0);
7bcb11d3
JS
390
391 CalculatePaperSizeFromId();
392
2997ca30
WS
393 m_minMarginTopLeft =
394 m_minMarginBottomRight =
395 m_marginTopLeft =
c47addef 396 m_marginBottomRight = wxPoint(0,0);
7bcb11d3
JS
397
398 // Flags
c9d59ee7
WS
399 m_defaultMinMargins = false;
400 m_enableMargins = true;
401 m_enableOrientation = true;
402 m_enablePaper = true;
403 m_enablePrinter = true;
404 m_enableHelp = false;
405 m_getDefaultInfo = false;
7bcb11d3 406}
c801d85f 407
7bcb11d3 408wxPageSetupDialogData::wxPageSetupDialogData(const wxPageSetupDialogData& dialogData)
5e472c1f 409 : wxObject()
7bcb11d3
JS
410{
411 (*this) = dialogData;
412}
413
414wxPageSetupDialogData::wxPageSetupDialogData(const wxPrintData& printData)
415{
c47addef 416 m_paperSize = wxSize(0,0);
2997ca30
WS
417 m_minMarginTopLeft =
418 m_minMarginBottomRight =
419 m_marginTopLeft =
c47addef 420 m_marginBottomRight = wxPoint(0,0);
7bcb11d3
JS
421
422 // Flags
c9d59ee7
WS
423 m_defaultMinMargins = false;
424 m_enableMargins = true;
425 m_enableOrientation = true;
426 m_enablePaper = true;
427 m_enablePrinter = true;
428 m_enableHelp = false;
429 m_getDefaultInfo = false;
7bcb11d3
JS
430
431 m_printData = printData;
432
433 // The wxPrintData paper size overrides these values, unless the size cannot
434 // be found.
435 CalculatePaperSizeFromId();
c801d85f
KB
436}
437
7bcb11d3 438wxPageSetupDialogData::~wxPageSetupDialogData()
c801d85f 439{
c801d85f
KB
440}
441
5e472c1f 442wxPageSetupDialogData& wxPageSetupDialogData::operator=(const wxPageSetupDialogData& data)
c801d85f 443{
7bcb11d3
JS
444 m_paperSize = data.m_paperSize;
445 m_minMarginTopLeft = data.m_minMarginTopLeft;
446 m_minMarginBottomRight = data.m_minMarginBottomRight;
447 m_marginTopLeft = data.m_marginTopLeft;
448 m_marginBottomRight = data.m_marginBottomRight;
449 m_defaultMinMargins = data.m_defaultMinMargins;
450 m_enableMargins = data.m_enableMargins;
451 m_enableOrientation = data.m_enableOrientation;
452 m_enablePaper = data.m_enablePaper;
453 m_enablePrinter = data.m_enablePrinter;
d0ee33f5 454 m_getDefaultInfo = data.m_getDefaultInfo;
7bcb11d3
JS
455 m_enableHelp = data.m_enableHelp;
456
457 m_printData = data.m_printData;
5e472c1f
GD
458
459 return *this;
7bcb11d3 460}
c801d85f 461
5e472c1f 462wxPageSetupDialogData& wxPageSetupDialogData::operator=(const wxPrintData& data)
7bcb11d3
JS
463{
464 m_printData = data;
24c5243d 465 CalculatePaperSizeFromId();
5e472c1f
GD
466
467 return *this;
c801d85f
KB
468}
469
7bcb11d3
JS
470// If a corresponding paper type is found in the paper database, will set the m_printData
471// paper size id member as well.
472void wxPageSetupDialogData::SetPaperSize(const wxSize& sz)
473{
474 m_paperSize = sz;
c801d85f 475
7bcb11d3
JS
476 CalculateIdFromPaperSize();
477}
c801d85f 478
7bcb11d3
JS
479// Sets the wxPrintData id, plus the paper width/height if found in the paper database.
480void wxPageSetupDialogData::SetPaperSize(wxPaperSize id)
481{
482 m_printData.SetPaperId(id);
483
484 CalculatePaperSizeFromId();
c801d85f
KB
485}
486
24c5243d
RD
487void wxPageSetupDialogData::SetPrintData(const wxPrintData& printData)
488{
489 m_printData = printData;
b494c48b 490 CalculatePaperSizeFromId();
24c5243d
RD
491}
492
7bcb11d3
JS
493// Use paper size defined in this object to set the wxPrintData
494// paper id
495void wxPageSetupDialogData::CalculateIdFromPaperSize()
c801d85f 496{
8826f46f 497 wxASSERT_MSG( (wxThePrintPaperDatabase != (wxPrintPaperDatabase*) NULL),
fbdcff4a 498 wxT("wxThePrintPaperDatabase should not be NULL. Do not create global print dialog data objects.") );
c801d85f 499
7bcb11d3
JS
500 wxSize sz = GetPaperSize();
501
502 wxPaperSize id = wxThePrintPaperDatabase->GetSize(wxSize(sz.x* 10, sz.y * 10));
503 if (id != wxPAPER_NONE)
c801d85f 504 {
7bcb11d3 505 m_printData.SetPaperId(id);
c801d85f
KB
506 }
507}
8826f46f 508
7bcb11d3
JS
509// Use paper id in wxPrintData to set this object's paper size
510void wxPageSetupDialogData::CalculatePaperSizeFromId()
511{
8826f46f 512 wxASSERT_MSG( (wxThePrintPaperDatabase != (wxPrintPaperDatabase*) NULL),
fbdcff4a 513 wxT("wxThePrintPaperDatabase should not be NULL. Do not create global print dialog data objects.") );
7bcb11d3
JS
514
515 wxSize sz = wxThePrintPaperDatabase->GetSize(m_printData.GetPaperId());
c801d85f 516
01cf2f95
VZ
517 // sz is in 10ths of a mm, while paper size is in mm
518 m_paperSize.x = sz.x / 10;
519 m_paperSize.y = sz.y / 10;
7bcb11d3 520}
8826f46f 521
88ac883a 522#endif // wxUSE_PRINTING_ARCHITECTURE