#include "RichEdit.h"

#define LNGMODULE richedit_lay
#define LNGFILE <RichEdit/RichEdit.lay>

#define LANGUAGE LNGC_('E', 'N', 'U', 'S', 17)
#include <Core/lng_master.h>

#define LANGUAGE LNGC_('C', 'S', 'C', 'Z', 15)
#include <Core/lng_version.h>


void RichEdit::StyleTool(Bar& bar, int width)
{
	bar.Add(style, width);
}

void RichEdit::FaceTool(Bar& bar, int width)
{
	bar.Add(face, width);
}

void RichEdit::HeightTool(Bar& bar, int width)
{
	bar.Add(height, width);
}

void RichEdit::BoldTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditBold),
	        formatinfo.charvalid & RichText::BOLD ? RichEditImg::Bold() : RichEditImg::BoldMixed(),
	        THISBACK(Bold))
	   .Check(formatinfo.IsBold() && (formatinfo.charvalid & RichText::BOLD))
	   .Key(key);
}

void RichEdit::ItalicTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditItalic),
            formatinfo.charvalid & RichText::ITALIC ? RichEditImg::Italic() : RichEditImg::ItalicMixed(),
	        THISBACK(Italic))
	   .Check(formatinfo.IsItalic() && (formatinfo.charvalid & RichText::ITALIC))
	   .Key(key);
}

void RichEdit::UnderlineTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditUnderline),
	        formatinfo.charvalid & RichText::UNDERLINE ? RichEditImg::Underline()
	                                               : RichEditImg::UnderlineMixed(),
	        THISBACK(Underline))
	   .Check(formatinfo.IsUnderline() && (formatinfo.charvalid & RichText::UNDERLINE))
	   .Key(key);
}

void RichEdit::StrikeoutTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditStrikeout),
	        formatinfo.charvalid & RichText::STRIKEOUT ? RichEditImg::Strikeout()
	                                               : RichEditImg::StrikeoutMixed(),
	        THISBACK(Strikeout))
	   .Check(formatinfo.IsStrikeout() && (formatinfo.charvalid & RichText::STRIKEOUT))
	   .Key(key);
}


void RichEdit::CapitalsTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditCapitals),
	        formatinfo.charvalid & RichText::CAPITALS  ? RichEditImg::Capitals()
	                                               : RichEditImg::CapitalsMixed(),
	        THISBACK(Capitals))
	   .Check(formatinfo.capitals && (formatinfo.charvalid & RichText::CAPITALS));
}

void RichEdit::SuperscriptTool(Bar& bar, dword key)
{
	int i = formatinfo.charvalid & RichText::SSCRIPT ? formatinfo.sscript : 0;
	bar.Add(s_(RichEditSuperscript),
	        formatinfo.charvalid & RichText::SSCRIPT ? RichEditImg::SuperScript()
	                                             : RichEditImg::SuperScriptMixed(),
			THISBACK1(SetScript, i == 1 ? 0 : 1))
	   .Check(i == 1);
}

void RichEdit::SubscriptTool(Bar& bar, dword key)
{
	int i = formatinfo.charvalid & RichText::SSCRIPT ? formatinfo.sscript : 0;
	bar.Add(s_(RichEditSubscript),
	        formatinfo.charvalid & RichText::SSCRIPT ? RichEditImg::SubScript()
	                                             : RichEditImg::SubScriptMixed(),
			THISBACK1(SetScript, i == 2 ? 0 : 2))
	   .Check(i == 2);
}

void RichEdit::FontTools(Bar& bar)
{
	FaceTool(bar);
	HeightTool(bar);
	bar.Gap();
	BoldTool(bar);
	ItalicTool(bar);
	UnderlineTool(bar);
	StrikeoutTool(bar);
	CapitalsTool(bar);
	SuperscriptTool(bar);
	SubscriptTool(bar);
}

void RichEdit::InkTool(Bar& bar)
{
	bar.Add(ink);
}

void RichEdit::PaperTool(Bar& bar)
{
	bar.Add(paper);
}

void RichEdit::LanguageTool(Bar& bar, int width)
{
	bar.Add(language, width);
}

void RichEdit::SpellCheckTool(Bar& bar)
{
	bar.Add(s_(RichEditShowSpellErrs), RichEditImg::SpellCheck(), THISBACK(SpellCheck))
	   .Check(spellcheck);
}

void RichEdit::IndexEntryTool(Bar& bar, int width)
{
	bar.Add(indexentry, width);
}

