Click here to Skip to main content
13,086,019 members (82,266 online)
Click here to Skip to main content

Stats

80.3K views
120 bookmarked
Posted 30 Jun 2009

WPF Book Reader

, 10 May 2012
A WPF book reader for cbz/cbr files
BookReader.exe.manifest
ICSharpCode.SharpZipLib.dll
SevenZipSharp.dll
WPFToolkit.dll
Dependencies
7z.dll
BookReader.application
BookReader.exe
BookReader
ClassDiagram.cd
Dependencies
7-zip.dll
7z.dll
ICSharpCode.SharpZipLib.dll
SevenZipSharp.dll
WPFToolkit.dll
Properties
app.manifest
Reader
Catalog
Common
Images
RAR
ZIP
Resources
TheRedPushPin.png
XAML
BookReader.application
BookReader.exe
BookReader.exe.manifest
7z.dll
SevenZipSharp.dll
WPFToolkit.dll
Release
BookReader.application
BookReader.exe
BookReader.exe.manifest
Dependencies
7z.dll
pdftohtml.exe
SevenZipSharp.dll
WPFToolkit.dll
BookReader.csproj.user
BookReader.csproj.vspscc
7z.dll
pdftohtml.exe
SevenZipSharp.dll
WPFToolkit.dll
app.manifest
acroread.png
document.png
ecran.png
exit.png
FitHeight.png
FitWidth.png
info.png
open_folder.png
refresh.png
Search.png
Theme
TheRedPushPin.png
tools.png
BookReader.csproj.user
ClassDiagram.cd
7-zip.dll
7z.dll
ICSharpCode.SharpZipLib.dll
SevenZipSharp.dll
WPFToolkit.dll
app.manifest
TheRedPushPin.png
BookReader.application
BookReader.exe
BookReader.exe.manifest
BookReader.vshost.exe
7z.dll
pdftohtml.exe
SevenZipSharp.dll
WPFToolkit.dll
BookReader.csproj.user
BookReader.csproj.vspscc
7z.dll
pdftohtml.exe
SevenZipSharp.dll
WPFToolkit.dll
app.manifest
acroread.png
book.ico
document.png
ecran.png
exit.png
FitHeight.png
FitWidth.png
info.png
open_folder.png
refresh.png
Search.png
TheRedPushPin.png
tools.png
using System;
using System.Configuration;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using System.Windows.Controls;
using System.Collections;
using System.Threading;

