/*
**	FILE:			LgiRes_String.h
**	AUTHOR:			Matthew Allen
**	DATE:			10/9/1999
**	DESCRIPTION:	Dialog Resource Editor
**
**
**	Copyright (C) 1999, Matthew Allen
**		fret@memecode.com
*/

#ifndef __LGIRES_STRING_H
#define __LGIRES_STRING_H

#include "lgi/common/Res.h"
#include "lgi/common/List.h"
#include "lgi/common/Combo.h"

////////////////////////////////////////////////////////////////
class ResStringGroup;
class ResStringUi;

////////////////////////////////////////////////////////////////
class StrLang
{
	LLanguageId Lang;
	char *Str;

public:
	StrLang();
	~StrLang();

	LLanguageId GetLang();
	void SetLang(LLanguageId i);
	char *&GetStr();
	void SetStr(const char *s);
	bool IsEnglish();

	bool operator ==(LLanguageId LangId);
	bool operator !=(LLanguageId LangId);
};

class ResString : public LListItem, public FieldSource
{
	friend class ResStringGroup;
	friend class ResStringView;

protected:
	ResStringGroup *Group;

	char RefStr[16];
	char IdStr[16];
	char **GetIndex(int i);
	StrLang *GetLang(LLanguageId i);
	LAutoString Define;		// #define used in code to reference
	int Ref;			// globally unique
	int Id;				// numerical value used in code to reference

public:
	char *Tag;			// Optional component tag, for turning off features.
	List<StrLang> Items;
	LArray<class ResDialogCtrl*> Refs;
	LView *UpdateWnd;

	ResString(ResStringGroup *group, int init_ref = -1);
	~ResString();
	ResString &operator =(ResString &s);
	ResStringGroup *GetGroup() { return Group; }

	// Data
	int GetRef() { return Ref; }
	int SetRef(int r);
	int GetId() { return Id; }
	int SetId(int id);
	int NewId(); // Creates a new Id based on #define
	char *Get(LLanguageId Lang = 0);
	void Set(const char *s, LLanguageId Lang = 0);
	void UnDuplicate();
	int Compare(LListItem *To, ssize_t Field);
	void CopyText();
	void PasteText();
	char *GetDefine() { return Define; }
	void SetDefine(const char *s);

	// Item
	int GetCols();
	const char *GetText(int i);
	void OnMouseClick(LMouse &m);

	// Fields
	bool GetFields(FieldTree &Fields);
	bool Serialize(FieldTree &Fields);

	// Persistence
	bool Test(ErrorCollection *e);
	bool Read(LXmlTag *t, SerialiseContext &Ctx);
	bool Write(LXmlTag *t, SerialiseContext &Ctx);
};

#define RESIZE_X1			0x0001
#define RESIZE_Y1			0x0002
#define RESIZE_X2			0x0004
#define RESIZE_Y2			0x0008

class ResStringGroup : public Resource, public LList
{
	friend class ResString;
	friend class ResStringView;
	friend class ResStringUi;

protected:
	ResStringUi *Ui;
	List<ResString> Strs;
	LArray<LLanguage*> Lang;
	LArray<LLanguage*> Visible;
	int SortCol;
	int SortAscend;

	void UpdateColumns();
	void OnColumnClick(int Col, LMouse &m);

public:
	ResStringGroup(AppWnd *w, int type = TYPE_STRING);
	~ResStringGroup();

	LView *Wnd() { return dynamic_cast<LView*>(this); }
	bool Attach(LViewI *Parent) { return LList::Attach(Parent); }
	ResStringGroup *GetStringGroup() { return this; }

	// Methods
	void Create(LXmlTag *load, SerialiseContext *ctx);
	ResString *CreateStr(bool User = false);
	void DeleteStr(ResString *Str);
	ResString *FindName(char *Name);
	ResString *FindRef(int Ref);
	int FindId(int Id, List<ResString*> &Strs);
	int UniqueRef();
	int UniqueId(char *Define = 0);
	int OnCommand(int Cmd, int Event, OsView hWnd);
	void SetLanguages();
	int GetLanguages() { return (int)Lang.Length(); }
	LLanguage *GetLanguage(int i) { return i < Lang.Length() ? Lang[i] : 0; }
	LLanguage *GetLanguage(LLanguageId Id);
	int GetLangIdx(LLanguageId Id);
	void AppendLanguage(LLanguageId Id);
	void DeleteLanguage(LLanguageId Id);
	void Sort();
	void RemoveUnReferenced();
	List<ResString> *GetStrs() { return &Strs; }
	void OnShowLanguages();

	// Clipboard access
	void DeleteSel();
	void Copy(bool Delete = false);
	void Paste();

	// Resource
	LView *CreateUI();
	void OnRightClick(LSubMenu *RClick);
	void OnCommand(int Cmd);
	bool Test(ErrorCollection *e);
	bool Read(LXmlTag *t, SerialiseContext &Ctx);
	bool Write(LXmlTag *t, SerialiseContext &Ctx);
	ResStringGroup *IsStringGroup() { return this; }

	// LList
	void OnItemClick(LListItem *Item, LMouse &m);
	void OnItemSelect(LArray<LListItem*> &Items);
};

class ResStringUi : public LLayout
{
	LToolBar *Tools;
	ResStringGroup *StringGrp;
	LStatusBar *Status;
	LStatusPane *StatusInfo;

public:
	ResStringUi(ResStringGroup *Res);
	~ResStringUi();

	void OnPaint(LSurface *pDC);
	void PourAll();
	void OnPosChange();
	void OnCreate();
	LMessage::Result OnEvent(LMessage *Msg);
};

class LangDlg : public LDialog
{
	LCombo *Sel;
	List<LLanguage> Langs;

public:
	LLanguage *Lang;

	LangDlg(LView *parent, List<LLanguage> &l, int Init = -1);
	int OnNotify(LViewI *Ctrl, LNotification n);
};

////////////////////////////////////////////////////////////////
#endif
