|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +#include "pch.h" |
| 5 | +#include "Terminal.hpp" |
| 6 | + |
| 7 | +using namespace Microsoft::Terminal::Core; |
| 8 | + |
| 9 | +// Method Description: |
| 10 | +// - Helper to determine the selected region of the buffer. Used for rendering. |
| 11 | +// Return Value: |
| 12 | +// - A vector of rectangles representing the regions to select, line by line. They are absolute coordinates relative to the buffer origin. |
| 13 | +std::vector<SMALL_RECT> Terminal::_GetSelectionRects() const |
| 14 | +{ |
| 15 | + std::vector<SMALL_RECT> selectionArea; |
| 16 | + |
| 17 | + if (!_selectionActive) |
| 18 | + { |
| 19 | + return selectionArea; |
| 20 | + } |
| 21 | + |
| 22 | + // create these new anchors for comparison and rendering |
| 23 | + COORD selectionAnchorWithOffset; |
| 24 | + COORD endSelectionPositionWithOffset; |
| 25 | + |
| 26 | + // Add anchor offset here to update properly on new buffer output |
| 27 | + THROW_IF_FAILED(ShortAdd(_selectionAnchor.Y, _selectionAnchor_YOffset, &selectionAnchorWithOffset.Y)); |
| 28 | + THROW_IF_FAILED(ShortAdd(_endSelectionPosition.Y, _endSelectionPosition_YOffset, &endSelectionPositionWithOffset.Y)); |
| 29 | + |
| 30 | + // clamp X values to be within buffer bounds |
| 31 | + const auto bufferWidth = _buffer->GetSize().RightInclusive(); |
| 32 | + selectionAnchorWithOffset.X = std::clamp(_selectionAnchor.X, static_cast<SHORT>(0), bufferWidth); |
| 33 | + endSelectionPositionWithOffset.X = std::clamp(_endSelectionPosition.X, static_cast<SHORT>(0), bufferWidth); |
| 34 | + |
| 35 | + // NOTE: (0,0) is top-left so vertical comparison is inverted |
| 36 | + const COORD& higherCoord = (selectionAnchorWithOffset.Y <= endSelectionPositionWithOffset.Y) ? |
| 37 | + selectionAnchorWithOffset : |
| 38 | + endSelectionPositionWithOffset; |
| 39 | + const COORD& lowerCoord = (selectionAnchorWithOffset.Y > endSelectionPositionWithOffset.Y) ? |
| 40 | + selectionAnchorWithOffset : |
| 41 | + endSelectionPositionWithOffset; |
| 42 | + |
| 43 | + selectionArea.reserve(lowerCoord.Y - higherCoord.Y + 1); |
| 44 | + for (auto row = higherCoord.Y; row <= lowerCoord.Y; row++) |
| 45 | + { |
| 46 | + SMALL_RECT selectionRow; |
| 47 | + |
| 48 | + selectionRow.Top = row; |
| 49 | + selectionRow.Bottom = row; |
| 50 | + |
| 51 | + if (_boxSelection || higherCoord.Y == lowerCoord.Y) |
| 52 | + { |
| 53 | + selectionRow.Left = std::min(higherCoord.X, lowerCoord.X); |
| 54 | + selectionRow.Right = std::max(higherCoord.X, lowerCoord.X); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + selectionRow.Left = (row == higherCoord.Y) ? higherCoord.X : 0; |
| 59 | + selectionRow.Right = (row == lowerCoord.Y) ? lowerCoord.X : bufferWidth; |
| 60 | + } |
| 61 | + |
| 62 | + selectionRow.Left = _ExpandWideGlyphSelectionLeft(selectionRow.Left, row); |
| 63 | + selectionRow.Right = _ExpandWideGlyphSelectionRight(selectionRow.Right, row); |
| 64 | + |
| 65 | + selectionArea.emplace_back(selectionRow); |
| 66 | + } |
| 67 | + return selectionArea; |
| 68 | +} |
| 69 | + |
| 70 | +// Method Description: |
| 71 | +// - Expands the selection left-wards to cover a wide glyph, if necessary |
| 72 | +// Arguments: |
| 73 | +// - position: the (x,y) coordinate on the visible viewport |
| 74 | +// Return Value: |
| 75 | +// - updated x position to encapsulate the wide glyph |
| 76 | +const SHORT Terminal::_ExpandWideGlyphSelectionLeft(const SHORT xPos, const SHORT yPos) const noexcept |
| 77 | +{ |
| 78 | + // don't change the value if at/outside the boundary |
| 79 | + if (xPos <= 0 || xPos >= _buffer->GetSize().RightInclusive()) |
| 80 | + { |
| 81 | + return xPos; |
| 82 | + } |
| 83 | + |
| 84 | + COORD position{ xPos, yPos }; |
| 85 | + const auto attr = _buffer->GetCellDataAt(position)->DbcsAttr(); |
| 86 | + if (attr.IsTrailing()) |
| 87 | + { |
| 88 | + // move off by highlighting the lead half too. |
| 89 | + // alters position.X |
| 90 | + _mutableViewport.DecrementInBounds(position); |
| 91 | + } |
| 92 | + return position.X; |
| 93 | +} |
| 94 | + |
| 95 | +// Method Description: |
| 96 | +// - Expands the selection right-wards to cover a wide glyph, if necessary |
| 97 | +// Arguments: |
| 98 | +// - position: the (x,y) coordinate on the visible viewport |
| 99 | +// Return Value: |
| 100 | +// - updated x position to encapsulate the wide glyph |
| 101 | +const SHORT Terminal::_ExpandWideGlyphSelectionRight(const SHORT xPos, const SHORT yPos) const noexcept |
| 102 | +{ |
| 103 | + // don't change the value if at/outside the boundary |
| 104 | + if (xPos <= 0 || xPos >= _buffer->GetSize().RightInclusive()) |
| 105 | + { |
| 106 | + return xPos; |
| 107 | + } |
| 108 | + |
| 109 | + COORD position{ xPos, yPos }; |
| 110 | + const auto attr = _buffer->GetCellDataAt(position)->DbcsAttr(); |
| 111 | + if (attr.IsLeading()) |
| 112 | + { |
| 113 | + // move off by highlighting the trailing half too. |
| 114 | + // alters position.X |
| 115 | + _mutableViewport.IncrementInBounds(position); |
| 116 | + } |
| 117 | + return position.X; |
| 118 | +} |
| 119 | + |
| 120 | +// Method Description: |
| 121 | +// - Checks if selection is active |
| 122 | +// Return Value: |
| 123 | +// - bool representing if selection is active. Used to decide copy/paste on right click |
| 124 | +const bool Terminal::IsSelectionActive() const noexcept |
| 125 | +{ |
| 126 | + return _selectionActive; |
| 127 | +} |
| 128 | + |
| 129 | +// Method Description: |
| 130 | +// - Record the position of the beginning of a selection |
| 131 | +// Arguments: |
| 132 | +// - position: the (x,y) coordinate on the visible viewport |
| 133 | +void Terminal::SetSelectionAnchor(const COORD position) |
| 134 | +{ |
| 135 | + _selectionAnchor = position; |
| 136 | + |
| 137 | + // include _scrollOffset here to ensure this maps to the right spot of the original viewport |
| 138 | + THROW_IF_FAILED(ShortSub(_selectionAnchor.Y, gsl::narrow<SHORT>(_scrollOffset), &_selectionAnchor.Y)); |
| 139 | + |
| 140 | + // copy value of ViewStartIndex to support scrolling |
| 141 | + // and update on new buffer output (used in _GetSelectionRects()) |
| 142 | + _selectionAnchor_YOffset = gsl::narrow<SHORT>(_ViewStartIndex()); |
| 143 | + |
| 144 | + _selectionActive = true; |
| 145 | + SetEndSelectionPosition(position); |
| 146 | +} |
| 147 | + |
| 148 | +// Method Description: |
| 149 | +// - Record the position of the end of a selection |
| 150 | +// Arguments: |
| 151 | +// - position: the (x,y) coordinate on the visible viewport |
| 152 | +void Terminal::SetEndSelectionPosition(const COORD position) |
| 153 | +{ |
| 154 | + _endSelectionPosition = position; |
| 155 | + |
| 156 | + // include _scrollOffset here to ensure this maps to the right spot of the original viewport |
| 157 | + THROW_IF_FAILED(ShortSub(_endSelectionPosition.Y, gsl::narrow<SHORT>(_scrollOffset), &_endSelectionPosition.Y)); |
| 158 | + |
| 159 | + // copy value of ViewStartIndex to support scrolling |
| 160 | + // and update on new buffer output (used in _GetSelectionRects()) |
| 161 | + _endSelectionPosition_YOffset = gsl::narrow<SHORT>(_ViewStartIndex()); |
| 162 | +} |
| 163 | + |
| 164 | +// Method Description: |
| 165 | +// - enable/disable box selection (ALT + selection) |
| 166 | +// Arguments: |
| 167 | +// - isEnabled: new value for _boxSelection |
| 168 | +void Terminal::SetBoxSelection(const bool isEnabled) noexcept |
| 169 | +{ |
| 170 | + _boxSelection = isEnabled; |
| 171 | +} |
| 172 | + |
| 173 | +// Method Description: |
| 174 | +// - clear selection data and disable rendering it |
| 175 | +void Terminal::ClearSelection() noexcept |
| 176 | +{ |
| 177 | + _selectionActive = false; |
| 178 | + _selectionAnchor = { 0, 0 }; |
| 179 | + _endSelectionPosition = { 0, 0 }; |
| 180 | + _selectionAnchor_YOffset = 0; |
| 181 | + _endSelectionPosition_YOffset = 0; |
| 182 | + |
| 183 | + _buffer->GetRenderTarget().TriggerSelection(); |
| 184 | +} |
| 185 | + |
| 186 | +// Method Description: |
| 187 | +// - get wstring text from highlighted portion of text buffer |
| 188 | +// Arguments: |
| 189 | +// - trimTrailingWhitespace: enable removing any whitespace from copied selection |
| 190 | +// and get text to appear on separate lines. |
| 191 | +// Return Value: |
| 192 | +// - wstring text from buffer. If extended to multiple lines, each line is separated by \r\n |
| 193 | +const std::wstring Terminal::RetrieveSelectedTextFromBuffer(bool trimTrailingWhitespace) const |
| 194 | +{ |
| 195 | + std::function<COLORREF(TextAttribute&)> GetForegroundColor = std::bind(&Terminal::GetForegroundColor, this, std::placeholders::_1); |
| 196 | + std::function<COLORREF(TextAttribute&)> GetBackgroundColor = std::bind(&Terminal::GetBackgroundColor, this, std::placeholders::_1); |
| 197 | + |
| 198 | + auto data = _buffer->GetTextForClipboard(!_boxSelection, |
| 199 | + trimTrailingWhitespace, |
| 200 | + _GetSelectionRects(), |
| 201 | + GetForegroundColor, |
| 202 | + GetBackgroundColor); |
| 203 | + |
| 204 | + std::wstring result; |
| 205 | + for (const auto& text : data.text) |
| 206 | + { |
| 207 | + result += text; |
| 208 | + } |
| 209 | + |
| 210 | + return result; |
| 211 | +} |
0 commit comments