Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

6195 lines (5085 sloc) 193.794 kb
/******************************************************************************
*
*
* Notepad2
*
* Edit.c
* Text File Editing Helper Stuff
*
* See Readme.txt for more information about this source code.
* Please send me your comments to this work.
*
* See License.txt for details about distribution and modification.
*
* (c) Florian Balmer 1996-2010
* florian.balmer@gmail.com
* http://www.flos-freeware.ch
*
*
******************************************************************************/
#define _WIN32_WINNT 0x501
#include <windows.h>
#include <shlwapi.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "notepad2.h"
#include "helpers.h"
#include "dialogs.h"
#include "scintilla.h"
#include "scilexer.h"
#include "styles.h"
#include "edit.h"
#include "scicall.h"
#include "resource.h"
extern HWND hwndMain;
extern HWND hwndEdit;
extern HINSTANCE g_hInstance;
extern LPMALLOC g_lpMalloc;
extern DWORD dwLastIOError;
extern HWND hDlgFindReplace;
extern UINT cpLastFind;
extern BOOL bReplaceInitialized;
static EDITFINDREPLACE efrSave;
static BOOL bSwitchedFindReplace = FALSE;
static int xFindReplaceDlgSave;
static int yFindReplaceDlgSave;
extern int xFindReplaceDlg;
extern int yFindReplaceDlg;
extern int iDefaultEncoding;
extern int iDefaultEOLMode;
extern int iLineEndings[3];
extern BOOL bFixLineEndings;
extern BOOL bAutoStripBlanks;
// Default Codepage and Character Set
extern int iDefaultCodePage;
extern int iDefaultCharSet;
extern BOOL bSkipUnicodeDetection;
extern BOOL bLoadASCIIasUTF8;
extern int iSrcEncoding;
extern int iWeakSrcEncoding;
// Supported Encodings
WCHAR wchANSI[8] = L"";
WCHAR wchOEM [8] = L"";
NP2ENCODING mEncoding[] = {
{ NCP_DEFAULT|NCP_RECODE, 0, "ansi,ansi,ascii,", 61000, L"" },
{ NCP_8BIT|NCP_RECODE, 0, "oem,oem,", 61001, L"" },
{ NCP_UNICODE|NCP_UNICODE_BOM, 0, "", 61002, L"" },
{ NCP_UNICODE|NCP_UNICODE_REVERSE|NCP_UNICODE_BOM, 0, "", 61003, L"" },
{ NCP_UNICODE, 0, "", 61004, L"" },
{ NCP_UNICODE|NCP_UNICODE_REVERSE, 0, "", 61005, L"" },
{ NCP_UTF8|NCP_RECODE, 0, "utf-8,utf8,", 61006, L"" },
{ NCP_UTF8|NCP_UTF8_SIGN, 0, "utf-8,utf8,", 61007, L"" },
{ NCP_8BIT|NCP_RECODE, CP_UTF7, "utf-7,utf7,", 61008, L"" },
{ NCP_8BIT|NCP_RECODE, 720, "DOS-720,dos720,", 61009, L"" },
{ NCP_8BIT|NCP_RECODE, 28596, "iso-8859-6,iso88596,arabic,csisolatinarabic,ecma114,isoir127,", 61010, L"" },
{ NCP_8BIT|NCP_RECODE, 10004, "x-mac-arabic,xmacarabic,", 61011, L"" },
{ NCP_8BIT|NCP_RECODE, 1256, "windows-1256,windows1256,cp1256", 61012, L"" },
{ NCP_8BIT|NCP_RECODE, 775, "ibm775,ibm775,cp500,", 61013, L"" },
{ NCP_8BIT|NCP_RECODE, 28594, "iso-8859-4,iso88594,csisolatin4,isoir110,l4,latin4,", 61014, L"" },
{ NCP_8BIT|NCP_RECODE, 1257, "windows-1257,windows1257,", 61015, L"" },
{ NCP_8BIT|NCP_RECODE, 852, "ibm852,ibm852,cp852,", 61016, L"" },
{ NCP_8BIT|NCP_RECODE, 28592, "iso-8859-2,iso88592,csisolatin2,isoir101,latin2,l2,", 61017, L"" },
{ NCP_8BIT|NCP_RECODE, 10029, "x-mac-ce,xmacce,", 61018, L"" },
{ NCP_8BIT|NCP_RECODE, 1250, "windows-1250,windows1250,xcp1250,", 61019, L"" },
{ NCP_8BIT|NCP_RECODE, 936, "gb2312,gb2312,chinese,cngb,csgb2312,csgb231280,gb231280,gbk,", 61020, L"" },
{ NCP_8BIT|NCP_RECODE, 10008, "x-mac-chinesesimp,xmacchinesesimp,", 61021, L"" },
{ NCP_8BIT|NCP_RECODE, 950, "big5,big5,cnbig5,csbig5,xxbig5,", 61022, L"" },
{ NCP_8BIT|NCP_RECODE, 10002, "x-mac-chinesetrad,xmacchinesetrad,", 61023, L"" },
{ NCP_8BIT|NCP_RECODE, 10082, "x-mac-croatian,xmaccroatian,", 61024, L"" },
{ NCP_8BIT|NCP_RECODE, 866, "cp866,cp866,ibm866,", 61025, L"" },
{ NCP_8BIT|NCP_RECODE, 28595, "iso-8859-5,iso88595,csisolatin5,csisolatincyrillic,cyrillic,isoir144,", 61026, L"" },
{ NCP_8BIT|NCP_RECODE, 20866, "koi8-r,koi8r,cskoi8r,koi,koi8,", 61027, L"" },
{ NCP_8BIT|NCP_RECODE, 21866, "koi8-u,koi8u,koi8ru,", 61028, L"" },
{ NCP_8BIT|NCP_RECODE, 10007, "x-mac-cyrillic,xmaccyrillic,", 61029, L"" },
{ NCP_8BIT|NCP_RECODE, 1251, "windows-1251,windows1251,xcp1251,", 61030, L"" },
{ NCP_8BIT|NCP_RECODE, 28603, "iso-8859-13,iso885913,", 61031, L"" },
{ NCP_8BIT|NCP_RECODE, 863, "ibm863,ibm863,", 61032, L"" },
{ NCP_8BIT|NCP_RECODE, 737, "ibm737,ibm737,", 61033, L"" },
{ NCP_8BIT|NCP_RECODE, 28597, "iso-8859-7,iso88597,csisolatingreek,ecma118,elot928,greek,greek8,isoir126,", 61034, L"" },
{ NCP_8BIT|NCP_RECODE, 10006, "x-mac-greek,xmacgreek,", 61035, L"" },
{ NCP_8BIT|NCP_RECODE, 1253, "windows-1253,windows1253,", 61036, L"" },
{ NCP_8BIT|NCP_RECODE, 869, "ibm869,ibm869,", 61037, L"" },
{ NCP_8BIT|NCP_RECODE, 862, "DOS-862,dos862,", 61038, L"" },
{ NCP_8BIT|NCP_RECODE, 38598, "iso-8859-8-i,iso88598i,logical,", 61039, L"" },
{ NCP_8BIT|NCP_RECODE, 28598, "iso-8859-8,iso88598,csisolatinhebrew,hebrew,isoir138,visual,", 61040, L"" },
{ NCP_8BIT|NCP_RECODE, 10005, "x-mac-hebrew,xmachebrew,", 61041, L"" },
{ NCP_8BIT|NCP_RECODE, 1255, "windows-1255,windows1255,", 61042, L"" },
{ NCP_8BIT|NCP_RECODE, 861, "ibm861,ibm861,", 61043, L"" },
{ NCP_8BIT|NCP_RECODE, 10079, "x-mac-icelandic,xmacicelandic,", 61044, L"" },
{ NCP_8BIT|NCP_RECODE, 10001, "x-mac-japanese,xmacjapanese,", 61045, L"" },
{ NCP_8BIT|NCP_RECODE, 932, "shift_jis,shiftjis,shiftjs,csshiftjis,cswindows31j,mskanji,xmscp932,xsjis,", 61046, L"" },
{ NCP_8BIT|NCP_RECODE, 10003, "x-mac-korean,xmackorean,", 61047, L"" },
{ NCP_8BIT|NCP_RECODE, 949, "windows-949,windows949,ksc56011987,csksc5601,euckr,isoir149,korean,ksc56011989", 61048, L"" },
{ NCP_8BIT|NCP_RECODE, 28593, "iso-8859-3,iso88593,latin3,isoir109,l3,", 61049, L"" },
{ NCP_8BIT|NCP_RECODE, 28605, "iso-8859-15,iso885915,latin9,l9,", 61050, L"" },
{ NCP_8BIT|NCP_RECODE, 865, "ibm865,ibm865,", 61051, L"" },
{ NCP_8BIT|NCP_RECODE, 437, "ibm437,ibm437,437,cp437,cspc8,codepage437,", 61052, L"" },
{ NCP_8BIT|NCP_RECODE, 858, "ibm858,ibm858,ibm00858,", 61053, L"" },
{ NCP_8BIT|NCP_RECODE, 860, "ibm860,ibm860,", 61054, L"" },
{ NCP_8BIT|NCP_RECODE, 10010, "x-mac-romanian,xmacromanian,", 61055, L"" },
{ NCP_8BIT|NCP_RECODE, 10021, "x-mac-thai,xmacthai,", 61056, L"" },
{ NCP_8BIT|NCP_RECODE, 874, "windows-874,windows874,dos874,iso885911,tis620,", 61057, L"" },
{ NCP_8BIT|NCP_RECODE, 857, "ibm857,ibm857,", 61058, L"" },
{ NCP_8BIT|NCP_RECODE, 28599, "iso-8859-9,iso88599,latin5,isoir148,l5,", 61059, L"" },
{ NCP_8BIT|NCP_RECODE, 10081, "x-mac-turkish,xmacturkish,", 61060, L"" },
{ NCP_8BIT|NCP_RECODE, 1254, "windows-1254,windows1254,", 61061, L"" },
{ NCP_8BIT|NCP_RECODE, 10017, "x-mac-ukrainian,xmacukrainian,", 61062, L"" },
{ NCP_8BIT|NCP_RECODE, 1258, "windows-1258,windows-258,", 61063, L"" },
{ NCP_8BIT|NCP_RECODE, 850, "ibm850,ibm850,", 61064, L"" },
{ NCP_8BIT|NCP_RECODE, 28591, "iso-8859-1,iso88591,cp819,latin1,ibm819,isoir100,latin1,l1,", 61065, L"" },
{ NCP_8BIT|NCP_RECODE, 10000, "macintosh,macintosh,", 61066, L"" },
{ NCP_8BIT|NCP_RECODE, 1252, "windows-1252,windows1252,cp367,cp819,ibm367,us,xansi,", 61067, L"" },
{ NCP_8BIT|NCP_RECODE, 37, "ebcdic-cp-us,ebcdiccpus,ebcdiccpca,ebcdiccpwt,ebcdiccpnl,ibm037,cp037,", 61068, L"" },
{ NCP_8BIT|NCP_RECODE, 500, "x-ebcdic-international,xebcdicinternational,", 61069, L"" },
/*{ NCP_8BIT|NCP_RECODE, 870, "CP870,cp870,ebcdiccproece,ebcdiccpyu,csibm870,ibm870,", 00000, L"" }, // IBM EBCDIC (Multilingual Latin-2) */
{ NCP_8BIT|NCP_RECODE, 875, "x-EBCDIC-GreekModern,xebcdicgreekmodern,", 61070, L"" },
{ NCP_8BIT|NCP_RECODE, 1026, "CP1026,cp1026,csibm1026,ibm1026,", 61071, L"" },
/*{ NCP_8BIT|NCP_RECODE, 1047, "IBM01047,ibm01047,", 00000, L"" }, // IBM EBCDIC (Open System Latin-1)
{ NCP_8BIT|NCP_RECODE, 1140, "x-ebcdic-cp-us-euro,xebcdiccpuseuro,", 00000, L"" }, // IBM EBCDIC (US-Canada-Euro)
{ NCP_8BIT|NCP_RECODE, 1141, "x-ebcdic-germany-euro,xebcdicgermanyeuro,", 00000, L"" }, // IBM EBCDIC (Germany-Euro)
{ NCP_8BIT|NCP_RECODE, 1142, "x-ebcdic-denmarknorway-euro,xebcdicdenmarknorwayeuro,", 00000, L"" }, // IBM EBCDIC (Denmark-Norway-Euro)
{ NCP_8BIT|NCP_RECODE, 1143, "x-ebcdic-finlandsweden-euro,xebcdicfinlandswedeneuro,", 00000, L"" }, // IBM EBCDIC (Finland-Sweden-Euro)
{ NCP_8BIT|NCP_RECODE, 1144, "x-ebcdic-italy-euro,xebcdicitalyeuro,", 00000, L"" }, // IBM EBCDIC (Italy-Euro)
{ NCP_8BIT|NCP_RECODE, 1145, "x-ebcdic-spain-euro,xebcdicspaineuro,", 00000, L"" }, // IBM EBCDIC (Spain-Latin America-Euro)
{ NCP_8BIT|NCP_RECODE, 1146, "x-ebcdic-uk-euro,xebcdicukeuro,", 00000, L"" }, // IBM EBCDIC (UK-Euro)
{ NCP_8BIT|NCP_RECODE, 1147, "x-ebcdic-france-euro,xebcdicfranceeuro,", 00000, L"" }, // IBM EBCDIC (France-Euro)
{ NCP_8BIT|NCP_RECODE, 1148, "x-ebcdic-international-euro,xebcdicinternationaleuro,", 00000, L"" }, // IBM EBCDIC (International-Euro)
{ NCP_8BIT|NCP_RECODE, 1149, "x-ebcdic-icelandic-euro,xebcdicicelandiceuro,", 00000, L"" }, // IBM EBCDIC (Icelandic-Euro)
{ NCP_8BIT|NCP_RECODE, 20273, "x-EBCDIC-Germany,xebcdicgermany,", 00000, L"" }, // IBM EBCDIC (Germany)
{ NCP_8BIT|NCP_RECODE, 20277, "x-EBCDIC-DenmarkNorway,xebcdicdenmarknorway,ebcdiccpdk,ebcdiccpno,", 00000, L"" }, // IBM EBCDIC (Denmark-Norway)
{ NCP_8BIT|NCP_RECODE, 20278, "x-EBCDIC-FinlandSweden,xebcdicfinlandsweden,ebcdicpfi,ebcdiccpse,", 00000, L"" }, // IBM EBCDIC (Finland-Sweden)
{ NCP_8BIT|NCP_RECODE, 20280, "x-EBCDIC-Italy,xebcdicitaly,", 00000, L"" }, // IBM EBCDIC (Italy)
{ NCP_8BIT|NCP_RECODE, 20284, "x-EBCDIC-Spain,xebcdicspain,ebcdiccpes,", 00000, L"" }, // IBM EBCDIC (Spain-Latin America)
{ NCP_8BIT|NCP_RECODE, 20285, "x-EBCDIC-UK,xebcdicuk,ebcdiccpgb,", 00000, L"" }, // IBM EBCDIC (UK)
{ NCP_8BIT|NCP_RECODE, 20290, "x-EBCDIC-JapaneseKatakana,xebcdicjapanesekatakana,", 00000, L"" }, // IBM EBCDIC (Japanese Katakana)
{ NCP_8BIT|NCP_RECODE, 20297, "x-EBCDIC-France,xebcdicfrance,ebcdiccpfr,", 00000, L"" }, // IBM EBCDIC (France)
{ NCP_8BIT|NCP_RECODE, 20420, "x-EBCDIC-Arabic,xebcdicarabic,ebcdiccpar1,", 00000, L"" }, // IBM EBCDIC (Arabic)
{ NCP_8BIT|NCP_RECODE, 20423, "x-EBCDIC-Greek,xebcdicgreek,ebcdiccpgr,", 00000, L"" }, // IBM EBCDIC (Greek)
{ NCP_8BIT|NCP_RECODE, 20424, "x-EBCDIC-Hebrew,xebcdichebrew,ebcdiccphe,", 00000, L"" }, // IBM EBCDIC (Hebrew)
{ NCP_8BIT|NCP_RECODE, 20833, "x-EBCDIC-KoreanExtended,xebcdickoreanextended,", 00000, L"" }, // IBM EBCDIC (Korean Extended)
{ NCP_8BIT|NCP_RECODE, 20838, "x-EBCDIC-Thai,xebcdicthai,ibmthai,csibmthai,", 00000, L"" }, // IBM EBCDIC (Thai)
{ NCP_8BIT|NCP_RECODE, 20871, "x-EBCDIC-Icelandic,xebcdicicelandic,ebcdiccpis,", 00000, L"" }, // IBM EBCDIC (Icelandic)
{ NCP_8BIT|NCP_RECODE, 20880, "x-EBCDIC-CyrillicRussian,xebcdiccyrillicrussian,ebcdiccyrillic,", 00000, L"" }, // IBM EBCDIC (Cyrillic Russian)
{ NCP_8BIT|NCP_RECODE, 20905, "x-EBCDIC-Turkish,xebcdicturkish,ebcdiccptr,", 00000, L"" }, // IBM EBCDIC (Turkish)
{ NCP_8BIT|NCP_RECODE, 20924, "IBM00924,ibm00924,ebcdiclatin9euro,", 00000, L"" }, // IBM EBCDIC (Open System-Euro Latin-1)
{ NCP_8BIT|NCP_RECODE, 21025, "x-EBCDIC-CyrillicSerbianBulgarian,xebcdiccyrillicserbianbulgarian,", 00000, L"" }, // IBM EBCDIC (Cyrillic Serbian-Bulgarian)
{ NCP_8BIT|NCP_RECODE, 50930, "x-EBCDIC-JapaneseAndKana,xebcdicjapaneseandkana,", 00000, L"" }, // IBM EBCDIC (Japanese and Japanese Katakana)
{ NCP_8BIT|NCP_RECODE, 50931, "x-EBCDIC-JapaneseAndUSCanada,xebcdicjapaneseanduscanada,", 00000, L"" }, // IBM EBCDIC (Japanese and US-Canada)
{ NCP_8BIT|NCP_RECODE, 50933, "x-EBCDIC-KoreanAndKoreanExtended,xebcdickoreanandkoreanextended,", 00000, L"" }, // IBM EBCDIC (Korean and Korean Extended)
{ NCP_8BIT|NCP_RECODE, 50935, "x-EBCDIC-SimplifiedChinese,xebcdicsimplifiedchinese,", 00000, L"" }, // IBM EBCDIC (Chinese Simplified)
{ NCP_8BIT|NCP_RECODE, 50937, "x-EBCDIC-TraditionalChinese,xebcdictraditionalchinese,", 00000, L"" }, // IBM EBCDIC (Chinese Traditional)
{ NCP_8BIT|NCP_RECODE, 50939, "x-EBCDIC-JapaneseAndJapaneseLatin,xebcdicjapaneseandjapaneselatin,", 00000, L"" }, // IBM EBCDIC (Japanese and Japanese-Latin)
{ NCP_8BIT|NCP_RECODE, 20105, "x-IA5,xia5,", 00000, L"" }, // Western European (IA5)
{ NCP_8BIT|NCP_RECODE, 20106, "x-IA5-German,xia5german,", 00000, L"" }, // German (IA5)
{ NCP_8BIT|NCP_RECODE, 20107, "x-IA5-Swedish,xia5swedish,", 00000, L"" }, // Swedish (IA5)
{ NCP_8BIT|NCP_RECODE, 20108, "x-IA5-Norwegian,xia5norwegian,", 00000, L"" }, // Norwegian (IA5)
{ NCP_8BIT|NCP_RECODE, 20936, "x-cp20936,xcp20936,", 00000, L"" }, // Chinese Simplified (GB2312)
{ NCP_8BIT|NCP_RECODE, 52936, "hz-gb-2312,hzgb2312,hz,", 00000, L"" }, // Chinese Simplified (HZ-GB2312) */
{ NCP_8BIT|NCP_RECODE, 54936, "gb18030,gb18030,", 61072, L"" },
/*{ NCP_8BIT|NCP_RECODE, 1361, "johab,johab,", 00000, L"" }, // Korean (Johab)
{ NCP_8BIT|NCP_RECODE, 20932, "euc-jp,,", 00000, L"" }, // Japanese (JIS X 0208-1990 & 0212-1990)
{ NCP_8BIT|NCP_RECODE, 50220, "iso-2022-jp,iso2022jp,", 00000, L"" }, // Japanese (JIS)
{ NCP_8BIT|NCP_RECODE, 50221, "csISO2022JP,csiso2022jp,", 00000, L"" }, // Japanese (JIS-Allow 1 byte Kana)
{ NCP_8BIT|NCP_RECODE, 50222, "_iso-2022-jp$SIO,iso2022jpSIO,", 00000, L"" }, // Japanese (JIS-Allow 1 byte Kana - SO/SI)
{ NCP_8BIT|NCP_RECODE, 50225, "iso-2022-kr,iso2022kr,csiso2022kr,", 00000, L"" }, // Korean (ISO-2022-KR)
{ NCP_8BIT|NCP_RECODE, 50227, "x-cp50227,xcp50227,", 00000, L"" }, // Chinese Simplified (ISO-2022)
{ NCP_8BIT|NCP_RECODE, 50229, "iso-2022-cn,iso2022cn,", 00000, L"" }, // Chinese Traditional (ISO-2022)
{ NCP_8BIT|NCP_RECODE, 20000, "x-Chinese-CNS,xchinesecns,", 00000, L"" }, // Chinese Traditional (CNS)
{ NCP_8BIT|NCP_RECODE, 20002, "x-Chinese-Eten,xchineseeten,", 00000, L"" }, // Chinese Traditional (Eten)
{ NCP_8BIT|NCP_RECODE, 51932, "euc-jp,eucjp,xeuc,xeucjp,", 00000, L"" }, // Japanese (EUC)
{ NCP_8BIT|NCP_RECODE, 51936, "euc-cn,euccn,xeuccn,", 00000, L"" }, // Chinese Simplified (EUC)
{ NCP_8BIT|NCP_RECODE, 51949, "euc-kr,euckr,cseuckr,", 00000, L"" }, // Korean (EUC)
{ NCP_8BIT|NCP_RECODE, 57002, "x-iscii-de,xisciide,", 00000, L"" }, // ISCII Devanagari
{ NCP_8BIT|NCP_RECODE, 57003, "x-iscii-be,xisciibe,", 00000, L"" }, // ISCII Bengali
{ NCP_8BIT|NCP_RECODE, 57004, "x-iscii-ta,xisciita,", 00000, L"" }, // ISCII Tamil
{ NCP_8BIT|NCP_RECODE, 57005, "x-iscii-te,xisciite,", 00000, L"" }, // ISCII Telugu
{ NCP_8BIT|NCP_RECODE, 57006, "x-iscii-as,xisciias,", 00000, L"" }, // ISCII Assamese
{ NCP_8BIT|NCP_RECODE, 57007, "x-iscii-or,xisciior,", 00000, L"" }, // ISCII Oriya
{ NCP_8BIT|NCP_RECODE, 57008, "x-iscii-ka,xisciika,", 00000, L"" }, // ISCII Kannada
{ NCP_8BIT|NCP_RECODE, 57009, "x-iscii-ma,xisciima,", 00000, L"" }, // ISCII Malayalam
{ NCP_8BIT|NCP_RECODE, 57010, "x-iscii-gu,xisciigu,", 00000, L"" }, // ISCII Gujarathi
{ NCP_8BIT|NCP_RECODE, 57011, "x-iscii-pa,xisciipa,", 00000, L"" }, // ISCII Panjabi */
};
extern LPMRULIST mruFind;
extern LPMRULIST mruReplace;
//=============================================================================
//
// EditCreate()
//
HWND EditCreate(HWND hwndParent)
{
HWND hwnd;
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
L"Scintilla",
NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
0,0,0,0,
hwndParent,
(HMENU)IDC_EDIT,
g_hInstance,
NULL);
SendMessage(hwnd,SCI_SETCODEPAGE,iDefaultCodePage,0);
SendMessage(hwnd,SCI_SETEOLMODE,SC_EOL_CRLF,0);
SendMessage(hwnd,SCI_SETPASTECONVERTENDINGS,1,0);
SendMessage(hwnd,SCI_SETMODEVENTMASK,/*SC_MODEVENTMASKALL*/SC_MOD_INSERTTEXT|SC_MOD_DELETETEXT,0);
SendMessage(hwnd,SCI_USEPOPUP,FALSE,0);
SendMessage(hwnd,SCI_SETCARETPERIOD,GetCaretBlinkTime(),0);
SendMessage(hwnd,SCI_SETSCROLLWIDTH,2048,0);
SendMessage(hwnd,SCI_SETSCROLLWIDTHTRACKING,TRUE,0);
SendMessage(hwnd,SCI_SETENDATLASTLINE,TRUE,0);
SendMessage(hwnd,SCI_SETCARETSTICKY,FALSE,0);
SendMessage(hwnd,SCI_SETXCARETPOLICY,CARET_SLOP|CARET_EVEN,50);
SendMessage(hwnd,SCI_SETYCARETPOLICY,CARET_EVEN,0);
SendMessage(hwnd,SCI_SETBACKSPACEUNINDENTS,FALSE,0);
SendMessage(hwnd,SCI_SETMULTIPLESELECTION,FALSE,0);
SendMessage(hwnd,SCI_SETADDITIONALSELECTIONTYPING,FALSE,0);
SendMessage(hwnd,SCI_SETVIRTUALSPACEOPTIONS,SCVS_NONE,0);
SendMessage(hwnd,SCI_SETADDITIONALCARETSBLINK,FALSE,0);
SendMessage(hwnd,SCI_SETADDITIONALCARETSVISIBLE,FALSE,0);
SendMessage(hwnd,SCI_ASSIGNCMDKEY,(SCK_NEXT + (SCMOD_CTRL << 16)),SCI_PARADOWN);
SendMessage(hwnd,SCI_ASSIGNCMDKEY,(SCK_PRIOR + (SCMOD_CTRL << 16)),SCI_PARAUP);
SendMessage(hwnd,SCI_ASSIGNCMDKEY,(SCK_NEXT + ((SCMOD_CTRL | SCMOD_SHIFT) << 16)),SCI_PARADOWNEXTEND);
SendMessage(hwnd,SCI_ASSIGNCMDKEY,(SCK_PRIOR + ((SCMOD_CTRL | SCMOD_SHIFT) << 16)),SCI_PARAUPEXTEND);
SendMessage(hwnd,SCI_ASSIGNCMDKEY,(SCK_HOME + (0 << 16)),SCI_VCHOMEWRAP);
SendMessage(hwnd,SCI_ASSIGNCMDKEY,(SCK_END + (0 << 16)),SCI_LINEENDWRAP);
SendMessage(hwnd,SCI_ASSIGNCMDKEY,(SCK_HOME + (SCMOD_SHIFT << 16)),SCI_VCHOMEWRAPEXTEND);
SendMessage(hwnd,SCI_ASSIGNCMDKEY,(SCK_END + (SCMOD_SHIFT << 16)),SCI_LINEENDWRAPEXTEND);
// Init default values for printing
EditPrintInit();
//SciInitThemes(hwnd);
return(hwnd);
}
//=============================================================================
//
// EditSetNewText()
//
extern BOOL bFreezeAppTitle;
extern FILEVARS fvCurFile;
void EditSetNewText(HWND hwnd,char* lpstrText,DWORD cbText)
{
bFreezeAppTitle = TRUE;
if (SendMessage(hwnd,SCI_GETREADONLY,0,0))
SendMessage(hwnd,SCI_SETREADONLY,FALSE,0);
SendMessage(hwnd,SCI_CANCEL,0,0);
SendMessage(hwnd,SCI_SETUNDOCOLLECTION,0,0);
SendMessage(hwnd,SCI_EMPTYUNDOBUFFER,0,0);
SendMessage(hwnd,SCI_CLEARALL,0,0);
SendMessage(hwnd,SCI_MARKERDELETEALL,(WPARAM)-1,0);
SendMessage(hwnd,SCI_SETSCROLLWIDTH,2048,0);
SendMessage(hwnd,SCI_SETXOFFSET,0,0);
FileVars_Apply(hwnd,&fvCurFile);
if (cbText > 0)
SendMessage(hwnd,SCI_ADDTEXT,cbText,(LPARAM)lpstrText);
SendMessage(hwnd,SCI_SETUNDOCOLLECTION,1,0);
SendMessage(hwnd,EM_EMPTYUNDOBUFFER,0,0);
SendMessage(hwnd,SCI_SETSAVEPOINT,0,0);
SendMessage(hwnd,SCI_GOTOPOS,0,0);
SendMessage(hwnd,SCI_CHOOSECARETX,0,0);
bFreezeAppTitle = FALSE;
}
//=============================================================================
//
// EditConvertText()
//
BOOL EditConvertText(HWND hwnd,UINT cpSource,UINT cpDest,BOOL bSetSavePoint)
{
struct TextRange tr = { { 0, -1 }, NULL };
int length, cbText, cbwText;
char *pchText;
WCHAR *pwchText;
if (cpSource == cpDest)
return(TRUE);
length = SendMessage(hwnd,SCI_GETLENGTH,0,0);
if (length == 0) {
SendMessage(hwnd,SCI_CANCEL,0,0);
SendMessage(hwnd,SCI_SETUNDOCOLLECTION,0,0);
SendMessage(hwnd,SCI_EMPTYUNDOBUFFER,0,0);
SendMessage(hwnd,SCI_CLEARALL,0,0);
SendMessage(hwnd,SCI_MARKERDELETEALL,(WPARAM)-1,0);
SendMessage(hwnd,SCI_SETCODEPAGE,cpDest,0);
SendMessage(hwnd,SCI_SETUNDOCOLLECTION,1,0);
SendMessage(hwnd,EM_EMPTYUNDOBUFFER,0,0);
SendMessage(hwnd,SCI_GOTOPOS,0,0);
SendMessage(hwnd,SCI_CHOOSECARETX,0,0);
if (bSetSavePoint)
SendMessage(hwnd,SCI_SETSAVEPOINT,0,0);
}
else {
pchText = GlobalAlloc(GPTR,length*5+2);
tr.lpstrText = pchText;
SendMessage(hwnd,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
pwchText = GlobalAlloc(GPTR,length*3+2);
cbwText = MultiByteToWideChar(cpSource,0,pchText,length,pwchText,length*3+2);
cbText = WideCharToMultiByte(cpDest,0,pwchText,cbwText,pchText,length*5+2,NULL,NULL);
SendMessage(hwnd,SCI_CANCEL,0,0);
SendMessage(hwnd,SCI_SETUNDOCOLLECTION,0,0);
SendMessage(hwnd,SCI_EMPTYUNDOBUFFER,0,0);
SendMessage(hwnd,SCI_CLEARALL,0,0);
SendMessage(hwnd,SCI_MARKERDELETEALL,(WPARAM)-1,0);
SendMessage(hwnd,SCI_SETCODEPAGE,cpDest,0);
SendMessage(hwnd,SCI_ADDTEXT,cbText,(LPARAM)pchText);
SendMessage(hwnd,SCI_EMPTYUNDOBUFFER,0,0);
SendMessage(hwnd,SCI_SETUNDOCOLLECTION,1,0);
SendMessage(hwnd,SCI_GOTOPOS,0,0);
SendMessage(hwnd,SCI_CHOOSECARETX,0,0);
GlobalFree(pchText);
GlobalFree(pwchText);
}
return(TRUE);
}
//=============================================================================
//
// EditSetNewEncoding()
//
BOOL EditSetNewEncoding(HWND hwnd,int iCurrentEncoding,int iNewEncoding,BOOL bNoUI,BOOL bSetSavePoint) {
if (iCurrentEncoding != iNewEncoding) {
if ((iCurrentEncoding == CPI_DEFAULT && iNewEncoding == CPI_DEFAULT) ||
(iCurrentEncoding != CPI_DEFAULT && iNewEncoding != CPI_DEFAULT))
return(TRUE);
if (SendMessage(hwnd,SCI_GETLENGTH,0,0) == 0) {
BOOL bIsEmptyUndoHistory =
(SendMessage(hwnd,SCI_CANUNDO,0,0) == 0 && SendMessage(hwnd,SCI_CANREDO,0,0) == 0);
if ((iCurrentEncoding == CPI_DEFAULT || iNewEncoding == CPI_DEFAULT) &&
(bNoUI || bIsEmptyUndoHistory || InfoBox(MBYESNO,L"MsgConv2",IDS_ASK_ENCODING2) == IDYES)) {
EditConvertText(hwnd,
(mEncoding[iCurrentEncoding].uFlags & NCP_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,
(mEncoding[iNewEncoding].uFlags & NCP_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,
bSetSavePoint);
return(TRUE);
}
else
return(FALSE);
}
else if ((iCurrentEncoding == CPI_DEFAULT || iNewEncoding == CPI_DEFAULT) &&
(bNoUI || InfoBox(MBYESNO,L"MsgConv1",IDS_ASK_ENCODING) == IDYES)) {
BeginWaitCursor();
EditConvertText(hwnd,
(mEncoding[iCurrentEncoding].uFlags & NCP_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,
(mEncoding[iNewEncoding].uFlags & NCP_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,
FALSE);
EndWaitCursor();
return(TRUE);
}
else
return(FALSE);
}
else
return(FALSE);
}
//=============================================================================
//
// EditGetClipboardText()
//
char* EditGetClipboardText(HWND hwnd)
{
HANDLE hmem;
WCHAR *pwch;
char *pmch;
char *ptmp;
int wlen, mlen, mlen2;
int codepage;
int eolmode;
if (!IsClipboardFormatAvailable(CF_UNICODETEXT) || !OpenClipboard(GetParent(hwnd)))
return(NULL);
hmem = GetClipboardData(CF_UNICODETEXT);
pwch = GlobalLock(hmem);
wlen = lstrlenW(pwch);
codepage = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
eolmode = SendMessage(hwnd,SCI_GETEOLMODE,0,0);
mlen = WideCharToMultiByte(codepage,0,pwch,wlen + 1,NULL,0,0,0) - 1;
pmch = LocalAlloc(LPTR,mlen + 1);
if (pmch)
WideCharToMultiByte(codepage,0,pwch,wlen + 1,pmch,mlen + 1,NULL,NULL);
ptmp = LocalAlloc(LPTR,mlen * 2 + 1);
if (ptmp) {
char *s = pmch;
char *d = ptmp;
int i;
for (i = 0; (i < mlen) && (*s != 0); i++) {
if (*s == '\n' || *s == '\r') {
if (eolmode == SC_EOL_CR) {
*d++ = '\r';
} else if (eolmode == SC_EOL_LF) {
*d++ = '\n';
} else { // eolmode == SC_EOL_CRLF
*d++ = '\r';
*d++ = '\n';
}
if ((*s == '\r') && (i+1 < mlen) && (*(s+1) == '\n')) {
i++;
s++;
}
s++;
} else {
*d++ = *s++;
}
}
*d++ = 0;
mlen2 = (d - ptmp) - 1;
LocalFree(pmch);
pmch = LocalAlloc(LPTR,mlen2 + 1);
lstrcpyA(pmch,ptmp);
LocalFree(ptmp);
}
GlobalUnlock(hmem);
CloseClipboard();
return(pmch);
}
//=============================================================================
//
// EditCopyAppend()
//
BOOL EditCopyAppend(HWND hwnd)
{
HANDLE hOld;
WCHAR *pszOld;
HANDLE hNew;
WCHAR *pszNew;
char *pszText;
int cchTextW;
WCHAR *pszTextW;
WCHAR *pszSep = L"\r\n\r\n";
UINT uCodePage;
int iCurPos;
int iAnchorPos;
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) {
SendMessage(hwnd,SCI_COPY,0,0);
return(TRUE);
}
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos != iAnchorPos) {
if (SC_SEL_RECTANGLE == SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
MsgBox(MBINFO,IDS_SELRECT);
return(FALSE);
}
else {
int iSelCount =
SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) -
SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
pszText = LocalAlloc(LPTR,iSelCount + 1);
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszText);
}
}
else {
int cchText = SendMessage(hwnd,SCI_GETLENGTH,0,0);
pszText = LocalAlloc(LPTR,cchText + 1);
SendMessage(hwnd,SCI_GETTEXT,LocalSize(pszText),(LPARAM)pszText);
}
uCodePage = (SendMessage(hwnd,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8) ? CP_UTF8 : CP_ACP;
cchTextW = MultiByteToWideChar(uCodePage,0,pszText,-1,NULL,0);
if (cchTextW > 0) {
pszTextW = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(pszSep) + cchTextW + 1));
lstrcpy(pszTextW,pszSep);
MultiByteToWideChar(uCodePage,0,pszText,-1,StrEnd(pszTextW),LocalSize(pszTextW)/sizeof(WCHAR));
}
else {
pszTextW = L"";
}
LocalFree(pszText);
if (!OpenClipboard(GetParent(hwnd))) {
LocalFree(pszTextW);
return(FALSE);
}
hOld = GetClipboardData(CF_UNICODETEXT);
pszOld = GlobalLock(hOld);
hNew = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,
sizeof(WCHAR) * (lstrlen(pszOld) + lstrlen(pszTextW) + 1));
pszNew = GlobalLock(hNew);
lstrcpy(pszNew,pszOld);
lstrcat(pszNew,pszTextW);
GlobalUnlock(hNew);
GlobalUnlock(hOld);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT,hNew);
CloseClipboard();
return(TRUE);
}
//=============================================================================
//
// EditDetectEOLMode() - moved here to handle Unicode files correctly
//
int EditDetectEOLMode(HWND hwnd,char* lpData,DWORD cbData)
{
int iEOLMode = iLineEndings[iDefaultEOLMode];
char *cp = (char*)lpData;
if (!cp)
return (iEOLMode);
while (*cp && (*cp != '\x0D' && *cp != '\x0A')) cp++;
if (*cp == '\x0D' && *(cp+1) == '\x0A')
iEOLMode = SC_EOL_CRLF;
else if (*cp == '\x0D' && *(cp+1) != '\x0A')
iEOLMode = SC_EOL_CR;
else if (*cp == '\x0A')
iEOLMode = SC_EOL_LF;
return (iEOLMode);
}
//=============================================================================
//
// Encoding Helper Functions
//
void Encoding_InitDefaults() {
wsprintf(wchANSI,L" (%i)",GetACP());
mEncoding[CPI_OEM].uCodePage = GetOEMCP();
wsprintf(wchOEM,L" (%i)",mEncoding[CPI_OEM].uCodePage);
}
int Encoding_MapIniSetting(BOOL bLoad,int iSetting) {
if (bLoad) {
switch (iSetting) {
case 0: return CPI_DEFAULT;
case 1: return CPI_UNICODEBOM;
case 2: return CPI_UNICODEBEBOM;
case 3: return CPI_UTF8;
case 4: return CPI_UTF8SIGN;
case 5: return CPI_OEM;
case 6: return CPI_UNICODE;
case 7: return CPI_UNICODEBE;
case 8: return CPI_UTF7;
default: {
int i;
for (i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); i++) {
if (mEncoding[i].uCodePage == iSetting && Encoding_IsValid(i))
return(i);
}
return CPI_DEFAULT;
}
}
}
else {
switch (iSetting) {
case CPI_DEFAULT: return 0;
case CPI_UNICODEBOM: return 1;
case CPI_UNICODEBEBOM: return 2;
case CPI_UTF8: return 3;
case CPI_UTF8SIGN: return 4;
case CPI_OEM: return 5;
case CPI_UNICODE: return 6;
case CPI_UNICODEBE: return 7;
case CPI_UTF7: return 8;
default: return(mEncoding[iSetting].uCodePage);
}
}
}
void Encoding_GetLabel(int iEncoding) {
if (mEncoding[iEncoding].wchLabel[0] == 0) {
WCHAR *pwsz;
WCHAR wch[256] = L"";
GetString(mEncoding[iEncoding].idsName,wch,COUNTOF(wch));
if (pwsz = StrChr(wch,L';')) {
if (pwsz = StrChr(CharNext(pwsz),L';')) {
pwsz = CharNext(pwsz);
}
}
if (!pwsz)
pwsz = wch;
StrCpyN(mEncoding[iEncoding].wchLabel,pwsz,COUNTOF(mEncoding[iEncoding].wchLabel));
}
}
int Encoding_MatchW(LPCWSTR pwszTest) {
char tchTest[256];
WideCharToMultiByte(CP_ACP,0,pwszTest,-1,tchTest,COUNTOF(tchTest),NULL,NULL);
return(Encoding_MatchA(tchTest));
}
int Encoding_MatchA(char *pchTest) {
int i;
char chTest[256];
char *pchSrc = pchTest;
char *pchDst = chTest;
*pchDst++ = ',';
while (*pchSrc) {
if (IsCharAlphaNumericA(*pchSrc))
*pchDst++ = *CharLowerA(pchSrc);
pchSrc++;
}
*pchDst++ = ',';
*pchDst = 0;
for (i = 0; i < COUNTOF(mEncoding); i++) {
if (StrStrIA(mEncoding[i].pszParseNames,chTest)) {
CPINFO cpi;
if ((mEncoding[i].uFlags & NCP_INTERNAL) ||
IsValidCodePage(mEncoding[i].uCodePage) &&
GetCPInfo(mEncoding[i].uCodePage,&cpi))
return(i);
else
return(-1);
}
}
return(-1);
}
BOOL Encoding_IsValid(int iTestEncoding) {
CPINFO cpi;
if (iTestEncoding >= 0 &&
iTestEncoding < COUNTOF(mEncoding)) {
if ((mEncoding[iTestEncoding].uFlags & NCP_INTERNAL) ||
IsValidCodePage(mEncoding[iTestEncoding].uCodePage) &&
GetCPInfo(mEncoding[iTestEncoding].uCodePage,&cpi)) {
return(TRUE);
}
}
return(FALSE);
}
typedef struct _ee {
int id;
WCHAR wch[256];
} ENCODINGENTRY, *PENCODINGENTRY;
int CmpEncoding(const void *s1, const void *s2) {
return StrCmp(((PENCODINGENTRY)s1)->wch,((PENCODINGENTRY)s2)->wch);
}
void Encoding_AddToListView(HWND hwnd,int idSel,BOOL bRecodeOnly)
{
int i;
int iSelItem = -1;
LVITEM lvi;
WCHAR wchBuf[256];
PENCODINGENTRY pEE = LocalAlloc(LPTR,COUNTOF(mEncoding) * sizeof(ENCODINGENTRY));
for (i = 0; i < COUNTOF(mEncoding); i++) {
pEE[i].id = i;
GetString(mEncoding[i].idsName,pEE[i].wch,COUNTOF(pEE[i].wch));
}
qsort(pEE,COUNTOF(mEncoding),sizeof(ENCODINGENTRY),CmpEncoding);
ZeroMemory(&lvi,sizeof(LVITEM));
lvi.mask = LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE;
lvi.pszText = wchBuf;
for (i = 0; i < COUNTOF(mEncoding); i++) {
int id = pEE[i].id;
if (!bRecodeOnly || (mEncoding[id].uFlags & NCP_RECODE)) {
CPINFO cpi;
WCHAR *pwsz;
lvi.iItem = ListView_GetItemCount(hwnd);
if (pwsz = StrChr(pEE[i].wch,L';')) {
StrCpyN(wchBuf,CharNext(pwsz),COUNTOF(wchBuf));
if (pwsz = StrChr(wchBuf,L';'))
*pwsz = 0;
}
else
StrCpyN(wchBuf,pEE[i].wch,COUNTOF(wchBuf));
if (id == CPI_DEFAULT)
StrCatN(wchBuf,wchANSI,COUNTOF(wchBuf));
else if (id == CPI_OEM)
StrCatN(wchBuf,wchOEM,COUNTOF(wchBuf));
if ((mEncoding[id].uFlags & NCP_INTERNAL) ||
(IsValidCodePage(mEncoding[id].uCodePage) &&
GetCPInfo(mEncoding[id].uCodePage,&cpi)))
lvi.iImage = 0;
else
lvi.iImage = 1;
lvi.lParam = (LPARAM)id;
ListView_InsertItem(hwnd,&lvi);
if (idSel == id)
iSelItem = lvi.iItem;
}
}
LocalFree(pEE);
if (iSelItem != -1) {
ListView_SetItemState(hwnd,iSelItem,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
ListView_EnsureVisible(hwnd,iSelItem,FALSE);
}
else {
ListView_SetItemState(hwnd,0,LVIS_FOCUSED,LVIS_FOCUSED);
ListView_EnsureVisible(hwnd,0,FALSE);
}
}
BOOL Encoding_GetFromListView(HWND hwnd,int *pidEncoding)
{
LVITEM lvi;
lvi.iItem = ListView_GetNextItem(hwnd,-1,LVNI_ALL | LVNI_SELECTED);
lvi.iSubItem = 0;
lvi.mask = LVIF_PARAM;
if (ListView_GetItem(hwnd,&lvi)) {
if (Encoding_IsValid(lvi.lParam)) {
*pidEncoding = lvi.lParam;
return (TRUE);
}
else
MsgBox(MBINFO,IDS_ERR_ENCODINGNA);
}
return(FALSE);
}
void Encoding_AddToComboboxEx(HWND hwnd,int idSel,BOOL bRecodeOnly)
{
int i;
int iSelItem = -1;
COMBOBOXEXITEM cbei;
WCHAR wchBuf[256];
PENCODINGENTRY pEE = LocalAlloc(LPTR,COUNTOF(mEncoding) * sizeof(ENCODINGENTRY));
for (i = 0; i < COUNTOF(mEncoding); i++) {
pEE[i].id = i;
GetString(mEncoding[i].idsName,pEE[i].wch,COUNTOF(pEE[i].wch));
}
qsort(pEE,COUNTOF(mEncoding),sizeof(ENCODINGENTRY),CmpEncoding);
ZeroMemory(&cbei,sizeof(COMBOBOXEXITEM));
cbei.mask = CBEIF_TEXT | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE | CBEIF_LPARAM;
cbei.pszText = wchBuf;
cbei.cchTextMax = COUNTOF(wchBuf);
cbei.iImage = 0;
cbei.iSelectedImage = 0;
for (i = 0; i < COUNTOF(mEncoding); i++) {
int id = pEE[i].id;
if (!bRecodeOnly || (mEncoding[id].uFlags & NCP_RECODE)) {
CPINFO cpi;
WCHAR *pwsz;
cbei.iItem = SendMessage(hwnd,CB_GETCOUNT,0,0);
if (pwsz = StrChr(pEE[i].wch,L';')) {
StrCpyN(wchBuf,CharNext(pwsz),COUNTOF(wchBuf));
if (pwsz = StrChr(wchBuf,L';'))
*pwsz = 0;
}
else
StrCpyN(wchBuf,pEE[i].wch,COUNTOF(wchBuf));
if (id == CPI_DEFAULT)
StrCatN(wchBuf,wchANSI,COUNTOF(wchBuf));
else if (id == CPI_OEM)
StrCatN(wchBuf,wchOEM,COUNTOF(wchBuf));
if ((mEncoding[id].uFlags & NCP_INTERNAL) ||
(IsValidCodePage(mEncoding[id].uCodePage) &&
GetCPInfo(mEncoding[id].uCodePage,&cpi)))
cbei.iImage = 0;
else
cbei.iImage = 1;
cbei.lParam = (LPARAM)id;
SendMessage(hwnd,CBEM_INSERTITEM,0,(LPARAM)&cbei);
if (idSel == id)
iSelItem = cbei.iItem;
}
}
LocalFree(pEE);
if (iSelItem != -1)
SendMessage(hwnd,CB_SETCURSEL,(WPARAM)iSelItem,0);
}
BOOL Encoding_GetFromComboboxEx(HWND hwnd,int *pidEncoding)
{
COMBOBOXEXITEM cbei;
cbei.iItem = SendMessage(hwnd,CB_GETCURSEL,0,0);
cbei.mask = CBEIF_LPARAM;
if (SendMessage(hwnd,CBEM_GETITEM,0,(LPARAM)&cbei)) {
if (Encoding_IsValid(cbei.lParam)) {
*pidEncoding = cbei.lParam;
return (TRUE);
}
else
MsgBox(MBINFO,IDS_ERR_ENCODINGNA);
}
return(FALSE);
}
BOOL IsUnicode(const char* pBuffer,int cb,LPBOOL lpbBOM,LPBOOL lpbReverse)
{
int i = 0xFFFF;
BOOL bIsTextUnicode;
BOOL bHasBOM;
BOOL bHasRBOM;
if (!pBuffer || cb < 2)
return FALSE;
if (!bSkipUnicodeDetection)
bIsTextUnicode = IsTextUnicode(pBuffer,cb,&i);
else
bIsTextUnicode = FALSE;
bHasBOM = (*pBuffer == '\xFF' && *(pBuffer+1) == '\xFE');
bHasRBOM = (*pBuffer == '\xFE' && *(pBuffer+1) == '\xFF');
/*{
char szBuf[512];
wsprintfA(szBuf,
"IS_TEXT_UNICODE_ASCII16\t\t\t%s\n"\
"IS_TEXT_UNICODE_REVERSE_ASCII16\t\t%s\n"\
"IS_TEXT_UNICODE_STATISTICS\t\t%s\n"\
"IS_TEXT_UNICODE_REVERSE_STATISTICS\t%s\n"\
"IS_TEXT_UNICODE_CONTROLS\t\t%s\n"\
"IS_TEXT_UNICODE_REVERSE_CONTROLS\t%s\n"\
"IS_TEXT_UNICODE_SIGNATURE\t\t%s\n"\
"IS_TEXT_UNICODE_REVERSE_SIGNATURE\t%s\n"\
"IS_TEXT_UNICODE_ILLEGAL_CHARS\t\t%s\n"\
"IS_TEXT_UNICODE_ODD_LENGTH\t\t%s\n"\
"IS_TEXT_UNICODE_DBCS_LEADBYTE\t\t%s\n"\
"IS_TEXT_UNICODE_NULL_BYTES\t\t%s\n\n"\
"IsTextUnicode()\t\t\t\t%i\n\n"\
"HasBOM\t\t\t\t\t%i\n"\
"HasRBOM\t\t\t\t\t%i",
i & IS_TEXT_UNICODE_ASCII16 ? "1" :"-",
i & IS_TEXT_UNICODE_REVERSE_ASCII16 ? "1" :"-",
i & IS_TEXT_UNICODE_STATISTICS ? "1" :"-",
i & IS_TEXT_UNICODE_REVERSE_STATISTICS ? "1" :"-",
i & IS_TEXT_UNICODE_CONTROLS ? "1" :"-",
i & IS_TEXT_UNICODE_REVERSE_CONTROLS ? "1" :"-",
i & IS_TEXT_UNICODE_SIGNATURE ? "1" :"-",
i & IS_TEXT_UNICODE_REVERSE_SIGNATURE ? "1" :"-",
i & IS_TEXT_UNICODE_ILLEGAL_CHARS ? "1" :"-",
i & IS_TEXT_UNICODE_ODD_LENGTH ? "1" :"-",
i & IS_TEXT_UNICODE_DBCS_LEADBYTE ? "1" :"-",
i & IS_TEXT_UNICODE_NULL_BYTES ? "1" :"-",
bIsTextUnicode,bHasBOM,bHasRBOM);
MessageBoxA(NULL,szBuf,"IsTextUnicode()",0);
}*/
if (i == 0xFFFF) // i doesn't seem to have been modified ...
i = 0;
if (bIsTextUnicode || bHasBOM || bHasRBOM ||
((i & (IS_TEXT_UNICODE_UNICODE_MASK | IS_TEXT_UNICODE_REVERSE_MASK)) &&
!((i & IS_TEXT_UNICODE_UNICODE_MASK) && (i & IS_TEXT_UNICODE_REVERSE_MASK)) &&
!(i & IS_TEXT_UNICODE_ODD_LENGTH) &&
!(i & IS_TEXT_UNICODE_ILLEGAL_CHARS && !(i & IS_TEXT_UNICODE_REVERSE_SIGNATURE)) &&
!((i & IS_TEXT_UNICODE_REVERSE_MASK) == IS_TEXT_UNICODE_REVERSE_STATISTICS))) {
if (lpbBOM)
*lpbBOM = (bHasBOM || bHasRBOM ||
(i & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE)))
? TRUE : FALSE;
if (lpbReverse)
*lpbReverse = (bHasRBOM || (i & IS_TEXT_UNICODE_REVERSE_MASK)) ? TRUE : FALSE;
return TRUE;
}
else
return FALSE;
}
BOOL IsUTF8(const char* pTest,int nLength)
{
static int byte_class_table[256] = {
/* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F */
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 80 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 90 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
/* A0 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
/* B0 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
/* C0 */ 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
/* D0 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
/* E0 */ 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 7,
/* F0 */ 9,10,10,10,11, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
/* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F */ };
/* state table */
typedef enum {
kSTART = 0,kA,kB,kC,kD,kE,kF,kG,kERROR,kNumOfStates } utf8_state;
static utf8_state state_table[] = {
/* kSTART, kA, kB, kC, kD, kE, kF, kG, kERROR */
/* 0x00-0x7F: 0 */ kSTART, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR,
/* 0x80-0x8F: 1 */ kERROR, kSTART, kA, kERROR, kA, kB, kERROR, kB, kERROR,
/* 0x90-0x9f: 2 */ kERROR, kSTART, kA, kERROR, kA, kB, kB, kERROR, kERROR,
/* 0xa0-0xbf: 3 */ kERROR, kSTART, kA, kA, kERROR, kB, kB, kERROR, kERROR,
/* 0xc0-0xc1, 0xf5-0xff: 4 */ kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR,
/* 0xc2-0xdf: 5 */ kA, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR,
/* 0xe0: 6 */ kC, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR,
/* 0xe1-0xec, 0xee-0xef: 7 */ kB, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR,
/* 0xed: 8 */ kD, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR,
/* 0xf0: 9 */ kF, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR,
/* 0xf1-0xf3: 10 */ kE, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR,
/* 0xf4: 11 */ kG, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR };
#define BYTE_CLASS(b) (byte_class_table[(unsigned char)b])
#define NEXT_STATE(b,cur) (state_table[(BYTE_CLASS(b) * kNumOfStates) + (cur)])
utf8_state current = kSTART;
int i;
const char* pt = pTest;
int len = nLength;
for(i = 0; i < len ; i++, pt++) {
current = NEXT_STATE(*pt,current);
if (kERROR == current)
break;
}
return (current == kSTART) ? TRUE : FALSE;
}
BOOL IsUTF7(const char* pTest,int nLength)
{
int i;
const char *pt = pTest;
for (i = 0; i < nLength; i++) {
if (*pt & 0x80 || !*pt)
return FALSE;
pt++;
}
return TRUE;
}
#define IsUTF8Signature(p) \
((*(p+0) == '\xEF' && *(p+1) == '\xBB' && *(p+2) == '\xBF'))
#define UTF8StringStart(p) \
(IsUTF8Signature(p)) ? (p+3) : (p)
/* byte length of UTF-8 sequence based on value of first byte.
for UTF-16 (21-bit space), max. code length is 4, so we only need to look
at 4 upper bits.
*/
static const INT utf8_lengths[16]=
{
1,1,1,1,1,1,1,1, /* 0000 to 0111 : 1 byte (plain ASCII) */
0,0,0,0, /* 1000 to 1011 : not valid */
2,2, /* 1100, 1101 : 2 bytes */
3, /* 1110 : 3 bytes */
4 /* 1111 :4 bytes */
};
/*++
Function :
UTF8_mbslen_bytes [INTERNAL]
Calculates the byte size of a NULL-terminated UTF-8 string.
Parameters :
char *utf8_string : string to examine
Return value :
size (in bytes) of a NULL-terminated UTF-8 string.
-1 if invalid NULL-terminated UTF-8 string
--*/
static INT UTF8_mbslen_bytes(LPCSTR utf8_string)
{
INT length=0;
INT code_size;
BYTE byte;
while(*utf8_string)
{
byte=(BYTE)*utf8_string;
if( (byte <= 0xF7) && (0 != (code_size = utf8_lengths[ byte >> 4 ])))
{
length+=code_size;
utf8_string+=code_size;
}
else
{
/* we got an invalid byte value but need to count it,
it will be later ignored during the string conversion */
//WARN("invalid first byte value 0x%02X in UTF-8 sequence!\n",byte);
length++;
utf8_string++;
}
}
length++; /* include NULL terminator */
return length;
}
/*++
Function :
UTF8_mbslen [INTERNAL]
Calculates the character size of a NULL-terminated UTF-8 string.
Parameters :
char *utf8_string : string to examine
int byte_length : byte size of string
Return value :
size (in characters) of a UTF-8 string.
-1 if invalid UTF-8 string
--*/
static INT UTF8_mbslen(LPCSTR source, INT byte_length)
{
INT wchar_length=0;
INT code_size;
BYTE byte;
while(byte_length > 0)
{
byte=(BYTE)*source;
/* UTF-16 can't encode 5-byte and 6-byte sequences, so maximum value
for first byte is 11110111. Use lookup table to determine sequence
length based on upper 4 bits of first byte */
if ((byte <= 0xF7) && (0 != (code_size=utf8_lengths[ byte >> 4])))
{
/* 1 sequence == 1 character */
wchar_length++;
if(code_size==4)
wchar_length++;
source+=code_size; /* increment pointer */
byte_length-=code_size; /* decrement counter*/
}
else
{
/*
unlike UTF8_mbslen_bytes, we ignore the invalid characters.
we only report the number of valid characters we have encountered
to match the Windows behavior.
*/
//WARN("invalid byte 0x%02X in UTF-8 sequence, skipping it!\n",
// byte);
source++;
byte_length--;
}
}
return wchar_length;
}
//=============================================================================
//
// EditLoadFile()
//
BOOL EditLoadFile(
HWND hwnd,
LPCWSTR pszFile,
BOOL bSkipEncodingDetection,
int* iEncoding,
int* iEOLMode,
BOOL *pbUnicodeErr,
BOOL *pbFileTooBig)
{
HANDLE hFile;
DWORD dwFileSize;
DWORD dwFileSizeLimit;
DWORD dwBufSize;
BOOL bReadSuccess;
char* lpData;
DWORD cbData;
//char *cp;
int _iDefaultEncoding;
BOOL bBOM;
BOOL bReverse;
*pbUnicodeErr = FALSE;
*pbFileTooBig = FALSE;
hFile = CreateFile(pszFile,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
dwLastIOError = GetLastError();
if (hFile == INVALID_HANDLE_VALUE) {
iSrcEncoding = -1;
iWeakSrcEncoding = -1;
return FALSE;
}
// calculate buffer limit
dwFileSize = GetFileSize(hFile,NULL);
dwBufSize = dwFileSize + 10;
// Check if a warning message should be displayed for large files
dwFileSizeLimit = IniGetInt(L"Settings2",L"FileLoadWarningMB",1);
if (dwFileSizeLimit != 0 && dwFileSizeLimit * 1024 * 1024 < dwFileSize) {
if (InfoBox(MBYESNO,L"MsgFileSizeWarning",IDS_WARNLOADBIGFILE) != IDYES) {
CloseHandle(hFile);
*pbFileTooBig = TRUE;
iSrcEncoding = -1;
iWeakSrcEncoding = -1;
return FALSE;
}
}
lpData = GlobalAlloc(GPTR,dwBufSize);
bReadSuccess = ReadFile(hFile,lpData,GlobalSize(lpData)-2,&cbData,NULL);
dwLastIOError = GetLastError();
CloseHandle(hFile);
if (!bReadSuccess) {
GlobalFree(lpData);
iSrcEncoding = -1;
iWeakSrcEncoding = -1;
return FALSE;
}
if (!Encoding_IsValid(iDefaultEncoding))
iDefaultEncoding = CPI_DEFAULT;
_iDefaultEncoding = iDefaultEncoding;
if (iWeakSrcEncoding != -1 && Encoding_IsValid(iWeakSrcEncoding))
_iDefaultEncoding = iWeakSrcEncoding;
*iEncoding = CPI_DEFAULT;
if (cbData == 0) {
FileVars_Init(NULL,0,&fvCurFile);
*iEOLMode = iLineEndings[iDefaultEOLMode];
if (iSrcEncoding == -1) {
if (bLoadASCIIasUTF8)
*iEncoding = CPI_UTF8;
else
*iEncoding = _iDefaultEncoding;
}
else
*iEncoding = iSrcEncoding;
SendMessage(hwnd,SCI_SETCODEPAGE,(mEncoding[*iEncoding].uFlags & NCP_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8,0);
EditSetNewText(hwnd,"",0);
SendMessage(hwnd,SCI_SETEOLMODE,iLineEndings[iDefaultEOLMode],0);
GlobalFree(lpData);
}
else if (!bSkipEncodingDetection && iSrcEncoding == -1 &&
IsUnicode(lpData,cbData,&bBOM,&bReverse) && !IsUTF8Signature(lpData)) // check for UTF-8 signature
{
char* lpDataUTF8;
if (bReverse) {
_swab(lpData,lpData,cbData);
if (bBOM)
*iEncoding = CPI_UNICODEBEBOM;
else
*iEncoding = CPI_UNICODEBE;
}
else {
if (bBOM)
*iEncoding = CPI_UNICODEBOM;
else
*iEncoding = CPI_UNICODE;
}
lpDataUTF8 = GlobalAlloc(GPTR,(cbData * 3) + 2);
cbData = WideCharToMultiByte(CP_UTF8,0,(bBOM) ? (LPWSTR)lpData + 1 : (LPWSTR)lpData,
(bBOM) ? (cbData)/sizeof(WCHAR) : cbData/sizeof(WCHAR) + 1,lpDataUTF8,GlobalSize(lpDataUTF8),NULL,NULL);
if (cbData == 0) {
cbData = WideCharToMultiByte(CP_ACP,0,(bBOM) ? (LPWSTR)lpData + 1 : (LPWSTR)lpData,
(-1),lpDataUTF8,GlobalSize(lpDataUTF8),NULL,NULL);
*pbUnicodeErr = TRUE;
}
GlobalFree(lpData);
SendMessage(hwnd,SCI_SETCODEPAGE,SC_CP_UTF8,0);
EditSetNewText(hwnd,"",0);
FileVars_Init(lpDataUTF8,cbData-1,&fvCurFile);
EditSetNewText(hwnd,lpDataUTF8,cbData-1);
*iEOLMode = EditDetectEOLMode(hwnd,lpDataUTF8,cbData-1);
GlobalFree(lpDataUTF8);
}
else {
FileVars_Init(lpData,cbData,&fvCurFile);
if (!bSkipEncodingDetection && (iSrcEncoding == -1 || iSrcEncoding == CPI_UTF8 || iSrcEncoding == CPI_UTF8SIGN) &&
((IsUTF8Signature(lpData) ||
FileVars_IsUTF8(&fvCurFile) ||
(iSrcEncoding == CPI_UTF8 || iSrcEncoding == CPI_UTF8SIGN) ||
(IsUTF8(lpData,cbData) &&
(((UTF8_mbslen_bytes(UTF8StringStart(lpData)) - 1 !=
UTF8_mbslen(UTF8StringStart(lpData),IsUTF8Signature(lpData) ? cbData-3 : cbData)) ||
mEncoding[_iDefaultEncoding].uFlags & NCP_UTF8 ||
bLoadASCIIasUTF8))))) && !(FileVars_IsNonUTF8(&fvCurFile) &&
(iSrcEncoding != CPI_UTF8 && iSrcEncoding != CPI_UTF8SIGN)))
{
SendMessage(hwnd,SCI_SETCODEPAGE,SC_CP_UTF8,0);
EditSetNewText(hwnd,"",0);
if (IsUTF8Signature(lpData)) {
EditSetNewText(hwnd,UTF8StringStart(lpData),cbData-3);
*iEncoding = CPI_UTF8SIGN;
*iEOLMode = EditDetectEOLMode(hwnd,UTF8StringStart(lpData),cbData-3);
}
else {
EditSetNewText(hwnd,lpData,cbData);
*iEncoding = CPI_UTF8;
*iEOLMode = EditDetectEOLMode(hwnd,lpData,cbData);
}
GlobalFree(lpData);
}
else {
UINT uCodePage = CP_UTF8;
LPWSTR lpDataWide;
int cbDataWide;
if (iSrcEncoding != -1)
*iEncoding = iSrcEncoding;
else {
*iEncoding = FileVars_GetEncoding(&fvCurFile);
if (*iEncoding == -1) {
if (fvCurFile.mask & FV_ENCODING)
*iEncoding = CPI_DEFAULT;
else {
if (iWeakSrcEncoding == -1)
*iEncoding = _iDefaultEncoding;
else if (mEncoding[iWeakSrcEncoding].uFlags & NCP_INTERNAL)
*iEncoding = iDefaultEncoding;
else
*iEncoding = _iDefaultEncoding;
}
}
}
if ((mEncoding[*iEncoding].uFlags & NCP_8BIT && mEncoding[*iEncoding].uCodePage != CP_UTF7) ||
(mEncoding[*iEncoding].uCodePage == CP_UTF7 && IsUTF7(lpData,cbData))) {
uCodePage = mEncoding[*iEncoding].uCodePage;
lpDataWide = GlobalAlloc(GPTR,cbData * 2 + 16);
cbDataWide = MultiByteToWideChar(uCodePage,0,lpData,cbData,lpDataWide,GlobalSize(lpDataWide)/sizeof(WCHAR));
GlobalFree(lpData);
lpData = GlobalAlloc(GPTR,cbDataWide * 3 + 16);
cbData = WideCharToMultiByte(CP_UTF8,0,lpDataWide,cbDataWide,lpData,GlobalSize(lpData),NULL,NULL);
GlobalFree(lpDataWide);
SendMessage(hwnd,SCI_SETCODEPAGE,SC_CP_UTF8,0);
EditSetNewText(hwnd,"",0);
EditSetNewText(hwnd,lpData,cbData);
*iEOLMode = EditDetectEOLMode(hwnd,lpData,cbData);
GlobalFree(lpData);
}
else {
SendMessage(hwnd,SCI_SETCODEPAGE,iDefaultCodePage,0);
EditSetNewText(hwnd,"",0);
EditSetNewText(hwnd,lpData,cbData);
*iEncoding = CPI_DEFAULT;
*iEOLMode = EditDetectEOLMode(hwnd,lpData,cbData);
GlobalFree(lpData);
}
}
}
iSrcEncoding = -1;
iWeakSrcEncoding = -1;
return TRUE;
}
//=============================================================================
//
// EditSaveFile()
//
BOOL EditSaveFile(
HWND hwnd,
LPCWSTR pszFile,
int iEncoding,
BOOL *pbCancelDataLoss,
BOOL bSaveCopy)
{
HANDLE hFile;
BOOL bWriteSuccess;
char* lpData;
DWORD cbData;
DWORD dwBytesWritten;
*pbCancelDataLoss = FALSE;
hFile = CreateFile(pszFile,
GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
dwLastIOError = GetLastError();
// failure could be due to missing attributes (2k/XP)
if (hFile == INVALID_HANDLE_VALUE)
{
DWORD dwAttributes = GetFileAttributes(pszFile);
if (dwAttributes != INVALID_FILE_ATTRIBUTES)
{
dwAttributes = dwAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
hFile = CreateFile(pszFile,
GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL | dwAttributes,
NULL);
dwLastIOError = GetLastError();
}
}
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
// ensure consistent line endings
if (bFixLineEndings)
SendMessage(hwnd,SCI_CONVERTEOLS,SendMessage(hwnd,SCI_GETEOLMODE,0,0),0);
// strip trailing blanks
if (bAutoStripBlanks)
EditStripTrailingBlanks(hwnd,TRUE);
// get text
cbData = SendMessage(hwnd,SCI_GETLENGTH,0,0);
lpData = GlobalAlloc(GPTR,cbData + 1);
SendMessage(hwnd,SCI_GETTEXT,GlobalSize(lpData),(LPARAM)lpData);
if (cbData == 0) {
bWriteSuccess = SetEndOfFile(hFile);
dwLastIOError = GetLastError();
}
else {
// FIXME: move checks in front of disk file access
/*if ((mEncoding[iEncoding].uFlags & NCP_UNICODE) == 0 && (mEncoding[iEncoding].uFlags & NCP_UTF8_SIGN) == 0) {
BOOL bEncodingMismatch = TRUE;
FILEVARS fv;
FileVars_Init(lpData,cbData,&fv);
if (fv.mask & FV_ENCODING) {
int iAltEncoding;
if (FileVars_IsValidEncoding(&fv)) {
iAltEncoding = FileVars_GetEncoding(&fv);
if (iAltEncoding == iEncoding)
bEncodingMismatch = FALSE;
else if ((mEncoding[iAltEncoding].uFlags & NCP_UTF8) && (mEncoding[iEncoding].uFlags & NCP_UTF8))
bEncodingMismatch = FALSE;
}
if (bEncodingMismatch) {
Encoding_GetLabel(iAltEncoding);
Encoding_GetLabel(iEncoding);
InfoBox(0,L"MsgEncodingMismatch",IDS_ENCODINGMISMATCH,
mEncoding[iAltEncoding].wchLabel,
mEncoding[iEncoding].wchLabel);
}
}
}*/
if (mEncoding[iEncoding].uFlags & NCP_UNICODE)
{
LPWSTR lpDataWide;
int cbDataWide;
SetEndOfFile(hFile);
lpDataWide = GlobalAlloc(GPTR,cbData * 2 + 16);
cbDataWide = MultiByteToWideChar(CP_UTF8,0,lpData,cbData,lpDataWide,GlobalSize(lpDataWide)/sizeof(WCHAR));
if (mEncoding[iEncoding].uFlags & NCP_UNICODE_BOM) {
if (mEncoding[iEncoding].uFlags & NCP_UNICODE_REVERSE)
WriteFile(hFile,(LPCVOID)"\xFE\xFF",2,&dwBytesWritten,NULL);
else
WriteFile(hFile,(LPCVOID)"\xFF\xFE",2,&dwBytesWritten,NULL);
}
if (mEncoding[iEncoding].uFlags & NCP_UNICODE_REVERSE)
_swab((char*)lpDataWide,(char*)lpDataWide,cbDataWide * sizeof(WCHAR));
bWriteSuccess = WriteFile(hFile,lpDataWide,cbDataWide * sizeof(WCHAR),&dwBytesWritten,NULL);
dwLastIOError = GetLastError();
GlobalFree(lpDataWide);
GlobalFree(lpData);
}
else if (mEncoding[iEncoding].uFlags & NCP_UTF8)
{
SetEndOfFile(hFile);
if (mEncoding[iEncoding].uFlags & NCP_UTF8_SIGN)
WriteFile(hFile,(LPCVOID)"\xEF\xBB\xBF",3,&dwBytesWritten,NULL);
bWriteSuccess = WriteFile(hFile,lpData,cbData,&dwBytesWritten,NULL);
dwLastIOError = GetLastError();
GlobalFree(lpData);
}
else if (mEncoding[iEncoding].uFlags & NCP_8BIT) {
BOOL bCancelDataLoss = FALSE;
UINT uCodePage = mEncoding[iEncoding].uCodePage;
LPWSTR lpDataWide = GlobalAlloc(GPTR,cbData * 2 + 16);
int cbDataWide = MultiByteToWideChar(CP_UTF8,0,lpData,cbData,lpDataWide,GlobalSize(lpDataWide)/sizeof(WCHAR));
// Special cases: 42, 50220, 50221, 50222, 50225, 50227, 50229, 54936, 57002-11, 65000, 65001
if (uCodePage == CP_UTF7 || uCodePage == 54936) {
GlobalFree(lpData);
lpData = GlobalAlloc(GPTR,GlobalSize(lpDataWide)*2);
}
else
ZeroMemory(lpData,GlobalSize(lpData));
if (uCodePage == CP_UTF7 || uCodePage == 54936)
cbData = WideCharToMultiByte(uCodePage,0,lpDataWide,cbDataWide,lpData,GlobalSize(lpData),NULL,NULL);
else {
cbData = WideCharToMultiByte(uCodePage,WC_NO_BEST_FIT_CHARS,lpDataWide,cbDataWide,lpData,GlobalSize(lpData),NULL,&bCancelDataLoss);
if (!bCancelDataLoss) {
cbData = WideCharToMultiByte(uCodePage,0,lpDataWide,cbDataWide,lpData,GlobalSize(lpData),NULL,NULL);
bCancelDataLoss = FALSE;
}
}
GlobalFree(lpDataWide);
if (!bCancelDataLoss || InfoBox(MBOKCANCEL,L"MsgConv3",IDS_ERR_UNICODE2) == IDOK) {
SetEndOfFile(hFile);
bWriteSuccess = WriteFile(hFile,lpData,cbData,&dwBytesWritten,NULL);
dwLastIOError = GetLastError();
}
else {
bWriteSuccess = FALSE;
*pbCancelDataLoss = TRUE;
}
GlobalFree(lpData);
}
else {
SetEndOfFile(hFile);
bWriteSuccess = WriteFile(hFile,lpData,cbData,&dwBytesWritten,NULL);
dwLastIOError = GetLastError();
GlobalFree(lpData);
}
}
CloseHandle(hFile);
if (bWriteSuccess)
{
if (!bSaveCopy)
SendMessage(hwnd,SCI_SETSAVEPOINT,0,0);
return TRUE;
}
else
return FALSE;
}
//=============================================================================
//
// EditMakeUppercase()
//
void EditMakeUppercase(HWND hwnd)
{
int cchTextW;
int iCurPos;
int iAnchorPos;
UINT cpEdit;
int i;
BOOL bChanged = FALSE;
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos != iAnchorPos)
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iSelCount = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) -
SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
char* pszText = GlobalAlloc(GPTR,(iSelCount)+2);
LPWSTR pszTextW = GlobalAlloc(GPTR,(iSelCount*2)+2);
if (pszText == NULL || pszTextW == NULL) {
GlobalFree(pszText);
GlobalFree(pszTextW);
return;
}
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszText);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
for (i = 0; i < cchTextW; i++) {
if (IsCharLowerW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharUpperW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
}
if (bChanged) {
WideCharToMultiByte(cpEdit,0,pszTextW,cchTextW,pszText,GlobalSize(pszText),NULL,NULL);
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_CLEAR,0,0);
SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)iSelCount,(LPARAM)pszText);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
GlobalFree(pszText);
GlobalFree(pszTextW);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditMakeLowercase()
//
void EditMakeLowercase(HWND hwnd)
{
int cchTextW;
int iCurPos;
int iAnchorPos;
UINT cpEdit;
int i;
BOOL bChanged = FALSE;
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos != iAnchorPos)
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iSelCount = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) -
SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
char* pszText = GlobalAlloc(GPTR,(iSelCount)+2);
LPWSTR pszTextW = GlobalAlloc(GPTR,(iSelCount*2)+2);
if (pszText == NULL || pszTextW == NULL) {
GlobalFree(pszText);
GlobalFree(pszTextW);
return;
}
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszText);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
for (i = 0; i < cchTextW; i++) {
if (IsCharUpperW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharLowerW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
}
if (bChanged) {
WideCharToMultiByte(cpEdit,0,pszTextW,cchTextW,pszText,GlobalSize(pszText),NULL,NULL);
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_CLEAR,0,0);
SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)iSelCount,(LPARAM)pszText);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
GlobalFree(pszText);
GlobalFree(pszTextW);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditInvertCase()
//
void EditInvertCase(HWND hwnd)
{
int cchTextW;
int iCurPos;
int iAnchorPos;
UINT cpEdit;
int i;
BOOL bChanged = FALSE;
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos != iAnchorPos)
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iSelCount = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) -
SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
char* pszText = GlobalAlloc(GPTR,(iSelCount)+2);
LPWSTR pszTextW = GlobalAlloc(GPTR,(iSelCount*2)+2);
if (pszText == NULL || pszTextW == NULL) {
GlobalFree(pszText);
GlobalFree(pszTextW);
return;
}
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszText);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
for (i = 0; i < cchTextW; i++) {
if (IsCharUpperW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharLowerW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
else if (IsCharLowerW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharUpperW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
}
if (bChanged) {
WideCharToMultiByte(cpEdit,0,pszTextW,cchTextW,pszText,GlobalSize(pszText),NULL,NULL);
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_CLEAR,0,0);
SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)iSelCount,(LPARAM)pszText);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
GlobalFree(pszText);
GlobalFree(pszTextW);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditTitleCase()
//
void EditTitleCase(HWND hwnd)
{
int cchTextW;
int iCurPos;
int iAnchorPos;
UINT cpEdit;
int i;
BOOL bNewWord = TRUE;
BOOL bChanged = FALSE;
#ifdef BOOKMARK_EDITION
BOOL bPrevWasSpace = FALSE;
#endif
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos != iAnchorPos)
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iSelCount = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) -
SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
char* pszText = GlobalAlloc(GPTR,(iSelCount)+2);
LPWSTR pszTextW = GlobalAlloc(GPTR,(iSelCount*2)+2);
if (pszText == NULL || pszTextW == NULL) {
GlobalFree(pszText);
GlobalFree(pszTextW);
return;
}
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszText);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
#ifdef BOOKMARK_EDITION
//Slightly enhanced function to make Title Case: Added some '-characters and bPrevWasSpace makes it better (for example "'Don't'" will now work)
bPrevWasSpace = TRUE;
for (i = 0; i < cchTextW; i++) {
if (!IsCharAlphaNumericW(pszTextW[i]) && (!StrChr(L"'`´''’",pszTextW[i]) || bPrevWasSpace ) ) {
bNewWord = TRUE;
}
else {
if (bNewWord) {
if (IsCharLowerW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharUpperW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
}
else {
if (IsCharUpperW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharLowerW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
}
bNewWord = FALSE;
}
if( StrChr(L" \r\n\t",pszTextW[i]) ) bPrevWasSpace = TRUE; else bPrevWasSpace = FALSE;
}
#else
for (i = 0; i < cchTextW; i++) {
if (!IsCharAlphaNumericW(pszTextW[i]) && !StrChr(L"",pszTextW[i])) {
bNewWord = TRUE;
}
else {
if (bNewWord) {
if (IsCharLowerW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharUpperW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
}
else {
if (IsCharUpperW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharLowerW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
}
bNewWord = FALSE;
}
}
#endif
if (bChanged) {
WideCharToMultiByte(cpEdit,0,pszTextW,cchTextW,pszText,GlobalSize(pszText),NULL,NULL);
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_CLEAR,0,0);
SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)iSelCount,(LPARAM)pszText);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
GlobalFree(pszText);
GlobalFree(pszTextW);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditSentenceCase()
//
void EditSentenceCase(HWND hwnd)
{
int cchTextW;
int iCurPos;
int iAnchorPos;
UINT cpEdit;
int i;
BOOL bNewSentence = TRUE;
BOOL bChanged = FALSE;
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos != iAnchorPos)
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iSelCount = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) -
SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
char* pszText = GlobalAlloc(GPTR,(iSelCount)+2);
LPWSTR pszTextW = GlobalAlloc(GPTR,(iSelCount*2)+2);
if (pszText == NULL || pszTextW == NULL) {
GlobalFree(pszText);
GlobalFree(pszTextW);
return;
}
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszText);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
for (i = 0; i < cchTextW; i++) {
if (StrChr(L".;!?\r\n",pszTextW[i])) {
bNewSentence = TRUE;
}
else {
if (IsCharAlphaNumericW(pszTextW[i])) {
if (bNewSentence) {
if (IsCharLowerW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharUpperW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
bNewSentence = FALSE;
}
else {
if (IsCharUpperW(pszTextW[i])) {
pszTextW[i] = LOWORD(CharLowerW((LPWSTR)MAKELONG(pszTextW[i],0)));
bChanged = TRUE;
}
}
}
}
}
if (bChanged) {
WideCharToMultiByte(cpEdit,0,pszTextW,cchTextW,pszText,GlobalSize(pszText),NULL,NULL);
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_CLEAR,0,0);
SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)iSelCount,(LPARAM)pszText);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
GlobalFree(pszText);
GlobalFree(pszTextW);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditURLEncode()
//
void EditURLEncode(HWND hwnd)
{
int cchTextW;
int iCurPos;
int iAnchorPos;
UINT cpEdit;
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos != iAnchorPos)
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iSelCount = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) -
SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
char* pszText;
LPWSTR pszTextW;
DWORD cchEscaped;
char* pszEscaped;
DWORD cchEscapedW;
LPWSTR pszEscapedW;
pszText = LocalAlloc(LPTR,(iSelCount)+2);
if (pszText == NULL) {
return;
}
pszTextW = LocalAlloc(LPTR,(iSelCount*2)+2);
if (pszTextW == NULL) {
LocalFree(pszText);
return;
}
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszText);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,LocalSize(pszTextW)/sizeof(WCHAR));
pszEscaped = LocalAlloc(LPTR,LocalSize(pszText) * 3);
if (pszEscaped == NULL) {
LocalFree(pszText);
LocalFree(pszTextW);
return;
}
pszEscapedW = LocalAlloc(LPTR,LocalSize(pszTextW) * 3);
if (pszEscapedW == NULL) {
LocalFree(pszText);
LocalFree(pszTextW);
LocalFree(pszEscaped);
return;
}
cchEscapedW = LocalSize(pszEscapedW) / sizeof(WCHAR);
UrlEscape(pszTextW,pszEscapedW,&cchEscapedW,URL_ESCAPE_SEGMENT_ONLY);
cchEscaped = WideCharToMultiByte(cpEdit,0,pszEscapedW,cchEscapedW,pszEscaped,LocalSize(pszEscaped),NULL,NULL);
if (iCurPos < iAnchorPos)
iAnchorPos = iCurPos + cchEscaped;
else
iCurPos = iAnchorPos + cchEscaped;
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_CLEAR,0,0);
SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)cchEscaped,(LPARAM)pszEscaped);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
LocalFree(pszText);
LocalFree(pszTextW);
LocalFree(pszEscaped);
LocalFree(pszEscapedW);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditURLDecode()
//
void EditURLDecode(HWND hwnd)
{
int cchTextW;
int iCurPos;
int iAnchorPos;
UINT cpEdit;
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos != iAnchorPos)
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iSelCount = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) -
SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
char* pszText;
LPWSTR pszTextW;
DWORD cchUnescaped;
char* pszUnescaped;
DWORD cchUnescapedW;
LPWSTR pszUnescapedW;
pszText = LocalAlloc(LPTR,(iSelCount)+2);
if (pszText == NULL) {
return;
}
pszTextW = LocalAlloc(LPTR,(iSelCount*2)+2);
if (pszTextW == NULL) {
LocalFree(pszText);
return;
}
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszText);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,LocalSize(pszTextW)/sizeof(WCHAR));
pszUnescaped = LocalAlloc(LPTR,LocalSize(pszText) * 3);
if (pszUnescaped == NULL) {
LocalFree(pszText);
LocalFree(pszTextW);
return;
}
pszUnescapedW = LocalAlloc(LPTR,LocalSize(pszTextW) * 3);
if (pszUnescapedW == NULL) {
LocalFree(pszText);
LocalFree(pszTextW);
LocalFree(pszUnescaped);
return;
}
cchUnescapedW = LocalSize(pszUnescapedW) / sizeof(WCHAR);
UrlUnescape(pszTextW,pszUnescapedW,&cchUnescapedW,0);
cchUnescaped = WideCharToMultiByte(cpEdit,0,pszUnescapedW,cchUnescapedW,pszUnescaped,LocalSize(pszUnescaped),NULL,NULL);
if (iCurPos < iAnchorPos)
iAnchorPos = iCurPos + cchUnescaped;
else
iCurPos = iAnchorPos + cchUnescaped;
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_CLEAR,0,0);
SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)cchUnescaped,(LPARAM)pszUnescaped);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
LocalFree(pszText);
LocalFree(pszTextW);
LocalFree(pszUnescaped);
LocalFree(pszUnescapedW);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditEscapeCChars()
//
void EditEscapeCChars(HWND hwnd) {
if (SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) - SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0))
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
EDITFINDREPLACE efr = { "", "", "", "", 0, 0, 0, 0, 0, 0, hwnd };
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
lstrcpyA(efr.szFind,"\\");
lstrcpyA(efr.szReplace,"\\\\");
EditReplaceAllInSelection(hwnd,&efr,FALSE);
lstrcpyA(efr.szFind,"\"");
lstrcpyA(efr.szReplace,"\\\"");
EditReplaceAllInSelection(hwnd,&efr,FALSE);
lstrcpyA(efr.szFind,"\'");
lstrcpyA(efr.szReplace,"\\\'");
EditReplaceAllInSelection(hwnd,&efr,FALSE);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditUnescapeCChars()
//
void EditUnescapeCChars(HWND hwnd) {
if (SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) - SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0))
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
EDITFINDREPLACE efr = { "", "", "", "", 0, 0, 0, 0, 0, 0, hwnd };
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
lstrcpyA(efr.szFind,"\\\\");
lstrcpyA(efr.szReplace,"\\");
EditReplaceAllInSelection(hwnd,&efr,FALSE);
lstrcpyA(efr.szFind,"\\\"");
lstrcpyA(efr.szReplace,"\"");
EditReplaceAllInSelection(hwnd,&efr,FALSE);
lstrcpyA(efr.szFind,"\\\'");
lstrcpyA(efr.szReplace,"\'");
EditReplaceAllInSelection(hwnd,&efr,FALSE);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
}
//=============================================================================
//
// EditModifyNumber()
//
void EditModifyNumber(HWND hwnd,BOOL bIncrease) {
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
int iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
int iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
if (iSelEnd - iSelStart) {
char chFormat[32] = "";
char chNumber[32];
int iNumber;
int iWidth;
if (SendMessage(hwnd,SCI_GETSELTEXT,0,0) <= COUNTOF(chNumber)) {
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)chNumber);
if (StrChrIA(chNumber,'-'))
return;
if (!StrChrIA(chNumber,'x') && sscanf(chNumber,"%d",&iNumber) == 1) {
iWidth = lstrlenA(chNumber);
if (iNumber >= 0) {
if (bIncrease && iNumber < INT_MAX)
iNumber++;
if (!bIncrease && iNumber > 0)
iNumber--;
wsprintfA(chFormat,"%%0%ii",iWidth);
wsprintfA(chNumber,chFormat,iNumber);
SendMessage(hwnd,SCI_REPLACESEL,0,(LPARAM)chNumber);
SendMessage(hwnd,SCI_SETSEL,iSelStart,iSelStart+lstrlenA(chNumber));
}
}
else if (sscanf(chNumber,"%x",&iNumber) == 1) {
int i;
BOOL bUppercase = FALSE;
iWidth = lstrlenA(chNumber) - 2;
if (iNumber >= 0) {
if (bIncrease && iNumber < INT_MAX)
iNumber++;
if (!bIncrease && iNumber > 0)
iNumber--;
for (i = lstrlenA(chNumber) -1 ; i >= 0; i--) {
if (IsCharLowerA(chNumber[i]))
break;
else if (IsCharUpper(chNumber[i])) {
bUppercase = TRUE;
break;
}
}
if (bUppercase)
wsprintfA(chFormat,"%%#0%iX",iWidth);
else
wsprintfA(chFormat,"%%#0%ix",iWidth);
wsprintfA(chNumber,chFormat,iNumber);
SendMessage(hwnd,SCI_REPLACESEL,0,(LPARAM)chNumber);
SendMessage(hwnd,SCI_SETSEL,iSelStart,iSelStart+lstrlenA(chNumber));
}
}
}
}
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditTabsToSpaces()
//
void EditTabsToSpaces(HWND hwnd,int nTabWidth,BOOL bOnlyIndentingWS)
{
char* pszText;
LPWSTR pszTextW;
int cchTextW;
int iTextW;
LPWSTR pszConvW;
int cchConvW;
int cchConvM;
int i,j;
int iLine;
int iCurPos;
int iAnchorPos;
int iSelStart;
int iSelEnd;
int iSelCount;
UINT cpEdit;
struct TextRange tr;
BOOL bIsLineStart = TRUE;
BOOL bModified = FALSE;
if (SC_SEL_RECTANGLE == SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
MsgBox(MBINFO,IDS_SELRECT);
return;
}
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos == iAnchorPos) /*{
iSelStart = 0;
iSelEnd = SendMessage(hwnd,SCI_GETLENGTH,0,0);
}*/
return;
else {
iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
}
iLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
iSelStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
iSelCount = iSelEnd - iSelStart;
pszText = GlobalAlloc(GPTR,(iSelCount)+2);
if (pszText == NULL)
return;
pszTextW = GlobalAlloc(GPTR,(iSelCount*2)+2);
if (pszTextW == NULL) {
GlobalFree(pszText);
return;
}
tr.chrg.cpMin = iSelStart;
tr.chrg.cpMax = iSelEnd;
tr.lpstrText = pszText;
SendMessage(hwnd,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
GlobalFree(pszText);
pszConvW = GlobalAlloc(GPTR,cchTextW*sizeof(WCHAR)*nTabWidth+2);
if (pszConvW == NULL) {
GlobalFree(pszTextW);
return;
}
cchConvW = 0;
// Contributed by Homam
// Thank you very much!
i=0;
for (iTextW = 0; iTextW < cchTextW; iTextW++)
{
WCHAR w = pszTextW[iTextW];
if (w == L'\t' && (!bOnlyIndentingWS || bOnlyIndentingWS && bIsLineStart)) {
for (j = 0; j < nTabWidth - i % nTabWidth; j++)
pszConvW[cchConvW++] = L' ';
i = 0;
bModified = TRUE;
}
else {
i++;
if (w == L'\n' || w == L'\r') {
i = 0;
bIsLineStart = TRUE;
}
else if (w != L' ')
bIsLineStart = FALSE;
pszConvW[cchConvW++] = w;
}
}
GlobalFree(pszTextW);
if (bModified) {
pszText = GlobalAlloc(GPTR,cchConvW * 3);
cchConvM = WideCharToMultiByte(cpEdit,0,pszConvW,cchConvW,pszText,GlobalSize(pszText),NULL,NULL);
GlobalFree(pszConvW);
if (iAnchorPos > iCurPos) {
iCurPos = iSelStart;
iAnchorPos = iSelStart + cchConvM;
}
else {
iAnchorPos = iSelStart;
iCurPos = iSelStart + cchConvM;
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iSelStart,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iSelEnd,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)cchConvM,(LPARAM)pszText);
//SendMessage(hwnd,SCI_CLEAR,0,0);
//SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)cchConvW,(LPARAM)pszText);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
GlobalFree(pszText);
}
else
GlobalFree(pszConvW);
}
//=============================================================================
//
// EditSpacesToTabs()
//
void EditSpacesToTabs(HWND hwnd,int nTabWidth,BOOL bOnlyIndentingWS)
{
char* pszText;
LPWSTR pszTextW;
int cchTextW;
int iTextW;
LPWSTR pszConvW;
int cchConvW;
int cchConvM;
int i,j,t;
int iLine;
int iCurPos;
int iAnchorPos;
int iSelStart;
int iSelEnd;
int iSelCount;
UINT cpEdit;
struct TextRange tr;
WCHAR space[256];
BOOL bIsLineStart = TRUE;
BOOL bModified = FALSE;
if (SC_SEL_RECTANGLE == SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
MsgBox(MBINFO,IDS_SELRECT);
return;
}
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos == iAnchorPos) /*{
iSelStart = 0;
iSelEnd = SendMessage(hwnd,SCI_GETLENGTH,0,0);
}*/
return;
else {
iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
}
iLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
iSelStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
iSelCount = iSelEnd - iSelStart;
pszText = GlobalAlloc(GPTR,(iSelCount)+2);
if (pszText == NULL)
return;
pszTextW = GlobalAlloc(GPTR,(iSelCount*2)+2);
if (pszTextW == NULL) {
GlobalFree(pszText);
return;
}
tr.chrg.cpMin = iSelStart;
tr.chrg.cpMax = iSelEnd;
tr.lpstrText = pszText;
SendMessage(hwnd,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
GlobalFree(pszText);
pszConvW = GlobalAlloc(GPTR,cchTextW*sizeof(WCHAR)+2);
if (pszConvW == NULL) {
GlobalFree(pszTextW);
return;
}
cchConvW = 0;
// Contributed by Homam
// Thank you very much!
i = j = 0;
for (iTextW = 0; iTextW < cchTextW; iTextW++)
{
WCHAR w = pszTextW[iTextW];
if ((w == L' ' || w == L'\t') && (!bOnlyIndentingWS || bOnlyIndentingWS && bIsLineStart)) {
space[j++] = w;
if (j == nTabWidth - i % nTabWidth || w == L'\t') {
if (j > 1 || pszTextW[iTextW+1] == L' ' || pszTextW[iTextW+1] == L'\t')
pszConvW[cchConvW++] = L'\t';
else
pszConvW[cchConvW++] = w;
i = j = 0;
bModified = bModified || (w != pszConvW[cchConvW-1]);
}
}
else {
i += j + 1;
if (j > 0) {
//space[j] = '\0';
for (t = 0; t < j; t++)
pszConvW[cchConvW++] = space[t];
j = 0;
}
if (w == L'\n' || w == L'\r') {
i = 0;
bIsLineStart = TRUE;
}
else
bIsLineStart = FALSE;
pszConvW[cchConvW++] = w;
}
}
if (j > 0) {
for (t = 0; t < j; t++)
pszConvW[cchConvW++] = space[t];
}
GlobalFree(pszTextW);
if (bModified || cchConvW != cchTextW) {
pszText = GlobalAlloc(GPTR,cchConvW * 3);
cchConvM = WideCharToMultiByte(cpEdit,0,pszConvW,cchConvW,pszText,GlobalSize(pszText),NULL,NULL);
GlobalFree(pszConvW);
if (iAnchorPos > iCurPos) {
iCurPos = iSelStart;
iAnchorPos = iSelStart + cchConvM;
}
else {
iAnchorPos = iSelStart;
iCurPos = iSelStart + cchConvM;
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iSelStart,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iSelEnd,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)cchConvM,(LPARAM)pszText);
//SendMessage(hwnd,SCI_CLEAR,0,0);
//SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)cchConvW,(LPARAM)pszText);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
GlobalFree(pszText);
}
else
GlobalFree(pszConvW);
}
//=============================================================================
//
// EditMoveUp()
//
void EditMoveUp(HWND hwnd)
{
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
int iCurLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iCurPos,0);
int iAnchorLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iAnchorPos,0);
if (iCurLine == iAnchorLine) {
int iLineCurPos = iCurPos - SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iCurLine,0);
int iLineAnchorPos = iAnchorPos - SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iAnchorLine,0);
if (iCurLine > 0) {
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_LINETRANSPOSE,0,0);
SendMessage(hwnd,SCI_SETSEL,
(WPARAM)SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iAnchorLine-1,0)+iLineAnchorPos,
(LPARAM)SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iCurLine-1,0)+iLineCurPos);
SendMessage(hwnd,SCI_CHOOSECARETX,0,0);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
}
else if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
int iLineSrc = min(iCurLine,iAnchorLine) -1;
if (iLineSrc >= 0) {
DWORD cLine;
char *pLine;
int iLineSrcStart;
int iLineSrcEnd;
int iLineDest;
int iLineDestStart;
cLine = SendMessage(hwnd,SCI_GETLINE,(WPARAM)iLineSrc,0);
pLine = LocalAlloc(LPTR,cLine+1);
SendMessage(hwnd,SCI_GETLINE,(WPARAM)iLineSrc,(LPARAM)pLine);
iLineSrcStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc,0);
iLineSrcEnd = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc+1,0);
iLineDest = max(iCurLine,iAnchorLine);
if (max(iCurPos,iAnchorPos) <= SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineDest,0)) {
if (iLineDest >= 1)
iLineDest--;
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iLineSrcStart,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iLineSrcEnd,0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
iLineDestStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineDest,0);
SendMessage(hwnd,SCI_INSERTTEXT,(WPARAM)iLineDestStart,(LPARAM)pLine);
LocalFree(pLine);
if (iLineDest == SendMessage(hwnd,SCI_GETLINECOUNT,0,0) -1) {
char chaEOL[] = "\r\n";
int iEOLMode = SendMessage(hwnd,SCI_GETEOLMODE,0,0);
if (iEOLMode == SC_EOL_CR)
chaEOL[1] = 0;
else if (iEOLMode == SC_EOL_LF) {
chaEOL[0] = '\n';
chaEOL[1] = 0;
}
SendMessage(hwnd,SCI_INSERTTEXT,(WPARAM)iLineDestStart,(LPARAM)chaEOL);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)
SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLineDest,0),0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)
SendMessage(hwnd,SCI_GETLENGTH,0,0),0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
}
if (iCurPos < iAnchorPos) {
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iCurLine-1,0);
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineDest,0);
}
else {
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iAnchorLine-1,0);
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineDest,0);
}
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditMoveDown()
//
void EditMoveDown(HWND hwnd)
{
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
int iCurLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iCurPos,0);
int iAnchorLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iAnchorPos,0);
if (iCurLine == iAnchorLine) {
int iLineCurPos = iCurPos - SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iCurLine,0);
int iLineAnchorPos = iAnchorPos - SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iAnchorLine,0);
if (iCurLine < SendMessage(hwndEdit,SCI_GETLINECOUNT,0,0) - 1) {
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_GOTOLINE,(WPARAM)iCurLine+1,0);
SendMessage(hwnd,SCI_LINETRANSPOSE,0,0);
SendMessage(hwnd,SCI_SETSEL,
(WPARAM)SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iAnchorLine+1,0)+iLineAnchorPos,
(LPARAM)SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iCurLine+1,0)+iLineCurPos);
SendMessage(hwnd,SCI_CHOOSECARETX,0,0);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
}
else if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
int iLineSrc = max(iCurLine,iAnchorLine) +1;
if (max(iCurPos,iAnchorPos) <= SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc-1,0)) {
if (iLineSrc >= 1)
iLineSrc--;
}
if (iLineSrc <= SendMessage(hwnd,SCI_GETLINECOUNT,0,0) -1) {
DWORD cLine;
char *pLine;
int iLineSrcStart;
int iLineSrcEnd;
int iLineDest;
int iLineDestStart;
BOOL bLastLine = (iLineSrc == SendMessage(hwnd,SCI_GETLINECOUNT,0,0) -1);
if (bLastLine &&
(SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLineSrc,0) -
SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc,0) == 0) &&
(SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLineSrc-1,0) -
SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc-1,0) == 0))
return;
if (bLastLine) {
char chaEOL[] = "\r\n";
int iEOLMode = SendMessage(hwnd,SCI_GETEOLMODE,0,0);
if (iEOLMode == SC_EOL_CR)
chaEOL[1] = 0;
else if (iEOLMode == SC_EOL_LF) {
chaEOL[0] = '\n';
chaEOL[1] = 0;
}
SendMessage(hwnd,SCI_APPENDTEXT,(WPARAM)lstrlenA(chaEOL),(LPARAM)chaEOL);
}
cLine = SendMessage(hwnd,SCI_GETLINE,(WPARAM)iLineSrc,0);
pLine = LocalAlloc(LPTR,cLine+3);
SendMessage(hwnd,SCI_GETLINE,(WPARAM)iLineSrc,(LPARAM)pLine);
iLineSrcStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc,0);
iLineSrcEnd = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc+1,0);
iLineDest = min(iCurLine,iAnchorLine);
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iLineSrcStart,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iLineSrcEnd,0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
iLineDestStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineDest,0);
SendMessage(hwnd,SCI_INSERTTEXT,(WPARAM)iLineDestStart,(LPARAM)pLine);
if (bLastLine) {
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)
SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)
SendMessage(hwnd,SCI_GETLINECOUNT,0,0)-2,0),0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)
SendMessage(hwnd,SCI_GETLENGTH,0,0),0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
}
LocalFree(pLine);
if (iCurPos < iAnchorPos) {
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iCurLine+1,0);
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc+1,0);
}
else {
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iAnchorLine+1,0);
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineSrc+1,0);
}
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditModifyLines()
//
void EditModifyLines(HWND hwnd,LPCWSTR pwszPrefix,LPCWSTR pwszAppend)
{
char mszPrefix[256*3] = "";
char mszAppend[256*3] = "";
int mbcp;
int iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
int iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
//if (iSelStart == iSelEnd) {
// iSelStart = 0;
// iSelEnd = SendMessage(hwnd,SCI_GETLENGTH,0,0);
//}
if (SendMessage(hwnd,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8)
mbcp = CP_UTF8;
else
mbcp = CP_ACP;
if (lstrlen(pwszPrefix))
WideCharToMultiByte(mbcp,0,pwszPrefix,-1,mszPrefix,COUNTOF(mszPrefix),NULL,NULL);
if (lstrlen(pwszAppend))
WideCharToMultiByte(mbcp,0,pwszAppend,-1,mszAppend,COUNTOF(mszAppend),NULL,NULL);
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iLine;
int iLineStart = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
int iLineEnd = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0);
//if (iSelStart > SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0))
// iLineStart++;
if (iSelEnd <= SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd,0))
{
if (iLineEnd - iLineStart >= 1)
iLineEnd--;
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
for (iLine = iLineStart; iLine <= iLineEnd; iLine++)
{
int iPos;
if (lstrlen(pwszPrefix)) {
iPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)lstrlenA(mszPrefix),(LPARAM)mszPrefix);
}
if (lstrlen(pwszAppend)) {
iPos = SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)lstrlenA(mszAppend),(LPARAM)mszAppend);
}
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
//// Fix selection
//if (iSelStart != iSelEnd && SendMessage(hwnd,SCI_GETTARGETEND,0,0) > SendMessage(hwnd,SCI_GETSELECTIONEND,0,0))
//{
// int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
// int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
// if (iCurPos > iAnchorPos)
// iCurPos = SendMessage(hwnd,SCI_GETTARGETEND,0,0);
// else
// iAnchorPos = SendMessage(hwnd,SCI_GETTARGETEND,0,0);
// SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
//}
// extend selection to start of first line
// the above code is not required when last line has been excluded
if (iSelStart != iSelEnd)
{
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos < iAnchorPos) {
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0);
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd+1,0);
}
else {
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0);
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd+1,0);
}
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
}
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditEncloseSelection()
//
void EditEncloseSelection(HWND hwnd,LPCWSTR pwszOpen,LPCWSTR pwszClose)
{
char mszOpen[256*3] = "";
char mszClose[256*3] = "";
int mbcp;
int iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
int iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
if (SendMessage(hwnd,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8)
mbcp = CP_UTF8;
else
mbcp = CP_ACP;
if (lstrlen(pwszOpen))
WideCharToMultiByte(mbcp,0,pwszOpen,-1,mszOpen,COUNTOF(mszOpen),NULL,NULL);
if (lstrlen(pwszClose))
WideCharToMultiByte(mbcp,0,pwszClose,-1,mszClose,COUNTOF(mszClose),NULL,NULL);
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
if (lstrlenA(mszOpen)) {
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iSelStart,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iSelStart,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)lstrlenA(mszOpen),(LPARAM)mszOpen);
}
if (lstrlenA(mszClose)) {
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iSelEnd + lstrlenA(mszOpen),0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iSelEnd + lstrlenA(mszOpen),0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)lstrlenA(mszClose),(LPARAM)mszClose);
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
// Fix selection
if (iSelStart == iSelEnd)
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iSelStart + lstrlenA(mszOpen),(WPARAM)iSelStart + lstrlenA(mszOpen));
else {
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos < iAnchorPos) {
iCurPos = iSelStart + lstrlenA(mszOpen);
iAnchorPos = iSelEnd + lstrlenA(mszOpen);
}
else {
iAnchorPos = iSelStart + lstrlenA(mszOpen);
iCurPos = iSelEnd + lstrlenA(mszOpen);
}
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
}
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditToggleLineComments()
//
void EditToggleLineComments(HWND hwnd,LPCWSTR pwszComment,BOOL bInsertAtStart)
{
char mszComment[256*3] = "";
int cchComment;
int mbcp;
int iAction = 0;
int iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
int iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
if (SendMessage(hwnd,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8)
mbcp = CP_UTF8;
else
mbcp = CP_ACP;
if (lstrlen(pwszComment))
WideCharToMultiByte(mbcp,0,pwszComment,-1,mszComment,COUNTOF(mszComment),NULL,NULL);
cchComment = lstrlenA(mszComment);
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0) && cchComment)
{
int iLine;
int iCommentCol = 0;
int iLineStart = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
int iLineEnd = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0);
if (iSelEnd <= SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd,0))
{
if (iLineEnd - iLineStart >= 1)
iLineEnd--;
}
if (!bInsertAtStart) {
iCommentCol = 1024;
for (iLine = iLineStart; iLine <= iLineEnd; iLine++) {
int iLineEndPos = SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
int iLineIndentPos = SendMessage(hwnd,SCI_GETLINEINDENTPOSITION,(WPARAM)iLine,0);
if (iLineIndentPos != iLineEndPos) {
int iIndentColumn = SendMessage(hwnd,SCI_GETCOLUMN,(WPARAM)iLineIndentPos,0);
iCommentCol = min(iCommentCol,iIndentColumn);
}
}
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
for (iLine = iLineStart; iLine <= iLineEnd; iLine++)
{
int iCommentPos;
int iIndentPos = SendMessage(hwnd,SCI_GETLINEINDENTPOSITION,(WPARAM)iLine,0);
char tchBuf[32];
struct TextRange tr;
if (iIndentPos == SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0))
continue;
tr.chrg.cpMin = iIndentPos;
tr.chrg.cpMax = tr.chrg.cpMin + min(31,cchComment);
tr.lpstrText = tchBuf;
SendMessage(hwnd,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
if (StrCmpNIA(tchBuf,mszComment,cchComment) == 0) {
switch (iAction) {
case 0:
iAction = 2;
case 2:
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iIndentPos,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iIndentPos+cchComment,0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
break;
case 1:
break;
}
}
else {
switch (iAction) {
case 0:
iAction = 1;
case 1:
iCommentPos = SendMessage(hwnd,SCI_FINDCOLUMN,(WPARAM)iLine,(LPARAM)iCommentCol);
SendMessage(hwnd,SCI_INSERTTEXT,(WPARAM)iCommentPos,(LPARAM)mszComment);
break;
case 2:
break;
}
}
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
// extend selection to start of first line
// the above code is not required when last line has been excluded
if (iSelStart != iSelEnd)
{
int iAnchorPos;
if (iCurPos == iSelStart) {
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0);
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd+1,0);
}
else {
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0);
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd+1,0);
}
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
}
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditPadWithSpaces()
//
void EditPadWithSpaces(HWND hwnd)
{
char *pmszPadStr;
int iMaxColumn = 0;
int iLine;
BOOL bIsRectangular = FALSE;
int iSelStart;
int iSelEnd;
int iLineStart;
int iLineEnd;
int iRcCurLine;
int iRcAnchorLine;
int iRcCurCol;
int iRcAnchorCol;
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
iLineStart = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
iLineEnd = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0);
if (iLineStart == iLineEnd) {
iLineStart = 0;
iLineEnd = SendMessage(hwnd,SCI_GETLINECOUNT,0,0) -1;
}
for (iLine = iLineStart; iLine <= iLineEnd; iLine++) {
int iPos = SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
iMaxColumn = max(iMaxColumn,SendMessage(hwnd,SCI_GETCOLUMN,(WPARAM)iPos,0));
}
}
else {
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
iRcCurLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iCurPos,0);
iRcAnchorLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iAnchorPos,0);
iRcCurCol = SendMessage(hwnd,SCI_GETCOLUMN,(WPARAM)iCurPos,0);
iRcAnchorCol = SendMessage(hwnd,SCI_GETCOLUMN,(WPARAM)iAnchorPos,0);
bIsRectangular = TRUE;
iLineStart = 0;
iLineEnd = SendMessage(hwnd,SCI_GETLINECOUNT,0,0) -1;
for (iLine = iLineStart; iLine <= iLineEnd; iLine++) {
int iPos = SendMessage(hwnd,SCI_GETLINESELENDPOSITION,(WPARAM)iLine,0);
if (iPos != INVALID_POSITION)
iMaxColumn = max(iMaxColumn,SendMessage(hwnd,SCI_GETCOLUMN,(WPARAM)iPos,0));
}
}
pmszPadStr = LocalAlloc(LPTR,(iMaxColumn +1 )* sizeof(char));
if (pmszPadStr) {
FillMemory(pmszPadStr,LocalSize(pmszPadStr),' ');
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
for (iLine = iLineStart; iLine <= iLineEnd; iLine++) {
int iPos;
int iPadLen;
int iLineSelEndPos;
iLineSelEndPos = SendMessage(hwnd,SCI_GETLINESELENDPOSITION,(WPARAM)iLine,0);
if (bIsRectangular && INVALID_POSITION == iLineSelEndPos)
continue;
iPos = SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
if (bIsRectangular && iPos > iLineSelEndPos)
continue;
iPadLen = iMaxColumn - SendMessage(hwnd,SCI_GETCOLUMN,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)iPadLen,(LPARAM)pmszPadStr);
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
if (!bIsRectangular &&
SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0) !=
SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0))
{
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos < iAnchorPos) {
iCurPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0);
iAnchorPos = SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLineEnd,0);
}
else {
iAnchorPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0);
iCurPos = SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLineEnd,0);
}
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
}
else if (bIsRectangular) {
int iCurPos = SendMessage(hwnd,SCI_FINDCOLUMN,(WPARAM)iRcCurLine,(LPARAM)iRcCurCol);
int iAnchorPos = SendMessage(hwnd,SCI_FINDCOLUMN,(WPARAM)iRcAnchorLine,(LPARAM)iRcAnchorCol);
SendMessage(hwnd,SCI_SETRECTANGULARSELECTIONCARET,(WPARAM)iCurPos,0);
SendMessage(hwnd,SCI_SETRECTANGULARSELECTIONANCHOR,(WPARAM)iAnchorPos,0);
}
}
//=============================================================================
//
// EditStripFirstCharacter()
//
void EditStripFirstCharacter(HWND hwnd)
{
int iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
int iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
if (iSelStart == iSelEnd) {
iSelStart = 0;
iSelEnd = SendMessage(hwnd,SCI_GETLENGTH,0,0);
}
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iLine;
int iLineStart = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
int iLineEnd = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0);
if (iSelStart > SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0))
iLineStart++;
if (iSelEnd <= SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd,0))
iLineEnd--;
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
for (iLine = iLineStart; iLine <= iLineEnd; iLine++)
{
int iPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
if (SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0)- iPos > 0)
{
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_SETTARGETEND,
(WPARAM)SendMessage(hwnd,SCI_POSITIONAFTER,(WPARAM)iPos,0),0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
}
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditStripLastCharacter()
//
void EditStripLastCharacter(HWND hwnd)
{
int iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
int iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
if (iSelStart == iSelEnd) {
iSelStart = 0;
iSelEnd = SendMessage(hwnd,SCI_GETLENGTH,0,0);
}
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iLine;
int iLineStart = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
int iLineEnd = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0);
if (iSelStart >= SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLineStart,0))
iLineStart++;
if (iSelEnd < SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLineEnd,0))
iLineEnd--;
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
for (iLine = iLineStart; iLine <= iLineEnd; iLine++)
{
int iStartPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
int iEndPos = SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0);
if (iEndPos - iStartPos > 0)
{
SendMessage(hwnd,SCI_SETTARGETSTART,
(WPARAM)SendMessage(hwnd,SCI_POSITIONBEFORE,(WPARAM)iEndPos,0),0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iEndPos,0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
}
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditStripTrailingBlanks()
//
void EditStripTrailingBlanks(HWND hwnd,BOOL bIgnoreSelection)
{
// Check if there is any selection... simply use a regular expression replace!
if (!bIgnoreSelection &&
(SendMessage(hwnd,SCI_GETSELECTIONEND,0,0) - SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0) != 0))
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
EDITFINDREPLACE efrTrim = { "[ \t]+$", "", "", "", SCFIND_REGEXP, 0, 0, 0, 0, 0, hwnd };
EditReplaceAllInSelection(hwnd,&efrTrim,FALSE);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
// Code from SciTE...
else
{
int line;
int maxLines;
int lineStart;
int lineEnd;
int i;
char ch;
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
maxLines = SendMessage(hwnd,SCI_GETLINECOUNT,0,0);
for (line = 0; line < maxLines; line++)
{
lineStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,line,0);
lineEnd = SendMessage(hwnd,SCI_GETLINEENDPOSITION,line,0);
i = lineEnd-1;
ch = (char)SendMessage(hwnd,SCI_GETCHARAT,i,0);
while ((i >= lineStart) && ((ch == ' ') || (ch == '\t')))
{
i--;
ch = (char)SendMessage(hwnd,SCI_GETCHARAT,i,0);
}
if (i < (lineEnd-1))
{
SendMessage(hwnd,SCI_SETTARGETSTART,i + 1,0);
SendMessage(hwnd,SCI_SETTARGETEND,lineEnd,0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
}
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
}
//=============================================================================
//
// EditCompressSpaces()
//
void EditCompressSpaces(HWND hwnd)
{
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
int iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
int iLineStart = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
int iLineEnd = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0);
int iLength = SendMessage(hwnd,SCI_GETLENGTH,0,0);
char* pszIn;
char* pszOut;
BOOL bIsLineStart, bIsLineEnd;
BOOL bModified = FALSE;
if (iSelStart != iSelEnd) {
int cch = SendMessage(hwnd,SCI_GETSELTEXT,0,0);
pszIn = LocalAlloc(LPTR,cch);
pszOut = LocalAlloc(LPTR,cch);
SendMessage(hwnd,SCI_GETSELTEXT,0,(LPARAM)pszIn);
bIsLineStart =
(iSelStart == SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0));
bIsLineEnd =
(iSelEnd == SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLineEnd,0));
}
else {
int cch = iLength + 1;
pszIn = LocalAlloc(GPTR,cch);
pszOut = LocalAlloc(GPTR,cch);
SendMessage(hwnd,SCI_GETTEXT,(WPARAM)cch,(LPARAM)pszIn);
bIsLineStart = TRUE;
bIsLineEnd = TRUE;
}
if (pszIn && pszOut) {
char *ci, *co = pszOut;
for (ci = pszIn; *ci; ci++) {
if (*ci == ' ' || *ci == '\t') {
if (*ci == '\t')
bModified = TRUE;
while (*(ci+1) == ' ' || *(ci+1) == '\t') {
ci++;
bModified = TRUE;
}
if (!bIsLineStart && (*(ci+1) != '\n' && *(ci+1) != '\r'))
*co++ = ' ';
else
bModified = TRUE;
}
else {
if (*ci == '\n' || *ci == '\r')
bIsLineStart = TRUE;
else
bIsLineStart = FALSE;
*co++ = *ci;
}
}
if (bIsLineEnd && co > pszOut && *(co-1) == ' ') {
*--co = 0;
bModified = TRUE;
}
if (bModified) {
if (iSelStart != iSelEnd)
SendMessage(hwnd,SCI_TARGETFROMSELECTION,0,0);
else {
SendMessage(hwnd,SCI_SETTARGETSTART,0,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iLength,0);
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)-1,(LPARAM)pszOut);
if (iCurPos > iAnchorPos) {
iCurPos = SendMessage(hwnd,SCI_GETTARGETEND,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETTARGETSTART,0,0);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
}
else if (iCurPos < iAnchorPos) {
iCurPos = SendMessage(hwnd,SCI_GETTARGETSTART,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETTARGETEND,0,0);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
}
if (pszIn)
LocalFree(pszIn);
if (pszOut)
LocalFree(pszOut);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditRemoveBlankLines()
//
void EditRemoveBlankLines(HWND hwnd)
{
int iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
int iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
if (iSelStart == iSelEnd) {
iSelStart = 0;
iSelEnd = SendMessage(hwnd,SCI_GETLENGTH,0,0);
}
if (SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0))
{
int iLine;
int iLineStart = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
int iLineEnd = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0);
if (iSelStart > SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0))
iLineStart++;
if (iSelEnd <= SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd,0))
iLineEnd--;
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
for (iLine = iLineStart; iLine <= iLineEnd; )
{
int iPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
if (SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0) == iPos)
{
int iPos2 = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine+1,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iPos,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iPos2,0);
SendMessage(hwnd,SCI_REPLACETARGET,0,(LPARAM)"");
iLineEnd--;
}
else
iLine++;
}
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
else
MsgBox(MBINFO,IDS_SELRECT);
}
//=============================================================================
//
// EditWrapToColumn()
//
void EditWrapToColumn(HWND hwnd,int nColumn/*,int nTabWidth*/)
{
char* pszText;
LPWSTR pszTextW;
int cchTextW;
int iTextW;
LPWSTR pszConvW;
int cchConvW;
int cchConvM;
int iLineLength;
int iLine;
int iCurPos;
int iAnchorPos;
int iSelStart;
int iSelEnd;
int iSelCount;
UINT cpEdit;
struct TextRange tr;
int cEOLMode;
WCHAR wszEOL[] = L"\r\n";
int cchEOL = 2;
BOOL bModified = FALSE;
if (SC_SEL_RECTANGLE == SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
MsgBox(MBINFO,IDS_SELRECT);
return;
}
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos == iAnchorPos) /*{
iSelStart = 0;
iSelEnd = SendMessage(hwnd,SCI_GETLENGTH,0,0);
}*/
return;
else {
iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
}
iLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
iSelStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
iSelCount = iSelEnd - iSelStart;
pszText = GlobalAlloc(GPTR,(iSelCount)+2);
if (pszText == NULL)
return;
pszTextW = GlobalAlloc(GPTR,(iSelCount*2)+2);
if (pszTextW == NULL) {
GlobalFree(pszText);
return;
}
tr.chrg.cpMin = iSelStart;
tr.chrg.cpMax = iSelEnd;
tr.lpstrText = pszText;
SendMessage(hwnd,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
cchTextW = MultiByteToWideChar(cpEdit,0,pszText,iSelCount,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
GlobalFree(pszText);
pszConvW = GlobalAlloc(GPTR,cchTextW*sizeof(WCHAR)*3+2);
if (pszConvW == NULL) {
GlobalFree(pszTextW);
return;
}
cEOLMode = SendMessage(hwnd,SCI_GETEOLMODE,0,0);
if (cEOLMode == SC_EOL_CR)
cchEOL = 1;
else if (cEOLMode == SC_EOL_LF) {
cchEOL = 1; wszEOL[0] = L'\n';
}
cchConvW = 0;
iLineLength = 0;
#define ISDELIMITER(wc) StrChr(L",;.:-+%&¦|/*?!\"\'~´#=",wc)
#define ISWHITE(wc) StrChr(L" \t",wc)
#define ISWORDEND(wc) (/*ISDELIMITER(wc) ||*/ StrChr(L" \t\r\n",wc))
for (iTextW = 0; iTextW < cchTextW; iTextW++)
{
WCHAR w;
w = pszTextW[iTextW];
//if (ISDELIMITER(w))
//{
// int iNextWordLen = 0;
// WCHAR w2 = pszTextW[iTextW + 1];
// if (iLineLength + iNextWordLen + 1 > nColumn) {
// pszConvW[cchConvW++] = wszEOL[0];
// if (cchEOL > 1)
// pszConvW[cchConvW++] = wszEOL[1];
// iLineLength = 0;
// bModified = TRUE;
// }
// while (w2 != L'\0' && !ISWORDEND(w2)) {
// iNextWordLen++;
// w2 = pszTextW[iTextW + iNextWordLen + 1];
// }
// if (ISDELIMITER(w2) && iNextWordLen > 0) // delimiters go with the word
// iNextWordLen++;
// pszConvW[cchConvW++] = w;
// iLineLength++;
// if (iNextWordLen > 0)
// {
// if (iLineLength + iNextWordLen + 1 > nColumn) {
// pszConvW[cchConvW++] = wszEOL[0];
// if (cchEOL > 1)
// pszConvW[cchConvW++] = wszEOL[1];
// iLineLength = 0;
// bModified = TRUE;
// }
// }
//}
if (ISWHITE(w))
{
int iNextWordLen = 0;
WCHAR w2;
while (pszTextW[iTextW+1] == L' ' || pszTextW[iTextW+1] == L'\t') {
iTextW++;
bModified = TRUE;
} // Modified: left out some whitespaces
w2 = pszTextW[iTextW + 1];
while (w2 != L'\0' && !ISWORDEND(w2)) {
iNextWordLen++;
w2 = pszTextW[iTextW + iNextWordLen + 1];
}
//if (ISDELIMITER(w2) /*&& iNextWordLen > 0*/) // delimiters go with the word
// iNextWordLen++;
if (iNextWordLen > 0)
{
if (iLineLength + iNextWordLen + 1 > nColumn) {
pszConvW[cchConvW++] = wszEOL[0];
if (cchEOL > 1)
pszConvW[cchConvW++] = wszEOL[1];
iLineLength = 0;
bModified = TRUE;
}
else {
if (iLineLength > 0) {
pszConvW[cchConvW++] = L' ';
iLineLength++;
}
}
}
}
else
{
pszConvW[cchConvW++] = w;
if (w == L'\r' || w == L'\n') {
iLineLength = 0;
}
else {
iLineLength++;
}
}
}
GlobalFree(pszTextW);
if (bModified) {
pszText = GlobalAlloc(GPTR,cchConvW * 3);
cchConvM = WideCharToMultiByte(cpEdit,0,pszConvW,cchConvW,pszText,GlobalSize(pszText),NULL,NULL);
GlobalFree(pszConvW);
if (iAnchorPos > iCurPos) {
iCurPos = iSelStart;
iAnchorPos = iSelStart + cchConvM;
}
else {
iAnchorPos = iSelStart;
iCurPos = iSelStart + cchConvM;
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iSelStart,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iSelEnd,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)cchConvM,(LPARAM)pszText);
//SendMessage(hwnd,SCI_CLEAR,0,0);
//SendMessage(hwnd,SCI_ADDTEXT,(WPARAM)cchConvW,(LPARAM)pszText);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
GlobalFree(pszText);
}
else
GlobalFree(pszConvW);
}
//=============================================================================
//
// EditJoinLinesEx()
//
void EditJoinLinesEx(HWND hwnd)
{
char* pszText;
char* pszJoin;
int cchJoin = 0;
int i;
int iLine;
int iCurPos;
int iAnchorPos;
int iSelStart;
int iSelEnd;
int iSelCount;
struct TextRange tr;
int cEOLMode;
char szEOL[] = "\r\n";
int cchEOL = 2;
BOOL bModified = FALSE;
if (SC_SEL_RECTANGLE == SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
MsgBox(MBINFO,IDS_SELRECT);
return;
}
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos == iAnchorPos)
return;
else {
iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
}
iLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
iSelStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
iSelCount = iSelEnd - iSelStart;
pszText = LocalAlloc(LPTR,(iSelCount)+2);
if (pszText == NULL)
return;
pszJoin = LocalAlloc(LPTR,LocalSize(pszText));
if (pszJoin == NULL) {
LocalFree(pszText);
return;
}
tr.chrg.cpMin = iSelStart;
tr.chrg.cpMax = iSelEnd;
tr.lpstrText = pszText;
SendMessage(hwnd,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
cEOLMode = SendMessage(hwnd,SCI_GETEOLMODE,0,0);
if (cEOLMode == SC_EOL_CR)
cchEOL = 1;
else if (cEOLMode == SC_EOL_LF) {
cchEOL = 1;
szEOL[0] = '\n';
}
cchJoin = 0;
for (i = 0; i < iSelCount; i++)
{
if (pszText[i] == '\r' || pszText[i] == '\n') {
if (pszText[i] == '\r' && pszText[i+1] == '\n')
i++;
if (!StrChrA("\r\n",pszText[i+1]) && pszText[i+1] != 0) {
pszJoin[cchJoin++] = ' ';
bModified = TRUE;
}
else {
while (StrChrA("\r\n",pszText[i+1])) {
i++;
bModified = TRUE;
}
if (pszText[i+1] != 0) {
pszJoin[cchJoin++] = szEOL[0];
if (cchEOL > 1)
pszJoin[cchJoin++] = szEOL[1];
if (cchJoin > cchEOL) {
pszJoin[cchJoin++] = szEOL[0];
if (cchEOL > 1)
pszJoin[cchJoin++] = szEOL[1];
}
}
}
}
else {
pszJoin[cchJoin++] = pszText[i];
}
}
LocalFree(pszText);
if (bModified) {
if (iAnchorPos > iCurPos) {
iCurPos = iSelStart;
iAnchorPos = iSelStart + cchJoin;
}
else {
iAnchorPos = iSelStart;
iCurPos = iSelStart + cchJoin;
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)iSelStart,0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)iSelEnd,0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)cchJoin,(LPARAM)pszJoin);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
}
LocalFree(pszJoin);
}
//=============================================================================
//
// EditSortLines()
//
static FARPROC pfnStrCmpLogicalW;
int CmpStd(const void *s1, const void *s2) {
return StrCmp(*(WCHAR**)s1,*(WCHAR**)s2);
}
int CmpStdRev(const void *s1, const void *s2) {
return -1 * StrCmp(*(WCHAR**)s1,*(WCHAR**)s2);
}
int CmpLogical(const void *s1, const void *s2) {
int i = pfnStrCmpLogicalW(*(WCHAR**)s1,*(WCHAR**)s2);
if (i != 0)
return i;
else
return StrCmp(*(WCHAR**)s1,*(WCHAR**)s2);
}
int CmpLogicalRev(const void *s1, const void *s2) {
int i = -1 * pfnStrCmpLogicalW(*(WCHAR**)s1,*(WCHAR**)s2);
if (i != 0)
return i;
else
return -1 * StrCmp(*(WCHAR**)s1,*(WCHAR**)s2);
}
void EditSortLines(HWND hwnd,int iSortFlags)
{
int iCurPos;
int iAnchorPos;
int iSelStart;
int iSelEnd;
int iLineStart;
int iLineEnd;
int iLineCount;
int i, iLine;
int cchTotal = 0;
int ichlMax = 3;
WCHAR **pwszLines;
char *pmszResult;
char *pmszBuf;
UINT uCodePage;
DWORD cEOLMode;
char mszEOL[] = "\r\n";
if (SC_SEL_RECTANGLE == SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
MsgBox(MBINFO,IDS_SELRECT);
return;
}
pfnStrCmpLogicalW = GetProcAddress(GetModuleHandle(L"shlwapi"),"StrCmpLogicalW");
iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos == iAnchorPos)
return;
else {
iSelStart = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
iSelEnd = SendMessage(hwnd,SCI_GETSELECTIONEND,0,0);
}
iLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
iSelStart = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
iLineStart = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelStart,0);
iLineEnd = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iSelEnd,0);
if (iSelEnd <= SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd,0))
iLineEnd--;
iLineCount = iLineEnd - iLineStart +1;
if (iLineCount < 2)
return;
uCodePage = (SendMessage(hwnd,SCI_GETCODEPAGE,0,0) == SC_CP_UTF8) ? CP_UTF8 : CP_ACP;
cEOLMode = SendMessage(hwnd,SCI_GETEOLMODE,0,0);
if (cEOLMode == SC_EOL_CR) {
mszEOL[1] = 0;
}
else if (cEOLMode == SC_EOL_LF) {
mszEOL[0] = '\n';
mszEOL[1] = 0;
}
pwszLines = LocalAlloc(LPTR,sizeof(WCHAR*) * iLineCount);
i = 0;
for (iLine = iLineStart; iLine <= iLineEnd; iLine++) {
char *pmsz;
int cchw;
int cchm = SendMessage(hwnd,SCI_GETLINE,(WPARAM)iLine,0);
pmsz = LocalAlloc(LPTR,cchm+1);
SendMessage(hwnd,SCI_GETLINE,(WPARAM)iLine,(LPARAM)pmsz);
StrTrimA(pmsz,"\r\n");
cchTotal += cchm;
ichlMax = max(ichlMax,cchm);
cchw = MultiByteToWideChar(uCodePage,0,pmsz,-1,NULL,0) -1;
if (cchw > 0) {
pwszLines[i] = LocalAlloc(LPTR,sizeof(WCHAR) * (cchw +1));
MultiByteToWideChar(uCodePage,0,pmsz,-1,pwszLines[i],LocalSize(pwszLines[i])/sizeof(WCHAR));
}
else
pwszLines[i] = StrDup(L"");
LocalFree(pmsz);
i++;
}
if (iSortFlags & SORT_DESCENDING) {
if (iSortFlags & SORT_LOGICAL && pfnStrCmpLogicalW)
qsort(pwszLines,iLineCount,sizeof(WCHAR*),CmpLogicalRev);
else
qsort(pwszLines,iLineCount,sizeof(WCHAR*),CmpStdRev);
}
else {
if (iSortFlags & SORT_LOGICAL && pfnStrCmpLogicalW)
qsort(pwszLines,iLineCount,sizeof(WCHAR*),CmpLogical);
else
qsort(pwszLines,iLineCount,sizeof(WCHAR*),CmpStd);
}
pmszResult = LocalAlloc(LPTR,cchTotal + 2*iLineCount +1);
pmszBuf = LocalAlloc(LPTR,ichlMax +1);
for (i = 0; i < iLineCount; i++) {
BOOL bDuplicate = FALSE;
if (pwszLines[i] && lstrlen(pwszLines[i])) {
if (iSortFlags & SORT_UNIQ) {
if (i < iLineCount-1) {
if (StrCmp(pwszLines[i],pwszLines[i+1]) == 0) {
bDuplicate = TRUE;
}
}
}
if (!bDuplicate) {
WideCharToMultiByte(uCodePage,0,pwszLines[i],-1,pmszBuf,LocalSize(pmszBuf),NULL,NULL);
StrCatBuffA(pmszResult,pmszBuf,LocalSize(pmszResult));
StrCatBuffA(pmszResult,mszEOL,LocalSize(pmszResult));
}
}
}
LocalFree(pmszBuf);
for (i = 0; i < iLineCount; i++) {
if (pwszLines[i])
LocalFree(pwszLines[i]);
}
LocalFree(pwszLines);
if (iAnchorPos > iCurPos) {
iCurPos = iSelStart;
iAnchorPos = iSelStart + lstrlenA(pmszResult);
}
else {
iAnchorPos = iSelStart;
iCurPos = iSelStart + lstrlenA(pmszResult);
}
SendMessage(hwnd,SCI_BEGINUNDOACTION,0,0);
SendMessage(hwnd,SCI_SETTARGETSTART,(WPARAM)SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineStart,0),0);
SendMessage(hwnd,SCI_SETTARGETEND,(WPARAM)SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLineEnd+1,0),0);
SendMessage(hwnd,SCI_REPLACETARGET,(WPARAM)lstrlenA(pmszResult),(LPARAM)pmszResult);
SendMessage(hwnd,SCI_SETSEL,(WPARAM)iAnchorPos,(LPARAM)iCurPos);
SendMessage(hwnd,SCI_ENDUNDOACTION,0,0);
LocalFree(pmszResult);
}
//=============================================================================
//
// EditJumpTo()
//
void EditJumpTo(HWND hwnd,int iNewLine,int iNewCol)
{
//int iScrLines;
//int iTopLine;
int iMaxLine = SendMessage(hwnd,SCI_GETLINECOUNT,0,0);
// Jumpt to end with line set to -1
if (iNewLine == -1) {
SendMessage(hwnd,SCI_DOCUMENTEND,0,0);
return;
}
// Line maximum is iMaxLine
iNewLine = min(iNewLine,iMaxLine);
// Column minimum is 1
iNewCol = max(iNewCol,1);
if (iNewLine > 0 && iNewLine <= iMaxLine && iNewCol > 0)
{
int iNewPos = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iNewLine-1,0);
int iLineEndPos = SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iNewLine-1,0);
while (iNewCol-1 > SendMessage(hwnd,SCI_GETCOLUMN,(WPARAM)iNewPos,0))
{
if (iNewPos >= iLineEndPos)
break;
iNewPos = SendMessage(hwnd,SCI_POSITIONAFTER,(WPARAM)iNewPos,0);
}
iNewPos = min(iNewPos,iLineEndPos);
EditSelectEx(hwnd,-1,iNewPos); // SCI_GOTOPOS(pos) is equivalent to SCI_SETSEL(-1, pos)
SendMessage(hwnd,SCI_CHOOSECARETX,0,0);
}
}
//=============================================================================
//
// EditSelectEx()
//
void EditSelectEx(HWND hwnd,int iAnchorPos,int iCurrentPos)
{
//int iScrLines;
//int iTopLine;
//int iNewLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iCurrentPos,0);
int iNewLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iCurrentPos,0);
int iAnchorLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iAnchorPos,0);
// Ensure that the first and last lines of a selection are always unfolded
// This needs to be done *before* the SCI_SETSEL message
SciCall_EnsureVisible(iAnchorLine);
if (iAnchorLine != iNewLine)
SciCall_EnsureVisible(iNewLine);
SendMessage(hwnd,SCI_SETXCARETPOLICY,CARET_SLOP|CARET_STRICT|CARET_EVEN,50);
SendMessage(hwnd,SCI_SETYCARETPOLICY,CARET_SLOP|CARET_STRICT|CARET_EVEN,5);
SendMessage(hwnd,SCI_SETSEL,iAnchorPos,iCurrentPos);
SendMessage(hwnd,SCI_SETXCARETPOLICY,CARET_SLOP|CARET_EVEN,50);
SendMessage(hwnd,SCI_SETYCARETPOLICY,CARET_EVEN,0);
//iScrLines = SendMessage(hwnd,SCI_LINESONSCREEN,0,0);
//iTopLine = SendMessage(hwnd,SCI_GETFIRSTVISIBLELINE,0,0);
//if (iScrLines > 20) {
// if (iNewLine - iTopLine < 5)
// SendMessage(hwnd,SCI_LINESCROLL,0,(LPARAM)max(iNewLine-5,0)-iTopLine);
// else if (iNewLine - (iTopLine + iScrLines) > -5)
// SendMessage(hwnd,SCI_LINESCROLL,0,(LPARAM)max(iNewLine-iScrLines+5,0)-iTopLine);
//}
}
//=============================================================================
//
// EditGetExcerpt()
//
void EditGetExcerpt(HWND hwnd,LPWSTR lpszExcerpt,DWORD cchExcerpt)
{
WCHAR tch[256] = L"";
WCHAR *p;
DWORD cch = 0;
UINT cpEdit;
struct TextRange tr;
char* pszText;
LPWSTR pszTextW;
int iCurPos = SendMessage(hwnd,SCI_GETCURRENTPOS,0,0);
int iAnchorPos = SendMessage(hwnd,SCI_GETANCHOR,0,0);
if (iCurPos == iAnchorPos || SC_SEL_RECTANGLE == SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {
lstrcpy(lpszExcerpt,L"");
return;
}
/*if (iCurPos != iAnchorPos && SC_SEL_RECTANGLE != SendMessage(hwnd,SCI_GETSELECTIONMODE,0,0)) {*/
tr.chrg.cpMin = SendMessage(hwnd,SCI_GETSELECTIONSTART,0,0);
tr.chrg.cpMax = min(SendMessage(hwnd,SCI_GETSELECTIONEND,0,0),(LONG)(tr.chrg.cpMin + COUNTOF(tch)));
/*}
else {
int iLine = SendMessage(hwnd,SCI_LINEFROMPOSITION,(WPARAM)iCurPos,0);
tr.chrg.cpMin = SendMessage(hwnd,SCI_POSITIONFROMLINE,(WPARAM)iLine,0);
tr.chrg.cpMax = min(SendMessage(hwnd,SCI_GETLINEENDPOSITION,(WPARAM)iLine,0),(LONG)(tr.chrg.cpMin + COUNTOF(tch)));
}*/
tr.chrg.cpMax = min(SendMessage(hwnd,SCI_GETLENGTH,0,0),tr.chrg.cpMax);
pszText = LocalAlloc(LPTR,(tr.chrg.cpMax - tr.chrg.cpMin)+2);
pszTextW = LocalAlloc(LPTR,((tr.chrg.cpMax - tr.chrg.cpMin)*2)+2);
if (pszText && pszTextW) {
tr.lpstrText = pszText;
SendMessage(hwnd,SCI_GETTEXTRANGE,0,(LPARAM)&tr);
cpEdit = SendMessage(hwnd,SCI_GETCODEPAGE,0,0);
MultiByteToWideChar(cpEdit,0,pszText,tr.chrg.cpMax - tr.chrg.cpMin,pszTextW,GlobalSize(pszTextW)/sizeof(WCHAR));
for (p = pszTextW; *p && cch < COUNTOF(tch)-1; p++) {
if (*p == L'\r' || *p == L'\n' || *p == L'\t' || *p == L' ') {
tch[cch++] = L' ';
while (*(p+1) == L'\r' || *(p+1) == L'\n' || *(p+1) == L'\t' || *(p+1) == L' ')
p++;
}
else
tch[cch++] = *p;
}
tch[cch++] = L'\0';
StrTrim(tch,L" ");
}
if (cch == 1)
lstrcpy(tch,L" ... ");
if (cch > cchExcerpt) {
tch[cchExcerpt-2] = L'.';
tch[cchExcerpt-3] = L'.';
tch[cchExcerpt-4] = L'.';
}
StrCpyN(lpszExcerpt,tch,cchExcerpt);
if (pszText)
LocalFree(pszText);
if (pszTextW)
LocalFree(pszTextW);
}
//=============================================================================
//
// EditFindReplaceDlgProcW()
//
BOOL CALLBACK EditFindReplaceDlgProcW(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM lParam)
{