diff options
author | George Hazan <ghazan@miranda.im> | 2020-04-03 20:05:55 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2020-04-03 20:05:55 +0300 |
commit | 6d89b6043c333831aed709e6576fa7d6c247b2ff (patch) | |
tree | 47c6ce4712fc90b1daaed8e37a515f9bcffdcb02 /libs/mTextControl | |
parent | e3f92ff9b8922e06a717a84b76c4e43cbb165f28 (diff) |
mTextControl moved to /libs
Diffstat (limited to 'libs/mTextControl')
-rw-r--r-- | libs/mTextControl/mtextcontrol.vcxproj | 33 | ||||
-rw-r--r-- | libs/mTextControl/mtextcontrol.vcxproj.filters | 18 | ||||
-rw-r--r-- | libs/mTextControl/res/version.rc | 9 | ||||
-rw-r--r-- | libs/mTextControl/src/FormattedTextDraw.cpp | 517 | ||||
-rw-r--r-- | libs/mTextControl/src/FormattedTextDraw.h | 191 | ||||
-rw-r--r-- | libs/mTextControl/src/ImageDataObjectHlp.cpp | 209 | ||||
-rw-r--r-- | libs/mTextControl/src/ImageDataObjectHlp.h | 28 | ||||
-rw-r--r-- | libs/mTextControl/src/dataobject.cpp | 276 | ||||
-rw-r--r-- | libs/mTextControl/src/enumformat.cpp | 220 | ||||
-rw-r--r-- | libs/mTextControl/src/fancy_rtf.cpp | 171 | ||||
-rw-r--r-- | libs/mTextControl/src/main.cpp | 58 | ||||
-rw-r--r-- | libs/mTextControl/src/richeditutils.cpp | 140 | ||||
-rw-r--r-- | libs/mTextControl/src/services.cpp | 206 | ||||
-rw-r--r-- | libs/mTextControl/src/services.h | 26 | ||||
-rw-r--r-- | libs/mTextControl/src/stdafx.cxx | 18 | ||||
-rw-r--r-- | libs/mTextControl/src/stdafx.h | 52 | ||||
-rw-r--r-- | libs/mTextControl/src/textcontrol.cpp | 146 | ||||
-rw-r--r-- | libs/mTextControl/src/textusers.cpp | 85 | ||||
-rw-r--r-- | libs/mTextControl/src/textusers.h | 42 | ||||
-rw-r--r-- | libs/mTextControl/src/version.h | 13 |
20 files changed, 2458 insertions, 0 deletions
diff --git a/libs/mTextControl/mtextcontrol.vcxproj b/libs/mTextControl/mtextcontrol.vcxproj new file mode 100644 index 0000000000..9bef453c29 --- /dev/null +++ b/libs/mTextControl/mtextcontrol.vcxproj @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectName>mTextControl</ProjectName> + <ProjectGuid>{E26D2311-C164-48CF-BA24-5CEDA873D1B2}</ProjectGuid> + </PropertyGroup> + <ImportGroup Label="PropertySheets"> + <Import Project="$(ProjectDir)..\..\build\vc.common\lib.props" /> + </ImportGroup> + <ItemDefinitionGroup> + <ClCompile> + <PreprocessorDefinitions>MTEXTCONTROL_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions> + </ClCompile> + </ItemDefinitionGroup> +</Project>
\ No newline at end of file diff --git a/libs/mTextControl/mtextcontrol.vcxproj.filters b/libs/mTextControl/mtextcontrol.vcxproj.filters new file mode 100644 index 0000000000..83cffdb711 --- /dev/null +++ b/libs/mTextControl/mtextcontrol.vcxproj.filters @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$(ProjectDir)..\..\build\vc.common\common.filters" /> + <ItemGroup> + <ClInclude Include="src\*.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClCompile Include="src\*.cpp;src\*.cxx"> + <Filter>Source Files</Filter> + </ClCompile> + <ResourceCompile Include="res\*.rc"> + <Filter>Resource Files</Filter> + </ResourceCompile> + <None Include="res\*.ico;res\*.bmp;res\*.cur"> + <Filter>Resource Files</Filter> + </None> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/libs/mTextControl/res/version.rc b/libs/mTextControl/res/version.rc new file mode 100644 index 0000000000..5a5ddd63ed --- /dev/null +++ b/libs/mTextControl/res/version.rc @@ -0,0 +1,9 @@ +// Microsoft Visual C++ generated resource script. +// +#ifdef APSTUDIO_INVOKED +#error this file is not editable by Microsoft Visual C++ +#endif //APSTUDIO_INVOKED + +#include "..\src\version.h" + +#include "..\..\build\Version.rc" diff --git a/libs/mTextControl/src/FormattedTextDraw.cpp b/libs/mTextControl/src/FormattedTextDraw.cpp new file mode 100644 index 0000000000..eca1044a5c --- /dev/null +++ b/libs/mTextControl/src/FormattedTextDraw.cpp @@ -0,0 +1,517 @@ +// Feel free to use this code in your own applications. +// The Author does not guarantee anything about this code. +// Author : Yves Maurer + +// FormattedTextDraw.cpp : Implementation of CFormattedTextDraw + +#include "stdafx.h" +#include "FormattedTextDraw.h" + +const IID IID_ITextServices = { // 8d33f740-cf58-11ce-a89d-00aa006cadc5 + 0x8d33f740, + 0xcf58, + 0x11ce, + { 0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5 } +}; + +const IID IID_ITextHost = { // c5bdd8d0-d26e-11ce-a89e-00aa006cadc5 + 0xc5bdd8d0, + 0xd26e, + 0x11ce, + { 0xa8, 0x9e, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5 } +}; + +const IID IID_ITextDocument = { + 0x8CC497C0, + 0xA1DF, + 0x11CE, + { 0x80, 0x98, 0x00, 0xAA, 0x00, 0x47, 0xBE, 0x5D } +}; + +///////////////////////////////////////////////////////////////////////////// +// CallBack functions + +DWORD CALLBACK EditStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) +{ + COOKIE *pCookie = (COOKIE*)dwCookie; + if (pCookie->isUnicode) { + if ((pCookie->cbSize - pCookie->cbCount)*sizeof(WCHAR) < (size_t)cb) + *pcb = LONG(pCookie->cbSize - pCookie->cbCount)*sizeof(WCHAR); + else + *pcb = cb & ~1UL; + memcpy(pbBuff, pCookie->unicode + pCookie->cbCount, *pcb); + pCookie->cbCount += *pcb / sizeof(WCHAR); + } + else { + if (pCookie->cbSize - pCookie->cbCount < (DWORD)cb) + *pcb = LONG(pCookie->cbSize - pCookie->cbCount); + else + *pcb = cb; + memcpy(pbBuff, pCookie->ansi + pCookie->cbCount, *pcb); + pCookie->cbCount += *pcb; + } + + return 0; // callback succeeded - no errors +} + +///////////////////////////////////////////////////////////////////////////// +// CFormattedTextDraw + +HRESULT CFormattedTextDraw::putRTFTextA(char *newVal) +{ + if (!m_spTextServices) + return S_FALSE; + + m_editCookie.isUnicode = false; + m_editCookie.ansi = newVal; + m_editCookie.cbSize = mir_strlen(m_editCookie.ansi); + m_editCookie.cbCount = 0; + + EDITSTREAM editStream; + editStream.dwCookie = (DWORD_PTR)&m_editCookie; + editStream.dwError = 0; + editStream.pfnCallback = (EDITSTREAMCALLBACK)EditStreamInCallback; + + LRESULT lResult = 0; + m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_RTF), (LPARAM)&editStream, &lResult); + return S_OK; +} + +HRESULT CFormattedTextDraw::putRTFTextW(WCHAR *newVal) +{ + if (!m_spTextServices) + return S_FALSE; + + m_editCookie.isUnicode = true; + m_editCookie.unicode = newVal; + m_editCookie.cbSize = mir_wstrlen(m_editCookie.unicode); + m_editCookie.cbCount = 0; + + EDITSTREAM editStream; + editStream.dwCookie = (DWORD_PTR)&m_editCookie; + editStream.dwError = 0; + editStream.pfnCallback = (EDITSTREAMCALLBACK)EditStreamInCallback; + + LRESULT lResult = 0; + m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_RTF | SF_UNICODE), (LPARAM)&editStream, &lResult); + return S_OK; + +} + +HRESULT CFormattedTextDraw::putTextA(char *newVal) +{ + if (!m_spTextServices) + return S_FALSE; + + m_editCookie.isUnicode = false; + m_editCookie.ansi = newVal; + m_editCookie.cbSize = mir_strlen(m_editCookie.ansi); + m_editCookie.cbCount = 0; + + EDITSTREAM editStream; + editStream.dwCookie = (DWORD_PTR)&m_editCookie; + editStream.dwError = 0; + editStream.pfnCallback = (EDITSTREAMCALLBACK)EditStreamInCallback; + + LRESULT lResult = 0; + m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_TEXT), (LPARAM)&editStream, &lResult); + + CHARFORMAT cf; + cf.cbSize = sizeof(cf); + cf.dwMask = CFM_FACE | CFM_BOLD; + cf.dwEffects = 0; + wcsncpy_s(cf.szFaceName, L"MS Shell Dlg", _TRUNCATE); + m_spTextServices->TxSendMessage(EM_SETCHARFORMAT, (WPARAM)(SCF_ALL), (LPARAM)&cf, &lResult); + + return S_OK; +} + +HRESULT CFormattedTextDraw::putTextW(WCHAR *newVal) +{ + if (!m_spTextServices) + return S_FALSE; + + m_editCookie.isUnicode = true; + m_editCookie.unicode = newVal; + m_editCookie.cbSize = mir_wstrlen(m_editCookie.unicode); + m_editCookie.cbCount = 0; + + EDITSTREAM editStream; + editStream.dwCookie = (DWORD_PTR)&m_editCookie; + editStream.dwError = 0; + editStream.pfnCallback = (EDITSTREAMCALLBACK)EditStreamInCallback; + + LRESULT lResult = 0; + m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_TEXT | SF_UNICODE), (LPARAM)&editStream, &lResult); + + CHARFORMAT cf; + cf.cbSize = sizeof(cf); + cf.dwMask = CFM_FACE | CFM_BOLD; + cf.dwEffects = 0; + wcsncpy_s(cf.szFaceName, L"MS Shell Dlg", _TRUNCATE); + m_spTextServices->TxSendMessage(EM_SETCHARFORMAT, (WPARAM)(SCF_ALL), (LPARAM)&cf, &lResult); + return S_OK; +} + +HRESULT CFormattedTextDraw::Draw(void *hdcDraw, RECT *prc) +{ + LOGFONT lf; + GetObject(GetCurrentObject((HDC)hdcDraw, OBJ_FONT), sizeof(lf), &lf); + + LRESULT lResult; + CHARFORMAT cf; + cf.cbSize = sizeof(cf); + cf.dwMask = CFM_FACE/*|CFM_COLOR*/ | CFM_CHARSET | CFM_SIZE | + (lf.lfWeight >= FW_BOLD ? CFM_BOLD : 0) | + (lf.lfItalic ? CFM_ITALIC : 0) | + (lf.lfUnderline ? CFM_UNDERLINE : 0) | + (lf.lfStrikeOut ? CFM_STRIKEOUT : 0); + cf.dwEffects = CFE_BOLD | CFE_ITALIC | CFE_STRIKEOUT | CFE_UNDERLINE; + cf.crTextColor = GetTextColor((HDC)hdcDraw); + cf.bCharSet = lf.lfCharSet; + cf.yHeight = 1440 * abs(lf.lfHeight) / GetDeviceCaps((HDC)hdcDraw, LOGPIXELSY); + wcsncpy_s(cf.szFaceName, lf.lfFaceName, _TRUNCATE); + m_spTextServices->TxSendMessage(EM_SETCHARFORMAT, (WPARAM)(SCF_ALL), (LPARAM)&cf, &lResult); + + m_spTextServices->TxDraw( + DVASPECT_CONTENT, // Draw Aspect + 0, // Lindex + nullptr, // Info for drawing optimization + nullptr, // target device information + (HDC)hdcDraw, // Draw device HDC + nullptr, // Target device HDC + (RECTL *)prc, // Bounding client rectangle + nullptr, // Clipping rectangle for metafiles + (RECT *)nullptr, // Update rectangle + nullptr, // Call back function + NULL, // Call back parameter + TXTVIEW_INACTIVE); // What view of the object could be TXTVIEW_ACTIVE + return S_OK; +} + +HRESULT CFormattedTextDraw::Create() +{ + return CreateTextServicesObject(); +} + +HRESULT CFormattedTextDraw::get_NaturalSize(void *hdcDraw, long *Width, long *Height) +{ + LOGFONT lf; + GetObject(GetCurrentObject((HDC)hdcDraw, OBJ_FONT), sizeof(lf), &lf); + + LRESULT lResult; + CHARFORMAT cf; + cf.cbSize = sizeof(cf); + cf.dwMask = CFM_FACE/*|CFM_COLOR*/ | CFM_CHARSET | CFM_SIZE | + (lf.lfWeight >= FW_BOLD ? CFM_BOLD : 0) | + (lf.lfItalic ? CFM_ITALIC : 0) | + (lf.lfUnderline ? CFM_UNDERLINE : 0) | + (lf.lfStrikeOut ? CFM_STRIKEOUT : 0); + cf.dwEffects = CFE_BOLD | CFE_ITALIC | CFE_STRIKEOUT | CFE_UNDERLINE; + cf.crTextColor = GetTextColor((HDC)hdcDraw); + cf.bCharSet = lf.lfCharSet; + cf.yHeight = 1440 * abs(lf.lfHeight) / GetDeviceCaps((HDC)hdcDraw, LOGPIXELSY); + wcsncpy_s(cf.szFaceName, lf.lfFaceName, _TRUNCATE); + + if (!m_spTextServices) + return S_FALSE; + + m_spTextServices->TxSendMessage(EM_SETCHARFORMAT, (WPARAM)(SCF_ALL), (LPARAM)&cf, &lResult); + + *Height = 1; + + SIZEL szExtent; + szExtent.cx = *Width; + szExtent.cy = *Height; + if (m_spTextServices->TxGetNaturalSize(DVASPECT_CONTENT, (HDC)hdcDraw, nullptr, nullptr, TXTNS_FITTOCONTENT, &szExtent, Width, Height) != S_OK) + return S_FALSE; + + return S_OK; +} + +///////////////////////////////////////////////////////////////////////////// +// ITextHost functions + +HDC CFormattedTextDraw::TxGetDC() +{ + return nullptr; +} + +INT CFormattedTextDraw::TxReleaseDC(HDC) +{ + return 1; +} + +BOOL CFormattedTextDraw::TxShowScrollBar(INT, BOOL) +{ + return FALSE; +} + +BOOL CFormattedTextDraw::TxEnableScrollBar(INT, INT) +{ + return FALSE; +} + +BOOL CFormattedTextDraw::TxSetScrollRange(INT, LONG, INT, BOOL) +{ + return FALSE; +} + +BOOL CFormattedTextDraw::TxSetScrollPos(INT, INT, BOOL) +{ + return FALSE; +} + +void CFormattedTextDraw::TxInvalidateRect(LPCRECT, BOOL) +{} + +void CFormattedTextDraw::TxViewChange(BOOL) +{} + +BOOL CFormattedTextDraw::TxCreateCaret(HBITMAP, INT, INT) +{ + return FALSE; +} + +BOOL CFormattedTextDraw::TxShowCaret(BOOL) +{ + return FALSE; +} + +BOOL CFormattedTextDraw::TxSetCaretPos(INT, INT) +{ + return FALSE; +} + +BOOL CFormattedTextDraw::TxSetTimer(UINT, UINT) +{ + return FALSE; +} + +void CFormattedTextDraw::TxKillTimer(UINT) +{ +} + +void CFormattedTextDraw::TxScrollWindowEx(INT, INT, LPCRECT, LPCRECT, HRGN, LPRECT, UINT) +{ +} + +void CFormattedTextDraw::TxSetCapture(BOOL) +{ +} + +void CFormattedTextDraw::TxSetFocus() +{ +} + +void CFormattedTextDraw::TxSetCursor(HCURSOR hcur, BOOL fText) +{ + if (fText) + SetCursor(LoadCursor(nullptr, MAKEINTRESOURCE(IDC_ARROW))); + else + SetCursor(hcur); +} + +BOOL CFormattedTextDraw::TxScreenToClient(LPPOINT lppt) +{ + if (!m_hwndParent) return FALSE; + return ScreenToClient(m_hwndParent, lppt); +} + +BOOL CFormattedTextDraw::TxClientToScreen(LPPOINT lppt) +{ + if (!m_hwndParent) return FALSE; + // BOOL result = ; + // lppt->x -= m_rcClient.left; + // lppt->y -= m_rcClient.left; + return ClientToScreen(m_hwndParent, lppt); +} + +HRESULT CFormattedTextDraw::TxActivate(LONG *) +{ + return S_OK; +} + +HRESULT CFormattedTextDraw::TxDeactivate(LONG) +{ + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetClientRect(LPRECT prc) +{ + *prc = m_rcClient; + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetViewInset(LPRECT prc) +{ + *prc = m_rcViewInset; + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetCharFormat(const CHARFORMATW **ppCF) +{ + *ppCF = m_pCF; + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetParaFormat(const PARAFORMAT **ppPF) +{ + *ppPF = &m_PF; + return S_OK; +} + +COLORREF CFormattedTextDraw::TxGetSysColor(int nIndex) +{ + return GetSysColor(nIndex); +} + +HRESULT CFormattedTextDraw::TxGetBackStyle(TXTBACKSTYLE *pstyle) +{ + *pstyle = TXTBACK_TRANSPARENT; + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetMaxLength(DWORD *plength) +{ + *plength = m_dwMaxLength; + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetScrollBars(DWORD *pdwScrollBar) +{ + *pdwScrollBar = m_dwScrollbar; + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetPasswordChar(wchar_t *) +{ + return S_FALSE; +} + +HRESULT CFormattedTextDraw::TxGetAcceleratorPos(LONG *pcp) +{ + *pcp = -1; + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetExtent(LPSIZEL) +{ + return E_NOTIMPL; +} + +HRESULT CFormattedTextDraw::OnTxCharFormatChange(const CHARFORMATW * pcf) +{ + memcpy(m_pCF, pcf, pcf->cbSize); + return S_OK; +} + +HRESULT CFormattedTextDraw::OnTxParaFormatChange(const PARAFORMAT * ppf) +{ + memcpy(&m_PF, ppf, ppf->cbSize); + return S_OK; +} + +HRESULT CFormattedTextDraw::TxGetPropertyBits(DWORD, DWORD *pdwBits) +{ + *pdwBits = m_dwPropertyBits; + return S_OK; +} + +HRESULT CFormattedTextDraw::TxNotify(DWORD, void *) +{ + return S_OK; +} + +HIMC CFormattedTextDraw::TxImmGetContext() +{ + return nullptr; +} + +void CFormattedTextDraw::TxImmReleaseContext(HIMC) +{} + +HRESULT CFormattedTextDraw::TxGetSelectionBarWidth(LONG *lSelBarWidth) +{ + *lSelBarWidth = 100; + return S_OK; +} + +///////////////////////////////////////////////////////////////////////////// +// custom functions + +HRESULT CFormattedTextDraw::CharFormatFromHFONT(CHARFORMAT2W* pCF, HFONT hFont) +// Takes an HFONT and fills in a CHARFORMAT2W structure with the corresponding info +{ + // Get LOGFONT for default font + if (!hFont) + hFont = (HFONT)GetStockObject(SYSTEM_FONT); + + // Get LOGFONT for passed hfont + LOGFONT lf; + if (!GetObject(hFont, sizeof(LOGFONT), &lf)) + return E_FAIL; + + // Set CHARFORMAT structure + memset(pCF, 0, sizeof(CHARFORMAT2W)); + pCF->cbSize = sizeof(CHARFORMAT2W); + + HWND hWnd = GetDesktopWindow(); + HDC hDC = GetDC(hWnd); + LONG yPixPerInch = GetDeviceCaps(hDC, LOGPIXELSY); + pCF->yHeight = -lf.lfHeight * LY_PER_INCH / yPixPerInch; + ReleaseDC(hWnd, hDC); + + pCF->yOffset = 0; + pCF->crTextColor = 0; + + pCF->dwEffects = CFM_EFFECTS | CFE_AUTOBACKCOLOR; + pCF->dwEffects &= ~(CFE_PROTECTED | CFE_LINK | CFE_AUTOCOLOR); + + if (lf.lfWeight < FW_BOLD) + pCF->dwEffects &= ~CFE_BOLD; + + if (!lf.lfItalic) + pCF->dwEffects &= ~CFE_ITALIC; + + if (!lf.lfUnderline) + pCF->dwEffects &= ~CFE_UNDERLINE; + + if (!lf.lfStrikeOut) + pCF->dwEffects &= ~CFE_STRIKEOUT; + + pCF->dwMask = CFM_ALL | CFM_BACKCOLOR | CFM_STYLE; + pCF->bCharSet = lf.lfCharSet; + pCF->bPitchAndFamily = lf.lfPitchAndFamily; + + mir_wstrcpy(pCF->szFaceName, lf.lfFaceName); + return S_OK; +} + +HRESULT CFormattedTextDraw::InitDefaultCharFormat() +{ + return CharFormatFromHFONT(m_pCF, nullptr); +} + +HRESULT CFormattedTextDraw::InitDefaultParaFormat() +{ + memset(&m_PF, 0, sizeof(PARAFORMAT2)); + m_PF.cbSize = sizeof(PARAFORMAT2); + m_PF.dwMask = PFM_ALL; + m_PF.wAlignment = PFA_LEFT; + m_PF.cTabCount = 1; + m_PF.rgxTabs[0] = lDefaultTab; + return S_OK; +} + +HRESULT CFormattedTextDraw::CreateTextServicesObject() +{ + IUnknown *spUnk; + HRESULT hr = MyCreateTextServices(nullptr, static_cast<ITextHost*>(this), &spUnk); + if (hr == S_OK) { + hr = spUnk->QueryInterface(IID_ITextServices, (void**)&m_spTextServices); + hr = spUnk->QueryInterface(IID_ITextDocument, (void**)&m_spTextDocument); + spUnk->Release(); + } + return hr; +} diff --git a/libs/mTextControl/src/FormattedTextDraw.h b/libs/mTextControl/src/FormattedTextDraw.h new file mode 100644 index 0000000000..a5128f48a2 --- /dev/null +++ b/libs/mTextControl/src/FormattedTextDraw.h @@ -0,0 +1,191 @@ +// Feel free to use this code in your own applications. +// The Author does not guarantee anything about this code. +// Author : Yves Maurer +// FormattedTextDraw.h : Declaration of the CFormattedTextDraw + +#ifndef __FORMATTEDTEXTDRAW_H_ +#define __FORMATTEDTEXTDRAW_H_ + +#ifndef LY_PER_INCH +#define LY_PER_INCH 1440 +#define HOST_BORDER 0 +#endif + +struct COOKIE +{ + bool isUnicode; + union + { + char *ansi; + WCHAR *unicode; + }; + size_t cbSize, cbCount; +}; + +///////////////////////////////////////////////////////////////////////////// +// IFormatttedTextDraw +interface IFormattedTextDraw +{ +public: + virtual ~IFormattedTextDraw() {}; + virtual HRESULT get_NaturalSize(void *hdcDraw, long *Width, long *pVal) = 0; + virtual HRESULT Create() = 0; + virtual HRESULT Draw(void *hdcDraw, RECT *prc) = 0; + virtual HRESULT putRTFTextA(char *newVal) = 0; + virtual HRESULT putRTFTextW(WCHAR *newVal) = 0; + virtual HRESULT putTextA(char *newVal) = 0; + virtual HRESULT putTextW(WCHAR *newVal) = 0; + + virtual ITextServices *getTextService() = 0; + virtual ITextDocument *getTextDocument() = 0; + virtual void setParentWnd(HWND hwnd, RECT rect) = 0; + + // COM-like functions + virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0; + virtual ULONG STDMETHODCALLTYPE Release(void) = 0; +}; + + +///////////////////////////////////////////////////////////////////////////// +// CFormattedTextDraw +class CFormattedTextDraw : + public ITextHost, + public IFormattedTextDraw +{ +public: + CFormattedTextDraw() + { + HDC hdcScreen; + + hdcScreen = GetDC(nullptr); + nPixelsPerInchX = GetDeviceCaps(hdcScreen, LOGPIXELSX); + nPixelsPerInchY = GetDeviceCaps(hdcScreen, LOGPIXELSY); + ReleaseDC(nullptr, hdcScreen); + + SetRectEmpty(&m_rcClient); + SetRectEmpty(&m_rcViewInset); + + m_pCF = (CHARFORMAT2W*)malloc(sizeof(CHARFORMAT2W)); + + InitDefaultCharFormat(); + InitDefaultParaFormat(); + m_spTextServices = nullptr; + m_spTextDocument = nullptr; + + m_dwPropertyBits = TXTBIT_RICHTEXT | TXTBIT_MULTILINE | TXTBIT_WORDWRAP | TXTBIT_USECURRENTBKG; + m_dwScrollbar = 0; + m_dwMaxLength = INFINITE; + } + + ~CFormattedTextDraw() + { + free(m_pCF); + if (m_spTextServices != nullptr) + m_spTextServices->Release(); + if (m_spTextDocument != nullptr) + m_spTextDocument->Release(); + } + + // Minimal COM functionality + HRESULT STDMETHODCALLTYPE QueryInterface( + /* [in] */ REFIID, + /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject) + { + *ppvObject = nullptr; + return S_FALSE; + } + + ULONG STDMETHODCALLTYPE AddRef(void) + { + return 0; + } + + ULONG STDMETHODCALLTYPE Release(void) + { + return 0; + } + + // IFormattedTextDraw +public: + HRESULT get_NaturalSize(void *hdcDraw, long *Width, long *pVal); + HRESULT Create(); + HRESULT Draw(void *hdcDraw, RECT *prc); + HRESULT putRTFTextA(char *newVal); + HRESULT putRTFTextW(WCHAR *newVal); + HRESULT putTextA(char *newVal); + HRESULT putTextW(WCHAR *newVal); + + ITextServices *getTextService() { return m_spTextServices; }; + ITextDocument *getTextDocument() { return m_spTextDocument; }; + virtual void setParentWnd(HWND hwnd, RECT rect) { m_hwndParent = hwnd; m_rcClient = rect; } + + // ITextHost + HDC TxGetDC(); + INT TxReleaseDC(HDC hdc); + BOOL TxShowScrollBar(INT fnBar, BOOL fShow); + BOOL TxEnableScrollBar(INT fuSBFlags, INT fuArrowflags); + BOOL TxSetScrollRange(INT fnBar, LONG nMinPos, INT nMaxPos, BOOL fRedraw); + BOOL TxSetScrollPos(INT fnBar, INT nPos, BOOL fRedraw); + void TxInvalidateRect(LPCRECT prc, BOOL fMode); + void TxViewChange(BOOL fUpdate); + BOOL TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight); + BOOL TxShowCaret(BOOL fShow); + BOOL TxSetCaretPos(INT x, INT y); + BOOL TxSetTimer(UINT idTimer, UINT uTimeout); + void TxKillTimer(UINT idTimer); + void TxScrollWindowEx(INT dx, INT dy, LPCRECT lprcScroll, LPCRECT lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate, UINT fuScroll); + void TxSetCapture(BOOL fCapture); + void TxSetFocus(); + void TxSetCursor(HCURSOR hcur, BOOL fText); + BOOL TxScreenToClient(LPPOINT lppt); + BOOL TxClientToScreen(LPPOINT lppt); + HRESULT TxActivate(LONG * plOldState); + HRESULT TxDeactivate(LONG lNewState); + HRESULT TxGetClientRect(LPRECT prc); + HRESULT TxGetViewInset(LPRECT prc); + HRESULT TxGetCharFormat(const CHARFORMATW **ppCF); + HRESULT TxGetParaFormat(const PARAFORMAT **ppPF); + COLORREF TxGetSysColor(int nIndex); + HRESULT TxGetBackStyle(TXTBACKSTYLE *pstyle); + HRESULT TxGetMaxLength(DWORD *plength); + HRESULT TxGetScrollBars(DWORD *pdwScrollBar); + HRESULT TxGetPasswordChar(wchar_t *pch); + HRESULT TxGetAcceleratorPos(LONG *pcp); + HRESULT TxGetExtent(LPSIZEL lpExtent); + HRESULT OnTxCharFormatChange(const CHARFORMATW * pcf); + HRESULT OnTxParaFormatChange(const PARAFORMAT * ppf); + HRESULT TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits); + HRESULT TxNotify(DWORD iNotify, void *pv); + HIMC TxImmGetContext(); + void TxImmReleaseContext(HIMC himc); + HRESULT TxGetSelectionBarWidth(LONG *lSelBarWidth); + + // Custom functions + HRESULT CharFormatFromHFONT(CHARFORMAT2W* pCF, HFONT hFont); + HRESULT InitDefaultCharFormat(); + HRESULT InitDefaultParaFormat(); + HRESULT CreateTextServicesObject(); + + // Variables + HWND m_hwndParent; + RECT m_rcClient; // Client Rect + RECT m_rcViewInset; // view rect inset + SIZEL m_sizelExtent; // Extent array + + int nPixelsPerInchX; // Pixels per logical inch along width + int nPixelsPerInchY; // Pixels per logical inch along height + + CHARFORMAT2W *m_pCF; + PARAFORMAT2 m_PF; + DWORD m_dwScrollbar; // Scroll bar style + DWORD m_dwPropertyBits; // Property bits + DWORD m_dwMaxLength; + COOKIE m_editCookie; + + ITextServices *m_spTextServices; + ITextDocument *m_spTextDocument; +}; + +void bbCodeParse(IFormattedTextDraw *ts); + +#endif //__FORMATTEDTEXTDRAW_H_ diff --git a/libs/mTextControl/src/ImageDataObjectHlp.cpp b/libs/mTextControl/src/ImageDataObjectHlp.cpp new file mode 100644 index 0000000000..358ce0b1ef --- /dev/null +++ b/libs/mTextControl/src/ImageDataObjectHlp.cpp @@ -0,0 +1,209 @@ +/* +Miranda SmileyAdd Plugin +Copyright (C) 2004-2005 Rein-Peter de Boer (peacow) and followers + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +//imagedataobject +//code taken partly from public example on the internet, source unknown. + +#include "stdafx.h" +#include "ImageDataObjectHlp.h" + +struct EMFCACHE +{ + HENHMETAFILE hEmf; + HICON hIcon; + EMFCACHE *prev; + EMFCACHE *next; +} *emfCache = nullptr; +int emfCacheSize = 0; +mir_cs csEmfCache; + +void UnloadEmfCache() +{ + while (emfCache) + { + EMFCACHE *tmp = emfCache->next; + delete emfCache; + emfCache = tmp; + } +} + +HENHMETAFILE CacheIconToEmf(HICON hIcon) +{ + HENHMETAFILE result = nullptr; + mir_cslock lck(csEmfCache); + for (EMFCACHE *p = emfCache; p; p = p->next) + if (p->hIcon == hIcon) + { + if (p->prev) + { + p->prev->next = p->next; + if (p->next) p->next->prev = p->prev; + p->prev = nullptr; + emfCache->prev = p; + p->next = emfCache; + emfCache = p; + result = CopyEnhMetaFile(emfCache->hEmf, nullptr); + break; + } + } + + // cache new item + if (!result) + { + EMFCACHE *newItem = new EMFCACHE; + newItem->prev = nullptr; + newItem->next = emfCache; + if (emfCache) emfCache->prev = newItem; + emfCache = newItem; + emfCacheSize++; + + HDC emfdc = CreateEnhMetaFile(nullptr, nullptr, nullptr, L"icon"); + DrawIconEx(emfdc, 0, 0, (HICON)hIcon, 16, 16, 0, nullptr, DI_NORMAL); + emfCache->hIcon = hIcon; + emfCache->hEmf = CloseEnhMetaFile(emfdc); + result = CopyEnhMetaFile(emfCache->hEmf, nullptr); + } + + // tail cutoff + if (emfCacheSize > 20) + { + int n = 0; + EMFCACHE *p; + for (p = emfCache; p; p = p->next) + if (++n > 20) + break; + while (p->next) + { + EMFCACHE *tmp = p->next; + p->next = p->next->next; + delete tmp; + } + if (p->next) p->next->prev = p; + emfCacheSize = 20; + } + + return result; +} + +HRESULT CreateDataObject(const FORMATETC *fmtetc, const STGMEDIUM *stgmed, UINT count, IDataObject **ppDataObject); + +// returns true on success, false on failure +//bool InsertBitmap(IRichEditOle* pRichEditOle, HBITMAP hBitmap, HGLOBAL hGlobal) +bool InsertBitmap(IRichEditOle* pRichEditOle, HENHMETAFILE hEmf) +{ + SCODE sc; + + // Get the image data object + // + static const FORMATETC lc_format[] = + { + { CF_ENHMETAFILE, nullptr, DVASPECT_CONTENT, -1, TYMED_ENHMF }//, + // { CF_BITMAP, 0, DVASPECT_CONTENT, -1, TYMED_GDI }, + // { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL } + }; + + STGMEDIUM lc_stgmed[] = + { + { TYMED_ENHMF, { (HBITMAP)hEmf }, nullptr }//, + // { TYMED_GDI, { hBitmap }, 0 }, + // { TYMED_HGLOBAL, { (HBITMAP)hGlobal }, 0 } + }; + + IDataObject *pods; + CreateDataObject(lc_format, lc_stgmed, 1, &pods); + + // Get the RichEdit container site + // + IOleClientSite *pOleClientSite; + pRichEditOle->GetClientSite(&pOleClientSite); + + // Initialize a Storage Object + // + LPLOCKBYTES lpLockBytes = nullptr; + sc = CreateILockBytesOnHGlobal(nullptr, TRUE, &lpLockBytes); + if (sc != S_OK) + { + pOleClientSite->Release(); + return false; + } + + IStorage *pStorage; + sc = StgCreateDocfileOnILockBytes(lpLockBytes, + STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, &pStorage); + if (sc != S_OK) + { + lpLockBytes->Release(); + pOleClientSite->Release(); + pods->Release(); + return false; + } + + // The final ole object which will be inserted in the richedit control + // + IOleObject *pOleObject; + sc = OleCreateStaticFromData(pods, IID_IOleObject, OLERENDER_FORMAT, + (LPFORMATETC)lc_format, pOleClientSite, pStorage, (void **)&pOleObject); + if (sc != S_OK) + { + pStorage->Release(); + lpLockBytes->Release(); + pOleClientSite->Release(); + return false; + } + + // all items are "contained" -- this makes our reference to this object + // weak -- which is needed for links to embedding silent update. + OleSetContainedObject(pOleObject, TRUE); + + // Now Add the object to the RichEdit + // + REOBJECT reobject = { 0 }; + + reobject.cbStruct = sizeof(REOBJECT); + reobject.cp = REO_CP_SELECTION; + reobject.dvaspect = DVASPECT_CONTENT; + reobject.poleobj = pOleObject; + reobject.polesite = pOleClientSite; + reobject.pstg = pStorage; + reobject.dwFlags = REO_BELOWBASELINE; + + sc = pOleObject->GetUserClassID(&reobject.clsid); + if (sc != S_OK) + { + pOleObject->Release(); + pStorage->Release(); + lpLockBytes->Release(); + pOleClientSite->Release(); + return false; + } + + // Insert the bitmap at the current location in the richedit control + // + sc = pRichEditOle->InsertObject(&reobject); + + // Release all unnecessary interfaces + // + pOleObject->Release(); + pStorage->Release(); + lpLockBytes->Release(); + pOleClientSite->Release(); + pods->Release(); + + return sc == S_OK; +} diff --git a/libs/mTextControl/src/ImageDataObjectHlp.h b/libs/mTextControl/src/ImageDataObjectHlp.h new file mode 100644 index 0000000000..fdc395aa48 --- /dev/null +++ b/libs/mTextControl/src/ImageDataObjectHlp.h @@ -0,0 +1,28 @@ +/* +Miranda SmileyAdd Plugin +Copyright (C) 2004 Rein-Peter de Boer (peacow) + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef _IMAGEDATAOBJECTHLP_ +#define _IMAGEDATAOBJECTHLP_ + +void UnloadEmfCache(); +HENHMETAFILE CacheIconToEmf(HICON hIcon); + +bool InsertBitmap(IRichEditOle* pRichEditOle, HENHMETAFILE hEmf); + +#endif diff --git a/libs/mTextControl/src/dataobject.cpp b/libs/mTextControl/src/dataobject.cpp new file mode 100644 index 0000000000..1d14d28afb --- /dev/null +++ b/libs/mTextControl/src/dataobject.cpp @@ -0,0 +1,276 @@ +// +// DATAOBJECT.CPP +// +// Implementation of the IDataObject COM interface +// +// By J Brown 2004 +// +// www.catch22.net +// + +//#define STRICT + +#include "stdafx.h" + +const ULONG MAX_FORMATS = 100; + +HRESULT CreateEnumFormatEtc(UINT nNumFormats, FORMATETC *pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc); + +class CDataObject : public IDataObject +{ +public: + // + // IUnknown members + // + HRESULT __stdcall QueryInterface(REFIID iid, void ** ppvObject); + ULONG __stdcall AddRef(void); + ULONG __stdcall Release(void); + + // + // IDataObject members + // + HRESULT __stdcall GetData(FORMATETC *pFormatEtc, STGMEDIUM *pMedium); + HRESULT __stdcall GetDataHere(FORMATETC *pFormatEtc, STGMEDIUM *pMedium); + HRESULT __stdcall QueryGetData(FORMATETC *pFormatEtc); + HRESULT __stdcall GetCanonicalFormatEtc(FORMATETC *pFormatEct, FORMATETC *pFormatEtcOut); + HRESULT __stdcall SetData(FORMATETC *pFormatEtc, STGMEDIUM *pMedium, BOOL fRelease); + HRESULT __stdcall EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC **ppEnumFormatEtc); + HRESULT __stdcall DAdvise(FORMATETC *pFormatEtc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection); + HRESULT __stdcall DUnadvise(DWORD dwConnection); + HRESULT __stdcall EnumDAdvise(IEnumSTATDATA **ppEnumAdvise); + + // + // Constructor / Destructor + // + CDataObject(const FORMATETC *fmt, const STGMEDIUM *stgmed, int count); + ~CDataObject(); + +private: + + int LookupFormatEtc(FORMATETC *pFormatEtc); + + // + // any private members and functions + // + LONG m_lRefCount; + + FORMATETC *m_pFormatEtc; + STGMEDIUM *m_pStgMedium; + LONG m_nNumFormats; + +}; + +// +// Constructor +// +CDataObject::CDataObject(const FORMATETC *fmtetc, const STGMEDIUM *stgmed, int count) +{ + m_lRefCount = 1; + m_nNumFormats = count; + + m_pFormatEtc = new FORMATETC[count]; + m_pStgMedium = new STGMEDIUM[count]; + + for (int i = 0; i < count; i++) { + m_pFormatEtc[i] = fmtetc[i]; + m_pStgMedium[i] = stgmed[i]; + } +} + +// +// Destructor +// +CDataObject::~CDataObject() +{ + // cleanup + for (int i = 0; i < m_nNumFormats; i++) { + // ReleaseStgMedium(&m_pStgMedium[i]); + } + + if (m_pFormatEtc) delete[] m_pFormatEtc; + if (m_pStgMedium) delete[] m_pStgMedium; +} + +// +// IUnknown::AddRef +// +ULONG __stdcall CDataObject::AddRef(void) +{ + // increment object reference count + return InterlockedIncrement(&m_lRefCount); +} + +// +// IUnknown::Release +// +ULONG __stdcall CDataObject::Release(void) +{ + // decrement object reference count + LONG count = InterlockedDecrement(&m_lRefCount); + + if (count == 0) { + delete this; + return 0; + } + + return count; +} + +// +// IUnknown::QueryInterface +// +HRESULT __stdcall CDataObject::QueryInterface(REFIID iid, void **ppvObject) +{ + // check to see what interface has been requested + if (iid == IID_IDataObject || iid == IID_IUnknown) { + AddRef(); + *ppvObject = this; + return S_OK; + } + + *ppvObject = nullptr; + return E_NOINTERFACE; +} + +int CDataObject::LookupFormatEtc(FORMATETC *pFormatEtc) +{ + for (int i = 0; i < m_nNumFormats; i++) { + if ((pFormatEtc->tymed & m_pFormatEtc[i].tymed) && + pFormatEtc->cfFormat == m_pFormatEtc[i].cfFormat && + pFormatEtc->dwAspect == m_pFormatEtc[i].dwAspect) { + return i; + } + } + + return -1; +} + +// +// IDataObject::GetData +// +HRESULT __stdcall CDataObject::GetData(FORMATETC *pFormatEtc, STGMEDIUM *pMedium) +{ + int idx; + + // + // try to match the requested FORMATETC with one of our supported formats + // + if ((idx = LookupFormatEtc(pFormatEtc)) == -1) { + return DV_E_FORMATETC; + } + + // + // found a match! transfer the data into the supplied storage-medium + // + pMedium->tymed = m_pFormatEtc[idx].tymed; + pMedium->pUnkForRelease = nullptr; + + switch (pMedium->tymed) { + case TYMED_HGLOBAL: + case TYMED_GDI: + case TYMED_ENHMF: + // pMedium->hBitmap = (HBITMAP)OleDuplicateData(m_pStgMedium[idx].hBitmap, pFormatEtc->cfFormat, 0); + pMedium->hBitmap = m_pStgMedium[idx].hBitmap; + break; + + default: + return DV_E_FORMATETC; + } + if (pMedium->hBitmap == nullptr) return STG_E_MEDIUMFULL; + + return S_OK; +} + +// +// IDataObject::GetDataHere +// +HRESULT __stdcall CDataObject::GetDataHere(FORMATETC *, STGMEDIUM *) +{ + // GetDataHere is only required for IStream and IStorage mediums + // It is an error to call GetDataHere for things like HGLOBAL and other clipboard formats + // + // OleFlushClipboard + // + return DATA_E_FORMATETC; +} + +// +// IDataObject::QueryGetData +// +// Called to see if the IDataObject supports the specified format of data +// +HRESULT __stdcall CDataObject::QueryGetData(FORMATETC *pFormatEtc) +{ + return (LookupFormatEtc(pFormatEtc) == -1) ? DV_E_FORMATETC : S_OK; +} + +// +// IDataObject::GetCanonicalFormatEtc +// +HRESULT __stdcall CDataObject::GetCanonicalFormatEtc(FORMATETC *, FORMATETC *pFormatEtcOut) +{ + // Apparently we have to set this field to NULL even though we don't do anything else + pFormatEtcOut->ptd = nullptr; + return E_NOTIMPL; +} + +// +// IDataObject::SetData +// +HRESULT __stdcall CDataObject::SetData(FORMATETC *, STGMEDIUM *, BOOL) +{ + return E_NOTIMPL; +} + +// +// IDataObject::EnumFormatEtc +// +HRESULT __stdcall CDataObject::EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC **ppEnumFormatEtc) +{ + if (dwDirection == DATADIR_GET) { + // for Win2k+ you can use the SHCreateStdEnumFmtEtc API call, however + // to support all Windows platforms we need to implement IEnumFormatEtc ourselves. + return CreateEnumFormatEtc(m_nNumFormats, m_pFormatEtc, ppEnumFormatEtc); + } + else { + // the direction specified is not support for drag+drop + return E_NOTIMPL; + } +} + +// +// IDataObject::DAdvise +// +HRESULT __stdcall CDataObject::DAdvise(FORMATETC *, DWORD, IAdviseSink *, DWORD *) +{ + return OLE_E_ADVISENOTSUPPORTED; +} + +// +// IDataObject::DUnadvise +// +HRESULT __stdcall CDataObject::DUnadvise(DWORD) +{ + return OLE_E_ADVISENOTSUPPORTED; +} + +// +// IDataObject::EnumDAdvise +// +HRESULT __stdcall CDataObject::EnumDAdvise(IEnumSTATDATA **) +{ + return OLE_E_ADVISENOTSUPPORTED; +} + +// +// Helper function +// +HRESULT CreateDataObject(const FORMATETC *fmtetc, const STGMEDIUM *stgmeds, UINT count, IDataObject **ppDataObject) +{ + if (ppDataObject == nullptr) + return E_INVALIDARG; + + *ppDataObject = new CDataObject(fmtetc, stgmeds, count); + + return (*ppDataObject) ? S_OK : E_OUTOFMEMORY; +} diff --git a/libs/mTextControl/src/enumformat.cpp b/libs/mTextControl/src/enumformat.cpp new file mode 100644 index 0000000000..8f3e9caee1 --- /dev/null +++ b/libs/mTextControl/src/enumformat.cpp @@ -0,0 +1,220 @@ +// +// ENUMFORMAT.CPP +// +// By J Brown 2004 +// +// www.catch22.net +// +// Implementation of the IEnumFORMATETC interface +// +// For Win2K and above look at the SHCreateStdEnumFmtEtc API call!! +// +// Apparently the order of formats in an IEnumFORMATETC object must be +// the same as those that were stored in the clipboard +// +// + +//#define STRICT + +#include "stdafx.h" + +class CEnumFormatEtc : public IEnumFORMATETC +{ +public: + // + // IUnknown members + // + HRESULT __stdcall QueryInterface(REFIID iid, void ** ppvObject); + ULONG __stdcall AddRef(void); + ULONG __stdcall Release(void); + + // + // IEnumFormatEtc members + // + HRESULT __stdcall Next(ULONG celt, FORMATETC * rgelt, ULONG * pceltFetched); + HRESULT __stdcall Skip(ULONG celt); + HRESULT __stdcall Reset(void); + HRESULT __stdcall Clone(IEnumFORMATETC ** ppEnumFormatEtc); + + // + // Construction / Destruction + // + CEnumFormatEtc(FORMATETC *pFormatEtc, int nNumFormats); + ~CEnumFormatEtc(); + +private: + + LONG m_lRefCount; // Reference count for this COM interface + ULONG m_nIndex; // current enumerator index + ULONG m_nNumFormats; // number of FORMATETC members + FORMATETC * m_pFormatEtc; // array of FORMATETC objects +}; + +// +// "Drop-in" replacement for SHCreateStdEnumFmtEtc. Called by CDataObject::EnumFormatEtc +// +HRESULT CreateEnumFormatEtc(UINT nNumFormats, FORMATETC *pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc) +{ + if (nNumFormats == 0 || pFormatEtc == nullptr || ppEnumFormatEtc == nullptr) + return E_INVALIDARG; + + *ppEnumFormatEtc = new CEnumFormatEtc(pFormatEtc, nNumFormats); + + return (*ppEnumFormatEtc) ? S_OK : E_OUTOFMEMORY; +} + +// +// Helper function to perform a "deep" copy of a FORMATETC +// +static void DeepCopyFormatEtc(FORMATETC *dest, FORMATETC *source) +{ + // copy the source FORMATETC into dest + *dest = *source; + + if (source->ptd) { + // allocate memory for the DVTARGETDEVICE if necessary + dest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE)); + + // copy the contents of the source DVTARGETDEVICE into dest->ptd + *(dest->ptd) = *(source->ptd); + } +} + +// +// Constructor +// +CEnumFormatEtc::CEnumFormatEtc(FORMATETC *pFormatEtc, int nNumFormats) +{ + m_lRefCount = 1; + m_nIndex = 0; + m_nNumFormats = nNumFormats; + m_pFormatEtc = new FORMATETC[nNumFormats]; + + // copy the FORMATETC structures + for (int i = 0; i < nNumFormats; i++) { + DeepCopyFormatEtc(&m_pFormatEtc[i], &pFormatEtc[i]); + } +} + +// +// Destructor +// +CEnumFormatEtc::~CEnumFormatEtc() +{ + if (m_pFormatEtc) { + for (ULONG i = 0; i < m_nNumFormats; i++) { + if (m_pFormatEtc[i].ptd) + CoTaskMemFree(m_pFormatEtc[i].ptd); + } + + delete[] m_pFormatEtc; + } +} + +// +// IUnknown::AddRef +// +ULONG __stdcall CEnumFormatEtc::AddRef(void) +{ + // increment object reference count + return InterlockedIncrement(&m_lRefCount); +} + +// +// IUnknown::Release +// +ULONG __stdcall CEnumFormatEtc::Release(void) +{ + // decrement object reference count + LONG count = InterlockedDecrement(&m_lRefCount); + + if (count == 0) { + delete this; + return 0; + } + else { + return count; + } +} + +// +// IUnknown::QueryInterface +// +HRESULT __stdcall CEnumFormatEtc::QueryInterface(REFIID iid, void **ppvObject) +{ + // check to see what interface has been requested + if (iid == IID_IEnumFORMATETC || iid == IID_IUnknown) { + AddRef(); + *ppvObject = this; + return S_OK; + } + else { + *ppvObject = nullptr; + return E_NOINTERFACE; + } +} + +// +// IEnumFORMATETC::Next +// +// If the returned FORMATETC structure contains a non-null "ptd" member, then +// the caller must free this using CoTaskMemFree (stated in the COM documentation) +// +HRESULT __stdcall CEnumFormatEtc::Next(ULONG celt, FORMATETC *pFormatEtc, ULONG * pceltFetched) +{ + ULONG copied = 0; + + // validate arguments + if (celt == 0 || pFormatEtc == nullptr) + return E_INVALIDARG; + + // copy FORMATETC structures into caller's buffer + while (m_nIndex < m_nNumFormats && copied < celt) { + DeepCopyFormatEtc(&pFormatEtc[copied], &m_pFormatEtc[m_nIndex]); + copied++; + m_nIndex++; + } + + // store result + if (pceltFetched != nullptr) + *pceltFetched = copied; + + // did we copy all that was requested? + return (copied == celt) ? S_OK : S_FALSE; +} + +// +// IEnumFORMATETC::Skip +// +HRESULT __stdcall CEnumFormatEtc::Skip(ULONG celt) +{ + m_nIndex += celt; + return (m_nIndex <= m_nNumFormats) ? S_OK : S_FALSE; +} + +// +// IEnumFORMATETC::Reset +// +HRESULT __stdcall CEnumFormatEtc::Reset(void) +{ + m_nIndex = 0; + return S_OK; +} + +// +// IEnumFORMATETC::Clone +// +HRESULT __stdcall CEnumFormatEtc::Clone(IEnumFORMATETC ** ppEnumFormatEtc) +{ + HRESULT hResult; + + // make a duplicate enumerator + hResult = CreateEnumFormatEtc(m_nNumFormats, m_pFormatEtc, ppEnumFormatEtc); + + if (hResult == S_OK) { + // manually set the index state + ((CEnumFormatEtc *)*ppEnumFormatEtc)->m_nIndex = m_nIndex; + } + + return hResult; +} diff --git a/libs/mTextControl/src/fancy_rtf.cpp b/libs/mTextControl/src/fancy_rtf.cpp new file mode 100644 index 0000000000..8746d2fc65 --- /dev/null +++ b/libs/mTextControl/src/fancy_rtf.cpp @@ -0,0 +1,171 @@ +#include "stdafx.h" +#include "ImageDataObjectHlp.h" +#include "FormattedTextDraw.h" + +struct BBCodeInfo +{ + wchar_t *start; + wchar_t *end; + bool(*func)(IFormattedTextDraw *ftd, CHARRANGE range, wchar_t *txt, DWORD cookie); + DWORD cookie; +}; + +enum { + BBS_BOLD_S, BBS_BOLD_E, BBS_ITALIC_S, BBS_ITALIC_E, BBS_UNDERLINE_S, BBS_UNDERLINE_E, + BBS_STRIKEOUT_S, BBS_STRIKEOUT_E, BBS_COLOR_S, BBS_COLOR_E, BBS_URL1, BBS_URL2, + BBS_IMG1, BBS_IMG2 +}; + +static bool bbCodeSimpleFunc(IFormattedTextDraw *ftd, CHARRANGE range, wchar_t *, DWORD cookie) +{ + CHARFORMAT cf = { 0 }; + cf.cbSize = sizeof(cf); + switch (cookie) { + case BBS_BOLD_S: + cf.dwMask = CFM_BOLD; + cf.dwEffects = CFE_BOLD; + break; + case BBS_BOLD_E: + cf.dwMask = CFM_BOLD; + break; + case BBS_ITALIC_S: + cf.dwMask = CFM_ITALIC; + cf.dwEffects = CFE_ITALIC; + break; + case BBS_ITALIC_E: + cf.dwMask = CFM_ITALIC; + break; + case BBS_UNDERLINE_S: + cf.dwMask = CFM_UNDERLINE; + cf.dwEffects = CFE_UNDERLINE; + break; + case BBS_UNDERLINE_E: + cf.dwMask = CFM_UNDERLINE; + break; + case BBS_STRIKEOUT_S: + cf.dwMask = CFM_STRIKEOUT; + cf.dwEffects = CFE_STRIKEOUT; + break; + case BBS_STRIKEOUT_E: + cf.dwMask = CFM_STRIKEOUT; + break; + } + + ITextServices *ts = ftd->getTextService(); + + LRESULT lResult; + ts->TxSendMessage(EM_SETSEL, range.cpMin, -1, &lResult); + ts->TxSendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf, &lResult); + ts->TxSendMessage(EM_SETSEL, range.cpMin, range.cpMax, &lResult); + ts->TxSendMessage(EM_REPLACESEL, FALSE, (LPARAM)L"", &lResult); + + return true; +} + +static bool bbCodeImageFunc(IFormattedTextDraw *ftd, CHARRANGE range, wchar_t *txt, DWORD) +{ + ITextServices *ts = ftd->getTextService(); + ITextDocument *td = ftd->getTextDocument(); + + long cnt; + LRESULT lResult; + ts->TxSendMessage(EM_SETSEL, range.cpMin, range.cpMax, &lResult); + IRichEditOle* RichEditOle; + ts->TxSendMessage(EM_GETOLEINTERFACE, 0, (LPARAM)&RichEditOle, &lResult); + td->Freeze(&cnt); + +#ifdef _WIN64 + bool res = InsertBitmap(RichEditOle, CacheIconToEmf((HICON)_wtoi64(txt))); +#else + bool res = InsertBitmap(RichEditOle, CacheIconToEmf((HICON)_wtoi(txt))); +#endif + + td->Unfreeze(&cnt); + RichEditOle->Release(); + return res; +} + +static BBCodeInfo bbCodes[] = +{ + { L"[b]", nullptr, bbCodeSimpleFunc, BBS_BOLD_S }, + { L"[/b]", nullptr, bbCodeSimpleFunc, BBS_BOLD_E }, + { L"[i]", nullptr, bbCodeSimpleFunc, BBS_ITALIC_S }, + { L"[/i]", nullptr, bbCodeSimpleFunc, BBS_ITALIC_E }, + { L"[u]", nullptr, bbCodeSimpleFunc, BBS_UNDERLINE_S }, + { L"[/u]", nullptr, bbCodeSimpleFunc, BBS_UNDERLINE_E }, + { L"[s]", nullptr, bbCodeSimpleFunc, BBS_STRIKEOUT_S }, + { L"[/s]", nullptr, bbCodeSimpleFunc, BBS_STRIKEOUT_E }, + + // { L"[color=", L"]", bbCodeSimpleFunc, BBS_COLOR_S }, + // { L"[/color]", 0, bbCodeSimpleFunc, BBS_COLOR_E } + + { L"[$hicon=", L"$]", bbCodeImageFunc, 0 } + + // { L"[url]", L"[/url]", bbCodeSimpleFunc, BBS_URL1 }, + // { L"[url=", L"]", bbCodeSimpleFunc, BBS_URL2 }, + // { L"[url]", L"[/url]", bbCodeSimpleFunc, BBS_IMG1 }, + // { L"[url=", L"]", bbCodeSimpleFunc, BBS_IMG2 }, +}; +static int bbCodeCount = sizeof(bbCodes) / sizeof(*bbCodes); + +void bbCodeParse(IFormattedTextDraw *ftd) +{ + ITextServices *ts = ftd->getTextService(); + LRESULT lResult; + + int pos = 0; + for (bool found = true; found;) { + found = false; + CHARRANGE fRange; fRange.cpMin = -1; + wchar_t *fText = nullptr; + BBCodeInfo *fBBCode = nullptr; + + for (int i = 0; i < bbCodeCount; i++) { + CHARRANGE range; + + FINDTEXTEX fte; + fte.chrg.cpMin = pos; + fte.chrg.cpMax = -1; + + fte.lpstrText = bbCodes[i].start; + ts->TxSendMessage(EM_FINDTEXTEX, (WPARAM)FR_DOWN, (LPARAM)&fte, &lResult); + if (lResult == -1) + continue; + range = fte.chrgText; + + if (bbCodes[i].end) { + fte.chrg.cpMin = fte.chrgText.cpMax; + fte.lpstrText = bbCodes[i].end; + ts->TxSendMessage(EM_FINDTEXTEX, (WPARAM)FR_DOWN, (LPARAM)&fte, &lResult); + if (lResult == -1) + continue; + range.cpMax = fte.chrgText.cpMax; + } + + if ((fRange.cpMin == -1) || (fRange.cpMin > range.cpMin)) { + fRange = range; + fBBCode = bbCodes + i; + found = true; + + if (fText) + { + delete[] fText; + fText = nullptr; + } + if (bbCodes[i].end) { + TEXTRANGE trg; + trg.chrg.cpMin = fte.chrg.cpMin; + trg.chrg.cpMax = fte.chrgText.cpMin; + trg.lpstrText = new wchar_t[trg.chrg.cpMax - trg.chrg.cpMin + 1]; + ts->TxSendMessage(EM_GETTEXTRANGE, 0, (LPARAM)&trg, &lResult); + fText = trg.lpstrText; + } + } + } + + if (found) { + found = fBBCode->func(ftd, fRange, fText, fBBCode->cookie); + if (fText) delete[] fText; + } + } +} diff --git a/libs/mTextControl/src/main.cpp b/libs/mTextControl/src/main.cpp new file mode 100644 index 0000000000..69a6c06a99 --- /dev/null +++ b/libs/mTextControl/src/main.cpp @@ -0,0 +1,58 @@ +/* +Miranda Text Control - Plugin for Miranda IM + +Copyright © 2005 Victor Pavlychko (nullbie@gmail.com), +© 2010 Merlin_de + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "stdafx.h" + +HMODULE hMsfteditDll = nullptr; +HINSTANCE g_hInst = nullptr; + +PCreateTextServices MyCreateTextServices = nullptr; + +void MTextControl_RegisterClass(); + +///////////////////////////////////////////////////////////////////////////////////////// + +void UnloadEmfCache(); + +BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD ul_reason_for_call, LPVOID) +{ + switch (ul_reason_for_call) { + case DLL_PROCESS_ATTACH: + g_hInst = hInst; + MyCreateTextServices = nullptr; + hMsfteditDll = LoadLibrary(L"msftedit.dll"); + if (hMsfteditDll) + MyCreateTextServices = (PCreateTextServices)GetProcAddress(hMsfteditDll, "CreateTextServices"); + + LoadTextUsers(); + + MTextControl_RegisterClass(); + break; + + case DLL_PROCESS_DETACH: + UnloadTextUsers(); + UnloadEmfCache(); + FreeLibrary(hMsfteditDll); + break; + } + + return TRUE; +} diff --git a/libs/mTextControl/src/richeditutils.cpp b/libs/mTextControl/src/richeditutils.cpp new file mode 100644 index 0000000000..c8867eb17a --- /dev/null +++ b/libs/mTextControl/src/richeditutils.cpp @@ -0,0 +1,140 @@ +#include "stdafx.h" + +class CREOleCallback : public IRichEditOleCallback +{ +private: + unsigned refCount = 1; + IStorage *pictStg = nullptr; + int nextStgId = 0; + +public: + CREOleCallback() {} + + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID * ppvObj) + { + if (IsEqualIID(riid, IID_IRichEditOleCallback)) { + *ppvObj = this; + this->AddRef(); + return S_OK; + } + *ppvObj = nullptr; + return E_NOINTERFACE; + } + + ULONG STDMETHODCALLTYPE AddRef() + { + if (this->refCount == 0) { + if (S_OK != StgCreateDocfile(nullptr, STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_DELETEONRELEASE, 0, &this->pictStg)) + this->pictStg = nullptr; + this->nextStgId = 0; + } + return ++this->refCount; + } + + ULONG STDMETHODCALLTYPE Release() + { + if (--this->refCount == 0) { + if (this->pictStg) + this->pictStg->Release(); + } + return this->refCount; + } + + HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(BOOL) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE DeleteObject(LPOLEOBJECT) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE GetClipboardData(CHARRANGE *, DWORD, LPDATAOBJECT *) + { + return E_NOTIMPL; + } + + HRESULT STDMETHODCALLTYPE GetContextMenu(WORD, LPOLEOBJECT, CHARRANGE *, HMENU *) + { + return E_INVALIDARG; + } + + HRESULT STDMETHODCALLTYPE GetDragDropEffect(BOOL, DWORD, LPDWORD) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE GetInPlaceContext(LPOLEINPLACEFRAME *, LPOLEINPLACEUIWINDOW *, LPOLEINPLACEFRAMEINFO) + { + return E_INVALIDARG; + } + + HRESULT STDMETHODCALLTYPE GetNewStorage(LPSTORAGE * lplpstg) + { + wchar_t sztName[64]; + mir_snwprintf(sztName, L"s%u", this->nextStgId); + if (this->pictStg == nullptr) + return STG_E_MEDIUMFULL; + + return this->pictStg->CreateStorage(sztName, STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE, 0, 0, lplpstg); + + } + + HRESULT STDMETHODCALLTYPE QueryAcceptData(LPDATAOBJECT, CLIPFORMAT *, DWORD, BOOL, HGLOBAL) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE QueryInsertObject(LPCLSID, LPSTORAGE, LONG) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE ShowContainerUI(BOOL) + { + return S_OK; + } +}; + +static CREOleCallback reOleCallback; + +void InitRichEdit(ITextServices *ts) +{ + LRESULT lResult; + ts->TxSendMessage(EM_SETOLECALLBACK, 0, (LPARAM)&reOleCallback, &lResult); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +static ATOM winClass = 0; + +static LRESULT CALLBACK RichEditProxyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + ITextServices *ts = (ITextServices *)GetWindowLongPtr(hwnd, GWLP_USERDATA); + if (ts && (msg != WM_DESTROY)) { + LRESULT lResult; + ts->TxSendMessage(msg, wParam, lParam, &lResult); + return lResult; + } + return 1; +} + +HWND CreateProxyWindow(ITextServices *ts) +{ + if (winClass == 0) { + WNDCLASSEX wcl = {}; + wcl.cbSize = sizeof(wcl); + wcl.lpfnWndProc = RichEditProxyWndProc; + wcl.style = CS_GLOBALCLASS; + wcl.hInstance = g_hInst; + wcl.hCursor = LoadCursor(nullptr, IDC_ARROW); + wcl.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); + wcl.lpszClassName = L"NBRichEditProxyWndClass"; + winClass = RegisterClassEx(&wcl); + } + + HWND hwnd = CreateWindow(L"NBRichEditProxyWndClass", L"", 0, 0, 0, 0, 0, nullptr, nullptr, g_hInst, nullptr); + SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)ts); + return hwnd; +} diff --git a/libs/mTextControl/src/services.cpp b/libs/mTextControl/src/services.cpp new file mode 100644 index 0000000000..e589d6efae --- /dev/null +++ b/libs/mTextControl/src/services.cpp @@ -0,0 +1,206 @@ +/* +Miranda Text Control - Plugin for Miranda IM +Copyright (C) 2005 Victor Pavlychko (nullbie@gmail.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "stdafx.h" +#include "FormattedTextDraw.h" + +struct TextObject +{ + DWORD options; + IFormattedTextDraw *ftd; + TextObject() : options(0), ftd(nullptr) {} + ~TextObject() { if (ftd) delete ftd; } +}; + +///////////////////////////////////////////////////////////////////////////////////////// +// elper functions + +void MText_InitFormatting0(IFormattedTextDraw *ftd, DWORD) +{ + LRESULT lResult; + + // urls + ftd->getTextService()->TxSendMessage(EM_AUTOURLDETECT, TRUE, 0, &lResult); +} + +void MText_InitFormatting1(TextObject *text) +{ + // bbcodes + bbCodeParse(text->ftd); + + // smilies + // HWND hwnd = (HWND)CallServiceSync(MS_TEXT_CREATEPROXY, (WPARAM)text, 0); + HWND hwnd = CreateProxyWindow(text->ftd->getTextService()); + SMADD_RICHEDIT3 sm = { 0 }; + sm.cbSize = sizeof(sm); + sm.hwndRichEditControl = hwnd; + sm.rangeToReplace = nullptr; + sm.Protocolname = nullptr; + sm.flags = SAFLRE_INSERTEMF; + CallService(MS_SMILEYADD_REPLACESMILEYS, RGB(0xff, 0xff, 0xff), (LPARAM)&sm); + DestroyWindow(hwnd); + + // text->ftd->getTextService()->TxSendMessage(EM_SETSEL, 0, -1, &lResult); + /* + // rtl stuff + PARAFORMAT2 pf2; + pf2.cbSize = sizeof(pf2); + pf2.dwMask = PFM_ALIGNMENT|PFM_RTLPARA; + pf2.wEffects = PFE_RTLPARA; + pf2.wAlignment = PFA_RIGHT; + text->ftd->getTextService()->TxSendMessage(EM_SETSEL, 0, -1, &lResult); + text->ftd->getTextService()->TxSendMessage(EM_SETPARAFORMAT, 0, (LPARAM)&pf2, &lResult); + text->ftd->getTextService()->TxSendMessage(EM_SETSEL, 0, 0, &lResult); + */ +} + +///////////////////////////////////////////////////////////////////////////////////////// +// allocate text object (unicode) + +MTEXTCONTROL_DLL(HANDLE) MTextCreateW(HANDLE userHandle, WCHAR *text) +{ + TextObject *result = new TextObject; + result->options = TextUserGetOptions(userHandle); + result->ftd = new CFormattedTextDraw; + result->ftd->Create(); + InitRichEdit(result->ftd->getTextService()); + + MText_InitFormatting0(result->ftd, result->options); + result->ftd->putTextW(text); + MText_InitFormatting1(result); + + return (HANDLE)result; +} + +///////////////////////////////////////////////////////////////////////////////////////// +// allocate text object (advanced) + +MTEXTCONTROL_DLL(HANDLE) MTextCreateEx(HANDLE userHandle, void *text, DWORD flags) +{ + TextObject *result = new TextObject; + result->options = TextUserGetOptions(userHandle); + result->ftd = new CFormattedTextDraw; + result->ftd->Create(); + InitRichEdit(result->ftd->getTextService()); + + MText_InitFormatting0(result->ftd, result->options); + if (flags & MTEXT_FLG_WCHAR) + result->ftd->putTextW((WCHAR *)text); + else + result->ftd->putTextA((char *)text); + MText_InitFormatting1(result); + delete result; + + return nullptr; +} + +///////////////////////////////////////////////////////////////////////////////////////// +// measure text object + +MTEXTCONTROL_DLL(int) MTextMeasure(HDC dc, SIZE *sz, HANDLE text) +{ + if (!text) return 0; + + long lWidth = sz->cx, lHeight = sz->cy; + ((TextObject *)text)->ftd->get_NaturalSize(dc, &lWidth, &lHeight); + sz->cx = lWidth; + sz->cy = lHeight; + return 0; +} + +///////////////////////////////////////////////////////////////////////////////////////// +// display text object + +MTEXTCONTROL_DLL(int) MTextDisplay(HDC dc, POINT pos, SIZE sz, HANDLE text) +{ + if (!text) return 0; + + COLORREF cl = GetTextColor(dc); + + LRESULT lResult; + CHARFORMAT cf = { 0 }; + cf.cbSize = sizeof(cf); + cf.dwMask = CFM_COLOR; + cf.crTextColor = cl; + ((TextObject *)text)->ftd->getTextService()->TxSendMessage(EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf, &lResult); + + SetBkMode(dc, TRANSPARENT); + + long lWidth = sz.cx, lHeight; + ((TextObject *)text)->ftd->get_NaturalSize(dc, &lWidth, &lHeight); + + RECT rt; + rt.left = pos.x; + rt.top = pos.y; + rt.right = pos.x + lWidth; + rt.bottom = pos.y + lHeight; + ((TextObject *)text)->ftd->Draw(dc, &rt); + + return 0; +} + +///////////////////////////////////////////////////////////////////////////////////////// +// set parent window for text object (this is required for mouse handling, etc) + +MTEXTCONTROL_DLL(int) MTextSetParent(HANDLE text, HWND hwnd, RECT rect) +{ + if (text) + ((TextObject *)text)->ftd->setParentWnd(hwnd, rect); + return 0; +} + +///////////////////////////////////////////////////////////////////////////////////////// +// send message to an object + +MTEXTCONTROL_DLL(int) MTextSendMessage(HWND hwnd, HANDLE text, UINT msg, WPARAM wParam, LPARAM lParam) +{ + if (!text) + return 0; + + LRESULT lResult; + ((TextObject *)text)->ftd->getTextService()->TxSendMessage(msg, wParam, lParam, &lResult); + + if (hwnd && (msg == WM_MOUSEMOVE)) { + HDC hdc = GetDC(hwnd); + ((TextObject *)text)->ftd->getTextService()->OnTxSetCursor(DVASPECT_CONTENT, 0, nullptr, nullptr, hdc, nullptr, nullptr, LOWORD(0), HIWORD(0)); + ReleaseDC(hwnd, hdc); + } + + return lResult; +} + +///////////////////////////////////////////////////////////////////////////////////////// +// create a proxy window + +MTEXTCONTROL_DLL(HWND) MTextCreateProxy(HANDLE text) +{ + if (!text) + return nullptr; + + return CreateProxyWindow(((TextObject *)text)->ftd->getTextService()); +} + +///////////////////////////////////////////////////////////////////////////////////////// +// destroy text object + +MTEXTCONTROL_DLL(int) MTextDestroy(HANDLE text) +{ + if (text) delete (TextObject *)text; + return 0; +} diff --git a/libs/mTextControl/src/services.h b/libs/mTextControl/src/services.h new file mode 100644 index 0000000000..f198e70bbe --- /dev/null +++ b/libs/mTextControl/src/services.h @@ -0,0 +1,26 @@ +/* +Miranda Text Control - Plugin for Miranda IM +Copyright (C) 2005 Victor Pavlychko (nullbie@gmail.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __services_h__ +#define __services_h__ + +void LoadServices(); + + +#endif // __services_h__ diff --git a/libs/mTextControl/src/stdafx.cxx b/libs/mTextControl/src/stdafx.cxx new file mode 100644 index 0000000000..0a3b3b3920 --- /dev/null +++ b/libs/mTextControl/src/stdafx.cxx @@ -0,0 +1,18 @@ +/* +Copyright (C) 2012-20 Miranda NG team (https://miranda-ng.org) + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation version 2 +of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "stdafx.h"
\ No newline at end of file diff --git a/libs/mTextControl/src/stdafx.h b/libs/mTextControl/src/stdafx.h new file mode 100644 index 0000000000..2956c868d8 --- /dev/null +++ b/libs/mTextControl/src/stdafx.h @@ -0,0 +1,52 @@ +/* +Miranda Text Control - Plugin for Miranda IM +Copyright (C) 2005 Victor Pavlychko (nullbie@gmail.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __headers_h__ +#define __headers_h__ + +#define _CRT_SECURE_NO_DEPRECATE + +#define OEMRESOURCE + +#include <windows.h> +#include <richedit.h> +#include <richole.h> +#include <tom.h> +#include <textserv.h> + +#include <m_database.h> +#include <m_langpack.h> +#include <m_utils.h> + +#include <m_text.h> +#include <m_smileyadd.h> + +#include "version.h" +#include "textusers.h" + +extern HINSTANCE g_hInst; +extern PCreateTextServices MyCreateTextServices; + +void InitRichEdit(ITextServices *ts); +HWND CreateProxyWindow(ITextServices *ts); + +#define MODULTITLE "Text Display" +#define MODULENAME "MTextControl" + +#endif // __headers_h__ diff --git a/libs/mTextControl/src/textcontrol.cpp b/libs/mTextControl/src/textcontrol.cpp new file mode 100644 index 0000000000..fe4c70ea12 --- /dev/null +++ b/libs/mTextControl/src/textcontrol.cpp @@ -0,0 +1,146 @@ +/* +Miranda Text Control - Plugin for Miranda IM +Copyright (C) 2005 Victor Pavlychko (nullbie@gmail.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "stdafx.h" + +LRESULT CALLBACK MTextControlWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); +LRESULT MTextControl_OnPaint(HWND hwnd, WPARAM wParam, LPARAM lParam); +//LRESULT MTextControl_Measure(HWND hwnd, int maxw, SIZE *size); + +struct TextControlData +{ + HANDLE htu; + wchar_t *text; + HANDLE mtext; +}; + +void MTextControl_RegisterClass() +{ + WNDCLASSEX wcl = {}; + wcl.cbSize = sizeof(wcl); + wcl.lpfnWndProc = MTextControlWndProc; + wcl.style = CS_GLOBALCLASS; + wcl.hInstance = g_hInst; + wcl.hCursor = LoadCursor(nullptr, IDC_ARROW); + wcl.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); + wcl.lpszClassName = L"MTextControl"; + RegisterClassEx(&wcl); +} + +LRESULT CALLBACK MTextControlWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + TextControlData *data = (TextControlData *)GetWindowLongPtr(hwnd, GWLP_USERDATA); + switch (msg) { + case WM_CREATE: + data = new TextControlData; + data->text = nullptr; + data->mtext = nullptr; + data->htu = htuDefault; + SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data); + PostMessage(hwnd, MTM_UPDATE, 0, 0); + return 0; + + case MTM_SETUSER: + data->htu = wParam ? (HANDLE)wParam : htuDefault; + // falldown, DefWindowProc won't process WM_USER ;) + + case WM_SETTEXT: + DefWindowProc(hwnd, msg, wParam, lParam); + // falldown + + case MTM_UPDATE: + if (data->text) delete[] data->text; + if (data->mtext) MTextDestroy(data->mtext); + { + int textLength = GetWindowTextLength(hwnd); + data->text = new wchar_t[textLength + 1]; + GetWindowText(hwnd, data->text, textLength + 1); + data->mtext = MTextCreateW(data->htu, data->text); + + RECT rc; GetClientRect(hwnd, &rc); + MTextSetParent(data->mtext, hwnd, rc); + + InvalidateRect(hwnd, nullptr, TRUE); + } + return TRUE; + + case WM_PAINT: + return MTextControl_OnPaint(hwnd, wParam, lParam); + + case WM_ERASEBKGND: + RECT rc; + GetClientRect(hwnd, &rc); + FillRect((HDC)wParam, &rc, GetSysColorBrush(COLOR_BTNFACE)); + return TRUE; + + case WM_MOUSEMOVE: + if (data && data->mtext) + return MTextSendMessage(hwnd, data->mtext, msg, wParam, lParam); + break; + } + + return DefWindowProc(hwnd, msg, wParam, lParam); +} + +/// Paint //////////////////////////////////// +LRESULT MTextControl_OnPaint(HWND hwnd, WPARAM, LPARAM) +{ + PAINTSTRUCT ps; + HDC hdc = BeginPaint(hwnd, &ps); + { + RECT rc; + GetClientRect(hwnd, &rc); + FrameRect(hdc, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH)); + } + + SetTextColor(hdc, RGB(0, 0, 0)); + SetBkMode(hdc, TRANSPARENT); + + // Find the text to draw + TextControlData *data = (TextControlData *)GetWindowLongPtr(hwnd, GWLP_USERDATA); + if (data->mtext) { + HFONT hfntSave = nullptr; + HFONT hfnt = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0); + if (!hfnt) + hfnt = (HFONT)SendMessage(GetParent(hwnd), WM_GETFONT, 0, 0); + if (hfnt) { + LOGFONT lf; + GetObject(hfnt, sizeof(lf), &lf); + hfntSave = (HFONT)SelectObject(hdc, hfnt); + } + + // Draw the text + RECT rc; + GetClientRect(hwnd, &rc); + POINT pos; + pos.x = 0; + pos.y = 2; + SIZE sz; + sz.cx = rc.right - rc.left; + sz.cy = rc.bottom - rc.top - 4; + MTextDisplay(hdc, pos, sz, data->mtext); + + if (hfntSave) + SelectObject(hdc, hfntSave); + } + + // Release the device context + EndPaint(hwnd, &ps); + return 0; +} diff --git a/libs/mTextControl/src/textusers.cpp b/libs/mTextControl/src/textusers.cpp new file mode 100644 index 0000000000..11be479126 --- /dev/null +++ b/libs/mTextControl/src/textusers.cpp @@ -0,0 +1,85 @@ +/* +Miranda Text Control - Plugin for Miranda IM +Copyright (C) 2005 Victor Pavlychko (nullbie@gmail.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "stdafx.h" + +HANDLE htuDefault = nullptr; + +static TextUser *textUserFirst = nullptr; +static TextUser *textUserLast = nullptr; + +MTEXTCONTROL_DLL(HANDLE) MTextRegister(const char *userTitle, DWORD options) +{ + TextUser *textUserNew = new TextUser; + textUserNew->name = new char[mir_strlen(userTitle) + 1]; + mir_strcpy(textUserNew->name, userTitle); + textUserNew->options = + (db_get_dw(0, MODULENAME, userTitle, options)&MTEXT_FANCY_MASK) | (textUserNew->options&MTEXT_SYSTEM_MASK); + db_set_dw(0, MODULENAME, userTitle, textUserNew->options); + textUserNew->prev = textUserLast; + textUserNew->next = nullptr; + if (textUserLast) { + textUserLast->next = textUserNew; + textUserLast = textUserNew; + } + else textUserFirst = textUserLast = textUserNew; + + return textUserNew; +} + +DWORD TextUserGetOptions(HANDLE userHandle) +{ + if (!userHandle) return 0; + return ((TextUser *)userHandle)->options; +} + +void TextUserSetOptions(HANDLE userHandle, DWORD options) +{ + if (!userHandle) return; + ((TextUser *)userHandle)->options = options; +} + +void TextUsersSave() +{ + for (TextUser *textUser = textUserFirst; textUser; textUser = textUser->next) + db_set_dw(0, MODULENAME, textUser->name, textUser->options); +} + +void TextUsersReset() +{ + for (TextUser *textUser = textUserFirst; textUser; textUser = textUser->next) + textUser->options = (db_get_dw(0, MODULENAME, textUser->name, 0) & MTEXT_FANCY_MASK) | (textUser->options&MTEXT_SYSTEM_MASK); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +void LoadTextUsers() +{ + htuDefault = MTextRegister("Text Controls", MTEXT_FANCY_MASK); +} + +void UnloadTextUsers() +{ + while (textUserFirst) { + delete[] textUserFirst->name; + TextUser *next = textUserFirst->next; + delete[] textUserFirst; + textUserFirst = next; + } +} diff --git a/libs/mTextControl/src/textusers.h b/libs/mTextControl/src/textusers.h new file mode 100644 index 0000000000..a78d39fe71 --- /dev/null +++ b/libs/mTextControl/src/textusers.h @@ -0,0 +1,42 @@ +/* +Miranda Text Control - Plugin for Miranda IM +Copyright (C) 2005 Victor Pavlychko (nullbie@gmail.com) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __textusers_h__ +#define __textusers_h__ + +#define MTEXT_GUI_MASK (MTEXT_BIDI_MASK|MTEXT_FANCY_SMILEYS|MTEXT_FANCY_BBCODES|MTEXT_FANCY_MATHMOD) + +struct TextUser : public MZeroedObject +{ + char *name; + DWORD options; + TextUser *prev, *next; +}; + +void LoadTextUsers(); +void UnloadTextUsers(); + +extern HANDLE htuDefault; + +DWORD TextUserGetOptions(HANDLE userHandle); +void TextUserSetOptions(HANDLE userHandle, DWORD options); +void TextUsersSave(); +void TextUsersReset(); + +#endif // __textusers_h__ diff --git a/libs/mTextControl/src/version.h b/libs/mTextControl/src/version.h new file mode 100644 index 0000000000..adc2eb7523 --- /dev/null +++ b/libs/mTextControl/src/version.h @@ -0,0 +1,13 @@ +#define __MAJOR_VERSION 0 +#define __MINOR_VERSION 8 +#define __RELEASE_NUM 0 +#define __BUILD_NUM 1 + +#include <stdver.h> + +#define __PLUGIN_NAME "Miranda text control" +#define __FILENAME "mTextControl.dll" +#define __DESCRIPTION "Provides text render API for different plugins." +#define __AUTHOR "Merlin_de, Victor Pavlychko" +#define __AUTHORWEB "https://miranda-ng.org/p/mTextControl/" +#define __COPYRIGHT "© 2005-2006 Victor Pavlychko, 2010 Merlin_de" |