| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) RedWolf Design |
| 5 | * Copyright (c) 2007, Sven2 |
| 6 | * Copyright (c) 2017-2020, The LegacyClonk Team and contributors |
| 7 | * |
| 8 | * Distributed under the terms of the ISC license; see accompanying file |
| 9 | * "COPYING" for details. |
| 10 | * |
| 11 | * "Clonk" is a registered trademark of Matthes Bender, used with permission. |
| 12 | * See accompanying file "TRADEMARK" for details. |
| 13 | * |
| 14 | * To redistribute this file separately, substitute the full license texts |
| 15 | * for the above references. |
| 16 | */ |
| 17 | |
| 18 | // IRC client dialog |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "C4Gui.h" |
| 23 | #include "C4GuiDialogs.h" |
| 24 | #include "C4InteractiveThread.h" |
| 25 | #include "C4Network2IRC.h" |
| 26 | |
| 27 | // a GUI control to chat in |
| 28 | class C4ChatControl : public C4GUI::Window, private C4InteractiveThread::Callback |
| 29 | { |
| 30 | private: |
| 31 | class ChatSheet; |
| 32 | enum SheetType { CS_Server, CS_Channel, CS_Query }; |
| 33 | |
| 34 | private: |
| 35 | C4GUI::Tabular *pTabMain, *pTabChats; |
| 36 | // login controls |
| 37 | C4GUI::Label *pLblLoginNick, *pLblLoginPass, *pLblLoginRealName, *pLblLoginChannel; |
| 38 | C4GUI::Edit *pEdtLoginNick, *pEdtLoginPass, *pEdtLoginRealName, *pEdtLoginChannel; |
| 39 | C4GUI::Button *pBtnLogin; |
| 40 | |
| 41 | StdStrBuf sTitle; |
| 42 | C4GUI::BaseInputCallback *pTitleChangeBC; |
| 43 | |
| 44 | C4Network2IRCClient *pIRCClient; |
| 45 | bool fInitialMessagesReceived; // set after initial update call, which fetches all messages. Subsequent calls fetch only unread messages |
| 46 | |
| 47 | public: |
| 48 | C4ChatControl(C4Network2IRCClient *pIRC); |
| 49 | virtual ~C4ChatControl(); |
| 50 | |
| 51 | protected: |
| 52 | virtual void UpdateSize() override; |
| 53 | C4GUI::InputResult OnLoginDataEnter(C4GUI::Edit *edt, bool fPasting, bool fPastingMore); // advance focus when user presses enter in one of the login edits |
| 54 | void OnConnectBtn(C4GUI::Control *btn); // callback: connect button pressed |
| 55 | |
| 56 | public: |
| 57 | virtual class C4GUI::Control *GetDefaultControl(); |
| 58 | C4Network2IRCClient *getIRCClient() { return pIRCClient; } |
| 59 | |
| 60 | void SetTitleChangeCB(C4GUI::BaseInputCallback *pNewCB); |
| 61 | virtual void OnShown(); // callback when shown |
| 62 | void UpdateShownPage(); |
| 63 | void Update(); |
| 64 | void UpdateTitle(); |
| 65 | bool DlgEnter(); |
| 66 | void OnSec1Timer() { Update(); } // timer proc |
| 67 | bool ProcessInput(const char *szInput, ChatSheet *pChatSheet); // process chat input - return false if no more pasting is to be done (i.e., on /quit or /part in channel) |
| 68 | const char *GetTitle() { return sTitle.getData(); } |
| 69 | void UserQueryQuit(); |
| 70 | ChatSheet *OpenQuery(const char *szForNick, bool fSelect, const char *szIdentFallback); |
| 71 | |
| 72 | private: |
| 73 | static bool IsServiceName(const char *szName); |
| 74 | ChatSheet *GetActiveChatSheet(); |
| 75 | ChatSheet *GetSheetByTitle(const char *szTitle, C4ChatControl::SheetType eType); |
| 76 | ChatSheet *GetSheetByIdent(const char *szIdent, C4ChatControl::SheetType eType); |
| 77 | ChatSheet *GetServerSheet(); |
| 78 | void ClearChatSheets(); |
| 79 | |
| 80 | // IRC event hook |
| 81 | virtual void OnThreadEvent(C4InteractiveEventType eEvent, const std::any &eventData) override { if (std::any_cast<decltype(pIRCClient)>(any: eventData) == pIRCClient) Update(); } |
| 82 | }; |
| 83 | |
| 84 | // container dialog for the C4ChatControl |
| 85 | class C4ChatDlg : public C4GUI::Dialog |
| 86 | { |
| 87 | private: |
| 88 | static C4ChatDlg *pInstance; |
| 89 | class C4ChatControl *pChatCtrl; |
| 90 | C4GUI::Button *pBtnClose; |
| 91 | |
| 92 | public: |
| 93 | C4ChatDlg(); |
| 94 | virtual ~C4ChatDlg(); |
| 95 | |
| 96 | static C4ChatDlg *ShowChat(); |
| 97 | static void StopChat(); |
| 98 | static bool IsChatActive(); |
| 99 | static bool IsChatEnabled(); |
| 100 | static bool ToggleChat(); |
| 101 | |
| 102 | protected: |
| 103 | // default control to be set if unprocessed keyboard input has been detected |
| 104 | virtual class C4GUI::Control *GetDefaultControl() override; |
| 105 | |
| 106 | // true for dialogs that should span the whole screen |
| 107 | // not just the mouse-viewport |
| 108 | virtual bool IsFreePlaceDialog() override { return true; } |
| 109 | |
| 110 | // true for dialogs that receive keyboard input even in shared mode |
| 111 | virtual bool IsExclusiveDialog() override { return true; } |
| 112 | |
| 113 | // for custom placement procedures; should call SetPos |
| 114 | virtual bool DoPlacement(C4GUI::Screen *pOnScreen, const C4Rect &rPreferredDlgRect) override; |
| 115 | |
| 116 | virtual void OnClosed(bool fOK) override; // callback when dlg got closed |
| 117 | virtual void OnShown() override; // callback when shown - should not delete the dialog |
| 118 | |
| 119 | virtual void UpdateSize() override; |
| 120 | |
| 121 | void OnChatTitleChange(const StdStrBuf &sNewTitle); |
| 122 | }; |
| 123 | |