/*-----------------------------------------------
	GPtIndex.CPP -- CGM Page Index Dialog Box
	Class IndexObject
  -----------------------------------------------*/

#include <windows.h>
#include "gpt.h"
#include <stdio.h>
//#include <stdarg.h>
IndexObjectPt IndexObject::ThisIndexObject= NULL;



LPSTR lstrchr  (LPSTR str, char ch) ;
LPSTR lstrrchr (LPSTR str, char ch) ;

BOOL FAR PASCAL _export IndexObject::IndexDlgProc(HWND hDlg, WORD message,
																WORD wParam, LONG lParam)
	  {
	  char  cLastChar ;
	  short nEditLen ;
	  char stg[1000];
	  char stg2[256];
	  CgmObjectPt cgm;
	  int nIndex;
	  int nPage = 0;
	  int nItem;
	  HWND hList = GetDlgItem(hDlg, IDD_PLIST);
	  HWND hPage = GetDlgItem(hDlg, IDD_PNUM);
	  switch (message)  // handle WM_INITDIALOG, WMCOMMAND
		  {
		  case WM_INITDIALOG:
		  ThisIndexObject =  (IndexObjectPt)GetObjectPt(lParam);
		  cgm = ThisIndexObject->CgmObj;
		  cgm->GetPageText( (PSTR)stg2);
		  wsprintf((LPSTR)stg,"Current Page = %d",cgm->GetPicNum() );
		  SetDlgItemText(hDlg,IDD_MESS, stg);

		  wsprintf((LPSTR)stg,"Last Page = %d", cgm->IndexCgm());
		  SetDlgItemText(hDlg,IDD_MESS2, stg);

		  wsprintf((LPSTR)stg,"%d", cgm->GetPicNum());
		  SetDlgItemText(hDlg,IDD_PNUM, stg);


		  void *foo = NULL;
		  do
			 {
			 *stg = '\0';
			 cgm->GetPictureText((PSTR)stg, &foo);
			 wsprintf((LPSTR)stg2,"%3d : %s ",++nPage,(LPSTR)stg );
			 SendMessage(hList, LB_ADDSTRING, NULL, (LONG)(LPSTR)stg2);
			 } while (foo);
		  SendMessage(hList, LB_SETCURSEL, cgm->GetPicNum()-1, NULL);

		  return TRUE ;

		  case WM_COMMAND:
		  switch (wParam)   // control ID's IDD_FLIST, IDD_FNAME, IDOK, IDCANCEL
			  {
			  case IDD_PLIST:        // Something done with listbox
			  switch (HIWORD (lParam))   // Handles LBN_SELCHANGE, LBN_DBLCLK
				  {
				  case LBN_DBLCLK:
				  EndDialog (hDlg,1+ SendMessage(hList,LB_GETCURSEL,NULL,NULL));
				  return TRUE;

				  case LBN_SELCHANGE:   // listbox selection change
				  nIndex = SendMessage(hList, LB_GETCURSEL, NULL, NULL);
				  wsprintf((LPSTR)stg,"%d", nIndex +1);
				  SetDlgItemText(hDlg,IDD_PNUM, stg);
				  return TRUE ;
				  }   // End  switch (HIWORD (lParam))
			  break ;     // End case IDD_FLIST


			  case IDOK:
			  stg[0] = 5;   // 5 bytes max
			  stg[1] = 0;
			  int num = SendMessage(hPage,EM_GETLINE,NULL,(LONG)(LPSTR)stg);
			  stg[num] = 0;
			  nItem = sscanf(stg,"%d", &num);
			  cgm = ThisIndexObject->CgmObj;
			  if (nItem !=0 && nItem !=EOF && num > 0 && num <= cgm->IndexCgm() )
				  EndDialog (hDlg, num);
			  else
				  {
				  wsprintf((LPSTR)stg,"%d",
									 SendMessage(hList,LB_GETCURSEL,NULL,NULL) +1 );
				  SetDlgItemText(hDlg,IDD_PNUM, stg);
				  MessageBeep(0);
				  }
			  return TRUE;

			  case IDCANCEL: EndDialog (hDlg, NO_PAGE_SELECTED);  return TRUE ;
			  }   // End switch (wParam)
		  break; // End case WM_COMMAND

		  }      // end switch (message)
	  return FALSE ;
	  }

 // ************* IndexObject::IndexCgm()
void IndexObject::IndexCgm(HWND hWndParent)
	{
	DWORD lParam;
	FARPROC lpfnDlgProc = MakeProcInstance(
		  (FARPROC)IndexObject::IndexDlgProc, Appl::hInstance) ;
	// Do dialog passing pointer to  IndexObject
	SetObjectPt(this, &lParam);
	Page = DialogBoxParam ( Appl::hInstance, "Index",  hWndParent,
						  lpfnDlgProc,lParam) ;
	FreeProcInstance(lpfnDlgProc);
	}


