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