[sword-cvs] swordreader/src/gui SimpleNavigator.cpp,NONE,1.1 SimpleNavigator.h,NONE,1.1

sword@www.crosswire.org sword@www.crosswire.org
Mon, 15 Mar 2004 01:40:47 -0700


Update of /cvs/core/swordreader/src/gui
In directory www:/tmp/cvs-serv23827

Added Files:
	SimpleNavigator.cpp SimpleNavigator.h 
Log Message:
Forgotten files simpleNavigator.* added.

SORRY GUYS!!



--- NEW FILE: SimpleNavigator.cpp ---
#include "ApplicationInterface.h"
#include "SimpleNavigator.h"
#include "NavChap.h"
#include "NavVerse.h"
#include "NavBooks.h"
#include "NavRenderText.h"

#include "NavFind.h"
#include <swordce.h>
#include <swmgr.h>

using namespace sword;

#define WIDTH 240
#define HEIGHT 294

RECT RECT_SCREEN;// = {0,0,240,294};

RECT RECT_SELECTLABEL = {4,4,120,20};

RECT RECT_POSITION = {164,4,236,20};

RECT RECT_FIELD = {6,24,240,250};

SimpleNavigator::SimpleNavigator(RECT* screen) {
	RECT_SCREEN=*screen;
	mode=-1;
	nextID = 1;
	chapterCache=false;
	books=pages[MODE_BOOK]=new NavBooks(this);
	chapters=pages[MODE_CHAP]=new NavChap(this);
	verses=pages[MODE_VERSE]=new NavVerse(this);
	text=pages[MODE_TEXT]=new NavRenderText(this);
	find=pages[MODE_FIND]=new NavFind(this);
	menus[MODE_BOOK]=MENU_BOOK;
	menus[MODE_CHAP]=MENU_CHAP;
	menus[MODE_VERSE]=MENU_VERSE;
	menus[MODE_TEXT]=MENU_TEXT;
	menus[MODE_FIND]=MENU_FIND;
	optStartID = 0;
	optEndID = 0;
	transStartID = 0;
	transEndID = 0;
	options = 0;
	opCount = 0;
}

SimpleNavigator::~SimpleNavigator() {
	delete books;
	delete chapters;
	delete verses;
	delete text;
	delete find;
	if (options)
		delete options;
}

void SimpleNavigator::refresh() {
	refreshScreen();
}

void SimpleNavigator::setMode(int mode) {
	if (this->mode>=0) pages[this->mode]->hide();
	this->mode=mode;
	selectMenu(menus[this->mode]);
	pages[this->mode]->show();
	updateTitle();
	refresh();
}

void SimpleNavigator::paint() {
	pages[mode]->paint();
}

void SimpleNavigator::updateTitle() {
	UString title=position.toString()+L" - SwRd";
	setTitle(title);
}

/* a menu-item is selected (maybe one of the translations) */
void SimpleNavigator::buttonClicked(int id) {
	if ((id >= transStartID) && (id <= transEndID)) {
		setModule(id);
		checkModuleMenu(id);
		chapterCache=false;
		setMode(mode);
	}
	else if ((id >= optStartID) && (id <= optEndID)) {
		toggleOptionMenu(id);
		chapterCache=false;
		setMode(mode);
	}
	else
		pages[mode]->buttonClicked(id);
}

bool SimpleNavigator::menuClicked(int id) {
	for (std::map<int,int>::iterator i=menus.begin();i!=menus.end();i++) {
		if (i->second==id) {
			setMode(i->first);
			return true;
		}
	}
	return false;
}

void SimpleNavigator::keyDown(WPARAM id, LPARAM lparam) {
	pages[mode]->keyDown(id,lparam);
}

void SimpleNavigator::mouseDown(int x, int y) {
	pages[mode]->userTap(x,y);
}

void SimpleNavigator::setBook(int number) {
	position.setBook(number);
	chapterCache=false;
}

void SimpleNavigator::setChap(int number) {
	position.setChap(number);
	chapterCache=false;
}

void SimpleNavigator::setVerse(int number) {
	position.setVerse(number);
}

