]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/gnome/gprint.cpp
Further compile fixes.
[wxWidgets.git] / src / gtk1 / gnome / gprint.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gprint.cpp
3 // Author: Robert Roebling
4 // Purpose: Implement GNOME printing support
5 // Created: 09/20/04
6 // Copyright: Robert Roebling
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "gprint.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/gtk/gnome/gprint.h"
22
23 #if wxUSE_LIBGNOMEPRINT
24
25 #include "wx/fontutil.h"
26 #include "wx/printdlg.h"
27 #include "wx/gtk/private.h"
28
29 #include <libgnomeprint/gnome-print.h>
30 #include <libgnomeprint/gnome-print-pango.h>
31 #include <libgnomeprintui/gnome-print-dialog.h>
32
33 //----------------------------------------------------------------------------
34 // wxGnomePrintNativeData
35 //----------------------------------------------------------------------------
36
37 IMPLEMENT_CLASS(wxGnomePrintNativeData, wxPrintNativeDataBase)
38
39 wxGnomePrintNativeData::wxGnomePrintNativeData()
40 {
41 m_config = gnome_print_config_default();
42 m_job = gnome_print_job_new( m_config );
43 }
44
45 wxGnomePrintNativeData::~wxGnomePrintNativeData()
46 {
47 g_object_unref (G_OBJECT (m_config));
48 g_object_unref (G_OBJECT (m_job));
49 }
50
51 bool wxGnomePrintNativeData::TransferTo( wxPrintData &data )
52 {
53 // TODO
54 return true;
55 }
56
57 bool wxGnomePrintNativeData::TransferFrom( const wxPrintData &data )
58 {
59 // TODO
60 return true;
61 }
62
63 //----------------------------------------------------------------------------
64 // wxGnomePrintFactory
65 //----------------------------------------------------------------------------
66
67 wxPrinterBase* wxGnomePrintFactory::CreatePrinter( wxPrintDialogData *data )
68 {
69 return new wxGnomePrinter( data );
70 }
71
72 wxPrintPreviewBase *wxGnomePrintFactory::CreatePrintPreview( wxPrintout *preview,
73 wxPrintout *printout,
74 wxPrintDialogData *data )
75 {
76 return new wxPostScriptPrintPreview( preview, printout, data );
77 }
78
79 wxPrintPreviewBase *wxGnomePrintFactory::CreatePrintPreview( wxPrintout *preview,
80 wxPrintout *printout,
81 wxPrintData *data )
82 {
83 return new wxPostScriptPrintPreview( preview, printout, data );
84 }
85
86 wxPrintDialogBase *wxGnomePrintFactory::CreatePrintDialog( wxWindow *parent,
87 wxPrintDialogData *data )
88 {
89 return new wxGenericPrintDialog( parent, data );
90 }
91
92 wxPrintDialogBase *wxGnomePrintFactory::CreatePrintDialog( wxWindow *parent,
93 wxPrintData *data )
94 {
95 return new wxGenericPrintDialog( parent, data );
96 }
97
98 bool wxGnomePrintFactory::HasPrintSetupDialog()
99 {
100 return true;
101 }
102
103 wxDialog *wxGnomePrintFactory::CreatePrintSetupDialog( wxWindow *parent, wxPrintData *data )
104 {
105 return new wxGnomePrintSetupDialog( parent, data );
106 }
107
108 bool wxGnomePrintFactory::HasOwnPrintToFile()
109 {
110 return true;
111 }
112
113 bool wxGnomePrintFactory::HasPrinterLine()
114 {
115 return true;
116 }
117
118 wxString wxGnomePrintFactory::CreatePrinterLine()
119 {
120 // We should query "gnome_config_default" here
121 return _("GNOME print");
122 }
123
124 bool wxGnomePrintFactory::HasStatusLine()
125 {
126 return true;
127 }
128
129 wxString wxGnomePrintFactory::CreateStatusLine()
130 {
131 // We should query "gnome_config_default" here
132 return _("Ready");
133 }
134
135 wxPrintNativeDataBase *wxGnomePrintFactory::CreatePrintNativeData()
136 {
137 return new wxGnomePrintNativeData;
138 }
139
140 //----------------------------------------------------------------------------
141 // wxGnomePrintSetupDialog
142 //----------------------------------------------------------------------------
143
144 IMPLEMENT_CLASS(wxGnomePrintSetupDialog, wxDialog)
145
146 wxGnomePrintSetupDialog::wxGnomePrintSetupDialog( wxWindow *parent, wxPrintData *data )
147 {
148 wxGnomePrintNativeData *native =
149 (wxGnomePrintNativeData*) data->GetNativeData();
150
151 m_widget = gnome_print_dialog_new (native->GetPrintJob(), (guchar*)"Print setup", 0);
152 }
153
154 wxGnomePrintSetupDialog::~wxGnomePrintSetupDialog()
155 {
156 m_widget = NULL;
157 }
158
159 int wxGnomePrintSetupDialog::ShowModal()
160 {
161 int response = gtk_dialog_run (GTK_DIALOG (m_widget));
162 gtk_widget_destroy(m_widget);
163 m_widget = NULL;
164
165 if (response == GNOME_PRINT_DIALOG_RESPONSE_CANCEL)
166 return wxID_CANCEL;
167
168 return wxID_OK;
169 }
170
171 bool wxGnomePrintSetupDialog::Validate()
172 {
173 return true;
174 }
175
176 bool wxGnomePrintSetupDialog::TransferDataToWindow()
177 {
178 return true;
179 }
180
181 bool wxGnomePrintSetupDialog::TransferDataFromWindow()
182 {
183 return true;
184 }
185
186
187 //----------------------------------------------------------------------------
188 // wxGnomePrinter
189 //----------------------------------------------------------------------------
190
191 IMPLEMENT_CLASS(wxGnomePrinter, wxPrinterBase)
192
193 wxGnomePrinter::wxGnomePrinter( wxPrintDialogData *data ) :
194 wxPrinterBase( data )
195 {
196 m_gpc = NULL;
197 }
198
199 wxGnomePrinter::~wxGnomePrinter()
200 {
201 }
202
203 bool wxGnomePrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt )
204 {
205 if (!printout)
206 {
207 sm_lastError = wxPRINTER_ERROR;
208 return false;
209 }
210
211 wxPrintData printdata = GetPrintDialogData().GetPrintData();
212 wxGnomePrintNativeData *data =
213 (wxGnomePrintNativeData*) printdata.GetNativeData();
214
215 GnomePrintJob *job = data->GetPrintJob();
216 m_gpc = gnome_print_job_get_context (job);
217
218 wxDC *dc;
219
220 if (prompt)
221 dc = PrintDialog( parent );
222 else
223 dc = new wxGnomePrintDC( this );
224
225 if (!dc)
226 {
227 gnome_print_job_close( job );
228 return false;
229 }
230
231 printout->SetDC(dc);
232
233 printout->OnPreparePrinting();
234 printout->OnBeginPrinting();
235
236 if (!printout->OnBeginDocument(0, 0))
237 {
238 sm_lastError = wxPRINTER_ERROR;
239 }
240 else
241 {
242 int pn;
243 for (pn = 1; pn <= 2; pn++)
244 {
245 dc->StartPage();
246 printout->OnPrintPage(pn);
247 dc->EndPage();
248 }
249
250 printout->OnEndDocument();
251 printout->OnEndPrinting();
252 }
253
254 gnome_print_job_close( job );
255 gnome_print_job_print( job );
256
257 delete dc;
258
259 return (sm_lastError == wxPRINTER_NO_ERROR);
260 }
261
262 wxDC* wxGnomePrinter::PrintDialog( wxWindow *parent )
263 {
264 wxPrintDialog dialog( parent, &m_printDialogData );
265 if (dialog.ShowModal() == wxID_CANCEL)
266 {
267 sm_lastError = wxPRINTER_ERROR;
268 return NULL;
269 }
270
271 return new wxGnomePrintDC( this );
272 }
273
274 bool wxGnomePrinter::Setup( wxWindow *parent )
275 {
276 return false;
277 }
278
279 //-----------------------------------------------------------------------------
280 // wxGnomePrintDC
281 //-----------------------------------------------------------------------------
282
283 IMPLEMENT_CLASS(wxGnomePrintDC, wxDCBase)
284
285 wxGnomePrintDC::wxGnomePrintDC( wxGnomePrinter *printer )
286 {
287 m_printer = printer;
288
289 m_gpc = printer->GetPrintContext();
290
291 m_layout = gnome_print_pango_create_layout( m_gpc );
292 m_fontdesc = pango_font_description_from_string( "Sans 12" );
293
294 m_currentRed = 0;
295 m_currentBlue = 0;
296 m_currentGreen = 0;
297
298 m_signX = 1; // default x-axis left to right
299 m_signY = -1; // default y-axis bottom up -> top down
300 }
301
302 wxGnomePrintDC::~wxGnomePrintDC()
303 {
304 }
305
306 bool wxGnomePrintDC::Ok() const
307 {
308 return true;
309 }
310
311 bool wxGnomePrintDC::DoFloodFill(wxCoord x1, wxCoord y1, const wxColour &col, int style )
312 {
313 return false;
314 }
315
316 bool wxGnomePrintDC::DoGetPixel(wxCoord x1, wxCoord y1, wxColour *col) const
317 {
318 return false;
319 }
320
321 void wxGnomePrintDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
322 {
323 if (m_pen.GetStyle() == wxTRANSPARENT) return;
324
325 SetPen( m_pen );
326
327 gnome_print_moveto ( m_gpc, XDEV2LOG(x1), YDEV2LOG(y1) );
328 gnome_print_lineto ( m_gpc, XDEV2LOG(x2), YDEV2LOG(y2) );
329 gnome_print_stroke ( m_gpc);
330
331 CalcBoundingBox( x1, y1 );
332 CalcBoundingBox( x2, y2 );
333 }
334
335 void wxGnomePrintDC::DoCrossHair(wxCoord x, wxCoord y)
336 {
337 }
338
339 void wxGnomePrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)
340 {
341 }
342
343 void wxGnomePrintDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
344 {
345 }
346
347 void wxGnomePrintDC::DoDrawPoint(wxCoord x, wxCoord y)
348 {
349 }
350
351 void wxGnomePrintDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
352 {
353 }
354
355 void wxGnomePrintDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
356 {
357 }
358
359 void wxGnomePrintDC::DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
360 {
361 }
362
363 void wxGnomePrintDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
364 {
365 if (m_brush.GetStyle () != wxTRANSPARENT)
366 {
367 SetBrush( m_brush );
368
369 gnome_print_newpath( m_gpc );
370 gnome_print_moveto( m_gpc, XLOG2DEV(x), YLOG2DEV(y) );
371 gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y) );
372 gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y + height) );
373 gnome_print_lineto( m_gpc, XLOG2DEV(x), YLOG2DEV(y + height) );
374 gnome_print_closepath( m_gpc );
375 gnome_print_fill( m_gpc );
376
377 CalcBoundingBox( x, y );
378 CalcBoundingBox( x + width, y + height );
379 }
380
381 if (m_pen.GetStyle () != wxTRANSPARENT)
382 {
383 SetPen (m_pen);
384
385 gnome_print_newpath( m_gpc );
386 gnome_print_moveto( m_gpc, XLOG2DEV(x), YLOG2DEV(y) );
387 gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y) );
388 gnome_print_lineto( m_gpc, XLOG2DEV(x + width), YLOG2DEV(y + height) );
389 gnome_print_lineto( m_gpc, XLOG2DEV(x), YLOG2DEV(y + height) );
390 gnome_print_closepath( m_gpc );
391 gnome_print_stroke( m_gpc );
392
393 CalcBoundingBox( x, y );
394 CalcBoundingBox( x + width, y + height );
395 }
396 }
397
398 void wxGnomePrintDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
399 {
400 }
401
402 void wxGnomePrintDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
403 {
404 }
405
406 void wxGnomePrintDC::DoDrawSpline(wxList *points)
407 {
408 }
409
410 bool wxGnomePrintDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
411 wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop, bool useMask,
412 wxCoord xsrcMask, wxCoord ysrcMask)
413 {
414 return false;
415 }
416
417 void wxGnomePrintDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
418 {
419 }
420
421 void wxGnomePrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool useMask )
422 {
423 }
424
425 void wxGnomePrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
426 {
427 if (m_textForegroundColour.Ok())
428 {
429 unsigned char red = m_textForegroundColour.Red();
430 unsigned char blue = m_textForegroundColour.Blue();
431 unsigned char green = m_textForegroundColour.Green();
432
433 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
434 {
435 double redPS = (double)(red) / 255.0;
436 double bluePS = (double)(blue) / 255.0;
437 double greenPS = (double)(green) / 255.0;
438
439 gnome_print_setrgbcolor( m_gpc, redPS, bluePS, greenPS );
440
441 m_currentRed = red;
442 m_currentBlue = blue;
443 m_currentGreen = green;
444 }
445 }
446
447 x = XLOG2DEV(x);
448 y = YLOG2DEV(y);
449
450 wxPrintf( wxT("x,y: %d,%d\n"), x, y );
451
452 bool underlined = m_font.Ok() && m_font.GetUnderlined();
453
454 #if wxUSE_UNICODE
455 const wxCharBuffer data = wxConvUTF8.cWC2MB( text );
456 #else
457 const wxWCharBuffer wdata = wxConvLocal.cMB2WC( text );
458 if ( !wdata )
459 return;
460 const wxCharBuffer data = wxConvUTF8.cWC2MB( wdata );
461 #endif
462
463 size_t datalen = strlen((const char*)data);
464 pango_layout_set_text( m_layout, (const char*) data, datalen);
465
466 if (underlined)
467 {
468 PangoAttrList *attrs = pango_attr_list_new();
469 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
470 a->start_index = 0;
471 a->end_index = datalen;
472 pango_attr_list_insert(attrs, a);
473 pango_layout_set_attributes(m_layout, attrs);
474 pango_attr_list_unref(attrs);
475 }
476
477 gnome_print_moveto (m_gpc, x, y);
478 gnome_print_pango_layout( m_gpc, m_layout );
479
480 if (underlined)
481 {
482 // undo underline attributes setting:
483 pango_layout_set_attributes(m_layout, NULL);
484 }
485
486 // CalcBoundingBox (x + width, y + height);
487 CalcBoundingBox (x, y);
488 }
489
490 void wxGnomePrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
491 {
492 }
493
494 void wxGnomePrintDC::Clear()
495 {
496 }
497
498 void wxGnomePrintDC::SetFont( const wxFont& font )
499 {
500 m_font = font;
501
502 if (m_font.Ok())
503 {
504 if (m_fontdesc)
505 pango_font_description_free( m_fontdesc );
506
507 m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
508
509 pango_layout_set_font_description( m_layout, m_fontdesc );
510 }
511 }
512
513 void wxGnomePrintDC::SetPen( const wxPen& pen )
514 {
515 if (!pen.Ok()) return;
516
517 int oldStyle = m_pen.GetStyle();
518
519 m_pen = pen;
520
521 gnome_print_setlinewidth( m_gpc, XLOG2DEVREL( 1000 * m_pen.GetWidth() ) / 1000.0f );
522
523 unsigned char red = m_pen.GetColour().Red();
524 unsigned char blue = m_pen.GetColour().Blue();
525 unsigned char green = m_pen.GetColour().Green();
526
527 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
528 {
529 double redPS = (double)(red) / 255.0;
530 double bluePS = (double)(blue) / 255.0;
531 double greenPS = (double)(green) / 255.0;
532
533 gnome_print_setrgbcolor( m_gpc, redPS, bluePS, greenPS );
534
535 m_currentRed = red;
536 m_currentBlue = blue;
537 m_currentGreen = green;
538 }
539 }
540
541 void wxGnomePrintDC::SetBrush( const wxBrush& brush )
542 {
543 }
544
545 void wxGnomePrintDC::SetLogicalFunction( int function )
546 {
547 }
548
549 void wxGnomePrintDC::SetBackground( const wxBrush& brush )
550 {
551 }
552
553 void wxGnomePrintDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
554 {
555 }
556
557 void wxGnomePrintDC::DestroyClippingRegion()
558 {
559 }
560
561 bool wxGnomePrintDC::StartDoc(const wxString& message)
562 {
563 SetDeviceOrigin( 0,0 );
564
565 return true;
566 }
567
568 void wxGnomePrintDC::EndDoc()
569 {
570 }
571
572 void wxGnomePrintDC::StartPage()
573 {
574 gnome_print_beginpage( m_gpc, (const guchar*) "1" );
575 }
576
577 void wxGnomePrintDC::EndPage()
578 {
579 gnome_print_showpage( m_gpc );
580 }
581
582 wxCoord wxGnomePrintDC::GetCharHeight() const
583 {
584 return 0;
585 }
586
587 wxCoord wxGnomePrintDC::GetCharWidth() const
588 {
589 return 0;
590 }
591
592 void wxGnomePrintDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
593 wxCoord *descent,
594 wxCoord *externalLeading,
595 wxFont *theFont ) const
596 {
597 }
598
599 void wxGnomePrintDC::DoGetSize(int* width, int* height) const
600 {
601 // No idea if that is efficient
602 GnomePrintConfig *config = gnome_print_config_default();
603
604 double w,h;
605 bool result = gnome_print_config_get_page_size( config, &w, &h );
606
607 if (!result)
608 {
609 // Standard PS resolution DIN A4 size.
610 w = 595.0;
611 h = 842.0;
612 }
613
614 if (width)
615 *width = (int) w;
616 if (height)
617 *height = (int) h;
618
619 wxPrintf( wxT("size %d,%d\n"), *width, *height );
620 }
621
622 void wxGnomePrintDC::DoGetSizeMM(int *width, int *height) const
623 {
624 double w,h;
625
626 /// Later, for now DIN A4
627 w = 210.0;
628 h = 297.0;
629
630 if (width)
631 *width = (int) w;
632 if (height)
633 *height = (int) h;
634 }
635
636 wxSize wxGnomePrintDC::GetPPI() const
637 {
638 return wxSize(72,72);
639 }
640
641 void wxGnomePrintDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
642 {
643 m_signX = (xLeftRight ? 1 : -1);
644 m_signY = (yBottomUp ? 1 : -1);
645
646 ComputeScaleAndOrigin();
647 }
648
649 void wxGnomePrintDC::SetDeviceOrigin( wxCoord x, wxCoord y )
650 {
651 int h = 0;
652 int w = 0;
653 GetSize( &w, &h );
654
655 wxDC::SetDeviceOrigin( x, h-y );
656 }
657
658 void wxGnomePrintDC::SetResolution(int ppi)
659 {
660 }
661
662 int wxGnomePrintDC::GetResolution()
663 {
664 return 72;
665 }
666
667
668 class wxGnomePrintModule: public wxModule
669 {
670 public:
671 wxGnomePrintModule() {}
672 bool OnInit() { wxPrintFactory::SetPrintFactory( new wxGnomePrintFactory ); return true; }
673 void OnExit() { }
674
675 private:
676 DECLARE_DYNAMIC_CLASS(wxGnomePrintModule)
677 };
678
679 IMPLEMENT_DYNAMIC_CLASS(wxGnomePrintModule, wxModule)
680
681 #endif
682 // wxUSE_LIBGNOMEPRINT