From 7afefc8342853df50dc1e1a192415cd1164a7382 Mon Sep 17 00:00:00 2001 From: Michael Vrhel Date: Fri, 10 May 2013 15:20:08 -0700 Subject: Implemented zooming and fixed and simplified the binding of several objects to the UI XAML --- winrt/mupdf_cpp/DocumentPage.cpp | 3 ++ winrt/mupdf_cpp/DocumentPage.h | 57 +++++++++++++++++++++++++++-- winrt/mupdf_cpp/MainPage.xaml | 4 +-- winrt/mupdf_cpp/MainPage.xaml.cpp | 75 +++++++++++++++++---------------------- winrt/mupdf_cpp/MainPage.xaml.h | 2 +- 5 files changed, 93 insertions(+), 48 deletions(-) (limited to 'winrt') diff --git a/winrt/mupdf_cpp/DocumentPage.cpp b/winrt/mupdf_cpp/DocumentPage.cpp index c705c237..637a19a4 100644 --- a/winrt/mupdf_cpp/DocumentPage.cpp +++ b/winrt/mupdf_cpp/DocumentPage.cpp @@ -10,5 +10,8 @@ namespace mupdf_cpp this->Width = 0; this->Zoom = 1.0; this->Content = NOTSET; + _isPropertyChangedObserved = false; } } + + diff --git a/winrt/mupdf_cpp/DocumentPage.h b/winrt/mupdf_cpp/DocumentPage.h index dc006e65..a7585410 100644 --- a/winrt/mupdf_cpp/DocumentPage.h +++ b/winrt/mupdf_cpp/DocumentPage.h @@ -8,6 +8,7 @@ using namespace Windows::UI::Xaml::Media::Imaging; using namespace Windows::UI::Xaml::Controls; using namespace Windows::Foundation::Collections; +using namespace Windows::UI::Xaml::Data; typedef enum { FULL_RESOLUTION = 0, @@ -21,8 +22,7 @@ namespace mupdf_cpp { // enables data binding with this class [Windows::UI::Xaml::Data::Bindable] - - public ref class DocumentPage sealed + public ref class DocumentPage sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged { private: int height; @@ -42,6 +42,7 @@ namespace mupdf_cpp void set(IVector^ value) { textbox = value; + DocumentPage::OnPropertyChanged("TextBox"); } } @@ -51,6 +52,7 @@ namespace mupdf_cpp void set(IVector^ value) { linkbox = value; + DocumentPage::OnPropertyChanged("LinkBox"); } } @@ -112,7 +114,58 @@ namespace mupdf_cpp void set(WriteableBitmap^ value) { image = value; + DocumentPage::OnPropertyChanged("Image"); } } + + private: + bool _isPropertyChangedObserved; + event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ _privatePropertyChanged; + + + protected: + /// + /// Notifies listeners that a property value has changed. + /// + /// Name of the property used to notify listeners. + void OnPropertyChanged(String^ propertyName) + { + if (_isPropertyChangedObserved) + { + PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName)); + } + } + + public: + + // in c++, it is not neccessary to include definitions of add, remove, and raise. + // these definitions have been made explicitly here so that we can check if the + // event has listeners before firing the event + virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged + { + virtual Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ e) + { + _isPropertyChangedObserved = true; + return _privatePropertyChanged += e; + } + virtual void remove(Windows::Foundation::EventRegistrationToken t) + { + _privatePropertyChanged -= t; + } + + protected: + virtual void raise(Object^ sender, Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e) + { + if (_isPropertyChangedObserved) + { + _privatePropertyChanged(sender, e); + } + } + } +#pragma endregion + + + + }; } diff --git a/winrt/mupdf_cpp/MainPage.xaml b/winrt/mupdf_cpp/MainPage.xaml index deb262cc..54226eac 100644 --- a/winrt/mupdf_cpp/MainPage.xaml +++ b/winrt/mupdf_cpp/MainPage.xaml @@ -82,7 +82,7 @@ HorizontalSnapPointsType="None" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" - MinZoomFactor="1" + MinZoomFactor="0.25" MaxZoomFactor="4"> SetSource(ras); + + DocumentPage^ doc_page = this->m_docPages->GetAt(page_num); + doc_page->Image = bmp; + + doc_page->Height = ras_size.Y; + doc_page->Width = ras_size.X; +} + Point MainPage::ComputePageSize(spatial_info_t spatial_info, int page_num) { Point screenSize; @@ -395,7 +411,6 @@ void mupdf_cpp::MainPage::CleanUp() m_flip_from_searchlink = false; m_num_pages = -1; m_search_rect_count = 0; - ResetSearch(); m_ren_status = REN_AVAILABLE; m_links_on = false; m_rectlist_page = -1; @@ -590,8 +605,6 @@ void mupdf_cpp::MainPage::OpenDocument(StorageFile^ file) render_task.then([this, k, ras_size] (InMemoryRandomAccessStream^ ras) { - /* Set up the image brush when rendering is completed, must be on - UI thread */ UpdatePage(k, ras, ras_size, FULL_RESOLUTION); }, task_continuation_context::use_current()); } @@ -704,7 +717,6 @@ void mupdf_cpp::MainPage::Slider_ValueChanged(Platform::Object^ sender, Windows: this->m_currpage = newValue; m_sliderchange = true; this->m_curr_flipView->SelectedIndex = newValue; - ResetSearch(); }, task_continuation_context::use_current()); } } @@ -810,6 +822,7 @@ void mupdf_cpp::MainPage::ShowSearchResults(SearchResult_t result) screenSize.Y *= screenScale; pageSize = mu_doc->GetPageSize(m_currpage); scale = fitPageToScreen(pageSize, screenSize); + auto doc_page = this->m_docPages->GetAt(old_page); /* Construct our list of rectangles */ for (int k = 0; k < result.box_count; k++) @@ -822,17 +835,13 @@ void mupdf_cpp::MainPage::ShowSearchResults(SearchResult_t result) rect_item->Width = curr_box->LowerRight.X - curr_box->UpperLeft.X; rect_item->X = curr_box->UpperLeft.X * scale.X; rect_item->Y = curr_box->UpperLeft.Y * scale.Y; - rect_item->Width *= scale.X; - rect_item->Height *= scale.Y; + rect_item->Width *= (scale.X * doc_page->Zoom); + rect_item->Height *= (scale.Y * doc_page->Zoom); rect_item->Index = k.ToString(); m_text_list->Append(rect_item); } /* Make sure the current page has its text results cleared */ - auto doc_page = this->m_docPages->GetAt(old_page); doc_page->TextBox = nullptr; - m_page_update = true; - this->m_docPages->SetAt(old_page, doc_page); - m_page_update = false; /* Go ahead and set our doc item to this in the vertical and horizontal view */ m_searchpage = new_page; @@ -874,29 +883,6 @@ void mupdf_cpp::MainPage::CancelSearch(Platform::Object^ sender, Windows::UI::Xa m_searchcts.cancel(); } -void mupdf_cpp::MainPage::ResetSearch(void) -{ - m_searchpage = -1; -#if 0 - wchar_t buf[20]; - String^ TempString = ref new String(buf); - - /* Remove all the rects */ - for (int k = 0; k < this->m_search_rect_count; k++) - { - unsigned int index; - int len = swprintf_s(buf, 20, L"%s_%d", L"Rect",k); - Rectangle^ curr_rect = (Rectangle^) (m_curr_flipView->FindName(TempString)); - if (curr_rect != nullptr) - { - Canvas^ results_Canvas = (Canvas^) curr_rect->Parent; - results_Canvas->Children->IndexOf(curr_rect, &index); - results_Canvas->Children->RemoveAt(index); - } - } -#endif -} - void mupdf_cpp::MainPage::AddTextCanvas() { /* Go ahead and set our doc item to this in the vertical and horizontal view */ @@ -905,9 +891,6 @@ void mupdf_cpp::MainPage::AddTextCanvas() if (doc_page->Content == FULL_RESOLUTION) // We should not be doing links for thumbnails { doc_page->TextBox = m_text_list; - m_page_update = true; - this->m_docPages->SetAt(m_currpage, doc_page); - m_page_update = false; } this->m_search_active = false; } @@ -1095,9 +1078,6 @@ void mupdf_cpp::MainPage::Linker(Platform::Object^ sender, Windows::UI::Xaml::Ro if (doc_page->Content == FULL_RESOLUTION) { doc_page->LinkBox = nullptr; - m_page_update = true; - this->m_docPages->SetAt(m_currpage, doc_page); - m_page_update = false; } } } @@ -1109,6 +1089,8 @@ void mupdf_cpp::MainPage::AddLinkCanvas() { /* See if the link object for this page has already been computed */ int link_page = m_linkset->GetAt(m_currpage); + auto doc_page = this->m_docPages->GetAt(m_currpage); + if (!link_page) { m_linkset->SetAt(m_currpage, true); @@ -1141,8 +1123,8 @@ void mupdf_cpp::MainPage::AddLinkCanvas() rect_item->Width = curr_link->LowerRight.X - curr_link->UpperLeft.X; rect_item->X = curr_link->UpperLeft.X * scale.X; rect_item->Y = curr_link->UpperLeft.Y * scale.Y; - rect_item->Width *= scale.X; - rect_item->Height *= scale.Y; + rect_item->Width *= (scale.X * doc_page->Zoom); + rect_item->Height *= (scale.Y * doc_page->Zoom); rect_item->Type = curr_link->Type; rect_item->Urilink = curr_link->Uri; rect_item->PageNum = curr_link->PageNum; @@ -1154,13 +1136,11 @@ void mupdf_cpp::MainPage::AddLinkCanvas() m_page_link_list->SetAt(m_currpage, link_list); } /* Go ahead and set our doc item to this in the vertical and horizontal view */ - auto doc_page = this->m_docPages->GetAt(m_currpage); if (doc_page->LinkBox == nullptr) { if (doc_page->Content == FULL_RESOLUTION) // We should not be doing links for thumbnails { doc_page->LinkBox = m_page_link_list->GetAt(m_currpage); - this->m_docPages->SetAt(m_currpage, doc_page); } } } @@ -1319,8 +1299,17 @@ void mupdf_cpp::MainPage::ScrollChanged(Platform::Object^ sender, if (!e->IsIntermediate) { doc_page->Zoom = scrollviewer->ZoomFactor; + int page = m_currpage; /* Render at new resolution */ + spatial_info_t spatial_info = InitSpatial(doc_page->Zoom); + Point ras_size = ComputePageSize(spatial_info, page); + auto render_task = + create_task(mu_doc->RenderPage(page, ras_size.X, ras_size.Y)); + render_task.then([this, page, ras_size] (InMemoryRandomAccessStream^ ras) + { + ReplaceImage(page, ras, ras_size); + }, task_continuation_context::use_current()); } } diff --git a/winrt/mupdf_cpp/MainPage.xaml.h b/winrt/mupdf_cpp/MainPage.xaml.h index 562ef704..ad66787f 100644 --- a/winrt/mupdf_cpp/MainPage.xaml.h +++ b/winrt/mupdf_cpp/MainPage.xaml.h @@ -103,6 +103,7 @@ namespace mupdf_cpp bool m_sliderchange; bool m_update_flip; + void ReplaceImage(int page_num, InMemoryRandomAccessStream^ ras, Point ras_size); void Picker(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void Searcher(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void OpenDocumentPrep(StorageFile^ file); @@ -120,7 +121,6 @@ namespace mupdf_cpp void FlipView_SelectionChanged(Object^ sender, SelectionChangedEventArgs^ e); void SearchNext(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void SearchPrev(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); - void ResetSearch(void); void CancelSearch(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void SearchInDirection(int dir, String^ textToFind); void ShowSearchResults(SearchResult_t result); -- cgit v1.2.3