3f06ad90cb640e5bd9ffa3d7490d3a4b1ee46bd9
[novacoin.git] / src / ui.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef BITCOIN_UI_H
5 #define BITCOIN_UI_H
6
7 #include <boost/function.hpp>
8 #include "wallet.h"
9
10 DECLARE_EVENT_TYPE(wxEVT_UITHREADCALL, -1)
11
12
13
14 extern wxLocale g_locale;
15
16
17
18 void HandleCtrlA(wxKeyEvent& event);
19 void UIThreadCall(boost::function0<void>);
20 int ThreadSafeMessageBox(const std::string& message, const std::string& caption="Message", int style=wxOK, wxWindow* parent=NULL, int x=-1, int y=-1);
21 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent);
22 void CalledSetStatusBar(const std::string& strText, int nField);
23 void MainFrameRepaint();
24 void CreateMainWindow();
25 void SetStartOnSystemStartup(bool fAutoStart);
26
27
28
29
30 inline int MyMessageBox(const wxString& message, const wxString& caption="Message", int style=wxOK, wxWindow* parent=NULL, int x=-1, int y=-1)
31 {
32 #ifdef GUI
33     if (!fDaemon)
34         return wxMessageBox(message, caption, style, parent, x, y);
35 #endif
36     printf("wxMessageBox %s: %s\n", std::string(caption).c_str(), std::string(message).c_str());
37     fprintf(stderr, "%s: %s\n", std::string(caption).c_str(), std::string(message).c_str());
38     return wxOK;
39 }
40 #define wxMessageBox  MyMessageBox
41
42
43
44
45
46
47 class CMainFrame : public CMainFrameBase
48 {
49 protected:
50     // Event handlers
51     void OnNotebookPageChanged(wxNotebookEvent& event);
52     void OnClose(wxCloseEvent& event);
53     void OnIconize(wxIconizeEvent& event);
54     void OnMouseEvents(wxMouseEvent& event);
55     void OnKeyDown(wxKeyEvent& event) { HandleCtrlA(event); }
56     void OnIdle(wxIdleEvent& event);
57     void OnPaint(wxPaintEvent& event);
58     void OnPaintListCtrl(wxPaintEvent& event);
59     void OnMenuFileExit(wxCommandEvent& event);
60     void OnUpdateUIOptionsGenerate(wxUpdateUIEvent& event);
61     void OnMenuOptionsChangeYourAddress(wxCommandEvent& event);
62     void OnMenuOptionsOptions(wxCommandEvent& event);
63     void OnMenuHelpAbout(wxCommandEvent& event);
64     void OnButtonSend(wxCommandEvent& event);
65     void OnButtonAddressBook(wxCommandEvent& event);
66     void OnSetFocusAddress(wxFocusEvent& event);
67     void OnMouseEventsAddress(wxMouseEvent& event);
68     void OnButtonNew(wxCommandEvent& event);
69     void OnButtonCopy(wxCommandEvent& event);
70     void OnListColBeginDrag(wxListEvent& event);
71     void OnListItemActivated(wxListEvent& event);
72     void OnListItemActivatedProductsSent(wxListEvent& event);
73     void OnListItemActivatedOrdersSent(wxListEvent& event);
74     void OnListItemActivatedOrdersReceived(wxListEvent& event);
75         
76 public:
77     /** Constructor */
78     CMainFrame(wxWindow* parent);
79     ~CMainFrame();
80
81     // Custom
82     enum
83     {
84         ALL = 0,
85         SENTRECEIVED = 1,
86         SENT = 2,
87         RECEIVED = 3,
88     };
89     int nPage;
90     wxListCtrl* m_listCtrl;
91     bool fShowGenerated;
92     bool fShowSent;
93     bool fShowReceived;
94     bool fRefreshListCtrl;
95     bool fRefreshListCtrlRunning;
96     bool fOnSetFocusAddress;
97     unsigned int nListViewUpdated;
98     bool fRefresh;
99
100     void OnUIThreadCall(wxCommandEvent& event);
101     int GetSortIndex(const std::string& strSort);
102     void InsertLine(bool fNew, int nIndex, uint256 hashKey, std::string strSort, const wxColour& colour, const wxString& str1, const wxString& str2, const wxString& str3, const wxString& str4, const wxString& str5);
103     bool DeleteLine(uint256 hashKey);
104     bool InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex=-1);
105     void RefreshListCtrl();
106     void RefreshStatusColumn();
107 };
108
109
110
111
112 class CTxDetailsDialog : public CTxDetailsDialogBase
113 {
114 protected:
115     // Event handlers
116     void OnButtonOK(wxCommandEvent& event);
117
118 public:
119     /** Constructor */
120     CTxDetailsDialog(wxWindow* parent, CWalletTx wtx);
121
122     // State
123     CWalletTx wtx;
124 };
125
126
127
128 class COptionsDialog : public COptionsDialogBase
129 {
130 protected:
131     // Event handlers
132     void OnListBox(wxCommandEvent& event);
133     void OnKillFocusTransactionFee(wxFocusEvent& event);
134     void OnCheckBoxUseProxy(wxCommandEvent& event);
135     void OnKillFocusProxy(wxFocusEvent& event);
136
137     void OnButtonOK(wxCommandEvent& event);
138     void OnButtonCancel(wxCommandEvent& event);
139     void OnButtonApply(wxCommandEvent& event);
140
141 public:
142     /** Constructor */
143     COptionsDialog(wxWindow* parent);
144
145     // Custom
146     bool fTmpStartOnSystemStartup;
147     bool fTmpMinimizeOnClose;
148     void SelectPage(int nPage);
149     CAddress GetProxyAddr();
150 };
151
152
153
154 class CAboutDialog : public CAboutDialogBase
155 {
156 protected:
157     // Event handlers
158     void OnButtonOK(wxCommandEvent& event);
159
160 public:
161     /** Constructor */
162     CAboutDialog(wxWindow* parent);
163 };
164
165
166
167 class CSendDialog : public CSendDialogBase
168 {
169 protected:
170     // Event handlers
171     void OnKeyDown(wxKeyEvent& event) { HandleCtrlA(event); }
172     void OnKillFocusAmount(wxFocusEvent& event);
173     void OnButtonAddressBook(wxCommandEvent& event);
174     void OnButtonPaste(wxCommandEvent& event);
175     void OnButtonSend(wxCommandEvent& event);
176     void OnButtonCancel(wxCommandEvent& event);
177         
178 public:
179     /** Constructor */
180     CSendDialog(wxWindow* parent, const wxString& strAddress="");
181
182     // Custom
183     bool fEnabledPrev;
184     std::string strFromSave;
185     std::string strMessageSave;
186 };
187
188
189
190 class CSendingDialog : public CSendingDialogBase
191 {
192 public:
193     // Event handlers
194     void OnClose(wxCloseEvent& event);
195     void OnButtonOK(wxCommandEvent& event);
196     void OnButtonCancel(wxCommandEvent& event);
197     void OnPaint(wxPaintEvent& event);
198         
199 public:
200     /** Constructor */
201     CSendingDialog(wxWindow* parent, const CAddress& addrIn, int64 nPriceIn, const CWalletTx& wtxIn);
202     ~CSendingDialog();
203
204     // State
205     CAddress addr;
206     int64 nPrice;
207     CWalletTx wtx;
208     wxDateTime start;
209     char pszStatus[10000];
210     bool fCanCancel;
211     bool fAbort;
212     bool fSuccess;
213     bool fUIDone;
214     bool fWorkDone;
215
216     void Close();
217     void Repaint();
218     bool Status();
219     bool Status(const std::string& str);
220     bool Error(const std::string& str);
221     void StartTransfer();
222     void OnReply2(CDataStream& vRecv);
223     void OnReply3(CDataStream& vRecv);
224 };
225
226 void SendingDialogStartTransfer(void* parg);
227 void SendingDialogOnReply2(void* parg, CDataStream& vRecv);
228 void SendingDialogOnReply3(void* parg, CDataStream& vRecv);
229
230
231
232 class CAddressBookDialog : public CAddressBookDialogBase
233 {
234 protected:
235     // Event handlers
236     void OnNotebookPageChanged(wxNotebookEvent& event);
237     void OnListEndLabelEdit(wxListEvent& event);
238     void OnListItemSelected(wxListEvent& event);
239     void OnListItemActivated(wxListEvent& event);
240     void OnButtonDelete(wxCommandEvent& event);
241     void OnButtonCopy(wxCommandEvent& event);
242     void OnButtonEdit(wxCommandEvent& event);
243     void OnButtonNew(wxCommandEvent& event);
244     void OnButtonOK(wxCommandEvent& event);
245     void OnButtonCancel(wxCommandEvent& event);
246     void OnClose(wxCloseEvent& event);
247
248 public:
249     /** Constructor */
250     CAddressBookDialog(wxWindow* parent, const wxString& strInitSelected, int nPageIn, bool fDuringSendIn);
251
252     // Custom
253     enum
254     {
255         SENDING = 0,
256         RECEIVING = 1,
257     };
258     int nPage;
259     wxListCtrl* m_listCtrl;
260     bool fDuringSend;
261     wxString GetAddress();
262     wxString GetSelectedAddress();
263     wxString GetSelectedSendingAddress();
264     wxString GetSelectedReceivingAddress();
265     bool CheckIfMine(const std::string& strAddress, const std::string& strTitle);
266 };
267
268
269
270 class CGetTextFromUserDialog : public CGetTextFromUserDialogBase
271 {
272 protected:
273     // Event handlers
274     void OnButtonOK(wxCommandEvent& event)     { EndModal(true); }
275     void OnButtonCancel(wxCommandEvent& event) { EndModal(false); }
276     void OnClose(wxCloseEvent& event)          { EndModal(false); }
277
278     void OnKeyDown(wxKeyEvent& event)
279     {
280         if (event.GetKeyCode() == '\r' || event.GetKeyCode() == WXK_NUMPAD_ENTER)
281             EndModal(true);
282         else
283             HandleCtrlA(event);
284     }
285
286 public:
287     /** Constructor */
288     CGetTextFromUserDialog(wxWindow* parent,
289                            const std::string& strCaption,
290                            const std::string& strMessage1,
291                            const std::string& strValue1="",
292                            const std::string& strMessage2="",
293                            const std::string& strValue2="") : CGetTextFromUserDialogBase(parent, wxID_ANY, strCaption)
294     {
295         int x = GetSize().GetWidth();
296         int y = GetSize().GetHeight();
297         m_staticTextMessage1->SetLabel(strMessage1);
298         m_textCtrl1->SetValue(strValue1);
299         y += wxString(strMessage1).Freq('\n') * 14;
300         if (!strMessage2.empty())
301         {
302             m_staticTextMessage2->Show(true);
303             m_staticTextMessage2->SetLabel(strMessage2);
304             m_textCtrl2->Show(true);
305             m_textCtrl2->SetValue(strValue2);
306             y += 46 + wxString(strMessage2).Freq('\n') * 14;
307         }
308 #ifndef __WXMSW__
309         x = x * 114 / 100;
310         y = y * 114 / 100;
311 #endif
312         SetSize(x, y);
313     }
314
315     // Custom
316     std::string GetValue()  { return (std::string)m_textCtrl1->GetValue(); }
317     std::string GetValue1() { return (std::string)m_textCtrl1->GetValue(); }
318     std::string GetValue2() { return (std::string)m_textCtrl2->GetValue(); }
319 };
320
321
322
323 class CMyTaskBarIcon : public wxTaskBarIcon
324 {
325 protected:
326     // Event handlers
327     void OnLeftButtonDClick(wxTaskBarIconEvent& event);
328     void OnMenuRestore(wxCommandEvent& event);
329     void OnMenuSend(wxCommandEvent& event);
330     void OnMenuOptions(wxCommandEvent& event);
331     void OnUpdateUIGenerate(wxUpdateUIEvent& event);
332     void OnMenuGenerate(wxCommandEvent& event);
333     void OnMenuExit(wxCommandEvent& event);
334
335 public:
336     CMyTaskBarIcon() : wxTaskBarIcon()
337     {
338         Show(true);
339     }
340
341     void Show(bool fShow=true);
342     void Hide();
343     void Restore();
344     void UpdateTooltip();
345     virtual wxMenu* CreatePopupMenu();
346
347 DECLARE_EVENT_TABLE()
348 };
349
350 #endif