Skip to content

Commit

Permalink
Enable nullability in PrintDialog (dotnet#6591)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetrou committed Feb 2, 2022
1 parent 5413548 commit 37ebd49
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 59 deletions.
8 changes: 4 additions & 4 deletions src/System.Windows.Forms/src/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2247,10 +2247,10 @@ System.Windows.Forms.ListControl.ValueMember.set -> void
~System.Windows.Forms.PictureBox.LoadAsync(string url) -> void
~System.Windows.Forms.PrintControllerWithStatusDialog.PrintControllerWithStatusDialog(System.Drawing.Printing.PrintController underlyingController) -> void
~System.Windows.Forms.PrintControllerWithStatusDialog.PrintControllerWithStatusDialog(System.Drawing.Printing.PrintController underlyingController, string dialogTitle) -> void
~System.Windows.Forms.PrintDialog.Document.get -> System.Drawing.Printing.PrintDocument
~System.Windows.Forms.PrintDialog.Document.set -> void
~System.Windows.Forms.PrintDialog.PrinterSettings.get -> System.Drawing.Printing.PrinterSettings
~System.Windows.Forms.PrintDialog.PrinterSettings.set -> void
System.Windows.Forms.PrintDialog.Document.get -> System.Drawing.Printing.PrintDocument?
System.Windows.Forms.PrintDialog.Document.set -> void
System.Windows.Forms.PrintDialog.PrinterSettings.get -> System.Drawing.Printing.PrinterSettings!
System.Windows.Forms.PrintDialog.PrinterSettings.set -> void
~System.Windows.Forms.PrintPreviewControl.Document.get -> System.Drawing.Printing.PrintDocument
~System.Windows.Forms.PrintPreviewControl.Document.set -> void
~System.Windows.Forms.PrintPreviewDialog.AcceptButton.get -> System.Windows.Forms.IButtonControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using static Interop;
Expand All @@ -25,18 +24,18 @@ public sealed class PrintDialog : CommonDialog
private const PD printRangeMask = PD.ALLPAGES | PD.PAGENUMS | PD.SELECTION | PD.CURRENTPAGE;

// If PrintDocument is not null, settings == printDocument.PrinterSettings
private PrinterSettings settings;
private PrintDocument printDocument;
private PrinterSettings? _printerSettings;
private PrintDocument? _printDocument;

// Implementing "current page" would require switching to PrintDlgEx, which is windows 2000 and later only
private bool allowCurrentPage;
private bool _allowCurrentPage;

private bool allowPages;
private bool allowPrintToFile;
private bool allowSelection;
private bool printToFile;
private bool showHelp;
private bool showNetwork;
private bool _allowPages;
private bool _allowPrintToFile;
private bool _allowSelection;
private bool _printToFile;
private bool _showHelp;
private bool _showNetwork;

/// <summary>
/// Initializes a new instance of the <see cref="PrintDialog"/> class.
Expand All @@ -53,8 +52,8 @@ public PrintDialog()
[SRDescription(nameof(SR.PDallowCurrentPageDescr))]
public bool AllowCurrentPage
{
get { return allowCurrentPage; }
set { allowCurrentPage = value; }
get { return _allowCurrentPage; }
set { _allowCurrentPage = value; }
}

/// <summary>
Expand All @@ -65,8 +64,8 @@ public bool AllowCurrentPage
[SRDescription(nameof(SR.PDallowPagesDescr))]
public bool AllowSomePages
{
get { return allowPages; }
set { allowPages = value; }
get { return _allowPages; }
set { _allowPages = value; }
}

/// <summary>
Expand All @@ -77,8 +76,8 @@ public bool AllowSomePages
[SRDescription(nameof(SR.PDallowPrintToFileDescr))]
public bool AllowPrintToFile
{
get { return allowPrintToFile; }
set { allowPrintToFile = value; }
get { return _allowPrintToFile; }
set { _allowPrintToFile = value; }
}

/// <summary>
Expand All @@ -89,8 +88,8 @@ public bool AllowPrintToFile
[SRDescription(nameof(SR.PDallowSelectionDescr))]
public bool AllowSelection
{
get { return allowSelection; }
set { allowSelection = value; }
get { return _allowSelection; }
set { _allowSelection = value; }
}

/// <summary>
Expand All @@ -99,19 +98,19 @@ public bool AllowSelection
[SRCategory(nameof(SR.CatData))]
[DefaultValue(null)]
[SRDescription(nameof(SR.PDdocumentDescr))]
public PrintDocument Document
public PrintDocument? Document
{
get { return printDocument; }
get { return _printDocument; }
set
{
printDocument = value;
if (printDocument is null)
_printDocument = value;
if (_printDocument is null)
{
settings = new PrinterSettings();
_printerSettings = new PrinterSettings();
}
else
{
settings = printDocument.PrinterSettings;
_printerSettings = _printDocument.PrinterSettings;
}
}
}
Expand Down Expand Up @@ -140,23 +139,24 @@ private PageSettings PageSettings
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.PDprinterSettingsDescr))]
[AllowNull]
public PrinterSettings PrinterSettings
{
get
{
if (settings is null)
if (_printerSettings is null)
{
settings = new PrinterSettings();
_printerSettings = new PrinterSettings();
}

return settings;
return _printerSettings;
}
set
{
if (value != PrinterSettings)
{
settings = value;
printDocument = null;
_printerSettings = value;
_printDocument = null;
}
}
}
Expand All @@ -169,8 +169,8 @@ public PrinterSettings PrinterSettings
[SRDescription(nameof(SR.PDprintToFileDescr))]
public bool PrintToFile
{
get { return printToFile; }
set { printToFile = value; }
get { return _printToFile; }
set { _printToFile = value; }
}

