[sword-svn] r104 - in trunk/src: gui simplegui

dtrotzjr at www.crosswire.org dtrotzjr at www.crosswire.org
Tue Mar 25 20:40:37 MST 2008


Author: dtrotzjr
Date: 2008-03-25 20:40:36 -0700 (Tue, 25 Mar 2008)
New Revision: 104

Added:
   trunk/src/gui/SRWidget.cpp
   trunk/src/gui/SRWidget.h
   trunk/src/gui/SwordReaderApp.cpp
   trunk/src/gui/SwordReaderApp.h
Modified:
   trunk/src/gui/ApplicationInterface.cpp
   trunk/src/gui/ApplicationInterface.h
   trunk/src/gui/Main.cpp
   trunk/src/gui/Main.h
   trunk/src/gui/NavFind.cpp
   trunk/src/gui/NavFind.h
   trunk/src/gui/SwordIndex.h
   trunk/src/simplegui/simplegui.vcproj
Log:
First round of restructuring. This commit does not build.

Modified: trunk/src/gui/ApplicationInterface.cpp
===================================================================
--- trunk/src/gui/ApplicationInterface.cpp	2008-03-24 04:31:10 UTC (rev 103)
+++ trunk/src/gui/ApplicationInterface.cpp	2008-03-26 03:40:36 UTC (rev 104)
@@ -35,93 +35,4 @@
 void closeApplicationInterface() {
     DeleteObject(textFont);
     DeleteObject(navFont);
-}
-
-void loadString(UINT id, LPTSTR buffer) {
-	LoadString(g_hInst, id, buffer, MAX_LOADSTRING);
-}
-
-WCString loadString(UINT id) {
-	TCHAR chars[MAX_LOADSTRING];
-	loadString(id,chars);
-	return WCString(chars);
-}
-
-void setTitle(const WCString &title) {
-	SetWindowText(g_hWnd,title.w_str());
-}
-
-void refreshScreen() {
-	InvalidateRect(g_hWnd, NULL, TRUE);
-	UpdateWindow(NULL);
-}
-
-void drawText(RECT* rt, const WCString &text) {
-	drawText(rt,text.w_str(), text.length());
-}
-
-void drawRText(RECT* rt, const WCString &text) {
-	drawRText(rt,text.w_str(), text.length());
-}
-
-#define MARGIN 2
-
-int drawVerseText(RECT* rt, const WCString &text) {
-	RECT tmp=*rt;
-	tmp.left+=MARGIN;
-	DrawText(hdc, text.w_str(),text.length(),&tmp,DT_LEFT|DT_WORDBREAK|DT_CALCRECT);
-	tmp.left=rt->left;
-	tmp.right=rt->right;
-	clearRect(&tmp);
-	tmp.left+=MARGIN;
-	DrawText(hdc, text.w_str(),text.length(),&tmp,DT_LEFT|DT_WORDBREAK);
-	return (tmp.bottom)-(tmp.top);
-}
-
-void setFont(int id) {
-	HFONT newFont;
-	switch (id) {
-		case FONT_NAVIGATION: newFont=navFont; break;
-		case FONT_TEXT: newFont=textFont; break;
-		default: id=FONT_DEFAULT; newFont=defaultFont;
-	}
-	if ((font==FONT_DEFAULT)&&(id!=FONT_DEFAULT)) //let's remember the default font
-		defaultFont=(HFONT)SelectObject(hdc,newFont);
-	else if (font!=id)
-		SelectObject(hdc,newFont);
-}
-
-void selectMenu(std::map<int,int>& menus, int mode) {
-	for (int i=0;i<menus.size();++i) {
-		setMenuSelected(g_hwndCB,menus[i],(mode==i));
-	}
-/*	setMenuSelected(g_hwndCB,MENU_BOOK,(mode==MENU_BOOK));
-	setMenuSelected(g_hwndCB,MENU_CHAP,(mode==MENU_CHAP));
-	setMenuSelected(g_hwndCB,MENU_VERSE,(mode==MENU_VERSE));
-	setMenuSelected(g_hwndCB,MENU_TEXT,(mode==MENU_TEXT));
-	setMenuSelected(g_hwndCB,MENU_FIND,(mode==MENU_FIND));
-*/
-}
-
-void addMenu(HMENU menu,int flags, int id, const WCString &text) {
-	AppendMenu(menu,flags,USERBUTTONS+id,text.w_str());
-}
-
-
-HMENU registerID(int id) {
-	return (HMENU) (USERBUTTONS+id);
-}
-
-
-
-void checkMenu(HMENU menu, int id, bool checked) {
-	CheckMenuItem(menu,USERBUTTONS+id,checked?MF_CHECKED:MF_UNCHECKED);
-}
-
-
-WCString getText(HWND edit) {
-	TCHAR chars[MAX_LOADSTRING];
-	SendMessage(edit,WM_GETTEXT,(WPARAM)MAX_LOADSTRING,(LPARAM)chars);
-	chars[MAX_LOADSTRING-1]=0; // in case the buffer was exceeded
-	return WCString(chars);
 }
\ No newline at end of file

