[sword-svn] r159 - trunk/src/SwordReader_GUI

dtrotzjr at www.crosswire.org dtrotzjr at www.crosswire.org
Sun Jul 27 23:05:05 MST 2008


Author: dtrotzjr
Date: 2008-07-27 23:05:05 -0700 (Sun, 27 Jul 2008)
New Revision: 159

Added:
   trunk/src/SwordReader_GUI/SRModuleView.cpp
   trunk/src/SwordReader_GUI/SRModuleView.h
Modified:
   trunk/src/SwordReader_GUI/SRMainFrame.cpp
   trunk/src/SwordReader_GUI/SRTextView.h
Log:
More demand/background loading loading stuff. Now has a thread that gets kicked off once the first page of text is loaded. The thread continues loading and rendering the page in the background so that it is viewable later. 
Very, very buggy as I have no thread safety in place.

Modified: trunk/src/SwordReader_GUI/SRMainFrame.cpp
===================================================================
--- trunk/src/SwordReader_GUI/SRMainFrame.cpp	2008-07-27 04:58:40 UTC (rev 158)
+++ trunk/src/SwordReader_GUI/SRMainFrame.cpp	2008-07-28 06:05:05 UTC (rev 159)
@@ -132,6 +132,7 @@
     GetClientRect(m_hWnd, &view_rect);
 
     CreateCommandBar();
+    view_rect.bottom -= m_menuBar->Height();
     
     m_viewModules[m_nCurrentModule] = new SRModuleView();
     if(!m_viewModules[m_nCurrentModule]->Create(this, view_rect))
@@ -139,8 +140,6 @@
 
     InitSword();
 
-    view_rect.bottom -= m_menuBar->Height();
-    
     m_viewBook = new SRBookChooser(s_wcsBookNames,MENU_CHAP);
     if(!m_viewBook->Create(this, view_rect))
         return FALSE;
@@ -238,6 +237,8 @@
 
     delete verse;
     
+    m_viewModules[m_nCurrentModule]->SetSwordReady();
+
     return TRUE;
 }
 

