OpenClonk
C4ChatDlg.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2007-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2013-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // IRC client dialog
17 
18 #ifndef INC_C4ChatDlg
19 #define INC_C4ChatDlg
20 
21 #include "gui/C4Gui.h"
23 
24 // a GUI control to chat in
26 {
27 private:
28  // one open channel or query
29  class ChatSheet : public C4GUI::Tabular::Sheet
30  {
31  public:
32  // one item in the nick list
33  class NickItem : public C4GUI::Window
34  {
35  private:
36  C4GUI::Icon *pStatusIcon; // status icon indicating channel status (e.g. operator)
37  C4GUI::Label *pNameLabel; // the nickname
38  bool fFlaggedExisting; // flagged for existance; used by user update func
39  int32_t iStatus;
40 
41  public:
42  NickItem(class C4Network2IRCUser *pByUser);
43 
44  protected:
45  void UpdateOwnPos() override;
46 
47  public:
48  const char *GetNick() const { return pNameLabel->GetText(); }
49  int32_t GetStatus() const { return iStatus; }
50 
51  void Update(class C4Network2IRCUser *pByUser);
52  void SetFlaggedExisting(bool fToVal) { fFlaggedExisting = fToVal; }
53  bool IsFlaggedExisting() const { return fFlaggedExisting; }
54 
55  static int32_t SortFunc(const C4GUI::Element *pEl1, const C4GUI::Element *pEl2, void *);
56 
57  };
58 
59  enum SheetType { CS_Server, CS_Channel, CS_Query };
60  private:
61  C4ChatControl *pChatControl;
62  C4GUI::TextWindow *pChatBox;
63  C4GUI::ListBox *pNickList;
64  C4GUI::WoodenLabel *pInputLbl;
65  C4GUI::Edit *pInputEdit;
66  int32_t iBackBufferIndex; // chat message history index
67  SheetType eType;
68  StdStrBuf sIdent;
69  bool fHasUnread;
70  StdStrBuf sChatTitle; // topic for channels; name+ident for queries; server name for server sheet
71 
72  C4KeyBinding *pKeyHistoryUp, *pKeyHistoryDown; // keys used to scroll through chat history
73 
74  public:
75  ChatSheet(C4ChatControl *pChatControl, const char *szTitle, const char *szIdent, SheetType eType);
76  ~ChatSheet() override;
77 
78  C4GUI::Edit *GetInputEdit() const { return pInputEdit; }
79  SheetType GetSheetType() const { return eType; }
80  const char *GetIdent() const { return sIdent.getData(); }
81  void SetIdent(const char *szToIdent) { sIdent.Copy(szToIdent); }
82  const char *GetChatTitle() const { return sChatTitle.getData(); }
83  void SetChatTitle(const char *szNewTitle) { sChatTitle.Copy(szNewTitle); }
84 
85  void AddTextLine(const char *szText, uint32_t dwClr);
86  void DoError(const char *szError);
87  void Update(bool fLock);
88  void UpdateUsers(C4Network2IRCUser *pUsers);
89  void ResetUnread(); // mark messages as read
90 
91  protected:
92  void UpdateSize() override;
93  void OnShown(bool fByUser) override;
94  void UserClose() override; // user pressed close button: Close queries, part channels, etc.
95 
96  C4GUI::Edit::InputResult OnChatInput(C4GUI::Edit *edt, bool fPasting, bool fPastingMore);
97  bool KeyHistoryUpDown(bool fUp);
98  void OnNickDblClick(class C4GUI::Element *pEl);
99 
100  private:
101  NickItem *GetNickItem(const char *szByNick);
102  NickItem *GetFirstNickItem() { return pNickList ? static_cast<NickItem *>(pNickList->GetFirst()) : nullptr; }
103  NickItem *GetNextNickItem(NickItem *pPrev) { return static_cast<NickItem *>(pPrev->GetNext()); }
104  };
105 
106 private:
107  C4GUI::Tabular *pTabMain, *pTabChats;
108  // login controls
109  C4GUI::Label *pLblLoginNick, *pLblLoginPass, *pLblLoginRealName, *pLblLoginChannel;
110  C4GUI::Edit *pEdtLoginNick, *pEdtLoginPass, *pEdtLoginRealName, *pEdtLoginChannel;
111  C4GUI::Button *pBtnLogin;
112 
113  StdStrBuf sTitle;
114  C4GUI::BaseInputCallback *pTitleChangeBC;
115 
116  C4Network2IRCClient *pIRCClient;
117  bool fInitialMessagesReceived; // set after initial update call, which fetches all messages. Subsequent calls fetch only unread messages
118 
119 public:
121  ~C4ChatControl() override;
122 
123 protected:
124  void UpdateSize() override;
125  C4GUI::Edit::InputResult OnLoginDataEnter(C4GUI::Edit *edt, bool fPasting, bool fPastingMore); // advance focus when user presses enter in one of the login edits
126  void OnConnectBtn(C4GUI::Control *btn); // callback: connect button pressed
127 
128 public:
129  virtual class C4GUI::Control *GetDefaultControl();
130  C4Network2IRCClient *getIRCClient() { return pIRCClient; }
131 
133  virtual void OnShown(); // callback when shown
134  void UpdateShownPage();
135  void Update();
136  void UpdateTitle();
137  bool DlgEnter();
138  void OnSec1Timer() { Update(); } // timer proc
139  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)
140  const char *GetTitle() { return sTitle.getData(); }
141  void UserQueryQuit();
142  ChatSheet *OpenQuery(const char *szForNick, bool fSelect, const char *szIdentFallback);
143 
144 private:
145  static bool IsServiceName(const char *szName);
146  ChatSheet *GetActiveChatSheet();
147  ChatSheet *GetSheetByTitle(const char *szTitle, C4ChatControl::ChatSheet::SheetType eType);
148  ChatSheet *GetSheetByIdent(const char *szIdent, C4ChatControl::ChatSheet::SheetType eType);
149  ChatSheet *GetServerSheet();
150  void ClearChatSheets();
151 
152  // IRC event hook
153  void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData) override { if (pEventData == pIRCClient) Update(); }
154 
155 };
156 
157 // container dialog for the C4ChatControl
158 class C4ChatDlg : public C4GUI::Dialog
159 {
160 private:
161  static C4ChatDlg *pInstance;
162  class C4ChatControl *pChatCtrl;
163 
164 public:
165  C4ChatDlg();
166  ~C4ChatDlg() override;
167 
168  static C4ChatDlg *ShowChat();
169  static void StopChat();
170  static bool IsChatActive();
171  static bool IsChatEnabled();
172  static bool ToggleChat();
173 
174 protected:
175  // default control to be set if unprocessed keyboard input has been detected
176  class C4GUI::Control *GetDefaultControl() override;
177 
178  // true for dialogs that should span the whole screen
179  // not just the mouse-viewport
180  bool IsFreePlaceDialog() override { return true; }
181 
182  // true for dialogs that receive keyboard input even in shared mode
183  bool IsExclusiveDialog() override { return true; }
184 
185  // for custom placement procedures; should call SetPos
186  bool DoPlacement(C4GUI::Screen *pOnScreen, const C4Rect &rPreferredDlgRect) override;
187 
188  void OnClosed(bool fOK) override; // callback when dlg got closed
189  void OnShown() override; // callback when shown - should not delete the dialog
190 
191  void UpdateSize() override;
192 
193  void OnExitBtn(C4GUI::Control *btn); // callback: exit button pressed
194  void OnChatTitleChange(const StdStrBuf &sNewTitle);
195 };
196 
197 #endif // INC_C4ChatDlg
C4InteractiveEventType
void Update(class C4Network2IRCUser *pByUser)
Definition: C4ChatDlg.cpp:55
NickItem(class C4Network2IRCUser *pByUser)
Definition: C4ChatDlg.cpp:29
void SetFlaggedExisting(bool fToVal)
Definition: C4ChatDlg.h:52
const char * GetNick() const
Definition: C4ChatDlg.h:48
static int32_t SortFunc(const C4GUI::Element *pEl1, const C4GUI::Element *pEl2, void *)
Definition: C4ChatDlg.cpp:77
void OnSec1Timer()
Definition: C4ChatDlg.h:138
virtual void OnShown()
Definition: C4ChatDlg.cpp:421
C4GUI::Edit::InputResult OnLoginDataEnter(C4GUI::Edit *edt, bool fPasting, bool fPastingMore)
Definition: C4ChatDlg.cpp:480
void SetTitleChangeCB(C4GUI::BaseInputCallback *pNewCB)
Definition: C4ChatDlg.cpp:383
void UpdateTitle()
Definition: C4ChatDlg.cpp:757
void UpdateShownPage()
Definition: C4ChatDlg.cpp:549
virtual class C4GUI::Control * GetDefaultControl()
Definition: C4ChatDlg.cpp:426
C4ChatControl(C4Network2IRCClient *pIRC)
Definition: C4ChatDlg.cpp:337
void OnConnectBtn(C4GUI::Control *btn)
Definition: C4ChatDlg.cpp:488
const char * GetTitle()
Definition: C4ChatDlg.h:140
void UserQueryQuit()
Definition: C4ChatDlg.cpp:931
C4Network2IRCClient * getIRCClient()
Definition: C4ChatDlg.h:130
bool ProcessInput(const char *szInput, ChatSheet *pChatSheet)
Definition: C4ChatDlg.cpp:800
~C4ChatControl() override
Definition: C4ChatDlg.cpp:377
bool DlgEnter()
Definition: C4ChatDlg.cpp:786
void UpdateSize() override
Definition: C4ChatDlg.cpp:391
ChatSheet * OpenQuery(const char *szForNick, bool fSelect, const char *szIdentFallback)
Definition: C4ChatDlg.cpp:734
static void StopChat()
Definition: C4ChatDlg.cpp:980
static bool IsChatEnabled()
Definition: C4ChatDlg.cpp:1004
void OnShown() override
Definition: C4ChatDlg.cpp:1032
bool IsExclusiveDialog() override
Definition: C4ChatDlg.h:183
static C4ChatDlg * ShowChat()
Definition: C4ChatDlg.cpp:966
void UpdateSize() override
Definition: C4ChatDlg.cpp:1040
~C4ChatDlg() override
void OnClosed(bool fOK) override
Definition: C4ChatDlg.cpp:1024
static bool ToggleChat()
Definition: C4ChatDlg.cpp:987
bool IsFreePlaceDialog() override
Definition: C4ChatDlg.h:180
class C4GUI::Control * GetDefaultControl() override
Definition: C4ChatDlg.cpp:1009
void OnChatTitleChange(const StdStrBuf &sNewTitle)
Definition: C4ChatDlg.cpp:1055
bool DoPlacement(C4GUI::Screen *pOnScreen, const C4Rect &rPreferredDlgRect) override
Definition: C4ChatDlg.cpp:1014
static bool IsChatActive()
Definition: C4ChatDlg.cpp:996
void OnExitBtn(C4GUI::Control *btn)
Definition: C4ChatDlg.cpp:1050
bool fOK
Definition: C4Gui.h:2083
Element * GetNext() const
Definition: C4Gui.h:449
Element * pPrev
Definition: C4Gui.h:377
const char * GetText()
Definition: C4Gui.h:503
Element * GetFirst()
Definition: C4Gui.h:1572
Definition: C4Rect.h:28
const char * getData() const
Definition: StdBuf.h:442
void Copy()
Definition: StdBuf.h:467