/// <summary>
Expand All @@ -181,8 +181,8 @@ public bool PrintToFile
[SRDescription(nameof(SR.PDshowHelpDescr))]
public bool ShowHelp
{
get { return showHelp; }
set { showHelp = value; }
get { return _showHelp; }
set { _showHelp = value; }
}

/// <summary>
Expand All @@ -193,8 +193,8 @@ public bool ShowHelp
[SRDescription(nameof(SR.PDshowNetworkDescr))]
public bool ShowNetwork
{
get { return showNetwork; }
set { showNetwork = value; }
get { return _showNetwork; }
set { _showNetwork = value; }
}

/// <summary>
Expand All @@ -218,39 +218,39 @@ private PD GetFlags()
flags |= PD.ENABLEPRINTHOOK;
}

if (!allowCurrentPage)
if (!_allowCurrentPage)
{
flags |= PD.NOCURRENTPAGE;
}

if (!allowPages)
if (!_allowPages)
{
flags |= PD.NOPAGENUMS;
}

if (!allowPrintToFile)
if (!_allowPrintToFile)
{
flags |= PD.DISABLEPRINTTOFILE;
}

if (!allowSelection)
if (!_allowSelection)
{
flags |= PD.NOSELECTION;
}

flags |= (PD)PrinterSettings.PrintRange;

if (printToFile)
if (_printToFile)
{
flags |= PD.PRINTTOFILE;
}

if (showHelp)
if (_showHelp)
{
flags |= PD.SHOWHELP;
}

if (!showNetwork)
if (!_showNetwork)
{
flags |= PD.NONETWORKBUTTON;
}
Expand All @@ -269,15 +269,15 @@ private PD GetFlags()
/// </summary>
public override void Reset()
{
allowCurrentPage = false;
allowPages = false;
allowPrintToFile = true;
allowSelection = false;
printDocument = null;
printToFile = false;
settings = null;
showHelp = false;
showNetwork = true;
_allowCurrentPage = false;
_allowPages = false;
_allowPrintToFile = true;
_allowSelection = false;
_printDocument = null;
_printToFile = false;
_printerSettings = null;
_showHelp = false;
_showNetwork = true;
}

internal unsafe static NativeMethods.PRINTDLGEX CreatePRINTDLGEX()
Expand Down Expand Up @@ -400,7 +400,7 @@ private unsafe bool ShowPrintDialog(IntPtr hwndOwner)
return false;
}

UpdatePrinterSettings(data.hDevMode, data.hDevNames, (short)data.nCopies, data.Flags, settings, PageSettings);
UpdatePrinterSettings(data.hDevMode, data.hDevNames, (short)data.nCopies, data.Flags, _printerSettings!, PageSettings);

PrintToFile = (data.Flags & PD.PRINTTOFILE) != 0;
PrinterSettings.PrintToFile = PrintToFile;
Expand Down Expand Up @@ -495,7 +495,6 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.PRINTDLGEX data)
data.nMaxPage = PrinterSettings.MaximumPage;
}

//
// The flags NativeMethods.PD_SHOWHELP and NativeMethods.PD_NONETWORKBUTTON don't work with
// PrintDlgEx. So we have to strip them out.
data.Flags &= ~(PD.SHOWHELP | PD.NONETWORKBUTTON);
Expand Down Expand Up @@ -556,7 +555,7 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.PRINTDLGEX data)
// Due to the nature of PRINTDLGEX vs PRINTDLG, separate but similar methods
// are required for updating the settings from the structure utilized by the dialog.
// Take information from print dialog and put in PrinterSettings
private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, short copies, PD flags, PrinterSettings settings, PageSettings pageSettings)
private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, short copies, PD flags, PrinterSettings settings, PageSettings? pageSettings)
{
// Mode
settings.SetHdevmode(hDevMode);
Expand Down

0 comments on commit 37ebd49

Please sign in to comment.