Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpf/Mac control printing fixes #2052

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/monomac
2 changes: 2 additions & 0 deletions src/Eto.Mac/Forms/Controls/ButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public Color? Color

public override void DrawBezelWithFrame(CGRect frame, NSView controlView)
{
if (!NSGraphicsContext.IsCurrentContextDrawingToScreen)
return;
colorize?.Begin(frame, controlView);
base.DrawBezelWithFrame(frame, controlView);
colorize?.End();
Expand Down
23 changes: 23 additions & 0 deletions src/Eto.Mac/Forms/Controls/CheckBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ protected override nfloat GetDefaultHeight()
return 10;
}
}

public override void DrawWithFrame(CGRect cellFrame, NSView inView)
{
if (NSGraphicsContext.IsCurrentContextDrawingToScreen)
{
base.DrawWithFrame(cellFrame, inView);
}
else
{
DrawTitle(AttributedTitle, TitleRectForBounds(cellFrame), inView);
var state = State;
var text = state == NSCellStateValue.On ? "☑" : state == NSCellStateValue.Mixed ? "-" : "☐";
var font = NSFont.SystemFontOfSize(NSFont.SystemFontSizeForControlSize(ControlSize));
var attributes = NSDictionary.FromObjectAndKey(font, NSStringAttributeKey.Font);
var str = new NSAttributedString(text, attributes);
var frame = cellFrame;
var size = str.Size;
var offset = (nfloat)Math.Max(0, (frame.Height - size.Height) / 2);
frame.Y += offset;
frame.Height -= offset;
str.DrawString(frame);
}
}
}

public class EtoCheckBoxButton : NSButton, IMacControl
Expand Down
9 changes: 6 additions & 3 deletions src/Eto.Mac/Forms/Controls/DropDownHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ public Color? TextColor

public override void DrawBezelWithFrame(CGRect frame, NSView controlView)
{
colorize?.Begin(frame, controlView);
base.DrawBezelWithFrame(frame, controlView);
colorize?.End();
if (NSGraphicsContext.IsCurrentContextDrawingToScreen)
{
colorize?.Begin(frame, controlView);
base.DrawBezelWithFrame(frame, controlView);
colorize?.End();
}
}

public override CGRect DrawTitle(NSAttributedString title, CGRect frame, NSView controlView)
Expand Down
23 changes: 23 additions & 0 deletions src/Eto.Mac/Forms/Controls/RadioButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ protected override nfloat GetDefaultHeight()
return 10;
}
}

public override void DrawWithFrame(CGRect cellFrame, NSView inView)
{
if (NSGraphicsContext.IsCurrentContextDrawingToScreen)
{
base.DrawWithFrame(cellFrame, inView);
}
else
{
DrawTitle(AttributedTitle, TitleRectForBounds(cellFrame), inView);
var state = State;
var text = state == NSCellStateValue.On ? "⦿" : state == NSCellStateValue.Mixed ? "⊖" : "○";
var font = NSFont.SystemFontOfSize(NSFont.SystemFontSizeForControlSize(ControlSize));
var attributes = NSDictionary.FromObjectAndKey(font, NSStringAttributeKey.Font);
var str = new NSAttributedString(text, attributes);
var frame = cellFrame;
var size = str.Size;
var offset = (nfloat)Math.Max(0, (frame.Height - size.Height) / 2);
frame.Y += offset;
frame.Height -= offset;
str.DrawString(frame);
}
}
}

