From fe4c746cd485dbdd47e2e269207f20366baa9cdd Mon Sep 17 00:00:00 2001 From: Don Ho Date: Thu, 19 Nov 2009 02:02:17 +0000 Subject: [PATCH] [NEW_FEATURE] Add Chinese BIG5 encoding (in progress). git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@564 f5eea248-9336-0410-98b8-ebc06183d4e3 --- PowerEditor/src/MISC/Common/Common.h | 20 ++++++-- PowerEditor/src/Notepad_plus.cpp | 52 +++++++++++++++----- PowerEditor/src/Notepad_plus.rc | 4 ++ PowerEditor/src/ScitillaComponent/Buffer.cpp | 15 +++++- PowerEditor/src/ScitillaComponent/Buffer.h | 10 ++++ PowerEditor/src/menuCmdID.h | 2 + 6 files changed, 83 insertions(+), 20 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.h b/PowerEditor/src/MISC/Common/Common.h index 889f5f71..ad249cbf 100644 --- a/PowerEditor/src/MISC/Common/Common.h +++ b/PowerEditor/src/MISC/Common/Common.h @@ -93,11 +93,21 @@ public: static WcharMbcsConvertor * getInstance() {return _pSelf;}; static void destroyInstance() {delete _pSelf;}; - const wchar_t * char2wchar(const char* mbStr, UINT codepage); - const wchar_t * char2wchar(const char * mbcs2Convert, UINT codepage, int *mstart, int *mend); - const char * wchar2char(const wchar_t* wcStr, UINT codepage); - const char * wchar2char(const wchar_t * wcStr, UINT codepage, long *mstart, long *mend); - + const wchar_t * char2wchar(const char *mbStr, UINT codepage); + const wchar_t * char2wchar(const char *mbcs2Convert, UINT codepage, int *mstart, int *mend); + const char * wchar2char(const wchar_t *wcStr, UINT codepage); + const char * wchar2char(const wchar_t *wcStr, UINT codepage, long *mstart, long *mend); + + const char * encode(UINT fromCodepage, UINT toCodepage, const char *txt2Encode) { + const wchar_t * strW = char2wchar(txt2Encode, fromCodepage); + return wchar2char(strW, toCodepage); + }; +/* + const char * encodeFromUtf8To(const char *txt2Encode) const { + const wchar_t * strW = wmc->char2wchar(txt2Encode, SC_CP_UTF8); + return wchar2char(strW, toCodepage); + }; +*/ protected: WcharMbcsConvertor() : _multiByteStr(NULL), _wideCharStr(NULL), _multiByteAllocLen(0), _wideCharAllocLen(0), initSize(1024) { }; diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index 8c3539b5..8e17f12c 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -3741,12 +3741,12 @@ void Notepad_plus::command(int id) break; } - case IDM_SEARCH_GONEXTMARKER1 : - case IDM_SEARCH_GONEXTMARKER2 : - case IDM_SEARCH_GONEXTMARKER3 : - case IDM_SEARCH_GONEXTMARKER4 : - case IDM_SEARCH_GONEXTMARKER5 : - case IDM_SEARCH_GONEXTMARKER_DEF : + case IDM_SEARCH_GONEXTMARKER1 : + case IDM_SEARCH_GONEXTMARKER2 : + case IDM_SEARCH_GONEXTMARKER3 : + case IDM_SEARCH_GONEXTMARKER4 : + case IDM_SEARCH_GONEXTMARKER5 : + case IDM_SEARCH_GONEXTMARKER_DEF : { int styleID; if (id == IDM_SEARCH_GONEXTMARKER1) @@ -3766,13 +3766,13 @@ void Notepad_plus::command(int id) break; } - - case IDM_SEARCH_GOPREVMARKER1 : - case IDM_SEARCH_GOPREVMARKER2 : - case IDM_SEARCH_GOPREVMARKER3 : - case IDM_SEARCH_GOPREVMARKER4 : - case IDM_SEARCH_GOPREVMARKER5 : - case IDM_SEARCH_GOPREVMARKER_DEF : + + case IDM_SEARCH_GOPREVMARKER1 : + case IDM_SEARCH_GOPREVMARKER2 : + case IDM_SEARCH_GOPREVMARKER3 : + case IDM_SEARCH_GOPREVMARKER4 : + case IDM_SEARCH_GOPREVMARKER5 : + case IDM_SEARCH_GOPREVMARKER_DEF : { int styleID; if (id == IDM_SEARCH_GOPREVMARKER1) @@ -4372,6 +4372,32 @@ void Notepad_plus::command(int id) break; } + case IDM_FORMAT_BIG5 : + { + int encoding = -1; + switch (id) + { + case IDM_FORMAT_BIG5: + encoding = CP_BIG5; + break; + + default : // IDM_FORMAT_ANSI + encoding = CP_ACP; + } + + int len = _pEditView->getCurrentDocLen(); + char *content = new char[len+1]; + _pEditView->execute(SCI_GETTEXT, len+1, (LPARAM)content); + WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); + const char *newContent = wmc->encode(encoding, SC_CP_UTF8, content); + _pEditView->execute(SCI_SETCODEPAGE, SC_CP_UTF8); + _pEditView->execute(SCI_SETTEXT, 0, (LPARAM)newContent); + _pEditView->getCurrentBuffer()->setEncoding(encoding); + delete [] content; + break; + } + + case IDM_FORMAT_CONV2_ANSI: case IDM_FORMAT_CONV2_AS_UTF_8: case IDM_FORMAT_CONV2_UTF_8: diff --git a/PowerEditor/src/Notepad_plus.rc b/PowerEditor/src/Notepad_plus.rc index c35ed214..716a0b20 100644 --- a/PowerEditor/src/Notepad_plus.rc +++ b/PowerEditor/src/Notepad_plus.rc @@ -396,6 +396,10 @@ BEGIN MENUITEM "Encode in UTF-8", IDM_FORMAT_UTF_8 MENUITEM "Encode in UCS-2 Big Endian", IDM_FORMAT_UCS_2BE MENUITEM "Encode in UCS-2 Little Endian", IDM_FORMAT_UCS_2LE + POPUP "More..." + BEGIN + MENUITEM "Chinese Traditional (Big5)", IDM_FORMAT_BIG5 + END MENUITEM SEPARATOR MENUITEM "Convert to ANSI", IDM_FORMAT_CONV2_ANSI MENUITEM "Convert to UTF-8 without BOM", IDM_FORMAT_CONV2_AS_UTF_8 diff --git a/PowerEditor/src/ScitillaComponent/Buffer.cpp b/PowerEditor/src/ScitillaComponent/Buffer.cpp index 1d7f0c1f..9bec9889 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScitillaComponent/Buffer.cpp @@ -64,7 +64,7 @@ static bool isInList(const TCHAR *token, const TCHAR *list) { Buffer::Buffer(FileManager * pManager, BufferID id, Document doc, DocFileStatus type, const TCHAR *fileName) //type must be either DOC_REGULAR or DOC_UNNAMED : _pManager(pManager), _id(id), _isDirty(false), _doc(doc), _isFileReadOnly(false), _isUserReadOnly(false), _recentTag(-1), _references(0), - _canNotify(false), _timeStamp(0), _needReloading(false) + _canNotify(false), _timeStamp(0), _needReloading(false), _encoding(-1) { NppParameters *pNppParamInst = NppParameters::getInstance(); const NewDocDefaultSettings & ndds = (pNppParamInst->getNppGUI()).getNewDocDefaultSettings(); @@ -603,6 +603,8 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy) { Utf8_16_Write UnicodeConvertor; UnicodeConvertor.setEncoding(mode); + int encoding = buffer->getEncoding(); + FILE *fp = UnicodeConvertor.fopen(fullpath, TEXT("wb")); if (fp) { @@ -617,7 +619,16 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy) { grabSize = blockSize; _pscratchTilla->getText(data, i, i + grabSize); - UnicodeConvertor.fwrite(data, grabSize); + if (encoding != -1) + { + WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); + const char *newData = wmc->encode(SC_CP_UTF8, encoding, data); + UnicodeConvertor.fwrite(newData, strlen(newData)); + } + else + { + UnicodeConvertor.fwrite(data, grabSize); + } } UnicodeConvertor.fclose(); diff --git a/PowerEditor/src/ScitillaComponent/Buffer.h b/PowerEditor/src/ScitillaComponent/Buffer.h index 182aa563..0b7348ee 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.h +++ b/PowerEditor/src/ScitillaComponent/Buffer.h @@ -220,6 +220,15 @@ public : _unicodeMode = mode; doNotify(BufferChangeUnicode | BufferChangeDirty); }; + + int getEncoding() const { + return _encoding; + }; + + void setEncoding(int encoding) { + _encoding = encoding; + }; + DocFileStatus getStatus() const { return _currentStatus; }; @@ -319,6 +328,7 @@ private : bool _isDirty; formatType _format; UniMode _unicodeMode; + int _encoding; bool _isUserReadOnly; bool _needLexer; //initially true //these properties have to be duplicated because of multiple references diff --git a/PowerEditor/src/menuCmdID.h b/PowerEditor/src/menuCmdID.h index 3469804f..29888d85 100644 --- a/PowerEditor/src/menuCmdID.h +++ b/PowerEditor/src/menuCmdID.h @@ -237,6 +237,8 @@ #define IDM_FORMAT_CONV2_UTF_8 (IDM_FORMAT + 11) #define IDM_FORMAT_CONV2_UCS_2BE (IDM_FORMAT + 12) #define IDM_FORMAT_CONV2_UCS_2LE (IDM_FORMAT + 13) + + #define IDM_FORMAT_BIG5 (IDM_FORMAT + 20) #define IDM_LANG (IDM + 6000) #define IDM_LANGSTYLE_CONFIG_DLG (IDM_LANG + 1)