From 1d0b695325e4a1d11c9986403766041e1be2ef4f Mon Sep 17 00:00:00 2001
From: George Hazan <ghazan@miranda.im>
Date: Mon, 30 Oct 2017 15:10:50 +0300
Subject: fixes #1010 (custom colors do not appear in the IRC log)

---
 plugins/TabSRMM/src/chat_main.cpp           |  1 +
 plugins/TabSRMM/src/chat_window.cpp         |  2 +-
 plugins/TabSRMM/src/generic_msghandlers.cpp |  4 +--
 plugins/TabSRMM/src/msgdialog.cpp           | 15 +++-----
 plugins/TabSRMM/src/msgdlgutils.cpp         | 37 ++++++++++++-------
 plugins/TabSRMM/src/msglog.cpp              | 10 +++---
 plugins/TabSRMM/src/msgs.cpp                |  3 --
 plugins/TabSRMM/src/msgs.h                  |  2 +-
 plugins/TabSRMM/src/utils.cpp               | 56 ++++++++++++++---------------
 plugins/TabSRMM/src/utils.h                 |  3 +-
 plugins/TabSRMM/src/version.h               |  2 +-
 11 files changed, 69 insertions(+), 66 deletions(-)

(limited to 'plugins/TabSRMM')

diff --git a/plugins/TabSRMM/src/chat_main.cpp b/plugins/TabSRMM/src/chat_main.cpp
index 72d0e9c0a3..9669930aea 100644
--- a/plugins/TabSRMM/src/chat_main.cpp
+++ b/plugins/TabSRMM/src/chat_main.cpp
@@ -215,6 +215,7 @@ static void stubShowRoom(SESSION_INFO *si)
 int Chat_Load()
 {
 	CheckUpdate();
+	Utils::RTF_CTableInit();
 
 	CHAT_MANAGER_INITDATA data = { &g_Settings, sizeof(MODULEINFO), sizeof(SESSION_INFO), LPGENW("Message Sessions") L"/" LPGENW("Group chats"), FONTMODE_ALTER };
 	pci = Chat_GetInterface(&data);
diff --git a/plugins/TabSRMM/src/chat_window.cpp b/plugins/TabSRMM/src/chat_window.cpp
index 2a9c2a8698..58bb9dce6e 100644
--- a/plugins/TabSRMM/src/chat_window.cpp
+++ b/plugins/TabSRMM/src/chat_window.cpp
@@ -639,7 +639,7 @@ void CChatRoomDlg::onClick_OK(CCtrlButton*)
 	if (ptszText.IsEmpty())
 		return;
 
-	DoRtfToTags(ptszText, 0, nullptr);
+	DoRtfToTags(ptszText);
 	ptszText.Trim();
 	ptszText.Replace(L"%", L"%%");
 
diff --git a/plugins/TabSRMM/src/generic_msghandlers.cpp b/plugins/TabSRMM/src/generic_msghandlers.cpp
index 6f6ca314bb..45fc240a4a 100644
--- a/plugins/TabSRMM/src/generic_msghandlers.cpp
+++ b/plugins/TabSRMM/src/generic_msghandlers.cpp
@@ -557,8 +557,8 @@ void CTabBaseDlg::DM_InitRichEdit()
 		LOGFONTA lf = m_pContainer->theme.logFonts[MSGFONTID_MESSAGEAREA];
 		inputcharcolor = m_pContainer->theme.fontColors[MSGFONTID_MESSAGEAREA];
 
-		for (int i = 0; i < Utils::rtf_ctable_size; i++)
-			if (Utils::rtf_ctable[i].clr == inputcharcolor)
+		for (int i = 0; i < Utils::rtf_clrs.getCount(); i++)
+			if (Utils::rtf_clrs[i].clr == inputcharcolor)
 				inputcharcolor = RGB(GetRValue(inputcharcolor), GetGValue(inputcharcolor), GetBValue(inputcharcolor) == 0 ? GetBValue(inputcharcolor) + 1 : GetBValue(inputcharcolor) - 1);
 
 		cf2.dwMask = CFM_COLOR | CFM_FACE | CFM_CHARSET | CFM_SIZE | CFM_WEIGHT | CFM_BOLD | CFM_ITALIC;
diff --git a/plugins/TabSRMM/src/msgdialog.cpp b/plugins/TabSRMM/src/msgdialog.cpp
index f5405007f5..2606353dc7 100644
--- a/plugins/TabSRMM/src/msgdialog.cpp
+++ b/plugins/TabSRMM/src/msgdialog.cpp
@@ -38,8 +38,6 @@ static const UINT addControls[] = { IDC_ADD, IDC_CANCELADD };
 static const UINT btnControls[] = { IDC_RETRY, IDC_CANCELSEND, IDC_MSGSENDLATER, IDC_ADD, IDC_CANCELADD };
 static const UINT errorControls[] = { IDC_STATICERRORICON, IDC_STATICTEXT, IDC_RETRY, IDC_CANCELSEND, IDC_MSGSENDLATER };
 
-static COLORREF rtfDefColors[] = { RGB(255, 0, 0), RGB(0, 0, 255), RGB(0, 255, 0), RGB(255, 0, 255), RGB(255, 255, 0), RGB(0, 255, 255), 0, RGB(255, 255, 255) };
-
 struct
 {
 	int id;
@@ -617,9 +615,6 @@ void CSrmmWindow::OnInitDialog()
 {
 	CTabBaseDlg::OnInitDialog();
 
-	if (Utils::rtf_ctable == 0)
-		Utils::RTF_CTableInit();
-
 	m_cache->setWindowData(this);
 
 	m_szProto = const_cast<char *>(m_cache->getProto());
@@ -1107,7 +1102,7 @@ void CSrmmWindow::onClick_Ok(CCtrlButton*)
 		return;
 
 	if (final_sendformat)
-		DoRtfToTags(decoded, _countof(rtfDefColors), rtfDefColors);
+		DoRtfToTags(decoded);
 	decoded.TrimRight();
 
 	T2Utf utfResult(decoded);
@@ -1208,8 +1203,8 @@ void CSrmmWindow::onClick_Color(CCtrlButton *pButton)
 
 	if (iSelection == ID_FONT_DEFAULTCOLOR) {
 		cf.crTextColor = M.GetDword(FONTMODULE, "Font16Col", 0);
-		for (int i = 0; i < Utils::rtf_ctable_size; i++)
-			if (Utils::rtf_ctable[i].clr == cf.crTextColor)
+		for (int i = 0; i < Utils::rtf_clrs.getCount(); i++)
+			if (Utils::rtf_clrs[i].clr == cf.crTextColor)
 				cf.crTextColor = RGB(GetRValue(cf.crTextColor), GetGValue(cf.crTextColor), GetBValue(cf.crTextColor) == 0 ? GetBValue(cf.crTextColor) + 1 : GetBValue(cf.crTextColor) - 1);
 
 		m_message.SendMsg(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
@@ -1217,8 +1212,8 @@ void CSrmmWindow::onClick_Color(CCtrlButton *pButton)
 	}
 	
 	for (int i = 0; i < RTF_CTABLE_DEFSIZE; i++) {
-		if (Utils::rtf_ctable[i].menuid == iSelection) {
-			cf.crTextColor = Utils::rtf_ctable[i].clr;
+		if (Utils::rtf_clrs[i].menuid == iSelection) {
+			cf.crTextColor = Utils::rtf_clrs[i].clr;
 			m_message.SendMsg(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
 		}
 	}
diff --git a/plugins/TabSRMM/src/msgdlgutils.cpp b/plugins/TabSRMM/src/msgdlgutils.cpp
index 09f477af63..19593fa9e7 100644
--- a/plugins/TabSRMM/src/msgdlgutils.cpp
+++ b/plugins/TabSRMM/src/msgdlgutils.cpp
@@ -827,7 +827,16 @@ static void CreateColorMap(CMStringW &Text, int iCount, COLORREF *pSrc, int *pDs
 // convert rich edit code to bbcode (if wanted). Otherwise, strip all RTF formatting
 // tags and return plain text
 
-BOOL CTabBaseDlg::DoRtfToTags(CMStringW &pszText, int iNumColors, COLORREF *pColors) const
+static int RtfColorToIndex(int iNumColors, int *pIndex, int iCol)
+{
+	for (int i = 0; i < iNumColors; i++)
+		if (pIndex[i] == iCol)
+			return i;
+
+	return -1;
+}
+
+BOOL CTabBaseDlg::DoRtfToTags(CMStringW &pszText) const
 {
 	if (pszText.IsEmpty())
 		return FALSE;
@@ -837,7 +846,11 @@ BOOL CTabBaseDlg::DoRtfToTags(CMStringW &pszText, int iNumColors, COLORREF *pCol
 
 	// create an index of colors in the module and map them to
 	// corresponding colors in the RTF color table
+	int iNumColors = Utils::rtf_clrs.getCount();
 	int *pIndex = (int*)_alloca(iNumColors * sizeof(int));
+	COLORREF *pColors = (COLORREF*)_alloca(iNumColors * sizeof(COLORREF));
+	for (int i = 0; i < iNumColors; i++)
+		pColors[i] = Utils::rtf_clrs[i].clr;
 	CreateColorMap(pszText, iNumColors, pColors, pIndex);
 
 	// scan the file for rtf commands and remove or parse them
@@ -867,22 +880,22 @@ BOOL CTabBaseDlg::DoRtfToTags(CMStringW &pszText, int iNumColors, COLORREF *pCol
 
 			if (!wcsncmp(p, L"\\cf", 3)) { // foreground color
 				int iCol = _wtoi(p + 3);
-				int iInd = -1;
-				for (int i = 0; i < iNumColors; i++)
-					if (pIndex[i] == iCol) {
-						iInd = i;
-						break;
-					}
+				int iInd = RtfColorToIndex(iNumColors, pIndex, iCol);
 
-				if (iCol && !isChat())
-					res.AppendFormat((iInd >= 0) ? (bInsideColor ? L"[/color][color=%s]" : L"[color=%s]") : (bInsideColor ? L"[/color]" : L""), Utils::rtf_ctable[iInd].szName);
+				if (iCol > 0) {
+					if (!isChat())
+						res.AppendFormat((iInd >= 0) ? (bInsideColor ? L"[/color][color=%s]" : L"[color=%s]") : (bInsideColor ? L"[/color]" : L""), Utils::rtf_clrs[iInd].szName);
+					// else
+					//		res.AppendFormat((iInd >= 0) ? L"%%c%02u" : L"%%C", iInd);					
+				}
 
 				bInsideColor = iInd >= 0;
 			}
 			else if (!wcsncmp(p, L"\\highlight", 10)) { //background color
-				wchar_t szTemp[20];
-				int iCol = _wtoi(p + 10);
-				mir_snwprintf(szTemp, L"%d", iCol);
+				/* if (isChat()) {
+					int iInd = RtfColorToIndex(iNumColors, pIndex, _wtoi(p + 10));
+					res.AppendFormat((iInd >= 0) ? L"%%f%02u" : L"%%F", iInd);
+				} */
 			}
 			else if (!wcsncmp(p, L"\\line", 5)) { // soft line break;
 				res.AppendChar('\n');
diff --git a/plugins/TabSRMM/src/msglog.cpp b/plugins/TabSRMM/src/msglog.cpp
index 512cd8373f..d96e3ddcbf 100644
--- a/plugins/TabSRMM/src/msglog.cpp
+++ b/plugins/TabSRMM/src/msglog.cpp
@@ -351,8 +351,10 @@ static void Build_RTF_Header(CMStringA &str, CTabBaseDlg *dat)
 	}
 
 	// bbcode colors...
-	for (i = 0; i < Utils::rtf_ctable_size; i++)
-		str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(Utils::rtf_ctable[i].clr), GetGValue(Utils::rtf_ctable[i].clr), GetBValue(Utils::rtf_ctable[i].clr));
+	for (i = 0; i < Utils::rtf_clrs.getCount(); i++) {
+		auto &p = Utils::rtf_clrs[i];
+		str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(p.clr), GetGValue(p.clr), GetBValue(p.clr));
+	}
 
 	// paragraph header
 	str.AppendFormat("}");
@@ -1083,8 +1085,8 @@ void CTabBaseDlg::ReplaceIcons(LONG startAt, int fAppend, BOOL isSent)
 			m_log.SendMsg(EM_REPLACESEL, FALSE, (LPARAM)L"");
 			UINT length = (unsigned int)_wtol(&trbuffer[7]);
 			int index = _wtol(&trbuffer[14]);
-			if (length > 0 && length < 20000 && index >= RTF_CTABLE_DEFSIZE && index < Utils::rtf_ctable_size) {
-				cf2.crTextColor = Utils::rtf_ctable[index].clr;
+			if (length > 0 && length < 20000 && index >= RTF_CTABLE_DEFSIZE && index < Utils::rtf_clrs.getCount()) {
+				cf2.crTextColor = Utils::rtf_clrs[index].clr;
 				cr.cpMin = fi.chrgText.cpMin;
 				cr.cpMax = cr.cpMin + length;
 				m_log.SendMsg(EM_EXSETSEL, 0, (LPARAM)&cr);
diff --git a/plugins/TabSRMM/src/msgs.cpp b/plugins/TabSRMM/src/msgs.cpp
index 411717f89b..fad83f4175 100644
--- a/plugins/TabSRMM/src/msgs.cpp
+++ b/plugins/TabSRMM/src/msgs.cpp
@@ -486,9 +486,6 @@ int SplitmsgShutdown(void)
 	UnloadIcons();
 	FreeTabConfig();
 
-	if (Utils::rtf_ctable)
-		mir_free(Utils::rtf_ctable);
-
 	UnloadTSButtonModule();
 	return 0;
 }
diff --git a/plugins/TabSRMM/src/msgs.h b/plugins/TabSRMM/src/msgs.h
index 397e5aba17..3882d33a4a 100644
--- a/plugins/TabSRMM/src/msgs.h
+++ b/plugins/TabSRMM/src/msgs.h
@@ -412,7 +412,7 @@ public:
 	void  AdjustBottomAvatarDisplay();
 	void  CalcDynamicAvatarSize(BITMAP *bminfo);
 	void  CheckStatusIconClick(POINT pt, const RECT &rc, int gap, int code);
-	BOOL  DoRtfToTags(CMStringW &pszText, int iNumColors, COLORREF *pColors) const;
+	BOOL  DoRtfToTags(CMStringW &pszText) const;
 	void  DrawStatusIcons(HDC hDC, const RECT &rc, int gap);
 	void  EnableSendButton(bool bMode) const;
 	void  EnableSending(bool bMode) const;
diff --git a/plugins/TabSRMM/src/utils.cpp b/plugins/TabSRMM/src/utils.cpp
index 607fe6ec74..8faf8bbce5 100644
--- a/plugins/TabSRMM/src/utils.cpp
+++ b/plugins/TabSRMM/src/utils.cpp
@@ -32,21 +32,7 @@
 #define MWF_LOG_TEXTFORMAT 0x2000000
 #define MSGDLGFONTCOUNT 22
 
-static TRTFColorTable _rtf_ctable[] =
-{
-	{ L"red", RGB(255, 0, 0), ID_FONT_RED },
-	{ L"blue", RGB(0, 0, 255), ID_FONT_BLUE },
-	{ L"green", RGB(0, 255, 0), ID_FONT_GREEN },
-	{ L"magenta", RGB(255, 0, 255), ID_FONT_MAGENTA },
-	{ L"yellow", RGB(255, 255, 0), ID_FONT_YELLOW },
-	{ L"cyan", RGB(0, 255, 255), ID_FONT_CYAN },
-	{ L"black", 0, ID_FONT_BLACK },
-	{ L"white", RGB(255, 255, 255), ID_FONT_WHITE },
-	{ L"", 0, 0 }
-};
-
-int				Utils::rtf_ctable_size = 0;
-TRTFColorTable* Utils::rtf_ctable = 0;
+OBJLIST<TRTFColorTable> Utils::rtf_clrs(10);
 
 MWindowList CWarning::hWindowList = 0;
 
@@ -95,9 +81,10 @@ void CTabBaseDlg::FormatRaw(CMStringW &msg, int flags, bool isSent)
 				CMStringW colorname = msg.Mid(beginmark + 7, 8);
 search_again:
 				bool clr_found = false;
-				for (int ii = 0; ii < Utils::rtf_ctable_size; ii++) {
-					if (!wcsnicmp(colorname, Utils::rtf_ctable[ii].szName, mir_wstrlen(Utils::rtf_ctable[ii].szName))) {
-						closing = beginmark + 7 + (int)mir_wstrlen(Utils::rtf_ctable[ii].szName);
+				for (int ii = 0; ii < Utils::rtf_clrs.getCount(); ii++) {
+					auto &rtfc = Utils::rtf_clrs[ii];
+					if (!wcsnicmp(colorname, rtfc.szName, mir_wstrlen(rtfc.szName))) {
+						closing = beginmark + 7 + (int)mir_wstrlen(rtfc.szName);
 						if (endmark != -1) {
 							msg.Delete(endmark, 8);
 							msg.Insert(endmark, L"c0 ");
@@ -427,14 +414,22 @@ int CTabBaseDlg::FindRTLLocale()
 /////////////////////////////////////////////////////////////////////////////////////////
 // init default color table. the table may grow when using custom colors via bbcodes
 
-void Utils::RTF_CTableInit()
+static TRTFColorTable _rtf_ctable[] =
 {
-	int iSize = sizeof(TRTFColorTable) * RTF_CTABLE_DEFSIZE;
+	{ L"red", RGB(255, 0, 0), ID_FONT_RED },
+	{ L"blue", RGB(0, 0, 255), ID_FONT_BLUE },
+	{ L"green", RGB(0, 255, 0), ID_FONT_GREEN },
+	{ L"magenta", RGB(255, 0, 255), ID_FONT_MAGENTA },
+	{ L"yellow", RGB(255, 255, 0), ID_FONT_YELLOW },
+	{ L"cyan", RGB(0, 255, 255), ID_FONT_CYAN },
+	{ L"black", 0, ID_FONT_BLACK },
+	{ L"white", RGB(255, 255, 255), ID_FONT_WHITE }
+};
 
-	rtf_ctable = (TRTFColorTable *)mir_alloc(iSize);
-	memset(rtf_ctable, 0, iSize);
-	memcpy(rtf_ctable, _rtf_ctable, iSize);
-	rtf_ctable_size = RTF_CTABLE_DEFSIZE;
+void Utils::RTF_CTableInit()
+{
+	for (int i = 0; i < _countof(_rtf_ctable); i++)
+		rtf_clrs.insert(new TRTFColorTable(_rtf_ctable[i]));
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////
@@ -442,16 +437,17 @@ void Utils::RTF_CTableInit()
 
 void Utils::RTF_ColorAdd(const wchar_t *tszColname, size_t length)
 {
-	wchar_t *stopped;
+	TRTFColorTable *p = new TRTFColorTable;
 
-	rtf_ctable_size++;
-	rtf_ctable = (TRTFColorTable *)mir_realloc(rtf_ctable, sizeof(TRTFColorTable) * rtf_ctable_size);
+	wchar_t *stopped;
 	COLORREF clr = wcstol(tszColname, &stopped, 16);
-	mir_snwprintf(rtf_ctable[rtf_ctable_size - 1].szName, length + 1, L"%06x", clr);
-	rtf_ctable[rtf_ctable_size - 1].menuid = 0;
+	mir_snwprintf(p->szName, length + 1, L"%06x", clr);
+	p->menuid = 0;
 
 	clr = wcstol(tszColname, &stopped, 16);
-	rtf_ctable[rtf_ctable_size - 1].clr = (RGB(GetBValue(clr), GetGValue(clr), GetRValue(clr)));
+	p->clr = (RGB(GetBValue(clr), GetGValue(clr), GetRValue(clr)));
+
+	rtf_clrs.insert(p);
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////
diff --git a/plugins/TabSRMM/src/utils.h b/plugins/TabSRMM/src/utils.h
index f6a3bf49b3..0218f7956e 100644
--- a/plugins/TabSRMM/src/utils.h
+++ b/plugins/TabSRMM/src/utils.h
@@ -104,8 +104,7 @@ public:
 	}
 
 public:
-	static	TRTFColorTable*		rtf_ctable;
-	static	int					rtf_ctable_size;
+	static OBJLIST<TRTFColorTable> rtf_clrs;
 };
 
 __forceinline LRESULT _dlgReturn(HWND hWnd, LRESULT result)
diff --git a/plugins/TabSRMM/src/version.h b/plugins/TabSRMM/src/version.h
index 46e0f319cc..354d5d3922 100644
--- a/plugins/TabSRMM/src/version.h
+++ b/plugins/TabSRMM/src/version.h
@@ -1,7 +1,7 @@
 #define __MAJOR_VERSION       3
 #define __MINOR_VERSION       5
 #define __RELEASE_NUM         0
-#define __BUILD_NUM           6
+#define __BUILD_NUM           7
 
 #include <stdver.h>
 
-- 
cgit v1.2.3