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