Modified: trunk/src/gui/ApplicationInterface.h
===================================================================
--- trunk/src/gui/ApplicationInterface.h	2008-03-24 04:31:10 UTC (rev 103)
+++ trunk/src/gui/ApplicationInterface.h	2008-03-26 03:40:36 UTC (rev 104)
@@ -1,32 +1,27 @@
 #ifndef APPLICATIONINTERFACE_H
 #define APPLICATIONINTERFACE_H
-
+// This file contains a wrapper for the windows CE functions that are needed for the gui
 #if _MSC_VER > 1000
 #pragma once
 #endif // _MSC_VER > 1000
-
-// This file contains a wrapper for the windows CE functions that are needed for the gui
-
 #define WIN32_LEAN_AND_MEAN
-
 #include <windows.h>
+#include <aygshell.h>
+#include <htmlctrl.h>
 
 #include "utils.h"
 #include "WCString.h"
+#include "swordce.h"
+#include "../simplegui/resource.h"
 
+
 #define setMenuSelected(hWndMB, idButton, checked) SendMessage((hWndMB), TB_CHECKBUTTON,\
 	(WPARAM)idButton, (LPARAM)MAKELONG(checked,0));
 #define getMenu(hWndMB)  (HMENU)SendMessage((hWndMB), SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
 #define getSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU);
 #define setSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_SETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU);
+#define registerID(id) (HMENU)(USERBUTTONS+id)
 
-extern HDC			hdc;		// Only valid when painting
-extern HBRUSH		background;	
-extern DWORD		g_tMain;
-extern HINSTANCE	g_hInst;	// The current instance
-extern HWND			g_hWnd;		// The main window
-extern HWND			g_hwndCB;
-
 #define WM_TXT_START WM_USER + 0x00F1
 #define WM_TXT_END   WM_USER + 0x00F2
 
@@ -45,7 +40,7 @@
 
 #define PADDING_WIDTH 3
 #define PADDING_HEIGHT 3
-
+/*
 void initApplicationInterface();
 
 void closeApplicationInterface();
@@ -108,4 +103,5 @@
 
 WCString getText(HWND edit);
 
+*/
 #endif

Modified: trunk/src/gui/Main.cpp
===================================================================
--- trunk/src/gui/Main.cpp	2008-03-24 04:31:10 UTC (rev 103)
+++ trunk/src/gui/Main.cpp	2008-03-26 03:40:36 UTC (rev 104)
@@ -1,6 +1,7 @@
 #include "ApplicationInterface.h"
 #include "Main.h"
 #include "swordce.h"
+#include "SwordReaderApp.h"
 
 #ifdef SIMPLE
 	#include "SimpleNavigator.h"
@@ -24,6 +25,7 @@
 					LPTSTR    lpCmdLine,
 					int       nCmdShow)
 {
+    SwordReaderApp *app = new SwordReaderApp();
 	MSG msg;
 	HACCEL hAccelTable;
 	bool ignoreMsgs = false;
@@ -32,17 +34,16 @@
 	// Some visual indication things are taking place...
 	HCURSOR hOldCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
 	
-	 g_tMain = GetCurrentThreadId();
 	// Perform application initialization:
-	if (!InitInstance (hInstance, nCmdShow)) 
-	{
-		return FALSE;
-	}
+	if (!app->InitApp(hInstance, nCmdShow)) 
+		return FALSE; // Either it failed, or it's already loaded...
 
 	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BIBLEREADER);
-	// Turn off the wait icon...
+	
+    // Turn off the wait icon...
 	SetCursor(hOldCursor);
