//	Zinc I18n Replacement Object - Z_REPSTR.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Services, Incorporated.  Pleasant Grove, Utah  USA

#include <z_repstr.hpp>

ZafI18nReplacement::ZafI18nReplacement()
{
    language = ZAF_NULLP(ZafLanguageData);
}

ZafI18nReplacement::~ZafI18nReplacement(void)
{
}

void ZafI18nReplacement::ReplaceAll(ZafWindow *window, const ZafIChar *languageName)
{
    if (!window || window->Error())
        return;

    // Use zafDataPersistence to locate the appropriate language table.
    language = new ZafLanguageData(window->StringID(), *zafObjectPersistence, languageName);
    if (!language->Error())
    {
		ReplaceText(window);
        ReplaceChildren(window);
    }

    // Clean up.
    delete language;
}

void ZafI18nReplacement::ReplaceChildren(ZafWindow *parent)
{
    // Replace language-specific text for support children.
	ZafWindowObject *object;
	for (object = parent->SupportFirst(); object; object = object->Next())
		ReplaceObject(object);

    // Replace language-specific text for normal children.
	for (object = parent->First(); object; object = object->Next())
		ReplaceObject(object);
}


ZafIChar *ZafI18nReplacement::LoadText(ZafWindowObject *object, ZafIChar *hotKeyChar, int *hotKeyIndex)
{
    // Load text.
    return (language->GetMessage(object->StringID(), false, hotKeyChar, hotKeyIndex));
}

void ZafI18nReplacement::ReplaceObject(ZafWindowObject *object)
{
	if (object->IsA(ID_ZAF_TITLE) ||
		object->IsA(ID_ZAF_MINIMIZE_BUTTON) ||
		object->IsA(ID_ZAF_MAXIMIZE_BUTTON))
		return;

	ReplaceText(object);
	ReplaceQuickTip(object);
	ReplaceHelpObjectTip(object);
	ReplaceUserText(object);

	// Replace the children's text.
	if (object->IsA(ID_ZAF_PULL_DOWN_ITEM))
	{
		ZafPullDownItem *item = (ZafPullDownItem *)object;
		if (item->menu.First())
			ReplaceChildren(&item->menu);
	}
	else if (object->IsA(ID_ZAF_POP_UP_ITEM))
	{
		ZafPopUpItem *item = (ZafPopUpItem *)object;
		if (item->menu.First())
			ReplaceChildren(&item->menu);
	}
	else if (object->IsA(ID_ZAF_SYSTEM_BUTTON))
	{
		ZafSystemButton *button = (ZafSystemButton *)object;
		if (button->menu.First())
			ReplaceChildren(&button->menu);
	}
	else if (object->IsA(ID_ZAF_WINDOW))
		ReplaceChildren((ZafWindow *)object);
}

void ZafI18nReplacement::ReplaceHelpObjectTip(ZafWindowObject *object)
{
    // Replace help object tip.
    ZafIChar nameBuf[256];
    sprintf(nameBuf, ZAF_ITEXT("%s_HTIP"), object->StringID());
    ZafIChar *i18nText = language->GetMessage(nameBuf);
    if (i18nText)
        object->SetHelpObjectTip(i18nText);
}

void ZafI18nReplacement::ReplaceQuickTip(ZafWindowObject *object)
{
    // Replace quick tip.
    ZafIChar nameBuf[256];
    sprintf(nameBuf, ZAF_ITEXT("%s_QTIP"), object->StringID());
    ZafIChar *i18nText = language->GetMessage(nameBuf);
    if (i18nText)
        object->SetQuickTip(i18nText);
}

