1 // Scintilla source code edit control
2 /** @file ScintillaWin.cxx
3 ** Windows specific subclass of ScintillaBase.
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
21 #define _WIN32_WINNT 0x0500
29 #if defined(NTDDI_WIN7) && !defined(DISABLE_D2D)
41 #include "Scintilla.h"
45 #include "LexerModule.h"
47 #include "SplitVector.h"
48 #include "Partitioning.h"
49 #include "RunStyles.h"
50 #include "ContractionState.h"
51 #include "CellBuffer.h"
54 #include "Indicator.h"
56 #include "LineMarker.h"
58 #include "AutoComplete.h"
59 #include "ViewStyle.h"
60 #include "CharClassify.h"
61 #include "Decoration.h"
63 #include "Selection.h"
64 #include "PositionCache.h"
66 #include "ScintillaBase.h"
67 #include "UniConversion.h"
71 #include "ExternalLexer.h"
74 #ifndef SPI_GETWHEELSCROLLLINES
75 #define SPI_GETWHEELSCROLLLINES 104
79 #define WM_UNICHAR 0x0109
82 #ifndef UNICODE_NOCHAR
83 #define UNICODE_NOCHAR 0xFFFF
86 #ifndef WM_IME_STARTCOMPOSITION
100 #define SC_WIN_IDLE 5001
102 typedef BOOL (WINAPI
*TrackMouseEventSig
)(LPTRACKMOUSEEVENT
);
104 // GCC has trouble with the standard COM ABI so do it the old C way with explicit vtables.
106 const TCHAR scintillaClassName
[] = TEXT("Scintilla");
107 const TCHAR callClassName
[] = TEXT("CallTip");
110 using namespace Scintilla
;
113 // Take care of 32/64 bit pointers
114 #ifdef GetWindowLongPtr
115 static void *PointerFromWindow(HWND hWnd
) {
116 return reinterpret_cast<void *>(::GetWindowLongPtr(hWnd
, 0));
118 static void SetWindowPointer(HWND hWnd
, void *ptr
) {
119 ::SetWindowLongPtr(hWnd
, 0, reinterpret_cast<LONG_PTR
>(ptr
));
121 static void SetWindowID(HWND hWnd
, int identifier
) {
122 ::SetWindowLongPtr(hWnd
, GWLP_ID
, identifier
);
125 static void *PointerFromWindow(HWND hWnd
) {
126 return reinterpret_cast<void *>(::GetWindowLong(hWnd
, 0));
128 static void SetWindowPointer(HWND hWnd
, void *ptr
) {
129 ::SetWindowLong(hWnd
, 0, reinterpret_cast<LONG
>(ptr
));
131 static void SetWindowID(HWND hWnd
, int identifier
) {
132 ::SetWindowLong(hWnd
, GWL_ID
, identifier
);
136 class ScintillaWin
; // Forward declaration for COM interface subobjects
138 typedef void VFunction(void);
142 class FormatEnumerator
{
147 CLIPFORMAT formats
[2];
149 FormatEnumerator(int pos_
, CLIPFORMAT formats_
[], int formatsLen_
);
182 public ScintillaBase
{
184 bool lastKeyDownConsumed
;
187 bool trackedMouseLeave
;
188 TrackMouseEventSig TrackMouseEventFn
;
190 unsigned int linesPerScroll
; ///< Intellimouse support
191 int wheelDelta
; ///< Wheel delta from roll
197 CLIPFORMAT cfColumnSelect
;
198 CLIPFORMAT cfLineSelect
;
205 static HINSTANCE hInstance
;
208 ID2D1HwndRenderTarget
*pRenderTarget
;
209 bool renderTargetValid
;
212 ScintillaWin(HWND hwnd
);
213 ScintillaWin(const ScintillaWin
&);
214 virtual ~ScintillaWin();
215 ScintillaWin
&operator=(const ScintillaWin
&);
217 virtual void Initialise();
218 virtual void Finalise();
219 void EnsureRenderTarget();
220 void DropRenderTarget();
223 static sptr_t
DirectFunction(
224 ScintillaWin
*sci
, UINT iMessage
, uptr_t wParam
, sptr_t lParam
);
225 static sptr_t PASCAL
SWndProc(
226 HWND hWnd
, UINT iMessage
, WPARAM wParam
, sptr_t lParam
);
227 static sptr_t PASCAL
CTWndProc(
228 HWND hWnd
, UINT iMessage
, WPARAM wParam
, sptr_t lParam
);
230 enum { invalidTimerID
, standardTimerID
, idleTimerID
};
232 virtual bool DragThreshold(Point ptStart
, Point ptNow
);
233 virtual void StartDrag();
234 sptr_t
WndPaint(uptr_t wParam
);
235 sptr_t
HandleComposition(uptr_t wParam
, sptr_t lParam
);
236 UINT
CodePageOfDocument();
237 virtual bool ValidCodePage(int codePage
) const;
238 virtual sptr_t
DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
239 virtual bool SetIdle(bool on
);
240 virtual void SetTicking(bool on
);
241 virtual void SetMouseCapture(bool on
);
242 virtual bool HaveMouseCapture();
243 virtual void SetTrackMouseLeaveEvent(bool on
);
244 virtual bool PaintContains(PRectangle rc
);
245 virtual void ScrollText(int linesToMove
);
246 virtual void UpdateSystemCaret();
247 virtual void SetVerticalScrollPos();
248 virtual void SetHorizontalScrollPos();
249 virtual bool ModifyScrollBars(int nMax
, int nPage
);
250 virtual void NotifyChange();
251 virtual void NotifyFocus(bool focus
);
252 virtual void SetCtrlID(int identifier
);
253 virtual int GetCtrlID();
254 virtual void NotifyParent(SCNotification scn
);
255 virtual void NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
);
256 virtual CaseFolder
*CaseFolderForEncoding();
257 virtual std::string
CaseMapString(const std::string
&s
, int caseMapping
);
259 virtual void CopyAllowLine();
260 virtual bool CanPaste();
261 virtual void Paste();
262 virtual void CreateCallTipWindow(PRectangle rc
);
263 virtual void AddToPopUp(const char *label
, int cmd
= 0, bool enabled
= true);
264 virtual void ClaimSelection();
267 void ImeStartComposition();
268 void ImeEndComposition();
270 void AddCharBytes(char b0
, char b1
);
272 void GetIntelliMouseParameters();
273 virtual void CopyToClipboard(const SelectionText
&selectedText
);
274 void ScrollMessage(WPARAM wParam
);
275 void HorizontalScrollMessage(WPARAM wParam
);
277 void FullPaintDC(HDC dc
);
278 bool IsCompatibleDC(HDC dc
);
279 DWORD
EffectFromState(DWORD grfKeyState
);
281 virtual int SetScrollInfo(int nBar
, LPCSCROLLINFO lpsi
, BOOL bRedraw
);
282 virtual bool GetScrollInfo(int nBar
, LPSCROLLINFO lpsi
);
283 void ChangeScrollPos(int barType
, int pos
);
285 void InsertPasteText(const char *text
, int len
, SelectionPosition selStart
, bool isRectangular
, bool isLine
);
288 // Public for benefit of Scintilla_DirectFunction
289 virtual sptr_t
WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
);
291 /// Implement IUnknown
292 STDMETHODIMP
QueryInterface(REFIID riid
, PVOID
*ppv
);
293 STDMETHODIMP_(ULONG
)AddRef();
294 STDMETHODIMP_(ULONG
)Release();
296 /// Implement IDropTarget
297 STDMETHODIMP
DragEnter(LPDATAOBJECT pIDataSource
, DWORD grfKeyState
,
298 POINTL pt
, PDWORD pdwEffect
);
299 STDMETHODIMP
DragOver(DWORD grfKeyState
, POINTL pt
, PDWORD pdwEffect
);
300 STDMETHODIMP
DragLeave();
301 STDMETHODIMP
Drop(LPDATAOBJECT pIDataSource
, DWORD grfKeyState
,
302 POINTL pt
, PDWORD pdwEffect
);
304 /// Implement important part of IDataObject
305 STDMETHODIMP
GetData(FORMATETC
*pFEIn
, STGMEDIUM
*pSTM
);
307 static bool Register(HINSTANCE hInstance_
);
308 static bool Unregister();
310 friend class DropSource
;
311 friend class DataObject
;
312 friend class DropTarget
;
313 bool DragIsRectangularOK(CLIPFORMAT fmt
) {
314 return drag
.rectangular
&& (fmt
== cfColumnSelect
);
318 // For use in creating a system caret
319 bool HasCaretSizeChanged();
320 BOOL
CreateSystemCaret();
321 BOOL
DestroySystemCaret();
322 HBITMAP sysCaretBitmap
;
325 bool keysAlwaysUnicode
;
328 HINSTANCE
ScintillaWin::hInstance
= 0;
330 ScintillaWin::ScintillaWin(HWND hwnd
) {
332 lastKeyDownConsumed
= false;
334 capturedMouse
= false;
335 trackedMouseLeave
= false;
336 TrackMouseEventFn
= 0;
339 wheelDelta
= 0; // Wheel delta from roll
345 // There does not seem to be a real standard for indicating that the clipboard
346 // contains a rectangular selection, so copy Developer Studio.
347 cfColumnSelect
= static_cast<CLIPFORMAT
>(
348 ::RegisterClipboardFormat(TEXT("MSDEVColumnSelect")));
350 // Likewise for line-copy (copies a full line when no text is selected)
351 cfLineSelect
= static_cast<CLIPFORMAT
>(
352 ::RegisterClipboardFormat(TEXT("MSDEVLineSelect")));
368 renderTargetValid
= true;
371 keysAlwaysUnicode
= false;
373 caret
.period
= ::GetCaretBlinkTime();
374 if (caret
.period
< 0)
380 ScintillaWin::~ScintillaWin() {}
382 void ScintillaWin::Initialise() {
383 // Initialize COM. If the app has already done this it will have
384 // no effect. If the app hasnt, we really shouldnt ask them to call
385 // it just so this internal feature works.
386 hrOle
= ::OleInitialize(NULL
);
388 // Find TrackMouseEvent which is available on Windows > 95
389 HMODULE user32
= ::GetModuleHandle(TEXT("user32.dll"));
391 TrackMouseEventFn
= (TrackMouseEventSig
)::GetProcAddress(user32
, "TrackMouseEvent");
392 if (TrackMouseEventFn
== NULL
) {
393 // Windows 95 has an emulation in comctl32.dll:_TrackMouseEvent
394 HMODULE commctrl32
= ::LoadLibrary(TEXT("comctl32.dll"));
395 if (commctrl32
!= NULL
) {
396 TrackMouseEventFn
= (TrackMouseEventSig
)
397 ::GetProcAddress(commctrl32
, "_TrackMouseEvent");
402 void ScintillaWin::Finalise() {
403 ScintillaBase::Finalise();
407 ::RevokeDragDrop(MainHWND());
408 if (SUCCEEDED(hrOle
)) {
413 void ScintillaWin::EnsureRenderTarget() {
415 if (!renderTargetValid
) {
417 renderTargetValid
= true;
419 if (pD2DFactory
&& !pRenderTarget
) {
421 HWND hw
= MainHWND();
422 GetClientRect(hw
, &rc
);
424 D2D1_SIZE_U size
= D2D1::SizeU(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
);
426 // Create a Direct2D render target.
428 pD2DFactory
->CreateHwndRenderTarget(
429 D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT
, D2D1::PixelFormat(), 96.0, 96.0),
430 D2D1::HwndRenderTargetProperties(hw
, size
),
433 pD2DFactory
->CreateHwndRenderTarget(
434 D2D1::RenderTargetProperties(
435 D2D1_RENDER_TARGET_TYPE_DEFAULT
,
436 D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM
, D2D1_ALPHA_MODE_PREMULTIPLIED
),
437 96.0f
, 96.0f
, D2D1_RENDER_TARGET_USAGE_NONE
, D2D1_FEATURE_LEVEL_DEFAULT
),
438 D2D1::HwndRenderTargetProperties(hw
, size
),
441 // Pixmaps were created to be compatible with previous render target so
442 // need to be recreated.
448 void ScintillaWin::DropRenderTarget() {
451 pRenderTarget
->Release();
457 HWND
ScintillaWin::MainHWND() {
458 return reinterpret_cast<HWND
>(wMain
.GetID());
461 bool ScintillaWin::DragThreshold(Point ptStart
, Point ptNow
) {
462 int xMove
= abs(ptStart
.x
- ptNow
.x
);
463 int yMove
= abs(ptStart
.y
- ptNow
.y
);
464 return (xMove
> ::GetSystemMetrics(SM_CXDRAG
)) ||
465 (yMove
> ::GetSystemMetrics(SM_CYDRAG
));
468 void ScintillaWin::StartDrag() {
469 inDragDrop
= ddDragging
;
471 dropWentOutside
= true;
472 IDataObject
*pDataObject
= reinterpret_cast<IDataObject
*>(&dob
);
473 IDropSource
*pDropSource
= reinterpret_cast<IDropSource
*>(&ds
);
474 //Platform::DebugPrintf("About to DoDragDrop %x %x\n", pDataObject, pDropSource);
475 HRESULT hr
= ::DoDragDrop(
478 DROPEFFECT_COPY
| DROPEFFECT_MOVE
, &dwEffect
);
479 //Platform::DebugPrintf("DoDragDrop = %x\n", hr);
481 if ((hr
== DRAGDROP_S_DROP
) && (dwEffect
== DROPEFFECT_MOVE
) && dropWentOutside
) {
482 // Remove dragged out text
487 SetDragPosition(SelectionPosition(invalidPosition
));
490 // Avoid warnings everywhere for old style casts by concentrating them here
491 static WORD
LoWord(DWORD l
) {
495 static WORD
HiWord(DWORD l
) {
499 static int InputCodePage() {
500 HKL inputLocale
= ::GetKeyboardLayout(0);
501 LANGID inputLang
= LOWORD(inputLocale
);
503 int res
= ::GetLocaleInfoA(MAKELCID(inputLang
, SORT_DEFAULT
),
504 LOCALE_IDEFAULTANSICODEPAGE
, sCodePage
, sizeof(sCodePage
));
507 return atoi(sCodePage
);
511 static const int VK_OEM_2
=0xbf;
512 static const int VK_OEM_3
=0xc0;
513 static const int VK_OEM_4
=0xdb;
514 static const int VK_OEM_5
=0xdc;
515 static const int VK_OEM_6
=0xdd;
518 /** Map the key codes to their equivalent SCK_ form. */
519 static int KeyTranslate(int keyIn
) {
520 //PLATFORM_ASSERT(!keyIn);
522 case VK_DOWN
: return SCK_DOWN
;
523 case VK_UP
: return SCK_UP
;
524 case VK_LEFT
: return SCK_LEFT
;
525 case VK_RIGHT
: return SCK_RIGHT
;
526 case VK_HOME
: return SCK_HOME
;
527 case VK_END
: return SCK_END
;
528 case VK_PRIOR
: return SCK_PRIOR
;
529 case VK_NEXT
: return SCK_NEXT
;
530 case VK_DELETE
: return SCK_DELETE
;
531 case VK_INSERT
: return SCK_INSERT
;
532 case VK_ESCAPE
: return SCK_ESCAPE
;
533 case VK_BACK
: return SCK_BACK
;
534 case VK_TAB
: return SCK_TAB
;
535 case VK_RETURN
: return SCK_RETURN
;
536 case VK_ADD
: return SCK_ADD
;
537 case VK_SUBTRACT
: return SCK_SUBTRACT
;
538 case VK_DIVIDE
: return SCK_DIVIDE
;
539 case VK_LWIN
: return SCK_WIN
;
540 case VK_RWIN
: return SCK_RWIN
;
541 case VK_APPS
: return SCK_MENU
;
542 case VK_OEM_2
: return '/';
543 case VK_OEM_3
: return '`';
544 case VK_OEM_4
: return '[';
545 case VK_OEM_5
: return '\\';
546 case VK_OEM_6
: return ']';
547 default: return keyIn
;
551 LRESULT
ScintillaWin::WndPaint(uptr_t wParam
) {
554 // Redirect assertions to debug output and save current state
555 bool assertsPopup
= Platform::ShowAssertionPopUps(false);
556 paintState
= painting
;
560 bool IsOcxCtrl
= (wParam
!= 0); // if wParam != 0, it contains
561 // a PAINSTRUCT* from the OCX
562 // Removed since this interferes with reporting other assertions as it occurs repeatedly
563 //PLATFORM_ASSERT(hRgnUpdate == NULL);
564 hRgnUpdate
= ::CreateRectRgn(0, 0, 0, 0);
566 pps
= reinterpret_cast<PAINTSTRUCT
*>(wParam
);
568 ::GetUpdateRgn(MainHWND(), hRgnUpdate
, FALSE
);
570 ::BeginPaint(MainHWND(), pps
);
572 if (technology
== SC_TECHNOLOGY_DEFAULT
) {
573 AutoSurface
surfaceWindow(pps
->hdc
, this);
575 rcPaint
= PRectangle(pps
->rcPaint
.left
, pps
->rcPaint
.top
, pps
->rcPaint
.right
, pps
->rcPaint
.bottom
);
576 PRectangle rcClient
= GetClientRectangle();
577 paintingAllText
= rcPaint
.Contains(rcClient
);
578 Paint(surfaceWindow
, rcPaint
);
579 surfaceWindow
->Release();
583 EnsureRenderTarget();
584 AutoSurface
surfaceWindow(pRenderTarget
, this);
586 pRenderTarget
->BeginDraw();
587 rcPaint
= PRectangle(pps
->rcPaint
.left
, pps
->rcPaint
.top
, pps
->rcPaint
.right
, pps
->rcPaint
.bottom
);
588 PRectangle rcClient
= GetClientRectangle();
589 paintingAllText
= rcPaint
.Contains(rcClient
);
590 Paint(surfaceWindow
, rcPaint
);
591 surfaceWindow
->Release();
592 HRESULT hr
= pRenderTarget
->EndDraw();
593 if (hr
== D2DERR_RECREATE_TARGET
) {
600 ::DeleteRgn(hRgnUpdate
);
605 ::EndPaint(MainHWND(), pps
);
606 if (paintState
== paintAbandoned
) {
607 // Painting area was insufficient to cover new styling or brace highlight positions
610 paintState
= notPainting
;
612 // Restore debug output state
613 Platform::ShowAssertionPopUps(assertsPopup
);
615 //Platform::DebugPrintf("Paint took %g\n", et.Duration());
619 sptr_t
ScintillaWin::HandleComposition(uptr_t wParam
, sptr_t lParam
) {
621 // Digital Mars compiler does not include Imm library
624 if (lParam
& GCS_RESULTSTR
) {
625 HIMC hIMC
= ::ImmGetContext(MainHWND());
627 const int maxLenInputIME
= 200;
628 wchar_t wcs
[maxLenInputIME
];
629 LONG bytes
= ::ImmGetCompositionStringW(hIMC
,
630 GCS_RESULTSTR
, wcs
, (maxLenInputIME
-1)*2);
631 int wides
= bytes
/ 2;
632 if (IsUnicodeMode()) {
633 char utfval
[maxLenInputIME
* 3];
634 unsigned int len
= UTF8Length(wcs
, wides
);
635 UTF8FromUTF16(wcs
, wides
, utfval
, len
);
637 AddCharUTF(utfval
, len
);
639 char dbcsval
[maxLenInputIME
* 2];
640 int size
= ::WideCharToMultiByte(InputCodePage(),
641 0, wcs
, wides
, dbcsval
, sizeof(dbcsval
) - 1, 0, 0);
642 for (int i
=0; i
<size
; i
++) {
646 // Set new position after converted
647 Point pos
= PointMainCaret();
648 COMPOSITIONFORM CompForm
;
649 CompForm
.dwStyle
= CFS_POINT
;
650 CompForm
.ptCurrentPos
.x
= pos
.x
;
651 CompForm
.ptCurrentPos
.y
= pos
.y
;
652 ::ImmSetCompositionWindow(hIMC
, &CompForm
);
653 ::ImmReleaseContext(MainHWND(), hIMC
);
657 return ::DefWindowProc(MainHWND(), WM_IME_COMPOSITION
, wParam
, lParam
);
661 // Translate message IDs from WM_* and EM_* to SCI_* so can partly emulate Windows Edit control
662 static unsigned int SciMessageFromEM(unsigned int iMessage
) {
664 case EM_CANPASTE
: return SCI_CANPASTE
;
665 case EM_CANUNDO
: return SCI_CANUNDO
;
666 case EM_EMPTYUNDOBUFFER
: return SCI_EMPTYUNDOBUFFER
;
667 case EM_FINDTEXTEX
: return SCI_FINDTEXT
;
668 case EM_FORMATRANGE
: return SCI_FORMATRANGE
;
669 case EM_GETFIRSTVISIBLELINE
: return SCI_GETFIRSTVISIBLELINE
;
670 case EM_GETLINECOUNT
: return SCI_GETLINECOUNT
;
671 case EM_GETSELTEXT
: return SCI_GETSELTEXT
;
672 case EM_GETTEXTRANGE
: return SCI_GETTEXTRANGE
;
673 case EM_HIDESELECTION
: return SCI_HIDESELECTION
;
674 case EM_LINEINDEX
: return SCI_POSITIONFROMLINE
;
675 case EM_LINESCROLL
: return SCI_LINESCROLL
;
676 case EM_REPLACESEL
: return SCI_REPLACESEL
;
677 case EM_SCROLLCARET
: return SCI_SCROLLCARET
;
678 case EM_SETREADONLY
: return SCI_SETREADONLY
;
679 case WM_CLEAR
: return SCI_CLEAR
;
680 case WM_COPY
: return SCI_COPY
;
681 case WM_CUT
: return SCI_CUT
;
682 case WM_GETTEXT
: return SCI_GETTEXT
;
683 case WM_SETTEXT
: return SCI_SETTEXT
;
684 case WM_GETTEXTLENGTH
: return SCI_GETTEXTLENGTH
;
685 case WM_PASTE
: return SCI_PASTE
;
686 case WM_UNDO
: return SCI_UNDO
;
691 static UINT
CodePageFromCharSet(DWORD characterSet
, UINT documentCodePage
) {
692 if (documentCodePage
== SC_CP_UTF8
) {
693 // The system calls here are a little slow so avoid if known case.
696 CHARSETINFO ci
= { 0, 0, { { 0, 0, 0, 0 }, { 0, 0 } } };
697 BOOL bci
= ::TranslateCharsetInfo((DWORD
*)characterSet
,
698 &ci
, TCI_SRCCHARSET
);
704 cp
= documentCodePage
;
707 if (!IsValidCodePage(cp
) && !GetCPInfo(cp
, &cpi
))
713 UINT
ScintillaWin::CodePageOfDocument() {
714 return CodePageFromCharSet(vs
.styles
[STYLE_DEFAULT
].characterSet
, pdoc
->dbcsCodePage
);
717 sptr_t
ScintillaWin::WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) {
719 //Platform::DebugPrintf("S M:%x WP:%x L:%x\n", iMessage, wParam, lParam);
720 iMessage
= SciMessageFromEM(iMessage
);
724 ctrlID
= ::GetDlgCtrlID(reinterpret_cast<HWND
>(wMain
.GetID()));
725 // Get Intellimouse scroll line parameters
726 GetIntelliMouseParameters();
727 ::RegisterDragDrop(MainHWND(), reinterpret_cast<IDropTarget
*>(&dt
));
731 Command(LoWord(wParam
));
735 return WndPaint(wParam
);
737 case WM_PRINTCLIENT
: {
738 HDC hdc
= reinterpret_cast<HDC
>(wParam
);
739 if (!IsCompatibleDC(hdc
)) {
740 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
747 ScrollMessage(wParam
);
751 HorizontalScrollMessage(wParam
);
756 if (paintState
== notPainting
) {
759 renderTargetValid
= false;
762 //Platform::DebugPrintf("Scintilla WM_SIZE %d %d\n", LoWord(lParam), HiWord(lParam));
768 // if autocomplete list active then send mousewheel message to it
770 HWND hWnd
= reinterpret_cast<HWND
>(ac
.lb
->GetID());
771 ::SendMessage(hWnd
, iMessage
, wParam
, lParam
);
775 // Don't handle datazoom.
776 // (A good idea for datazoom would be to "fold" or "unfold" details.
777 // i.e. if datazoomed out only class structures are visible, when datazooming in the control
778 // structures appear, then eventually the individual statements...)
779 if (wParam
& MK_SHIFT
) {
780 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
783 // Either SCROLL or ZOOM. We handle the wheel steppings calculation
784 wheelDelta
-= static_cast<short>(HiWord(wParam
));
785 if (abs(wheelDelta
) >= WHEEL_DELTA
&& linesPerScroll
> 0) {
786 int linesToScroll
= linesPerScroll
;
787 if (linesPerScroll
== WHEEL_PAGESCROLL
)
788 linesToScroll
= LinesOnScreen() - 1;
789 if (linesToScroll
== 0) {
792 linesToScroll
*= (wheelDelta
/ WHEEL_DELTA
);
794 wheelDelta
= wheelDelta
% WHEEL_DELTA
;
796 wheelDelta
= - (-wheelDelta
% WHEEL_DELTA
);
798 if (wParam
& MK_CONTROL
) {
799 // Zoom! We play with the font sizes in the styles.
800 // Number of steps/line is ignored, we just care if sizing up or down
801 if (linesToScroll
< 0) {
802 KeyCommand(SCI_ZOOMIN
);
804 KeyCommand(SCI_ZOOMOUT
);
808 ScrollTo(topLine
+ linesToScroll
);
814 if (wParam
== standardTimerID
&& timer
.ticking
) {
816 } else if (wParam
== idleTimerID
&& idler
.state
) {
817 SendMessage(MainHWND(), SC_WIN_IDLE
, 0, 1);
824 // wParam=dwTickCountInitial, or 0 to initialize. lParam=bSkipUserInputTest
826 if (lParam
|| (WAIT_TIMEOUT
== MsgWaitForMultipleObjects(0, 0, 0, 0, QS_INPUT
|QS_HOTKEY
))) {
828 // User input was given priority above, but all events do get a turn. Other
829 // messages, notifications, etc. will get interleaved with the idle messages.
831 // However, some things like WM_PAINT are a lower priority, and will not fire
832 // when there's a message posted. So, several times a second, we stop and let
833 // the low priority events have a turn (after which the timer will fire again).
835 DWORD dwCurrent
= GetTickCount();
836 DWORD dwStart
= wParam
? wParam
: dwCurrent
;
837 const DWORD maxWorkTime
= 50;
839 if (dwCurrent
>= dwStart
&& dwCurrent
> maxWorkTime
&& dwCurrent
- maxWorkTime
< dwStart
)
840 PostMessage(MainHWND(), SC_WIN_IDLE
, dwStart
, 0);
848 case WM_GETMINMAXINFO
:
849 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
851 case WM_LBUTTONDOWN
: {
853 // Digital Mars compiler does not include Imm library
854 // For IME, set the composition string as the result string.
855 HIMC hIMC
= ::ImmGetContext(MainHWND());
856 ::ImmNotifyIME(hIMC
, NI_COMPOSITIONSTR
, CPS_COMPLETE
, 0);
857 ::ImmReleaseContext(MainHWND(), hIMC
);
860 //Platform::DebugPrintf("Buttdown %d %x %x %x %x %x\n",iMessage, wParam, lParam,
861 // Platform::IsKeyDown(VK_SHIFT),
862 // Platform::IsKeyDown(VK_CONTROL),
863 // Platform::IsKeyDown(VK_MENU));
864 ::SetFocus(MainHWND());
865 ButtonDown(Point::FromLong(lParam
), ::GetMessageTime(),
866 (wParam
& MK_SHIFT
) != 0,
867 (wParam
& MK_CONTROL
) != 0,
868 Platform::IsKeyDown(VK_MENU
));
873 SetTrackMouseLeaveEvent(true);
874 ButtonMove(Point::FromLong(lParam
));
878 SetTrackMouseLeaveEvent(false);
880 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
883 ButtonUp(Point::FromLong(lParam
),
885 (wParam
& MK_CONTROL
) != 0);
889 ::SetFocus(MainHWND());
890 if (!PointInSelection(Point::FromLong(lParam
))) {
892 SetEmptySelection(PositionFromLocation(Point::FromLong(lParam
)));
897 if (LoWord(lParam
) == HTCLIENT
) {
898 if (inDragDrop
== ddDragging
) {
899 DisplayCursor(Window::cursorUp
);
901 // Display regular (drag) cursor over selection
903 if (0 != ::GetCursorPos(&pt
)) {
904 ::ScreenToClient(MainHWND(), &pt
);
905 if (PointInSelMargin(Point(pt
.x
, pt
.y
))) {
906 DisplayCursor(GetMarginCursor(Point(pt
.x
, pt
.y
)));
907 } else if (PointInSelection(Point(pt
.x
, pt
.y
)) && !SelectionEmpty()) {
908 DisplayCursor(Window::cursorArrow
);
909 } else if (PointIsHotspot(Point(pt
.x
, pt
.y
))) {
910 DisplayCursor(Window::cursorHand
);
912 DisplayCursor(Window::cursorText
);
918 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
922 if (((wParam
>= 128) || !iscntrl(wParam
)) || !lastKeyDownConsumed
) {
923 if (::IsWindowUnicode(MainHWND()) || keysAlwaysUnicode
) {
924 wchar_t wcs
[2] = {static_cast<wchar_t>(wParam
), 0};
925 if (IsUnicodeMode()) {
926 // For a wide character version of the window:
928 unsigned int len
= UTF8Length(wcs
, 1);
929 UTF8FromUTF16(wcs
, 1, utfval
, len
);
930 AddCharUTF(utfval
, len
);
932 UINT cpDest
= CodePageOfDocument();
934 int size
= ::WideCharToMultiByte(cpDest
,
935 0, wcs
, 1, inBufferCP
, sizeof(inBufferCP
) - 1, 0, 0);
936 inBufferCP
[size
] = '\0';
937 AddCharUTF(inBufferCP
, size
);
940 if (IsUnicodeMode()) {
941 AddCharBytes('\0', LOBYTE(wParam
));
943 AddChar(LOBYTE(wParam
));
950 if (wParam
== UNICODE_NOCHAR
) {
951 return IsUnicodeMode() ? 1 : 0;
952 } else if (lastKeyDownConsumed
) {
955 if (IsUnicodeMode()) {
957 wchar_t wcs
[2] = {static_cast<wchar_t>(wParam
), 0};
958 unsigned int len
= UTF8Length(wcs
, 1);
959 UTF8FromUTF16(wcs
, 1, utfval
, len
);
960 AddCharUTF(utfval
, len
);
969 //Platform::DebugPrintf("S keydown %d %x %x %x %x\n",iMessage, wParam, lParam, ::IsKeyDown(VK_SHIFT), ::IsKeyDown(VK_CONTROL));
970 lastKeyDownConsumed
= false;
971 int ret
= KeyDown(KeyTranslate(wParam
),
972 Platform::IsKeyDown(VK_SHIFT
),
973 Platform::IsKeyDown(VK_CONTROL
),
974 Platform::IsKeyDown(VK_MENU
),
975 &lastKeyDownConsumed
);
976 if (!ret
&& !lastKeyDownConsumed
) {
977 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
983 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
986 //Platform::DebugPrintf("S keyup %d %x %x\n",iMessage, wParam, lParam);
987 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
989 case WM_SETTINGCHANGE
:
990 //Platform::DebugPrintf("Setting Changed\n");
991 InvalidateStyleData();
992 // Get Intellimouse scroll line parameters
993 GetIntelliMouseParameters();
997 return DLGC_HASSETSEL
| DLGC_WANTALLKEYS
;
1000 HWND wOther
= reinterpret_cast<HWND
>(wParam
);
1001 HWND wThis
= MainHWND();
1002 HWND wCT
= reinterpret_cast<HWND
>(ct
.wCallTip
.GetID());
1004 !(::IsChild(wThis
, wOther
) || (wOther
== wCT
))) {
1005 SetFocusState(false);
1006 DestroySystemCaret();
1012 SetFocusState(true);
1013 DestroySystemCaret();
1014 CreateSystemCaret();
1017 case WM_SYSCOLORCHANGE
:
1018 //Platform::DebugPrintf("Setting Changed\n");
1019 InvalidateStyleData();
1022 case WM_IME_STARTCOMPOSITION
: // dbcs
1023 ImeStartComposition();
1024 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
1026 case WM_IME_ENDCOMPOSITION
: // dbcs
1027 ImeEndComposition();
1028 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
1030 case WM_IME_COMPOSITION
:
1031 return HandleComposition(wParam
, lParam
);
1034 AddCharBytes(HIBYTE(wParam
), LOBYTE(wParam
));
1038 case WM_CONTEXTMENU
:
1039 if (displayPopupMenu
) {
1040 Point pt
= Point::FromLong(lParam
);
1041 if ((pt
.x
== -1) && (pt
.y
== -1)) {
1042 // Caused by keyboard so display menu near caret
1043 pt
= PointMainCaret();
1044 POINT spt
= {static_cast<int>(pt
.x
), static_cast<int>(pt
.y
)};
1045 ::ClientToScreen(MainHWND(), &spt
);
1046 pt
= Point(spt
.x
, spt
.y
);
1051 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
1053 case WM_INPUTLANGCHANGE
:
1054 //::SetThreadLocale(LOWORD(lParam));
1055 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
1057 case WM_INPUTLANGCHANGEREQUEST
:
1058 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
1061 return 1; // Avoid any background erasure as whole window painted.
1063 case WM_CAPTURECHANGED
:
1064 capturedMouse
= false;
1067 // These are not handled in Scintilla and its faster to dispatch them here.
1068 // Also moves time out to here so profile doesn't count lots of empty message calls.
1071 case WM_MOUSEACTIVATE
:
1075 case WM_NCMOUSEMOVE
:
1076 case WM_NCLBUTTONDOWN
:
1077 case WM_IME_SETCONTEXT
:
1080 case WM_WINDOWPOSCHANGING
:
1081 case WM_WINDOWPOSCHANGED
:
1082 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
1084 case EM_LINEFROMCHAR
:
1085 if (static_cast<int>(wParam
) < 0) {
1086 wParam
= SelectionStart().Position();
1088 return pdoc
->LineFromPosition(wParam
);
1090 case EM_EXLINEFROMCHAR
:
1091 return pdoc
->LineFromPosition(lParam
);
1095 *reinterpret_cast<int *>(wParam
) = SelectionStart().Position();
1098 *reinterpret_cast<int *>(lParam
) = SelectionEnd().Position();
1100 return MAKELONG(SelectionStart().Position(), SelectionEnd().Position());
1106 Sci_CharacterRange
*pCR
= reinterpret_cast<Sci_CharacterRange
*>(lParam
);
1107 pCR
->cpMin
= SelectionStart().Position();
1108 pCR
->cpMax
= SelectionEnd().Position();
1113 int nStart
= static_cast<int>(wParam
);
1114 int nEnd
= static_cast<int>(lParam
);
1115 if (nStart
== 0 && nEnd
== -1) {
1116 nEnd
= pdoc
->Length();
1119 nStart
= nEnd
; // Remove selection
1121 if (nStart
> nEnd
) {
1122 SetSelection(nEnd
, nStart
);
1124 SetSelection(nStart
, nEnd
);
1126 EnsureCaretVisible();
1134 Sci_CharacterRange
*pCR
= reinterpret_cast<Sci_CharacterRange
*>(lParam
);
1135 sel
.selType
= Selection::selStream
;
1136 if (pCR
->cpMin
== 0 && pCR
->cpMax
== -1) {
1137 SetSelection(pCR
->cpMin
, pdoc
->Length());
1139 SetSelection(pCR
->cpMin
, pCR
->cpMax
);
1141 EnsureCaretVisible();
1142 return pdoc
->LineFromPosition(SelectionStart().Position());
1145 case SCI_GETDIRECTFUNCTION
:
1146 return reinterpret_cast<sptr_t
>(DirectFunction
);
1148 case SCI_GETDIRECTPOINTER
:
1149 return reinterpret_cast<sptr_t
>(this);
1152 ::SetFocus(MainHWND());
1155 case SCI_SETKEYSUNICODE
:
1156 keysAlwaysUnicode
= wParam
!= 0;
1159 case SCI_GETKEYSUNICODE
:
1160 return keysAlwaysUnicode
;
1162 case SCI_SETTECHNOLOGY
:
1163 if ((wParam
== SC_TECHNOLOGY_DEFAULT
) || (wParam
== SC_TECHNOLOGY_DIRECTWRITE
)) {
1164 if (technology
!= static_cast<int>(wParam
)) {
1165 if (static_cast<int>(wParam
) == SC_TECHNOLOGY_DIRECTWRITE
) {
1166 #if defined(USE_D2D)
1168 // Failed to load Direct2D or DirectWrite so no effect
1174 technology
= wParam
;
1175 // Invalidate all cached information including layout.
1177 InvalidateStyleRedraw();
1183 case SCI_LOADLEXERLIBRARY
:
1184 LexerManager::GetInstance()->Load(reinterpret_cast<const char *>(lParam
));
1189 return ScintillaBase::WndProc(iMessage
, wParam
, lParam
);
1191 } catch (std::bad_alloc
&) {
1192 errorStatus
= SC_STATUS_BADALLOC
;
1194 errorStatus
= SC_STATUS_FAILURE
;
1199 bool ScintillaWin::ValidCodePage(int codePage
) const {
1200 return codePage
== 0 || codePage
== SC_CP_UTF8
||
1201 codePage
== 932 || codePage
== 936 || codePage
== 949 ||
1202 codePage
== 950 || codePage
== 1361;
1205 sptr_t
ScintillaWin::DefWndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) {
1206 return ::DefWindowProc(MainHWND(), iMessage
, wParam
, lParam
);
1209 void ScintillaWin::SetTicking(bool on
) {
1210 if (timer
.ticking
!= on
) {
1212 if (timer
.ticking
) {
1213 timer
.tickerID
= ::SetTimer(MainHWND(), standardTimerID
, timer
.tickSize
, NULL
)
1214 ? reinterpret_cast<TickerID
>(standardTimerID
) : 0;
1216 ::KillTimer(MainHWND(), reinterpret_cast<uptr_t
>(timer
.tickerID
));
1220 timer
.ticksToWait
= caret
.period
;
1223 bool ScintillaWin::SetIdle(bool on
) {
1224 // On Win32 the Idler is implemented as a Timer on the Scintilla window. This
1225 // takes advantage of the fact that WM_TIMER messages are very low priority,
1226 // and are only posted when the message queue is empty, i.e. during idle time.
1227 if (idler
.state
!= on
) {
1229 idler
.idlerID
= ::SetTimer(MainHWND(), idleTimerID
, 10, NULL
)
1230 ? reinterpret_cast<IdlerID
>(idleTimerID
) : 0;
1232 ::KillTimer(MainHWND(), reinterpret_cast<uptr_t
>(idler
.idlerID
));
1235 idler
.state
= idler
.idlerID
!= 0;
1240 void ScintillaWin::SetMouseCapture(bool on
) {
1241 if (mouseDownCaptures
) {
1243 ::SetCapture(MainHWND());
1251 bool ScintillaWin::HaveMouseCapture() {
1252 // Cannot just see if GetCapture is this window as the scroll bar also sets capture for the window
1253 return capturedMouse
;
1254 //return capturedMouse && (::GetCapture() == MainHWND());
1257 void ScintillaWin::SetTrackMouseLeaveEvent(bool on
) {
1258 if (on
&& TrackMouseEventFn
&& !trackedMouseLeave
) {
1259 TRACKMOUSEEVENT tme
;
1260 tme
.cbSize
= sizeof(tme
);
1261 tme
.dwFlags
= TME_LEAVE
;
1262 tme
.hwndTrack
= MainHWND();
1263 TrackMouseEventFn(&tme
);
1265 trackedMouseLeave
= on
;
1268 bool ScintillaWin::PaintContains(PRectangle rc
) {
1269 bool contains
= true;
1270 if ((paintState
== painting
) && (!rc
.Empty())) {
1271 if (!rcPaint
.Contains(rc
)) {
1274 // In bounding rectangle so check more accurately using region
1275 HRGN hRgnRange
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
1277 HRGN hRgnDest
= ::CreateRectRgn(0, 0, 0, 0);
1279 int combination
= ::CombineRgn(hRgnDest
, hRgnRange
, hRgnUpdate
, RGN_DIFF
);
1280 if (combination
!= NULLREGION
) {
1283 ::DeleteRgn(hRgnDest
);
1285 ::DeleteRgn(hRgnRange
);
1292 void ScintillaWin::ScrollText(int /* linesToMove */) {
1293 //Platform::DebugPrintf("ScintillaWin::ScrollText %d\n", linesToMove);
1294 //::ScrollWindow(MainHWND(), 0,
1295 // vs.lineHeight * linesToMove, 0, 0);
1296 //::UpdateWindow(MainHWND());
1300 void ScintillaWin::UpdateSystemCaret() {
1302 if (HasCaretSizeChanged()) {
1303 DestroySystemCaret();
1304 CreateSystemCaret();
1306 Point pos
= PointMainCaret();
1307 ::SetCaretPos(pos
.x
, pos
.y
);
1311 int ScintillaWin::SetScrollInfo(int nBar
, LPCSCROLLINFO lpsi
, BOOL bRedraw
) {
1312 return ::SetScrollInfo(MainHWND(), nBar
, lpsi
, bRedraw
);
1315 bool ScintillaWin::GetScrollInfo(int nBar
, LPSCROLLINFO lpsi
) {
1316 return ::GetScrollInfo(MainHWND(), nBar
, lpsi
) ? true : false;
1319 // Change the scroll position but avoid repaint if changing to same value
1320 void ScintillaWin::ChangeScrollPos(int barType
, int pos
) {
1322 sizeof(sci
), 0, 0, 0, 0, 0, 0
1324 sci
.fMask
= SIF_POS
;
1325 GetScrollInfo(barType
, &sci
);
1326 if (sci
.nPos
!= pos
) {
1329 SetScrollInfo(barType
, &sci
, TRUE
);
1333 void ScintillaWin::SetVerticalScrollPos() {
1334 ChangeScrollPos(SB_VERT
, topLine
);
1337 void ScintillaWin::SetHorizontalScrollPos() {
1338 ChangeScrollPos(SB_HORZ
, xOffset
);
1341 bool ScintillaWin::ModifyScrollBars(int nMax
, int nPage
) {
1342 bool modified
= false;
1344 sizeof(sci
), 0, 0, 0, 0, 0, 0
1346 sci
.fMask
= SIF_PAGE
| SIF_RANGE
;
1347 GetScrollInfo(SB_VERT
, &sci
);
1348 int vertEndPreferred
= nMax
;
1349 if (!verticalScrollBarVisible
)
1350 nPage
= vertEndPreferred
+ 1;
1351 if ((sci
.nMin
!= 0) ||
1352 (sci
.nMax
!= vertEndPreferred
) ||
1353 (sci
.nPage
!= static_cast<unsigned int>(nPage
)) ||
1355 sci
.fMask
= SIF_PAGE
| SIF_RANGE
;
1357 sci
.nMax
= vertEndPreferred
;
1361 SetScrollInfo(SB_VERT
, &sci
, TRUE
);
1365 PRectangle rcText
= GetTextRectangle();
1366 int horizEndPreferred
= scrollWidth
;
1367 if (horizEndPreferred
< 0)
1368 horizEndPreferred
= 0;
1369 unsigned int pageWidth
= rcText
.Width();
1370 if (!horizontalScrollBarVisible
|| (wrapState
!= eWrapNone
))
1371 pageWidth
= horizEndPreferred
+ 1;
1372 sci
.fMask
= SIF_PAGE
| SIF_RANGE
;
1373 GetScrollInfo(SB_HORZ
, &sci
);
1374 if ((sci
.nMin
!= 0) ||
1375 (sci
.nMax
!= horizEndPreferred
) ||
1376 (sci
.nPage
!= pageWidth
) ||
1378 sci
.fMask
= SIF_PAGE
| SIF_RANGE
;
1380 sci
.nMax
= horizEndPreferred
;
1381 sci
.nPage
= pageWidth
;
1384 SetScrollInfo(SB_HORZ
, &sci
, TRUE
);
1386 if (scrollWidth
< static_cast<int>(pageWidth
)) {
1387 HorizontalScrollTo(0);
1393 void ScintillaWin::NotifyChange() {
1394 ::SendMessage(::GetParent(MainHWND()), WM_COMMAND
,
1395 MAKELONG(GetCtrlID(), SCEN_CHANGE
),
1396 reinterpret_cast<LPARAM
>(MainHWND()));
1399 void ScintillaWin::NotifyFocus(bool focus
) {
1400 ::SendMessage(::GetParent(MainHWND()), WM_COMMAND
,
1401 MAKELONG(GetCtrlID(), focus
? SCEN_SETFOCUS
: SCEN_KILLFOCUS
),
1402 reinterpret_cast<LPARAM
>(MainHWND()));
1405 void ScintillaWin::SetCtrlID(int identifier
) {
1406 ::SetWindowID(reinterpret_cast<HWND
>(wMain
.GetID()), identifier
);
1409 int ScintillaWin::GetCtrlID() {
1410 return ::GetDlgCtrlID(reinterpret_cast<HWND
>(wMain
.GetID()));
1413 void ScintillaWin::NotifyParent(SCNotification scn
) {
1414 scn
.nmhdr
.hwndFrom
= MainHWND();
1415 scn
.nmhdr
.idFrom
= GetCtrlID();
1416 ::SendMessage(::GetParent(MainHWND()), WM_NOTIFY
,
1417 GetCtrlID(), reinterpret_cast<LPARAM
>(&scn
));
1420 void ScintillaWin::NotifyDoubleClick(Point pt
, bool shift
, bool ctrl
, bool alt
) {
1421 //Platform::DebugPrintf("ScintillaWin Double click 0\n");
1422 ScintillaBase::NotifyDoubleClick(pt
, shift
, ctrl
, alt
);
1423 // Send myself a WM_LBUTTONDBLCLK, so the container can handle it too.
1424 ::SendMessage(MainHWND(),
1426 shift
? MK_SHIFT
: 0,
1427 MAKELPARAM(pt
.x
, pt
.y
));
1430 class CaseFolderUTF8
: public CaseFolderTable
{
1431 // Allocate the expandable storage here so that it does not need to be reallocated
1432 // for each call to Fold.
1433 std::vector
<wchar_t> utf16Mixed
;
1434 std::vector
<wchar_t> utf16Folded
;
1439 virtual size_t Fold(char *folded
, size_t sizeFolded
, const char *mixed
, size_t lenMixed
) {
1440 if ((lenMixed
== 1) && (sizeFolded
> 0)) {
1441 folded
[0] = mapping
[static_cast<unsigned char>(mixed
[0])];
1444 if (lenMixed
> utf16Mixed
.size()) {
1445 utf16Mixed
.resize(lenMixed
+ 8);
1447 size_t nUtf16Mixed
= ::MultiByteToWideChar(65001, 0, mixed
,
1448 static_cast<int>(lenMixed
),
1450 static_cast<int>(utf16Mixed
.size()));
1452 if (nUtf16Mixed
== 0) {
1453 // Failed to convert -> bad UTF-8
1458 if (nUtf16Mixed
* 4 > utf16Folded
.size()) { // Maximum folding expansion factor of 4
1459 utf16Folded
.resize(nUtf16Mixed
* 4 + 8);
1461 int lenFlat
= ::LCMapStringW(LOCALE_SYSTEM_DEFAULT
,
1462 LCMAP_LINGUISTIC_CASING
| LCMAP_LOWERCASE
,
1464 static_cast<int>(nUtf16Mixed
),
1466 static_cast<int>(utf16Folded
.size()));
1468 size_t lenOut
= UTF8Length(&utf16Folded
[0], lenFlat
);
1469 if (lenOut
< sizeFolded
) {
1470 UTF8FromUTF16(&utf16Folded
[0], lenFlat
, folded
, static_cast<int>(lenOut
));
1479 class CaseFolderDBCS
: public CaseFolderTable
{
1480 // Allocate the expandable storage here so that it does not need to be reallocated
1481 // for each call to Fold.
1482 std::vector
<wchar_t> utf16Mixed
;
1483 std::vector
<wchar_t> utf16Folded
;
1486 CaseFolderDBCS(UINT cp_
) : cp(cp_
) {
1489 virtual size_t Fold(char *folded
, size_t sizeFolded
, const char *mixed
, size_t lenMixed
) {
1490 if ((lenMixed
== 1) && (sizeFolded
> 0)) {
1491 folded
[0] = mapping
[static_cast<unsigned char>(mixed
[0])];
1494 if (lenMixed
> utf16Mixed
.size()) {
1495 utf16Mixed
.resize(lenMixed
+ 8);
1497 size_t nUtf16Mixed
= ::MultiByteToWideChar(cp
, 0, mixed
,
1498 static_cast<int>(lenMixed
),
1500 static_cast<int>(utf16Mixed
.size()));
1502 if (nUtf16Mixed
== 0) {
1503 // Failed to convert -> bad input
1508 if (nUtf16Mixed
* 4 > utf16Folded
.size()) { // Maximum folding expansion factor of 4
1509 utf16Folded
.resize(nUtf16Mixed
* 4 + 8);
1511 int lenFlat
= ::LCMapStringW(LOCALE_SYSTEM_DEFAULT
,
1512 LCMAP_LINGUISTIC_CASING
| LCMAP_LOWERCASE
,
1514 static_cast<int>(nUtf16Mixed
),
1516 static_cast<int>(utf16Folded
.size()));
1518 size_t lenOut
= ::WideCharToMultiByte(cp
, 0,
1519 &utf16Folded
[0], lenFlat
,
1522 if (lenOut
< sizeFolded
) {
1523 ::WideCharToMultiByte(cp
, 0,
1524 &utf16Folded
[0], lenFlat
,
1525 folded
, static_cast<int>(lenOut
), NULL
, 0);
1534 CaseFolder
*ScintillaWin::CaseFolderForEncoding() {
1535 UINT cpDest
= CodePageOfDocument();
1536 if (cpDest
== SC_CP_UTF8
) {
1537 return new CaseFolderUTF8();
1539 if (pdoc
->dbcsCodePage
== 0) {
1540 CaseFolderTable
*pcf
= new CaseFolderTable();
1541 pcf
->StandardASCII();
1542 // Only for single byte encodings
1543 UINT cpDoc
= CodePageOfDocument();
1544 for (int i
=0x80; i
<0x100; i
++) {
1545 char sCharacter
[2] = "A";
1546 sCharacter
[0] = static_cast<char>(i
);
1547 wchar_t wCharacter
[20];
1548 unsigned int lengthUTF16
= ::MultiByteToWideChar(cpDoc
, 0, sCharacter
, 1,
1549 wCharacter
, sizeof(wCharacter
)/sizeof(wCharacter
[0]));
1550 if (lengthUTF16
== 1) {
1552 int charsConverted
= ::LCMapStringW(LOCALE_SYSTEM_DEFAULT
,
1553 LCMAP_LINGUISTIC_CASING
| LCMAP_LOWERCASE
,
1554 wCharacter
, lengthUTF16
, wLower
, sizeof(wLower
)/sizeof(wLower
[0]));
1555 char sCharacterLowered
[20];
1556 unsigned int lengthConverted
= ::WideCharToMultiByte(cpDoc
, 0,
1557 wLower
, charsConverted
,
1558 sCharacterLowered
, sizeof(sCharacterLowered
), NULL
, 0);
1559 if ((lengthConverted
== 1) && (sCharacter
[0] != sCharacterLowered
[0])) {
1560 pcf
->SetTranslation(sCharacter
[0], sCharacterLowered
[0]);
1566 return new CaseFolderDBCS(cpDest
);
1571 std::string
ScintillaWin::CaseMapString(const std::string
&s
, int caseMapping
) {
1573 return std::string();
1575 if (caseMapping
== cmSame
)
1578 UINT cpDoc
= CodePageOfDocument();
1580 unsigned int lengthUTF16
= ::MultiByteToWideChar(cpDoc
, 0, s
.c_str(),
1581 static_cast<int>(s
.size()), NULL
, 0);
1582 if (lengthUTF16
== 0) // Failed to convert
1585 DWORD mapFlags
= LCMAP_LINGUISTIC_CASING
|
1586 ((caseMapping
== cmUpper
) ? LCMAP_UPPERCASE
: LCMAP_LOWERCASE
);
1588 // Many conversions performed by search function are short so optimize this case.
1589 enum { shortSize
=20 };
1591 if (s
.size() > shortSize
) {
1592 // Use dynamic allocations for long strings
1594 // Change text to UTF-16
1595 std::vector
<wchar_t> vwcText(lengthUTF16
);
1596 ::MultiByteToWideChar(cpDoc
, 0, s
.c_str(), static_cast<int>(s
.size()), &vwcText
[0], lengthUTF16
);
1599 int charsConverted
= ::LCMapStringW(LOCALE_SYSTEM_DEFAULT
, mapFlags
,
1600 &vwcText
[0], lengthUTF16
, NULL
, 0);
1601 std::vector
<wchar_t> vwcConverted(charsConverted
);
1602 ::LCMapStringW(LOCALE_SYSTEM_DEFAULT
, mapFlags
,
1603 &vwcText
[0], lengthUTF16
, &vwcConverted
[0], charsConverted
);
1605 // Change back to document encoding
1606 unsigned int lengthConverted
= ::WideCharToMultiByte(cpDoc
, 0,
1607 &vwcConverted
[0], static_cast<int>(vwcConverted
.size()),
1609 std::vector
<char> vcConverted(lengthConverted
);
1610 ::WideCharToMultiByte(cpDoc
, 0,
1611 &vwcConverted
[0], static_cast<int>(vwcConverted
.size()),
1612 &vcConverted
[0], static_cast<int>(vcConverted
.size()), NULL
, 0);
1614 return std::string(&vcConverted
[0], vcConverted
.size());
1617 // Use static allocations for short strings as much faster
1618 // A factor of 15 for single character strings
1620 // Change text to UTF-16
1621 wchar_t vwcText
[shortSize
];
1622 ::MultiByteToWideChar(cpDoc
, 0, s
.c_str(), static_cast<int>(s
.size()),
1623 vwcText
, lengthUTF16
);
1626 int charsConverted
= ::LCMapStringW(LOCALE_SYSTEM_DEFAULT
, mapFlags
,
1627 vwcText
, lengthUTF16
, NULL
, 0);
1628 // Full mapping may produce up to 3 characters per input character
1629 wchar_t vwcConverted
[shortSize
*3];
1630 ::LCMapStringW(LOCALE_SYSTEM_DEFAULT
, mapFlags
, vwcText
, lengthUTF16
,
1631 vwcConverted
, charsConverted
);
1633 // Change back to document encoding
1634 unsigned int lengthConverted
= ::WideCharToMultiByte(cpDoc
, 0,
1635 vwcConverted
, charsConverted
,
1637 // Each UTF-16 code unit may need up to 3 bytes in UTF-8
1638 char vcConverted
[shortSize
* 3 * 3];
1639 ::WideCharToMultiByte(cpDoc
, 0,
1640 vwcConverted
, charsConverted
,
1641 vcConverted
, lengthConverted
, NULL
, 0);
1643 return std::string(vcConverted
, lengthConverted
);
1647 void ScintillaWin::Copy() {
1648 //Platform::DebugPrintf("Copy\n");
1650 SelectionText selectedText
;
1651 CopySelectionRange(&selectedText
);
1652 CopyToClipboard(selectedText
);
1656 void ScintillaWin::CopyAllowLine() {
1657 SelectionText selectedText
;
1658 CopySelectionRange(&selectedText
, true);
1659 CopyToClipboard(selectedText
);
1662 bool ScintillaWin::CanPaste() {
1663 if (!Editor::CanPaste())
1665 if (::IsClipboardFormatAvailable(CF_TEXT
))
1667 if (IsUnicodeMode())
1668 return ::IsClipboardFormatAvailable(CF_UNICODETEXT
) != 0;
1672 class GlobalMemory
{
1676 GlobalMemory() : hand(0), ptr(0) {
1678 GlobalMemory(HGLOBAL hand_
) : hand(hand_
), ptr(0) {
1680 ptr
= ::GlobalLock(hand
);
1684 PLATFORM_ASSERT(!ptr
);
1686 void Allocate(size_t bytes
) {
1687 hand
= ::GlobalAlloc(GMEM_MOVEABLE
| GMEM_ZEROINIT
, bytes
);
1689 ptr
= ::GlobalLock(hand
);
1693 PLATFORM_ASSERT(ptr
);
1694 HGLOBAL handCopy
= hand
;
1695 ::GlobalUnlock(hand
);
1700 void SetClip(UINT uFormat
) {
1701 ::SetClipboardData(uFormat
, Unlock());
1703 operator bool() const {
1707 return ::GlobalSize(hand
);
1711 void ScintillaWin::InsertPasteText(const char *text
, int len
, SelectionPosition selStart
, bool isRectangular
, bool isLine
) {
1712 if (isRectangular
) {
1713 PasteRectangular(selStart
, text
, len
);
1715 char *convertedText
= 0;
1716 if (convertPastes
) {
1717 // Convert line endings of the paste into our local line-endings mode
1718 convertedText
= Document::TransformLineEnds(&len
, text
, len
, pdoc
->eolMode
);
1719 text
= convertedText
;
1722 int insertPos
= pdoc
->LineStart(pdoc
->LineFromPosition(sel
.MainCaret()));
1723 pdoc
->InsertString(insertPos
, text
, len
);
1724 // add the newline if necessary
1725 if ((len
> 0) && (text
[len
-1] != '\n' && text
[len
-1] != '\r')) {
1726 const char *endline
= StringFromEOLMode(pdoc
->eolMode
);
1727 pdoc
->InsertString(insertPos
+ len
, endline
, static_cast<int>(strlen(endline
)));
1728 len
+= static_cast<int>(strlen(endline
));
1730 if (sel
.MainCaret() == insertPos
) {
1731 SetEmptySelection(sel
.MainCaret() + len
);
1734 InsertPaste(selStart
, text
, len
);
1736 delete []convertedText
;
1740 void ScintillaWin::Paste() {
1741 if (!::OpenClipboard(MainHWND()))
1744 bool isLine
= SelectionEmpty() && (::IsClipboardFormatAvailable(cfLineSelect
) != 0);
1745 ClearSelection(multiPasteMode
== SC_MULTIPASTE_EACH
);
1746 SelectionPosition selStart
= sel
.IsRectangular() ?
1747 sel
.Rectangular().Start() :
1748 sel
.Range(sel
.Main()).Start();
1749 bool isRectangular
= ::IsClipboardFormatAvailable(cfColumnSelect
) != 0;
1751 // Always use CF_UNICODETEXT if available
1752 GlobalMemory
memUSelection(::GetClipboardData(CF_UNICODETEXT
));
1753 if (memUSelection
) {
1754 wchar_t *uptr
= static_cast<wchar_t *>(memUSelection
.ptr
);
1758 // Default Scintilla behaviour in Unicode mode
1759 if (IsUnicodeMode()) {
1760 unsigned int bytes
= memUSelection
.Size();
1761 len
= UTF8Length(uptr
, bytes
/ 2);
1762 putf
= new char[len
+ 1];
1763 UTF8FromUTF16(uptr
, bytes
/ 2, putf
, len
);
1765 // CF_UNICODETEXT available, but not in Unicode mode
1766 // Convert from Unicode to current Scintilla code page
1767 UINT cpDest
= CodePageOfDocument();
1768 len
= ::WideCharToMultiByte(cpDest
, 0, uptr
, -1,
1769 NULL
, 0, NULL
, NULL
) - 1; // subtract 0 terminator
1770 putf
= new char[len
+ 1];
1771 ::WideCharToMultiByte(cpDest
, 0, uptr
, -1,
1772 putf
, len
+ 1, NULL
, NULL
);
1775 InsertPasteText(putf
, len
, selStart
, isRectangular
, isLine
);
1778 memUSelection
.Unlock();
1780 // CF_UNICODETEXT not available, paste ANSI text
1781 GlobalMemory
memSelection(::GetClipboardData(CF_TEXT
));
1783 char *ptr
= static_cast<char *>(memSelection
.ptr
);
1785 unsigned int bytes
= memSelection
.Size();
1786 unsigned int len
= bytes
;
1787 for (unsigned int i
= 0; i
< bytes
; i
++) {
1788 if ((len
== bytes
) && (0 == ptr
[i
]))
1792 // In Unicode mode, convert clipboard text to UTF-8
1793 if (IsUnicodeMode()) {
1794 wchar_t *uptr
= new wchar_t[len
+1];
1796 unsigned int ulen
= ::MultiByteToWideChar(CP_ACP
, 0,
1797 ptr
, len
, uptr
, len
+1);
1799 unsigned int mlen
= UTF8Length(uptr
, ulen
);
1800 char *putf
= new char[mlen
+ 1];
1802 // CP_UTF8 not available on Windows 95, so use UTF8FromUTF16()
1803 UTF8FromUTF16(uptr
, ulen
, putf
, mlen
);
1809 InsertPasteText(putf
, mlen
, selStart
, isRectangular
, isLine
);
1813 InsertPasteText(ptr
, len
, selStart
, isRectangular
, isLine
);
1816 memSelection
.Unlock();
1823 void ScintillaWin::CreateCallTipWindow(PRectangle
) {
1824 if (!ct
.wCallTip
.Created()) {
1825 ct
.wCallTip
= ::CreateWindow(callClassName
, TEXT("ACallTip"),
1826 WS_POPUP
, 100, 100, 150, 20,
1828 GetWindowInstance(MainHWND()),
1830 ct
.wDraw
= ct
.wCallTip
;
1834 void ScintillaWin::AddToPopUp(const char *label
, int cmd
, bool enabled
) {
1835 HMENU hmenuPopup
= reinterpret_cast<HMENU
>(popup
.GetID());
1837 ::AppendMenuA(hmenuPopup
, MF_SEPARATOR
, 0, "");
1839 ::AppendMenuA(hmenuPopup
, MF_STRING
, cmd
, label
);
1841 ::AppendMenuA(hmenuPopup
, MF_STRING
| MF_DISABLED
| MF_GRAYED
, cmd
, label
);
1844 void ScintillaWin::ClaimSelection() {
1845 // Windows does not have a primary selection
1848 /// Implement IUnknown
1850 STDMETHODIMP_(ULONG
)FormatEnumerator_AddRef(FormatEnumerator
*fe
);
1851 STDMETHODIMP
FormatEnumerator_QueryInterface(FormatEnumerator
*fe
, REFIID riid
, PVOID
*ppv
) {
1852 //Platform::DebugPrintf("EFE QI");
1854 if (riid
== IID_IUnknown
)
1855 *ppv
= reinterpret_cast<IEnumFORMATETC
*>(fe
);
1856 if (riid
== IID_IEnumFORMATETC
)
1857 *ppv
= reinterpret_cast<IEnumFORMATETC
*>(fe
);
1859 return E_NOINTERFACE
;
1860 FormatEnumerator_AddRef(fe
);
1863 STDMETHODIMP_(ULONG
)FormatEnumerator_AddRef(FormatEnumerator
*fe
) {
1866 STDMETHODIMP_(ULONG
)FormatEnumerator_Release(FormatEnumerator
*fe
) {
1873 /// Implement IEnumFORMATETC
1874 STDMETHODIMP
FormatEnumerator_Next(FormatEnumerator
*fe
, ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
) {
1875 //Platform::DebugPrintf("EFE Next %d %d", fe->pos, celt);
1876 if (rgelt
== NULL
) return E_POINTER
;
1877 // We only support one format, so this is simple.
1878 unsigned int putPos
= 0;
1879 while ((fe
->pos
< fe
->formatsLen
) && (putPos
< celt
)) {
1880 rgelt
->cfFormat
= fe
->formats
[fe
->pos
];
1882 rgelt
->dwAspect
= DVASPECT_CONTENT
;
1884 rgelt
->tymed
= TYMED_HGLOBAL
;
1889 *pceltFetched
= putPos
;
1890 return putPos
? S_OK
: S_FALSE
;
1892 STDMETHODIMP
FormatEnumerator_Skip(FormatEnumerator
*fe
, ULONG celt
) {
1896 STDMETHODIMP
FormatEnumerator_Reset(FormatEnumerator
*fe
) {
1900 STDMETHODIMP
FormatEnumerator_Clone(FormatEnumerator
*fe
, IEnumFORMATETC
**ppenum
) {
1901 FormatEnumerator
*pfe
;
1903 pfe
= new FormatEnumerator(fe
->pos
, fe
->formats
, fe
->formatsLen
);
1905 return E_OUTOFMEMORY
;
1907 return FormatEnumerator_QueryInterface(pfe
, IID_IEnumFORMATETC
,
1908 reinterpret_cast<void **>(ppenum
));
1911 static VFunction
*vtFormatEnumerator
[] = {
1912 (VFunction
*)(FormatEnumerator_QueryInterface
),
1913 (VFunction
*)(FormatEnumerator_AddRef
),
1914 (VFunction
*)(FormatEnumerator_Release
),
1915 (VFunction
*)(FormatEnumerator_Next
),
1916 (VFunction
*)(FormatEnumerator_Skip
),
1917 (VFunction
*)(FormatEnumerator_Reset
),
1918 (VFunction
*)(FormatEnumerator_Clone
)
1921 FormatEnumerator::FormatEnumerator(int pos_
, CLIPFORMAT formats_
[], int formatsLen_
) {
1922 vtbl
= vtFormatEnumerator
;
1923 ref
= 0; // First QI adds first reference...
1925 formatsLen
= formatsLen_
;
1926 for (int i
=0; i
<formatsLen
; i
++)
1927 formats
[i
] = formats_
[i
];
1930 /// Implement IUnknown
1931 STDMETHODIMP
DropSource_QueryInterface(DropSource
*ds
, REFIID riid
, PVOID
*ppv
) {
1932 return ds
->sci
->QueryInterface(riid
, ppv
);
1934 STDMETHODIMP_(ULONG
)DropSource_AddRef(DropSource
*ds
) {
1935 return ds
->sci
->AddRef();
1937 STDMETHODIMP_(ULONG
)DropSource_Release(DropSource
*ds
) {
1938 return ds
->sci
->Release();
1941 /// Implement IDropSource
1942 STDMETHODIMP
DropSource_QueryContinueDrag(DropSource
*, BOOL fEsc
, DWORD grfKeyState
) {
1944 return DRAGDROP_S_CANCEL
;
1945 if (!(grfKeyState
& MK_LBUTTON
))
1946 return DRAGDROP_S_DROP
;
1950 STDMETHODIMP
DropSource_GiveFeedback(DropSource
*, DWORD
) {
1951 return DRAGDROP_S_USEDEFAULTCURSORS
;
1954 static VFunction
*vtDropSource
[] = {
1955 (VFunction
*)(DropSource_QueryInterface
),
1956 (VFunction
*)(DropSource_AddRef
),
1957 (VFunction
*)(DropSource_Release
),
1958 (VFunction
*)(DropSource_QueryContinueDrag
),
1959 (VFunction
*)(DropSource_GiveFeedback
)
1962 DropSource::DropSource() {
1963 vtbl
= vtDropSource
;
1967 /// Implement IUnkown
1968 STDMETHODIMP
DataObject_QueryInterface(DataObject
*pd
, REFIID riid
, PVOID
*ppv
) {
1969 //Platform::DebugPrintf("DO QI %x\n", pd);
1970 return pd
->sci
->QueryInterface(riid
, ppv
);
1972 STDMETHODIMP_(ULONG
)DataObject_AddRef(DataObject
*pd
) {
1973 return pd
->sci
->AddRef();
1975 STDMETHODIMP_(ULONG
)DataObject_Release(DataObject
*pd
) {
1976 return pd
->sci
->Release();
1978 /// Implement IDataObject
1979 STDMETHODIMP
DataObject_GetData(DataObject
*pd
, FORMATETC
*pFEIn
, STGMEDIUM
*pSTM
) {
1980 return pd
->sci
->GetData(pFEIn
, pSTM
);
1983 STDMETHODIMP
DataObject_GetDataHere(DataObject
*, FORMATETC
*, STGMEDIUM
*) {
1984 //Platform::DebugPrintf("DOB GetDataHere\n");
1988 STDMETHODIMP
DataObject_QueryGetData(DataObject
*pd
, FORMATETC
*pFE
) {
1989 if (pd
->sci
->DragIsRectangularOK(pFE
->cfFormat
) &&
1991 (pFE
->dwAspect
& DVASPECT_CONTENT
) != 0 &&
1992 pFE
->lindex
== -1 &&
1993 (pFE
->tymed
& TYMED_HGLOBAL
) != 0
1998 bool formatOK
= (pFE
->cfFormat
== CF_TEXT
) ||
1999 ((pFE
->cfFormat
== CF_UNICODETEXT
) && pd
->sci
->IsUnicodeMode());
2002 (pFE
->dwAspect
& DVASPECT_CONTENT
) == 0 ||
2003 pFE
->lindex
!= -1 ||
2004 (pFE
->tymed
& TYMED_HGLOBAL
) == 0
2006 //Platform::DebugPrintf("DOB QueryGetData No %x\n",pFE->cfFormat);
2007 //return DATA_E_FORMATETC;
2010 //Platform::DebugPrintf("DOB QueryGetData OK %x\n",pFE->cfFormat);
2014 STDMETHODIMP
DataObject_GetCanonicalFormatEtc(DataObject
*pd
, FORMATETC
*, FORMATETC
*pFEOut
) {
2015 //Platform::DebugPrintf("DOB GetCanon\n");
2016 if (pd
->sci
->IsUnicodeMode())
2017 pFEOut
->cfFormat
= CF_UNICODETEXT
;
2019 pFEOut
->cfFormat
= CF_TEXT
;
2021 pFEOut
->dwAspect
= DVASPECT_CONTENT
;
2022 pFEOut
->lindex
= -1;
2023 pFEOut
->tymed
= TYMED_HGLOBAL
;
2027 STDMETHODIMP
DataObject_SetData(DataObject
*, FORMATETC
*, STGMEDIUM
*, BOOL
) {
2028 //Platform::DebugPrintf("DOB SetData\n");
2032 STDMETHODIMP
DataObject_EnumFormatEtc(DataObject
*pd
, DWORD dwDirection
, IEnumFORMATETC
**ppEnum
) {
2034 //Platform::DebugPrintf("DOB EnumFormatEtc %d\n", dwDirection);
2035 if (dwDirection
!= DATADIR_GET
) {
2039 FormatEnumerator
*pfe
;
2040 if (pd
->sci
->IsUnicodeMode()) {
2041 CLIPFORMAT formats
[] = {CF_UNICODETEXT
, CF_TEXT
};
2042 pfe
= new FormatEnumerator(0, formats
, 2);
2044 CLIPFORMAT formats
[] = {CF_TEXT
};
2045 pfe
= new FormatEnumerator(0, formats
, 1);
2047 return FormatEnumerator_QueryInterface(pfe
, IID_IEnumFORMATETC
,
2048 reinterpret_cast<void **>(ppEnum
));
2049 } catch (std::bad_alloc
&) {
2050 pd
->sci
->errorStatus
= SC_STATUS_BADALLOC
;
2051 return E_OUTOFMEMORY
;
2053 pd
->sci
->errorStatus
= SC_STATUS_FAILURE
;
2058 STDMETHODIMP
DataObject_DAdvise(DataObject
*, FORMATETC
*, DWORD
, IAdviseSink
*, PDWORD
) {
2059 //Platform::DebugPrintf("DOB DAdvise\n");
2063 STDMETHODIMP
DataObject_DUnadvise(DataObject
*, DWORD
) {
2064 //Platform::DebugPrintf("DOB DUnadvise\n");
2068 STDMETHODIMP
DataObject_EnumDAdvise(DataObject
*, IEnumSTATDATA
**) {
2069 //Platform::DebugPrintf("DOB EnumDAdvise\n");
2073 static VFunction
*vtDataObject
[] = {
2074 (VFunction
*)(DataObject_QueryInterface
),
2075 (VFunction
*)(DataObject_AddRef
),
2076 (VFunction
*)(DataObject_Release
),
2077 (VFunction
*)(DataObject_GetData
),
2078 (VFunction
*)(DataObject_GetDataHere
),
2079 (VFunction
*)(DataObject_QueryGetData
),
2080 (VFunction
*)(DataObject_GetCanonicalFormatEtc
),
2081 (VFunction
*)(DataObject_SetData
),
2082 (VFunction
*)(DataObject_EnumFormatEtc
),
2083 (VFunction
*)(DataObject_DAdvise
),
2084 (VFunction
*)(DataObject_DUnadvise
),
2085 (VFunction
*)(DataObject_EnumDAdvise
)
2088 DataObject::DataObject() {
2089 vtbl
= vtDataObject
;
2093 /// Implement IUnknown
2094 STDMETHODIMP
DropTarget_QueryInterface(DropTarget
*dt
, REFIID riid
, PVOID
*ppv
) {
2095 //Platform::DebugPrintf("DT QI %x\n", dt);
2096 return dt
->sci
->QueryInterface(riid
, ppv
);
2098 STDMETHODIMP_(ULONG
)DropTarget_AddRef(DropTarget
*dt
) {
2099 return dt
->sci
->AddRef();
2101 STDMETHODIMP_(ULONG
)DropTarget_Release(DropTarget
*dt
) {
2102 return dt
->sci
->Release();
2105 /// Implement IDropTarget by forwarding to Scintilla
2106 STDMETHODIMP
DropTarget_DragEnter(DropTarget
*dt
, LPDATAOBJECT pIDataSource
, DWORD grfKeyState
,
2107 POINTL pt
, PDWORD pdwEffect
) {
2109 return dt
->sci
->DragEnter(pIDataSource
, grfKeyState
, pt
, pdwEffect
);
2111 dt
->sci
->errorStatus
= SC_STATUS_FAILURE
;
2115 STDMETHODIMP
DropTarget_DragOver(DropTarget
*dt
, DWORD grfKeyState
, POINTL pt
, PDWORD pdwEffect
) {
2117 return dt
->sci
->DragOver(grfKeyState
, pt
, pdwEffect
);
2119 dt
->sci
->errorStatus
= SC_STATUS_FAILURE
;
2123 STDMETHODIMP
DropTarget_DragLeave(DropTarget
*dt
) {
2125 return dt
->sci
->DragLeave();
2127 dt
->sci
->errorStatus
= SC_STATUS_FAILURE
;
2131 STDMETHODIMP
DropTarget_Drop(DropTarget
*dt
, LPDATAOBJECT pIDataSource
, DWORD grfKeyState
,
2132 POINTL pt
, PDWORD pdwEffect
) {
2134 return dt
->sci
->Drop(pIDataSource
, grfKeyState
, pt
, pdwEffect
);
2136 dt
->sci
->errorStatus
= SC_STATUS_FAILURE
;
2141 static VFunction
*vtDropTarget
[] = {
2142 (VFunction
*)(DropTarget_QueryInterface
),
2143 (VFunction
*)(DropTarget_AddRef
),
2144 (VFunction
*)(DropTarget_Release
),
2145 (VFunction
*)(DropTarget_DragEnter
),
2146 (VFunction
*)(DropTarget_DragOver
),
2147 (VFunction
*)(DropTarget_DragLeave
),
2148 (VFunction
*)(DropTarget_Drop
)
2151 DropTarget::DropTarget() {
2152 vtbl
= vtDropTarget
;
2157 * DBCS: support Input Method Editor (IME).
2158 * Called when IME Window opened.
2160 void ScintillaWin::ImeStartComposition() {
2162 // Digital Mars compiler does not include Imm library
2164 // Move IME Window to current caret position
2165 HIMC hIMC
= ::ImmGetContext(MainHWND());
2166 Point pos
= PointMainCaret();
2167 COMPOSITIONFORM CompForm
;
2168 CompForm
.dwStyle
= CFS_POINT
;
2169 CompForm
.ptCurrentPos
.x
= pos
.x
;
2170 CompForm
.ptCurrentPos
.y
= pos
.y
;
2172 ::ImmSetCompositionWindow(hIMC
, &CompForm
);
2174 // Set font of IME window to same as surrounded text.
2176 // Since the style creation code has been made platform independent,
2177 // The logfont for the IME is recreated here.
2178 int styleHere
= (pdoc
->StyleAt(sel
.MainCaret())) & 31;
2179 LOGFONTA lf
= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ""};
2180 int sizeZoomed
= vs
.styles
[styleHere
].size
+ vs
.zoomLevel
* SC_FONT_SIZE_MULTIPLIER
;
2181 if (sizeZoomed
<= 2 * SC_FONT_SIZE_MULTIPLIER
) // Hangs if sizeZoomed <= 1
2182 sizeZoomed
= 2 * SC_FONT_SIZE_MULTIPLIER
;
2183 AutoSurface
surface(this);
2184 int deviceHeight
= sizeZoomed
;
2186 deviceHeight
= (sizeZoomed
* surface
->LogPixelsY()) / 72;
2188 // The negative is to allow for leading
2189 lf
.lfHeight
= -(abs(deviceHeight
/ SC_FONT_SIZE_MULTIPLIER
));
2190 lf
.lfWeight
= vs
.styles
[styleHere
].weight
;
2191 lf
.lfItalic
= static_cast<BYTE
>(vs
.styles
[styleHere
].italic
? 1 : 0);
2192 lf
.lfCharSet
= DEFAULT_CHARSET
;
2193 lf
.lfFaceName
[0] = '\0';
2194 if (vs
.styles
[styleHere
].fontName
)
2195 strcpy(lf
.lfFaceName
, vs
.styles
[styleHere
].fontName
);
2197 ::ImmSetCompositionFontA(hIMC
, &lf
);
2199 ::ImmReleaseContext(MainHWND(), hIMC
);
2200 // Caret is displayed in IME window. So, caret in Scintilla is useless.
2206 /** Called when IME Window closed. */
2207 void ScintillaWin::ImeEndComposition() {
2208 ShowCaretAtCurrentPosition();
2211 void ScintillaWin::AddCharBytes(char b0
, char b1
) {
2213 int inputCodePage
= InputCodePage();
2214 if (inputCodePage
&& IsUnicodeMode()) {
2215 char utfval
[4] = "\0\0\0";
2218 if (b0
) { // Two bytes from IME
2221 ansiChars
[2] = '\0';
2222 ::MultiByteToWideChar(inputCodePage
, 0, ansiChars
, 2, wcs
, 1);
2225 ansiChars
[1] = '\0';
2226 ::MultiByteToWideChar(inputCodePage
, 0, ansiChars
, 1, wcs
, 1);
2228 unsigned int len
= UTF8Length(wcs
, 1);
2229 UTF8FromUTF16(wcs
, 1, utfval
, len
);
2231 AddCharUTF(utfval
, len
? len
: 1);
2236 dbcsChars
[2] = '\0';
2237 AddCharUTF(dbcsChars
, 2, true);
2243 void ScintillaWin::GetIntelliMouseParameters() {
2244 // This retrieves the number of lines per scroll as configured inthe Mouse Properties sheet in Control Panel
2245 ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES
, 0, &linesPerScroll
, 0);
2248 void ScintillaWin::CopyToClipboard(const SelectionText
&selectedText
) {
2249 if (!::OpenClipboard(MainHWND()))
2253 GlobalMemory uniText
;
2255 // Default Scintilla behaviour in Unicode mode
2256 if (IsUnicodeMode()) {
2257 int uchars
= UTF16Length(selectedText
.s
, selectedText
.len
);
2258 uniText
.Allocate(2 * uchars
);
2260 UTF16FromUTF8(selectedText
.s
, selectedText
.len
, static_cast<wchar_t *>(uniText
.ptr
), uchars
);
2264 // Convert to Unicode using the current Scintilla code page
2265 UINT cpSrc
= CodePageFromCharSet(
2266 selectedText
.characterSet
, selectedText
.codePage
);
2267 int uLen
= ::MultiByteToWideChar(cpSrc
, 0, selectedText
.s
, selectedText
.len
, 0, 0);
2268 uniText
.Allocate(2 * uLen
);
2270 ::MultiByteToWideChar(cpSrc
, 0, selectedText
.s
, selectedText
.len
,
2271 static_cast<wchar_t *>(uniText
.ptr
), uLen
);
2277 // Copy ANSI text to clipboard on Windows 9x
2278 // Convert from Unicode text, so other ANSI programs can
2280 // Windows NT, 2k, XP automatically generates CF_TEXT
2281 GlobalMemory ansiText
;
2282 ansiText
.Allocate(selectedText
.len
);
2284 ::WideCharToMultiByte(CP_ACP
, 0, static_cast<wchar_t *>(uniText
.ptr
), -1,
2285 static_cast<char *>(ansiText
.ptr
), selectedText
.len
, NULL
, NULL
);
2286 ansiText
.SetClip(CF_TEXT
);
2289 uniText
.SetClip(CF_UNICODETEXT
);
2291 // There was a failure - try to copy at least ANSI text
2292 GlobalMemory ansiText
;
2293 ansiText
.Allocate(selectedText
.len
);
2295 memcpy(static_cast<char *>(ansiText
.ptr
), selectedText
.s
, selectedText
.len
);
2296 ansiText
.SetClip(CF_TEXT
);
2300 if (selectedText
.rectangular
) {
2301 ::SetClipboardData(cfColumnSelect
, 0);
2304 if (selectedText
.lineCopy
) {
2305 ::SetClipboardData(cfLineSelect
, 0);
2311 void ScintillaWin::ScrollMessage(WPARAM wParam
) {
2312 //DWORD dwStart = timeGetTime();
2313 //Platform::DebugPrintf("Scroll %x %d\n", wParam, lParam);
2316 memset(&sci
, 0, sizeof(sci
));
2317 sci
.cbSize
= sizeof(sci
);
2318 sci
.fMask
= SIF_ALL
;
2320 GetScrollInfo(SB_VERT
, &sci
);
2322 //Platform::DebugPrintf("ScrollInfo %d mask=%x min=%d max=%d page=%d pos=%d track=%d\n", b,sci.fMask,
2323 //sci.nMin, sci.nMax, sci.nPage, sci.nPos, sci.nTrackPos);
2325 int topLineNew
= topLine
;
2326 switch (LoWord(wParam
)) {
2334 topLineNew
-= LinesToScroll(); break;
2335 case SB_PAGEDOWN
: topLineNew
+= LinesToScroll(); break;
2336 case SB_TOP
: topLineNew
= 0; break;
2337 case SB_BOTTOM
: topLineNew
= MaxScrollPos(); break;
2338 case SB_THUMBPOSITION
: topLineNew
= sci
.nTrackPos
; break;
2339 case SB_THUMBTRACK
: topLineNew
= sci
.nTrackPos
; break;
2341 ScrollTo(topLineNew
);
2344 void ScintillaWin::HorizontalScrollMessage(WPARAM wParam
) {
2346 PRectangle rcText
= GetTextRectangle();
2347 int pageWidth
= rcText
.Width() * 2 / 3;
2348 switch (LoWord(wParam
)) {
2352 case SB_LINEDOWN
: // May move past the logical end
2360 if (xPos
> scrollWidth
- rcText
.Width()) { // Hit the end exactly
2361 xPos
= scrollWidth
- rcText
.Width();
2370 case SB_THUMBPOSITION
:
2371 case SB_THUMBTRACK
: {
2372 // Do NOT use wParam, its 16 bit and not enough for very long lines. Its still possible to overflow the 32 bit but you have to try harder =]
2374 si
.cbSize
= sizeof(si
);
2375 si
.fMask
= SIF_TRACKPOS
;
2376 if (GetScrollInfo(SB_HORZ
, &si
)) {
2377 xPos
= si
.nTrackPos
;
2382 HorizontalScrollTo(xPos
);
2386 * Redraw all of text area.
2387 * This paint will not be abandoned.
2389 void ScintillaWin::FullPaint() {
2390 if (technology
== SC_TECHNOLOGY_DEFAULT
) {
2391 HDC hdc
= ::GetDC(MainHWND());
2393 ::ReleaseDC(MainHWND(), hdc
);
2400 * Redraw all of text area on the specified DC.
2401 * This paint will not be abandoned.
2403 void ScintillaWin::FullPaintDC(HDC hdc
) {
2404 paintState
= painting
;
2405 rcPaint
= GetClientRectangle();
2406 paintingAllText
= true;
2407 if (technology
== SC_TECHNOLOGY_DEFAULT
) {
2408 AutoSurface
surfaceWindow(hdc
, this);
2409 if (surfaceWindow
) {
2410 Paint(surfaceWindow
, rcPaint
);
2411 surfaceWindow
->Release();
2414 #if defined(USE_D2D)
2415 EnsureRenderTarget();
2416 AutoSurface
surfaceWindow(pRenderTarget
, this);
2417 if (surfaceWindow
) {
2418 pRenderTarget
->BeginDraw();
2419 Paint(surfaceWindow
, rcPaint
);
2420 surfaceWindow
->Release();
2421 HRESULT hr
= pRenderTarget
->EndDraw();
2422 if (hr
== D2DERR_RECREATE_TARGET
) {
2428 paintState
= notPainting
;
2431 static bool CompareDevCap(HDC hdc
, HDC hOtherDC
, int nIndex
) {
2432 return ::GetDeviceCaps(hdc
, nIndex
) == ::GetDeviceCaps(hOtherDC
, nIndex
);
2435 bool ScintillaWin::IsCompatibleDC(HDC hOtherDC
) {
2436 HDC hdc
= ::GetDC(MainHWND());
2438 CompareDevCap(hdc
, hOtherDC
, TECHNOLOGY
) &&
2439 CompareDevCap(hdc
, hOtherDC
, LOGPIXELSY
) &&
2440 CompareDevCap(hdc
, hOtherDC
, LOGPIXELSX
) &&
2441 CompareDevCap(hdc
, hOtherDC
, BITSPIXEL
) &&
2442 CompareDevCap(hdc
, hOtherDC
, PLANES
);
2443 ::ReleaseDC(MainHWND(), hdc
);
2444 return isCompatible
;
2447 DWORD
ScintillaWin::EffectFromState(DWORD grfKeyState
) {
2448 // These are the Wordpad semantics.
2450 if (inDragDrop
== ddDragging
) // Internal defaults to move
2451 dwEffect
= DROPEFFECT_MOVE
;
2453 dwEffect
= DROPEFFECT_COPY
;
2454 if (grfKeyState
& MK_ALT
)
2455 dwEffect
= DROPEFFECT_MOVE
;
2456 if (grfKeyState
& MK_CONTROL
)
2457 dwEffect
= DROPEFFECT_COPY
;
2461 /// Implement IUnknown
2462 STDMETHODIMP
ScintillaWin::QueryInterface(REFIID riid
, PVOID
*ppv
) {
2464 if (riid
== IID_IUnknown
)
2465 *ppv
= reinterpret_cast<IDropTarget
*>(&dt
);
2466 if (riid
== IID_IDropSource
)
2467 *ppv
= reinterpret_cast<IDropSource
*>(&ds
);
2468 if (riid
== IID_IDropTarget
)
2469 *ppv
= reinterpret_cast<IDropTarget
*>(&dt
);
2470 if (riid
== IID_IDataObject
)
2471 *ppv
= reinterpret_cast<IDataObject
*>(&dob
);
2473 return E_NOINTERFACE
;
2477 STDMETHODIMP_(ULONG
) ScintillaWin::AddRef() {
2481 STDMETHODIMP_(ULONG
) ScintillaWin::Release() {
2485 /// Implement IDropTarget
2486 STDMETHODIMP
ScintillaWin::DragEnter(LPDATAOBJECT pIDataSource
, DWORD grfKeyState
,
2487 POINTL
, PDWORD pdwEffect
) {
2488 if (pIDataSource
== NULL
)
2490 FORMATETC fmtu
= {CF_UNICODETEXT
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
2491 HRESULT hrHasUText
= pIDataSource
->QueryGetData(&fmtu
);
2492 hasOKText
= (hrHasUText
== S_OK
);
2494 FORMATETC fmte
= {CF_TEXT
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
2495 HRESULT hrHasText
= pIDataSource
->QueryGetData(&fmte
);
2496 hasOKText
= (hrHasText
== S_OK
);
2499 *pdwEffect
= DROPEFFECT_NONE
;
2503 *pdwEffect
= EffectFromState(grfKeyState
);
2507 STDMETHODIMP
ScintillaWin::DragOver(DWORD grfKeyState
, POINTL pt
, PDWORD pdwEffect
) {
2509 if (!hasOKText
|| pdoc
->IsReadOnly()) {
2510 *pdwEffect
= DROPEFFECT_NONE
;
2514 *pdwEffect
= EffectFromState(grfKeyState
);
2516 // Update the cursor.
2517 POINT rpt
= {pt
.x
, pt
.y
};
2518 ::ScreenToClient(MainHWND(), &rpt
);
2519 SetDragPosition(SPositionFromLocation(Point(rpt
.x
, rpt
.y
), false, false, UserVirtualSpace()));
2523 errorStatus
= SC_STATUS_FAILURE
;
2528 STDMETHODIMP
ScintillaWin::DragLeave() {
2530 SetDragPosition(SelectionPosition(invalidPosition
));
2533 errorStatus
= SC_STATUS_FAILURE
;
2538 STDMETHODIMP
ScintillaWin::Drop(LPDATAOBJECT pIDataSource
, DWORD grfKeyState
,
2539 POINTL pt
, PDWORD pdwEffect
) {
2541 *pdwEffect
= EffectFromState(grfKeyState
);
2543 if (pIDataSource
== NULL
)
2546 SetDragPosition(SelectionPosition(invalidPosition
));
2548 STGMEDIUM medium
= {0, {0}, 0};
2551 bool dataAllocated
= false;
2553 FORMATETC fmtu
= {CF_UNICODETEXT
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
2554 HRESULT hr
= pIDataSource
->GetData(&fmtu
, &medium
);
2555 if (SUCCEEDED(hr
) && medium
.hGlobal
) {
2556 wchar_t *udata
= static_cast<wchar_t *>(::GlobalLock(medium
.hGlobal
));
2558 if (IsUnicodeMode()) {
2559 int tlen
= ::GlobalSize(medium
.hGlobal
);
2560 // Convert UTF-16 to UTF-8
2561 int dataLen
= UTF8Length(udata
, tlen
/2);
2562 data
= new char[dataLen
+1];
2563 UTF8FromUTF16(udata
, tlen
/2, data
, dataLen
);
2564 dataAllocated
= true;
2566 // Convert UTF-16 to ANSI
2568 // Default Scintilla behavior in Unicode mode
2569 // CF_UNICODETEXT available, but not in Unicode mode
2570 // Convert from Unicode to current Scintilla code page
2571 UINT cpDest
= CodePageOfDocument();
2572 int tlen
= ::WideCharToMultiByte(cpDest
, 0, udata
, -1,
2573 NULL
, 0, NULL
, NULL
) - 1; // subtract 0 terminator
2574 data
= new char[tlen
+ 1];
2575 memset(data
, 0, (tlen
+1));
2576 ::WideCharToMultiByte(cpDest
, 0, udata
, -1,
2577 data
, tlen
+ 1, NULL
, NULL
);
2578 dataAllocated
= true;
2584 FORMATETC fmte
= {CF_TEXT
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
2585 hr
= pIDataSource
->GetData(&fmte
, &medium
);
2586 if (SUCCEEDED(hr
) && medium
.hGlobal
) {
2587 data
= static_cast<char *>(::GlobalLock(medium
.hGlobal
));
2591 if (data
&& convertPastes
) {
2592 // Convert line endings of the drop into our local line-endings mode
2593 int len
= static_cast<int>(strlen(data
));
2594 char *convertedText
= Document::TransformLineEnds(&len
, data
, len
, pdoc
->eolMode
);
2597 data
= convertedText
;
2598 dataAllocated
= true;
2602 //Platform::DebugPrintf("Bad data format: 0x%x\n", hres);
2606 FORMATETC fmtr
= {cfColumnSelect
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
2607 HRESULT hrRectangular
= pIDataSource
->QueryGetData(&fmtr
);
2609 POINT rpt
= {pt
.x
, pt
.y
};
2610 ::ScreenToClient(MainHWND(), &rpt
);
2611 SelectionPosition movePos
= SPositionFromLocation(Point(rpt
.x
, rpt
.y
), false, false, UserVirtualSpace());
2613 DropAt(movePos
, data
, *pdwEffect
== DROPEFFECT_MOVE
, hrRectangular
== S_OK
);
2615 ::GlobalUnlock(medium
.hGlobal
);
2618 if (medium
.pUnkForRelease
!= NULL
)
2619 medium
.pUnkForRelease
->Release();
2621 ::GlobalFree(medium
.hGlobal
);
2628 errorStatus
= SC_STATUS_FAILURE
;
2633 /// Implement important part of IDataObject
2634 STDMETHODIMP
ScintillaWin::GetData(FORMATETC
*pFEIn
, STGMEDIUM
*pSTM
) {
2635 bool formatOK
= (pFEIn
->cfFormat
== CF_TEXT
) ||
2636 ((pFEIn
->cfFormat
== CF_UNICODETEXT
) && IsUnicodeMode());
2639 (pFEIn
->dwAspect
& DVASPECT_CONTENT
) == 0 ||
2640 pFEIn
->lindex
!= -1 ||
2641 (pFEIn
->tymed
& TYMED_HGLOBAL
) == 0
2643 //Platform::DebugPrintf("DOB GetData No %d %x %x fmt=%x\n", lenDrag, pFEIn, pSTM, pFEIn->cfFormat);
2644 return DATA_E_FORMATETC
;
2646 pSTM
->tymed
= TYMED_HGLOBAL
;
2647 //Platform::DebugPrintf("DOB GetData OK %d %x %x\n", lenDrag, pFEIn, pSTM);
2650 if (pFEIn
->cfFormat
== CF_UNICODETEXT
) {
2651 int uchars
= UTF16Length(drag
.s
, drag
.len
);
2652 text
.Allocate(2 * uchars
);
2654 UTF16FromUTF8(drag
.s
, drag
.len
, static_cast<wchar_t *>(text
.ptr
), uchars
);
2657 text
.Allocate(drag
.len
);
2659 memcpy(static_cast<char *>(text
.ptr
), drag
.s
, drag
.len
);
2662 pSTM
->hGlobal
= text
? text
.Unlock() : 0;
2663 pSTM
->pUnkForRelease
= 0;
2667 bool ScintillaWin::Register(HINSTANCE hInstance_
) {
2669 hInstance
= hInstance_
;
2672 // Register the Scintilla class
2675 // Register Scintilla as a wide character window
2676 WNDCLASSEXW wndclass
;
2677 wndclass
.cbSize
= sizeof(wndclass
);
2678 wndclass
.style
= CS_GLOBALCLASS
| CS_HREDRAW
| CS_VREDRAW
;
2679 wndclass
.lpfnWndProc
= ScintillaWin::SWndProc
;
2680 wndclass
.cbClsExtra
= 0;
2681 wndclass
.cbWndExtra
= sizeof(ScintillaWin
*);
2682 wndclass
.hInstance
= hInstance
;
2683 wndclass
.hIcon
= NULL
;
2684 wndclass
.hCursor
= NULL
;
2685 wndclass
.hbrBackground
= NULL
;
2686 wndclass
.lpszMenuName
= NULL
;
2687 wndclass
.lpszClassName
= L
"Scintilla";
2688 wndclass
.hIconSm
= 0;
2689 result
= ::RegisterClassExW(&wndclass
) != 0;
2692 // Register Scintilla as a normal character window
2693 WNDCLASSEX wndclass
;
2694 wndclass
.cbSize
= sizeof(wndclass
);
2695 wndclass
.style
= CS_GLOBALCLASS
| CS_HREDRAW
| CS_VREDRAW
;
2696 wndclass
.lpfnWndProc
= ScintillaWin::SWndProc
;
2697 wndclass
.cbClsExtra
= 0;
2698 wndclass
.cbWndExtra
= sizeof(ScintillaWin
*);
2699 wndclass
.hInstance
= hInstance
;
2700 wndclass
.hIcon
= NULL
;
2701 wndclass
.hCursor
= NULL
;
2702 wndclass
.hbrBackground
= NULL
;
2703 wndclass
.lpszMenuName
= NULL
;
2704 wndclass
.lpszClassName
= scintillaClassName
;
2705 wndclass
.hIconSm
= 0;
2706 result
= ::RegisterClassEx(&wndclass
) != 0;
2710 // Register the CallTip class
2711 WNDCLASSEX wndclassc
;
2712 wndclassc
.cbSize
= sizeof(wndclassc
);
2713 wndclassc
.style
= CS_GLOBALCLASS
| CS_HREDRAW
| CS_VREDRAW
;
2714 wndclassc
.cbClsExtra
= 0;
2715 wndclassc
.cbWndExtra
= sizeof(ScintillaWin
*);
2716 wndclassc
.hInstance
= hInstance
;
2717 wndclassc
.hIcon
= NULL
;
2718 wndclassc
.hbrBackground
= NULL
;
2719 wndclassc
.lpszMenuName
= NULL
;
2720 wndclassc
.lpfnWndProc
= ScintillaWin::CTWndProc
;
2721 wndclassc
.hCursor
= ::LoadCursor(NULL
, IDC_ARROW
);
2722 wndclassc
.lpszClassName
= callClassName
;
2723 wndclassc
.hIconSm
= 0;
2725 result
= ::RegisterClassEx(&wndclassc
) != 0;
2731 bool ScintillaWin::Unregister() {
2732 bool result
= ::UnregisterClass(scintillaClassName
, hInstance
) != 0;
2733 if (::UnregisterClass(callClassName
, hInstance
) == 0)
2738 bool ScintillaWin::HasCaretSizeChanged() {
2740 ( (0 != vs
.caretWidth
) && (sysCaretWidth
!= vs
.caretWidth
) )
2741 || ((0 != vs
.lineHeight
) && (sysCaretHeight
!= vs
.lineHeight
))
2748 BOOL
ScintillaWin::CreateSystemCaret() {
2749 sysCaretWidth
= vs
.caretWidth
;
2750 if (0 == sysCaretWidth
) {
2753 sysCaretHeight
= vs
.lineHeight
;
2754 int bitmapSize
= (((sysCaretWidth
+ 15) & ~15) >> 3) *
2756 char *bits
= new char[bitmapSize
];
2757 memset(bits
, 0, bitmapSize
);
2758 sysCaretBitmap
= ::CreateBitmap(sysCaretWidth
, sysCaretHeight
, 1,
2759 1, reinterpret_cast<BYTE
*>(bits
));
2761 BOOL retval
= ::CreateCaret(
2762 MainHWND(), sysCaretBitmap
,
2763 sysCaretWidth
, sysCaretHeight
);
2764 ::ShowCaret(MainHWND());
2768 BOOL
ScintillaWin::DestroySystemCaret() {
2769 ::HideCaret(MainHWND());
2770 BOOL retval
= ::DestroyCaret();
2771 if (sysCaretBitmap
) {
2772 ::DeleteObject(sysCaretBitmap
);
2778 sptr_t PASCAL
ScintillaWin::CTWndProc(
2779 HWND hWnd
, UINT iMessage
, WPARAM wParam
, sptr_t lParam
) {
2780 // Find C++ object associated with window.
2781 ScintillaWin
*sciThis
= reinterpret_cast<ScintillaWin
*>(PointerFromWindow(hWnd
));
2783 // ctp will be zero if WM_CREATE not seen yet
2785 if (iMessage
== WM_CREATE
) {
2786 // Associate CallTip object with window
2787 CREATESTRUCT
*pCreate
= reinterpret_cast<CREATESTRUCT
*>(lParam
);
2788 SetWindowPointer(hWnd
, pCreate
->lpCreateParams
);
2791 return ::DefWindowProc(hWnd
, iMessage
, wParam
, lParam
);
2794 if (iMessage
== WM_NCDESTROY
) {
2795 ::SetWindowLong(hWnd
, 0, 0);
2796 return ::DefWindowProc(hWnd
, iMessage
, wParam
, lParam
);
2797 } else if (iMessage
== WM_PAINT
) {
2799 ::BeginPaint(hWnd
, &ps
);
2800 Surface
*surfaceWindow
= Surface::Allocate(sciThis
->technology
);
2801 if (surfaceWindow
) {
2802 #if defined(USE_D2D)
2803 ID2D1HwndRenderTarget
*pCTRenderTarget
= 0;
2806 GetClientRect(hWnd
, &rc
);
2807 // Create a Direct2D render target.
2808 if (sciThis
->technology
== SC_TECHNOLOGY_DEFAULT
) {
2809 surfaceWindow
->Init(ps
.hdc
, hWnd
);
2811 #if defined(USE_D2D)
2812 pD2DFactory
->CreateHwndRenderTarget(
2813 D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT
, D2D1::PixelFormat(), 96.0, 96.0),
2814 D2D1::HwndRenderTargetProperties(hWnd
, D2D1::SizeU(rc
.right
- rc
.left
, rc
.bottom
- rc
.top
)),
2816 surfaceWindow
->Init(pCTRenderTarget
, hWnd
);
2817 pCTRenderTarget
->BeginDraw();
2820 surfaceWindow
->SetUnicodeMode(SC_CP_UTF8
== sciThis
->ct
.codePage
);
2821 surfaceWindow
->SetDBCSMode(sciThis
->ct
.codePage
);
2822 sciThis
->ct
.PaintCT(surfaceWindow
);
2823 #if defined(USE_D2D)
2824 if (pCTRenderTarget
)
2825 pCTRenderTarget
->EndDraw();
2827 surfaceWindow
->Release();
2828 delete surfaceWindow
;
2829 #if defined(USE_D2D)
2830 if (pCTRenderTarget
)
2831 pCTRenderTarget
->Release();
2834 ::EndPaint(hWnd
, &ps
);
2836 } else if ((iMessage
== WM_NCLBUTTONDOWN
) || (iMessage
== WM_NCLBUTTONDBLCLK
)) {
2838 pt
.x
= static_cast<short>(LOWORD(lParam
));
2839 pt
.y
= static_cast<short>(HIWORD(lParam
));
2840 ScreenToClient(hWnd
, &pt
);
2841 sciThis
->ct
.MouseClick(Point(pt
.x
, pt
.y
));
2842 sciThis
->CallTipClick();
2844 } else if (iMessage
== WM_LBUTTONDOWN
) {
2845 // This does not fire due to the hit test code
2846 sciThis
->ct
.MouseClick(Point::FromLong(lParam
));
2847 sciThis
->CallTipClick();
2849 } else if (iMessage
== WM_SETCURSOR
) {
2850 ::SetCursor(::LoadCursor(NULL
, IDC_ARROW
));
2852 } else if (iMessage
== WM_NCHITTEST
) {
2855 return ::DefWindowProc(hWnd
, iMessage
, wParam
, lParam
);
2859 sciThis
->errorStatus
= SC_STATUS_FAILURE
;
2861 return ::DefWindowProc(hWnd
, iMessage
, wParam
, lParam
);
2864 sptr_t
ScintillaWin::DirectFunction(
2865 ScintillaWin
*sci
, UINT iMessage
, uptr_t wParam
, sptr_t lParam
) {
2866 PLATFORM_ASSERT(::GetCurrentThreadId() == ::GetWindowThreadProcessId(sci
->MainHWND(), NULL
));
2867 return sci
->WndProc(iMessage
, wParam
, lParam
);
2871 #ifndef STATIC_BUILD
2872 __declspec(dllexport
)
2874 sptr_t __stdcall
Scintilla_DirectFunction(
2875 ScintillaWin
*sci
, UINT iMessage
, uptr_t wParam
, sptr_t lParam
) {
2876 return sci
->WndProc(iMessage
, wParam
, lParam
);
2879 sptr_t PASCAL
ScintillaWin::SWndProc(
2880 HWND hWnd
, UINT iMessage
, WPARAM wParam
, sptr_t lParam
) {
2881 //Platform::DebugPrintf("S W:%x M:%x WP:%x L:%x\n", hWnd, iMessage, wParam, lParam);
2883 // Find C++ object associated with window.
2884 ScintillaWin
*sci
= reinterpret_cast<ScintillaWin
*>(PointerFromWindow(hWnd
));
2885 // sci will be zero if WM_CREATE not seen yet
2888 if (iMessage
== WM_CREATE
) {
2889 // Create C++ object associated with window
2890 sci
= new ScintillaWin(hWnd
);
2891 SetWindowPointer(hWnd
, sci
);
2892 return sci
->WndProc(iMessage
, wParam
, lParam
);
2896 return ::DefWindowProc(hWnd
, iMessage
, wParam
, lParam
);
2898 if (iMessage
== WM_NCDESTROY
) {
2904 ::SetWindowLong(hWnd
, 0, 0);
2905 return ::DefWindowProc(hWnd
, iMessage
, wParam
, lParam
);
2907 return sci
->WndProc(iMessage
, wParam
, lParam
);
2912 // This function is externally visible so it can be called from container when building statically.
2913 // Must be called once only.
2914 int Scintilla_RegisterClasses(void *hInstance
) {
2915 Platform_Initialise(hInstance
);
2916 bool result
= ScintillaWin::Register(reinterpret_cast<HINSTANCE
>(hInstance
));
2918 Scintilla_LinkLexers();
2923 // This function is externally visible so it can be called from container when building statically.
2924 int Scintilla_ReleaseResources() {
2925 bool result
= ScintillaWin::Unregister();
2926 Platform_Finalise();
2930 #ifndef STATIC_BUILD
2931 extern "C" int APIENTRY
DllMain(HINSTANCE hInstance
, DWORD dwReason
, LPVOID
) {
2932 //Platform::DebugPrintf("Scintilla::DllMain %d %d\n", hInstance, dwReason);
2933 if (dwReason
== DLL_PROCESS_ATTACH
) {
2934 if (!Scintilla_RegisterClasses(hInstance
))
2936 } else if (dwReason
== DLL_PROCESS_DETACH
) {
2937 Scintilla_ReleaseResources();