-	// Main message loop:
+	
+    // Main message loop:
 	while (GetMessage(&msg, NULL, 0, 0)) 
 	{
 		if(msg.message == WM_TXT_START){ 
@@ -65,257 +66,6 @@
 	return msg.wParam;
 }
 
-ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
-{
-	WNDCLASS	wc;
-
-    wc.style			= CS_HREDRAW | CS_VREDRAW;
-    wc.lpfnWndProc		= (WNDPROC) WndProc;
-    wc.cbClsExtra		= 0;
-    wc.cbWndExtra		= 0;
-    wc.hInstance		= hInstance;
-    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BIBLEREADER));
-    wc.hCursor			= 0;
-    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH); // Setting this does not prevent us from drawing our ownbackground. dtrotzjr
-	wc.lpszMenuName		= 0;
-    wc.lpszClassName	= szWindowClass;
-
-	return RegisterClass(&wc);
-}
-
-BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
-{
-	g_hWnd = NULL;
-	g_hwndCB = NULL;
-	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
-	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name
-
-	g_hInst = hInstance;		// Store instance handle in our global variable
-	
-	// Create a general options file for storing options 
-	// and navigaiton history
-	const char *cwd = getWorkingDirectory();
-	char confFName[MAX_PATH];
-	_snprintf(confFName, MAX_PATH, "%s\\options.conf", cwd);
-
-	g_swordConf = new SWConfig(confFName);
-	g_swordConf->Load();
-
-	// Initialize global strings
-	LoadString(hInstance, IDC_BIBLEREADER, szWindowClass, MAX_LOADSTRING);
-	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
-
-	//If it is already running, then focus on the window
-	g_hWnd = FindWindow(szWindowClass, NULL);	
-	if (g_hWnd) 
-	{
-		// set focus to foremost child window
-		// The "| 0x01" is used to bring any owned windows to the foreground and
-		// activate them.
-		SetForegroundWindow((HWND)((ULONG) g_hWnd | 0x00000001));
-		return 0;
-	} 
-
-	MyRegisterClass(hInstance, szWindowClass);
-	
-	RECT	rect;
-	GetClientRect(g_hWnd, &rect);
-
-	g_hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
-		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
-	if (!g_hWnd)
-	{	
-		return FALSE;
-	}
-	//When the main window is created using CW_USEDEFAULT the height of the menubar (if one
-	// is created is not taken into account). So we resize the window after creating it
-	// if a menubar is present
-	{
-		RECT rc;
-		GetWindowRect(g_hWnd, &rc);
-		rc.bottom -= MENU_HEIGHT;
-		if (g_hwndCB)
-			MoveWindow(g_hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
-	}
-	
-	if (!g_navigator->initialized()) {
-		DialogBox(g_hInst, (LPCTSTR)IDD_ERROR_NOBIBLES, g_hWnd, (DLGPROC)Error);
-		return FALSE;
-	}
-	
-	initApplicationInterface();
-
-	g_navigator->updateTitle();
-
-	ShowWindow(g_hWnd, nCmdShow);
-	g_navigator->setMode(MODE_TEXT);
-	SetForegroundWindow((HWND)((ULONG) g_hWnd | 0x00000001));
-	UpdateWindow(g_hWnd);
-
-	return TRUE;
-}
-
-LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
-{
-	int wmId, wmEvent;
-	PAINTSTRUCT ps;
-	NMHDR *nm;
-
-	switch (message) 
-	{
-		case WM_NOTIFY:
-			nm = (NMHDR *)lParam;
-			switch (nm->code) {
-			case NM_HOTSPOT:
-				g_navigator->urlClicked(((NM_HTMLVIEW *)lParam)->szTarget);
-			}
-		break;
-		case WM_LBUTTONDOWN:
-			g_navigator->mouseDown(LOWORD(lParam),HIWORD(lParam));//x,y
-			break;
-		case WM_KEYDOWN:
-			g_navigator->keyDown(wParam, lParam);
-			break;
-		
-		case WM_COMMAND:
-			wmId    = LOWORD(wParam); 
-			wmEvent = HIWORD(wParam); 
-			switch (wmId) {	
-				case IDOK:
-					SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd);
-					SendMessage (hWnd, WM_CLOSE, 0, 0);
-					break;
-				case MENU_ABOUT:
-					MessageBox(0, L"Sword reader program for Pocket PC ver. 1.0.1\n\nsee: http://www.crosswire.org/sword/swordreader/\n\nGUI by Johan Gorter, 2004\nNow being maintained by B. Drake, Robin Randall and David Trotz\nProverbs 16:3", L"About", MB_OK);
-					break;
-				case MENU_SHUTDOWN:
-					SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd);
-					SendMessage (hWnd, WM_CLOSE, 0, 0);
-					exit(0);
-				default:
-					if ((wmId>=USERBUTTONS)&&(wmId<USERBUTTONS+1000)) {
-						g_navigator->buttonClicked(wmId-USERBUTTONS);
-						break;
-					} 
-					else if (g_navigator->menuClicked(wmId))
-						{}
-					else
-						return DefWindowProc(hWnd, message, wParam, lParam);
-					break;
-			}
-			break;
-		case WM_CREATE:
-			g_hWnd=hWnd;
-			RECT clientRect;
-			GetClientRect(g_hWnd,&clientRect);
-			clientRect.bottom -= MENU_HEIGHT;
-			g_navigator=new NAVIGATOR(&clientRect);
-			g_hwndCB = CreateRpCommandBar(hWnd);
-			setMenuSelected(g_hwndCB,MENU_TEXT,1);
-			// Fill up the translation popup menu
-			makeTranslations();
-			// Fill up the options popup menu
-			makeGlobalOptions();
-            // Initialize the shell activate info structure
-            memset (&s_sai, 0, sizeof (s_sai));
-            s_sai.cbSize = sizeof (s_sai);
-			break;
-		case WM_PAINT:
-			hdc = BeginPaint(hWnd, &ps);
-			SetBkMode(hdc,OPAQUE);
-			SetBkColor(hdc,(COLORREF)GetSysColor(COLOR_WINDOW));
-			g_navigator->paint();
-			EndPaint(hWnd, &ps);
-			break; 
-		case WM_DESTROY:
-			CommandBar_Destroy(g_hwndCB);
-			delete g_navigator;
-			PostQuitMessage(0);
-			g_swordConf->Save();
-			delete g_swordConf;
-            closeApplicationInterface();
-			break;
-		case WM_ACTIVATE:
-	        // Notify shell of our activate message
-			SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
-     		break;
-		case WM_SETTINGCHANGE:
-			SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
-     		break;
-		case WM_SIZE:
-			
-			break;
-		default:
-			return DefWindowProc(hWnd, message, wParam, lParam);
-   }
-   return 0;
-}
-
-HWND CreateRpCommandBar(HWND hwnd)
-{
-	SHMENUBARINFO mbi;
-
-	memset(&mbi, 0, sizeof(SHMENUBARINFO));
-	mbi.cbSize     = sizeof(SHMENUBARINFO);
-	mbi.hwndParent = hwnd;
-	mbi.nToolBarId = IDM_MENU;
-	mbi.hInstRes   = g_hInst;
-	mbi.nBmpId     = 0;
-	mbi.cBmpImages = 0;
-
-	if (!SHCreateMenuBar(&mbi)) 
-		return NULL;
-
-	return mbi.hwndMB;
-}
-
-void makeTranslations() {
-	HMENU menuMenu = getSubMenu(g_hwndCB,MENU_MENU);
-	
-	HMENU menuTranslations=CreatePopupMenu();
-	InsertMenu(menuMenu,0,MF_BYPOSITION | MF_POPUP,(UINT)menuTranslations,L"Translation");
-	g_navigator->fillTranslationsMenu(menuTranslations);
-
-	DrawMenuBar(g_hwndCB);
-}
-
-void makeGlobalOptions() {
-	HMENU menuMenu = getSubMenu(g_hwndCB,MENU_MENU);
-	
-	HMENU menuGlobalOptions=CreatePopupMenu();
-	InsertMenu(menuMenu,0,MF_BYPOSITION | MF_POPUP,(UINT)menuGlobalOptions,L"Options");
-	g_navigator->fillGlobalOptionsMenu(menuGlobalOptions);
-	g_navigator->toggleOptionMenu(-1);	// get all checked menu choiced correct, but don't change anything
-
-	DrawMenuBar(g_hwndCB);
-}
-
-LRESULT CALLBACK Error(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
-{
-	SHINITDLGINFO shidi;
-
-	switch (message)
-	{
-		case WM_INITDIALOG:
-			// Create a Done button and size it.  
-			shidi.dwMask = SHIDIM_FLAGS;
-			shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
-			shidi.hDlg = hDlg;
-			SHInitDialog(&shidi);
-
-			return TRUE; 
-
-		case WM_COMMAND:
-			if (LOWORD(wParam) == IDOK)
-			{
-				EndDialog(hDlg, LOWORD(wParam));
-				return TRUE;
-			}
-			break;
-	}
-    return FALSE;
-}
-
 namespace sword {
 	char *SWBuf::nullStr = "";
 }

Modified: trunk/src/gui/Main.h
===================================================================
--- trunk/src/gui/Main.h	2008-03-24 04:31:10 UTC (rev 103)
+++ trunk/src/gui/Main.h	2008-03-26 03:40:36 UTC (rev 104)
@@ -13,17 +13,4 @@
 					LPTSTR    lpCmdLine,
 					int       nCmdShow);
 
-ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass);
-
-BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
-
-LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
-
-HWND CreateRpCommandBar(HWND hwnd);
-
-void makeTranslations();
-void makeGlobalOptions();
-
-LRESULT CALLBACK Error(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
-
 #endif 

Modified: trunk/src/gui/NavFind.cpp
===================================================================
--- trunk/src/gui/NavFind.cpp	2008-03-24 04:31:10 UTC (rev 103)
+++ trunk/src/gui/NavFind.cpp	2008-03-26 03:40:36 UTC (rev 104)
@@ -261,3 +261,9 @@
 	navigator->position.search(query,rangeMode,methodMode,this);
 }
 
+WCString NavFind::GetSearchText(HWND edit) {
+	TCHAR chars[MAX_LOADSTRING];
+	SendMessage(edit,WM_GETTEXT,(WPARAM)MAX_LOADSTRING,(LPARAM)chars);
+	chars[MAX_LOADSTRING-1]=0; // in case the buffer was exceeded
+	return WCString(chars);
+}

Modified: trunk/src/gui/NavFind.h
===================================================================
--- trunk/src/gui/NavFind.h	2008-03-24 04:31:10 UTC (rev 103)
+++ trunk/src/gui/NavFind.h	2008-03-26 03:40:36 UTC (rev 104)
@@ -24,6 +24,7 @@
 	virtual void setProgress(int percent);
 	virtual void addResult(const WCString &verseStr);
 	void placeWidgets();
+    WCString GetSearchText(HWND edit);
 private:
 	//Navigator* navigator;
 	std::vector<Verse> verses;

Added: trunk/src/gui/SRWidget.cpp
===================================================================
--- trunk/src/gui/SRWidget.cpp	                        (rev 0)
+++ trunk/src/gui/SRWidget.cpp	2008-03-26 03:40:36 UTC (rev 104)
@@ -0,0 +1,23 @@
+#include "SRWidget.h"
+
+SRWidget::SRWidget(void)
+{
+}
+
+SRWidget::~SRWidget(void)
+{
+}
+
+COLORREF SRWidget::setBackColor(HDC hdc, COLORREF  color)
+{
+    m_backColor = color;
+    return SetBkColor(hdc, m_backColor);
+}
+
+COLORREF SRWidget::setForeColor(HDC hdc, COLORREF  color)
+{
+    m_foreColor = color;
+    return SetTextColor(hdc, m_foreColor);
+}
+
+void SRWidget::SetFont(HFONT font) { /*TODO*/ }
\ No newline at end of file