void ZafI18nReplacement::ReplaceText(ZafWindowObject *object)
{
	if (object->IsA(ID_ZAF_BIGNUM) ||
		object->IsA(ID_ZAF_DATE) ||
		object->IsA(ID_ZAF_FORMATTED_STRING) ||
		object->IsA(ID_ZAF_INTEGER) ||
		object->IsA(ID_ZAF_REAL) ||
		object->IsA(ID_ZAF_TIME) ||
		object->IsA(ID_ZAF_UTIME) ||
		object->IsA(ID_ZAF_BITMAP) ||
		object->IsA(ID_ZAF_IMAGE) ||
		object->IsA(ID_ZAF_PROGRESS_BAR) ||
		object->IsA(ID_ZAF_SCROLL_BAR) ||
		object->IsA(ID_ZAF_SPIN_CONTROL) ||
		object->IsA(ID_ZAF_SPLITTER) ||
		object->IsA(ID_ZAF_HZ_LIST) ||
		object->IsA(ID_ZAF_VT_LIST) ||
		object->IsA(ID_ZAF_TREE_LIST) ||
		object->IsA(ID_ZAF_NOTEBOOK) ||
		object->IsA(ID_ZAF_TABLE) ||
		object->IsA(ID_ZAF_TABLE_HEADER) ||
		object->IsA(ID_ZAF_TABLE_RECORD) ||
		object->IsA(ID_ZAF_TOOL_BAR) ||
		object->IsA(ID_ZAF_STATUS_BAR) ||
		object->IsA(ID_ZAF_PULL_DOWN_MENU) ||
		object->IsA(ID_ZAF_SYSTEM_BUTTON))
		return;

	// Replace the object's textual data.
	ZafIChar hotKeyChar = 0;
	int hotKeyIndex = -1;
	ZafIChar *i18nText;
	if (object->IsA(ID_ZAF_WINDOW) && object->parent && object->parent->IsA(ID_ZAF_NOTEBOOK))
	{
		// Notebook tab and hotkey.
		ZafNotebook *notebook = (ZafNotebook *)object->parent;
		int page = object->ListIndex();
		i18nText = LoadText(object, &hotKeyChar, &hotKeyIndex);
		if (i18nText)
		{
			notebook->SetTabText(i18nText, page);
			notebook->SetTabHotKey(page, hotKeyChar, hotKeyIndex);
		}
	}
	else if (object->IsA(ID_ZAF_MESSAGE_WINDOW))
	{
		// Title text.
		i18nText = LoadText(object);
		if (i18nText)
			object->SetText(i18nText);

		// Message text.
		ZafIChar nameBuf[256];
		sprintf(nameBuf, ZAF_ITEXT("%s_MESSAGE"), object->StringID());
		i18nText = language->GetMessage(nameBuf);
		if (i18nText)
			((ZafMessageWindow *)object)->SetMessage(i18nText);
	}
	else if (object->IsA(ID_ZAF_GROUP))
	{
		// Group title and hotkey.
		ZafGroup *group = (ZafGroup *)object;
		i18nText = LoadText(group, &hotKeyChar, &hotKeyIndex);
		if (i18nText)
		{
			group->SetText(i18nText);
			group->SetHotKey(hotKeyChar, hotKeyIndex);
		}
	}
	else if (object->IsA(ID_ZAF_BUTTON))
	{
		// Button text and hotkey.
		ZafButton *button = (ZafButton *)object;
		i18nText = LoadText(button, &hotKeyChar, &hotKeyIndex);
		if (i18nText)
		{
			button->SetText(i18nText);
			button->SetHotKey(hotKeyChar, hotKeyIndex);
		}
	}
	else if (object->IsA(ID_ZAF_PROMPT))
	{
		// Prompt text and hotkey.
		ZafPrompt *prompt = (ZafPrompt *)object;
		i18nText = LoadText(prompt, &hotKeyChar, &hotKeyIndex);
		if (i18nText)
		{
			prompt->SetText(i18nText);
			prompt->SetHotKey(hotKeyChar, hotKeyIndex);
		}
	}
	else
	{
		// Object text.
		i18nText = LoadText(object);
		if (i18nText)
			object->SetText(i18nText);
	}
}

void ZafI18nReplacement::ReplaceUserText(ZafWindowObject *object)
{
    // Replace user text.
    ZafIChar nameBuf[256];
    sprintf(nameBuf, ZAF_ITEXT("%s_UTEXT"), object->StringID());
    ZafIChar *i18nText = language->GetMessage(nameBuf);
    if (i18nText)
	{
		delete []object->userText;
        object->userText = strdup(i18nText);
	}
}

