/* QuickHelp Demo © Paweî Marciniak <pmarciniak@lodz.home.pl>*/

#include <stdio.h>
#include <string.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/gadtools.h>
#include <graphics/text.h>
#include <graphics/gfxbase.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/gadtools.h>
#include <proto/diskfont.h> 
#include <clib/quickhelp_protos.h>
#include <pragmas/quickhelp_pragmas.h>
#include <libraries/quickhelp.h>

extern struct GfxBase					*GfxBase	= NULL;
extern struct DosLibrary			*DosBase	= NULL;
extern struct IntuitionBase		*IntuitionBase	= NULL;
extern struct ExecBase				*ExecBase	= NULL;
extern struct Library					*DiskFontBase	= NULL;
extern struct Library					*GadToolsBase	= NULL;
extern struct Library					*QuickHelpBase	= NULL;

/* Prototypy */
int SetupScreen( void );
void CloseDownScreen( void );
int OpenDisplay( WORD, WORD );
void CloseDisplay(void );
UWORD ComputeX( UWORD );
UWORD ComputeY( UWORD );
void ComputeFont( void );
LONG OpenLibraries( void );
void CloseLibraries( void );
int OpenFonts( void );
void CloseFonts( void );


/* zmienna status informuje czy okno pomocy jest
otwarte ( TRUE ) czy zamkniëte ( FALSE ) */

int	status=FALSE;

struct Screen *Screen=NULL;
struct Window *APP_Window=NULL;

STRPTR PubScreenName="Workbench";
STRPTR APP_TitleWindow="Demo okno";
STRPTR APP_TitleScreenWindow="Demo ekran okno";

APTR  VisualInfo = NULL;

/* Opcjonalny Font */

struct TextAttr *Font, ScreenFont;
struct TextFont	*APP_Font;

WORD FontX=NULL;
WORD FontY=NULL;
UWORD OffX, OffY;

ULONG			IClass;
UWORD			Code, Qualifier;
struct Gadget	*IObject;

/* Rozmiar Okna */

	WORD Width=320;
	WORD Height=150;

/* Tytaj zaczynajâ sië wszystkie poûyteczne funkcje */

/* Funkcja otwiera okreôlony font */

int OpenFonts( void )
{
	if (!(APP_Font=OpenDiskFont( &ScreenFont )))
		return( FALSE );
	return( TRUE );
}


/* Funkcja zamyka otwarty font */

void CloseFonts( void )
{
	if ( APP_Font )	
	{
		CloseFont( APP_Font );
		APP_Font=NULL;
	}
}


UWORD ComputeX( UWORD value )
{
	return(( UWORD )((( FontX * value ) + 4 ) / 8 ));
}


UWORD ComputeY( UWORD value )
{
	return(( UWORD )((( FontY * value ) + 4 ) / 8 ));
}


void ComputeFont( void )
{
	Font = &ScreenFont;
	Font->ta_Name = (STRPTR)Screen->RastPort.Font->tf_Message.mn_Node.ln_Name;
	Font->ta_YSize = FontY = Screen->RastPort.Font->tf_YSize;
	FontX = Screen->RastPort.Font->tf_XSize;

	OffX = Screen->WBorLeft;
	OffY = Screen->RastPort.TxHeight + Screen->WBorTop + 1;
	return;
}


/* Funkcja otwiera okno */

int OpenAPP_Display( WORD Left, WORD Top )
{
	if(!(APP_Window=OpenWindowTags( 0,
		WA_Left,				Left,
		WA_Top,					Top,
		WA_Width,				Width,
		WA_Height,			Height,
		WA_Title,				APP_TitleWindow,
		WA_ScreenTitle,	APP_TitleScreenWindow,
		WA_Flags,				WFLG_DRAGBAR | WFLG_SIZEGADGET | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH | WFLG_HASZOOM,
		WA_IDCMP,				IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | IDCMP_GADGETHELP | 
										IDCMP_CHANGEWINDOW | IDCMP_INACTIVEWINDOW | IDCMP_ACTIVEWINDOW,
		WA_Gadgets,			FALSE,
		WA_AutoAdjust,	TRUE,
		WA_PubScreen,		(struct Screen *)Screen,
		TAG_DONE)))
			return( FALSE );
	GT_RefreshWindow( APP_Window, NULL );
	return( TRUE );
}


/* Funkcja zamyka otwarte okno */

void CloseAPP_Display(void)
{
	if(APP_Window) 
	{
		CloseWindow((struct Window *)APP_Window);
		APP_Window=NULL;
	}
}


/* Funkcja odczytuje komunikat Intuition */
/* kod tej funkcji napisaî Raul A. Sobon */

LONG ReadIMsg( struct Window *iwnd )
{
	struct IntuiMessage *imsg;

	if ( imsg = GT_GetIMsg( iwnd->UserPort ))
	{
		IClass	  =	imsg->Class;
		Qualifier	=	imsg->Qualifier;
		Code			=	imsg->Code;
		IObject		=	imsg->IAddress;

		GT_ReplyIMsg( imsg );
		return( TRUE );
	}
	return( FALSE );
}


/* Funkcja alokuje ekran */

int SetupScreen( void )
{
	if (!(Screen = LockPubScreen( PubScreenName )))
		return( FALSE );

	ComputeFont();

	if(!(OpenFonts()))
		return( FALSE );

	if (!( VisualInfo = GetVisualInfo( Screen, TAG_DONE )))
		return( FALSE );

	return( TRUE );
}


/* Funkcja dealokuje ekran */

void CloseDownScreen( void )
{
	if ( VisualInfo )
	{
		FreeVisualInfo( VisualInfo );
		VisualInfo = NULL;
	}

	CloseFonts();

	if ( Screen )
	{
		UnlockPubScreen( NULL, Screen );
		Screen = NULL;
	}
}


