/*

			Slide Show Screen Saver

*/

#include "screensv.h"

/* Function prototypes */

static void WriteProfileInt(LPSTR key, LPSTR tag, int i);

/* Global used by SCRNSAVE.LIB. Required for all screen savers. */

char szAppName[40];

/* Globals exported. */

char szName[]="Slide Show Screen Saver";

/* Externals defined in SCRNSAVE.LIB. Required for all screen savers. */

HINSTANCE hMainInstance;
HWND hMainWindow;
char szName[TITLEBARNAMELEN];
char szIniFile[MAXFILELEN];
char szScreenSaver[22];
char szHelpFile[MAXFILELEN];
char szNoHelpMemory[BUFFLEN];
UINT MyHelpMessage;
HOOKPROC fpMessageFilter;

/* RegisterDialogClasses -- Entry point for registering window
							classes required by configuration dialog box. */

BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
    return TRUE;
}

//	creditsDialogue  --  Display the credits dialogue

DialogueProc(creditsDialogue)
{
    switch (message) {
        case WM_COMMAND:
            switch (wParam) {
                case IDOK:
                    EndDialog(hDlg, TRUE);
                    return TRUE;

            }
            break;
    }
    return FALSE;
}

/* ScreenSaverConfigureDialog  --  Dialogue box function for
								   screen saver configuration dialogue. */

