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