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

dtrotzjr at www.crosswire.org dtrotzjr at www.crosswire.org
Wed Jul 30 21:58:30 MST 2008


Author: dtrotzjr
Date: 2008-07-30 21:58:30 -0700 (Wed, 30 Jul 2008)
New Revision: 160

Modified:
   trunk/src/SwordReader_GUI/SRModuleView.cpp
   trunk/src/SwordReader_GUI/SRModuleView.h
   trunk/src/SwordReader_GUI/SRTextView.cpp
   trunk/src/SwordReader_GUI/SRTextView.h
   trunk/src/SwordReader_GUI/SwordReaderResource.h
Log:
Some multi-threading tweaks. I handle resource allocation differently than is traditional for multi-threaded applications. I don't use mutexs as would be expected because basically if the main thread needs the resource that means the child process is no longer relevant and needs to be told to stop. For example: we are loading the rest of a long chapter using a thread and the user decides to go to the next chapter, when that happens whatever the child process is doing does not matter since we don't care about that chapter so we simply tell it to stop immediately and we wait until it does, then we load the new chapter. The concept of a mutex is there, but I do not explicitly use them. Anyone who cares to feel free to review my code and tell me of any gotchas my approach might present. 

Modified: trunk/src/SwordReader_GUI/SRModuleView.cpp
===================================================================
--- trunk/src/SwordReader_GUI/SRModuleView.cpp	2008-07-28 06:05:05 UTC (rev 159)
+++ trunk/src/SwordReader_GUI/SRModuleView.cpp	2008-07-31 04:58:30 UTC (rev 160)
@@ -8,7 +8,12 @@
     SRModuleView *view = reinterpret_cast<SRModuleView*>(arg);
     if(!view)
         return 1;
+    view->m_fThreadRunning = TRUE;
+    view->m_wStatusBarHeight = SR_STATUS_BAR_HEIGHT;
     view->LoadTextView(true);
+    view->m_fThreadRunning = FALSE;
+    view->m_fAbortThread = FALSE;
+    view->m_wStatusBarHeight = 0;
     return 0;
 }
 
@@ -17,6 +22,8 @@
 , m_pModule(NULL)
 , m_fChapterChanged(FALSE)
 , m_fSwordInit(FALSE)
+, m_fThreadRunning(FALSE)
+, m_fAbortThread(FALSE)
 {
 
 }
@@ -91,9 +98,17 @@
     BOOL done = FALSE;
     char strNum[10];
     HDC hdc;
+    INT rc;
     
     if(!m_fSwordInit)
         return;
+
+    if(!fInThread && m_fThreadRunning){
+        m_fAbortThread = TRUE;
+        rc = WaitForSingleObject(m_hLoadTextThread, INFINITE);
+        if(rc != WAIT_OBJECT_0)
+            return;
+    }
     if(!fInThread && !m_fChapterChanged) // This text should still be valid...
         return;
     else
@@ -111,7 +126,7 @@
     buf = "<html><body>";
     AddText(buf.w_str(), buf.length()); // a <style> section presumably gets skipped
 	
-	while (!done) {
+    while (!done && (!fInThread || !m_fAbortThread)) {
         m_pModule->SetKey(keyCur);
         int pvHeading = 0;
         text = (TCHAR *)m_pModule->RenderText();
@@ -128,7 +143,7 @@
                 s += "</b><br /><br />";
 			}
 			else break;
-		} while (true);
+		} while (!fInThread || !m_fAbortThread);
         s += GetVerseHeader(keyCur);
         s += text + " ";
 		
@@ -145,7 +160,7 @@
             RECT r = {0};
             r.left = 0;
             r.right = m_rect.right;
-            r.top = m_rect.bottom - 22;
+            r.top = m_rect.bottom - 12;
             //r.top = 30;
             r.bottom = m_rect.bottom;
             //r.bottom = 60;
@@ -153,11 +168,18 @@
             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);
+            INT nLen = wcsnlen(szStat, 12);
+            //HBRUSH brushBG =  CreateSolidBrush((COLORREF)BUTTON_BACKGROUND);
+            
+            HBRUSH brushPBAR =  CreateSolidBrush((COLORREF)0x00cf8b55);
+			//FillRect(hdc, &r, brushBG);
+			//Rectangle(hdc, r.left, r.top, r.right, r.bottom);
+            r.right = (r.right - r.left) * (percent/100);
+            SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
+            FillRect(hdc, &r, brushPBAR);
 			Rectangle(hdc, r.left, r.top, r.right, r.bottom);
+            SetBkColor(hdc, (COLORREF)0x00cf8b55);
+            
             DrawText(hdc, &szStat[0], nLen, &r, DT_VCENTER | DT_CENTER | DT_SINGLELINE);
             ReleaseDC(m_hWnd, hdc);
         }
@@ -166,7 +188,9 @@
     buf = "</body></html>";
     AddText(buf.w_str(), buf.length());
 
-    RefreshWindow();
+    if(!fInThread || !m_fAbortThread)
+        RefreshWindow();
+
     if(done){
         ScrollToVerse(m_verse.Verse());
     }else if(!fInThread){ // Overkill but, I am a paranoid guy.

Modified: trunk/src/SwordReader_GUI/SRModuleView.h
===================================================================
--- trunk/src/SwordReader_GUI/SRModuleView.h	2008-07-28 06:05:05 UTC (rev 159)
+++ trunk/src/SwordReader_GUI/SRModuleView.h	2008-07-31 04:58:30 UTC (rev 160)
@@ -43,5 +43,8 @@
 
     HANDLE      m_hLoadTextThread;
     DWORD       m_dwLoadTextThreadID;
+    BOOL        m_fThreadRunning;
+    BOOL        m_fAbortThread;
     friend DWORD WINAPI LoadTextThread(LPVOID arg);
+
 };

Modified: trunk/src/SwordReader_GUI/SRTextView.cpp
===================================================================
--- trunk/src/SwordReader_GUI/SRTextView.cpp	2008-07-28 06:05:05 UTC (rev 159)
+++ trunk/src/SwordReader_GUI/SRTextView.cpp	2008-07-31 04:58:30 UTC (rev 160)
@@ -54,6 +54,7 @@
 , m_nLineX(0)
 , m_nLineY(0)
 , m_dwLineNum(0)
+, m_wStatusBarHeight(0)
 {
     m_wcsClassName = "SRTextView";
     m_wcsWindowName = "SwordReader";
@@ -828,6 +829,8 @@
         }
 
         ScrollWindowEx(m_hWnd, 0, nDragDist, NULL, NULL, NULL, &rectUpdateRect, NULL);
+        // Invalidate the status bar area as well, otherwise it leaves artifacts.
+        rectUpdateRect.top -= m_wStatusBarHeight;
         InvalidateRect(m_hWnd, &rectUpdateRect, TRUE);
         ::UpdateWindow(m_hWnd);
 
@@ -859,6 +862,8 @@
         }
 
         ScrollWindowEx(m_hWnd, 0, m_nRollVelocity, NULL, NULL, NULL, &rectUpdateRect, NULL);
+        // Invalidate the status bar area as well, otherwise it leaves artifacts.
+        rectUpdateRect.top -= m_wStatusBarHeight;
         InvalidateRect(m_hWnd, &rectUpdateRect, TRUE);
         ::UpdateWindow(m_hWnd);
         m_nRollVelocity = (m_nRollVelocity - m_nRollVelocity/6) - ((nDirection*m_nRollVelocity < 10) ? nDirection : 0); // the nDirection part ensures continual degrading in the velocity
@@ -900,6 +905,8 @@
             fDone = TRUE;
         }
         ScrollWindowEx(m_hWnd, 0, nDist, NULL, NULL, NULL, &rectUpdateRect, NULL);
+        // Invalidate the status bar area as well, otherwise it leaves artifacts.
+        rectUpdateRect.top -= m_wStatusBarHeight;
         InvalidateRect(m_hWnd, &rectUpdateRect, TRUE);
         ::UpdateWindow(m_hWnd);
     }
@@ -919,6 +926,8 @@
         m_nTop = -pt.y;
 
         ScrollWindowEx(m_hWnd, 0, nDragDist, NULL, NULL, NULL, &rectUpdateRect, NULL);
+        // Invalidate the status bar area as well, otherwise it leaves artifacts.
+        rectUpdateRect.top -= m_wStatusBarHeight;
         InvalidateRect(m_hWnd, &rectUpdateRect, TRUE);
         ::UpdateWindow(m_hWnd);
     }

Modified: trunk/src/SwordReader_GUI/SRTextView.h
===================================================================
--- trunk/src/SwordReader_GUI/SRTextView.h	2008-07-28 06:05:05 UTC (rev 159)
+++ trunk/src/SwordReader_GUI/SRTextView.h	2008-07-31 04:58:30 UTC (rev 160)
@@ -750,5 +750,9 @@
     DWORD                       m_dwLineNum;
     //! Where to start pre-rendering at if the flag m_fAppended is true.
     DWORD                       m_dwWordIndex;
+    /*! If our text display has a status bar we need to know about it so that 
+        we can redraw over it while scrolling.
+     */
+    WORD                        m_wStatusBarHeight;
 
 };

Modified: trunk/src/SwordReader_GUI/SwordReaderResource.h
===================================================================
--- trunk/src/SwordReader_GUI/SwordReaderResource.h	2008-07-28 06:05:05 UTC (rev 159)
+++ trunk/src/SwordReader_GUI/SwordReaderResource.h	2008-07-31 04:58:30 UTC (rev 160)
@@ -66,6 +66,8 @@
 #define BUTTON_OT_FOREGROUND    0x00888800
 #define BUTTON_NT_FOREGROUND    0x00008888
 
+#define SR_STATUS_BAR_HEIGHT    12
 
 
+
 #endif
\ No newline at end of file




More information about the sword-cvs mailing list