Update of /cvsroot/mahogany/M/src/modules
In directory sc8-pr-cvs1:/tmp/cvs-serv19252/src/modules

Modified Files:
        TextViewer.cpp 
Log Message:
implemented printing in text viewer (bug 812)

Index: TextViewer.cpp
===================================================================
RCS file: /cvsroot/mahogany/M/src/modules/TextViewer.cpp,v
retrieving revision 1.48
retrieving revision 1.49
diff -b -u -2 -r1.48 -r1.49
--- TextViewer.cpp      18 Sep 2003 16:31:02 -0000      1.48
+++ TextViewer.cpp      2 Oct 2003 22:54:26 -0000       1.49
@@ -34,5 +34,5 @@
 #include <wx/textbuf.h>
 
-class TextViewerWindow;
+#include <wx/html/htmprint.h>   // for wxHtmlEasyPrinting
 
 #ifdef __WXMSW__
@@ -47,4 +47,23 @@
 //#define USE_AUTO_URL_DETECTION
 
+class TextViewerWindow;
+
+// ----------------------------------------------------------------------------
+// wxTextEasyPrinting: easy way to print text (TODO: move to wxWindows)
+// ----------------------------------------------------------------------------
+
+class wxTextEasyPrinting : public wxHtmlEasyPrinting
+{
+public:
+   wxTextEasyPrinting(const wxString& name, wxWindow *parent = NULL)
+      : wxHtmlEasyPrinting(name, parent) { }
+
+   bool Print(wxTextCtrl *text) { return PrintText(ControlToHtml(text)); }
+   bool Preview(wxTextCtrl *text) { return PreviewText(ControlToHtml(text)); }
+
+private:
+   static wxString ControlToHtml(wxTextCtrl *text);
+};
+
 // ----------------------------------------------------------------------------
 // TextViewer: a wxTextCtrl-based MessageViewer implementation
@@ -116,7 +135,14 @@
 
 private:
+   // create m_printText if necessary
+   void InitPrinting();
+
+
    // the viewer window
    TextViewerWindow *m_window;
 
+   // the object which does the printing
+   wxTextEasyPrinting *m_printText;
+
    // the position of the last match used by Find() and FindAgain()
    long m_posFind;
@@ -205,4 +231,100 @@
 
 // ============================================================================
+// wxTextEasyPrinting implementation
+// ============================================================================
+
+/* static */
+wxString wxTextEasyPrinting::ControlToHtml(wxTextCtrl *text)
+{
+   wxCHECK_MSG( text, wxEmptyString, _T("NULL control in wxTextEasyPrinting") );
+
+   const long posEnd = text->GetLastPosition();
+
+   wxString s;
+   s.reserve(posEnd + 100);
+
+   s << _T("<html><body><tt>");
+
+   wxString ch;
+   wxTextAttr attr, attrNew;
+   for ( long pos = 0; pos < posEnd; pos++ )
+   {
+      ch = text->GetRange(pos, pos + 1);
+
+      // this is not implemented in wxGTK and doesn't want to work under MSW
+      // for some reason (TODO: debug it)
+#if 0
+      if ( text->GetStyle(pos, attrNew) )
+      {
+         bool changed = false;
+         if ( attrNew.GetTextColour() != attr.GetTextColour() )
+         {
+            if ( attrNew.HasTextColour() )
+            {
+               s << _T("<font color=\"#")
+                 << Col2Html(attrNew.GetTextColour())
+                 << _T("\">");
+            }
+            else
+            {
+               s << _T("</font>");
+            }
+
+            changed = true;
+         }
+
+         if ( changed )
+         {
+            attr = attrNew;
+         }
+      }
+#endif // 0
+
+      switch ( ch[0u] )
+      {
+         case '<':
+            s += "&lt;";
+            break;
+
+         case '>':
+            s += "&gt;";
+            break;
+
+         case '&':
+            s += "&amp;";
+            break;
+
+         case '"':
+            s += "&quot;";
+            break;
+
+         case '\t':
+            // we hardcode the tab width to 8 spaces
+            s += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
+            break;
+
+         case '\r':
+            // ignore, we process '\n' below
+            break;
+
+         case '\n':
+            s += "<br>\n";
+            break;
+
+         case ' ':
+            s += "&nbsp;";
+            break;
+
+         default:
+            s += ch[0u];
+      }
+   }
+
+   s << _T("</tt></body></html>");
+
+   return s;
+}
+
+// ============================================================================
 // TextViewerWindow implementation
 // ============================================================================
@@ -372,4 +494,5 @@
 {
    m_window = NULL;
+   m_printText = NULL;
    m_posFind = -1;
 }
@@ -463,16 +586,30 @@
 // ----------------------------------------------------------------------------
 
+void TextViewer::InitPrinting()
+{
+   if ( !m_printText )
+   {
+      m_printText = new wxTextEasyPrinting(_("Mahogany Printing"),
+                                           GetFrame(m_window));
+   }
+
+   wxMApp *app = (wxMApp *)mApplication;
+
+   *m_printText->GetPrintData() = *app->GetPrintData();
+   *m_printText->GetPageSetupData() = *app->GetPageSetupData();
+}
+
 bool TextViewer::Print()
 {
-   // just to give the error message...
-   PrintPreview();
+   InitPrinting();
 
-   return false;
+   return m_printText->Print(m_window);
 }
 
 void TextViewer::PrintPreview()
 {
-   wxLogError(_("Sorry, printing is not supported by the text viewer.\n"
-                "Please change to another viewer to print this message."));
+   InitPrinting();
+
+   (void)m_printText->Preview(m_window);
 }
 



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Mahogany-cvsupdates mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/mahogany-cvsupdates

Reply via email to