Added: trunk/src/gui/SRWidget.h
===================================================================
--- trunk/src/gui/SRWidget.h	                        (rev 0)
+++ trunk/src/gui/SRWidget.h	2008-03-26 03:40:36 UTC (rev 104)
@@ -0,0 +1,21 @@
+#pragma once
+#include <windows.h>
+
+class SRWidget
+{
+public:
+    SRWidget(void);
+    virtual ~SRWidget(void);
+protected:
+    // We may not want to cache the color. 
+    // One reason we may is to track the preferred
+    // back color compared to say the color of a rectangle.
+    COLORREF    m_backColor;
+    COLORREF    m_foreColor;
+    HFONT       m_curFont;
+    HFONT       m_oldFont;
+public:
+    COLORREF setBackColor(HDC hdc, COLORREF color);
+    COLORREF setForeColor(HDC hdc, COLORREF color);
+    void SetFont(HFONT font);
+};

Modified: trunk/src/gui/SwordIndex.h
===================================================================
--- trunk/src/gui/SwordIndex.h	2008-03-24 04:31:10 UTC (rev 103)
+++ trunk/src/gui/SwordIndex.h	2008-03-26 03:40:36 UTC (rev 104)
@@ -7,6 +7,7 @@
 
 #include "resource.h"
 #include "utils.h"
+#include "WCString.h"
 #include <versekey.h>
 #include <ztext.h>
 #include <swmgr.h>