Added: trunk/src/SwordReader_GUI/SRModuleView.cpp
===================================================================
--- trunk/src/SwordReader_GUI/SRModuleView.cpp	                        (rev 0)
+++ trunk/src/SwordReader_GUI/SRModuleView.cpp	2008-07-28 06:05:05 UTC (rev 159)
@@ -0,0 +1,268 @@
+#include "SRModuleView.h"
+#include "SRFramework/WCString.h"
+#include "SRMainFrame.h"
+#include <string.h>
+
+DWORD WINAPI LoadTextThread(LPVOID arg)
+{
+    SRModuleView *view = reinterpret_cast<SRModuleView*>(arg);
+    if(!view)
+        return 1;
+    view->LoadTextView(true);
+    return 0;
+}
+
+SRModuleView::SRModuleView(void)
+: SRTextView()
+, m_pModule(NULL)
+, m_fChapterChanged(FALSE)
+, m_fSwordInit(FALSE)
+{
+
+}
+
+SRModuleView::~SRModuleView(void)
+{
+}
+
+VOID SRModuleView::SetModule(SWModule *pModule)
+{
+    if(m_pModule == pModule)
+        return;
+    m_pModule = pModule;
+    m_fChapterChanged = TRUE;
+    LoadTextView();
+}
+
+VOID SRModuleView::SetKey(const VerseKey &verse)
+{
+    if(m_verse == verse)
+        return;
+    
+    if(verse.Book() != m_verse.Book() || (m_verse.Book == verse.Book() && m_verse.Chapter() != verse.Chapter()))
+        m_fChapterChanged = true;
+    
+    m_verse = verse;
+
+    LoadTextView();
+}
+
+VOID SRModuleView::SetBook(INT nBook)
+{
+    if(nBook > BIBLE_OT_BOOKS){
+        if( m_verse.Testament() != 2 || m_verse.Book() + BIBLE_OT_BOOKS != nBook){
+            m_verse.Testament(SWORD_NEW_TESTAMENT);
+            m_verse.Book(nBook - BIBLE_OT_BOOKS);
+            m_verse.Chapter(1);
+            m_verse.Verse(1);
+        }
+    }else{
+        if( m_verse.Testament() != 1 || m_verse.Book != nBook){
+            m_verse.Testament(SWORD_OLD_TESTAMENT);
+            m_verse.Book(nBook);
+            m_verse.Chapter(1);
+            m_verse.Verse(1);
+        }
+    }
+    m_fChapterChanged = true;
+}
+
+VOID SRModuleView::SetChapter(INT nChapter)
+{
+    if(m_verse.Chapter() != nChapter){
+        m_verse.Chapter(nChapter);
+        m_verse.Verse(1);
+        m_fChapterChanged = true;
+    }
+}
+
+VOID SRModuleView::SetVerse(INT nVerse, BOOL fScroll)
+{
+    if(nVerse < 0)
+        return; // This isn't a valid verse.
+    m_verse.Verse(nVerse);
+    if(fScroll)
+        ScrollToVerse(m_verse.Verse());
+}
+
+VOID SRModuleView::LoadTextView(BOOL fInThread)
+{
+	WCString buf, text, s;
+    BOOL done = FALSE;
+    char strNum[10];
+    HDC hdc;
+    
+    if(!m_fSwordInit)
+        return;
+    if(!fInThread && !m_fChapterChanged) // This text should still be valid...
+        return;
+    else
+        m_fChapterChanged = false;
+
+    if(!fInThread)
+        Clear();
+
+    VerseKey keyCur(m_verse);
+    if(!fInThread)
+        keyCur.Verse(1);
+    else
+        keyCur.Verse(m_nLoadedTill);
+    
+    buf = "<html><body>";
+    AddText(buf.w_str(), buf.length()); // a <style> section presumably gets skipped
+	
+	while (!done) {
+        m_pModule->SetKey(keyCur);
+        int pvHeading = 0;
+        text = (TCHAR *)m_pModule->RenderText();
+        s = "";
+		do {
+			sprintf(strNum, "%i", pvHeading++);
+            SWBuf preverseHeading = m_pModule->getEntryAttributes()["Heading"]["Preverse"][strNum].c_str();
+			if (preverseHeading.length()) {
+                if(keyCur.Verse() != 1)
+                    s +=  "<br /><br /><b>";
+                else
+                    s += "<b>";
+                s += (TCHAR *)m_pModule->RenderText(preverseHeading);
+                s += "</b><br /><br />";
+			}
+			else break;
+		} while (true);
+        s += GetVerseHeader(keyCur);
+        s += text + " ";
+		
+        AddText(s.w_str(), s.length());
+
+        if (keyCur.Verse() == keyCur.getVerseMax() ){
+			done = TRUE;
+            break;
+        }else if(!fInThread && CurrentPageFilled()){
+            done = FALSE;
+            break;
+        }else if(fInThread){
+            TCHAR szStat[64] = {0};
+            RECT r = {0};
+            r.left = 0;
+            r.right = m_rect.right;
+            r.top = m_rect.bottom - 22;
+            //r.top = 30;
+            r.bottom = m_rect.bottom;
+            //r.bottom = 60;
+            hdc = GetDC(m_hWnd);
+            PreRenderBuff(hdc); // Allows the text to show as soon as it is loaded in.
+            float percent = 100 * (float)keyCur.Verse()/(float)keyCur.getVerseMax();
+            swprintf(szStat, L"Loading... %f%% done", percent);
+            INT nLen = wcsnlen(szStat, 16);
+            HBRUSH brushBG =  CreateSolidBrush((COLORREF)BUTTON_BACKGROUND);
+            SetBkColor(hdc, BUTTON_SEL_BACKGROUND);
+			FillRect(hdc, &r, brushBG);
+			Rectangle(hdc, r.left, r.top, r.right, r.bottom);
+            DrawText(hdc, &szStat[0], nLen, &r, DT_VCENTER | DT_CENTER | DT_SINGLELINE);
+            ReleaseDC(m_hWnd, hdc);
+        }
+        keyCur.increment(1);
+	}
+    buf = "</body></html>";
+    AddText(buf.w_str(), buf.length());
+
+    RefreshWindow();
+    if(done){
+        ScrollToVerse(m_verse.Verse());
+    }else if(!fInThread){ // Overkill but, I am a paranoid guy.
+        m_nLoadedTill = keyCur.Verse();
+        CreateLoadTextThread();
+    }
+
+}
+
+VOID SRModuleView::CreateLoadTextThread()
+{
+    m_hLoadTextThread = CreateThread(NULL, 0, LoadTextThread, (LPVOID) this, 0, &m_dwLoadTextThreadID);
+}
+
+WCString SRModuleView::GetVerseHeader(VerseKey key) const
+{
+    wchar_t wverse[16] = {0};
+    wchar_t wchap[16] = {0};
+    _itow(key.Verse(), wverse, 10);
+    _itow(key.Chapter(), wchap, 10);
+
+	WCString result = "\n<b><a name=\"";
+	result += wverse;
+	result += "\">";
+    if (key.Verse() == 1) {
+		result += wchap;
+		result += ":";
+	}
+	result += wverse;
+	result += "</a></b>&nbsp;";
+
+	return result;
+}
+    
+const VerseKey& SRModuleView::GetVerseKey() const
+{
+    return m_verse;
+}
+
+VOID SRModuleView::RefreshScreen(BOOL fReloadText)
+{
+    if(fReloadText)
+        m_fChapterChanged = TRUE;
+    LoadTextView();
+}
+
+BOOL SRModuleView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
+{
+    INT a = 0;
+    switch(nChar)
+    {
+    case VK_UP:
+        ScrollFullPage(1);
+        if(GetVerseNum() > -1)
+            m_verse.Verse(GetVerseNum());
+        //UpdateWindowTitle();
+        break;
+    case VK_DOWN:
+        ScrollFullPage(-1);
+        if(GetVerseNum() > -1)
+            m_verse.Verse(GetVerseNum());
+        //UpdateWindowTitle();
+        break;
+    case VK_LEFT:
+        if(m_verse.Verse() > 1){
+            m_verse.Verse(1);
+            ScrollToVerse(1);
+        }else{
+            m_verse.decrement(1);
+            m_verse.Verse(1);
+            m_fChapterChanged = true;
+            LoadTextView();
+        }
+        //UpdateWindowTitle();
+        break;
+    case VK_RIGHT:
+        m_verse.Chapter(m_verse.Chapter() + 1);
+        m_fChapterChanged = true;
+        LoadTextView();
+        break;
+    default:
+        return FALSE;
+    }
+    return TRUE;
+}
+
+WCString SRModuleView::GetWindowTitle()
+{    
+    wchar_t wverse[16] = {0};
+    wchar_t wchapt[16] = {0};
+    
+    WCString title =  
+        WCString(m_pModule->Name()) + " " + 
+        SRMainFrame::GetBookNames()[(m_verse.Book() - 1) + BIBLE_OT_BOOKS*(m_verse.Testament() - 1)] + WCString(" ") + 
+        WCString(_itow(m_verse.Chapter(),wchapt, 10)) + WCString(":") + 
+        WCString(_itow(m_verse.Verse(), wverse, 10));
+    return title;
+}
+

