TextView: add shortcuts for wordwise delete

Change-Id: Ie67f6255c3f5d9d8ccc6699ed42dd71ae593fa16
Reviewed-on: https://review.haiku-os.org/c/1573
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
Kacper Kasper
2019-07-14 14:14:39 +00:00
committed by Stephan Aßmus
parent b281541f9a
commit 17c9e98743
2 changed files with 103 additions and 13 deletions
+14 -11
View File
@@ -287,10 +287,10 @@ private:
void _ValidateLayoutData(); void _ValidateLayoutData();
void _ResetTextRect(); void _ResetTextRect();
void _HandleBackspace(); void _HandleBackspace(int32 modifiers = -1);
void _HandleArrowKey(uint32 arrowKey, void _HandleArrowKey(uint32 arrowKey,
int32 modifiers = -1); int32 modifiers = -1);
void _HandleDelete(); void _HandleDelete(int32 modifiers = -1);
void _HandlePageKey(uint32 pageKey, void _HandlePageKey(uint32 pageKey,
int32 modifiers = -1); int32 modifiers = -1);
void _HandleAlphaKey(const char* bytes, void _HandleAlphaKey(const char* bytes,
@@ -450,17 +450,20 @@ private:
LayoutData* fLayoutData; LayoutData* fLayoutData;
int32 fLastClickOffset; int32 fLastClickOffset;
bool fInstalledNavigateCommandWordwiseShortcuts; bool fInstalledNavigateCommandWordwiseShortcuts : 1;
bool fInstalledNavigateOptionWordwiseShortcuts; bool fInstalledNavigateOptionWordwiseShortcuts : 1;
bool fInstalledNavigateOptionLinewiseShortcuts; bool fInstalledNavigateOptionLinewiseShortcuts : 1;
bool fInstalledNavigateHomeEndDocwiseShortcuts; bool fInstalledNavigateHomeEndDocwiseShortcuts : 1;
bool fInstalledSelectCommandWordwiseShortcuts; bool fInstalledSelectCommandWordwiseShortcuts : 1;
bool fInstalledSelectOptionWordwiseShortcuts; bool fInstalledSelectOptionWordwiseShortcuts : 1;
bool fInstalledSelectOptionLinewiseShortcuts; bool fInstalledSelectOptionLinewiseShortcuts : 1;
bool fInstalledSelectHomeEndDocwiseShortcuts; bool fInstalledSelectHomeEndDocwiseShortcuts : 1;
uint32 _reserved[5]; bool fInstalledRemoveCommandWordwiseShortcuts : 1;
bool fInstalledRemoveOptionWordwiseShortcuts : 1;
uint32 _reserved[6];
}; };
#endif // _TEXTVIEW_H #endif // _TEXTVIEW_H
+89 -2
View File
@@ -196,6 +196,7 @@ static const float kVerticalScrollBarStep = 12.0;
static const int32 kMsgNavigateArrow = '_NvA'; static const int32 kMsgNavigateArrow = '_NvA';
static const int32 kMsgNavigatePage = '_NvP'; static const int32 kMsgNavigatePage = '_NvP';
static const int32 kMsgRemoveWord = '_RmW';
static property_info sPropertyList[] = { static property_info sPropertyList[] = {
@@ -954,6 +955,17 @@ BTextView::MessageReceived(BMessage* message)
break; break;
} }
case kMsgRemoveWord:
{
int32 key = message->GetInt32("key", 0);
int32 modifiers = message->GetInt32("modifiers", 0);
if (key == B_DELETE)
_HandleDelete(modifiers);
else if (key == B_BACKSPACE)
_HandleBackspace(modifiers);
break;
}
default: default:
BView::MessageReceived(message); BView::MessageReceived(message);
break; break;
@@ -3064,6 +3076,9 @@ BTextView::_InitObject(BRect textRect, const BFont* initialFont,
fInstalledSelectOptionLinewiseShortcuts = false; fInstalledSelectOptionLinewiseShortcuts = false;
fInstalledSelectHomeEndDocwiseShortcuts = false; fInstalledSelectHomeEndDocwiseShortcuts = false;
fInstalledRemoveCommandWordwiseShortcuts = false;
fInstalledRemoveOptionWordwiseShortcuts = false;
// We put these here instead of in the constructor initializer list // We put these here instead of in the constructor initializer list
// to have less code duplication, and a single place where to do changes // to have less code duplication, and a single place where to do changes
// if needed. // if needed.
@@ -3115,8 +3130,25 @@ BTextView::_InitObject(BRect textRect, const BFont* initialFont,
//! Handles when Backspace key is pressed. //! Handles when Backspace key is pressed.
void void
BTextView::_HandleBackspace() BTextView::_HandleBackspace(int32 modifiers)
{ {
if (modifiers < 0) {
BMessage* currentMessage = Window()->CurrentMessage();
if (currentMessage == NULL
|| currentMessage->FindInt32("modifiers", &modifiers) != B_OK) {
modifiers = 0;
}
}
bool controlKeyDown = (modifiers & B_CONTROL_KEY) != 0;
bool optionKeyDown = (modifiers & B_OPTION_KEY) != 0;
bool commandKeyDown = (modifiers & B_COMMAND_KEY) != 0;
if ((commandKeyDown || optionKeyDown) && !controlKeyDown) {
fSelStart = _PreviousWordStart(fCaretOffset - 1);
fSelEnd = fCaretOffset;
}
if (fUndo) { if (fUndo) {
TypingUndoBuffer* undoBuffer = dynamic_cast<TypingUndoBuffer*>( TypingUndoBuffer* undoBuffer = dynamic_cast<TypingUndoBuffer*>(
fUndo); fUndo);
@@ -3320,8 +3352,25 @@ BTextView::_HandleArrowKey(uint32 arrowKey, int32 modifiers)
//! Handles when the Delete key is pressed. //! Handles when the Delete key is pressed.
void void
BTextView::_HandleDelete() BTextView::_HandleDelete(int32 modifiers)
{ {
if (modifiers < 0) {
BMessage* currentMessage = Window()->CurrentMessage();
if (currentMessage == NULL
|| currentMessage->FindInt32("modifiers", &modifiers) != B_OK) {
modifiers = 0;
}
}
bool controlKeyDown = (modifiers & B_CONTROL_KEY) != 0;
bool optionKeyDown = (modifiers & B_OPTION_KEY) != 0;
bool commandKeyDown = (modifiers & B_COMMAND_KEY) != 0;
if ((commandKeyDown || optionKeyDown) && !controlKeyDown) {
fSelStart = fCaretOffset;
fSelEnd = _NextWordEnd(fCaretOffset) + 1;
}
if (fUndo) { if (fUndo) {
TypingUndoBuffer* undoBuffer = dynamic_cast<TypingUndoBuffer*>( TypingUndoBuffer* undoBuffer = dynamic_cast<TypingUndoBuffer*>(
fUndo); fUndo);
@@ -4972,6 +5021,20 @@ BTextView::_Activate()
fInstalledSelectCommandWordwiseShortcuts = true; fInstalledSelectCommandWordwiseShortcuts = true;
} }
if (!Window()->HasShortcut(B_DELETE, B_COMMAND_KEY)
&& !Window()->HasShortcut(B_BACKSPACE, B_COMMAND_KEY)) {
message = new BMessage(kMsgRemoveWord);
message->AddInt32("key", B_DELETE);
message->AddInt32("modifiers", B_COMMAND_KEY);
Window()->AddShortcut(B_DELETE, B_COMMAND_KEY, message, this);
message = new BMessage(kMsgRemoveWord);
message->AddInt32("key", B_BACKSPACE);
message->AddInt32("modifiers", B_COMMAND_KEY);
Window()->AddShortcut(B_BACKSPACE, B_COMMAND_KEY, message, this);
fInstalledRemoveCommandWordwiseShortcuts = true;
}
if (!Window()->HasShortcut(B_LEFT_ARROW, B_OPTION_KEY) if (!Window()->HasShortcut(B_LEFT_ARROW, B_OPTION_KEY)
&& !Window()->HasShortcut(B_RIGHT_ARROW, B_OPTION_KEY)) { && !Window()->HasShortcut(B_RIGHT_ARROW, B_OPTION_KEY)) {
@@ -5004,6 +5067,20 @@ BTextView::_Activate()
fInstalledSelectOptionWordwiseShortcuts = true; fInstalledSelectOptionWordwiseShortcuts = true;
} }
if (!Window()->HasShortcut(B_DELETE, B_OPTION_KEY)
&& !Window()->HasShortcut(B_BACKSPACE, B_OPTION_KEY)) {
message = new BMessage(kMsgRemoveWord);
message->AddInt32("key", B_DELETE);
message->AddInt32("modifiers", B_OPTION_KEY);
Window()->AddShortcut(B_DELETE, B_OPTION_KEY, message, this);
message = new BMessage(kMsgRemoveWord);
message->AddInt32("key", B_BACKSPACE);
message->AddInt32("modifiers", B_OPTION_KEY);
Window()->AddShortcut(B_BACKSPACE, B_OPTION_KEY, message, this);
fInstalledRemoveOptionWordwiseShortcuts = true;
}
if (!Window()->HasShortcut(B_UP_ARROW, B_OPTION_KEY) if (!Window()->HasShortcut(B_UP_ARROW, B_OPTION_KEY)
&& !Window()->HasShortcut(B_DOWN_ARROW, B_OPTION_KEY)) { && !Window()->HasShortcut(B_DOWN_ARROW, B_OPTION_KEY)) {
@@ -5098,6 +5175,11 @@ BTextView::_Deactivate()
B_COMMAND_KEY | B_SHIFT_KEY); B_COMMAND_KEY | B_SHIFT_KEY);
fInstalledSelectCommandWordwiseShortcuts = false; fInstalledSelectCommandWordwiseShortcuts = false;
} }
if (fInstalledRemoveCommandWordwiseShortcuts) {
Window()->RemoveShortcut(B_DELETE, B_COMMAND_KEY);
Window()->RemoveShortcut(B_BACKSPACE, B_COMMAND_KEY);
fInstalledRemoveCommandWordwiseShortcuts = false;
}
if (fInstalledNavigateOptionWordwiseShortcuts) { if (fInstalledNavigateOptionWordwiseShortcuts) {
Window()->RemoveShortcut(B_LEFT_ARROW, B_OPTION_KEY); Window()->RemoveShortcut(B_LEFT_ARROW, B_OPTION_KEY);
@@ -5109,6 +5191,11 @@ BTextView::_Deactivate()
Window()->RemoveShortcut(B_RIGHT_ARROW, B_OPTION_KEY | B_SHIFT_KEY); Window()->RemoveShortcut(B_RIGHT_ARROW, B_OPTION_KEY | B_SHIFT_KEY);
fInstalledSelectOptionWordwiseShortcuts = false; fInstalledSelectOptionWordwiseShortcuts = false;
} }
if (fInstalledRemoveOptionWordwiseShortcuts) {
Window()->RemoveShortcut(B_DELETE, B_OPTION_KEY);
Window()->RemoveShortcut(B_BACKSPACE, B_OPTION_KEY);
fInstalledRemoveOptionWordwiseShortcuts = false;
}
if (fInstalledNavigateOptionLinewiseShortcuts) { if (fInstalledNavigateOptionLinewiseShortcuts) {
Window()->RemoveShortcut(B_UP_ARROW, B_OPTION_KEY); Window()->RemoveShortcut(B_UP_ARROW, B_OPTION_KEY);