]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/printing/printing.cpp
Make a couple virtuals protected so they can be overridden.
[wxWidgets.git] / samples / printing / printing.cpp
index 8bf639ad09cbfc7312f4da0fe9654f8848d2f34d..ace34b5d7f1c35b80d6e24da3e3bb4fcb45ce63c 100644 (file)
 #endif
 
 #ifdef __WXMAC__
-    #include "wx/mac/printdlg.h"
+    #include "wx/osx/printdlg.h"
 #endif
 
 #include "printing.h"
 
-#ifndef __WXMSW__
-    #include "mondrian.xpm"
+#ifndef wxHAS_IMAGES_IN_RESOURCES
+    #include "../sample.xpm"
 #endif
 
 // Global print data, to remember settings during the session
@@ -117,14 +117,12 @@ bool MyApp::OnInit(void)
     // Create the main frame window
     // ----------------------------
 
-    MyFrame* frame = new MyFrame((wxFrame *) NULL, _T("wxWidgets Printing Demo"),
+    MyFrame* frame = new MyFrame((wxFrame *) NULL, wxT("wxWidgets Printing Demo"),
                                  wxPoint(0, 0), wxSize(400, 400));
 
     frame->Centre(wxBOTH);
     frame->Show();
 
-    SetTopWindow(frame);
-
     return true;
 }
 
@@ -212,7 +210,7 @@ void MyApp::Draw(wxDC&dc)
     str.Printf( wxT("---- Text at angle %d ----"), i );
     dc.DrawRotatedText( str, 100, 300, i );
 
-    wxIcon my_icon = wxICON(mondrian);
+    wxIcon my_icon = wxICON(sample);
 
     dc.DrawIcon( my_icon, 100, 100);
 
@@ -230,11 +228,17 @@ void MyApp::Draw(wxDC&dc)
     if (window_dc)
         gc = wxGraphicsContext::Create( *window_dc );
 
+#ifdef __WXMSW__
+    wxEnhMetaFileDC *emf_dc = wxDynamicCast( &dc, wxEnhMetaFileDC );
+    if (emf_dc)
+        gc = wxGraphicsContext::Create( *emf_dc );
+#endif
+
     if (gc)
     {
         // make a path that contains a circle and some lines, centered at 100,100
         gc->SetPen( *wxRED_PEN );
-        gc->SetFont( m_testFont, *wxGREEN );
+
         wxGraphicsPath path = gc->CreatePath();
         path.AddCircle( 50.0, 50.0, 50.0 );
         path.MoveToPoint(0.0, 50.0);
@@ -246,6 +250,17 @@ void MyApp::Draw(wxDC&dc)
 
         gc->StrokePath(path);
 
+        // draw some text
+        wxString text("Text by wxGraphicsContext");
+        gc->SetFont( m_testFont, *wxBLACK );
+        gc->DrawText(text, 25.0, 60.0);
+
+        // draw rectangle around the text
+        double w, h, d, el;
+        gc->GetTextExtent(text, &w, &h, &d, &el);
+        gc->SetPen( *wxBLACK_PEN );
+        gc->DrawRectangle(25.0, 60.0, w, h);
+
         delete gc;
     }
 #endif
@@ -272,32 +287,42 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
 #endif
     EVT_MENU(WXPRINT_ANGLEUP, MyFrame::OnAngleUp)
     EVT_MENU(WXPRINT_ANGLEDOWN, MyFrame::OnAngleDown)
+
+    EVT_MENU_RANGE(WXPRINT_FRAME_MODAL_APP,
+                   WXPRINT_FRAME_MODAL_NON,
+                   MyFrame::OnPreviewFrameModalityKind)
 END_EVENT_TABLE()
 
 MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const wxSize&size)
         : wxFrame(frame, wxID_ANY, title, pos, size)
 {
     m_canvas = NULL;
+    m_previewModality = wxPreviewFrame_AppModal;
 
 #if wxUSE_STATUSBAR
     // Give us a status line
     CreateStatusBar(2);
-    SetStatusText(_T("Printing demo"));
+    SetStatusText(wxT("Printing demo"));
 #endif // wxUSE_STATUSBAR
 
     // Load icon and bitmap
-    SetIcon( wxICON( mondrian) );
+    SetIcon( wxICON( sample) );
 
     // Make a menubar
     wxMenu *file_menu = new wxMenu;
 
-    file_menu->Append(wxID_PRINT, _T("&Print..."),                 _T("Print"));
-    file_menu->Append(WXPRINT_PAGE_SETUP, _T("Page Set&up..."),    _T("Page setup"));
+    file_menu->Append(wxID_PRINT, wxT("&Print..."),                 wxT("Print"));
+    file_menu->Append(WXPRINT_PAGE_SETUP, wxT("Page Set&up..."),    wxT("Page setup"));
 #ifdef __WXMAC__
-    file_menu->Append(WXPRINT_PAGE_MARGINS, _T("Page Margins..."), _T("Page margins"));
+    file_menu->Append(WXPRINT_PAGE_MARGINS, wxT("Page Margins..."), wxT("Page margins"));
 #endif
-    file_menu->Append(wxID_PREVIEW, _T("Print Pre&view"),          _T("Preview"));
+    file_menu->Append(wxID_PREVIEW, wxT("Print Pre&view"),          wxT("Preview"));
 
+    wxMenu * const menuModalKind = new wxMenu;
+    menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_APP, "&App modal");
+    menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_WIN, "&Window modal");
+    menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_NON, "&Not modal");
+    file_menu->AppendSubMenu(menuModalKind, "Preview frame &modal kind");
 #if wxUSE_ACCEL
     // Accelerators
     wxAcceleratorEntry entries[1];
@@ -308,24 +333,24 @@ MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const
 
 #if defined(__WXMSW__) &&wxTEST_POSTSCRIPT_IN_MSW
     file_menu->AppendSeparator();