Added: trunk/src/gui/SwordReaderApp.cpp
===================================================================
--- trunk/src/gui/SwordReaderApp.cpp	                        (rev 0)
+++ trunk/src/gui/SwordReaderApp.cpp	2008-03-26 03:40:36 UTC (rev 104)
@@ -0,0 +1,349 @@
+#include "SwordReaderApp.h"
+
+SHACTIVATEINFO SwordReaderApp::s_sai;
+
+SwordReaderApp::SwordReaderApp()
+: m_navigator(NULL)
+, m_swordConf(NULL)
+, m_dwAppThreadID(0)
+, m_hInst(0)
+, m_hWnd(0)
+{
+    memset(&s_sai, 0, sizeof(s_sai));
+    s_sai.cbSize = sizeof(s_sai);
+}
+
+SwordReaderApp::~SwordReaderApp()
+{
+}
+
+bool SwordReaderApp::InitApp(HINSTANCE hInstance, int nCmdShow)
+{
+    WNDCLASS	wc;
+	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
+	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name
+    
+    m_dwAppThreadID = GetCurrentThreadId();
+	m_hInst = hInstance;
+	
+	// Create a general options file for storing options 
+	// and navigaiton history
+	const char *cwd = getWorkingDirectory();
+	char confFName[MAX_PATH];
+	_snprintf(confFName, MAX_PATH, "%s\\options.conf", cwd);
+
+	m_swordConf = new SWConfig(confFName);
+	m_swordConf->Load();
+
+	// Initialize global strings
+	LoadString(m_hInst, IDC_BIBLEREADER, szWindowClass, MAX_LOADSTRING);
+	LoadString(m_hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
+
+	//If it is already running, then focus on the window
+	m_hWnd = FindWindow(szWindowClass, NULL);	
+	if (m_hWnd) 
+	{
+		// set focus to foremost child window
+		// The "| 0x01" is used to bring any owned windows to the foreground and
+		// activate them.
+		SetForegroundWindow((HWND)((ULONG) m_hWnd | 0x00000001));
+		return FALSE;
+	} 
+
+
+    // Register the main app window class...
+    wc.style			= CS_HREDRAW | CS_VREDRAW;
+    wc.lpfnWndProc		= (WNDPROC) SwordReaderApp::MessageRoute;
+    wc.cbClsExtra		= 0;
+    wc.cbWndExtra		= 0;
+    wc.hInstance		= m_hInst;
+    wc.hIcon			= LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_BIBLEREADER));
+    wc.hCursor			= 0;
+    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
+	wc.lpszMenuName		= 0;
+    wc.lpszClassName	= szWindowClass;
+
+    if(!RegisterClass(&wc)){
+        return FALSE;
+    }
+
+	m_hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
+		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, m_hInst, NULL);
+
+	if (!m_hWnd){	
+		return FALSE;
+	}
+	//When the main window is created using CW_USEDEFAULT the height of the menubar (if one
+	// is created is not taken into account). So we resize the window after creating it
+	// if a menubar is present
+	RECT rc;
+	GetWindowRect(m_hWnd, &rc);
+	rc.bottom -= MENU_HEIGHT;
+	if (m_hwndCB)
+	    MoveWindow(m_hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
+	
+	if (!m_navigator->initialized()) {
+        // I am not convinced that the statement (LPCTSTR)IDD_ERROR_NOBIBLES is correct. -- REVISIT
+        MessageBox(m_hWnd, L"Error: No bibles installed.", L"Error", MB_OK | MB_ICONEXCLAMATION);
+		return FALSE;
+	}
+
+	m_backColor = 0x00FFFFFF;
+    m_foreColor = 0x00000000;
+
+	m_navigator->updateTitle();
+
+	ShowWindow(m_hWnd, nCmdShow);
+	m_navigator->setMode(MODE_TEXT);
+	SetForegroundWindow((HWND)((ULONG) m_hWnd | 0x00000001));
+	UpdateWindow(m_hWnd);
+
+	return TRUE;
+
+}
+
+/* Routes the messages to the appropiate WindowProcedure by unwrapping the lParam 
+* we associated with this window upon creation.
+*/
+LRESULT CALLBACK SwordReaderApp::MessageRoute(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
+{
+    SwordReaderApp*    wnd = NULL;
+
+    if (message == WM_CREATE) {	
+        ::SetWindowLong (hwnd, GWL_USERDATA, long((LPCREATESTRUCT(lParam))->lpCreateParams));
+    }
+
+    wnd = (SwordReaderApp*) (::GetWindowLong (hwnd, GWL_USERDATA));
+
+    if (wnd)
+        return wnd->WndProc(hwnd, message, wParam, lParam);
+    return ::DefWindowProc(hwnd, message, wParam, lParam);
+}
+
+BOOL SwordReaderApp::OnNotify(WPARAM wParam, LPARAM lParam){
+    NMHDR *nm = (NMHDR *)lParam;
+
+    switch (nm->code) {
+	    case NM_HOTSPOT:
+		    m_navigator->urlClicked(((NM_HTMLVIEW *)lParam)->szTarget);
+            return TRUE;
+	}
+    return FALSE;
+}
+
+BOOL SwordReaderApp::OnLButtonDown(WPARAM wParam, LPARAM lParam)
+{
+    m_navigator->mouseDown(LOWORD(lParam),HIWORD(lParam));
+    return TRUE;
+}
+
+BOOL SwordReaderApp::OnKeyDown(WPARAM wParam, LPARAM lParam)
+{
+    m_navigator->keyDown(wParam, lParam);
+    return TRUE;
+}
+
+BOOL SwordReaderApp::OnCommand(UINT message, WPARAM wParam, LPARAM lParam)
+{
+    BOOL handled = FALSE;
+    INT wmId    = LOWORD(wParam); 
+    INT wmEvent = HIWORD(wParam); 
+
+    switch (wmId) {	
+        case IDOK:
+            SendMessage(m_hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)m_hWnd);
+            SendMessage(m_hWnd, WM_CLOSE, 0, 0);
+            handled = TRUE;
+            break;
+        case MENU_ABOUT:
+            MessageBox(0, L"Sword reader program for Pocket PC ver. 1.0.1\n\nsee: http://www.crosswire.org/sword/swordreader/\n\nGUI by Johan Gorter, 2004\nNow being maintained by B. Drake, Robin Randall and David Trotz\nProverbs 16:3", L"About", MB_OK);
+            handled = TRUE;
+            break;
+        case MENU_SHUTDOWN:
+            SendMessage(m_hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)m_hWnd);
+            SendMessage (m_hWnd, WM_CLOSE, 0, 0);
+            handled = TRUE;
+            exit(0);
+        default:
+            if ((wmId>=USERBUTTONS)&&(wmId<USERBUTTONS+1000)) {
+                m_navigator->buttonClicked(wmId-USERBUTTONS);
+                handled = TRUE;
+                break;
+            } 
+            else if (m_navigator->menuClicked(wmId)){
+            }else
+                return DefWindowProc(m_hWnd, message, wParam, lParam);
+            break;
+    }
+    return handled;
+}
+
+BOOL SwordReaderApp::OnCreate(WPARAM wParam, LPARAM lParam)
+{
+    RECT clientRect;
+    GetClientRect(m_hWnd,&clientRect);
+    clientRect.bottom -= MENU_HEIGHT;
+    m_navigator = new SimpleNavigator(&clientRect);
+
+    CreateCommandBar();
+    setMenuSelected(m_hwndCB,MENU_TEXT,1);
+    
+    CreateTranslationsMenu();
+    CreateGlobalOptionsMenu();
+    
+    return TRUE;
+}
+
+BOOL SwordReaderApp::CreateCommandBar()
+{
+    SHMENUBARINFO mbi;
+	memset(&mbi, 0, sizeof(SHMENUBARINFO));
+	mbi.cbSize     = sizeof(SHMENUBARINFO);
+	mbi.hwndParent = m_hWnd;
+	mbi.nToolBarId = IDM_MENU;
+	mbi.hInstRes   = m_hInst;
+	mbi.nBmpId     = 0;
+	mbi.cBmpImages = 0;
+
+	if (!SHCreateMenuBar(&mbi)) 
+		return FALSE;
+
+    m_hwndCB = mbi.hwndMB;
+    return TRUE;
+}
+
+BOOL SwordReaderApp::OnPaint(WPARAM wParam, LPARAM lParam)
+{
+    PAINTSTRUCT ps;
+    HDC hdc = BeginPaint(m_hWnd, &ps);
+	SetBkMode(hdc,OPAQUE);
+	SetBkColor(hdc,(COLORREF)GetSysColor(COLOR_WINDOW));
+	m_navigator->paint();
+	EndPaint(m_hWnd, &ps);
+    return TRUE;
+}
+
+BOOL SwordReaderApp::OnDestroy(WPARAM wParam, LPARAM lParam)
+{
+    CommandBar_Destroy(m_hwndCB);
+	delete m_navigator;
+	m_swordConf->Save();
+	delete m_swordConf;
+    PostQuitMessage(0);
+    return TRUE;
+}
+
+BOOL SwordReaderApp::OnSize(WPARAM wParam, LPARAM lParam)
+{
+    return FALSE;
+}
+
+BOOL SwordReaderApp::OnActivate(WPARAM wParam, LPARAM lParam)
+{
+    SHHandleWMActivate(m_hWnd, wParam, lParam, &s_sai, FALSE);
+    return TRUE;
+}
+
+BOOL SwordReaderApp::OnSettingChange(WPARAM wParam, LPARAM lParam)
+{
+    SHHandleWMSettingChange(m_hWnd, wParam, lParam, &s_sai);
+    return TRUE;
+}
+
+LRESULT CALLBACK SwordReaderApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
+{
+    BOOL handled = FALSE;
+
+    if(hWnd != m_hWnd)
+        return FALSE;
+
+	switch (message) 
+	{
+		case WM_NOTIFY:
+			handled = OnNotify(wParam, lParam);
+            break;
+		case WM_LBUTTONDOWN:
+		    handled = OnLButtonDown(wParam, lParam);
+			break;
+		case WM_KEYDOWN:
+			handled = OnKeyDown(wParam, lParam);
+			break;
+		case WM_COMMAND:
+            handled = OnCommand(message, wParam, lParam);
+			break;
+		case WM_CREATE:
+			handled = OnCreate(wParam, lParam);
+			break;
+		case WM_PAINT:
+			handled = OnPaint(wParam, lParam);
+			break; 
+		case WM_DESTROY:
+			handled = OnDestroy(wParam, lParam);
+			break;
+		case WM_ACTIVATE:
+            handled = OnActivate(wParam, lParam);
+     		break;
+		case WM_SETTINGCHANGE:
+			handled = OnSettingChange(wParam, lParam);
+     		break;
+		case WM_SIZE:
+            handled = OnSize(wParam, lParam);
+			break;
+		default:
+			return DefWindowProc(m_hWnd, message, wParam, lParam);
+   }
+   return handled;
+}
+
+BOOL SwordReaderApp::CreateTranslationsMenu() {
+    if(!m_hwndCB)
+        return FALSE;
+	HMENU menuMenu = getSubMenu(m_hwndCB,MENU_MENU);
+	HMENU menuTranslations = CreatePopupMenu();
+
+	InsertMenu(menuMenu,0,MF_BYPOSITION | MF_POPUP,(UINT)menuTranslations,L"Translation");
+	m_navigator->fillTranslationsMenu(menuTranslations);
+
+	DrawMenuBar(m_hwndCB);
+    return TRUE;
+}
+
+BOOL SwordReaderApp::CreateGlobalOptionsMenu() {
+    if(!m_hwndCB)
+        return FALSE;
+
+	HMENU menuMenu = getSubMenu(m_hwndCB,MENU_MENU);
+	HMENU menuGlobalOptions = CreatePopupMenu();
+
+	InsertMenu(menuMenu,0,MF_BYPOSITION | MF_POPUP,(UINT)menuGlobalOptions,L"Options");
+	m_navigator->fillGlobalOptionsMenu(menuGlobalOptions);
+	m_navigator->toggleOptionMenu(-1);	// get all checked menu choiced correct, but don't change anything
+
+	DrawMenuBar(m_hwndCB);
+    return TRUE;
+}
+
+void SwordReaderApp::SetTitle(const WCString &title) {
+	SetWindowText(m_hWnd,title.w_str());
+}
+
+void SwordReaderApp::RefreshScreen() {
+	InvalidateRect(m_hWnd, NULL, TRUE);
+	UpdateWindow(NULL);
+}
+
+void SwordReaderApp::SelectMenu(std::map<int,int>& menus, int mode) {
+	for (unsigned int i = 0;i < menus.size(); i++) {
+		setMenuSelected(m_hwndCB,menus[i],(mode==i));
+	}
+}
+
+void SwordReaderApp::AddMenu(HMENU menu,int flags, int id, const WCString &text) {
+	AppendMenu(menu,flags,USERBUTTONS+id,text.w_str());
+}
+
+
+
+void SwordReaderApp::SetMenu(HMENU menu, int id, bool checked) {
+	CheckMenuItem(menu,USERBUTTONS+id,checked?MF_CHECKED:MF_UNCHECKED);
+}
\ No newline at end of file