void RichEdit::HyperlinkTool(Bar& bar, int width)
{
	bar.Add(hyperlink, width);
}

void RichEdit::LeftTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::ALIGN ? formatinfo.align : Null;
	bar.Add(s_(RichEditLeft), RichEditImg::Left(), THISBACK(AlignLeft))
	   .Check(a == ALIGN_LEFT)
	   .Key(K_CTRL_L);
}

void RichEdit::RightTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::ALIGN ? formatinfo.align : Null;
	bar.Add(s_(RichEditRight), RichEditImg::Right(), THISBACK(AlignRight))
	   .Check(a == ALIGN_RIGHT)
	   .Key(key);
}

void RichEdit::CenterTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::ALIGN ? formatinfo.align : Null;
	bar.Add(s_(RichEditCenter), RichEditImg::Center(), THISBACK(AlignCenter))
	   .Check(a == ALIGN_CENTER)
	   .Key(key);
}

void RichEdit::JustifyTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::ALIGN ? formatinfo.align : Null;
	bar.Add(s_(RichEditJustify), RichEditImg::Justify(), THISBACK(AlignJustify))
	   .Check(a == ALIGN_JUSTIFY)
	   .Key(key);
}

void  RichEdit::RoundBulletTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::BULLET ? formatinfo.bullet : Null;
	bar.Add(s_(RichEditRoundBullet), RichEditImg::RoundBullet(),
	        THISBACK1(SetBullet, RichPara::BULLET_ROUND))
	   .Check(a == RichPara::BULLET_ROUND)
	   .Key(key);
}

void  RichEdit::RoundWhiteBulletTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::BULLET ? formatinfo.bullet : Null;
	bar.Add(s_(RichEditWhiteRndBlt), RichEditImg::RoundWhiteBullet(),
	        THISBACK1(SetBullet, RichPara::BULLET_ROUNDWHITE))
	   .Check(a == RichPara::BULLET_ROUNDWHITE)
	   .Key(key);
}

void  RichEdit::BoxBulletTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::BULLET ? formatinfo.bullet : Null;
	bar.Add(s_(RichEditBxBullet), RichEditImg::BoxBullet(),
	        THISBACK1(SetBullet, RichPara::BULLET_BOX))
	   .Check(a == RichPara::BULLET_BOX)
	   .Key(key);
}

void  RichEdit::BoxWhiteBulletTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::BULLET ? formatinfo.bullet : Null;
	bar.Add(s_(RichEditWhiteBxBullet), RichEditImg::BoxWhiteBullet(),
	        THISBACK1(SetBullet, RichPara::BULLET_BOXWHITE))
	   .Check(a == RichPara::BULLET_BOXWHITE)
	   .Key(key);
}

void  RichEdit::TextBulletTool(Bar& bar, dword key)
{
	int a = formatinfo.paravalid & RichText::BULLET ? formatinfo.bullet : Null;
	bar.Add(s_(RichEditTextBlt), RichEditImg::TextBullet(),
	        THISBACK1(SetBullet, RichPara::BULLET_TEXT))
	   .Check(a == RichPara::BULLET_TEXT)
	   .Key(key);
}

void RichEdit::ParaFormatTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditParaFmt2), RichEditImg::ParaFormat(), THISBACK(ParaFormat))
	   .Key(key);
}

void RichEdit::LabelTool(Bar& bar, int width)
{
	bar.Add(label, width);
}

void RichEdit::ParaTools(Bar& bar)
{
	LeftTool(bar);
	CenterTool(bar);
	RightTool(bar);
	JustifyTool(bar);
	bar.Gap();
	RoundBulletTool(bar);
	RoundWhiteBulletTool(bar);
	BoxBulletTool(bar);
	BoxWhiteBulletTool(bar);
	TextBulletTool(bar);
	bar.Gap();
	ParaFormatTool(bar);
}

void RichEdit::UndoTool(Bar& bar, dword key)
{
	bar.Add(undo.GetCount(), s_(RichEditUndo), CtrlImg::undo(), THISBACK(Undo))
	   .Repeat()
	   .Key(key);
}

void RichEdit::RedoTool(Bar& bar, dword key)
{
	bar.Add(redo.GetCount(), s_(RichEditRedo), CtrlImg::redo(), THISBACK(Redo))
	   .Repeat()
	   .Key(key);
}

void RichEdit::CutTool(Bar& bar, dword key)
{
	bar.Add(IsSelection() || objectpos >= 0, s_(RichEditCut), CtrlImg::cut(), THISBACK(Cut))
	   .Key(key);
}

void RichEdit::CopyTool(Bar& bar, dword key)
{
	bar.Add(IsSelection() || objectpos >= 0, s_(RichEditCopy), CtrlImg::copy(), THISBACK(Copy))
	   .Key(key);
}

void RichEdit::PasteTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditPaste), CtrlImg::paste(), THISBACK(Paste))
	   .Key(key);
}

void RichEdit::ObjectTool(Bar& bar, dword key)
{
	for(int i = 0; i < RichObject::GetTypeCount(); i++) {
		String cn = RichObject::GetType(i).GetCreateName();
		if(!IsNull(cn))
			bar.Add(cn, THISBACK1(InsertObject, i));
	}
}

void RichEdit::PrintTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditPrint), CtrlImg::print(), THISBACK(Print))
	   .Key(key);
}

void RichEdit::FindReplaceTool(Bar& bar, dword key)
{
	bar.Add(s_(RichEditFind_Replace), RichEditImg::FindReplace(), THISBACK(OpenFindReplace))
	   .Key(key);
}

void RichEdit::EditTools(Bar& bar)
{
	UndoTool(bar);
	RedoTool(bar);
	bar.Gap();
	CutTool(bar);
	CopyTool(bar);
	PasteTool(bar);
	bar.Gap();
	FindReplaceTool(bar);
}

void RichEdit::InsertTableTool(Bar& bar, dword key)
{
	bar.Add(!IsSelection(), s_(RichEditInsertTable), RichEditImg::InsertTable(), THISBACK(InsertTable))
	   .Key(key);
}

void RichEdit::TablePropertiesTool(Bar& bar, dword key)
{
	bar.Add(!IsSelection() && cursorp.table, s_(RichTextTablePropsCaption), RichEditImg::TableProperties(),
	        THISBACK(TableProps))
	   .Key(key);
}

void RichEdit::InsertTableRowTool(Bar& bar, dword key)
{
	bar.Add(!IsSelection() && cursorp.table, s_(RichEditInsertTableRow), RichEditImg::TableInsertRow(),
	        THISBACK(TableInsertRow))
	   .Key(key);
}

void RichEdit::RemoveTableRowTool(Bar& bar, dword key)
{
	bar.Add(!IsSelection() && cursorp.table, s_(RichEditRemoveTableRow),
	        RichEditImg::TableRemoveRow(), THISBACK(TableRemoveRow))
	   .Key(key);
}

void RichEdit::InsertTableColumnTool(Bar& bar, dword key)
{
	bar.Add(!IsSelection() && cursorp.table, s_(RichEditInsertTableColumn), RichEditImg::TableInsertColumn(),
	        THISBACK(TableInsertColumn))
	   .Key(key);
}

void RichEdit::RemoveTableColumnTool(Bar& bar, dword key)
{
	bar.Add(!IsSelection() && cursorp.table, s_(RichEditRemoveTableColumn),
	        RichEditImg::TableRemoveColumn(), THISBACK(TableRemoveColumn))
	   .Key(key);
}

void RichEdit::SplitJoinCellTool(Bar& bar, dword key)
{
	bar.Add(!IsSelection() && cursorp.table, s_(RichEditSplitJoinCell),
	        RichEditImg::TableRemoveColumn(), THISBACK(TableRemoveColumn))
	   .Key(key);
}

void RichEdit::TableTools(Bar& bar)
{
	InsertTableTool(bar);
	TablePropertiesTool(bar);
	InsertTableRowTool(bar);
	RemoveTableRowTool(bar);
	InsertTableColumnTool(bar);
	RemoveTableColumnTool(bar);
}

void RichEdit::DefaultBar(Bar& bar, bool extended)
{
	StyleTool(bar);
	bar.Gap();
	FontTools(bar);
	bar.Gap();
	InkTool(bar);
	PaperTool(bar);
	bar.Gap();
	LanguageTool(bar);
	SpellCheckTool(bar);
	if(extended) {
		bar.Gap();
		IndexEntryTool(bar);
	}
	bar.Break();
	if(extended) {
		HyperlinkTool(bar);
		bar.Gap();
	}
	ParaTools(bar);
	bar.Gap();
	if(extended) {
		LabelTool(bar);
		bar.Gap();
	}
	EditTools(bar);
	bar.Gap();
	TableTools(bar);
	bar.Gap();
	PrintTool(bar);
}