void SimpleNavigator::fillTranslationsMenu(HMENU menuTranslations) {
	transStartID = nextID;
	this->menuTranslations=menuTranslations;
	if (menuTranslations) {
		for (ModuleMap::iterator i=position.firstModule();i!=position.lastModule();i++) {
			addMenu(menuTranslations, nextID++, toUString(String(i->first)));
		}
	}
	checkModuleMenu(transStartID);
	transEndID = nextID - 1;
}

void SimpleNavigator::fillGlobalOptionsMenu(HMENU menuOptions) {
	if (menuOptions) {
		if (options)
			delete options;
		optStartID = nextID;
		this->menuOptions=menuOptions;
		StringList optionNames = position.manager->getGlobalOptions();

		// count options
		opCount = 0;
		for (StringList::iterator i = optionNames.begin(); i != optionNames.end(); i++) {
			StringList values = position.manager->getGlobalOptionValues(*i);
			bool supported = true;
			for (StringList::iterator j = values.begin(); j != values.end(); j++) {
				if ((*j != "On") && (*j != "Off"))
					supported = false;
			}
			if (supported)
				opCount++;
		}

		options = new SWBuf[opCount];

		// add options;
		for (i = optionNames.begin(); i != optionNames.end(); i++) {
//			HMENU menuOption = CreatePopupMenu();
//			InsertMenu(menuOptions,0,MF_POPUP,(UINT)menuOption, toUString(i->c_str()).c_str());
			StringList values = position.manager->getGlobalOptionValues(*i);
			bool supported = true;
			for (StringList::iterator j = values.begin(); j != values.end(); j++) {
//				addMenu(menuOption, nextID++, toUString(j->c_str()));
				if ((*j != "On") && (*j != "Off"))
					supported = false;
			}
			if (supported) {
				options[nextID - optStartID] = *i;
				addMenu(menuOptions, nextID++, toUString(i->c_str()));
			}
		}
	}
	optEndID = nextID - 1;
	// TODO: if no options, disable options menu
}

void SimpleNavigator::setModule(int id) {
	int modulenr = transStartID;
	ModuleMap::iterator i;
	for (i=position.firstModule();
			(i!=position.lastModule())&&(id!=modulenr);
			i++) { 
		modulenr++; 
	};
	if (i!=position.lastModule()) {
		position.setModule(i->second);
	}
}


void SimpleNavigator::checkModuleMenu(int id) {
	int current=transStartID;
	for (ModuleMap::iterator i=position.firstModule();i!=position.lastModule();i++) {
		checkMenu(menuTranslations,current,(current==id));
		current++;
	}
}


void SimpleNavigator::toggleOptionMenu(int id) {
	if (options) {
		for (int i = optStartID; i <= optEndID; i++) {
			SWBuf val = position.manager->getGlobalOption(options[i-optStartID]);
			if (i == id) {
				val = (val=="On")?"Off":"On";
				position.manager->setGlobalOption(options[i-optStartID], val);
			}
			checkMenu(menuOptions, i, (val == "On"));
		}
	}
}

