I'm trying to write a small text editor in wxWidgets, using wxDev-Cpp. So far, all is well. However, for the life of me I can't work out how to print (As in, onto paper). As far as I can deduce, I need to use the wxPrinter class. Browsing the wxWidgets manual, though, proved a fruitless experience, as it might as well be written in chinese! Can anyone give me a simple way of printing the contents of an edit control?

Ben Ward

Dani AI

Generated

Quick practical answer for : treat printing like any other device drawing in wxWidgets — subclass wxPrintout, pull the text from your wxTextCtrl, word-wrap it against the printer DC, compute lines-per-page from the DC metrics, and then draw the correct slice of lines inside OnPrintPage. Below is a minimal pattern that shows the essential pieces (word-wrap, pagination, DrawText). It deliberately keeps layout simple so it’s easy to adapt to your editor.

class TextPrintout : public wxPrintout {
public:
  TextPrintout(const wxString& title, const wxString& text)
    : wxPrintout(title), m_text(text), m_prepared(false) {}

  bool OnPrintPage(int pageNum) override {
    wxDC *dc = GetDC(); if (!dc) return false;
    if (!m_prepared) { prepare(dc); m_prepared = true; }
    if (pageNum < 1 || pageNum > m_pageCount) return false;

    int left = 50, top = 50;
    int lh = dc->GetCharHeight();
    int start = (pageNum - 1) * m_linesPerPage;
    int end = std::min(start + m_linesPerPage, (int)m_lines.size());
    for (int i = start; i < end; ++i)
      dc->DrawText(m_lines[i], left, top + (i - start) * lh);
    return true;
  }

  bool HasPage(int pageNum) override { return pageNum >= 1 && pageNum <= m_pageCount; }
  void GetPageInfo(int *minPage, int *maxPage, int *selFrom, int *selTo) override {
    *minPage = 1; *maxPage = std::max(1, m_pageCount); *selFrom = 1; *selTo = *maxPage;
  }

private:
  wxString m_text; std::vector<wxString> m_lines; bool m_prepared;
  int m_linesPerPage=0, m_pageCount=0;

  void prepare(wxDC *dc) {
    int pageW = dc->GetSize().x - 100;
    wxStringTokenizer para(m_text, "\n");
    while (para.HasMoreTokens()) {
      wxString p = para.GetNextToken();
      wxString cur;
      wxStringTokenizer words(p, " ");
      while (words.HasMoreTokens()) {
        wxString w = words.GetNextToken();
        wxString trial = cur.IsEmpty() ? w : cur + " " + w;
        int tw, th; dc->GetTextExtent(trial, &tw, &th);
        if (tw <= pageW) cur = trial; else { if (!cur.IsEmpty()) m_lines.push_back(cur); cur = w; }
      }
      if (!cur.IsEmpty()) m_lines.push_back(cur);
      else if (p.IsEmpty()) m_lines.push_back(wxEmptyString);
    }
    int usableH = dc->GetSize().y - 100;
    m_linesPerPage = std::max(1, usableH / dc->GetCharHeight());
    m_pageCount = (m_lines.size() + m_linesPerPage - 1) / m_linesPerPage;
  }
};

To start printing: create a TextPrintout with myTextCtrl->GetValue() and call wxPrinter::Print(parent, printout, true) (or use wxPrintPreview for preview). Keep in mind printer DC metrics (DPI/fonts) differ from the screen — measuring is done from the real DC in OnPrintPage.

For and others seeing linker errors like -lwxmsw28: that means your project is asking for a wxWidgets library that the linker cannot find. Ensure the wxWidgets build you have matches your compiler/bitness and add the correct lib folder to the linker path (or rebuild wxWidgets for your toolchain). wxDev-C++ is old; using a modern MinGW-w64 toolchain, Code::Blocks, MSYS2, or Visual Studio (with matching wxWidgets build) usually avoids those mismatches. Also consider the wxWidgets samples that ship with the library — they show real wxPrintout implementations you can adapt.

Recommended Answers

All 4 Replies

I'm trying to write a small text editor in wxWidgets, using wxDev-Cpp. So far, all is well. However, for the life of me I can't work out how to print (As in, onto paper). As far as I can deduce, I need to use the wxPrinter class. Browsing the wxWidgets manual, though, proved a fruitless experience, as it might as well be written in chinese! Can anyone give me a simple way of printing the contents of an edit control?

Ben Ward

When I use wxDevC++, I get a 'can't find -lwxmsw28' error... How did you get it to work?

When I use wxDevC++, I get a 'can't find -lwxmsw28' error... How did you get it to work?

Save yourself some pains and download codelite

Save yourself some pains and download codelite

Has the codelite visual designer for wxWidgets like wxDevC++ ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.