summaryrefslogtreecommitdiff
path: root/plugins/SmileyAdd/src/smileyroutines.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2013-04-03 18:05:17 +0000
committerGeorge Hazan <george.hazan@gmail.com>2013-04-03 18:05:17 +0000
commita4f31a97e407280dc7e306a6c96948b53f9c0cab (patch)
tree8e76329ed4784e0c8e1ee81070c5b9985989263a /plugins/SmileyAdd/src/smileyroutines.cpp
parentf07feab44bd3ef047229f511fadb3a151f200654 (diff)
SmileyAdd:
- array indexes totally unborkred; - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@4296 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/SmileyAdd/src/smileyroutines.cpp')
-rw-r--r--plugins/SmileyAdd/src/smileyroutines.cpp232
1 files changed, 88 insertions, 144 deletions
diff --git a/plugins/SmileyAdd/src/smileyroutines.cpp b/plugins/SmileyAdd/src/smileyroutines.cpp
index 63440e4a83..651c34b0c3 100644
--- a/plugins/SmileyAdd/src/smileyroutines.cpp
+++ b/plugins/SmileyAdd/src/smileyroutines.cpp
@@ -52,45 +52,34 @@ void LookupAllSmileys(SmileyPackType* smileyPack, SmileyPackCType* smileyCPack,
int i = 0;
if (sml)
- {
- for (int j=0; j<sml->getCount(); j++)
- {
+ for (int j=0; j < sml->getCount(); j++) {
(*sml)[j].find(tmpstr, smileys[i], false);
i++;
}
- }
if (smlc)
- {
- for (int j=0; j<smlc->getCount(); j++)
- {
+ for (int j=0; j < smlc->getCount(); j++) {
(*smlc)[j].find(tmpstr, smileys[i], false);
i++;
}
- }
- int* csmlit = (int*)alloca(smlsz * sizeof(int));
- if (csmlit == NULL) return;
+ int *csmlit = (int*)alloca(smlsz * sizeof(int));
memset(csmlit, 0, smlsz * sizeof(int));
long numCharsSoFar = 0;
bkstring::size_type smloff = 0;
- for (;;)
- {
+ while (true) {
int firstSml = -1;
int firstSmlRef = -1;
SmileyLookup::SmileyLocVecType* smlf = NULL;
- for (int csml=0; csml<smlsz; csml++)
- {
+ for (int csml=0; csml < smlsz; csml++) {
SmileyLookup::SmileyLocVecType& smlv = smileys[csml];
int tsml;
- for (tsml = csmlit[csml]; tsml < smlv.getCount(); tsml++)
- {
- if (smlv[tsml].pos >= smloff)
- {
+ for (tsml = csmlit[csml]; tsml < smlv.getCount(); tsml++) {
+ if (smlv[tsml].pos >= smloff) {
if (firstSmlRef == -1 || smlv[tsml].pos < (*smlf)[firstSmlRef].pos ||
(smlv[tsml].pos == (*smlf)[firstSmlRef].pos && smlv[tsml].len > (*smlf)[firstSmlRef].len))
{
@@ -104,61 +93,52 @@ void LookupAllSmileys(SmileyPackType* smileyPack, SmileyPackCType* smileyCPack,
csmlit[csml] = tsml;
}
- // Check if smiley found
- if (firstSml != -1)
- {
- ReplaceSmileyType *dat = new ReplaceSmileyType;
+ // // Nothing to parse, exiting
+ if (firstSml == -1)
+ break;
- const TCHAR* textToSearch = lpstrText + smloff;
- const TCHAR* textSmlStart = lpstrText + (*smlf)[firstSmlRef].pos;
- const TCHAR* textSmlEnd = textSmlStart + (*smlf)[firstSmlRef].len;
+ ReplaceSmileyType *dat = new ReplaceSmileyType;
- // check if leading space exist
- const TCHAR* prech = _tcsdec(textToSearch, textSmlStart);
- dat->ldspace = prech != NULL ? _istspace(*prech) != 0 : smloff == 0;
+ const TCHAR* textToSearch = lpstrText + smloff;
+ const TCHAR* textSmlStart = lpstrText + (*smlf)[firstSmlRef].pos;
+ const TCHAR* textSmlEnd = textSmlStart + (*smlf)[firstSmlRef].len;
- // check if trailing space exist
- dat->trspace = *textSmlEnd == 0 || _istspace(*textSmlEnd);
+ // check if leading space exist
+ const TCHAR* prech = _tcsdec(textToSearch, textSmlStart);
+ dat->ldspace = prech != NULL ? _istspace(*prech) != 0 : smloff == 0;
- // compute text location in RichEdit
- dat->loc.cpMin = (long)_tcsnccnt(textToSearch, (*smlf)[firstSmlRef].pos - smloff) + numCharsSoFar;
- dat->loc.cpMax = numCharsSoFar = (long)_tcsnccnt(textSmlStart, (*smlf)[firstSmlRef].len) + dat->loc.cpMin;
+ // check if trailing space exist
+ dat->trspace = *textSmlEnd == 0 || _istspace(*textSmlEnd);
- if (!opt.EnforceSpaces || (dat->ldspace && dat->trspace))
- {
- dat->ldspace |= !opt.SurroundSmileyWithSpaces;
- dat->trspace |= !opt.SurroundSmileyWithSpaces;
+ // compute text location in RichEdit
+ dat->loc.cpMin = (long)_tcsnccnt(textToSearch, (*smlf)[firstSmlRef].pos - smloff) + numCharsSoFar;
+ dat->loc.cpMax = numCharsSoFar = (long)_tcsnccnt(textSmlStart, (*smlf)[firstSmlRef].len) + dat->loc.cpMin;
- if (firstSml < smlszo)
- {
- dat->sml = smileyPack->GetSmiley((*sml)[firstSml].GetIndex());
- dat->smlc = NULL;
- }
- else
- {
- dat->smlc = smileyCPack->GetSmiley((*smlc)[firstSml-smlszo].GetIndex());
- dat->sml = NULL;
- }
+ if (!opt.EnforceSpaces || (dat->ldspace && dat->trspace)) {
+ dat->ldspace |= !opt.SurroundSmileyWithSpaces;
+ dat->trspace |= !opt.SurroundSmileyWithSpaces;
- if (dat->sml != NULL || dat->smlc != NULL)
- {
- // First smiley found record it
- smllist.insert(dat);
- if (firstOnly) break;
- }
- else
- delete dat;
+ if (firstSml < smlszo) {
+ dat->sml = smileyPack->GetSmiley((*sml)[firstSml].GetIndex());
+ dat->smlc = NULL;
+ }
+ else {
+ dat->smlc = smileyCPack->GetSmiley((*smlc)[firstSml-smlszo].GetIndex());
+ dat->sml = NULL;
}
- else
- delete dat;
- // Advance string pointer to search for the next smiley
- smloff = (*smlf)[firstSmlRef].pos + (*smlf)[firstSmlRef].len;
- csmlit[firstSml]++;
+ if (dat->sml != NULL || dat->smlc != NULL) {
+ // First smiley found record it
+ smllist.insert(dat);
+ if (firstOnly) break;
+ }
+ else delete dat;
}
- else
- // Nothing to parse exit
- break;
+ else delete dat;
+
+ // Advance string pointer to search for the next smiley
+ smloff = (*smlf)[firstSmlRef].pos + (*smlf)[firstSmlRef].len;
+ csmlit[firstSml]++;
}
delete[] smileys;
}
@@ -169,13 +149,11 @@ void FindSmileyInText(SmileyPackType* smp, const TCHAR* str,
{
SmileysQueueType smllist;
LookupAllSmileys(smp, NULL, str, smllist, true);
- if (smllist.getCount() == 0)
- {
+ if (smllist.getCount() == 0) {
size = 0;
*sml = NULL;
}
- else
- {
+ else {
first = smllist[0].loc.cpMin;
size = smllist[0].loc.cpMax - smllist[0].loc.cpMin;
*sml = smllist[0].sml;
@@ -187,24 +165,19 @@ SmileyType* FindButtonSmiley(SmileyPackType* smp)
{
unsigned start, size;
SmileyType* sml;
-
FindSmileyInText(smp, smp->GetButtonSmiley(), start, size, &sml);
-
return sml;
}
void UpdateSelection(CHARRANGE& sel, int pos, int dif)
{
- if (sel.cpMax == sel.cpMin)
- {
- if (sel.cpMax < LONG_MAX && sel.cpMax > pos)
- {
+ if (sel.cpMax == sel.cpMin) {
+ if (sel.cpMax < LONG_MAX && sel.cpMax > pos) {
sel.cpMax += dif;
sel.cpMin += dif;
}
}
- else
- {
+ else {
if (sel.cpMax >= pos && sel.cpMax < LONG_MAX) sel.cpMax += dif;
if (sel.cpMin > pos) sel.cpMin += dif;
}
@@ -220,15 +193,13 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
return;
ITextDocument* TextDocument;
- if (RichEditOle->QueryInterface(IID_ITextDocument, (void**)&TextDocument) != S_OK)
- {
+ if (RichEditOle->QueryInterface(IID_ITextDocument, (void**)&TextDocument) != S_OK) {
RichEditOle->Release();
return;
}
long cnt;
- if (smp == NULL && smcp == NULL)
- {
+ if (smp == NULL && smcp == NULL) {
if (unFreeze) TextDocument->Unfreeze(&cnt);
TextDocument->Release();
RichEditOle->Release();
@@ -237,8 +208,7 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
// retrieve text range
ITextRange* TextRange;
- if (TextDocument->Range(sel.cpMin, sel.cpMax, &TextRange) != S_OK)
- {
+ if (TextDocument->Range(sel.cpMin, sel.cpMax, &TextRange) != S_OK) {
TextDocument->Release();
RichEditOle->Release();
return;
@@ -246,8 +216,7 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
// retrieve text to parse for smileys
BSTR btxt = 0;
- if (TextRange->GetText(&btxt) != S_OK)
- {
+ if (TextRange->GetText(&btxt) != S_OK) {
TextRange->Release();
TextDocument->Release();
RichEditOle->Release();
@@ -257,14 +226,11 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
TextRange->Release();
SmileysQueueType smllist;
-
-
LookupAllSmileys(smp, smcp, W2T_SM(btxt), smllist, false);
SysFreeString(btxt);
- if (smllist.getCount() != 0)
- {
+ if (smllist.getCount() != 0) {
// disable screen updates
TextDocument->Freeze(&cnt);
@@ -296,15 +262,11 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
// Determine background color
// This logic trying to minimize number of background color changes
static COLORREF bkgColor = GetSysColor(COLOR_WINDOW);
-// if (!insemf)
-// {
- COLORREF bkgColorPv = (COLORREF)SendMessage(hwnd, EM_SETBKGNDCOLOR, 0, bkgColor);
- if (bkgColorPv != bkgColor)
- {
- bkgColor = bkgColorPv;
- SendMessage(hwnd, EM_SETBKGNDCOLOR, 0, bkgColor);
- }
-// }
+ COLORREF bkgColorPv = (COLORREF)SendMessage(hwnd, EM_SETBKGNDCOLOR, 0, bkgColor);
+ if (bkgColorPv != bkgColor) {
+ bkgColor = bkgColorPv;
+ SendMessage(hwnd, EM_SETBKGNDCOLOR, 0, bkgColor);
+ }
HDC hdc = GetDC(hwnd);
int sclX = GetDeviceCaps(hdc, LOGPIXELSX);
@@ -315,20 +277,18 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
BSTR spaceb = SysAllocString(_T(" "));
// Replace smileys specified in the list in RichEdit
- for (int j = smllist.getCount(); j--; )
- {
+ for (int j = smllist.getCount()-1; j >= 0; j--) {
CHARRANGE& smlpos = smllist[j].loc;
- if (ignoreLast && oldSel.cpMax == smlpos.cpMax) continue;
+ if (ignoreLast && oldSel.cpMax == smlpos.cpMax)
+ continue;
smlpos.cpMin += sel.cpMin;
smlpos.cpMax += sel.cpMin;
// Find all back to back smileys and for propper hidden text detection
- if ( numBTBSm == 0 )
- {
+ if (numBTBSm == 0) {
CHARRANGE lastPos = smlpos;
- for (int jn = j; jn--; )
- {
+ for (int jn = j; jn--; ) {
if (jn != j && smllist[jn].loc.cpMax != lastPos.cpMin)
break;
@@ -340,8 +300,7 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
TextFont->GetHidden(&hid);
if (hid == tomFalse) numBTBSm = 0;
}
- if ( numBTBSm != 0 )
- {
+ if (numBTBSm != 0) {
--numBTBSm;
continue;
}
@@ -355,20 +314,19 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
BSTR btxt = NULL;
- if (smlc == NULL && sml->IsText())
- {
+ if (smlc == NULL && sml->IsText()) {
btxt = SysAllocString(T2W_SM(sml->GetToolText().c_str()));
TextSelection->SetText(btxt);
}
- else
- {
+ else {
TextSelection->GetText(&btxt);
// Get font properties
SendMessage(hwnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&chf);
//do not look for smileys in hyperlinks
- if ((chf.dwEffects & (CFE_LINK | CFE_HIDDEN)) != 0) continue;
+ if ((chf.dwEffects & (CFE_LINK | CFE_HIDDEN)) != 0)
+ continue;
SIZE osize;
if (sml)
@@ -376,11 +334,11 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
else
smlc->GetSize(osize);
- if (osize.cx == 0 || osize.cy == 0) continue;
+ if (osize.cx == 0 || osize.cy == 0)
+ continue;
int sizeX, sizeY;
- if (opt.ScaleToTextheight)
- {
+ if (opt.ScaleToTextheight) {
sizeY = CalculateTextHeight(hdc, &chf);
sizeX = osize.cx * sizeY / osize.cy;
@@ -390,14 +348,12 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
int dy = osize.cy - sizeY;
sizeY += dy & 1;
}
- else
- {
+ else {
sizeX = osize.cx;
sizeY = osize.cy;
}
- if (smlc != NULL && opt.MaxCustomSmileySize && (unsigned)sizeY > opt.MaxCustomSmileySize)
- {
+ if (smlc != NULL && opt.MaxCustomSmileySize && (unsigned)sizeY > opt.MaxCustomSmileySize) {
sizeY = opt.MaxCustomSmileySize;
sizeX = osize.cx * sizeY / osize.cy;
@@ -408,8 +364,7 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
sizeY += dy & 1;
}
- if (opt.MinSmileySize && (unsigned)sizeY < opt.MinSmileySize)
- {
+ if (opt.MinSmileySize && (unsigned)sizeY < opt.MinSmileySize) {
sizeY = opt.MinSmileySize;
sizeX = osize.cx * sizeY / osize.cy;
@@ -429,8 +384,7 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
if (chf.dwEffects & CFE_AUTOBACKCOLOR) chf.crBackColor = bkgColor;
// insert space after
- if (!smllist[j].trspace && useHidden)
- {
+ if (!smllist[j].trspace && useHidden) {
TextSelection->SetStart(smlpos.cpMax);
TextSelection->TypeText(spaceb);
UpdateSelection(oldSel, smlpos.cpMax , 1);
@@ -439,16 +393,12 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
TextSelection->SetRange(smlpos.cpMin, smlpos.cpMax);
}
- if (g_HiddenTextSupported && useHidden)
- {
+ if (g_HiddenTextSupported && useHidden) {
TextFont->SetHidden(tomTrue);
TextSelection->SetEnd(smlpos.cpMin);
UpdateSelection(oldSel, smlpos.cpMin , 1);
}
- else
- {
- UpdateSelection(oldSel, smlpos.cpMin, -(int)SysStringLen(btxt)+1);
- }
+ else UpdateSelection(oldSel, smlpos.cpMin, -(int)SysStringLen(btxt)+1);
ISmileyBase* smileyBase = CreateAniSmileyObject(smlc ? smlc : sml, chf.crBackColor, ishpp);
if (smileyBase == NULL) continue;
@@ -463,9 +413,7 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
RichEditOle->GetClientSite(&pOleClientSite);
// Now Add the object to the RichEdit
- REOBJECT reobject = { 0 };
-
- reobject.cbStruct = sizeof(REOBJECT);
+ REOBJECT reobject = { sizeof(REOBJECT) };
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.poleobj = smileyBase;
@@ -479,8 +427,7 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
smileyBase->Release();
// insert space before
- if (!smllist[j].ldspace && useHidden)
- {
+ if (!smllist[j].ldspace && useHidden) {
TextSelection->SetRange(smlpos.cpMin, smlpos.cpMin);
TextSelection->TypeText(spaceb);
UpdateSelection(oldSel, smlpos.cpMin , 1);
@@ -502,8 +449,7 @@ void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const
if (cnt == 0) UpdateWindow(hwnd);
}
- if (unFreeze)
- {
+ if (unFreeze) {
TextDocument->Unfreeze(&cnt);
if (cnt == 0) UpdateWindow(hwnd);
}
@@ -521,16 +467,14 @@ void ReplaceSmileysWithText(HWND hwnd, CHARRANGE& sel, bool keepFrozen)
return;
ITextDocument* TextDocument;
- if (RichEditOle->QueryInterface(IID_ITextDocument, (void**)&TextDocument) != S_OK)
- {
+ if (RichEditOle->QueryInterface(IID_ITextDocument, (void**)&TextDocument) != S_OK) {
RichEditOle->Release();
return;
}
// retrieve text range
ITextRange* TextRange;
- if (TextDocument->Range(0, 0, &TextRange) != S_OK)
- {
+ if (TextDocument->Range(0, 0, &TextRange) != S_OK) {
TextDocument->Release();
RichEditOle->Release();
return;
@@ -546,16 +490,15 @@ void ReplaceSmileysWithText(HWND hwnd, CHARRANGE& sel, bool keepFrozen)
SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&oldSel);
int objectCount = RichEditOle->GetObjectCount();
- for (int i = objectCount - 1; i >= 0; i--)
- {
+ for (int i = objectCount-1; i >= 0; i--) {
REOBJECT reObj = {0};
reObj.cbStruct = sizeof(REOBJECT);
HRESULT hr = RichEditOle->GetObject(i, &reObj, REO_GETOBJ_POLEOBJ);
- if (FAILED(hr)) continue;
+ if (FAILED(hr))
+ continue;
- if (reObj.cp < sel.cpMin)
- {
+ if (reObj.cp < sel.cpMin) {
reObj.poleobj->Release();
break;
}
@@ -565,7 +508,8 @@ void ReplaceSmileysWithText(HWND hwnd, CHARRANGE& sel, bool keepFrozen)
reObj.poleobj->QueryInterface(IID_ISmileyAddSmiley, (void**) &igsc);
reObj.poleobj->Release();
- if (igsc == NULL) continue;
+ if (igsc == NULL)
+ continue;
TextRange->SetRange(reObj.cp, reObj.cp + 1);