void SimpleNavigator::urlClicked(const WCHAR *target) {
	const char * t = wstrtostr(target);
	if (strstr(t, "type=morph")) {
		const char *val = strstr(t, "value=");
		if ((val) && (strlen(val) > 6)) {
			val += 6;
			SWModule *mod = (position.verse->Testament() == 1) ? position.hebrewMorph : position.greekMorph;
			if (mod) {
				mod->SetKey(val);
				MessageBox(0, strtowstr(mod->StripText()), L"Morphology", MB_OK);
			}
		}
	}
	else if (strstr(t, "type=Strongs")) {
		const char *val = strstr(t, "value=G");
		if ((val) && (strlen(val) > 7)) {
			val += 7;
			if (position.greekLex) {
				position.greekLex->SetKey(val);
				MessageBox(0, strtowstr(position.greekLex->StripText()), L"Greek Strong's", MB_OK);
			}
		}
		val = strstr(t, "value=H");
		if ((val) && (strlen(val) > 7)) {
			val += 7;
			if (position.hebrewLex) {
				position.hebrewLex->SetKey(val);
				MessageBox(0, strtowstr(position.hebrewLex->StripText()), L"Hebrew Strong's", MB_OK);
			}
		}
	}
	else if (strstr(t, "noteID=")) {
		const char *val = strstr(t, "noteID=");
		if ((val) && (strlen(val) > 7)) {
			val += 7;
			SWBuf buf = val;
			const char *tmp = buf.c_str();
			val = strstr(tmp, ".n.");
			if (val) {
				buf.setSize(val - tmp);
				SWBuf saveKey = position.verse->getText();
				position.verse->setText(buf.c_str());
				position.bible->StripText();	// force lookup
				const char *n = strstr(t, ".n.");
				if (n) {
					SWBuf body = position.bible->getEntryAttributes()["Footnote"][n+3]["body"];
					body = position.bible->StripText(body.c_str());
					MessageBox(0, strtowstr(body), L"Footnote", MB_OK);
				}
				position.verse->setText(saveKey);
			}
			else {
				val = strstr(tmp, ".x.");
				if (val) {
					buf.setSize(val - tmp);
					SWBuf saveKey = position.verse->getText();
					position.verse->setText(buf.c_str());
					position.bible->StripText();	// force lookup
					const char *n = strstr(t, ".x.");
					if (n) {
						SWBuf body = position.bible->getEntryAttributes()["Footnote"][n+3]["body"];
						body = position.bible->StripText(body.c_str());
						MessageBox(0, strtowstr(body), L"CrossReference", MB_OK);
					}
					position.verse->setText(saveKey);
				}
			}
		}
	}
	else MessageBox(0, target, L"url clicked", MB_OK);

}
int SimpleNavigator::getID() {

	return nextID++;

}
--- NEW FILE: SimpleNavigator.h ---
#ifndef SIMPLENAVIGATOR_H
#define SIMPLENAVIGATOR_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include "utils.h"
#include "SwordIndex.h"
#include "NavPage.h"
#include <swbuf.h>
#include <map>

using namespace sword;

#define NUM_MENUS	5

#define MODE_BOOK	0
#define MODE_CHAP	1
#define MODE_VERSE	2
#define MODE_TEXT	3
#define MODE_FIND	4

#define BUTTON_BACKGROUND 0x00A0A0A0

extern RECT RECT_SCREEN;

class NavNumbers;
class NavBooks;
class NavRenderText;
class NavFind;

class SimpleNavigator
{
protected:
	std::map<int,NavPage*> pages;
	std::map<int,int> menus;
	int mode;

	HMENU menuTranslations;
	HMENU menuOptions;

	int nextID;
	int transStartID;
	int transEndID;
	int optStartID;
	int optEndID;
	SWBuf *options;
	int opCount;
	UString getVerseHeader();

	void setModule(int id);
	void checkModuleMenu(int id);

public:
	SwordIndex position;
	NavPage* chapters;
	NavPage* verses;
	NavPage* books;
	NavPage* text;
	NavPage* find;

	SimpleNavigator(RECT* screen);
	virtual ~SimpleNavigator();

	//	Navigation
	void setBook(int number);
	void setChap(int number);
	void setVerse(int number);

	int getBook() {return position.getBook();};
	int getChap() {return position.verse->Chapter();};
	int getVerse() {return position.verse->Verse();};

	int chapCount() {return position.chapCount();}
	int verseCount() {return position.verseCount();}

	//	Change user interface
	void updateTitle();
	void fillTranslationsMenu(HMENU menuTranslations);
	void fillGlobalOptionsMenu(HMENU menuTranslations);
	void toggleOptionMenu(int id);

	//	Interface with pages
	int getID();
	void setMode(int mode);
	bool chapterCache;	// to be set to false if 'text' should reload 
						// its chapter
	void refresh();

	//	Interface with system
	bool initialized() {return position.initialized();}
	void buttonClicked(int id);
	void keyDown(WPARAM id, LPARAM lparam);
	void urlClicked(const WCHAR *target);
	bool menuClicked(int id);
	void mouseDown(int x, int y);
	void paint();
};

#endif