Chat-O-Matic/application/views/SendTextView.cpp

187 lines
3.8 KiB
C++
Raw Permalink Normal View History

/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "SendTextView.h"
2021-07-09 10:39:48 -05:00
#include <StringList.h>
#include <Window.h>
#include "AppMessages.h"
2021-07-31 20:18:36 -05:00
#include "Server.h"
SendTextView::SendTextView(const char* name, ConversationView* convView)
:
BTextView(name),
fChatView(convView),
fCurrentIndex(0),
fHistoryIndex(0)
{
}
void
SendTextView::KeyDown(const char* bytes, int32 numBytes)
{
int32 modifiers = Window()->CurrentMessage()->GetInt32("modifiers", 0);
2021-07-09 10:39:48 -05:00
if (bytes[0] == B_TAB) {
_AutoComplete();
return;
}
// Reset auto-complete state if user typed/sent something other than tab
2021-07-09 10:39:48 -05:00
fCurrentIndex = 0;
fCurrentWord.SetTo("");
for (int i = 0; i < numBytes; i++) {
if ((bytes[i] == B_UP_ARROW) && (modifiers == 0)) {
_UpHistory();
return;
}
else if ((bytes[i] == B_DOWN_ARROW) && (modifiers == 0)) {
_DownHistory();
return;
}
if ((bytes[i] == B_ENTER)
&& ((modifiers & B_COMMAND_KEY) || (modifiers & B_SHIFT_KEY))) {
Insert("\n");
return;
}
else if (bytes[i] == B_ENTER) {
_AppendHistory();
fChatView->MessageReceived(new BMessage(APP_CHAT));
return;
}
}
BTextView::KeyDown(bytes, numBytes);
}
2021-07-09 10:39:48 -05:00
void
SendTextView::_AutoComplete()
{
if (fChatView == NULL || fChatView->GetConversation() == NULL)
2021-07-31 20:18:36 -05:00
return;
2021-07-09 10:39:48 -05:00
BStringList words;
BString text = Text();
text.Split(" ", true, words);
if (words.CountStrings() <= 0)
return;
BString lastWord = words.StringAt(words.CountStrings() - 1);
if (fCurrentWord.IsEmpty() == true)
fCurrentWord = lastWord;
2021-07-31 20:18:36 -05:00
// Now to find the substitutes
BString substitution;
if (fCurrentWord.StartsWith("/") == true) {
2021-08-14 22:10:39 -05:00
substitution =
_NextMatch(_CommandNames(), BString(fCurrentWord).RemoveFirst("/"));
if (substitution.IsEmpty() == false)
substitution.Prepend("/");
}
2021-07-31 20:18:36 -05:00
else
substitution = _NextMatch(_UserNames(), fCurrentWord);
2021-07-31 20:18:36 -05:00
2021-08-14 22:10:39 -05:00
// Apply the substitution or jet off
if (substitution.IsEmpty() == true)
2021-07-31 20:18:36 -05:00
fCurrentIndex = 0;
else {
int32 index = text.FindLast(lastWord);
int32 newindex = index + substitution.Length();
2021-08-14 22:10:39 -05:00
Delete(index, index + lastWord.CountChars());
Insert(index, substitution, substitution.Length());
Select(newindex, newindex);
2021-07-31 20:18:36 -05:00
}
}
2021-07-09 10:39:48 -05:00
BString
SendTextView::_NextMatch(BStringList list, BString current)
2021-07-31 20:18:36 -05:00
{
BString match;
for (int i = 0, j = 0; i < list.CountStrings(); i++)
if (list.StringAt(i).StartsWith(current)) {
2021-07-31 20:18:36 -05:00
if (j == fCurrentIndex) {
match = list.StringAt(i);
2021-07-31 20:18:36 -05:00
fCurrentIndex++;
break;
}
j++;
}
return match;
2021-07-31 20:18:36 -05:00
}
BStringList
SendTextView::_CommandNames()
2021-07-31 20:18:36 -05:00
{
if (fCurrentIndex == 0) {
int64 instance = fChatView->GetConversation()->GetProtocolLooper()->GetInstance();
BStringList cmdNames;
CommandMap cmds = Server::Get()->Commands(instance);
for (int i = 0; i < cmds.CountItems(); i++)
cmdNames.Add(cmds.KeyAt(i));
fCurrentList = cmdNames;
}
return fCurrentList;
}
BStringList
SendTextView::_UserNames()
{
if (fCurrentIndex == 0) {
BStringList nameAndId;
UserMap users = fChatView->GetConversation()->Users();
for (int i = 0; i < users.CountItems(); i++) {
nameAndId.Add(users.KeyAt(i));
nameAndId.Add(users.ValueAt(i)->GetName());
2021-07-09 10:39:48 -05:00
}
fCurrentList = nameAndId;
}
return fCurrentList;
2021-07-09 10:39:48 -05:00
}
void
SendTextView::_AppendHistory()
{
fHistoryIndex = 0;
fHistory.Add(BString(Text()), 0);
if (fHistory.CountStrings() == 21)
fHistory.Remove(20);
}
void
SendTextView::_UpHistory()
{
if (fHistoryIndex == 0 && TextLength() > 0)
_AppendHistory();
if (fHistoryIndex < fHistory.CountStrings()) {
fHistoryIndex++;
SetText(fHistory.StringAt(fHistoryIndex - 1));
2021-07-31 20:18:36 -05:00
Select(TextLength(), TextLength());
}
}
void
SendTextView::_DownHistory()
{
if (fHistoryIndex > 1) {
fHistoryIndex--;
SetText(fHistory.StringAt(fHistoryIndex - 1));
2021-07-31 20:18:36 -05:00
Select(TextLength(), TextLength());
}
}