-    file_menu->Append(WXPRINT_PRINT_PS, _T("Print PostScript..."),           _T("Print (PostScript)"));
-    file_menu->Append(WXPRINT_PAGE_SETUP_PS, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
-    file_menu->Append(WXPRINT_PREVIEW_PS, _T("Print Preview PostScript"),    _T("Preview (PostScript)"));
+    file_menu->Append(WXPRINT_PRINT_PS, wxT("Print PostScript..."),           wxT("Print (PostScript)"));
+    file_menu->Append(WXPRINT_PAGE_SETUP_PS, wxT("Page Setup PostScript..."), wxT("Page setup (PostScript)"));
+    file_menu->Append(WXPRINT_PREVIEW_PS, wxT("Print Preview PostScript"),    wxT("Preview (PostScript)"));
 #endif
 
     file_menu->AppendSeparator();
-    file_menu->Append(WXPRINT_ANGLEUP, _T("Angle up\tAlt-U"),                _T("Raise rotated text angle"));
-    file_menu->Append(WXPRINT_ANGLEDOWN, _T("Angle down\tAlt-D"),            _T("Lower rotated text angle"));
+    file_menu->Append(WXPRINT_ANGLEUP, wxT("Angle up\tAlt-U"),                wxT("Raise rotated text angle"));
+    file_menu->Append(WXPRINT_ANGLEDOWN, wxT("Angle down\tAlt-D"),            wxT("Lower rotated text angle"));
     file_menu->AppendSeparator();
-    file_menu->Append(wxID_EXIT, _T("E&xit"),                                _T("Exit program"));
+    file_menu->Append(wxID_EXIT, wxT("E&xit"),                                wxT("Exit program"));
 
     wxMenu *help_menu = new wxMenu;
-    help_menu->Append(wxID_ABOUT, _T("&About"),                              _T("About this demo"));
+    help_menu->Append(wxID_ABOUT, wxT("&About"),                              wxT("About this demo"));
 
     wxMenuBar *menu_bar = new wxMenuBar;
 
-    menu_bar->Append(file_menu, _T("&File"));
-    menu_bar->Append(help_menu, _T("&Help"));
+    menu_bar->Append(file_menu, wxT("&File"));
+    menu_bar->Append(help_menu, wxT("&Help"));
 
     // Associate the menu bar with the frame
     SetMenuBar(menu_bar);
@@ -351,13 +376,17 @@ void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
     wxPrintDialogData printDialogData(* g_printData);
 
     wxPrinter printer(&printDialogData);
-    MyPrintout printout(this, _T("My printout"));
+    MyPrintout printout(this, wxT("My printout"));
     if (!printer.Print(this, &printout, true /*prompt*/))
     {
         if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
-            wxLogError(_T("There was a problem printing. Perhaps your current printer is not set correctly?"));
+        {
+            wxLogError(wxT("There was a problem printing. Perhaps your current printer is not set correctly?"));
+        }
         else
-            wxLogMessage(_T("You canceled printing"));
+        {
+            wxLogMessage(wxT("You canceled printing"));
+        }
     }
     else
     {
@@ -374,14 +403,14 @@ void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
     if (!preview->IsOk())
     {
         delete preview;
-        wxLogError(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"));
+        wxLogError(wxT("There was a problem previewing.\nPerhaps your current printer is not set correctly?"));
         return;
     }
 
     wxPreviewFrame *frame =
-        new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
+        new wxPreviewFrame(preview, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
     frame->Centre(wxBOTH);
-    frame->Initialize();
+    frame->InitializeWithModality(m_previewModality);
     frame->Show();
 }
 
@@ -400,7 +429,7 @@ void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
 {
     wxPostScriptPrinter printer(g_printData);
-    MyPrintout printout(_T("My printout"));
+    MyPrintout printout(wxT("My printout"));
     printer.Print(this, &printout, true/*prompt*/);
 
     (*g_printData) = printer.GetPrintData();
@@ -412,7 +441,7 @@ void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
     wxPrintDialogData printDialogData(* g_printData);
     wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, &printDialogData);
     wxPreviewFrame *frame =
-        new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
+        new wxPreviewFrame(preview, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
     frame->Centre(wxBOTH);
     frame->Initialize();
     frame->Show();
@@ -445,8 +474,8 @@ void MyFrame::OnPageMargins(wxCommandEvent& WXUNUSED(event))
 
 void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
 {
-    (void)wxMessageBox(_T("wxWidgets printing demo\nAuthor: Julian Smart"),
-                       _T("About wxWidgets printing demo"), wxOK|wxCENTRE);
+    (void)wxMessageBox(wxT("wxWidgets printing demo\nAuthor: Julian Smart"),
+                       wxT("About wxWidgets printing demo"), wxOK|wxCENTRE);
 }
 
 void MyFrame::OnAngleUp(wxCommandEvent& WXUNUSED(event))
@@ -461,6 +490,12 @@ void MyFrame::OnAngleDown(wxCommandEvent& WXUNUSED(event))
     m_canvas->Refresh();
 }
 
+void MyFrame::OnPreviewFrameModalityKind(wxCommandEvent& event)
+{
+    m_previewModality = static_cast<wxPreviewFrameModalityKind>(
+                            wxPreviewFrame_AppModal +
+                                (event.GetId() - WXPRINT_FRAME_MODAL_APP));
+}
 
 // ----------------------------------------------------------------------------
 // MyCanvas
@@ -623,7 +658,7 @@ void MyPrintout::DrawPageTwo()
     int ppiPrinterX, ppiPrinterY;
     GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
 
-    // This scales the DC so that the printout roughly represents the the screen
+    // This scales the DC so that the printout roughly represents the screen
     // scaling. The text point size _should_ be the right size but in fact is
     // too small for some reason. This is a detail that will need to be
     // addressed at some point but can be fudged for the moment.
@@ -658,8 +693,8 @@ void MyPrintout::DrawPageTwo()
     dc->SetBrush(*wxTRANSPARENT_BRUSH);
 
     { // GetTextExtent demo:
-        wxString words[7] = { _T("This "), _T("is "), _T("GetTextExtent "),
-                             _T("testing "), _T("string. "), _T("Enjoy "), _T("it!") };
+        wxString words[7] = { wxT("This "), wxT("is "), wxT("GetTextExtent "),
+                             wxT("testing "), wxT("string. "), wxT("Enjoy "), wxT("it!") };
         wxCoord w, h;
         long x = 200, y= 250;
         wxFont fnt(15, wxSWISS, wxNORMAL, wxNORMAL);
@@ -681,7 +716,7 @@ void MyPrintout::DrawPageTwo()
 
     dc->SetFont(wxGetApp().GetTestFont());
 
-    dc->DrawText(_T("Some test text"), 200, 300 );
+    dc->DrawText(wxT("Some test text"), 200, 300 );
 
     // TESTING
 
@@ -704,7 +739,7 @@ void MyPrintout::DrawPageTwo()
     dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
         (long)rightMarginLogical,  (long)bottomMarginLogical);
 
-    WritePageHeader(this, dc, _T("A header"), logUnitsFactor);
+    WritePageHeader(this, dc, wxT("A header"), logUnitsFactor);
 }
 
 // Writes a header on a page. Margin units are in millimetres.