#include "XRes.h"
#include "XResLib.h"
#include "XApp.h"
#include "XWindow.h"
#include "XString.h"
#include "XMsgBox.h"
#include "XIcon.h"
#include "xexcept.h"

XIcon :: ~XIcon()
{
	if (handle && loaded)
		WinFreeFileIcon(handle);
}


BOOL XIcon::Load(const char *p, const BOOL copy)
{
	loaded = TRUE;
	if (handle)
		WinFreeFileIcon(handle);
	handle = WinLoadFileIcon((PSZ) p, copy);
	return handle != NULLHANDLE ? TRUE : FALSE;
}


BOOL XIcon::Load(const XResource * r)
{
	handle = WinLoadPointer(HWND_DESKTOP, r->GetResourceLibrary()->GetModuleHandle(), r->GetID());
	return handle != NULLHANDLE ? TRUE : FALSE;
}


/*@ 
@class XResource
@type overview
@symbol _
@remarks The class XResource describes resources used in an application. A resource may be an icon, a dialog template
a menubar etc. An resource is decribed in the class XResource with an ID and a library (XResourceLibrary).
The resources may reside in the EXE-file or in a resource-DLL (see XResouceLibrary for details).
*/


/*@ XResource::XResource()
@group constructors/destructors
@remarks Constructs a resource. Resources are defined by an ID and
a XResourceLibrary which contains the resource.
@parameters    ULONG theResourceID       the ID of the resource<BR>
              XResourceLibrary * theLib 	pointer to the library which contains the resource.
*/


/*@ XResourceLibrary :: XResourceLibrary(const XProcess * app, const char *path)
@group constructors/destructors
@remarks Constructs a resource library.
@parameters    <t 'ø' c=2>
					øXProcess * theOwner       øA pointer to the owning process. Every
                                         resource which is build from the library will
                                         be owned by this process
               øchar * thePath            øpath of the library without extension ".DLL".
                                         If the path is empty, a resource library will
                                         be created with the resources which are
                                         linked to the exe-file.
                                         If the file cannot be found an error ocures.
                                      Default is NULL.
					</t>
@exceptions	If the method fails an exception of the type XException is thrown.
*/
XResourceLibrary :: XResourceLibrary(const XProcess * app, const char *path)
{
	char errorBuffer[100];

	proc = (XProcess *) app;
	moduleHandle = 0;
	LONG res;

	if (path)
	{
		if ((res = DosLoadModule((PSZ) errorBuffer, 100, (PSZ) path, &moduleHandle)) != 0)
			OOLThrow("error loading resourcefile", res);
	}
}


/*@ XResourceLibrary::LoadString(XString * string, const unsigned long resId)
@group load resources
@remarks Loads a string out of the library.
@parameters    XString * string          variable which will get the string<BR>
               ULONG theResourceID       the ID of the string
@exceptions	If the method fails an exception of the type XException is thrown.
*/
BOOL XResourceLibrary::LoadString(XString * string, const unsigned long resId)
{
	char s[255];
	SHORT len;

	if ((len = WinLoadString(proc->hab, moduleHandle, resId, 255, (PCH) s)) == 0)
		OOLThrow("error loading string from resources - string is empty or not avaible", -10);
	else
		*string = s;
	return TRUE;
}


HWND XResourceLibrary::LoadMenu(const XWindow * fr, const unsigned long id)
{
	LONG hwnd;

	if ((hwnd = WinLoadMenu(fr->GetHandle(), moduleHandle, id)) == 0)
		OOLThrow("error loading menu - menu not found", -10);

	return hwnd;
}


/*@ XResourceLibrary :: ~XResourceLibrary()
@group constructors/destructors
@remarks Destructs a resource library.
*/
XResourceLibrary :: ~XResourceLibrary()
{
	if (moduleHandle > 0)
		DosFreeModule(moduleHandle);
}