/* Funkcja otwiera wszystkie biblioteki */

LONG OpenLibraries( void )
{

	if ( !(DosBase = (struct DosLibrary *) OpenLibrary((UBYTE *) "dos.library", 36 )))
		return( FALSE );
	if ( !(IntuitionBase = (struct IntuitionBase *) OpenLibrary((UBYTE *) "intuition.library", 39 )))
		return( FALSE );
	if ( !(GadToolsBase = (struct Library *) OpenLibrary((UBYTE *) "gadtools.library", 36 )))
		return( FALSE );
	if ( !(GfxBase = (struct GfxBase *) OpenLibrary((UBYTE *) "graphics.library" , 36 )))
		return( FALSE );
	if ( !(DiskFontBase = (struct Library *) OpenLibrary((UBYTE *) "diskfont.library" , 36 )))
	{
		printf("Non Diskfont.library\n");
		return( FALSE );
	}
	if ( !(QuickHelpBase = (struct Library *) OpenLibrary((UBYTE *) "quickhelp.library" , 37 )))
	{
		printf("Non quickhelp.library\n");
		return( FALSE );
	}
	return( TRUE );
}


/* Funkcja zamyka wszystkie biblioteki */

void CloseLibraries( void )
{
	if (DiskFontBase)		CloseLibrary( (struct Library *) DiskFontBase );
	if (QuickHelpBase)	CloseLibrary( (struct Library *) QuickHelpBase );
	if (GfxBase)				CloseLibrary( (struct Library *) GfxBase );
	if (GadToolsBase)		CloseLibrary( (struct Library *) GadToolsBase );
	if (IntuitionBase)	CloseLibrary( (struct Library *) IntuitionBase );
	if (DosBase)				CloseLibrary( (struct Library *) DOSBase );
}


/* Gîówna funkcja */

int main( int argc, char *argv[] )
{
BOOL running=TRUE;

	if(!(OpenLibraries()))
	{
		CloseLibraries();
		return( FALSE );
	}
	
	if(!(SetupScreen()))
	{
		CloseDownScreen();
		CloseLibraries();
		return( FALSE );
	}

	if(!(OpenAPP_Display( 20, 20 )))
	{
		CloseDownScreen();
		CloseLibraries();
		return( FALSE );
	}

	/* Ustawiamy font i wîâczamy pomoc */
	SetFont(APP_Window->RPort,APP_Font);
	HelpControl(APP_Window, HC_GADGETHELP);


  do
	{
		WaitPort( APP_Window->UserPort );

		while ( ReadIMsg( APP_Window ))
		{
			switch ( IClass )
			{
				case	IDCMP_ACTIVEWINDOW:
				case	IDCMP_INACTIVEWINDOW:
				case	IDCMP_CHANGEWINDOW:
					CloseHelp();
				break;


				case	IDCMP_REFRESHWINDOW:
					GT_BeginRefresh( APP_Window );
					GT_EndRefresh( APP_Window, TRUE );
					CloseHelp();
				break;

				case	IDCMP_CLOSEWINDOW:
					CloseHelp();
					running=FALSE;
				break;

				case IDCMP_GADGETHELP:

			 	  if (IObject == NULL)
		  	  {
		  	  	/* myszka poza naszym oknem */
						CloseHelp();
		  	  }
		 		  else
						if (IObject == (APTR) APP_Window)
			    	{
			    		/* myszka nad naszym oknem */
							CloseHelp();

		 		 		}
		    		else
		    		{

							LONG sysgtype = ((struct Gadget *) IObject)->GadgetType & 0xF0;

							switch (sysgtype)
							{
			    			case GTYP_SIZING:
									CloseHelp();
									if(!(status=OpenHelp("Pîynna zmiana rozmiaru okna", APP_Font, APP_Window, 0 )))
									{
										printf("%s\n",GetQuickHelpString());
									}
								break;

					 	  	case GTYP_WDRAGGING:
										CloseHelp();
									if(!(status=OpenHelp("- Listwa przesuwania okna -\n---\nnacisnij lewy klawisz myszy nad\ntâ listwâ i przesuï do miejsca\ndo którego chcesz", APP_Font, APP_Window, 0)))
									{
										printf("%s\n",GetQuickHelpString());
									}
								break;

						    case GTYP_WUPFRONT:
										CloseHelp();
									if(!(status=OpenHelp("Zmiana gîëbokoôci okna", APP_Font, APP_Window, TAG_DONE  )))
									{
										printf("%s\n",GetQuickHelpString());
									}
								break;

				 			  case GTYP_WDOWNBACK:
										CloseHelp();
									if(!(status=OpenHelp("Skokowa zmiana wielkoôci", APP_Font, APP_Window,0 )))
									{
										printf("%s\n",GetQuickHelpString());
									}
								break;

						    case GTYP_CLOSE:
										CloseHelp();
									if(!(status=OpenHelp("Kliknij tutaj, ûeby\nzamknâê okno", APP_Font, APP_Window, 0)))
									{
										printf("%s\n",GetQuickHelpString());
									}
								break;
  /*
						    case 0:

									switch(IObject->GadgetID)
									{

									  Tutaj nasze gadgety

									}
								break;
  */
			  	  		default:
										CloseHelp();
									if(!(status=OpenHelp("Nieznany Gadget", APP_Font, APP_Window, 0)))
									{
										printf("%s\n",GetQuickHelpString());
									}
								break;
							}
  			  	}
				break;
						
				default:
				break;
			}
		}
	} while ( running );

	CloseAPP_Display();
	CloseDownScreen();
  CloseHelp();
	CloseLibraries();
	return( TRUE );
}
