Add wallet privkey encryption.
[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 OnMenuOptionsEncryptWallet(wxCommandEvent& event);
63     void OnMenuOptionsChangeWalletPassphrase(wxCommandEvent& event);
64     void OnMenuOptionsOptions(wxCommandEvent& event);
65     void OnMenuHelpAbout(wxCommandEvent& event);
66     void OnButtonSend(wxCommandEvent& event);
67     void OnButtonAddressBook(wxCommandEvent& event);
68     void OnSetFocusAddress(wxFocusEvent& event);
69     void OnMouseEventsAddress(wxMouseEvent& event);
70     void OnButtonNew(wxCommandEvent& event);
71     void OnButtonCopy(wxCommandEvent& event);
72     void OnListColBeginDrag(wxListEvent& event);
73     void OnListItemActivated(wxListEvent& event);
74     void OnListItemActivatedProductsSent(wxListEvent& event);
75     void OnListItemActivatedOrdersSent(wxListEvent& event);
76     void OnListItemActivatedOrdersReceived(wxListEvent& event);
77         
78 public:
79     /** Constructor */
80     CMainFrame(wxWindow* parent);
81     ~CMainFrame();
82
83     // Custom
84     enum
85     {
86         ALL = 0,
87         SENTRECEIVED = 1,
88         SENT = 2,
89         RECEIVED = 3,
90     };
91     int nPage;
92     wxListCtrl* m_listCtrl;
93     bool fShowGenerated;
94     bool fShowSent;
95     bool fShowReceived;
96     bool fRefreshListCtrl;
97     bool fRefreshListCtrlRunning;
98     bool fOnSetFocusAddress;
99     unsigned int nListViewUpdated;
100     bool fRefresh;
101
102     void OnUIThreadCall(wxCommandEvent& event);
103     int GetSortIndex(const std::string& strSort);
104     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);
105     bool DeleteLine(uint256 hashKey);
106     bool InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex=-1);
107     void RefreshListCtrl();
108     void RefreshStatusColumn();
109 };
110
111
112
113
114 class CTxDetailsDialog : public CTxDetailsDialogBase
115 {
116 protected:
117     // Event handlers
118     void OnButtonOK(wxCommandEvent& event);
119
120 public:
121     /** Constructor */
122     CTxDetailsDialog(wxWindow* parent, CWalletTx wtx);
123
124     // State
125     CWalletTx wtx;
126 };
127
128
129
130 class COptionsDialog : public COptionsDialogBase
131 {
132 protected:
133     // Event handlers
134     void OnListBox(wxCommandEvent& event);
135     void OnKillFocusTransactionFee(wxFocusEvent& event);
136     void OnCheckBoxUseProxy(wxCommandEvent& event);
137     void OnKillFocusProxy(wxFocusEvent& event);
138
139     void OnButtonOK(wxCommandEvent& event);
140     void OnButtonCancel(wxCommandEvent& event);
141     void OnButtonApply(wxCommandEvent& event);
142
143 public:
144     /** Constructor */
145     COptionsDialog(wxWindow* parent);
146
147     // Custom
148     bool fTmpStartOnSystemStartup;
149     bool fTmpMinimizeOnClose;
150     void SelectPage(int nPage);
151     CAddress GetProxyAddr();
152 };
153
154
155
156 class CAboutDialog : public CAboutDialogBase
157 {
158 protected:
159     // Event handlers
160     void OnButtonOK(wxCommandEvent& event);
161
162 public:
163     /** Constructor */
164     CAboutDialog(wxWindow* parent);
165 };
166
167
168
169 class CSendDialog : public CSendDialogBase
170 {
171 protected:
172     // Event handlers
173     void OnKeyDown(wxKeyEvent& event) { HandleCtrlA(event); }
174     void OnKillFocusAmount(wxFocusEvent& event);
175     void OnButtonAddressBook(wxCommandEvent& event);
176     void OnButtonPaste(wxCommandEvent& event);
177     void OnButtonSend(wxCommandEvent& event);
178     void OnButtonCancel(wxCommandEvent& event);
179         
180 public:
181     /** Constructor */
182     CSendDialog(wxWindow* parent, const wxString& strAddress="");
183
184     // Custom
185     bool fEnabledPrev;
186     std::string strFromSave;
187     std::string strMessageSave;
188 };
189
190
191
192 class CSendingDialog : public CSendingDialogBase
193 {
194 public:
195     // Event handlers
196     void OnClose(wxCloseEvent& event);
197     void OnButtonOK(wxCommandEvent& event);
198     void OnButtonCancel(wxCommandEvent& event);
199     void OnPaint(wxPaintEvent& event);
200         
201 public:
202     /** Constructor */
203     CSendingDialog(wxWindow* parent, const CAddress& addrIn, int64 nPriceIn, const CWalletTx& wtxIn);
204     ~CSendingDialog();
205
206     // State
207     CAddress addr;
208     int64 nPrice;
209     CWalletTx wtx;
210     wxDateTime start;
211     char pszStatus[10000];
212     bool fCanCancel;
213     bool fAbort;
214     bool fSuccess;
215     bool fUIDone;
216     bool fWorkDone;
217
218     void Close();
219     void Repaint();
220     bool Status();
221     bool Status(const std::string& str);
222     bool Error(const std::string& str);
223     void StartTransfer();
224     void OnReply2(CDataStream& vRecv);
225     void OnReply3(CDataStream& vRecv);
226 };
227
228 void SendingDialogStartTransfer(void* parg);
229 void SendingDialogOnReply2(void* parg, CDataStream& vRecv);
230 void SendingDialogOnReply3(void* parg, CDataStream& vRecv);
231
232
233
234 class CAddressBookDialog : public CAddressBookDialogBase
235 {
236 protected:
237     // Event handlers
238     void OnNotebookPageChanged(wxNotebookEvent& event);
239     void OnListEndLabelEdit(wxListEvent& event);
240     void OnListItemSelected(wxListEvent& event);
241     void OnListItemActivated(wxListEvent& event);
242     void OnButtonDelete(wxCommandEvent& event);
243     void OnButtonCopy(wxCommandEvent& event);
244     void OnButtonEdit(wxCommandEvent& event);
245     void OnButtonNew(wxCommandEvent& event);
246     void OnButtonOK(wxCommandEvent& event);
247     void OnButtonCancel(wxCommandEvent& event);
248     void OnClose(wxCloseEvent& event);
249
250 public:
251     /** Constructor */
252     CAddressBookDialog(wxWindow* parent, const wxString& strInitSelected, int nPageIn, bool fDuringSendIn);
253
254     // Custom
255     enum
256     {
257         SENDING = 0,
258         RECEIVING = 1,
259     };
260     int nPage;
261     wxListCtrl* m_listCtrl;
262     bool fDuringSend;
263     wxString GetAddress();
264     wxString GetSelectedAddress();
265     wxString GetSelectedSendingAddress();
266     wxString GetSelectedReceivingAddress();
267     bool CheckIfMine(const std::string& strAddress, const std::string& strTitle);
268 };
269
270
271
272 class CGetTextFromUserDialog : public CGetTextFromUserDialogBase
273 {
274 protected:
275     // Event handlers
276     void OnButtonOK(wxCommandEvent& event)     { EndModal(true); }
277     void OnButtonCancel(wxCommandEvent& event) { EndModal(false); }
278     void OnClose(wxCloseEvent& event)          { EndModal(false); }
279
280     void OnKeyDown(wxKeyEvent& event)
281     {
282         if (event.GetKeyCode() == '\r' || event.GetKeyCode() == WXK_NUMPAD_ENTER)
283             EndModal(true);
284         else
285             HandleCtrlA(event);
286     }
287
288 public:
289     /** Constructor */
290     CGetTextFromUserDialog(wxWindow* parent,
291                            const std::string& strCaption,
292                            const std::string& strMessage1,
293                            const std::string& strValue1="",
294                            const std::string& strMessage2="",
295                            const std::string& strValue2="") : CGetTextFromUserDialogBase(parent, wxID_ANY, strCaption)
296     {
297         int x = GetSize().GetWidth();
298         int y = GetSize().GetHeight();
299         m_staticTextMessage1->SetLabel(strMessage1);
300         m_textCtrl1->SetValue(strValue1);
301         y += wxString(strMessage1).Freq('\n') * 14;
302         if (!strMessage2.empty())
303         {
304             m_staticTextMessage2->Show(true);
305             m_staticTextMessage2->SetLabel(strMessage2);
306             m_textCtrl2->Show(true);
307             m_textCtrl2->SetValue(strValue2);
308             y += 46 + wxString(strMessage2).Freq('\n') * 14;
309         }
310 #ifndef __WXMSW__
311         x = x * 114 / 100;
312         y = y * 114 / 100;
313 #endif
314         SetSize(x, y);
315     }
316
317     // Custom
318     std::string GetValue()  { return (std::string)m_textCtrl1->GetValue(); }
319     std::string GetValue1() { return (std::string)m_textCtrl1->GetValue(); }
320     std::string GetValue2() { return (std::string)m_textCtrl2->GetValue(); }
321 };
322
323
324
325 class CMyTaskBarIcon : public wxTaskBarIcon
326 {
327 protected:
328     // Event handlers
329     void OnLeftButtonDClick(wxTaskBarIconEvent& event);
330     void OnMenuRestore(wxCommandEvent& event);
331     void OnMenuSend(wxCommandEvent& event);
332     void OnMenuOptions(wxCommandEvent& event);
333     void OnUpdateUIGenerate(wxUpdateUIEvent& event);
334     void OnMenuGenerate(wxCommandEvent& event);
335     void OnMenuExit(wxCommandEvent& event);
336
337 public:
338     CMyTaskBarIcon() : wxTaskBarIcon()
339     {
340         Show(true);
341     }
342
343     void Show(bool fShow=true);
344     void Hide();
345     void Restore();
346     void UpdateTooltip();
347     virtual wxMenu* CreatePopupMenu();
348
349 DECLARE_EVENT_TABLE()
350 };
351
352 #endif