Skip to content

Commit

Permalink
Use print settings from print page setup tab (GTK text window)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake-Madden committed Sep 18, 2023
1 parent 8bcf726 commit fa317ff
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 38 deletions.
50 changes: 23 additions & 27 deletions src/ui/controls/formattedtextctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,39 +388,37 @@ void FormattedTextCtrl::OnPrint([[maybe_unused]] wxCommandEvent& event)
wxDELETE(dc);
wxDELETE(printOut);
#elif defined(__WXGTK__)
// UNDER CONSTRUCTION!!!
if (m_printData)
{
SetPrintOrientation(m_printData->GetOrientation());
if (m_printData->GetPaperId() == wxPAPER_NONE)
{
SetPaperSizeInMillimeters(m_printData->GetPaperSize());
}
else
{
/* values in here are hard coded, so a little more precise than
converting from millimeters to twips*/
SetPaperSize(m_printData->GetPaperId());
}
}
const wxSize paperSize = wxThePrintPaperDatabase->GetSize(m_printData->GetPaperId());
const double paperWidthInInches = (paperSize.GetWidth() / 10) * 0.0393700787;
const double paperHeightInInches = (paperSize.GetHeight() / 10) * 0.0393700787;

GtkPrintOperation* operation = gtk_print_operation_new();

GtkPrintSettings* settings = gtk_print_settings_new();
gtk_print_settings_set_paper_width(settings, paperWidthInInches, GTK_UNIT_INCH);
gtk_print_settings_set_paper_height(settings, paperHeightInInches, GTK_UNIT_INCH);

if (m_printData)
{
gtk_print_settings_set_orientation(settings,
(m_printData->GetOrientation() == wxLANDSCAPE ?
GTK_PAGE_ORIENTATION_LANDSCAPE : GTK_PAGE_ORIENTATION_PORTRAIT));
gtk_print_settings_set_n_copies(settings, m_printData->GetNoCopies());

GtkPaperSize* paperSize = _GtkGetPaperSize(m_printData->GetPaperId(), m_printData->GetPaperSize());
gtk_print_settings_set_paper_size(settings, paperSize);
gtk_paper_size_free(paperSize);
}
gtk_print_operation_set_print_settings(operation, settings);

// page setup tab
GtkPageSetup* pgSetup = gtk_page_setup_new();
gtk_page_setup_set_orientation(pgSetup, gtk_print_settings_get_orientation(settings));

GtkPaperSize* paper_size = gtk_print_settings_get_paper_size(settings);
if (paper_size != nullptr)
{
gtk_page_setup_set_paper_size(pgSetup, paper_size);
gtk_paper_size_free(paper_size);
}
gtk_print_operation_set_default_page_setup(operation, pgSetup);
gtk_print_operation_set_embed_page_setup(operation, TRUE);
g_object_unref(pgSetup);

_GtkPrintData printData;
printData.m_markupContent = GetUnthemedFormattedText().utf8_string();

Expand All @@ -447,10 +445,8 @@ void FormattedTextCtrl::OnPrint([[maybe_unused]] wxCommandEvent& event)
if (settings != nullptr)
{ g_object_unref(settings); }
settings = g_object_ref(gtk_print_operation_get_print_settings(operation));
m_printData->SetNoCopies(gtk_print_settings_get_n_copies(settings));
m_printData->SetOrientation(
(gtk_print_settings_get_orientation(settings) == GTK_PAGE_ORIENTATION_LANDSCAPE) ?
wxLANDSCAPE : wxPORTRAIT);

_GtkUpdatePrintSettingsFromPageSetup(operation, settings, m_printData);
}
else if (error)
{
Expand All @@ -477,8 +473,8 @@ void FormattedTextCtrl::OnPrint([[maybe_unused]] wxCommandEvent& event)
wxCoord textWidth{ 0 }, textHeight{ 0 };
dc.GetTextExtent(L" ", &textWidth, &textHeight);
const size_t spacesCount = (m_printData->GetOrientation() == wxPORTRAIT)?
safe_divide<size_t>((PaperWidthInInches- .5f) * 72, textWidth) :
safe_divide<size_t>((PaperHeightInInches- .5f) * 72, textWidth);
safe_divide<size_t>((PaperWidthInInches - .5f) * 72, textWidth) :
safe_divide<size_t>((PaperHeightInInches - .5f) * 72, textWidth);

// format the header
wxString expandedLeftHeader = ExpandUnixPrintString(GetLeftPrinterHeader());
Expand Down
8 changes: 8 additions & 0 deletions src/ui/controls/formattedtextctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ class FormattedTextCtrl final : public wxTextCtrl
/// @brief Sets the paper size.
/// @param size The paper size to use.
void SetPaperSize(const wxPaperSize size);
/// @brief Sets the paper size.
/// @param size The size to use.
void SetPaperSize(const wxSize size)
{ m_paperSize = size; }
/// @returns The paper size.
[[nodiscard]]
wxSize GetPaperSize() const noexcept
{ return m_paperSize; }
/// @returns The printable page's rectangle (including margins).
[[nodiscard]]
wxRect GetPageRect() const
Expand Down
18 changes: 11 additions & 7 deletions src/ui/controls/gtk/gtktextview-helper.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
///////////////////////////////////////////////////////////////////////////////
// Name: gtktextview-helper.cpp
// Author: Blake Madden
// Copyright: (c) 2005-2023 Blake Madden
// Licence: 3-Clause BSD licence
// SPDX-License-Identifier: BSD-3-Clause
///////////////////////////////////////////////////////////////////////////////
/** @date 2005-2023
@copyright Blake Madden
@author Blake Madden
Tim-Philipp Mueller (Gtk+ bug report patch for GtkTextView Pango functionality)
Anthony Bretaudeau (portions of wxWidgets printing code)
@details This program is free software; you can redistribute it and/or modify
it under the terms of the 3-Clause BSD License (some portions are wxWindows licensed, where noted).
SPDX-License-Identifier: BSD-3-Clause
SPDX-License-Identifier: wxWindows
@{*/

#include <algorithm>
#include "gtktextview-helper.h"
Expand Down
Loading

0 comments on commit fa317ff

Please sign in to comment.