Ejemplo n.º 1
0
    private void RenderSong(RenderOption option, object o = null)
    {
        try
        {
            AppSettings appSettings = null;
            PageSetup   pageSetup   = new PageSetup();

            if (_workingSettings != null)
            {
                appSettings = _workingSettings;
            }
            else
            {
                appSettings = _appSettings;
            }


            DrawingArea area = new DrawingArea();

            pageSetup.SetLeftMargin(0.5, Unit.Inch);
            pageSetup.SetTopMargin(0.5, Unit.Inch);
            pageSetup.SetRightMargin(0.5, Unit.Inch);
            pageSetup.SetBottomMargin(0, Unit.Inch);

            if (option == RenderOption.ScreenView)
            {
                area = (DrawingArea)o;
            }
            else
            {
                PageSetup defaultPageSetup = new PageSetup();

                // Alter our set dimensions by the printer default so that it lines up in the actual
                // print out like it does on the screen.
                pageSetup.SetLeftMargin(pageSetup.GetLeftMargin(Unit.Inch) - defaultPageSetup.GetLeftMargin(Unit.Inch), Unit.Inch);
                pageSetup.SetTopMargin(pageSetup.GetTopMargin(Unit.Inch) - defaultPageSetup.GetTopMargin(Unit.Inch), Unit.Inch);
                pageSetup.SetRightMargin(pageSetup.GetRightMargin(Unit.Inch) + defaultPageSetup.GetRightMargin(Unit.Inch), Unit.Inch);
                pageSetup.SetBottomMargin(pageSetup.GetBottomMargin(Unit.Inch) + defaultPageSetup.GetBottomMargin(Unit.Inch), Unit.Inch);


                //pageSetup.SetLeftMargin(0.25, Unit.Inch);
                //pageSetup.SetTopMargin(0.25, Unit.Inch);
                //pageSetup.SetRightMargin(0.75, Unit.Inch);
                //pageSetup.SetBottomMargin(0.75, Unit.Inch);

                area = null;
            }


            //CSGen.Operations.IPageRenderer renderer = new CSGen.PageRenderer (_currentSong, forDisplay: option == RenderOption.ScreenView);
            CSGen.Operations.IPageRenderer renderer = new CSGen.Operations.RenderEngine(_currentSong, appSettings, pageSetup, option);

            //            if (option == RenderOption.ScreenView)
            //            {
            //                area = (DrawingArea)o;
            //                pageSetup = renderer.PageSetup;
            //            }
            //            else
            //            {
            //                pageSetup = new PageSetup();
            //            }

            // TODO: Remove when new render engine is complete.
            // Load configuration options.
            //            if (_workingFonts != null)
            //                renderer.Fonts = _workingFonts;
            //            else
            //                renderer.Fonts = _fonts;
            //            renderer.AppSettings = appSettings;
            //
            //            pageSetup.SetLeftMargin(0.5, Unit.Inch);
            //            pageSetup.SetTopMargin(0.5, Unit.Inch);
            //            pageSetup.SetBottomMargin(0.5, Unit.Inch);
            //            pageSetup.SetRightMargin(0.5, Unit.Inch);
            // end TODO


            if (option == RenderOption.ScreenView)
            {
                Cairo.Context cr = Gdk.CairoHelper.Create(area.GdkWindow);
                renderer.CalculatePages(cr);

                area.WidthRequest  = renderer.DisplayWidthExtent;
                area.HeightRequest = renderer.DisplayHeightExtent;

                int offset = 0;
                for (int i = 0; i < renderer.Pages; i++)
                {
                    renderer.DrawPage(i, offset);
                    offset += renderer.DisplayPaperHeight;
                }

                ((IDisposable)cr.GetTarget()).Dispose();
                ((IDisposable)cr).Dispose();
            }
            else
            {
                PrintSettings printSettings = new PrintSettings();
                printSettings.PaperSize = pageSetup.PaperSize;


                using (PrintOperation print = new PrintOperation())
                {
                    print.DefaultPageSetup = pageSetup;
                    print.PrintSettings    = printSettings;
                    print.BeginPrint      += (object obj, BeginPrintArgs args) =>
                    {
                        renderer.CalculatePages(args.Context.CairoContext);
                        print.NPages = renderer.Pages;
                    };
                    print.DrawPage += (object obj, DrawPageArgs args) =>
                    {
                        renderer.DrawPage(args.PageNr);
                    };

                    if (option == RenderOption.Print)
                    {
                        print.Run(PrintOperationAction.PrintDialog, null);
                    }
                    else
                    {
                        print.Run(PrintOperationAction.Preview, null);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageDialog dialog = new MessageDialog(
                this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok,
                "Message: {0}\n\n Stacktrace: {1}", ex.Message, ex.StackTrace);
            dialog.Run();
            dialog.Destroy();
        }
    }