DialogueProc(ScreenSaverConfigureDialog)
{
	static HWND hIDOK;
	BOOL intok;
	int is;

    switch (message) {
        case WM_INITDIALOG:
            GetIniEntries();
            GetIniSettings();
#ifdef INTERNET_LINKS
			ShowWindow(GetDlgItem(hDlg, IDC_CO_FOURMILAB_STATIC), SW_HIDE);
			ShowWindow(GetDlgItem(hDlg, IDC_CO_FOURMILAB_HOME), SW_SHOW);
#else
			EnableWindow(GetDlgItem(hDlg, IDC_CO_HELP), FALSE);
			ShowWindow(GetDlgItem(hDlg, IDC_CO_FOURMILAB_STATIC), SW_SHOW);
			ShowWindow(GetDlgItem(hDlg, IDC_CO_FOURMILAB_HOME), SW_HIDE);
#endif
			CheckDlgButton(hDlg, IDC_CO_DATE_TIME, showDateTime); 
			CheckDlgButton(hDlg, IDC_CO_FILE_NAME, showFileName); 
			CheckDlgButton(hDlg, IDC_CO_PALETTE, forcePalette); 
			CheckDlgButton(hDlg, IDC_CO_ORDER_RANDOM, shufflePlay); 
			CheckDlgButton(hDlg, IDC_CO_ORDER_ALPHA, !shufflePlay);
			CheckDlgButton(hDlg, IDC_CO_POS_RANDOM, randomPosition); 
			CheckDlgButton(hDlg, IDC_CO_POS_CENTRE, !randomPosition);
			SetDlgItemText(hDlg, IDC_CO_PATH, slidePath);
			SetDlgItemInt(hDlg, IDC_CO_CHANGE_TIME, changeTime, FALSE);
			CheckDlgButton(hDlg, IDC_CO_SOUND_NEVER, soundMode == SOUND_NEVER); 
			CheckDlgButton(hDlg, IDC_CO_SOUND_SYNC, soundMode == SOUND_SYNC); 
			CheckDlgButton(hDlg, IDC_CO_SOUND_CONT, soundMode == SOUND_CONTINUOUS); 
			CheckDlgButton(hDlg, IDC_CO_SOUND_RETURN, soundMode == SOUND_KEY); 
			SetDlgItemInt(hDlg, IDC_CO_SOUND_PAUSE, soundPause, FALSE);
            hIDOK = GetDlgItem(hDlg, IDOK);
            return TRUE;

        case WM_COMMAND:
            switch (wParam) {
                case IDOK:
					GetDlgItemText(hDlg, IDC_CO_PATH, slidePath, sizeof slidePath);
					showDateTime = IsDlgButtonChecked(hDlg, IDC_CO_DATE_TIME);
					showFileName = IsDlgButtonChecked(hDlg, IDC_CO_FILE_NAME);
					forcePalette = IsDlgButtonChecked(hDlg, IDC_CO_PALETTE);
					shufflePlay = IsDlgButtonChecked(hDlg, IDC_CO_ORDER_RANDOM);
					randomPosition = IsDlgButtonChecked(hDlg, IDC_CO_POS_RANDOM);
					if (IsDlgButtonChecked(hDlg, IDC_CO_SOUND_NEVER)) {
						soundMode = SOUND_NEVER;
					}
					if (IsDlgButtonChecked(hDlg, IDC_CO_SOUND_SYNC)) {
						soundMode = SOUND_SYNC;
					}
					if (IsDlgButtonChecked(hDlg, IDC_CO_SOUND_CONT)) {
						soundMode = SOUND_CONTINUOUS;
					}
					if (IsDlgButtonChecked(hDlg, IDC_CO_SOUND_RETURN)) {
						soundMode = SOUND_KEY;
					}

					is = GetDlgItemInt(hDlg, IDC_CO_CHANGE_TIME, &intok, FALSE);
					if (intok) {
						changeTime = is;
					} else {
						changeTime = 60;
					}
					is = GetDlgItemInt(hDlg, IDC_CO_SOUND_PAUSE, &intok, FALSE);
					if (intok) {
						soundPause = is;
					} else {
						soundPause = 15;
					}

					/*	If the user supplied a trailing slash or
						backslash on the directory name, prune it.  */

					while ((strlen(slidePath) > 0) &&
							((slidePath[strlen(slidePath) - 1] == '/') ||
							 (slidePath[strlen(slidePath) - 1] == '\\'))) {
						slidePath[strlen(slidePath) - 1] = 0;	
					}
					if (strlen(slidePath) == 0) {
						strcpy(slidePath, rstring(IDS_DEFAULT_PATH));
					}

					/* Write out all configuration parameters. */
                    
					WriteProfileInt(szAppName, rstring(INI_DATE_TIME), showDateTime);
					WriteProfileInt(szAppName, rstring(INI_FILE_NAME), showFileName);
					WriteProfileInt(szAppName, rstring(INI_PALETTE), forcePalette);
					WriteProfileInt(szAppName, rstring(INI_SHUFFLE), shufflePlay);
					WriteProfileInt(szAppName, rstring(INI_SCREENPOS), randomPosition);
					WriteProfileInt(szAppName, rstring(INI_CHANGETIME), changeTime);
					WriteProfileInt(szAppName, rstring(INI_SOUNDMODE), soundMode);
					WriteProfileInt(szAppName, rstring(INI_SOUNDPAUSE), soundPause);
					WritePrivateProfileString(szAppName, rstring(INI_PATH), slidePath, szIniFile);
                    EndDialog(hDlg, TRUE);
                    return TRUE;

                case IDCANCEL:
                    EndDialog(hDlg, FALSE);
                    return TRUE;

				case IDC_CO_FILE_BROWSE:
					{
						OPENFILENAME ofn;
						static char ofilt[] =
							"Images and sound files\0"
							"*.jpg;*.gif;*.png;*.bmp;*.wav;*.mid;*.rmi\0"
							"All files (*.*)\0" "*.*\0\0";
						char browsepath[MAX_PATH];
						
						GetDlgItemText(hDlg, IDC_CO_PATH, browsepath, sizeof browsepath);
						ofn.lStructSize = sizeof(OPENFILENAME);
						ofn.hwndOwner = hDlg; 
						ofn.hInstance = NULL;
						ofn.lpstrFilter = ofilt; 
						ofn.lpstrCustomFilter = NULL;
						ofn.nMaxCustFilter = 0; 
						ofn.nFilterIndex = 1;
						ofn.lpstrFile = browsepath; 
						ofn.nMaxFile = MAX_PATH;
						ofn.lpstrFileTitle = NULL; 
						ofn.nMaxFileTitle = 0;
						ofn.lpstrInitialDir = NULL; 
						ofn.lpstrTitle = rstring(IDS_OPENTITLE);
						ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR |
							OFN_PATHMUSTEXIST; 
						ofn.nFileOffset = 0;
						ofn.nFileExtension = 0; 
						ofn.lpstrDefExt = NULL;
						ofn.lCustData = 0L; 
						ofn.lpfnHook = NULL;
						ofn.lpTemplateName = NULL; 
						if (GetOpenFileName(&ofn)) {
							if (ofn.nFileOffset > 0) {
								browsepath[ofn.nFileOffset - 1] = 0;
							}
							SetDlgItemText(hDlg, IDC_CO_PATH, browsepath);
						}	
						return TRUE;
					}

#ifdef INTERNET_LINKS
				case IDC_CO_FOURMILAB_HOME:
					HlinkSimpleNavigateToString(
						L"http://www.fourmilab.ch/",
						NULL, NULL, NULL, 0, NULL, 0, 0);
					return TRUE;

				case IDC_CO_HELP:
DoHelp:				HlinkSimpleNavigateToString(
						L"http://www.fourmilab.ch/slidescr/",
						NULL, NULL, NULL, 0, NULL, 0, 0);
					return TRUE;					
#else

                case ID_HELP:
DoHelp:
                    MessageBox(hDlg, "Slide Show screen saver.", szName, MB_OK);
                    break;
#endif

				case IDC_CO_CREDITS:
					DialogBox(hMainInstance, MAKEINTRESOURCE(DLG_CREDITS),
						hDlg, creditsDialogue);
					return TRUE;
            }
            break;

        default:
            if (message == MyHelpMessage)     // Context sensitive help msg.
                goto DoHelp;
    }
    return FALSE;
}