public class EtoRadioButton : NSButton, IMacControl
Expand Down
14 changes: 3 additions & 11 deletions src/Eto.Mac/Forms/Printing/PrintDialogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public DialogResult ShowDialog(Window parent)
}
else
{
var printInfo = settings.ToNS();
var printInfo = PrintSettings.ToNS();
if (parent != null)
{
var parentHandler = (IMacWindow)parent.Handler;
Expand All @@ -65,16 +65,8 @@ public DialogResult ShowDialog(Window parent)

public PrintSettings PrintSettings
{
get
{
if (settings == null)
settings = Control.PrintInfo.ToEto();
return settings;
}
set
{
settings = value;
}
get => settings ?? (settings = Control.PrintInfo.ToEto() ?? new PrintSettings());
set => settings = value;
}

public bool AllowCopies
Expand Down
38 changes: 23 additions & 15 deletions src/Eto.Mac/Forms/Printing/PrintDocumentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ public void PrintOperationDidRun(IntPtr printOperation, bool success, IntPtr con

public bool Print(bool showPanel, Window parent, NSPrintPanel panel)
{
var op = NSPrintOperation.FromView(Control);
if (printSettings != null)
op.PrintInfo = printSettings.ToNS();
else
PrintSettingsHandler.SetDefaults(op.PrintInfo);
var op = NSPrintOperation.FromView(Control, PrintSettings.ToNS());

(Control as PrintView)?.PrepareForPrint(op);

Expand Down Expand Up @@ -229,28 +225,40 @@ public override void PrepareForPrint(NSPrintOperation operation)
base.PrepareForPrint(operation);

var paperSize = operation.PrintInfo.ImageablePageBounds.Size;
var size = Frame.Size;
var size = _preferredSize.ToNS();

// todo: take into account if width > page size and multiply number of pages as needed.
var scale = (nfloat)Math.Min(1.0, paperSize.Width / size.Width);

_numPages = (int)Math.Ceiling(_preferredSize.Height / paperSize.Height);
operation.PrintInfo.ScalingFactor = scale;

size.Width = (nfloat)Math.Max(size.Width, paperSize.Width);
size.Height = (nfloat)Math.Max(size.Height, paperSize.Height);
_numPages = (int)Math.Ceiling(_preferredSize.Height / (paperSize.Height / scale));

size.Width = (nfloat)(Math.Max(size.Width, paperSize.Width / scale));
size.Height = (nfloat)(Math.Max(size.Height, paperSize.Height / scale * _numPages));
SetFrameSize(size);
UpdateConstraintsForSubtreeIfNeeded();
LayoutSubtreeIfNeeded();

// scale to fit
if (scale < 1)
ScaleUnitSquareToSize(new CGSize(scale, scale));

}

public override CGRect RectForPage(nint pageNumber)
{
var operation = NSPrintOperation.CurrentOperation;
var paperSize = operation.PrintInfo.ImageablePageBounds.Size;
var size = Frame.Size;
size.Width = (nfloat)Math.Min(size.Width, paperSize.Width);
size.Height = (nfloat)Math.Min(size.Height, paperSize.Height);
var location = new CGPoint(0, 0);
location.Y = (nfloat)Math.Max(0, _preferredSize.Height - paperSize.Height * pageNumber);
return new CGRect(location, size);
var frame = Frame;
var scale = operation.PrintInfo.ScalingFactor;

var rect = new CGRect();
rect.Y = (nfloat)Math.Max(0, frame.Height - (paperSize.Height * pageNumber / scale));
rect.Width = (nfloat)(Math.Min(frame.Width, paperSize.Width / scale));
rect.Height = (nfloat)(Math.Min(frame.Height, paperSize.Height / scale));

return rect;
}

public override bool KnowsPageRange(ref NSRange aRange)
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.WinForms/Forms/CursorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void Create(Stream stream)
{
// using Cursor constructor doesn't support 32-bit cursors
// so we save to a temp file and use LoadCursorFromFile.
var tmp = Path.GetTempFileName();
var tmp = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); //Path.GetTempFileName();
try
{
using (var tmpStream = File.Create(tmp))
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Wpf/Forms/Controls/WebView2Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void Progress(double progress)
reportProgress?.Invoke(info);
}
// download bootstrapper to temp folder
var tempFile = Path.GetTempFileName() + ".exe";
var tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) + ".exe";
try
{
info.Text = Loc("Downloading bootstrapper...");
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Wpf/Forms/EnableThemingInScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool EnsureActivateContextCreated()
return contextCreationSucceeded.Value;

// Use a custom manifest from resources and write it to a temp file
var manifestLoc = Path.GetTempFileName();
var manifestLoc = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
try
{
var stream = typeof(EnableThemingInScope).Assembly.GetManifestResourceStream("Eto.Wpf.XPThemes.manifest");
Expand Down
Loading