IRCWindow.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "IRCWindow.h"
  2. #include "IRCChannel.h"
  3. #include "IRCChannelMemberListModel.h"
  4. #include "IRCClient.h"
  5. #include "IRCLogBufferModel.h"
  6. #include <LibGUI/GBoxLayout.h>
  7. #include <LibGUI/GSplitter.h>
  8. #include <LibGUI/GTableView.h>
  9. #include <LibGUI/GTextBox.h>
  10. #include <LibGUI/GTextEditor.h>
  11. IRCWindow::IRCWindow(IRCClient& client, void* owner, Type type, const String& name, GWidget* parent)
  12. : GWidget(parent)
  13. , m_client(client)
  14. , m_owner(owner)
  15. , m_type(type)
  16. , m_name(name)
  17. {
  18. set_layout(make<GBoxLayout>(Orientation::Vertical));
  19. // Make a container for the log buffer view + (optional) member list.
  20. auto* container = new GSplitter(Orientation::Horizontal, this);
  21. m_table_view = new GTableView(container);
  22. m_table_view->set_headers_visible(false);
  23. m_table_view->set_font(Font::default_fixed_width_font());
  24. m_table_view->set_alternating_row_colors(false);
  25. if (m_type == Server) {
  26. m_table_view->set_column_hidden(IRCLogBufferModel::Column::Name, true);
  27. }
  28. if (m_type == Channel) {
  29. auto* member_view = new GTableView(container);
  30. member_view->set_headers_visible(false);
  31. member_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  32. member_view->set_preferred_size({ 100, 0 });
  33. member_view->set_alternating_row_colors(false);
  34. member_view->set_model(channel().member_model());
  35. member_view->set_activates_on_selection(true);
  36. }
  37. m_text_editor = new GTextEditor(GTextEditor::SingleLine, this);
  38. m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  39. m_text_editor->set_preferred_size({ 0, 19 });
  40. m_text_editor->on_return_pressed = [this] {
  41. if (m_type == Channel)
  42. m_client.handle_user_input_in_channel(m_name, m_text_editor->text());
  43. else if (m_type == Query)
  44. m_client.handle_user_input_in_query(m_name, m_text_editor->text());
  45. else if (m_type == Server)
  46. m_client.handle_user_input_in_server(m_text_editor->text());
  47. m_text_editor->clear();
  48. };
  49. m_client.register_subwindow(*this);
  50. }
  51. IRCWindow::~IRCWindow()
  52. {
  53. m_client.unregister_subwindow(*this);
  54. }
  55. void IRCWindow::set_log_buffer(const IRCLogBuffer& log_buffer)
  56. {
  57. m_log_buffer = &log_buffer;
  58. m_table_view->set_model(log_buffer.model());
  59. }
  60. bool IRCWindow::is_active() const
  61. {
  62. return m_client.current_window() == this;
  63. }
  64. void IRCWindow::did_add_message()
  65. {
  66. if (!is_active()) {
  67. ++m_unread_count;
  68. m_client.aid_update_window_list();
  69. return;
  70. }
  71. m_table_view->scroll_to_bottom();
  72. }
  73. void IRCWindow::clear_unread_count()
  74. {
  75. if (!m_unread_count)
  76. return;
  77. m_unread_count = 0;
  78. m_client.aid_update_window_list();
  79. }
  80. int IRCWindow::unread_count() const
  81. {
  82. return m_unread_count;
  83. }