private void PopulateMargins()
        {
            PageMargin[] margins = PrintData.PageMargins;
            Thickness?   custom  = Settings.PrintMarginCustom;

            if (custom.HasValue)
            {
                PageMargin[] resized = new PageMargin[margins.Length + 1];
                margins.CopyTo(resized, 1);
                resized[0] = new PageMargin("Custom", custom.Value);
                margins    = resized;
            }

            foreach (PageMargin each in margins)
            {
                BackstageComboBoxItem item = new BackstageComboBoxItem();
                item.Header = each.Description;

                Thickness margin = each.Margin;
                item.Description = "Top:\t" + margin.Top.ToString()
                                   + "\"\tBottom:\t" + margin.Bottom.ToString()
                                   + "\"\nLeft:\t" + margin.Left.ToString()
                                   + "\"\tRight:\t" + margin.Right.ToString() + "\"";
                item.Image = GetMarginIcon(margin);

                item.Tag = each;

                marginCombo.Items.Add(item);
            }

            marginCombo.SelectedIndex = Settings.PrintMargin;
        }
        private void PopulatePaperSizes()
        {
            PaperSize[] sizes  = PrintData.PaperSizes;
            Size?       custom = Settings.PrintPaperSizeCustom;

            if (custom.HasValue)
            {
                PaperSize[] resized = new PaperSize[sizes.Length + 1];
                sizes.CopyTo(resized, 1);
                resized[0] = new PaperSize("Custom", custom.Value);
                sizes      = resized;
            }

            foreach (PaperSize each in sizes)
            {
                BackstageComboBoxItem item = new BackstageComboBoxItem();
                item.Header = each.Description;

                Size size = each.Size;
                item.Description = size.Width.ToString() + "\" x " + size.Height.ToString() + "\"";
                item.Image       = GetPaperIcon(size);

                item.Tag = each;

                paperSizeCombo.Items.Add(item);
            }

            paperSizeCombo.SelectedIndex = Settings.PrintPaperSize;
        }
        private void customMargin_Click(object sender, RoutedEventArgs e)
        {
            marginCombo.IsDropDownOpen = false;

            CustomMarginDialog margin = new CustomMarginDialog();

            margin.Owner = Window.GetWindow(this);
            if (margin.ShowDialog() != true)
            {
                return;
            }

            Thickness custom = margin.SelectedMargin;

            Settings.PrintMarginCustom = custom;

            // In case "Custom" is already selected, this value will be true.
            bool needsPreviewRefresh = false;

            BackstageComboBoxItem item = (BackstageComboBoxItem)marginCombo.Items[0];

            if (item.Header != "Custom")
            {
                item        = new BackstageComboBoxItem();
                item.Header = "Custom";

                marginCombo.Items.Insert(0, item);
            }
            else if (item.IsSelected)
            {
                needsPreviewRefresh = true;
            }

            item.Description = "Top:\t" + custom.Top.ToString()
                               + "\"\tBottom:\t" + custom.Bottom.ToString()
                               + "\"\nLeft:\t" + custom.Left.ToString()
                               + "\"\tRight:\t" + custom.Right.ToString() + "\"";
            item.Image = GetMarginIcon(custom);
            item.Tag   = new PageMargin("Custom", custom);

            item.IsSelected = true;

            if (needsPreviewRefresh)
            {
                ShowPreview();
            }
        }
        private void customSize_Click(object sender, RoutedEventArgs e)
        {
            paperSizeCombo.IsDropDownOpen = false;

            CustomPaperSizeDialog paperSize = new CustomPaperSizeDialog();

            paperSize.Owner = Window.GetWindow(this);
            if (paperSize.ShowDialog() != true)
            {
                return;
            }

            Size custom = paperSize.SelectedSize;

            Settings.PrintPaperSizeCustom = custom;

            // In case "Custom" is already selected, this value will be true.
            bool needsPreviewRefresh = false;

            BackstageComboBoxItem item = (BackstageComboBoxItem)paperSizeCombo.Items[0];

            if (item.Header != "Custom")
            {
                item        = new BackstageComboBoxItem();
                item.Header = "Custom";

                paperSizeCombo.Items.Insert(0, item);
            }
            else if (item.IsSelected)
            {
                needsPreviewRefresh = true;
            }

            item.Description = custom.Width.ToString() + "\" x " + custom.Height.ToString() + "\"";
            item.Image       = GetPaperIcon(custom);
            item.Tag         = new PaperSize("Custom", custom);

            item.IsSelected = true;

            if (needsPreviewRefresh)
            {
                ShowPreview();
            }
        }
        private void PopulatePrinters()
        {
            bool enablePrinting = false;

            try
            {
                PrintQueueCollection printers          = Printers.GetPrinters();
                PrintQueue           defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();
                string defaultPrinter = defaultPrintQueue.HostingPrintServer.Name + "\\" + defaultPrintQueue.Description;

                List <BackstageComboBoxItem> items = new List <BackstageComboBoxItem>();

                foreach (PrintQueue each in printers)
                {
                    BackstageComboBoxItem item = new BackstageComboBoxItem();
                    item.Tag         = each;
                    item.Header      = each.Name;
                    item.Description = GetFriendlyQueueStatus(each.QueueStatus) +
                                       (each.NumberOfJobs > 0 ? " (" + each.NumberOfJobs.ToString() + " document" +
                                        (each.NumberOfJobs != 1 ? "s" : "") + " waiting)" : "");
                    item.Image = new BitmapImage(new Uri(GetIconQueueStatus(each.QueueStatus, each.Description == defaultPrinter), UriKind.Absolute));

                    items.Add(item);
                }

                enablePrinting = items.Count > 0;

                if (enablePrinting)
                {
                    PrintQueue tag = selectedPrinter;

                    printersCombo.Items.Clear();

                    foreach (object each in items)
                    {
                        printersCombo.Items.Add(each);
                    }

                    if (tag != null)
                    {
                        bool found = false;

                        foreach (FrameworkElement each in printersCombo.Items)
                        {
                            if (((PrintQueue)each.Tag).Description == tag.Description)
                            {
                                found = true;
                                printersCombo.SelectedItem = each;
                                break;
                            }
                        }

                        if (!found)
                        {
                            printersCombo.SelectedIndex = 0;
                        }
                    }
                    else
                    {
                        printersCombo.SelectedIndex = 0;
                    }
                }
            }
            catch
            {
                enablePrinting = false;
            }

            printButton.IsEnabled = enablePrinting;

            if (!enablePrinting)
            {
                printersCombo.Items.Clear();
                noPrintersItem.Header = "No Printers Installed";
                printersCombo.Items.Add(noPrintersItem);
                printersCombo.SelectedIndex = 0;
            }
        }