Chat.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*\
  2. |*| Copyright 2015-2016 bill-auger <https://github.com/bill-auger/av-caster/issues>
  3. |*|
  4. |*| This file is part of the AvCaster program.
  5. |*|
  6. |*| AvCaster is free software: you can redistribute it and/or modify
  7. |*| it under the terms of the GNU General Public License version 3
  8. |*| as published by the Free Software Foundation.
  9. |*|
  10. |*| AvCaster is distributed in the hope that it will be useful,
  11. |*| but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. |*| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. |*| GNU General Public License for more details.
  14. |*|
  15. |*| You should have received a copy of the GNU General Public License
  16. |*| along with AvCaster. If not, see <http://www.gnu.org/licenses/>.
  17. \*/
  18. //[Headers] You can add your own extra header files here...
  19. #include "../Controllers/AvCaster.h"
  20. #include "../Trace/TraceChat.h"
  21. //[/Headers]
  22. #include "Chat.h"
  23. //[MiscUserDefs] You can add your own user definitions and misc code here...
  24. //[/MiscUserDefs]
  25. //==============================================================================
  26. Chat::Chat (MainContent* main_content)
  27. {
  28. //[Constructor_pre] You can add your own custom stuff here..
  29. //[/Constructor_pre]
  30. addAndMakeVisible (chatGroup = new GroupComponent ("chatGroup",
  31. TRANS("Chat")));
  32. chatGroup->setColour (GroupComponent::outlineColourId, Colours::white);
  33. chatGroup->setColour (GroupComponent::textColourId, Colours::white);
  34. addAndMakeVisible (chatHistoryGroup = new GroupComponent ("chatHistoryGroup",
  35. String()));
  36. chatHistoryGroup->setColour (GroupComponent::outlineColourId, Colours::white);
  37. chatHistoryGroup->setColour (GroupComponent::textColourId, Colours::white);
  38. addAndMakeVisible (chatHistoryText = new TextEditor ("chatHistoryText"));
  39. chatHistoryText->setMultiLine (true);
  40. chatHistoryText->setReturnKeyStartsNewLine (false);
  41. chatHistoryText->setReadOnly (true);
  42. chatHistoryText->setScrollbarsShown (true);
  43. chatHistoryText->setCaretVisible (false);
  44. chatHistoryText->setPopupMenuEnabled (true);
  45. chatHistoryText->setColour (TextEditor::textColourId, Colours::white);
  46. chatHistoryText->setColour (TextEditor::backgroundColourId, Colours::black);
  47. chatHistoryText->setText (String());
  48. addAndMakeVisible (chatEntryGroup = new GroupComponent ("chatEntryGroup",
  49. String()));
  50. chatEntryGroup->setExplicitFocusOrder (1);
  51. chatEntryGroup->setColour (GroupComponent::outlineColourId, Colours::white);
  52. chatEntryGroup->setColour (GroupComponent::textColourId, Colours::white);
  53. addAndMakeVisible (chatEntryText = new TextEditor ("chatEntryText"));
  54. chatEntryText->setExplicitFocusOrder (2);
  55. chatEntryText->setMultiLine (false);
  56. chatEntryText->setReturnKeyStartsNewLine (false);
  57. chatEntryText->setReadOnly (false);
  58. chatEntryText->setScrollbarsShown (false);
  59. chatEntryText->setCaretVisible (true);
  60. chatEntryText->setPopupMenuEnabled (true);
  61. chatEntryText->setColour (TextEditor::textColourId, Colours::white);
  62. chatEntryText->setColour (TextEditor::backgroundColourId, Colour (0xff202020));
  63. chatEntryText->setText (String());
  64. addAndMakeVisible (chatList = new ChatList());
  65. chatList->setExplicitFocusOrder (2);
  66. chatList->setName ("chatList");
  67. //[UserPreSize]
  68. //[/UserPreSize]
  69. setSize (1, 1);
  70. //[Constructor] You can add your own custom stuff here..
  71. // configure look and feel , validations, and listeners
  72. TextEditor::Listener* this_text_listener = static_cast<TextEditor::Listener*>(this) ;
  73. main_content->configureTextEditor(this->chatEntryText , this_text_listener ,
  74. GUI::MAX_MOTD_LEN , String::empty ) ;
  75. this->chatEntryText->setTextToShowWhenEmpty(GUI::CHAT_PROMPT_TEXT , GUI::TEXT_EMPTY_COLOR) ;
  76. setFontSize() ;
  77. // defer visibility until connected to some chat channel
  78. updateVisiblilty(false) ;
  79. //[/Constructor]
  80. }
  81. Chat::~Chat()
  82. {
  83. //[Destructor_pre]. You can add your own custom destruction code here..
  84. //[/Destructor_pre]
  85. chatGroup = nullptr;
  86. chatHistoryGroup = nullptr;
  87. chatHistoryText = nullptr;
  88. chatEntryGroup = nullptr;
  89. chatEntryText = nullptr;
  90. chatList = nullptr;
  91. //[Destructor]. You can add your own custom destruction code here..
  92. //[/Destructor]
  93. }
  94. //==============================================================================
  95. void Chat::paint (Graphics& g)
  96. {
  97. //[UserPrePaint] Add your own custom painting code here..
  98. //[/UserPrePaint]
  99. g.setColour (Colour (0xff282828));
  100. g.fillRoundedRectangle (20.0f, 18.0f, static_cast<float> (getWidth() - 40), static_cast<float> (getHeight() - 36), 4.000f);
  101. //[UserPaint] Add your own custom painting code here..
  102. //[/UserPaint]
  103. }
  104. void Chat::resized()
  105. {
  106. //[UserPreResize] Add your own custom resize code here..
  107. //[/UserPreResize]
  108. chatGroup->setBounds (16, 8, getWidth() - 32, getHeight() - 24);
  109. chatHistoryGroup->setBounds (28, 22, getWidth() - 56, getHeight() - 90);
  110. chatHistoryText->setBounds (32, 32, getWidth() - 64, getHeight() - 104);
  111. chatEntryGroup->setBounds (28, getHeight() - 68, getWidth() - 56, 38);
  112. chatEntryText->setBounds (32, getHeight() - 58, getWidth() - 64, 24);
  113. chatList->setBounds (getWidth() - 160, 32, 128, 48);
  114. //[UserResized] Add your own custom resize handling here..
  115. //[/UserResized]
  116. }
  117. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  118. /* Chat public instance methods */
  119. void Chat::updateVisiblilty(bool is_visible)
  120. {
  121. String group_title = (is_visible) ? GUI::CHAT_GROUP_TITLE : AvCaster::GetVersionString() ;
  122. DEBUG_TRACE_CHAT_VISIBILITY
  123. const MessageManagerLock mmLock ;
  124. this->chatGroup ->setText (group_title) ;
  125. this->chatHistoryGroup->setVisible(is_visible) ;
  126. this->chatHistoryText ->setVisible(is_visible) ;
  127. this->chatEntryGroup ->setVisible(is_visible) ;
  128. this->chatEntryText ->setVisible(is_visible) ;
  129. this->chatList ->setVisible(is_visible) ;
  130. }
  131. #ifndef DISABLE_CHAT
  132. void Chat::addChatLine(String prefix , String nick , String message)
  133. {
  134. String newline = (this->chatHistoryText->getText().isEmpty()) ? String::empty : newLine ;
  135. prefix = prefix .trim() ;
  136. nick = nick .trim() ;
  137. message = message.trim() ;
  138. prefix = (prefix.isEmpty() ) ? String::empty : prefix + " " ;
  139. nick = (nick == GUI::CLIENT_NICK) ? String::empty : nick + ": " ;
  140. const MessageManagerLock mmLock ;
  141. this->chatHistoryText->moveCaretToEnd() ;
  142. this->chatHistoryText->insertTextAtCaret(newline + prefix + nick + message) ;
  143. }
  144. #endif // DISABLE_CHAT
  145. /* Chat private instance methods */
  146. void Chat::textEditorReturnKeyPressed(TextEditor& a_text_editor)
  147. {
  148. if (&a_text_editor != this->chatEntryText) return ;
  149. #ifndef DISABLE_CHAT
  150. AvCaster::SendChat(this->chatEntryText->getText()) ;
  151. #endif // DISABLE_CHAT
  152. this->chatEntryText->clear() ;
  153. }
  154. void Chat::setFontSize()
  155. {
  156. DEBUG_TRACE_SET_FONTSIZE
  157. Font current_font = this->chatHistoryText->getFont() ;
  158. int new_font_size = getFontSize() ;
  159. Font new_font = current_font.withHeight(new_font_size) ;
  160. this->chatHistoryText->applyFontToAllText(new_font) ;
  161. this->chatEntryText ->applyFontToAllText(new_font) ;
  162. // resized() ; // TODO: dynamic font size nyi
  163. }
  164. int Chat::getFontSize()
  165. {
  166. // return GUI::FONT_SIZE ;
  167. return 16 ;
  168. /* TODO: dynamic font size nyi
  169. int font_size_n = int(this->fontSize.getValue()) ;
  170. int font_size_n = int(this->fontSize.getValue()) ;
  171. int font_size = GUI::FONT_SIZES[font_size_n].getIntValue() ;
  172. return font_size ;
  173. */
  174. }
  175. //[/MiscUserCode]
  176. //==============================================================================
  177. #if 0
  178. /* -- Projucer information section --
  179. This is where the Projucer stores the metadata that describe this GUI layout, so
  180. make changes in here at your peril!
  181. BEGIN_JUCER_METADATA
  182. <JUCER_COMPONENT documentType="Component" className="Chat" componentName="" parentClasses="public Component, public TextEditor::Listener"
  183. constructorParams="MainContent* main_content" variableInitialisers=""
  184. snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330"
  185. fixedSize="0" initialWidth="1" initialHeight="1">
  186. <BACKGROUND backgroundColour="0">
  187. <ROUNDRECT pos="20 18 40M 36M" cornerSize="4" fill="solid: ff282828" hasStroke="0"/>
  188. </BACKGROUND>
  189. <GROUPCOMPONENT name="chatGroup" id="6607ba656d5c8919" memberName="chatGroup"
  190. virtualName="" explicitFocusOrder="0" pos="16 8 32M 24M" outlinecol="ffffffff"
  191. textcol="ffffffff" title="Chat"/>
  192. <GROUPCOMPONENT name="chatHistoryGroup" id="258cc166964ef054" memberName="chatHistoryGroup"
  193. virtualName="" explicitFocusOrder="0" pos="28 22 56M 90M" outlinecol="ffffffff"
  194. textcol="ffffffff" title=""/>
  195. <TEXTEDITOR name="chatHistoryText" id="f98355c5e336ef72" memberName="chatHistoryText"
  196. virtualName="" explicitFocusOrder="0" pos="32 32 64M 104M" textcol="ffffffff"
  197. bkgcol="ff000000" initialText="" multiline="1" retKeyStartsLine="0"
  198. readonly="1" scrollbars="1" caret="0" popupmenu="1"/>
  199. <GROUPCOMPONENT name="chatEntryGroup" id="da473ae049822d5c" memberName="chatEntryGroup"
  200. virtualName="" explicitFocusOrder="1" pos="28 68R 56M 38" outlinecol="ffffffff"
  201. textcol="ffffffff" title=""/>
  202. <TEXTEDITOR name="chatEntryText" id="1dd802ce165b4013" memberName="chatEntryText"
  203. virtualName="" explicitFocusOrder="2" pos="32 58R 64M 24" textcol="ffffffff"
  204. bkgcol="ff202020" initialText="" multiline="0" retKeyStartsLine="0"
  205. readonly="0" scrollbars="0" caret="1" popupmenu="1"/>
  206. <GENERICCOMPONENT name="chatList" id="90795555172fbed0" memberName="chatList" virtualName=""
  207. explicitFocusOrder="2" pos="160R 32 128 48" class="ChatList"
  208. params=""/>
  209. </JUCER_COMPONENT>
  210. END_JUCER_METADATA
  211. */
  212. #endif
  213. //[EndFile] You can add extra defines here...
  214. //[/EndFile]