/* GetIniSettings -- Get initial settings from WIN.INI

	Params:  hWnd -- Handle to window

	Return:  None
	
	Note that initial installation defaults are set by the default
	arguments to the retrievals of the profile variables.
*/

void GetIniSettings(void)
{
	showDateTime = GetPrivateProfileInt(szAppName, rstring(INI_DATE_TIME), TRUE, szIniFile);
	showFileName = GetPrivateProfileInt(szAppName, rstring(INI_FILE_NAME), FALSE, szIniFile);
	forcePalette = GetPrivateProfileInt(szAppName, rstring(INI_PALETTE), FALSE, szIniFile);
	shufflePlay = GetPrivateProfileInt(szAppName, rstring(INI_SHUFFLE), TRUE, szIniFile);
	randomPosition = GetPrivateProfileInt(szAppName, rstring(INI_SCREENPOS), TRUE, szIniFile);
	changeTime = GetPrivateProfileInt(szAppName, rstring(INI_CHANGETIME), 60, szIniFile);
	soundMode = GetPrivateProfileInt(szAppName, rstring(INI_SOUNDMODE), SOUND_CONTINUOUS, szIniFile);
	soundPause = GetPrivateProfileInt(szAppName, rstring(INI_SOUNDPAUSE), 15, szIniFile);
	GetPrivateProfileString(szAppName, rstring(INI_PATH), rstring(IDS_DEFAULT_PATH), slidePath, sizeof slidePath, szIniFile);
}

/* WriteProfileInt - Write an unsigned integer value to CONTROL.INI.

	Params:  name - szSection - [section] name in .INI file
		            szKey     - key= in .INI file
					i         - value for key above

	Return:  None
*/

static void WriteProfileInt(LPSTR szSection, LPSTR szKey, int i)
{
    char achBuf[40];

    /* Write out as unsigned because GetPrivateProfileInt() can't
       cope with signed values! */

    wsprintf(achBuf, "%u", i);
    WritePrivateProfileString(szSection, szKey, achBuf, szIniFile);
}

void GetIniEntries(void)
{
	// Load common strings from stringtable...
    LoadString(hMainInstance, idsAppName, szAppName, 40); 
	LoadString(hMainInstance, idsIniFile, szIniFile, MAXFILELEN);
	LoadString(hMainInstance, idsScreenSaver, szScreenSaver, 22);
	LoadString(hMainInstance, idsHelpFile, szHelpFile, MAXFILELEN);
	LoadString(hMainInstance, idsNoHelpMemory, szNoHelpMemory, BUFFLEN);
}