Added: trunk/src/gui/SwordReaderApp.h
===================================================================
--- trunk/src/gui/SwordReaderApp.h	                        (rev 0)
+++ trunk/src/gui/SwordReaderApp.h	2008-03-26 03:40:36 UTC (rev 104)
@@ -0,0 +1,70 @@
+#pragma once
+#include <swconfig.h>
+#include "SimpleNavigator.h"
+#include "SRWidget.h"
+#include "ApplicationInterface.h"
+
+//#include "swordce.h"
+
+class SwordReaderApp : SRWidget
+{
+public:
+    SwordReaderApp();
+    virtual ~SwordReaderApp();
+    bool InitApp(HINSTANCE hInstance, int nCmdShow);
+        //! The actual call back function for this window.
+    /*! This only acts as a mediator between the callback function we really 
+        want and the call back the Win API is expecting.
+        @param hwnd     Handle to the window.
+        @param message  Specifies the message. 
+        @param wParam   Specifies additional message information. The contents 
+                        of this parameter depend on the value of the message 
+                        parameter. 
+        @param lParam   Specifies additional message information. The contents 
+                        of this parameter depend on the value of the message 
+                        parameter. 
+     */
+    static LRESULT CALLBACK MessageRoute(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
+    //! The expected WinProc callback function.
+    /*! Handles any messages sent to this window.
+        @param hwnd     Handle to the window.
+        @param message  Specifies the message. 
+        @param wParam   Specifies additional message information. The contents 
+                        of this parameter depend on the value of the message 
+                        parameter. 
+        @param lParam   Specifies additional message information. The contents 
+                        of this parameter depend on the value of the message 
+                        parameter. 
+     */
+    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
+    void SetTitle(const WCString &title);
+    void RefreshScreen();
+private:
+    // Message Handlers...
+    BOOL OnNotify(WPARAM wParam, LPARAM lParam);
+    BOOL OnLButtonDown(WPARAM wParam, LPARAM lParam);
+    BOOL OnKeyDown(WPARAM wParam, LPARAM lParam);
+    BOOL OnCommand(UINT message, WPARAM wParam, LPARAM lParam);
+    BOOL OnPaint(WPARAM wParam, LPARAM lParam);
+    BOOL OnCreate(WPARAM wParam, LPARAM lParam);
+    BOOL OnDestroy(WPARAM wParam, LPARAM lParam);
+    BOOL OnActivate(WPARAM wParam, LPARAM lParam);
+    BOOL OnSettingChange(WPARAM wParam, LPARAM lParam);
+    BOOL OnSize(WPARAM wParam, LPARAM lParam);
+    // Helpers...
+    BOOL CreateCommandBar();
+    BOOL CreateTranslationsMenu();
+    BOOL CreateGlobalOptionsMenu();
+    void SelectMenu(std::map<int,int>& menus, int mode);
+    void AddMenu(HMENU menu,int flags, int id, const WCString &text);
+    void SetMenu(HMENU menu, int id, bool checked);
+
+    // I debate which of these should be cached...
+    SimpleNavigator     *m_navigator;
+    SWConfig            *m_swordConf;
+    DWORD               m_dwAppThreadID;
+    HINSTANCE	        m_hInst;
+    HWND			    m_hWnd;
+    HWND			    m_hwndCB;
+    static SHACTIVATEINFO s_sai;
+};

Modified: trunk/src/simplegui/simplegui.vcproj
===================================================================
--- trunk/src/simplegui/simplegui.vcproj	2008-03-24 04:31:10 UTC (rev 103)
+++ trunk/src/simplegui/simplegui.vcproj	2008-03-26 03:40:36 UTC (rev 104)
@@ -696,6 +696,10 @@
 				</FileConfiguration>
 			</File>
 			<File
+				RelativePath="..\gui\BTextViewer.cpp"
+				>
+			</File>
+			<File
 				RelativePath="..\gui\Main.cpp"
 				>
 				<FileConfiguration
@@ -1160,6 +1164,10 @@
 				</FileConfiguration>
 			</File>
 			<File
+				RelativePath="..\gui\SRWidget.cpp"
+				>
+			</File>
+			<File
 				RelativePath="..\gui\SwordIndex.cpp"
 				>
 				<FileConfiguration
@@ -1218,6 +1226,10 @@
 				</FileConfiguration>
 			</File>
 			<File
+				RelativePath="..\gui\SwordReaderApp.cpp"
+				>
+			</File>
+			<File
 				RelativePath="..\gui\TextControl.cpp"
 				>
 				<FileConfiguration
@@ -1405,6 +1417,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\gui\BTextViewer.h"
+				>
+			</File>
+			<File
 				RelativePath="..\gui\Main.h"
 				>
 			</File>
@@ -1441,10 +1457,18 @@
 				>
 			</File>
 			<File
+				RelativePath="..\gui\SRWidget.h"
+				>
+			</File>
+			<File
 				RelativePath="..\gui\SwordIndex.h"
 				>
 			</File>
 			<File
+				RelativePath="..\gui\SwordReaderApp.h"
+				>
+			</File>
+			<File
 				RelativePath="..\gui\TextControl.h"
 				>
 			</File>




More information about the sword-cvs mailing list