namespace BookReader
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		#region -----------------private and constructor-----------------

		private bool WaitAtBottom = false;
		private DispatcherTimer _TimerClock;
		private Catalog _Catalog = new Catalog();

		public MainWindow()
		{
			InitializeComponent();
		}

		#endregion

		#region -----------------loading/closing and timer-----------------
		/// <summary>
		/// On loading, create the timer and catalog
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			try
			{
				//create a dispatch timer to load the image cache
				_TimerClock = new DispatcherTimer();
				_TimerClock.Interval = new TimeSpan(0, 0, 5);
				_TimerClock.IsEnabled = true;
				_TimerClock.Tick += new EventHandler(TimerElapse);

				//load the catalog of books
				_Catalog.Load(Properties.Settings.Default.Catalog);
				CatalogListBox.DataContext = _Catalog.Books;

				this.BookListBox.SelectionChanged += new SelectionChangedEventHandler(BookListBox_SelectionChanged);
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		/// <summary>
		/// Save the catalog on closing
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try
			{
				_Catalog.Save();
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		/// <summary>
		/// Ask the book to manage his cache
		/// </summary>
		/// <param name="tag"></param>
		/// <param name="args"></param>
		public void TimerElapse(object tag, EventArgs args)
		{
			try
			{
				if (_Catalog.CurrentBook != null)
				{
					_Catalog.CurrentBook.ManageCache();
					this.Info.Content = _Catalog.CurrentBook.Cache.GetCacheInfo();
				}
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}
		#endregion

		#region -----------------manage page scrool-----------------

		/// <summary>
		/// manage the wheel when on top or end of scrool viewer
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void PageContent_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
		{
			if (_Catalog.CurrentBook == null) return;

			if (e.Delta > 0)
			{
				ManageScroolUp();
			}
			else
			{
				ManageScroolDown();
			}
		}

		/// <summary>
		/// manage the wheel when on top or end of scrool viewer
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void PageContent_PreviewKeyUp(object sender, KeyEventArgs e)
		{
			if (_Catalog.CurrentBook == null) return;

			if (e.Key == Key.PageDown || e.Key == Key.Down)
			{
				ManageScroolDown();
			}
			else if (e.Key == Key.PageUp || e.Key == Key.Up)
			{
				ManageScroolUp();
			}
		}

		/// <summary>
		/// Scrool up if at the top of the page
		/// </summary>
		private void ManageScroolUp()
		{
			try
			{
				if (this.PageContent.VerticalOffset == 0)
				{
					//can we go up?
					if (_Catalog.CurrentBook.GotoPreviousPage())
					{

						this.PageImage.Source = _Catalog.CurrentBook.GetCurrentPageImage();
						this.PageInfo.Content = _Catalog.CurrentBook.CurrentPage.FilePath;
						this.PageContent.ScrollToBottom();

						BookListBoxChangePage(this.BookListBox.SelectedIndex - 1);
					}
				}
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		/// <summary>
		/// Scrool down if at the end of the page
		/// </summary>
		private void ManageScroolDown()
		{
			try
			{
				if (this.PageContent.VerticalOffset + this.PageContent.ViewportHeight >= this.PageContent.ExtentHeight)
				{
					if (!WaitAtBottom)
					{
						WaitAtBottom = true;
						return;
					}
					else WaitAtBottom = false;

					if (_Catalog.CurrentBook.GotoNextPage())
					{
						this.PageImage.Source = _Catalog.CurrentBook.GetCurrentPageImage();
						this.PageInfo.Content = _Catalog.CurrentBook.CurrentPage.FilePath;
						this.PageContent.ScrollToTop();

						BookListBoxChangePage(this.BookListBox.SelectedIndex + 1);
					}
				}
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}
		#endregion

		#region -----------------menu item events-----------------

		private void LoadBook( IBook book )
		{
			try
			{
				if (_Catalog.CurrentBook != null)
					_Catalog.CurrentBook.UnLoad();

				_Catalog.CurrentBook = book;
				_Catalog.CurrentBook.Load();

				FillPageView();

				this.PageImage.Source = _Catalog.CurrentBook.GetCurrentPageImage();
				this.PageInfo.Content = _Catalog.CurrentBook.CurrentPage.FilePath;
				this.PageImage.Focus();
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		private void MenuItem_Load(object sender, RoutedEventArgs e)
		{
			try
			{
				LoadBook((IBook)CatalogListBox.SelectedValue);
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		private void MenuItem_BookMark(object sender, RoutedEventArgs e)
		{
			try
			{
				if (_Catalog.CurrentBook != null)
				{
					_Catalog.CurrentBook.SetMark();
					_Catalog.IsChanged = true;
				}
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		private void MenuItem_GotoBookMark(object sender, RoutedEventArgs e)
		{
			try
			{
				if (_Catalog.CurrentBook != (IBook)CatalogListBox.SelectedValue)
				{
					LoadBook((IBook)CatalogListBox.SelectedValue);
				}
				_Catalog.CurrentBook.GotoMark();
				this.PageImage.Source = _Catalog.CurrentBook.GetCurrentPageImage();
				this.PageContent.ScrollToHome();
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		private void MenuItem_ClearBookMark(object sender, RoutedEventArgs e)
		{
			try
			{
				((IBook)CatalogListBox.SelectedValue).Bookmark = string.Empty;
				_Catalog.IsChanged = true;
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}
		#endregion

		#region -----------------toolbar events-----------------

		private void btnAbout_Click(object sender, RoutedEventArgs e)
		{
			AboutWindow dlg = new AboutWindow();
			dlg.ShowDialog();
		}

		private void btnRefresh_Click(object sender, RoutedEventArgs e)
		{
			_Catalog.Refresh();
		}

		/// <summary>
		/// Close the soft, nothing special
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnQuit_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		/// <summary>
		/// Display the options dialog
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnOptions_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				OptionWindow dlg = new OptionWindow();

				//load the catalog of books if we change significative values 
				if (dlg.ShowDialog() == true)
				{
					_Catalog.Load(Properties.Settings.Default.Catalog);
					CatalogListBox.DataContext = _Catalog.Books;
				}
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		private GridLength _old;
		private bool _isFullSreen = false;

		private void btnFullScreen_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				if (_isFullSreen)
				{
					this.WindowState = WindowState.Normal;
					this.DockPanelLeft.Visibility = Visibility.Visible;
					this.gridSplitter1.Visibility = Visibility.Visible;
					this.MainGrid.ColumnDefinitions[0].Width = _old;
					this.WindowStyle = WindowStyle.ToolWindow;
					_isFullSreen = false;
				}
				else
				{
					this.WindowState = WindowState.Maximized;
					this.DockPanelLeft.Visibility = Visibility.Collapsed;
					this.gridSplitter1.Visibility = Visibility.Collapsed;
					_old = this.MainGrid.ColumnDefinitions[0].Width;
					this.MainGrid.ColumnDefinitions[0].Width = new GridLength(0);
					this.WindowStyle = WindowStyle.None;
					_isFullSreen = true;
				}
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}
		#endregion

		#region -----------------Page view events-----------------

		private void BookListBoxChangePage(int pageIndex)
		{
			try
			{
				this.BookListBox.SelectionChanged -= new SelectionChangedEventHandler(BookListBox_SelectionChanged);

				this.BookListBox.SelectedItem = this.BookListBox.Items[pageIndex];

				this.BookListBox.SelectionChanged += new SelectionChangedEventHandler(BookListBox_SelectionChanged);
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		private void BookListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			try
			{
				if (_Catalog.CurrentBook != null)
				{
					if (_Catalog.CurrentBook.GotoPage((IBookItem)((ListBoxItem)this.BookListBox.SelectedItem).Tag))
					{
						this.PageImage.Source = _Catalog.CurrentBook.GetCurrentPageImage();
						this.PageContent.ScrollToHome();
					}
				}
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}

		public void FillPageView()
		{
			try
			{
				if (_Catalog.CurrentBook == null)
					return;

				BookListBox.Items.Clear();

				foreach (IBookItem page in _Catalog.CurrentBook.Pages)
				{
					ListBoxItem item = new ListBoxItem();
					item.Tag = page;
					item.Content = page.FileName;
					BookListBox.Items.Add(item);
				}
			}
			catch (Exception err)
			{
				ExceptionManagement.Manage(err, true);
			}
		}
		#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 GNU General Public License (GPLv3)

Share

About the Author

Guillaume Waser
Architect XEROX
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 10 May 2012
Article Copyright 2009 by Guillaume Waser
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid