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

dtrotzjr at www.crosswire.org dtrotzjr at www.crosswire.org
Tue May 20 10:15:45 MST 2008


Author: dtrotzjr
Date: 2008-05-20 10:15:44 -0700 (Tue, 20 May 2008)
New Revision: 128

Added:
   trunk/src/SwordReader_GUI/SRBookChooser.cpp
   trunk/src/SwordReader_GUI/SRBookChooser.h
Log:
Forgot to add these in a previous commit.

Added: trunk/src/SwordReader_GUI/SRBookChooser.cpp
===================================================================
--- trunk/src/SwordReader_GUI/SRBookChooser.cpp	                        (rev 0)
+++ trunk/src/SwordReader_GUI/SRBookChooser.cpp	2008-05-20 17:15:44 UTC (rev 128)
@@ -0,0 +1,238 @@
+#include "SRBookChooser.h"
+#include "SwordReaderResource.h"
+
+BOOL SRBookChooser::s_fRegistered = false;
+
+SRBookChooser::SRBookChooser(WCString *wcsBookNames, WORD wNextMenuID)
+:m_nEndBook(BIBLE_TOTAL_BOOKS)
+,m_nSelectedBook(0)
+,m_nStartAt(1)
+,m_wcsPrompt("Select a Book:")
+,m_wNextMenuID(wNextMenuID)
+,m_wcsBookNames(wcsBookNames)
+{   
+    m_wcsClassName = "SRBookChooser";
+    m_wcsWindowName = "Book Chooser";
+}
+
+BOOL SRBookChooser::Create(SRWnd *pParentWnd, RECT bounds)
+{
+    if(!Register())
+        return FALSE;
+
+    if(!SRWnd::Create(m_wcsClassName,m_wcsWindowName,WS_CHILD | WS_VISIBLE, bounds, pParentWnd, NULL, m_hInstance))
+        return FALSE;
+
+    return TRUE;
+}
+
+SRBookChooser::~SRBookChooser() 
+{
+}
+
+BOOL SRBookChooser::Register()
+{
+    // Register window class...
+    WNDCLASS    wc;
+    if(s_fRegistered)
+        return TRUE;
+
+    wc.style            = CS_HREDRAW | CS_VREDRAW | CS_PARENTDC;
+    wc.lpfnWndProc      = (WNDPROC) MessageRoute;
+    wc.cbClsExtra       = 0;
+    wc.cbWndExtra       = 0;
+    wc.hInstance        = m_hInstance;
+    wc.hIcon            = NULL;
+    wc.hCursor          = 0;
+    wc.hbrBackground    = (HBRUSH) GetStockObject(WHITE_BRUSH);
+    wc.lpszMenuName     = 0;
+    wc.lpszClassName    = m_wcsClassName.w_str();
+
+    if(RegisterClass(&wc) == 0) 
+        return FALSE;
+    s_fRegistered = TRUE;
+
+    return TRUE;
+}
+
+INT SRBookChooser::MaxCols()
+{
+    RECT clientRect;
+    ::GetClientRect(m_hWnd,&clientRect);
+    return ( ((clientRect.right  - BUTTON_PADDING_WIDTH) - clientRect.left)/(BUTTON_PADDING_WIDTH + BUTTON_WIDTH_BOOK) );
+}
+
+INT SRBookChooser::MaxRows()
+{
+    RECT clientRect;
+    INT nMaxBooks = 0; 
+    INT nMaxRows = 0;
+    ::GetClientRect(m_hWnd,&clientRect);
+    
+    // 2 less rows due to the Prompt and the More Prev buttons.
+    nMaxRows =  ( ((clientRect.bottom - BUTTON_PADDING_HEIGHT) - clientRect.top)/(BUTTON_PADDING_HEIGHT + BUTTON_HEIGHT) ) - 2;
+    nMaxBooks =  MaxCols() * nMaxRows;
+
+    if(m_nStartAt == 1 && m_nEndBook <= nMaxBooks + MaxCols())
+        nMaxRows++; // We can fit another row since there is no need for a More or Prev button.
+    return nMaxRows;
+
+}
+
+INT SRBookChooser::MaxBooksPerScreen()
+{
+    return MaxCols() * MaxRows();
+}
+
+// Tries to center the buttons by calculating a left edge
+INT SRBookChooser::LeftEdge()
+{
+    RECT clientRect;
+    GetClientRect(m_hWnd, &clientRect);
+    return ((clientRect.right - clientRect.left)/2) - 
+        ((MaxCols()*BUTTON_WIDTH_BOOK + (MaxCols() - 1)*BUTTON_PADDING_WIDTH)/2 );
+}
+BOOL SRBookChooser::OnPaint() {
+    RECT buttonRect;
+    RECT clientRect;
+    INT nCurrent = m_nStartAt;
+    INT nColStart = 0;
+    PAINTSTRUCT ps;
+    INT nRow;
+    INT nCol;
+    INT nMaxCols = MaxCols();
+    INT nMaxRows = MaxRows();
+
+    HDC hdc = BeginPaint(m_hWnd, &ps);
+    
+    HBRUSH brushBG =  CreateSolidBrush((COLORREF)BUTTON_BACKGROUND);
+    GetClientRect(m_hWnd, &clientRect);
+    FillRect(hdc, &clientRect, (HBRUSH)GetStockObject(WHITE_BRUSH));
+    
+    // Draw the prompt.
+    buttonRect.top = BUTTON_PADDING_HEIGHT;
+    buttonRect.left = LeftEdge();
+    buttonRect.right = clientRect.right - LeftEdge();
+    buttonRect.bottom = buttonRect.top + BUTTON_HEIGHT;
+    DrawText(hdc, m_wcsPrompt.w_str(), -1, &buttonRect, DT_CENTER | DT_VCENTER);
+
+    // Init the first button's bounds
+    buttonRect.top = 2*BUTTON_PADDING_HEIGHT + BUTTON_HEIGHT;
+    buttonRect.bottom = buttonRect.top + BUTTON_HEIGHT;
+    buttonRect.left = LeftEdge();
+    buttonRect.right = buttonRect.left + BUTTON_WIDTH_BOOK;
+    
+    SetBkColor(hdc, BUTTON_BACKGROUND);
+
+    for(nRow = 0; nRow < nMaxRows; nRow++){
+        for(nCol = 0; nCol < nMaxCols; nCol++){
+            nCurrent = m_nStartAt + (nRow * nMaxCols) + nCol;
+            FillRect(hdc, &buttonRect, brushBG); 
+            DrawText(hdc, m_wcsBookNames[nCurrent - 1].w_str(), -1, &buttonRect, DT_CENTER | DT_VCENTER);
+            if(nCurrent == m_nEndBook)
+                break;
+            // Move the bounds right.
+            buttonRect.left += BUTTON_WIDTH_BOOK + BUTTON_PADDING_WIDTH;
+            buttonRect.right = buttonRect.left + BUTTON_WIDTH_BOOK;
+
+        }
+        // Move the bounds down and all the way back to the left.
+        buttonRect.left = LeftEdge();
+        buttonRect.right = buttonRect.left + BUTTON_WIDTH_BOOK;
+        buttonRect.top += BUTTON_HEIGHT + BUTTON_PADDING_HEIGHT;
+        buttonRect.bottom = buttonRect.top + BUTTON_HEIGHT;
+        if(nCurrent == m_nEndBook)
+           break;
+    }
+    
+    // If we didn't reach the end we need to draw the More button...
+    if(nCurrent < m_nEndBook){
+        buttonRect.left = clientRect.right - (BUTTON_WIDTH_MORE + LeftEdge());
+        buttonRect.right = buttonRect.left + BUTTON_WIDTH_MORE;
+        buttonRect.top = clientRect.bottom - (BUTTON_HEIGHT + BUTTON_PADDING_HEIGHT);
+        buttonRect.bottom = clientRect.bottom - BUTTON_PADDING_HEIGHT;
+        
+        FillRect(hdc, &buttonRect, brushBG); 
+        DrawText(hdc, L"More >>", -1, &buttonRect, DT_CENTER | DT_VCENTER);  
+    }
+    // We are not on the first page of Books we need to draw a Prev button...
+    if(m_nStartAt != 1){
+        buttonRect.left = LeftEdge();
+        buttonRect.right = buttonRect.left + BUTTON_WIDTH_MORE;
+        buttonRect.top = clientRect.bottom - (BUTTON_HEIGHT + BUTTON_PADDING_HEIGHT);
+        buttonRect.bottom = clientRect.bottom - BUTTON_PADDING_HEIGHT;
+        
+        FillRect(hdc, &buttonRect, brushBG); 
+        DrawText(hdc, L"<< Prev", -1, &buttonRect, DT_CENTER | DT_VCENTER);  
+    }
+    //Clean up.
+    DeleteObject(brushBG);
+    EndPaint(m_hWnd,&ps);
+    return TRUE;
+}
+/* Return:
+ * > 0  - the Book found.
+ *   0  - More button pressed.
+ *  -1  - Prev button pressed.
+ *  -2  - White space tapped. Ignore
+ */
+INT SRBookChooser::BookAt(int x, int y) 
+{
+    RECT clientRect;
+    INT nCols = (x - BUTTON_PADDING_WIDTH) / (BUTTON_WIDTH_BOOK + BUTTON_PADDING_WIDTH);
+    INT nRows = (y - BUTTON_PADDING_HEIGHT)/ (BUTTON_HEIGHT + BUTTON_PADDING_HEIGHT) - 1;
+    GetClientRect(m_hWnd, &clientRect);
+    // I ignore the minimal amount of white space between the buttons.
+    INT nBook = m_nStartAt + (nRows * MaxCols()) + nCols; 
+    
+    if(y < BUTTON_HEIGHT + 2*BUTTON_PADDING_HEIGHT)
+        return -2; // Tapped the title area.
+
+    // Check if the tap was on a button...
+    if(y > clientRect.bottom - (BUTTON_HEIGHT + BUTTON_PADDING_HEIGHT) ){
+        if(x > BUTTON_PADDING_WIDTH && x < BUTTON_PADDING_WIDTH + BUTTON_WIDTH_MORE){
+            return -1;
+        }else if( (x > (MaxCols()*(BUTTON_WIDTH_BOOK+BUTTON_PADDING_WIDTH) - BUTTON_WIDTH_MORE)) &&
+                  (x < (MaxCols()*(BUTTON_WIDTH_BOOK+BUTTON_PADDING_WIDTH))) ){
+            return 0;
+        }else
+            return -2;
+    }
+    if(nBook > m_nEndBook || nBook >= m_nStartAt + MaxBooksPerScreen())
+        return -2;
+
+    return nBook;
+}
+
+BOOL SRBookChooser::OnLButtonUp(WORD fwKeys, INT xPos, INT yPos)
+{
+    TCHAR buf[16] = {0};
+    INT found = BookAt(xPos, yPos);
+    if(found == 0 && (m_nStartAt + MaxBooksPerScreen() <= m_nEndBook) ){
+        m_nStartAt += MaxBooksPerScreen();
+        RefreshWindow();
+    }else if(found == -1 && m_nStartAt != 1){
+        m_nStartAt -= MaxBooksPerScreen();
+        RefreshWindow();
+    }else if(found == -2){
+        return TRUE;
+    }else if(found != 0 && found != -1){
+        m_nSelectedBook = found;
+        // We also send the value found, thus when this messge is recv'd the new value can be processed. 
+        ::SendMessage(m_pParentWnd->GetWindowHandle(),WM_COMMAND,SR_SETBOOK, found);
+        ::SendMessage(m_pParentWnd->GetWindowHandle(),WM_COMMAND,m_wNextMenuID, found);
+    }
+    return TRUE;
+}
+void SRBookChooser::SetSelectedBook(INT nSelectedBook)
+{
+    if(nSelectedBook < 0 || nSelectedBook > m_nEndBook)
+        m_nSelectedBook = 0;
+    else
+        m_nSelectedBook = nSelectedBook;
+}
+
+void SRBookChooser::SetEndBook(INT nEndBook)
+{
+    m_nEndBook = nEndBook;
+}
\ No newline at end of file

Added: trunk/src/SwordReader_GUI/SRBookChooser.h
===================================================================
--- trunk/src/SwordReader_GUI/SRBookChooser.h	                        (rev 0)
+++ trunk/src/SwordReader_GUI/SRBookChooser.h	2008-05-20 17:15:44 UTC (rev 128)
@@ -0,0 +1,36 @@
+#ifndef SRBOOKCHOOSER_H
+#define SRBOOKCHOOSER_H
+
+#include "SRFramework/SRWnd.h"
+using namespace SRFramework;
+
+class SRBookChooser: public SRWnd {
+public:
+	SRBookChooser(WCString *wcsBookNames, WORD wNextMenuID);
+	virtual ~SRBookChooser();
+	
+	// redraw the screen. This should use methods in ApplicationInterface.h to do the drawing
+	BOOL OnPaint();
+    INT GetSelectedBook() { return m_nSelectedBook; }
+    void SetSelectedBook(INT nSelectedBook);
+    void SetEndBook(INT nEndBook);
+    BOOL Register();
+    BOOL Create(SRWnd *pParentWnd, RECT bounds);
+    BOOL OnLButtonUp(WORD fwKeys, INT xPos, INT yPos);
+protected:
+	INT BookAt(int x, int y);
+    INT LeftEdge();
+	INT MaxRows();
+    INT MaxCols();
+    INT MaxBooksPerScreen();
+    
+	INT      m_nEndBook;
+    INT      m_nStartAt;
+    INT      m_nSelectedBook;
+    WORD     m_wNextMenuID;
+	WCString m_wcsPrompt;
+    WCString *m_wcsBookNames;
+    static BOOL s_fRegistered;
+};
+
+#endif




More information about the sword-cvs mailing list