From ab4881f2f099f3bf5dc3516932a8c97de68cb9af Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Mon, 16 Aug 2021 21:50:43 -0500 Subject: [PATCH] Receiving multi-color messages Chat messages can now be formatted with colors in addition to varying faces with "color_start," "color_length," and "color" slots in IM_MESSAGE_RECEIVED. --- application/ChatProtocolMessages.h | 5 ++++- application/views/ConversationView.cpp | 28 +++++++++++++++++++++++++- application/views/ConversationView.h | 2 ++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/application/ChatProtocolMessages.h b/application/ChatProtocolMessages.h index b017b48..36508bc 100644 --- a/application/ChatProtocolMessages.h +++ b/application/ChatProtocolMessages.h @@ -77,10 +77,13 @@ enum im_what_code { buffer, rather than a specific conversation. face_start and face_length specify the location of formatted text in the body, and "face" is the desired font face. Unsupported in bulk, + color_* works much the same, but with colors. Not much else to say. i.e., IM_LOGS_RECEIVED. Requires: String "body" Allows: String "chat_id", String "user_id", String "user_name", - int32s "face_start", int32s "face_length", uint16s "face" */ + int32s "face_start", int32s "face_length", uint16s "face" + int32s "color_start", int32s "color_length", + rgb_colors "color" */ IM_MESSAGE_RECEIVED = 22, /*! Logs received →App diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index 11e8432..c6124d3 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -413,7 +413,7 @@ ConversationView::_AppendMessage(BMessage* msg) } // … else we're jamming a message into this view no matter what it takes! - if (msg->HasInt32("face_start") == true) { + if (msg->HasInt32("face_start") || msg->HasInt32("color_start")) { _AppendFormattedMessage(msg); return; } @@ -504,10 +504,12 @@ ConversationView::_AppendFormattedMessage(BMessage* msg) uint16 face = 0; UInt16IntMap face_indices; rgb_color color = ui_color(B_PANEL_TEXT_COLOR); + int32 colorIndice = -1; BFont font; for (int i = 0; i < body.CountChars(); i++) { _EnableStartingFaces(msg, i, &face, &face_indices); + _EnableStartingColor(msg, i, &color, &colorIndice); if (face == B_REGULAR_FACE) { font = BFont(); @@ -517,6 +519,9 @@ ConversationView::_AppendFormattedMessage(BMessage* msg) font.SetFace(face); } + if (colorIndice <= 0) + color = ui_color(B_PANEL_TEXT_COLOR); + int32 bytes; const char* curChar = body.CharAt(i, &bytes); char append[bytes]; @@ -527,6 +532,7 @@ ConversationView::_AppendFormattedMessage(BMessage* msg) fReceiveView->Append(append, color, &font); _DisableEndingFaces(msg, &face, &face_indices); + colorIndice--; } fReceiveView->Append("\n"); } @@ -576,6 +582,26 @@ ConversationView::_DisableEndingFaces(BMessage* msg, uint16* face, } +void +ConversationView::_EnableStartingColor(BMessage* msg, int32 index, + rgb_color* color, int32* indice) +{ + rgb_color newColor; + int32 color_start, color_length, i = 0; + while (msg->FindInt32("color_start", i, &color_start) == B_OK) { + if (color_start == index + && msg->FindInt32("color_length", i, &color_length) == B_OK + && msg->FindColor("color", i, &newColor) == B_OK) + { + *indice = color_length; + *color = newColor; + break; + } + i++; + } +} + + void ConversationView::_UserMessage(const char* format, const char* bodyFormat, BMessage* msg) diff --git a/application/views/ConversationView.h b/application/views/ConversationView.h index 07146fc..dd7a999 100644 --- a/application/views/ConversationView.h +++ b/application/views/ConversationView.h @@ -64,6 +64,8 @@ private: uint16* face, UInt16IntMap* indices); void _DisableEndingFaces(BMessage* msg, uint16* face, UInt16IntMap* indices); + void _EnableStartingColor(BMessage* msg, int32 index, + rgb_color* color, int32* indice); void _UserMessage(const char* format, const char* bodyFormat, BMessage* msg);