#include "ide.h" struct FileLine : Moveable { String file; int line; }; struct LngEntry : Moveable > { bool added; VectorMap text; Vector fileline; LngEntry(const LngEntry& e, int) { added = e.added; text <<= e.text; fileline <<= e.fileline; } LngEntry() {} void AddFileLine(CParser& p) { FileLine& fl = fileline.Add(); fl.file = p.GetFileName(); fl.line = p.GetLine(); } }; struct TFile : Moveable { bool dirty; bool java; String package, file; VectorMap map; Vector ls; void MakeLS() { Index lngset; lngset.Add(LNG_enUS); for(int i = 0; i < map.GetCount(); i++) for(int j = 0; j < map[i].text.GetCount(); j++) lngset.FindAdd(map[i].text.GetKey(j)); ls = lngset.PickKeys(); Sort(ls); } rval_default(TFile); TFile() { dirty = false; } }; void t_error(CParser& p) { PutConsole(p.GetFileName() + Format("(%d): t_/tt_ works only with plain text literals", p.GetLine())); } void LngParseCFile(const String& fn, VectorMap& lng) { String data = LoadFile(fn); CParser p(data, fn); try { while(!p.IsEof()) { if((p.Id("t_") || p.Id("tt_")) && p.Char('(')) if(p.IsString()) { String tid = p.ReadString(); LngEntry& le = lng.GetAdd(tid); le.text.GetAdd(LNG_enUS) = GetENUS(tid); le.AddFileLine(p); le.added = true; if(!p.Char(')')) t_error(p); } else t_error(p); else p.SkipTerm(); } } catch(CParser::Error e) { PutConsole(e); ShowConsole(); } } const Index& LngIndex() { static Index l; if(l.GetCount() == 0) { const int *x = GetAllLanguages(); while(*x) l.Add(*x++); } return l; } bool LngParseTFile(const String& fn, VectorMap& lng) { String data = LoadFile(fn); CParser p(data, fn); try { if(p.Char('#')) while(!p.IsEof()) { if(p.IsChar2('T', '_')) break; else p.SkipTerm(); } String id; while(!p.IsEof()) { if(p.Id("T_")) { p.PassChar('('); id = Null; do id.Cat(p.ReadString()); while(p.Char('+')); p.PassChar(')'); lng.GetAdd(id).added = false; } else { if(IsNull(id)) p.ThrowError("missing T_"); String lngs = p.ReadId(); p.PassChar('('); if(lngs.GetLength() == 4) { int lang = LNG_(ToUpper(lngs[0]), ToUpper(lngs[1]), ToUpper(lngs[2]), ToUpper(lngs[3])); if(LngIndex().Find(lang) >= 0) { String lt; do lt.Cat(p.ReadString()); while(p.Char('+')); lng.GetAdd(id).text.GetAdd(lang) = lt; p.PassChar(')'); continue; } } p.ThrowError("invalid language"); } } } catch(CParser::Error e) { PutConsole(e); ShowConsole(); return false; } return true; } String CreateTFile(const VectorMap& map, const Vector& lngset, bool rep, bool obsolete, bool java) { const char *linepfx = (java ? " + " : " "); int ascflags = (java ? 0 : ASCSTRING_OCTALHI) | ASCSTRING_SMART; String out; String cfile; out << "#ifdef _MSC_VER\r\n#pragma setlocale(\"C\")\r\n#endif"; for(int i = 0; i < map.GetCount(); i++) { if(i) out << "\r\n"; const LngEntry& e = map[i]; String nc; if(e.fileline.GetCount()) nc = e.fileline[0].file; if(nc != cfile) { cfile = nc; if(!IsNull(cfile) && !rep) out << "\r\n// " << GetFileName(cfile) << "\r\n\r\n"; if(IsNull(cfile) && obsolete) out << "\r\n// Obsolete\r\n\r\n"; } if(!IsNull(cfile) || rep || obsolete) { String id = map.GetKey(i); out << "T_(" << AsCString(id, 70, linepfx, ascflags) << ")\r\n"; for(int j = 0; j < lngset.GetCount(); j++) { int lang = lngset[j]; if(rep || lang != LNG_enUS) { int q = e.text.Find(lang); if(!rep || q >= 0 && !IsNull(e.text[q])) { int c = (lang >> 15) & 31; if(c) { out.Cat(c + 'a' - 1); c = (lang >> 10) & 31; if(c) { out.Cat(c + 'a' - 1); c = (lang >> 5) & 31; if(c) { out.Cat(c + 'A' - 1); c = lang & 31; if(c) out.Cat(c + 'A' - 1); } } } out << '(' << AsCString(q >= 0 ? e.text[q] : String(), 70, linepfx, ascflags) << ")\r\n"; } } } } } return out; } struct LangDlg : WithLangLayout { void Serialize(Stream& s); Vector& tfile; void AddLang(); void RemoveLang(); void AddLangAll(); void RemoveLangAll(); void EnterFile(); void EnterText(); void ToggleWork(); void LangMenu(Bar& bar); bool ShouldWrite(int i) { return file.Get(i, 1); } LangDlg(Vector& map); }; void LangDlg::Serialize(Stream& s) { SerializePlacement(s); String f = file.GetKey(); int l = lang.GetKey(); String t = text.GetKey(); s % f % l % t; file.FindSetCursor(f); lang.FindSetCursor(l); text.FindSetCursor(t); } void LangDlg::AddLang() { if(!file.IsCursor()) return; WithAddLangLayout dlg; CtrlLayoutOKCancel(dlg, "Add language"); dlg.lang <<= dlg.Breaker(999); TFile& tf = tfile[file.GetCursor()]; Vector& ls = tf.ls; for(;;) { int l = ~dlg.lang; dlg.ok.Enable((l & 31) && FindIndex(ls, l) < 0); switch(dlg.Run()) { case IDOK: l = ~dlg.lang; if(FindIndex(ls, l) < 0) { ls.Add(l); tf.dirty = true; Sort(ls); EnterFile(); lang.FindSetCursor(l); return; } break; case IDCANCEL: return; } } } void LangDlg::AddLangAll() { WithAddLangLayout dlg; CtrlLayoutOKCancel(dlg, "Add to all"); if(dlg.Run() != IDOK) return; int l = ~dlg.lang; for(int i = 0; i < tfile.GetCount(); i++) { TFile& tf = tfile[i]; Vector& ls = tf.ls; if(FindIndex(ls, l) < 0) { ls.Add(l); tf.dirty = true; Sort(ls); } } EnterFile(); lang.FindSetCursor(l); } void LangDlg::RemoveLang() { if(file.IsCursor() && lang.IsCursor() && (int)lang.GetKey() != LNG_enUS && PromptOKCancel("Remove selected language version?")) { TFile& tf = tfile[file.GetCursor()]; tf.ls.Remove(lang.GetCursor()); tf.dirty = true; EnterFile(); } } void LangDlg::RemoveLangAll() { WithAddLangLayout dlg; CtrlLayoutOKCancel(dlg, "Remove from all"); if(file.IsCursor() && lang.IsCursor()) dlg.lang <<= tfile[file.GetCursor()].ls[lang.GetCursor()]; if(dlg.Run() != IDOK) return; int l = ~dlg.lang; for(int i = 0; i < tfile.GetCount(); i++) { TFile& tf = tfile[i]; Vector& ls = tf.ls; int q = FindIndex(ls, l); if(q >= 0) { ls.Remove(q); tf.dirty = true; Sort(ls); } } EnterFile(); lang.FindSetCursor(l); } void LangDlg::LangMenu(Bar& bar) { bar.Add(file.IsCursor(), "Add..", [=] { AddLang(); }); bar.Add(lang.IsCursor(), "Remove", [=] { RemoveLang(); }); bar.Separator(); bar.Add(file.IsCursor(), "Add to all..", [=] { AddLangAll(); }); bar.Add(lang.IsCursor(), "Remove from all..", [=] { RemoveLangAll(); }); } struct FontAndColorDisplay : Display { Font font; Color color; void Paint(Draw& w, const Rect& r, const Value& q, Color ink, Color paper, dword s) const { w.DrawRect(r, paper); String vt = q; const char *txt = GetENUS(vt); int x = r.left; if(txt != ~vt) { String id(~vt, txt - 1); w.DrawText(x, r.top, id, font, txt[-1] == '\a' ? LtRed : Magenta); x += GetTextSize(id, font).cx + 6; } w.DrawText(x, r.top, AsCString(txt), font, ink); } FontAndColorDisplay(Font f, Color c) : font(f), color(c) {} }; void LangDlg::EnterFile() { TFile& tf = tfile[file.GetCursor()]; Sort(tf.ls); lang.Clear(); for(int i = 0; i < tf.ls.GetCount(); i++) lang.Add(tf.ls[i], LNGAsText(tf.ls[i])); lang.GoBegin(); static FontAndColorDisplay normal(ArialZ(11)(), Null); static FontAndColorDisplay added(ArialZ(11)().Bold(), Null); static FontAndColorDisplay obsolete(ArialZ(11)().Italic(), Null); text.Clear(); for(int i = 0; i < tf.map.GetCount(); i++) { text.Add(tf.map.GetKey(i)); LngEntry& e = tf.map[i]; Display *d; if(e.fileline.GetCount() == 0) d = &obsolete; else if(e.added) d = &added; else d = &normal; text.SetDisplay(text.GetCount() - 1, 0, *d); } text.GoBegin(); } void LangDlg::EnterText() { source.Clear(); VectorMap& map = tfile[file.GetCursor()].map; int q = text.GetCursor(); if(q < 0 || q >= map.GetCount()) return; LngEntry& e = map[q]; for(int i = 0; i < e.fileline.GetCount(); i++) { FileLine& f = e.fileline[i]; source.Add(f.file, f.line, GetFileName(f.file) + " (" + AsString(f.line) + ")"); } } void LangDlg::ToggleWork() { if(file.GetCount()) { bool q = !(int)file.Get(0, 1); for(int i = 0; i < file.GetCount(); i++) file.Set(i, 1, q); } } LangDlg::LangDlg(Vector& tfile) : tfile(tfile) { CtrlLayoutOKCancel(*this, "Translation files"); file.AddColumn("File"); HeaderCtrl::Column& m = file.AddColumn().Ctrls