John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | 60f507b | 2015-06-13 00:41:00 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | c411eb9 | 2017-07-25 09:39:30 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/pwl/cpwl_edit_ctrl.h" |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 8 | |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 9 | #include "core/fpdfdoc/cpvt_section.h" |
| 10 | #include "core/fpdfdoc/cpvt_word.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 11 | #include "core/fxge/fx_font.h" |
Dan Sinclair | c411eb9 | 2017-07-25 09:39:30 -0400 | [diff] [blame] | 12 | #include "fpdfsdk/pwl/cpwl_caret.h" |
| 13 | #include "fpdfsdk/pwl/cpwl_edit_impl.h" |
| 14 | #include "fpdfsdk/pwl/cpwl_font_map.h" |
| 15 | #include "fpdfsdk/pwl/cpwl_scroll_bar.h" |
| 16 | #include "fpdfsdk/pwl/cpwl_wnd.h" |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 17 | #include "public/fpdf_fwlevent.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 18 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 19 | CPWL_EditCtrl::CPWL_EditCtrl() |
Dan Sinclair | 6b0158f | 2017-07-24 09:42:55 -0400 | [diff] [blame] | 20 | : m_pEdit(new CPWL_EditImpl), |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 21 | m_pEditCaret(nullptr), |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 22 | m_bMouseDown(false), |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 23 | m_nCharSet(FX_CHARSET_Default) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 24 | |
dsinclair | dbc7716 | 2016-07-13 11:34:23 -0700 | [diff] [blame] | 25 | CPWL_EditCtrl::~CPWL_EditCtrl() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 26 | |
Tom Sepez | bf15730 | 2017-09-15 13:26:32 -0700 | [diff] [blame] | 27 | void CPWL_EditCtrl::OnCreate(CreateParams* pParamsToAdjust) { |
| 28 | pParamsToAdjust->eCursorType = FXCT_VBEAM; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 31 | void CPWL_EditCtrl::OnCreated() { |
Tom Sepez | bf15730 | 2017-09-15 13:26:32 -0700 | [diff] [blame] | 32 | SetFontSize(GetCreationParams().fFontSize); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | m_pEdit->SetFontMap(GetFontMap()); |
| 35 | m_pEdit->SetNotify(this); |
| 36 | m_pEdit->Initialize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 37 | } |
| 38 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 39 | bool CPWL_EditCtrl::IsWndHorV() { |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 40 | CFX_Matrix mt = GetWindowMatrix(); |
Dan Sinclair | 1f403ce | 2017-02-21 12:56:24 -0500 | [diff] [blame] | 41 | return mt.Transform(CFX_PointF(1, 1)).y == mt.Transform(CFX_PointF(0, 1)).y; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 44 | void CPWL_EditCtrl::SetCursor() { |
| 45 | if (IsValid()) { |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 46 | if (CFX_SystemHandler* pSH = GetSystemHandler()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | if (IsWndHorV()) |
| 48 | pSH->SetCursor(FXCT_VBEAM); |
| 49 | else |
| 50 | pSH->SetCursor(FXCT_HBEAM); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 51 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 52 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 55 | WideString CPWL_EditCtrl::GetSelectedText() { |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 56 | if (m_pEdit) |
Diana Gage | 89e6562 | 2017-07-20 18:09:31 -0700 | [diff] [blame] | 57 | return m_pEdit->GetSelectedText(); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 58 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 59 | return WideString(); |
Diana Gage | dce2d72 | 2017-06-20 11:17:11 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 62 | void CPWL_EditCtrl::ReplaceSelection(const WideString& text) { |
Diana Gage | ab39097 | 2017-07-28 17:07:39 -0700 | [diff] [blame] | 63 | if (!m_pEdit) |
| 64 | return; |
| 65 | |
| 66 | m_pEdit->ClearSelection(); |
| 67 | m_pEdit->InsertText(text, FX_CHARSET_Default); |
Diana Gage | 1c7f142 | 2017-07-24 11:19:52 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Henrique Nakashima | 55469ae | 2017-10-04 11:08:45 -0400 | [diff] [blame^] | 70 | bool CPWL_EditCtrl::RePosChildWnd() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | m_pEdit->SetPlateRect(GetClientRect()); |
Henrique Nakashima | 55469ae | 2017-10-04 11:08:45 -0400 | [diff] [blame^] | 72 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Dan Sinclair | fb00ec2 | 2017-07-05 09:28:15 -0400 | [diff] [blame] | 75 | void CPWL_EditCtrl::SetScrollInfo(const PWL_SCROLL_INFO& info) { |
| 76 | if (CPWL_Wnd* pChild = GetVScrollBar()) |
| 77 | pChild->SetScrollInfo(info); |
| 78 | } |
| 79 | |
Dan Sinclair | 7e0336e | 2017-07-05 09:39:50 -0400 | [diff] [blame] | 80 | void CPWL_EditCtrl::SetScrollPosition(float pos) { |
| 81 | if (CPWL_Wnd* pChild = GetVScrollBar()) |
| 82 | pChild->SetScrollPosition(pos); |
| 83 | } |
| 84 | |
Dan Sinclair | 63fbd8d | 2017-07-05 14:10:36 -0400 | [diff] [blame] | 85 | void CPWL_EditCtrl::ScrollWindowVertically(float pos) { |
| 86 | m_pEdit->SetScrollPos(CFX_PointF(m_pEdit->GetScrollPos().x, pos)); |
| 87 | } |
| 88 | |
Tom Sepez | bf15730 | 2017-09-15 13:26:32 -0700 | [diff] [blame] | 89 | void CPWL_EditCtrl::CreateChildWnd(const CreateParams& cp) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 90 | if (!IsReadOnly()) |
| 91 | CreateEditCaret(cp); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Tom Sepez | bf15730 | 2017-09-15 13:26:32 -0700 | [diff] [blame] | 94 | void CPWL_EditCtrl::CreateEditCaret(const CreateParams& cp) { |
thestig | 732f6a0 | 2016-05-12 10:41:56 -0700 | [diff] [blame] | 95 | if (m_pEditCaret) |
| 96 | return; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 97 | |
thestig | 732f6a0 | 2016-05-12 10:41:56 -0700 | [diff] [blame] | 98 | m_pEditCaret = new CPWL_Caret; |
| 99 | m_pEditCaret->SetInvalidRect(GetClientRect()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 100 | |
Tom Sepez | bf15730 | 2017-09-15 13:26:32 -0700 | [diff] [blame] | 101 | CreateParams ecp = cp; |
thestig | 732f6a0 | 2016-05-12 10:41:56 -0700 | [diff] [blame] | 102 | ecp.pParentWnd = this; |
| 103 | ecp.dwFlags = PWS_CHILD | PWS_NOREFRESHCLIP; |
| 104 | ecp.dwBorderWidth = 0; |
dsinclair | 92cb5e5 | 2016-05-16 11:38:28 -0700 | [diff] [blame] | 105 | ecp.nBorderStyle = BorderStyle::SOLID; |
Lei Zhang | d24236a | 2017-06-29 18:28:58 -0700 | [diff] [blame] | 106 | ecp.rcRectWnd = CFX_FloatRect(); |
thestig | 732f6a0 | 2016-05-12 10:41:56 -0700 | [diff] [blame] | 107 | |
| 108 | m_pEditCaret->Create(ecp); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 111 | void CPWL_EditCtrl::SetFontSize(float fFontSize) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 112 | m_pEdit->SetFontSize(fFontSize); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 115 | float CPWL_EditCtrl::GetFontSize() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | return m_pEdit->GetFontSize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 119 | bool CPWL_EditCtrl::OnKeyDown(uint16_t nChar, uint32_t nFlag) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | if (m_bMouseDown) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 121 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 122 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 123 | bool bRet = CPWL_Wnd::OnKeyDown(nChar, nFlag); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 124 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 125 | // FILTER |
| 126 | switch (nChar) { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 127 | default: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 128 | return false; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 129 | case FWL_VKEY_Delete: |
| 130 | case FWL_VKEY_Up: |
| 131 | case FWL_VKEY_Down: |
| 132 | case FWL_VKEY_Left: |
| 133 | case FWL_VKEY_Right: |
| 134 | case FWL_VKEY_Home: |
| 135 | case FWL_VKEY_End: |
| 136 | case FWL_VKEY_Insert: |
| 137 | case 'C': |
| 138 | case 'V': |
| 139 | case 'X': |
| 140 | case 'A': |
| 141 | case 'Z': |
| 142 | case 'c': |
| 143 | case 'v': |
| 144 | case 'x': |
| 145 | case 'a': |
| 146 | case 'z': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | break; |
| 148 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 149 | |
thestig | 594b20b | 2016-05-12 21:56:43 -0700 | [diff] [blame] | 150 | if (nChar == FWL_VKEY_Delete && m_pEdit->IsSelected()) |
| 151 | nChar = FWL_VKEY_Unknown; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 152 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | switch (nChar) { |
| 154 | case FWL_VKEY_Delete: |
| 155 | Delete(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 156 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | case FWL_VKEY_Insert: |
| 158 | if (IsSHIFTpressed(nFlag)) |
| 159 | PasteText(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 160 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 161 | case FWL_VKEY_Up: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 162 | m_pEdit->OnVK_UP(IsSHIFTpressed(nFlag), false); |
| 163 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 164 | case FWL_VKEY_Down: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 165 | m_pEdit->OnVK_DOWN(IsSHIFTpressed(nFlag), false); |
| 166 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | case FWL_VKEY_Left: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 168 | m_pEdit->OnVK_LEFT(IsSHIFTpressed(nFlag), false); |
| 169 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 170 | case FWL_VKEY_Right: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 171 | m_pEdit->OnVK_RIGHT(IsSHIFTpressed(nFlag), false); |
| 172 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 173 | case FWL_VKEY_Home: |
| 174 | m_pEdit->OnVK_HOME(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag)); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 175 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 176 | case FWL_VKEY_End: |
| 177 | m_pEdit->OnVK_END(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag)); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 178 | return true; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 179 | case FWL_VKEY_Unknown: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 180 | if (!IsSHIFTpressed(nFlag)) |
Diana Gage | 22bf7a5 | 2017-07-21 11:33:18 -0700 | [diff] [blame] | 181 | ClearSelection(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | else |
| 183 | CutText(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 184 | return true; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 185 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | break; |
| 187 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 188 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 189 | return bRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 190 | } |
| 191 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 192 | bool CPWL_EditCtrl::OnChar(uint16_t nChar, uint32_t nFlag) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | if (m_bMouseDown) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 194 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | |
| 196 | CPWL_Wnd::OnChar(nChar, nFlag); |
| 197 | |
| 198 | // FILTER |
| 199 | switch (nChar) { |
| 200 | case 0x0A: |
| 201 | case 0x1B: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 202 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | default: |
| 204 | break; |
| 205 | } |
| 206 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 207 | bool bCtrl = IsCTRLpressed(nFlag); |
| 208 | bool bAlt = IsALTpressed(nFlag); |
| 209 | bool bShift = IsSHIFTpressed(nFlag); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 211 | uint16_t word = nChar; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | |
| 213 | if (bCtrl && !bAlt) { |
| 214 | switch (nChar) { |
| 215 | case 'C' - 'A' + 1: |
| 216 | CopyText(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 217 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | case 'V' - 'A' + 1: |
| 219 | PasteText(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 220 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | case 'X' - 'A' + 1: |
| 222 | CutText(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 223 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | case 'A' - 'A' + 1: |
| 225 | SelectAll(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 226 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | case 'Z' - 'A' + 1: |
| 228 | if (bShift) |
| 229 | Redo(); |
| 230 | else |
| 231 | Undo(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 232 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 233 | default: |
| 234 | if (nChar < 32) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 235 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | if (IsReadOnly()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 240 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 241 | |
| 242 | if (m_pEdit->IsSelected() && word == FWL_VKEY_Back) |
| 243 | word = FWL_VKEY_Unknown; |
| 244 | |
Diana Gage | 22bf7a5 | 2017-07-21 11:33:18 -0700 | [diff] [blame] | 245 | ClearSelection(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | |
| 247 | switch (word) { |
| 248 | case FWL_VKEY_Back: |
| 249 | Backspace(); |
| 250 | break; |
| 251 | case FWL_VKEY_Return: |
| 252 | InsertReturn(); |
| 253 | break; |
| 254 | case FWL_VKEY_Unknown: |
| 255 | break; |
| 256 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | InsertWord(word, GetCharSet()); |
| 258 | break; |
| 259 | } |
| 260 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 261 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 264 | bool CPWL_EditCtrl::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | CPWL_Wnd::OnLButtonDown(point, nFlag); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 266 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 267 | if (ClientHitTest(point)) { |
Henrique Nakashima | 55469ae | 2017-10-04 11:08:45 -0400 | [diff] [blame^] | 268 | if (m_bMouseDown && !InvalidateRect(nullptr)) |
| 269 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 270 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 271 | m_bMouseDown = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 272 | SetCapture(); |
| 273 | |
| 274 | m_pEdit->OnMouseDown(point, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag)); |
| 275 | } |
| 276 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 277 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 280 | bool CPWL_EditCtrl::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | CPWL_Wnd::OnLButtonUp(point, nFlag); |
| 282 | |
| 283 | if (m_bMouseDown) { |
| 284 | // can receive keybord message |
| 285 | if (ClientHitTest(point) && !IsFocused()) |
| 286 | SetFocus(); |
| 287 | |
| 288 | ReleaseCapture(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 289 | m_bMouseDown = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 290 | } |
| 291 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 292 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 295 | bool CPWL_EditCtrl::OnMouseMove(const CFX_PointF& point, uint32_t nFlag) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | CPWL_Wnd::OnMouseMove(point, nFlag); |
| 297 | |
| 298 | if (m_bMouseDown) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 299 | m_pEdit->OnMouseMove(point, false, false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 300 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 301 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | } |
| 303 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 304 | void CPWL_EditCtrl::SetEditCaret(bool bVisible) { |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 305 | CFX_PointF ptHead; |
| 306 | CFX_PointF ptFoot; |
tsepez | 63f545c | 2016-09-13 16:08:49 -0700 | [diff] [blame] | 307 | if (bVisible) |
Dan Sinclair | 1f403ce | 2017-02-21 12:56:24 -0500 | [diff] [blame] | 308 | GetCaretInfo(&ptHead, &ptFoot); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 309 | |
Dan Sinclair | 0fdd1ae | 2017-07-05 16:00:48 -0400 | [diff] [blame] | 310 | SetCaret(bVisible, ptHead, ptFoot); |
Henrique Nakashima | 55469ae | 2017-10-04 11:08:45 -0400 | [diff] [blame^] | 311 | // Note, |this| may no longer be viable at this point. If more work needs to |
| 312 | // be done, check the return value of SetCaret(). |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Dan Sinclair | 1f403ce | 2017-02-21 12:56:24 -0500 | [diff] [blame] | 315 | void CPWL_EditCtrl::GetCaretInfo(CFX_PointF* ptHead, CFX_PointF* ptFoot) const { |
Dan Sinclair | 6b0158f | 2017-07-24 09:42:55 -0400 | [diff] [blame] | 316 | CPWL_EditImpl_Iterator* pIterator = m_pEdit->GetIterator(); |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 317 | pIterator->SetAt(m_pEdit->GetCaret()); |
| 318 | CPVT_Word word; |
| 319 | CPVT_Line line; |
| 320 | if (pIterator->GetWord(word)) { |
Dan Sinclair | 1f403ce | 2017-02-21 12:56:24 -0500 | [diff] [blame] | 321 | ptHead->x = word.ptWord.x + word.fWidth; |
| 322 | ptHead->y = word.ptWord.y + word.fAscent; |
| 323 | ptFoot->x = word.ptWord.x + word.fWidth; |
| 324 | ptFoot->y = word.ptWord.y + word.fDescent; |
thestig | 821d59e | 2016-05-11 12:59:22 -0700 | [diff] [blame] | 325 | } else if (pIterator->GetLine(line)) { |
Dan Sinclair | 1f403ce | 2017-02-21 12:56:24 -0500 | [diff] [blame] | 326 | ptHead->x = line.ptLine.x; |
| 327 | ptHead->y = line.ptLine.y + line.fLineAscent; |
| 328 | ptFoot->x = line.ptLine.x; |
| 329 | ptFoot->y = line.ptLine.y + line.fLineDescent; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 330 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Henrique Nakashima | 55469ae | 2017-10-04 11:08:45 -0400 | [diff] [blame^] | 333 | bool CPWL_EditCtrl::SetCaret(bool bVisible, |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 334 | const CFX_PointF& ptHead, |
| 335 | const CFX_PointF& ptFoot) { |
Henrique Nakashima | 55469ae | 2017-10-04 11:08:45 -0400 | [diff] [blame^] | 336 | if (!m_pEditCaret) |
| 337 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | |
Henrique Nakashima | 55469ae | 2017-10-04 11:08:45 -0400 | [diff] [blame^] | 339 | if (!IsFocused() || m_pEdit->IsSelected()) |
| 340 | bVisible = false; |
| 341 | |
| 342 | ObservedPtr thisObserved(this); |
| 343 | m_pEditCaret->SetCaret(bVisible, ptHead, ptFoot); |
| 344 | if (!thisObserved) |
| 345 | return false; |
| 346 | |
| 347 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 350 | WideString CPWL_EditCtrl::GetText() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 351 | return m_pEdit->GetText(); |
| 352 | } |
| 353 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 354 | void CPWL_EditCtrl::SetSelection(int32_t nStartChar, int32_t nEndChar) { |
| 355 | m_pEdit->SetSelection(nStartChar, nEndChar); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Diana Gage | 4d02e90 | 2017-07-20 17:20:31 -0700 | [diff] [blame] | 358 | void CPWL_EditCtrl::GetSelection(int32_t& nStartChar, int32_t& nEndChar) const { |
| 359 | m_pEdit->GetSelection(nStartChar, nEndChar); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Diana Gage | 22bf7a5 | 2017-07-21 11:33:18 -0700 | [diff] [blame] | 362 | void CPWL_EditCtrl::ClearSelection() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 363 | if (!IsReadOnly()) |
Diana Gage | 22bf7a5 | 2017-07-21 11:33:18 -0700 | [diff] [blame] | 364 | m_pEdit->ClearSelection(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void CPWL_EditCtrl::SelectAll() { |
| 368 | m_pEdit->SelectAll(); |
| 369 | } |
| 370 | |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 371 | void CPWL_EditCtrl::SetScrollPos(const CFX_PointF& point) { |
thestig | 732f6a0 | 2016-05-12 10:41:56 -0700 | [diff] [blame] | 372 | m_pEdit->SetScrollPos(point); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 375 | CFX_PointF CPWL_EditCtrl::GetScrollPos() const { |
thestig | 732f6a0 | 2016-05-12 10:41:56 -0700 | [diff] [blame] | 376 | return m_pEdit->GetScrollPos(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 379 | void CPWL_EditCtrl::CopyText() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 380 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 381 | void CPWL_EditCtrl::PasteText() {} |
| 382 | |
| 383 | void CPWL_EditCtrl::CutText() {} |
| 384 | |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 385 | void CPWL_EditCtrl::InsertWord(uint16_t word, int32_t nCharset) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 386 | if (!IsReadOnly()) |
| 387 | m_pEdit->InsertWord(word, nCharset); |
| 388 | } |
| 389 | |
| 390 | void CPWL_EditCtrl::InsertReturn() { |
| 391 | if (!IsReadOnly()) |
| 392 | m_pEdit->InsertReturn(); |
| 393 | } |
| 394 | |
| 395 | void CPWL_EditCtrl::Delete() { |
| 396 | if (!IsReadOnly()) |
| 397 | m_pEdit->Delete(); |
| 398 | } |
| 399 | |
| 400 | void CPWL_EditCtrl::Backspace() { |
| 401 | if (!IsReadOnly()) |
| 402 | m_pEdit->Backspace(); |
| 403 | } |
| 404 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 405 | bool CPWL_EditCtrl::CanUndo() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 406 | return !IsReadOnly() && m_pEdit->CanUndo(); |
| 407 | } |
| 408 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 409 | bool CPWL_EditCtrl::CanRedo() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | return !IsReadOnly() && m_pEdit->CanRedo(); |
| 411 | } |
| 412 | |
| 413 | void CPWL_EditCtrl::Redo() { |
| 414 | if (CanRedo()) |
| 415 | m_pEdit->Redo(); |
| 416 | } |
| 417 | |
| 418 | void CPWL_EditCtrl::Undo() { |
| 419 | if (CanUndo()) |
| 420 | m_pEdit->Undo(); |
| 421 | } |
| 422 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 423 | int32_t CPWL_EditCtrl::GetCharSet() const { |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 424 | return m_nCharSet < 0 ? FX_CHARSET_Default : m_nCharSet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | void CPWL_EditCtrl::SetReadyToInput() { |
| 428 | if (m_bMouseDown) { |
| 429 | ReleaseCapture(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 430 | m_bMouseDown = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 431 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 432 | } |