Added: trunk/src/SwordReader_GUI/SRModuleView.h
===================================================================
--- trunk/src/SwordReader_GUI/SRModuleView.h	                        (rev 0)
+++ trunk/src/SwordReader_GUI/SRModuleView.h	2008-07-28 06:05:05 UTC (rev 159)
@@ -0,0 +1,47 @@
+#pragma once
+#include "srtextview.h"
+#include <versekey.h>
+#include <swmodule.h>
+
+using namespace sword;
+using namespace SRFramework;
+
+class SRModuleView :
+    public SRTextView
+{
+public:
+    SRModuleView(void);
+    virtual ~SRModuleView(void);
+
+    VOID        SetModule(SWModule *pModule);
+    VOID        SetKey(const VerseKey &verse);
+    VOID        SetBook(INT nBook);
+    VOID        SetChapter(INT nChapter);
+    VOID        SetVerse(INT nVerse, BOOL fScroll);
+    VOID        RefreshScreen(BOOL fReloadText = FALSE);
+    BOOL        OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
+    const VerseKey& GetVerseKey() const;
+    WCString SRModuleView::GetWindowTitle();
+    SWModule*   GetModule()     { return m_pModule;                 }
+    INT         GetBook()       { return m_verse.Book();            }
+    INT         GetChapter()    { return m_verse.Chapter();         }
+    INT         GetVerse()      { return m_verse.Verse();           }
+    INT         GetTestament()  { return m_verse.Testament();       }
+    INT         GetChapterMax() { return m_verse.getChapterMax();   }
+    INT         GetVerseMax()   { return m_verse.getVerseMax();     }
+    VOID        SetSwordReady() { m_fSwordInit = TRUE; }
+private:
+    VOID        LoadTextView(BOOL fInThread = FALSE);
+    VOID        CreateLoadTextThread();
+    WCString    GetVerseHeader(VerseKey key) const;
+    
+    VerseKey    m_verse;
+    INT         m_nLoadedTill;
+    SWModule    *m_pModule;
+    BOOL        m_fChapterChanged;
+    BOOL        m_fSwordInit;  
+
+    HANDLE      m_hLoadTextThread;
+    DWORD       m_dwLoadTextThreadID;
+    friend DWORD WINAPI LoadTextThread(LPVOID arg);
+};

Modified: trunk/src/SwordReader_GUI/SRTextView.h
===================================================================
--- trunk/src/SwordReader_GUI/SRTextView.h	2008-07-27 04:58:40 UTC (rev 158)
+++ trunk/src/SwordReader_GUI/SRTextView.h	2008-07-28 06:05:05 UTC (rev 159)
@@ -483,7 +483,7 @@
 	friend class SRTextWord;
 	friend class SRTextLines;
 
-private:
+protected:
     // Private Methods
     //! Handles the WM_PAINT message for this window control. 
     /*! This is the heart and soul of this window control. Well in theory it 




More information about the sword-cvs mailing list