Click here to Skip to main content
11,567,539 members (42,633 online)
Click here to Skip to main content
Add your own
alternative version

GoalBook - A Hybrid Smart Client

, 25 Sep 2009 CPOL 43K 731 69
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
GoalBook.zip
GoalBook
Dependencies
CAG WPF 2
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.Presentation.dll
Microsoft.Practices.Composite.UnityExtensions.dll
Microsoft.Practices.ObjectBuilder2.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
vssver2.scc
CSLA
Csla.dll
Csla.XmlSerializers.dll
vssver2.scc
Infragistics
Infragistics3.Wpf.DataPresenter.v9.1.Express.dll
Infragistics3.Wpf.Editors.v9.1.Express.dll
Infragistics3.Wpf.v9.1.Express.dll
vssver2.scc
WPFToolkit
vssver2.scc
WPFToolkit.dll
GoalBook.Controls
CheckedList
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Goals
GoalBook.Goals.csproj.user
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
GoalBook.Infrastructure
Comparers
vssver2.scc
Constants
vssver2.scc
Controls
Converters
vssver2.scc
Enums
vssver2.scc
Events
vssver2.scc
GoalBook.Infrastructure.csproj.user
Helpers
vssver2.scc
Interfaces
vssver2.scc
ObjectModel
vssver2.scc
Printing
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Notes
GoalBook.Notes.csproj.user
Properties
vssver2.scc
Resources
arrow_redo.png
arrow_undo.png
cut.png
page_copy.png
paste_plain.png
text_bold.png
text_indent.png
text_indent_remove.png
text_italic.png
text_list_bullets.png
text_list_numbers.png
vssver2.scc
world_link.png
Views
vssver2.scc
vssver2.scc
GoalBook.Public
Encryption
vssver2.scc
HtmlConverter
vssver2.scc
HtmlParser
vssver2.scc
Misc
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Shell
App.ico
Commands
vssver2.scc
GoalBook.Shell.csproj.user
Journal.ico
Journal48.ico
Misc
vssver2.scc
Modules
vssver2.scc
Properties
licenses.licx
vssver2.scc
Resources
arrow_refresh.png
arrow_undo.png
checked.gif
cross.png
disk.png
email.png
erase.png
error.png
exclamation.png
flag_red.png
group.png
help.png
information.png
magnifier.png
page_red.png
pencil.png
printer.png
user.png
vssver2.scc
Wave.jpg
world.png
world_link.png
Services
vssver2.scc
Splash.png
Views
vssver2.scc
vssver2.scc
Windows
vssver2.scc
GoalBook.snk
GoalBook.Synchronisation
Events
vssver2.scc
Properties
vssver2.scc
ToodleDo
vssver2.scc
vssver2.scc
GoalBook.Tasks
Controls
vssver2.scc
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
vssver2.scc
// <copyright file="PrintService.cs" company="GoalBook"> 
//    Copyright © 2009 Mark Brownsword. All rights reserved.
//    This source code and supporting files are licensed under The Code Project  
//    Open License (CPOL) as detailed at http://www.codeproject.com/info/cpol10.aspx. 
// </copyright>
namespace GoalBook.Shell.Services
{
    #region Using Statements
    using System.Printing;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Documents.Serialization;
    using System.Windows.Media;
    using System.Windows.Xps;
    using GoalBook.Infrastructure;
    using GoalBook.Infrastructure.Constants;
    using GoalBook.Infrastructure.Events;
    using GoalBook.Infrastructure.Interfaces;
    using GoalBook.Infrastructure.Printing;
    using Microsoft.Practices.Composite.Events;
    using Microsoft.Practices.Unity;
    using ShellResources = GoalBook.Shell.Properties.Resources;    
    #endregion

    /// <summary>
    /// Print Service.
    /// </summary>
    public class PrintService : IPrintService
    {              
        #region Constants and Enums
        #endregion

        #region Instance and Shared Fields
        /// <summary>
        /// Declaration for IUnityContainer.
        /// </summary>
        private readonly IUnityContainer unityContainer;

        /// <summary>
        /// Declaration for PrintDialog.
        /// </summary>
        private readonly PrintDialog printDialog;        
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the PrintService class.
        /// </summary> 
        /// <param name="unityContainer">Reference to IUnityContainer</param>
        public PrintService(IUnityContainer unityContainer)
        {
            this.unityContainer = unityContainer;
                                    
            // Initialise PrintDialog.
            this.printDialog = new PrintDialog();
            this.printDialog.PageRangeSelection = PageRangeSelection.AllPages;
            this.printDialog.UserPageRangeEnabled = true;            
        }
        #endregion

        #region Delegates and Events
        #endregion

        #region Properties
        /// <summary>
        /// Gets PrintableAreaWidth.
        /// </summary>
        public double PrintableAreaWidth { get; private set; }

        /// <summary>
        /// Gets PrintableAreaHeight.
        /// </summary>
        public double PrintableAreaHeight { get; private set; }
        #endregion

        #region Public and internal Methods
        /// <summary>
        /// Print the specified FlowDocument.
        /// </summary>
        /// <param name="title">title parameter</param>
        /// <param name="document">FlowDocument to print</param>
        public void Print(string title, FlowDocument document)
        {            
            // Return if user cancels.
            if (this.printDialog.ShowDialog().Equals(false)) 
            { 
                return; 
            }
                                      
            // Print the document.
            if (this.printDialog.PrintableAreaWidth > 0 && this.printDialog.PrintableAreaHeight > 0)
            {
                // Initialise the XpsDocumentWriter.
                XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(this.printDialog.PrintQueue);
                xpsDocumentWriter.WritingCancelled += new WritingCancelledEventHandler(this.PrintCancelled);
                xpsDocumentWriter.WritingCompleted += new WritingCompletedEventHandler(this.PrintCompleted);

                // Publish event PrintBegin.
                this.OnPrintBegin(ShellResources.PrintBeginMessage);

                // Initialise PageDefinition (Header and Footer).
                PageDefinition pageDefinition = new PageDefinition();
                pageDefinition.HeaderPadding = new Thickness(75, 0, 75, 0);
                pageDefinition.HeaderHeight = 50;
                pageDefinition.HeaderTemplate = title;
                pageDefinition.HeaderFontFamily = new FontFamily(PrintConstants.FONT_FAMILY_ARIAL);
                pageDefinition.HeaderFontSize = 14;
                pageDefinition.FooterPadding = new Thickness(75, 0, 75, 0);
                pageDefinition.FooterHeight = 50;
                pageDefinition.FooterTemplate = AssemblyInfoProperties.ProductName;
                pageDefinition.FooterFontFamily = new FontFamily(PrintConstants.FONT_FAMILY_ARIAL);
                pageDefinition.FooterFontSize = 12;
                pageDefinition.PageSize = new Size(this.printDialog.PrintableAreaWidth, this.printDialog.PrintableAreaHeight);

                // Set document properties to ensure content fits between 
                // header and footer and spans the whole page width.
                document.PagePadding = new Thickness(75, 75, 75, 75);
                document.ColumnWidth = this.printDialog.PrintableAreaWidth;
                
                // Initialise the FlowDocumentPaginator.
                FlowDocumentPaginator paginator = new FlowDocumentPaginator(pageDefinition, document);

                // Print asynchronously.            
                xpsDocumentWriter.WriteAsync(paginator);
            }            
        }               
        #endregion

        #region Base Class Overrides
        #endregion

        #region Private and Protected Methods
        /// <summary>
        /// Raise the PrintBegin event.
        /// </summary>  
        /// <param name="message">Message to publish</param>
        private void OnPrintBegin(string message)
        {
            this.unityContainer.Resolve<IEventAggregator>().GetEvent<PrintBeginEvent>().Publish(message);
        }

        /// <summary>
        /// Raise the PrintEnd event.
        /// </summary>   
        /// <param name="message">Message to publish</param>
        private void OnPrintEnd(string message)
        {
            this.unityContainer.Resolve<IEventAggregator>().GetEvent<PrintEndEvent>().Publish(message);
        }

        /// <summary>
        /// Raise the PrintException event.
        /// </summary>
        /// <param name="ex">Exception to publish</param>
        private void OnPrintException(PrintDialogException ex)
        {
            this.unityContainer.Resolve<IEventAggregator>().GetEvent<PrintExceptionEvent>().Publish(ex);
        }

        /// <summary>
        /// Print Completed.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">WritingCompletedEventArgs parameter</param>
        private void PrintCompleted(object sender, WritingCompletedEventArgs e)
        {
            if (e.Cancelled || e.Error != null)
            {
                this.OnPrintException(new PrintDialogException(
                    string.Format(ShellResources.PrintExceptionMessage, e.Error.Message), e.Error));
            }
            else
            {
                this.OnPrintEnd(ShellResources.PrintEndMessage);
            }
        }

        /// <summary>
        /// Print Cancelled.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">WritingCancelledEventArgs parameter</param>
        private void PrintCancelled(object sender, WritingCancelledEventArgs e)
        {
            if (e.Error != null)
            {
                this.OnPrintException(new PrintDialogException(
                    string.Format(ShellResources.PrintExceptionMessage, e.Error.Message.Replace("\r\n", string.Empty)), e.Error));
            }
            else
            {
                this.OnPrintEnd(ShellResources.PrintEndMessage);
            }
        }
        #endregion

        #region Event Handlers
        #endregion
    }  
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mark Brownsword
Software Developer (Senior)
Australia Australia
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 25 Sep 2009
Article Copyright 2009 by Mark Brownsword
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid