From 84c3fb07b0b8199c7f85c5de280e7100bad0786f Mon Sep 17 00:00:00 2001 From: Jaromil Date: Sat, 23 Apr 2011 11:49:47 +0200 Subject: directory re-organization (keeps the old build system) there is no internal modification of any file in this commit files are moved into directories according to established standards in sourcecode distribution; these directories contain: src - Files that are used in constructing the executable binaries, but are not installed. doc - Files in HTML and text format that document usage, quirks of the implementation, and contributor checklists. locale - Files that contain human language translation of strings used in the program contrib - Files contributed from distributions or other third party implementing scripts and auxiliary programs --- src/ui.cpp | 2933 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 2933 insertions(+) create mode 100644 src/ui.cpp (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp new file mode 100644 index 0000000000..2277da6a3a --- /dev/null +++ b/src/ui.cpp @@ -0,0 +1,2933 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + +#include "headers.h" +#ifdef _MSC_VER +#include +#endif + + + +DEFINE_EVENT_TYPE(wxEVT_UITHREADCALL) + +CMainFrame* pframeMain = NULL; +CMyTaskBarIcon* ptaskbaricon = NULL; +bool fClosedToTray = false; +wxLocale g_locale; + + + + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// Util +// + +void HandleCtrlA(wxKeyEvent& event) +{ + // Ctrl-a select all + event.Skip(); + wxTextCtrl* textCtrl = (wxTextCtrl*)event.GetEventObject(); + if (event.GetModifiers() == wxMOD_CONTROL && event.GetKeyCode() == 'A') + textCtrl->SetSelection(-1, -1); +} + +bool Is24HourTime() +{ + //char pszHourFormat[256]; + //pszHourFormat[0] = '\0'; + //GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITIME, pszHourFormat, 256); + //return (pszHourFormat[0] != '0'); + return true; +} + +string DateStr(int64 nTime) +{ + // Can only be used safely here in the UI + return (string)wxDateTime((time_t)nTime).FormatDate(); +} + +string DateTimeStr(int64 nTime) +{ + // Can only be used safely here in the UI + wxDateTime datetime((time_t)nTime); + if (Is24HourTime()) + return (string)datetime.Format("%x %H:%M"); + else + return (string)datetime.Format("%x ") + itostr((datetime.GetHour() + 11) % 12 + 1) + (string)datetime.Format(":%M %p"); +} + +wxString GetItemText(wxListCtrl* listCtrl, int nIndex, int nColumn) +{ + // Helper to simplify access to listctrl + wxListItem item; + item.m_itemId = nIndex; + item.m_col = nColumn; + item.m_mask = wxLIST_MASK_TEXT; + if (!listCtrl->GetItem(item)) + return ""; + return item.GetText(); +} + +int InsertLine(wxListCtrl* listCtrl, const wxString& str0, const wxString& str1) +{ + int nIndex = listCtrl->InsertItem(listCtrl->GetItemCount(), str0); + listCtrl->SetItem(nIndex, 1, str1); + return nIndex; +} + +int InsertLine(wxListCtrl* listCtrl, const wxString& str0, const wxString& str1, const wxString& str2, const wxString& str3, const wxString& str4) +{ + int nIndex = listCtrl->InsertItem(listCtrl->GetItemCount(), str0); + listCtrl->SetItem(nIndex, 1, str1); + listCtrl->SetItem(nIndex, 2, str2); + listCtrl->SetItem(nIndex, 3, str3); + listCtrl->SetItem(nIndex, 4, str4); + return nIndex; +} + +int InsertLine(wxListCtrl* listCtrl, void* pdata, const wxString& str0, const wxString& str1, const wxString& str2, const wxString& str3, const wxString& str4) +{ + int nIndex = listCtrl->InsertItem(listCtrl->GetItemCount(), str0); + listCtrl->SetItemPtrData(nIndex, (wxUIntPtr)pdata); + listCtrl->SetItem(nIndex, 1, str1); + listCtrl->SetItem(nIndex, 2, str2); + listCtrl->SetItem(nIndex, 3, str3); + listCtrl->SetItem(nIndex, 4, str4); + return nIndex; +} + +void SetItemTextColour(wxListCtrl* listCtrl, int nIndex, const wxColour& colour) +{ + // Repaint on Windows is more flickery if the colour has ever been set, + // so don't want to set it unless it's different. Default colour has + // alpha 0 transparent, so our colours don't match using operator==. + wxColour c1 = listCtrl->GetItemTextColour(nIndex); + if (!c1.IsOk()) + c1 = wxColour(0,0,0); + if (colour.Red() != c1.Red() || colour.Green() != c1.Green() || colour.Blue() != c1.Blue()) + listCtrl->SetItemTextColour(nIndex, colour); +} + +void SetSelection(wxListCtrl* listCtrl, int nIndex) +{ + int nSize = listCtrl->GetItemCount(); + long nState = (wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED); + for (int i = 0; i < nSize; i++) + listCtrl->SetItemState(i, (i == nIndex ? nState : 0), nState); +} + +int GetSelection(wxListCtrl* listCtrl) +{ + int nSize = listCtrl->GetItemCount(); + for (int i = 0; i < nSize; i++) + if (listCtrl->GetItemState(i, wxLIST_STATE_FOCUSED)) + return i; + return -1; +} + +string HtmlEscape(const char* psz, bool fMultiLine=false) +{ + int len = 0; + for (const char* p = psz; *p; p++) + { + if (*p == '<') len += 4; + else if (*p == '>') len += 4; + else if (*p == '&') len += 5; + else if (*p == '"') len += 6; + else if (*p == ' ' && p > psz && p[-1] == ' ' && p[1] == ' ') len += 6; + else if (*p == '\n' && fMultiLine) len += 5; + else + len++; + } + string str; + str.reserve(len); + for (const char* p = psz; *p; p++) + { + if (*p == '<') str += "<"; + else if (*p == '>') str += ">"; + else if (*p == '&') str += "&"; + else if (*p == '"') str += """; + else if (*p == ' ' && p > psz && p[-1] == ' ' && p[1] == ' ') str += " "; + else if (*p == '\n' && fMultiLine) str += "
\n"; + else + str += *p; + } + return str; +} + +string HtmlEscape(const string& str, bool fMultiLine=false) +{ + return HtmlEscape(str.c_str(), fMultiLine); +} + +void CalledMessageBox(const string& message, const string& caption, int style, wxWindow* parent, int x, int y, int* pnRet, bool* pfDone) +{ + *pnRet = wxMessageBox(message, caption, style, parent, x, y); + *pfDone = true; +} + +int ThreadSafeMessageBox(const string& message, const string& caption, int style, wxWindow* parent, int x, int y) +{ +#ifdef __WXMSW__ + return wxMessageBox(message, caption, style, parent, x, y); +#else + if (wxThread::IsMain() || fDaemon) + { + return wxMessageBox(message, caption, style, parent, x, y); + } + else + { + int nRet = 0; + bool fDone = false; + UIThreadCall(bind(CalledMessageBox, message, caption, style, parent, x, y, &nRet, &fDone)); + while (!fDone) + Sleep(100); + return nRet; + } +#endif +} + +bool ThreadSafeAskFee(int64 nFeeRequired, const string& strCaption, wxWindow* parent) +{ + if (nFeeRequired < CENT || nFeeRequired <= nTransactionFee || fDaemon) + return true; + string strMessage = strprintf( + _("This transaction is over the size limit. You can still send it for a fee of %s, " + "which goes to the nodes that process your transaction and helps to support the network. " + "Do you want to pay the fee?"), + FormatMoney(nFeeRequired).c_str()); + return (ThreadSafeMessageBox(strMessage, strCaption, wxYES_NO, parent) == wxYES); +} + +void CalledSetStatusBar(const string& strText, int nField) +{ + if (nField == 0 && GetWarnings("statusbar") != "") + return; + if (pframeMain && pframeMain->m_statusBar) + pframeMain->m_statusBar->SetStatusText(strText, nField); +} + +void SetDefaultReceivingAddress(const string& strAddress) +{ + // Update main window address and database + if (pframeMain == NULL) + return; + if (strAddress != pframeMain->m_textCtrlAddress->GetValue()) + { + uint160 hash160; + if (!AddressToHash160(strAddress, hash160)) + return; + if (!mapPubKeys.count(hash160)) + return; + CWalletDB().WriteDefaultKey(mapPubKeys[hash160]); + pframeMain->m_textCtrlAddress->SetValue(strAddress); + } +} + + + + + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// CMainFrame +// + +CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent) +{ + Connect(wxEVT_UITHREADCALL, wxCommandEventHandler(CMainFrame::OnUIThreadCall), NULL, this); + + // Set initially selected page + wxNotebookEvent event; + event.SetSelection(0); + OnNotebookPageChanged(event); + m_notebook->ChangeSelection(0); + + // Init + fRefreshListCtrl = false; + fRefreshListCtrlRunning = false; + fOnSetFocusAddress = false; + fRefresh = false; + m_choiceFilter->SetSelection(0); + double dResize = 1.0; +#ifdef __WXMSW__ + SetIcon(wxICON(bitcoin)); +#else + SetIcon(bitcoin80_xpm); + SetBackgroundColour(m_toolBar->GetBackgroundColour()); + wxFont fontTmp = m_staticText41->GetFont(); + fontTmp.SetFamily(wxFONTFAMILY_TELETYPE); + m_staticTextBalance->SetFont(fontTmp); + m_staticTextBalance->SetSize(140, 17); + // resize to fit ubuntu's huge default font + dResize = 1.22; + SetSize(dResize * GetSize().GetWidth(), 1.15 * GetSize().GetHeight()); +#endif + m_staticTextBalance->SetLabel(FormatMoney(GetBalance()) + " "); + m_listCtrl->SetFocus(); + ptaskbaricon = new CMyTaskBarIcon(); +#ifdef __WXMAC_OSX__ + // Mac automatically moves wxID_EXIT, wxID_PREFERENCES and wxID_ABOUT + // to their standard places, leaving these menus empty. + GetMenuBar()->Remove(2); // remove Help menu + GetMenuBar()->Remove(0); // remove File menu +#endif + + // Init column headers + int nDateWidth = DateTimeStr(1229413914).size() * 6 + 8; + if (!strstr(DateTimeStr(1229413914).c_str(), "2008")) + nDateWidth += 12; +#ifdef __WXMAC_OSX__ + nDateWidth += 5; + dResize -= 0.01; +#endif + wxListCtrl* pplistCtrl[] = {m_listCtrlAll, m_listCtrlSentReceived, m_listCtrlSent, m_listCtrlReceived}; + foreach(wxListCtrl* p, pplistCtrl) + { + p->InsertColumn(0, "", wxLIST_FORMAT_LEFT, dResize * 0); + p->InsertColumn(1, "", wxLIST_FORMAT_LEFT, dResize * 0); + p->InsertColumn(2, _("Status"), wxLIST_FORMAT_LEFT, dResize * 112); + p->InsertColumn(3, _("Date"), wxLIST_FORMAT_LEFT, dResize * nDateWidth); + p->InsertColumn(4, _("Description"), wxLIST_FORMAT_LEFT, dResize * 409 - nDateWidth); + p->InsertColumn(5, _("Debit"), wxLIST_FORMAT_RIGHT, dResize * 79); + p->InsertColumn(6, _("Credit"), wxLIST_FORMAT_RIGHT, dResize * 79); + } + + // Init status bar + int pnWidths[3] = { -100, 88, 300 }; +#ifndef __WXMSW__ + pnWidths[1] = pnWidths[1] * 1.1 * dResize; + pnWidths[2] = pnWidths[2] * 1.1 * dResize; +#endif + m_statusBar->SetFieldsCount(3, pnWidths); + + // Fill your address text box + vector vchPubKey; + if (CWalletDB("r").ReadDefaultKey(vchPubKey)) + m_textCtrlAddress->SetValue(PubKeyToAddress(vchPubKey)); + + // Fill listctrl with wallet transactions + RefreshListCtrl(); +} + +CMainFrame::~CMainFrame() +{ + pframeMain = NULL; + delete ptaskbaricon; + ptaskbaricon = NULL; +} + +void CMainFrame::OnNotebookPageChanged(wxNotebookEvent& event) +{ + event.Skip(); + nPage = event.GetSelection(); + if (nPage == ALL) + { + m_listCtrl = m_listCtrlAll; + fShowGenerated = true; + fShowSent = true; + fShowReceived = true; + } + else if (nPage == SENTRECEIVED) + { + m_listCtrl = m_listCtrlSentReceived; + fShowGenerated = false; + fShowSent = true; + fShowReceived = true; + } + else if (nPage == SENT) + { + m_listCtrl = m_listCtrlSent; + fShowGenerated = false; + fShowSent = true; + fShowReceived = false; + } + else if (nPage == RECEIVED) + { + m_listCtrl = m_listCtrlReceived; + fShowGenerated = false; + fShowSent = false; + fShowReceived = true; + } + RefreshListCtrl(); + m_listCtrl->SetFocus(); +} + +void CMainFrame::OnClose(wxCloseEvent& event) +{ + if (fMinimizeOnClose && event.CanVeto() && !IsIconized()) + { + // Divert close to minimize + event.Veto(); + fClosedToTray = true; + Iconize(true); + } + else + { + Destroy(); + CreateThread(Shutdown, NULL); + } +} + +void CMainFrame::OnIconize(wxIconizeEvent& event) +{ + event.Skip(); + // Hide the task bar button when minimized. + // Event is sent when the frame is minimized or restored. + // wxWidgets 2.8.9 doesn't have IsIconized() so there's no way + // to get rid of the deprecated warning. Just ignore it. + if (!event.Iconized()) + fClosedToTray = false; +#if defined(__WXGTK__) || defined(__WXMAC_OSX__) + if (GetBoolArg("-minimizetotray")) { +#endif + // The tray icon sometimes disappears on ubuntu karmic + // Hiding the taskbar button doesn't work cleanly on ubuntu lucid + // Reports of CPU peg on 64-bit linux + if (fMinimizeToTray && event.Iconized()) + fClosedToTray = true; + Show(!fClosedToTray); + ptaskbaricon->Show(fMinimizeToTray || fClosedToTray); +#if defined(__WXGTK__) || defined(__WXMAC_OSX__) + } +#endif +} + +void CMainFrame::OnMouseEvents(wxMouseEvent& event) +{ + event.Skip(); + RandAddSeed(); + RAND_add(&event.m_x, sizeof(event.m_x), 0.25); + RAND_add(&event.m_y, sizeof(event.m_y), 0.25); +} + +void CMainFrame::OnListColBeginDrag(wxListEvent& event) +{ + // Hidden columns not resizeable + if (event.GetColumn() <= 1 && !fDebug) + event.Veto(); + else + event.Skip(); +} + +int CMainFrame::GetSortIndex(const string& strSort) +{ +#ifdef __WXMSW__ + return 0; +#else + // The wx generic listctrl implementation used on GTK doesn't sort, + // so we have to do it ourselves. Remember, we sort in reverse order. + // In the wx generic implementation, they store the list of items + // in a vector, so indexed lookups are fast, but inserts are slower + // the closer they are to the top. + int low = 0; + int high = m_listCtrl->GetItemCount(); + while (low < high) + { + int mid = low + ((high - low) / 2); + if (strSort.compare(m_listCtrl->GetItemText(mid).c_str()) >= 0) + high = mid; + else + low = mid + 1; + } + return low; +#endif +} + +void CMainFrame::InsertLine(bool fNew, int nIndex, uint256 hashKey, string strSort, const wxColour& colour, const wxString& str2, const wxString& str3, const wxString& str4, const wxString& str5, const wxString& str6) +{ + strSort = " " + strSort; // leading space to workaround wx2.9.0 ubuntu 9.10 bug + long nData = *(long*)&hashKey; // where first char of hidden column is displayed + + // Find item + if (!fNew && nIndex == -1) + { + string strHash = " " + hashKey.ToString(); + while ((nIndex = m_listCtrl->FindItem(nIndex, nData)) != -1) + if (GetItemText(m_listCtrl, nIndex, 1) == strHash) + break; + } + + // fNew is for blind insert, only use if you're sure it's new + if (fNew || nIndex == -1) + { + nIndex = m_listCtrl->InsertItem(GetSortIndex(strSort), strSort); + } + else + { + // If sort key changed, must delete and reinsert to make it relocate + if (GetItemText(m_listCtrl, nIndex, 0) != strSort) + { + m_listCtrl->DeleteItem(nIndex); + nIndex = m_listCtrl->InsertItem(GetSortIndex(strSort), strSort); + } + } + + m_listCtrl->SetItem(nIndex, 1, " " + hashKey.ToString()); + m_listCtrl->SetItem(nIndex, 2, str2); + m_listCtrl->SetItem(nIndex, 3, str3); + m_listCtrl->SetItem(nIndex, 4, str4); + m_listCtrl->SetItem(nIndex, 5, str5); + m_listCtrl->SetItem(nIndex, 6, str6); + m_listCtrl->SetItemData(nIndex, nData); + SetItemTextColour(m_listCtrl, nIndex, colour); +} + +bool CMainFrame::DeleteLine(uint256 hashKey) +{ + long nData = *(long*)&hashKey; + + // Find item + int nIndex = -1; + string strHash = " " + hashKey.ToString(); + while ((nIndex = m_listCtrl->FindItem(nIndex, nData)) != -1) + if (GetItemText(m_listCtrl, nIndex, 1) == strHash) + break; + + if (nIndex != -1) + m_listCtrl->DeleteItem(nIndex); + + return nIndex != -1; +} + +string FormatTxStatus(const CWalletTx& wtx) +{ + // Status + if (!wtx.IsFinal()) + { + if (wtx.nLockTime < 500000000) + return strprintf(_("Open for %d blocks"), nBestHeight - wtx.nLockTime); + else + return strprintf(_("Open until %s"), DateTimeStr(wtx.nLockTime).c_str()); + } + else + { + int nDepth = wtx.GetDepthInMainChain(); + if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0) + return strprintf(_("%d/offline?"), nDepth); + else if (nDepth < 6) + return strprintf(_("%d/unconfirmed"), nDepth); + else + return strprintf(_("%d confirmations"), nDepth); + } +} + +string SingleLine(const string& strIn) +{ + string strOut; + bool fOneSpace = false; + foreach(unsigned char c, strIn) + { + if (isspace(c)) + { + fOneSpace = true; + } + else if (c > ' ') + { + if (fOneSpace && !strOut.empty()) + strOut += ' '; + strOut += c; + fOneSpace = false; + } + } + return strOut; +} + +bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) +{ + int64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime(); + int64 nCredit = wtx.GetCredit(true); + int64 nDebit = wtx.GetDebit(); + int64 nNet = nCredit - nDebit; + uint256 hash = wtx.GetHash(); + string strStatus = FormatTxStatus(wtx); + bool fConfirmed = wtx.fConfirmedDisplayed = wtx.IsConfirmed(); + wxColour colour = (fConfirmed ? wxColour(0,0,0) : wxColour(128,128,128)); + map mapValue = wtx.mapValue; + wtx.nLinesDisplayed = 1; + nListViewUpdated++; + + // Filter + if (wtx.IsCoinBase()) + { + // Don't show generated coin until confirmed by at least one block after it + // so we don't get the user's hopes up until it looks like it's probably accepted. + // + // It is not an error when generated blocks are not accepted. By design, + // some percentage of blocks, like 10% or more, will end up not accepted. + // This is the normal mechanism by which the network copes with latency. + // + // We display regular transactions right away before any confirmation + // because they can always get into some block eventually. Generated coins + // are special because if their block is not accepted, they are not valid. + // + if (wtx.GetDepthInMainChain() < 2) + { + wtx.nLinesDisplayed = 0; + return false; + } + + if (!fShowGenerated) + return false; + } + + // Find the block the tx is in + CBlockIndex* pindex = NULL; + map::iterator mi = mapBlockIndex.find(wtx.hashBlock); + if (mi != mapBlockIndex.end()) + pindex = (*mi).second; + + // Sort order, unrecorded transactions sort to the top + string strSort = strprintf("%010d-%01d-%010u", + (pindex ? pindex->nHeight : INT_MAX), + (wtx.IsCoinBase() ? 1 : 0), + wtx.nTimeReceived); + + // Insert line + if (nNet > 0 || wtx.IsCoinBase()) + { + // + // Credit + // + string strDescription; + if (wtx.IsCoinBase()) + { + // Generated + strDescription = _("Generated"); + if (nCredit == 0) + { + int64 nUnmatured = 0; + foreach(const CTxOut& txout, wtx.vout) + nUnmatured += txout.GetCredit(); + if (wtx.IsInMainChain()) + { + strDescription = strprintf(_("Generated (%s matures in %d more blocks)"), FormatMoney(nUnmatured).c_str(), wtx.GetBlocksToMaturity()); + + // Check if the block was requested by anyone + if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0) + strDescription = _("Generated - Warning: This block was not received by any other nodes and will probably not be accepted!"); + } + else + { + strDescription = _("Generated (not accepted)"); + } + } + } + else if (!mapValue["from"].empty() || !mapValue["message"].empty()) + { + // Received by IP connection + if (!fShowReceived) + return false; + if (!mapValue["from"].empty()) + strDescription += _("From: ") + mapValue["from"]; + if (!mapValue["message"].empty()) + { + if (!strDescription.empty()) + strDescription += " - "; + strDescription += mapValue["message"]; + } + } + else + { + // Received by Bitcoin Address + if (!fShowReceived) + return false; + foreach(const CTxOut& txout, wtx.vout) + { + if (txout.IsMine()) + { + vector vchPubKey; + if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey)) + { + CRITICAL_BLOCK(cs_mapAddressBook) + { + //strDescription += _("Received payment to "); + //strDescription += _("Received with address "); + strDescription += _("Received with: "); + string strAddress = PubKeyToAddress(vchPubKey); + map::iterator mi = mapAddressBook.find(strAddress); + if (mi != mapAddressBook.end() && !(*mi).second.empty()) + { + string strLabel = (*mi).second; + strDescription += strAddress.substr(0,12) + "... "; + strDescription += "(" + strLabel + ")"; + } + else + strDescription += strAddress; + } + } + break; + } + } + } + + string strCredit = FormatMoney(nNet, true); + if (!fConfirmed) + strCredit = "[" + strCredit + "]"; + + InsertLine(fNew, nIndex, hash, strSort, colour, + strStatus, + nTime ? DateTimeStr(nTime) : "", + SingleLine(strDescription), + "", + strCredit); + } + else + { + bool fAllFromMe = true; + foreach(const CTxIn& txin, wtx.vin) + fAllFromMe = fAllFromMe && txin.IsMine(); + + bool fAllToMe = true; + foreach(const CTxOut& txout, wtx.vout) + fAllToMe = fAllToMe && txout.IsMine(); + + if (fAllFromMe && fAllToMe) + { + // Payment to self + int64 nChange = wtx.GetChange(); + InsertLine(fNew, nIndex, hash, strSort, colour, + strStatus, + nTime ? DateTimeStr(nTime) : "", + _("Payment to yourself"), + FormatMoney(-(nDebit - nChange), true), + FormatMoney(nCredit - nChange, true)); + } + else if (fAllFromMe) + { + // + // Debit + // + if (!fShowSent) + return false; + + int64 nTxFee = nDebit - wtx.GetValueOut(); + wtx.nLinesDisplayed = 0; + for (int nOut = 0; nOut < wtx.vout.size(); nOut++) + { + const CTxOut& txout = wtx.vout[nOut]; + if (txout.IsMine()) + continue; + + string strAddress; + if (!mapValue["to"].empty()) + { + // Sent to IP + strAddress = mapValue["to"]; + } + else + { + // Sent to Bitcoin Address + uint160 hash160; + if (ExtractHash160(txout.scriptPubKey, hash160)) + strAddress = Hash160ToAddress(hash160); + } + + string strDescription = _("To: "); + CRITICAL_BLOCK(cs_mapAddressBook) + if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty()) + strDescription += mapAddressBook[strAddress] + " "; + strDescription += strAddress; + if (!mapValue["message"].empty()) + { + if (!strDescription.empty()) + strDescription += " - "; + strDescription += mapValue["message"]; + } + else if (!mapValue["comment"].empty()) + { + if (!strDescription.empty()) + strDescription += " - "; + strDescription += mapValue["comment"]; + } + + int64 nValue = txout.nValue; + if (nTxFee > 0) + { + nValue += nTxFee; + nTxFee = 0; + } + + InsertLine(fNew, nIndex, hash, strprintf("%s-%d", strSort.c_str(), nOut), colour, + strStatus, + nTime ? DateTimeStr(nTime) : "", + SingleLine(strDescription), + FormatMoney(-nValue, true), + ""); + nIndex = -1; + wtx.nLinesDisplayed++; + } + } + else + { + // + // Mixed debit transaction, can't break down payees + // + bool fAllMine = true; + foreach(const CTxOut& txout, wtx.vout) + fAllMine = fAllMine && txout.IsMine(); + foreach(const CTxIn& txin, wtx.vin) + fAllMine = fAllMine && txin.IsMine(); + + InsertLine(fNew, nIndex, hash, strSort, colour, + strStatus, + nTime ? DateTimeStr(nTime) : "", + "", + FormatMoney(nNet, true), + ""); + } + } + + return true; +} + +void CMainFrame::RefreshListCtrl() +{ + fRefreshListCtrl = true; + ::wxWakeUpIdle(); +} + +void CMainFrame::OnIdle(wxIdleEvent& event) +{ + if (fRefreshListCtrl) + { + // Collect list of wallet transactions and sort newest first + bool fEntered = false; + vector > vSorted; + TRY_CRITICAL_BLOCK(cs_mapWallet) + { + printf("RefreshListCtrl starting\n"); + fEntered = true; + fRefreshListCtrl = false; + vWalletUpdated.clear(); + + // Do the newest transactions first + vSorted.reserve(mapWallet.size()); + for (map::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) + { + const CWalletTx& wtx = (*it).second; + unsigned int nTime = UINT_MAX - wtx.GetTxTime(); + vSorted.push_back(make_pair(nTime, (*it).first)); + } + m_listCtrl->DeleteAllItems(); + } + if (!fEntered) + return; + + sort(vSorted.begin(), vSorted.end()); + + // Fill list control + for (int i = 0; i < vSorted.size();) + { + if (fShutdown) + return; + bool fEntered = false; + TRY_CRITICAL_BLOCK(cs_mapWallet) + { + fEntered = true; + uint256& hash = vSorted[i++].second; + map::iterator mi = mapWallet.find(hash); + if (mi != mapWallet.end()) + InsertTransaction((*mi).second, true); + } + if (!fEntered || i == 100 || i % 500 == 0) + wxYield(); + } + + printf("RefreshListCtrl done\n"); + + // Update transaction total display + MainFrameRepaint(); + } + else + { + // Check for time updates + static int64 nLastTime; + if (GetTime() > nLastTime + 30) + { + TRY_CRITICAL_BLOCK(cs_mapWallet) + { + nLastTime = GetTime(); + for (map::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) + { + CWalletTx& wtx = (*it).second; + if (wtx.nTimeDisplayed && wtx.nTimeDisplayed != wtx.GetTxTime()) + InsertTransaction(wtx, false); + } + } + } + } +} + +void CMainFrame::RefreshStatusColumn() +{ + static int nLastTop; + static CBlockIndex* pindexLastBest; + static unsigned int nLastRefreshed; + + int nTop = max((int)m_listCtrl->GetTopItem(), 0); + if (nTop == nLastTop && pindexLastBest == pindexBest) + return; + + TRY_CRITICAL_BLOCK(cs_mapWallet) + { + int nStart = nTop; + int nEnd = min(nStart + 100, m_listCtrl->GetItemCount()); + + if (pindexLastBest == pindexBest && nLastRefreshed == nListViewUpdated) + { + // If no updates, only need to do the part that moved onto the screen + if (nStart >= nLastTop && nStart < nLastTop + 100) + nStart = nLastTop + 100; + if (nEnd >= nLastTop && nEnd < nLastTop + 100) + nEnd = nLastTop; + } + nLastTop = nTop; + pindexLastBest = pindexBest; + nLastRefreshed = nListViewUpdated; + + for (int nIndex = nStart; nIndex < min(nEnd, m_listCtrl->GetItemCount()); nIndex++) + { + uint256 hash((string)GetItemText(m_listCtrl, nIndex, 1)); + map::iterator mi = mapWallet.find(hash); + if (mi == mapWallet.end()) + { + printf("CMainFrame::RefreshStatusColumn() : tx not found in mapWallet\n"); + continue; + } + CWalletTx& wtx = (*mi).second; + if (wtx.IsCoinBase() || + wtx.GetTxTime() != wtx.nTimeDisplayed || + wtx.IsConfirmed() != wtx.fConfirmedDisplayed) + { + if (!InsertTransaction(wtx, false, nIndex)) + m_listCtrl->DeleteItem(nIndex--); + } + else + { + m_listCtrl->SetItem(nIndex, 2, FormatTxStatus(wtx)); + } + } + } +} + +void CMainFrame::OnPaint(wxPaintEvent& event) +{ + event.Skip(); + if (fRefresh) + { + fRefresh = false; + Refresh(); + } +} + + +unsigned int nNeedRepaint = 0; +unsigned int nLastRepaint = 0; +int64 nLastRepaintTime = 0; +int64 nRepaintInterval = 500; + +void ThreadDelayedRepaint(void* parg) +{ + while (!fShutdown) + { + if (nLastRepaint != nNeedRepaint && GetTimeMillis() - nLastRepaintTime >= nRepaintInterval) + { + nLastRepaint = nNeedRepaint; + if (pframeMain) + { + printf("DelayedRepaint\n"); + wxPaintEvent event; + pframeMain->fRefresh = true; + pframeMain->GetEventHandler()->AddPendingEvent(event); + } + } + Sleep(nRepaintInterval); + } +} + +void MainFrameRepaint() +{ + // This is called by network code that shouldn't access pframeMain + // directly because it could still be running after the UI is closed. + if (pframeMain) + { + // Don't repaint too often + static int64 nLastRepaintRequest; + if (GetTimeMillis() - nLastRepaintRequest < 100) + { + nNeedRepaint++; + return; + } + nLastRepaintRequest = GetTimeMillis(); + + printf("MainFrameRepaint\n"); + wxPaintEvent event; + pframeMain->fRefresh = true; + pframeMain->GetEventHandler()->AddPendingEvent(event); + } +} + +void CMainFrame::OnPaintListCtrl(wxPaintEvent& event) +{ + // Skip lets the listctrl do the paint, we're just hooking the message + event.Skip(); + + if (ptaskbaricon) + ptaskbaricon->UpdateTooltip(); + + // + // Slower stuff + // + static int nTransactionCount; + bool fPaintedBalance = false; + if (GetTimeMillis() - nLastRepaintTime >= nRepaintInterval) + { + nLastRepaint = nNeedRepaint; + nLastRepaintTime = GetTimeMillis(); + + // Update listctrl contents + if (!vWalletUpdated.empty()) + { + TRY_CRITICAL_BLOCK(cs_mapWallet) + { + string strTop; + if (m_listCtrl->GetItemCount()) + strTop = (string)m_listCtrl->GetItemText(0); + foreach(uint256 hash, vWalletUpdated) + { + map::iterator mi = mapWallet.find(hash); + if (mi != mapWallet.end()) + InsertTransaction((*mi).second, false); + } + vWalletUpdated.clear(); + if (m_listCtrl->GetItemCount() && strTop != (string)m_listCtrl->GetItemText(0)) + m_listCtrl->ScrollList(0, INT_MIN/2); + } + } + + // Balance total + TRY_CRITICAL_BLOCK(cs_mapWallet) + { + fPaintedBalance = true; + m_staticTextBalance->SetLabel(FormatMoney(GetBalance()) + " "); + + // Count hidden and multi-line transactions + nTransactionCount = 0; + for (map::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) + { + CWalletTx& wtx = (*it).second; + nTransactionCount += wtx.nLinesDisplayed; + } + } + } + if (!vWalletUpdated.empty() || !fPaintedBalance) + nNeedRepaint++; + + // Update status column of visible items only + RefreshStatusColumn(); + + // Update status bar + static string strPrevWarning; + string strWarning = GetWarnings("statusbar"); + if (strWarning != "") + m_statusBar->SetStatusText(string(" ") + _(strWarning), 0); + else if (strPrevWarning != "") + m_statusBar->SetStatusText("", 0); + strPrevWarning = strWarning; + + string strGen = ""; + if (fGenerateBitcoins) + strGen = _(" Generating"); + if (fGenerateBitcoins && vNodes.empty()) + strGen = _("(not connected)"); + m_statusBar->SetStatusText(strGen, 1); + + string strStatus = strprintf(_(" %d connections %d blocks %d transactions"), vNodes.size(), nBestHeight, nTransactionCount); + m_statusBar->SetStatusText(strStatus, 2); + + // Update receiving address + string strDefaultAddress = PubKeyToAddress(vchDefaultKey); + if (m_textCtrlAddress->GetValue() != strDefaultAddress) + m_textCtrlAddress->SetValue(strDefaultAddress); +} + + +void UIThreadCall(boost::function0 fn) +{ + // Call this with a function object created with bind. + // bind needs all parameters to match the function's expected types + // and all default parameters specified. Some examples: + // UIThreadCall(bind(wxBell)); + // UIThreadCall(bind(wxMessageBox, wxT("Message"), wxT("Title"), wxOK, (wxWindow*)NULL, -1, -1)); + // UIThreadCall(bind(&CMainFrame::OnMenuHelpAbout, pframeMain, event)); + if (pframeMain) + { + wxCommandEvent event(wxEVT_UITHREADCALL); + event.SetClientData((void*)new boost::function0(fn)); + pframeMain->GetEventHandler()->AddPendingEvent(event); + } +} + +void CMainFrame::OnUIThreadCall(wxCommandEvent& event) +{ + boost::function0* pfn = (boost::function0*)event.GetClientData(); + (*pfn)(); + delete pfn; +} + +void CMainFrame::OnMenuFileExit(wxCommandEvent& event) +{ + // File->Exit + Close(true); +} + +void CMainFrame::OnMenuOptionsGenerate(wxCommandEvent& event) +{ + // Options->Generate Coins + GenerateBitcoins(event.IsChecked()); +} + +void CMainFrame::OnUpdateUIOptionsGenerate(wxUpdateUIEvent& event) +{ + event.Check(fGenerateBitcoins); +} + +void CMainFrame::OnMenuOptionsChangeYourAddress(wxCommandEvent& event) +{ + // Options->Your Receiving Addresses + CAddressBookDialog dialog(this, "", CAddressBookDialog::RECEIVING, false); + if (!dialog.ShowModal()) + return; +} + +void CMainFrame::OnMenuOptionsOptions(wxCommandEvent& event) +{ + // Options->Options + COptionsDialog dialog(this); + dialog.ShowModal(); +} + +void CMainFrame::OnMenuHelpAbout(wxCommandEvent& event) +{ + // Help->About + CAboutDialog dialog(this); + dialog.ShowModal(); +} + +void CMainFrame::OnButtonSend(wxCommandEvent& event) +{ + // Toolbar: Send + CSendDialog dialog(this); + dialog.ShowModal(); +} + +void CMainFrame::OnButtonAddressBook(wxCommandEvent& event) +{ + // Toolbar: Address Book + CAddressBookDialog dialogAddr(this, "", CAddressBookDialog::SENDING, false); + if (dialogAddr.ShowModal() == 2) + { + // Send + CSendDialog dialogSend(this, dialogAddr.GetSelectedAddress()); + dialogSend.ShowModal(); + } +} + +void CMainFrame::OnSetFocusAddress(wxFocusEvent& event) +{ + // Automatically select-all when entering window + event.Skip(); + m_textCtrlAddress->SetSelection(-1, -1); + fOnSetFocusAddress = true; +} + +void CMainFrame::OnMouseEventsAddress(wxMouseEvent& event) +{ + event.Skip(); + if (fOnSetFocusAddress) + m_textCtrlAddress->SetSelection(-1, -1); + fOnSetFocusAddress = false; +} + +void CMainFrame::OnButtonNew(wxCommandEvent& event) +{ + // Ask name + CGetTextFromUserDialog dialog(this, + _("New Receiving Address"), + _("You should use a new address for each payment you receive.\n\nLabel"), + ""); + if (!dialog.ShowModal()) + return; + string strName = dialog.GetValue(); + + // Generate new key + string strAddress = PubKeyToAddress(GetKeyFromKeyPool()); + + // Save + SetAddressBookName(strAddress, strName); + SetDefaultReceivingAddress(strAddress); +} + +void CMainFrame::OnButtonCopy(wxCommandEvent& event) +{ + // Copy address box to clipboard + if (wxTheClipboard->Open()) + { + wxTheClipboard->SetData(new wxTextDataObject(m_textCtrlAddress->GetValue())); + wxTheClipboard->Close(); + } +} + +void CMainFrame::OnListItemActivated(wxListEvent& event) +{ + uint256 hash((string)GetItemText(m_listCtrl, event.GetIndex(), 1)); + CWalletTx wtx; + CRITICAL_BLOCK(cs_mapWallet) + { + map::iterator mi = mapWallet.find(hash); + if (mi == mapWallet.end()) + { + printf("CMainFrame::OnListItemActivated() : tx not found in mapWallet\n"); + return; + } + wtx = (*mi).second; + } + CTxDetailsDialog dialog(this, wtx); + dialog.ShowModal(); + //CTxDetailsDialog* pdialog = new CTxDetailsDialog(this, wtx); + //pdialog->Show(); +} + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// CTxDetailsDialog +// + +CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetailsDialogBase(parent) +{ + CRITICAL_BLOCK(cs_mapAddressBook) + { + string strHTML; + strHTML.reserve(4000); + strHTML += ""; + + int64 nTime = wtx.GetTxTime(); + int64 nCredit = wtx.GetCredit(); + int64 nDebit = wtx.GetDebit(); + int64 nNet = nCredit - nDebit; + + + + strHTML += _("Status: ") + FormatTxStatus(wtx); + int nRequests = wtx.GetRequestCount(); + if (nRequests != -1) + { + if (nRequests == 0) + strHTML += _(", has not been successfully broadcast yet"); + else if (nRequests == 1) + strHTML += strprintf(_(", broadcast through %d node"), nRequests); + else + strHTML += strprintf(_(", broadcast through %d nodes"), nRequests); + } + strHTML += "
"; + + strHTML += _("Date: ") + (nTime ? DateTimeStr(nTime) : "") + "
"; + + + // + // From + // + if (wtx.IsCoinBase()) + { + strHTML += _("Source: Generated
"); + } + else if (!wtx.mapValue["from"].empty()) + { + // Online transaction + if (!wtx.mapValue["from"].empty()) + strHTML += _("From: ") + HtmlEscape(wtx.mapValue["from"]) + "
"; + } + else + { + // Offline transaction + if (nNet > 0) + { + // Credit + foreach(const CTxOut& txout, wtx.vout) + { + if (txout.IsMine()) + { + vector vchPubKey; + if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey)) + { + string strAddress = PubKeyToAddress(vchPubKey); + if (mapAddressBook.count(strAddress)) + { + strHTML += string() + _("From: ") + _("unknown") + "
"; + strHTML += _("To: "); + strHTML += HtmlEscape(strAddress); + if (!mapAddressBook[strAddress].empty()) + strHTML += _(" (yours, label: ") + mapAddressBook[strAddress] + ")"; + else + strHTML += _(" (yours)"); + strHTML += "
"; + } + } + break; + } + } + } + } + + + // + // To + // + string strAddress; + if (!wtx.mapValue["to"].empty()) + { + // Online transaction + strAddress = wtx.mapValue["to"]; + strHTML += _("To: "); + if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty()) + strHTML += mapAddressBook[strAddress] + " "; + strHTML += HtmlEscape(strAddress) + "
"; + } + + + // + // Amount + // + if (wtx.IsCoinBase() && nCredit == 0) + { + // + // Coinbase + // + int64 nUnmatured = 0; + foreach(const CTxOut& txout, wtx.vout) + nUnmatured += txout.GetCredit(); + strHTML += _("Credit: "); + if (wtx.IsInMainChain()) + strHTML += strprintf(_("(%s matures in %d more blocks)"), FormatMoney(nUnmatured).c_str(), wtx.GetBlocksToMaturity()); + else + strHTML += _("(not accepted)"); + strHTML += "
"; + } + else if (nNet > 0) + { + // + // Credit + // + strHTML += _("Credit: ") + FormatMoney(nNet) + "
"; + } + else + { + bool fAllFromMe = true; + foreach(const CTxIn& txin, wtx.vin) + fAllFromMe = fAllFromMe && txin.IsMine(); + + bool fAllToMe = true; + foreach(const CTxOut& txout, wtx.vout) + fAllToMe = fAllToMe && txout.IsMine(); + + if (fAllFromMe) + { + // + // Debit + // + foreach(const CTxOut& txout, wtx.vout) + { + if (txout.IsMine()) + continue; + + if (wtx.mapValue["to"].empty()) + { + // Offline transaction + uint160 hash160; + if (ExtractHash160(txout.scriptPubKey, hash160)) + { + string strAddress = Hash160ToAddress(hash160); + strHTML += _("To: "); + if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty()) + strHTML += mapAddressBook[strAddress] + " "; + strHTML += strAddress; + strHTML += "
"; + } + } + + strHTML += _("Debit: ") + FormatMoney(-txout.nValue) + "
"; + } + + if (fAllToMe) + { + // Payment to self + int64 nChange = wtx.GetChange(); + int64 nValue = nCredit - nChange; + strHTML += _("Debit: ") + FormatMoney(-nValue) + "
"; + strHTML += _("Credit: ") + FormatMoney(nValue) + "
"; + } + + int64 nTxFee = nDebit - wtx.GetValueOut(); + if (nTxFee > 0) + strHTML += _("Transaction fee: ") + FormatMoney(-nTxFee) + "
"; + } + else + { + // + // Mixed debit transaction + // + foreach(const CTxIn& txin, wtx.vin) + if (txin.IsMine()) + strHTML += _("Debit: ") + FormatMoney(-txin.GetDebit()) + "
"; + foreach(const CTxOut& txout, wtx.vout) + if (txout.IsMine()) + strHTML += _("Credit: ") + FormatMoney(txout.GetCredit()) + "
"; + } + } + + strHTML += _("Net amount: ") + FormatMoney(nNet, true) + "
"; + + + // + // Message + // + if (!wtx.mapValue["message"].empty()) + strHTML += string() + "
" + _("Message:") + "
" + HtmlEscape(wtx.mapValue["message"], true) + "
"; + if (!wtx.mapValue["comment"].empty()) + strHTML += string() + "
" + _("Comment:") + "
" + HtmlEscape(wtx.mapValue["comment"], true) + "
"; + + if (wtx.IsCoinBase()) + strHTML += string() + "
" + _("Generated coins must wait 120 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, it will change to \"not accepted\" and not be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.") + "
"; + + + // + // Debug view + // + if (fDebug) + { + strHTML += "

debug print

"; + foreach(const CTxIn& txin, wtx.vin) + if (txin.IsMine()) + strHTML += "Debit: " + FormatMoney(-txin.GetDebit()) + "
"; + foreach(const CTxOut& txout, wtx.vout) + if (txout.IsMine()) + strHTML += "Credit: " + FormatMoney(txout.GetCredit()) + "
"; + + strHTML += "
Transaction:
"; + strHTML += HtmlEscape(wtx.ToString(), true); + + strHTML += "
Inputs:
"; + CRITICAL_BLOCK(cs_mapWallet) + { + foreach(const CTxIn& txin, wtx.vin) + { + COutPoint prevout = txin.prevout; + map::iterator mi = mapWallet.find(prevout.hash); + if (mi != mapWallet.end()) + { + const CWalletTx& prev = (*mi).second; + if (prevout.n < prev.vout.size()) + { + strHTML += HtmlEscape(prev.ToString(), true); + strHTML += "    " + FormatTxStatus(prev) + ", "; + strHTML = strHTML + "IsMine=" + (prev.vout[prevout.n].IsMine() ? "true" : "false") + "
"; + } + } + } + } + } + + + + strHTML += "
"; + string(strHTML.begin(), strHTML.end()).swap(strHTML); + m_htmlWin->SetPage(strHTML); + m_buttonOK->SetFocus(); + } +} + +void CTxDetailsDialog::OnButtonOK(wxCommandEvent& event) +{ + EndModal(false); +} + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// Startup folder +// + +#ifdef __WXMSW__ +string StartupShortcutPath() +{ + return MyGetSpecialFolderPath(CSIDL_STARTUP, true) + "\\Bitcoin.lnk"; +} + +bool GetStartOnSystemStartup() +{ + return filesystem::exists(StartupShortcutPath().c_str()); +} + +void SetStartOnSystemStartup(bool fAutoStart) +{ + // If the shortcut exists already, remove it for updating + remove(StartupShortcutPath().c_str()); + + if (fAutoStart) + { + CoInitialize(NULL); + + // Get a pointer to the IShellLink interface. + IShellLink* psl = NULL; + HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, + CLSCTX_INPROC_SERVER, IID_IShellLink, + reinterpret_cast(&psl)); + + if (SUCCEEDED(hres)) + { + // Get the current executable path + TCHAR pszExePath[MAX_PATH]; + GetModuleFileName(NULL, pszExePath, sizeof(pszExePath)); + + // Set the path to the shortcut target + psl->SetPath(pszExePath); + PathRemoveFileSpec(pszExePath); + psl->SetWorkingDirectory(pszExePath); + psl->SetShowCmd(SW_SHOWMINNOACTIVE); + + // Query IShellLink for the IPersistFile interface for + // saving the shortcut in persistent storage. + IPersistFile* ppf = NULL; + hres = psl->QueryInterface(IID_IPersistFile, + reinterpret_cast(&ppf)); + if (SUCCEEDED(hres)) + { + WCHAR pwsz[MAX_PATH]; + // Ensure that the string is ANSI. + MultiByteToWideChar(CP_ACP, 0, StartupShortcutPath().c_str(), -1, pwsz, MAX_PATH); + // Save the link by calling IPersistFile::Save. + hres = ppf->Save(pwsz, TRUE); + ppf->Release(); + } + psl->Release(); + } + CoUninitialize(); + } +} + +#elif defined(__WXGTK__) + +// Follow the Desktop Application Autostart Spec: +// http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html + +boost::filesystem::path GetAutostartDir() +{ + namespace fs = boost::filesystem; + + char* pszConfigHome = getenv("XDG_CONFIG_HOME"); + if (pszConfigHome) return fs::path(pszConfigHome) / fs::path("autostart"); + char* pszHome = getenv("HOME"); + if (pszHome) return fs::path(pszHome) / fs::path(".config/autostart"); + return fs::path(); +} + +boost::filesystem::path GetAutostartFilePath() +{ + return GetAutostartDir() / boost::filesystem::path("bitcoin.desktop"); +} + +bool GetStartOnSystemStartup() +{ + boost::filesystem::ifstream optionFile(GetAutostartFilePath()); + if (!optionFile.good()) + return false; + // Scan through file for "Hidden=true": + string line; + while (!optionFile.eof()) + { + getline(optionFile, line); + if (line.find("Hidden") != string::npos && + line.find("true") != string::npos) + return false; + } + optionFile.close(); + + return true; +} + +void SetStartOnSystemStartup(bool fAutoStart) +{ + if (!fAutoStart) + { + unlink(GetAutostartFilePath().native_file_string().c_str()); + } + else + { + char pszExePath[MAX_PATH+1]; + memset(pszExePath, 0, sizeof(pszExePath)); + if (readlink("/proc/self/exe", pszExePath, sizeof(pszExePath)-1) == -1) + return; + + boost::filesystem::create_directories(GetAutostartDir()); + + boost::filesystem::ofstream optionFile(GetAutostartFilePath(), ios_base::out|ios_base::trunc); + if (!optionFile.good()) + { + wxMessageBox(_("Cannot write autostart/bitcoin.desktop file"), "Bitcoin"); + return; + } + // Write a bitcoin.desktop file to the autostart directory: + optionFile << "[Desktop Entry]\n"; + optionFile << "Type=Application\n"; + optionFile << "Name=Bitcoin\n"; + optionFile << "Exec=" << pszExePath << "\n"; + optionFile << "Terminal=false\n"; + optionFile << "Hidden=false\n"; + optionFile.close(); + } +} +#else + +// TODO: OSX startup stuff; see: +// http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html + +bool GetStartOnSystemStartup() { return false; } +void SetStartOnSystemStartup(bool fAutoStart) { } + +#endif + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// COptionsDialog +// + +COptionsDialog::COptionsDialog(wxWindow* parent) : COptionsDialogBase(parent) +{ + // Set up list box of page choices + m_listBox->Append(_("Main")); + //m_listBox->Append(_("Test 2")); + m_listBox->SetSelection(0); + SelectPage(0); +#ifndef __WXMSW__ + SetSize(1.0 * GetSize().GetWidth(), 1.2 * GetSize().GetHeight()); +#endif +#if defined(__WXGTK__) || defined(__WXMAC_OSX__) + m_checkBoxStartOnSystemStartup->SetLabel(_("&Start Bitcoin on window system startup")); + if (!GetBoolArg("-minimizetotray")) + { + // Minimize to tray is just too buggy on Linux + fMinimizeToTray = false; + m_checkBoxMinimizeToTray->SetValue(false); + m_checkBoxMinimizeToTray->Enable(false); + m_checkBoxMinimizeOnClose->SetLabel(_("&Minimize on close")); + } +#endif +#ifdef __WXMAC_OSX__ + m_checkBoxStartOnSystemStartup->Enable(false); // not implemented yet +#endif + + // Init values + m_textCtrlTransactionFee->SetValue(FormatMoney(nTransactionFee)); + m_checkBoxLimitProcessors->SetValue(fLimitProcessors); + m_spinCtrlLimitProcessors->Enable(fLimitProcessors); + m_spinCtrlLimitProcessors->SetValue(nLimitProcessors); + int nProcessors = wxThread::GetCPUCount(); + if (nProcessors < 1) + nProcessors = 999; + m_spinCtrlLimitProcessors->SetRange(1, nProcessors); + m_checkBoxStartOnSystemStartup->SetValue(fTmpStartOnSystemStartup = GetStartOnSystemStartup()); + m_checkBoxMinimizeToTray->SetValue(fMinimizeToTray); + m_checkBoxMinimizeOnClose->SetValue(fMinimizeOnClose); + if (fHaveUPnP) + m_checkBoxUseUPnP->SetValue(fUseUPnP); + else + m_checkBoxUseUPnP->Enable(false); + m_checkBoxUseProxy->SetValue(fUseProxy); + m_textCtrlProxyIP->Enable(fUseProxy); + m_textCtrlProxyPort->Enable(fUseProxy); + m_staticTextProxyIP->Enable(fUseProxy); + m_staticTextProxyPort->Enable(fUseProxy); + m_textCtrlProxyIP->SetValue(addrProxy.ToStringIP()); + m_textCtrlProxyPort->SetValue(addrProxy.ToStringPort()); + + m_buttonOK->SetFocus(); +} + +void COptionsDialog::SelectPage(int nPage) +{ + m_panelMain->Show(nPage == 0); + m_panelTest2->Show(nPage == 1); + + m_scrolledWindow->Layout(); + m_scrolledWindow->SetScrollbars(0, 0, 0, 0, 0, 0); +} + +void COptionsDialog::OnListBox(wxCommandEvent& event) +{ + SelectPage(event.GetSelection()); +} + +void COptionsDialog::OnKillFocusTransactionFee(wxFocusEvent& event) +{ + event.Skip(); + int64 nTmp = nTransactionFee; + ParseMoney(m_textCtrlTransactionFee->GetValue(), nTmp); + m_textCtrlTransactionFee->SetValue(FormatMoney(nTmp)); +} + +void COptionsDialog::OnCheckBoxLimitProcessors(wxCommandEvent& event) +{ + m_spinCtrlLimitProcessors->Enable(event.IsChecked()); +} + +void COptionsDialog::OnCheckBoxUseProxy(wxCommandEvent& event) +{ + m_textCtrlProxyIP->Enable(event.IsChecked()); + m_textCtrlProxyPort->Enable(event.IsChecked()); + m_staticTextProxyIP->Enable(event.IsChecked()); + m_staticTextProxyPort->Enable(event.IsChecked()); +} + +CAddress COptionsDialog::GetProxyAddr() +{ + // Be careful about byte order, addr.ip and addr.port are big endian + CAddress addr(m_textCtrlProxyIP->GetValue() + ":" + m_textCtrlProxyPort->GetValue()); + if (addr.ip == INADDR_NONE) + addr.ip = addrProxy.ip; + int nPort = atoi(m_textCtrlProxyPort->GetValue()); + addr.port = htons(nPort); + if (nPort <= 0 || nPort > USHRT_MAX) + addr.port = addrProxy.port; + return addr; +} + +void COptionsDialog::OnKillFocusProxy(wxFocusEvent& event) +{ + event.Skip(); + m_textCtrlProxyIP->SetValue(GetProxyAddr().ToStringIP()); + m_textCtrlProxyPort->SetValue(GetProxyAddr().ToStringPort()); +} + + +void COptionsDialog::OnButtonOK(wxCommandEvent& event) +{ + OnButtonApply(event); + EndModal(false); +} + +void COptionsDialog::OnButtonCancel(wxCommandEvent& event) +{ + EndModal(false); +} + +void COptionsDialog::OnButtonApply(wxCommandEvent& event) +{ + CWalletDB walletdb; + + int64 nPrevTransactionFee = nTransactionFee; + if (ParseMoney(m_textCtrlTransactionFee->GetValue(), nTransactionFee) && nTransactionFee != nPrevTransactionFee) + walletdb.WriteSetting("nTransactionFee", nTransactionFee); + + int nPrevMaxProc = (fLimitProcessors ? nLimitProcessors : INT_MAX); + if (fLimitProcessors != m_checkBoxLimitProcessors->GetValue()) + { + fLimitProcessors = m_checkBoxLimitProcessors->GetValue(); + walletdb.WriteSetting("fLimitProcessors", fLimitProcessors); + } + if (nLimitProcessors != m_spinCtrlLimitProcessors->GetValue()) + { + nLimitProcessors = m_spinCtrlLimitProcessors->GetValue(); + walletdb.WriteSetting("nLimitProcessors", nLimitProcessors); + } + if (fGenerateBitcoins && (fLimitProcessors ? nLimitProcessors : INT_MAX) > nPrevMaxProc) + GenerateBitcoins(fGenerateBitcoins); + + if (fTmpStartOnSystemStartup != m_checkBoxStartOnSystemStartup->GetValue()) + { + fTmpStartOnSystemStartup = m_checkBoxStartOnSystemStartup->GetValue(); + SetStartOnSystemStartup(fTmpStartOnSystemStartup); + } + + if (fMinimizeToTray != m_checkBoxMinimizeToTray->GetValue()) + { + fMinimizeToTray = m_checkBoxMinimizeToTray->GetValue(); + walletdb.WriteSetting("fMinimizeToTray", fMinimizeToTray); + ptaskbaricon->Show(fMinimizeToTray || fClosedToTray); + } + + if (fMinimizeOnClose != m_checkBoxMinimizeOnClose->GetValue()) + { + fMinimizeOnClose = m_checkBoxMinimizeOnClose->GetValue(); + walletdb.WriteSetting("fMinimizeOnClose", fMinimizeOnClose); + } + + if (fHaveUPnP && fUseUPnP != m_checkBoxUseUPnP->GetValue()) + { + fUseUPnP = m_checkBoxUseUPnP->GetValue(); + walletdb.WriteSetting("fUseUPnP", fUseUPnP); + MapPort(fUseUPnP); + } + + fUseProxy = m_checkBoxUseProxy->GetValue(); + walletdb.WriteSetting("fUseProxy", fUseProxy); + + addrProxy = GetProxyAddr(); + walletdb.WriteSetting("addrProxy", addrProxy); +} + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// CAboutDialog +// + +CAboutDialog::CAboutDialog(wxWindow* parent) : CAboutDialogBase(parent) +{ + m_staticTextVersion->SetLabel(strprintf(_("version %s"), FormatFullVersion().c_str())); + + // Change (c) into UTF-8 or ANSI copyright symbol + wxString str = m_staticTextMain->GetLabel(); +#if wxUSE_UNICODE + str.Replace("(c)", wxString::FromUTF8("\xC2\xA9")); +#else + str.Replace("(c)", "\xA9"); +#endif + m_staticTextMain->SetLabel(str); +#ifndef __WXMSW__ + // Resize on Linux to make the window fit the text. + // The text was wrapped manually rather than using the Wrap setting because + // the wrap would be too small on Linux and it can't be changed at this point. + wxFont fontTmp = m_staticTextMain->GetFont(); + if (fontTmp.GetPointSize() > 8); + fontTmp.SetPointSize(8); + m_staticTextMain->SetFont(fontTmp); + SetSize(GetSize().GetWidth() + 44, GetSize().GetHeight() + 10); +#endif +} + +void CAboutDialog::OnButtonOK(wxCommandEvent& event) +{ + EndModal(false); +} + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// CSendDialog +// + +CSendDialog::CSendDialog(wxWindow* parent, const wxString& strAddress) : CSendDialogBase(parent) +{ + // Init + m_textCtrlAddress->SetValue(strAddress); + m_choiceTransferType->SetSelection(0); + m_bitmapCheckMark->Show(false); + fEnabledPrev = true; + m_textCtrlAddress->SetFocus(); + //// todo: should add a display of your balance for convenience +#ifndef __WXMSW__ + wxFont fontTmp = m_staticTextInstructions->GetFont(); + if (fontTmp.GetPointSize() > 9); + fontTmp.SetPointSize(9); + m_staticTextInstructions->SetFont(fontTmp); + SetSize(725, 180); +#endif + + // Set Icon + wxIcon iconSend; + iconSend.CopyFromBitmap(wxBitmap(send16noshadow_xpm)); + SetIcon(iconSend); + + // Fixup the tab order + m_buttonPaste->MoveAfterInTabOrder(m_buttonCancel); + m_buttonAddress->MoveAfterInTabOrder(m_buttonPaste); + this->Layout(); +} + +void CSendDialog::OnKillFocusAmount(wxFocusEvent& event) +{ + // Reformat the amount + event.Skip(); + if (m_textCtrlAmount->GetValue().Trim().empty()) + return; + int64 nTmp; + if (ParseMoney(m_textCtrlAmount->GetValue(), nTmp)) + m_textCtrlAmount->SetValue(FormatMoney(nTmp)); +} + +void CSendDialog::OnButtonAddressBook(wxCommandEvent& event) +{ + // Open address book + CAddressBookDialog dialog(this, m_textCtrlAddress->GetValue(), CAddressBookDialog::SENDING, true); + if (dialog.ShowModal()) + m_textCtrlAddress->SetValue(dialog.GetSelectedAddress()); +} + +void CSendDialog::OnButtonPaste(wxCommandEvent& event) +{ + // Copy clipboard to address box + if (wxTheClipboard->Open()) + { + if (wxTheClipboard->IsSupported(wxDF_TEXT)) + { + wxTextDataObject data; + wxTheClipboard->GetData(data); + m_textCtrlAddress->SetValue(data.GetText()); + } + wxTheClipboard->Close(); + } +} + +void CSendDialog::OnButtonSend(wxCommandEvent& event) +{ + static CCriticalSection cs_sendlock; + TRY_CRITICAL_BLOCK(cs_sendlock) + { + CWalletTx wtx; + string strAddress = (string)m_textCtrlAddress->GetValue(); + + // Parse amount + int64 nValue = 0; + if (!ParseMoney(m_textCtrlAmount->GetValue(), nValue) || nValue <= 0) + { + wxMessageBox(_("Error in amount "), _("Send Coins")); + return; + } + if (nValue > GetBalance()) + { + wxMessageBox(_("Amount exceeds your balance "), _("Send Coins")); + return; + } + if (nValue + nTransactionFee > GetBalance()) + { + wxMessageBox(string(_("Total exceeds your balance when the ")) + FormatMoney(nTransactionFee) + _(" transaction fee is included "), _("Send Coins")); + return; + } + + // Parse bitcoin address + uint160 hash160; + bool fBitcoinAddress = AddressToHash160(strAddress, hash160); + + if (fBitcoinAddress) + { + CRITICAL_BLOCK(cs_main) + { + // Send to bitcoin address + CScript scriptPubKey; + scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG; + + string strError = SendMoney(scriptPubKey, nValue, wtx, true); + if (strError == "") + wxMessageBox(_("Payment sent "), _("Sending...")); + else if (strError == "ABORTED") + return; // leave send dialog open + else + { + wxMessageBox(strError + " ", _("Sending...")); + EndModal(false); + } + } + } + else + { + // Parse IP address + CAddress addr(strAddress); + if (!addr.IsValid()) + { + wxMessageBox(_("Invalid address "), _("Send Coins")); + return; + } + + // Message + wtx.mapValue["to"] = strAddress; + + // Send to IP address + CSendingDialog* pdialog = new CSendingDialog(this, addr, nValue, wtx); + if (!pdialog->ShowModal()) + return; + } + + CRITICAL_BLOCK(cs_mapAddressBook) + if (!mapAddressBook.count(strAddress)) + SetAddressBookName(strAddress, ""); + + EndModal(true); + } +} + +void CSendDialog::OnButtonCancel(wxCommandEvent& event) +{ + // Cancel + EndModal(false); +} + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// CSendingDialog +// + +CSendingDialog::CSendingDialog(wxWindow* parent, const CAddress& addrIn, int64 nPriceIn, const CWalletTx& wtxIn) : CSendingDialogBase(NULL) // we have to give null so parent can't destroy us +{ + addr = addrIn; + nPrice = nPriceIn; + wtx = wtxIn; + start = wxDateTime::UNow(); + memset(pszStatus, 0, sizeof(pszStatus)); + fCanCancel = true; + fAbort = false; + fSuccess = false; + fUIDone = false; + fWorkDone = false; +#ifndef __WXMSW__ + SetSize(1.2 * GetSize().GetWidth(), 1.08 * GetSize().GetHeight()); +#endif + + SetTitle(strprintf(_("Sending %s to %s"), FormatMoney(nPrice).c_str(), wtx.mapValue["to"].c_str())); + m_textCtrlStatus->SetValue(""); + + CreateThread(SendingDialogStartTransfer, this); +} + +CSendingDialog::~CSendingDialog() +{ + printf("~CSendingDialog()\n"); +} + +void CSendingDialog::Close() +{ + // Last one out turn out the lights. + // fWorkDone signals that work side is done and UI thread should call destroy. + // fUIDone signals that UI window has closed and work thread should call destroy. + // This allows the window to disappear and end modality when cancelled + // without making the user wait for ConnectNode to return. The dialog object + // hangs around in the background until the work thread exits. + if (IsModal()) + EndModal(fSuccess); + else + Show(false); + if (fWorkDone) + Destroy(); + else + fUIDone = true; +} + +void CSendingDialog::OnClose(wxCloseEvent& event) +{ + if (!event.CanVeto() || fWorkDone || fAbort || !fCanCancel) + { + Close(); + } + else + { + event.Veto(); + wxCommandEvent cmdevent; + OnButtonCancel(cmdevent); + } +} + +void CSendingDialog::OnButtonOK(wxCommandEvent& event) +{ + if (fWorkDone) + Close(); +} + +void CSendingDialog::OnButtonCancel(wxCommandEvent& event) +{ + if (fCanCancel) + fAbort = true; +} + +void CSendingDialog::OnPaint(wxPaintEvent& event) +{ + event.Skip(); + if (strlen(pszStatus) > 130) + m_textCtrlStatus->SetValue(string("\n") + pszStatus); + else + m_textCtrlStatus->SetValue(string("\n\n") + pszStatus); + m_staticTextSending->SetFocus(); + if (!fCanCancel) + m_buttonCancel->Enable(false); + if (fWorkDone) + { + m_buttonOK->Enable(true); + m_buttonOK->SetFocus(); + m_buttonCancel->Enable(false); + } + if (fAbort && fCanCancel && IsShown()) + { + strcpy(pszStatus, _("CANCELLED")); + m_buttonOK->Enable(true); + m_buttonOK->SetFocus(); + m_buttonCancel->Enable(false); + m_buttonCancel->SetLabel(_("Cancelled")); + Close(); + wxMessageBox(_("Transfer cancelled "), _("Sending..."), wxOK, this); + } +} + + +// +// Everything from here on is not in the UI thread and must only communicate +// with the rest of the dialog through variables and calling repaint. +// + +void CSendingDialog::Repaint() +{ + Refresh(); + wxPaintEvent event; + GetEventHandler()->AddPendingEvent(event); +} + +bool CSendingDialog::Status() +{ + if (fUIDone) + { + Destroy(); + return false; + } + if (fAbort && fCanCancel) + { + memset(pszStatus, 0, 10); + strcpy(pszStatus, _("CANCELLED")); + Repaint(); + fWorkDone = true; + return false; + } + return true; +} + +bool CSendingDialog::Status(const string& str) +{ + if (!Status()) + return false; + + // This can be read by the UI thread at any time, + // so copy in a way that can be read cleanly at all times. + memset(pszStatus, 0, min(str.size()+1, sizeof(pszStatus))); + strlcpy(pszStatus, str.c_str(), sizeof(pszStatus)); + + Repaint(); + return true; +} + +bool CSendingDialog::Error(const string& str) +{ + fCanCancel = false; + fWorkDone = true; + Status(string(_("Error: ")) + str); + return false; +} + +void SendingDialogStartTransfer(void* parg) +{ + ((CSendingDialog*)parg)->StartTransfer(); +} + +void CSendingDialog::StartTransfer() +{ + // Make sure we have enough money + if (nPrice + nTransactionFee > GetBalance()) + { + Error(_("Insufficient funds")); + return; + } + + // We may have connected already for product details + if (!Status(_("Connecting..."))) + return; + CNode* pnode = ConnectNode(addr, 15 * 60); + if (!pnode) + { + Error(_("Unable to connect")); + return; + } + + // Send order to seller, with response going to OnReply2 via event handler + if (!Status(_("Requesting public key..."))) + return; + pnode->PushRequest("checkorder", wtx, SendingDialogOnReply2, this); +} + +void SendingDialogOnReply2(void* parg, CDataStream& vRecv) +{ + ((CSendingDialog*)parg)->OnReply2(vRecv); +} + +void CSendingDialog::OnReply2(CDataStream& vRecv) +{ + if (!Status(_("Received public key..."))) + return; + + CScript scriptPubKey; + int nRet; + try + { + vRecv >> nRet; + if (nRet > 0) + { + string strMessage; + if (!vRecv.empty()) + vRecv >> strMessage; + if (nRet == 2) + Error(_("Recipient is not accepting transactions sent by IP address")); + else + Error(_("Transfer was not accepted")); + //// todo: enlarge the window and enable a hidden white box to put seller's message + return; + } + vRecv >> scriptPubKey; + } + catch (...) + { + //// what do we want to do about this? + Error(_("Invalid response received")); + return; + } + + // Pause to give the user a chance to cancel + while (wxDateTime::UNow() < start + wxTimeSpan(0, 0, 0, 2 * 1000)) + { + Sleep(200); + if (!Status()) + return; + } + + CRITICAL_BLOCK(cs_main) + { + // Pay + if (!Status(_("Creating transaction..."))) + return; + if (nPrice + nTransactionFee > GetBalance()) + { + Error(_("Insufficient funds")); + return; + } + CReserveKey reservekey; + int64 nFeeRequired; + if (!CreateTransaction(scriptPubKey, nPrice, wtx, reservekey, nFeeRequired)) + { + if (nPrice + nFeeRequired > GetBalance()) + Error(strprintf(_("This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds"), FormatMoney(nFeeRequired).c_str())); + else + Error(_("Transaction creation failed")); + return; + } + + // Transaction fee + if (!ThreadSafeAskFee(nFeeRequired, _("Sending..."), this)) + { + Error(_("Transaction aborted")); + return; + } + + // Make sure we're still connected + CNode* pnode = ConnectNode(addr, 2 * 60 * 60); + if (!pnode) + { + Error(_("Lost connection, transaction cancelled")); + return; + } + + // Last chance to cancel + Sleep(50); + if (!Status()) + return; + fCanCancel = false; + if (fAbort) + { + fCanCancel = true; + if (!Status()) + return; + fCanCancel = false; + } + if (!Status(_("Sending payment..."))) + return; + + // Commit + if (!CommitTransaction(wtx, reservekey)) + { + Error(_("The transaction was rejected. This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.")); + return; + } + + // Send payment tx to seller, with response going to OnReply3 via event handler + CWalletTx wtxSend = wtx; + wtxSend.fFromMe = false; + pnode->PushRequest("submitorder", wtxSend, SendingDialogOnReply3, this); + + Status(_("Waiting for confirmation...")); + MainFrameRepaint(); + } +} + +void SendingDialogOnReply3(void* parg, CDataStream& vRecv) +{ + ((CSendingDialog*)parg)->OnReply3(vRecv); +} + +void CSendingDialog::OnReply3(CDataStream& vRecv) +{ + int nRet; + try + { + vRecv >> nRet; + if (nRet > 0) + { + Error(_("The payment was sent, but the recipient was unable to verify it.\n" + "The transaction is recorded and will credit to the recipient,\n" + "but the comment information will be blank.")); + return; + } + } + catch (...) + { + //// what do we want to do about this? + Error(_("Payment was sent, but an invalid response was received")); + return; + } + + fSuccess = true; + fWorkDone = true; + Status(_("Payment completed")); +} + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// CAddressBookDialog +// + +CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInitSelected, int nPageIn, bool fDuringSendIn) : CAddressBookDialogBase(parent) +{ + // Set initially selected page + wxNotebookEvent event; + event.SetSelection(nPageIn); + OnNotebookPageChanged(event); + m_notebook->ChangeSelection(nPageIn); + + fDuringSend = fDuringSendIn; + if (!fDuringSend) + m_buttonCancel->Show(false); + + // Set Icon + wxIcon iconAddressBook; + iconAddressBook.CopyFromBitmap(wxBitmap(addressbook16_xpm)); + SetIcon(iconAddressBook); + + // Init column headers + m_listCtrlSending->InsertColumn(0, _("Name"), wxLIST_FORMAT_LEFT, 200); + m_listCtrlSending->InsertColumn(1, _("Address"), wxLIST_FORMAT_LEFT, 350); + m_listCtrlSending->SetFocus(); + m_listCtrlReceiving->InsertColumn(0, _("Label"), wxLIST_FORMAT_LEFT, 200); + m_listCtrlReceiving->InsertColumn(1, _("Bitcoin Address"), wxLIST_FORMAT_LEFT, 350); + m_listCtrlReceiving->SetFocus(); + + // Fill listctrl with address book data + CRITICAL_BLOCK(cs_mapKeys) + CRITICAL_BLOCK(cs_mapAddressBook) + { + string strDefaultReceiving = (string)pframeMain->m_textCtrlAddress->GetValue(); + foreach(const PAIRTYPE(string, string)& item, mapAddressBook) + { + string strAddress = item.first; + string strName = item.second; + uint160 hash160; + bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160)); + wxListCtrl* plistCtrl = fMine ? m_listCtrlReceiving : m_listCtrlSending; + int nIndex = InsertLine(plistCtrl, strName, strAddress); + if (strAddress == (fMine ? strDefaultReceiving : string(strInitSelected))) + plistCtrl->SetItemState(nIndex, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED); + } + } +} + +wxString CAddressBookDialog::GetSelectedAddress() +{ + int nIndex = GetSelection(m_listCtrl); + if (nIndex == -1) + return ""; + return GetItemText(m_listCtrl, nIndex, 1); +} + +wxString CAddressBookDialog::GetSelectedSendingAddress() +{ + int nIndex = GetSelection(m_listCtrlSending); + if (nIndex == -1) + return ""; + return GetItemText(m_listCtrlSending, nIndex, 1); +} + +wxString CAddressBookDialog::GetSelectedReceivingAddress() +{ + int nIndex = GetSelection(m_listCtrlReceiving); + if (nIndex == -1) + return ""; + return GetItemText(m_listCtrlReceiving, nIndex, 1); +} + +void CAddressBookDialog::OnNotebookPageChanged(wxNotebookEvent& event) +{ + event.Skip(); + nPage = event.GetSelection(); + if (nPage == SENDING) + m_listCtrl = m_listCtrlSending; + else if (nPage == RECEIVING) + m_listCtrl = m_listCtrlReceiving; + m_buttonDelete->Show(nPage == SENDING); + m_buttonCopy->Show(nPage == RECEIVING); + this->Layout(); + m_listCtrl->SetFocus(); +} + +void CAddressBookDialog::OnListEndLabelEdit(wxListEvent& event) +{ + // Update address book with edited name + event.Skip(); + if (event.IsEditCancelled()) + return; + string strAddress = (string)GetItemText(m_listCtrl, event.GetIndex(), 1); + SetAddressBookName(strAddress, string(event.GetText())); + pframeMain->RefreshListCtrl(); +} + +void CAddressBookDialog::OnListItemSelected(wxListEvent& event) +{ + event.Skip(); + if (nPage == RECEIVING) + SetDefaultReceivingAddress((string)GetSelectedReceivingAddress()); +} + +void CAddressBookDialog::OnListItemActivated(wxListEvent& event) +{ + event.Skip(); + if (fDuringSend) + { + // Doubleclick returns selection + EndModal(GetSelectedAddress() != "" ? 2 : 0); + return; + } + + // Doubleclick edits item + wxCommandEvent event2; + OnButtonEdit(event2); +} + +void CAddressBookDialog::OnButtonDelete(wxCommandEvent& event) +{ + if (nPage != SENDING) + return; + for (int nIndex = m_listCtrl->GetItemCount()-1; nIndex >= 0; nIndex--) + { + if (m_listCtrl->GetItemState(nIndex, wxLIST_STATE_SELECTED)) + { + string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1); + CWalletDB().EraseName(strAddress); + m_listCtrl->DeleteItem(nIndex); + } + } + pframeMain->RefreshListCtrl(); +} + +void CAddressBookDialog::OnButtonCopy(wxCommandEvent& event) +{ + // Copy address box to clipboard + if (wxTheClipboard->Open()) + { + wxTheClipboard->SetData(new wxTextDataObject(GetSelectedAddress())); + wxTheClipboard->Close(); + } +} + +bool CAddressBookDialog::CheckIfMine(const string& strAddress, const string& strTitle) +{ + uint160 hash160; + bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160)); + if (fMine) + wxMessageBox(_("This is one of your own addresses for receiving payments and cannot be entered in the address book. "), strTitle); + return fMine; +} + +void CAddressBookDialog::OnButtonEdit(wxCommandEvent& event) +{ + int nIndex = GetSelection(m_listCtrl); + if (nIndex == -1) + return; + string strName = (string)m_listCtrl->GetItemText(nIndex); + string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1); + string strAddressOrg = strAddress; + + if (nPage == SENDING) + { + // Ask name and address + do + { + CGetTextFromUserDialog dialog(this, _("Edit Address"), _("Name"), strName, _("Address"), strAddress); + if (!dialog.ShowModal()) + return; + strName = dialog.GetValue1(); + strAddress = dialog.GetValue2(); + } + while (CheckIfMine(strAddress, _("Edit Address"))); + + } + else if (nPage == RECEIVING) + { + // Ask name + CGetTextFromUserDialog dialog(this, _("Edit Address Label"), _("Label"), strName); + if (!dialog.ShowModal()) + return; + strName = dialog.GetValue(); + } + + // Write back + if (strAddress != strAddressOrg) + CWalletDB().EraseName(strAddressOrg); + SetAddressBookName(strAddress, strName); + m_listCtrl->SetItem(nIndex, 1, strAddress); + m_listCtrl->SetItemText(nIndex, strName); + pframeMain->RefreshListCtrl(); +} + +void CAddressBookDialog::OnButtonNew(wxCommandEvent& event) +{ + string strName; + string strAddress; + + if (nPage == SENDING) + { + // Ask name and address + do + { + CGetTextFromUserDialog dialog(this, _("Add Address"), _("Name"), strName, _("Address"), strAddress); + if (!dialog.ShowModal()) + return; + strName = dialog.GetValue1(); + strAddress = dialog.GetValue2(); + } + while (CheckIfMine(strAddress, _("Add Address"))); + } + else if (nPage == RECEIVING) + { + // Ask name + CGetTextFromUserDialog dialog(this, + _("New Receiving Address"), + _("You should use a new address for each payment you receive.\n\nLabel"), + ""); + if (!dialog.ShowModal()) + return; + strName = dialog.GetValue(); + + // Generate new key + strAddress = PubKeyToAddress(GetKeyFromKeyPool()); + } + + // Add to list and select it + SetAddressBookName(strAddress, strName); + int nIndex = InsertLine(m_listCtrl, strName, strAddress); + SetSelection(m_listCtrl, nIndex); + m_listCtrl->SetFocus(); + if (nPage == SENDING) + pframeMain->RefreshListCtrl(); +} + +void CAddressBookDialog::OnButtonOK(wxCommandEvent& event) +{ + // OK + EndModal(GetSelectedAddress() != "" ? 1 : 0); +} + +void CAddressBookDialog::OnButtonCancel(wxCommandEvent& event) +{ + // Cancel + EndModal(0); +} + +void CAddressBookDialog::OnClose(wxCloseEvent& event) +{ + // Close + EndModal(0); +} + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// CMyTaskBarIcon +// + +enum +{ + ID_TASKBAR_RESTORE = 10001, + ID_TASKBAR_OPTIONS, + ID_TASKBAR_GENERATE, + ID_TASKBAR_EXIT, +}; + +BEGIN_EVENT_TABLE(CMyTaskBarIcon, wxTaskBarIcon) + EVT_TASKBAR_LEFT_DCLICK(CMyTaskBarIcon::OnLeftButtonDClick) + EVT_MENU(ID_TASKBAR_RESTORE, CMyTaskBarIcon::OnMenuRestore) + EVT_MENU(ID_TASKBAR_OPTIONS, CMyTaskBarIcon::OnMenuOptions) + EVT_MENU(ID_TASKBAR_GENERATE, CMyTaskBarIcon::OnMenuGenerate) + EVT_UPDATE_UI(ID_TASKBAR_GENERATE, CMyTaskBarIcon::OnUpdateUIGenerate) + EVT_MENU(ID_TASKBAR_EXIT, CMyTaskBarIcon::OnMenuExit) +END_EVENT_TABLE() + +void CMyTaskBarIcon::Show(bool fShow) +{ + static char pszPrevTip[200]; + if (fShow) + { + string strTooltip = _("Bitcoin"); + if (fGenerateBitcoins) + strTooltip = _("Bitcoin - Generating"); + if (fGenerateBitcoins && vNodes.empty()) + strTooltip = _("Bitcoin - (not connected)"); + + // Optimization, only update when changed, using char array to be reentrant + if (strncmp(pszPrevTip, strTooltip.c_str(), sizeof(pszPrevTip)-1) != 0) + { + strlcpy(pszPrevTip, strTooltip.c_str(), sizeof(pszPrevTip)); +#ifdef __WXMSW__ + // somehow it'll choose the wrong size and scale it down if + // we use the main icon, so we hand it one with only 16x16 + SetIcon(wxICON(favicon), strTooltip); +#else + SetIcon(bitcoin80_xpm, strTooltip); +#endif + } + } + else + { + strlcpy(pszPrevTip, "", sizeof(pszPrevTip)); + RemoveIcon(); + } +} + +void CMyTaskBarIcon::Hide() +{ + Show(false); +} + +void CMyTaskBarIcon::OnLeftButtonDClick(wxTaskBarIconEvent& event) +{ + Restore(); +} + +void CMyTaskBarIcon::OnMenuRestore(wxCommandEvent& event) +{ + Restore(); +} + +void CMyTaskBarIcon::OnMenuOptions(wxCommandEvent& event) +{ + // Since it's modal, get the main window to do it + wxCommandEvent event2(wxEVT_COMMAND_MENU_SELECTED, wxID_PREFERENCES); + pframeMain->GetEventHandler()->AddPendingEvent(event2); +} + +void CMyTaskBarIcon::Restore() +{ + pframeMain->Show(); + wxIconizeEvent event(0, false); + pframeMain->GetEventHandler()->AddPendingEvent(event); + pframeMain->Iconize(false); + pframeMain->Raise(); +} + +void CMyTaskBarIcon::OnMenuGenerate(wxCommandEvent& event) +{ + GenerateBitcoins(event.IsChecked()); +} + +void CMyTaskBarIcon::OnUpdateUIGenerate(wxUpdateUIEvent& event) +{ + event.Check(fGenerateBitcoins); +} + +void CMyTaskBarIcon::OnMenuExit(wxCommandEvent& event) +{ + pframeMain->Close(true); +} + +void CMyTaskBarIcon::UpdateTooltip() +{ + if (IsIconInstalled()) + Show(true); +} + +wxMenu* CMyTaskBarIcon::CreatePopupMenu() +{ + wxMenu* pmenu = new wxMenu; + pmenu->Append(ID_TASKBAR_RESTORE, _("&Open Bitcoin")); + pmenu->Append(ID_TASKBAR_OPTIONS, _("O&ptions...")); + pmenu->AppendCheckItem(ID_TASKBAR_GENERATE, _("&Generate Coins"))->Check(fGenerateBitcoins); +#ifndef __WXMAC_OSX__ // Mac has built-in quit menu + pmenu->AppendSeparator(); + pmenu->Append(ID_TASKBAR_EXIT, _("E&xit")); +#endif + return pmenu; +} + + + + + + +////////////////////////////////////////////////////////////////////////////// +// +// CMyApp +// + +void CreateMainWindow() +{ + pframeMain = new CMainFrame(NULL); + if (GetBoolArg("-min")) + pframeMain->Iconize(true); +#if defined(__WXGTK__) || defined(__WXMAC_OSX__) + if (!GetBoolArg("-minimizetotray")) + fMinimizeToTray = false; +#endif + pframeMain->Show(true); // have to show first to get taskbar button to hide + if (fMinimizeToTray && pframeMain->IsIconized()) + fClosedToTray = true; + pframeMain->Show(!fClosedToTray); + ptaskbaricon->Show(fMinimizeToTray || fClosedToTray); + CreateThread(ThreadDelayedRepaint, NULL); +} + + +// Define a new application +class CMyApp : public wxApp +{ +public: + CMyApp(){}; + ~CMyApp(){}; + bool OnInit(); + bool OnInit2(); + int OnExit(); + + // Hook Initialize so we can start without GUI + virtual bool Initialize(int& argc, wxChar** argv); + + // 2nd-level exception handling: we get all the exceptions occurring in any + // event handler here + virtual bool OnExceptionInMainLoop(); + + // 3rd, and final, level exception handling: whenever an unhandled + // exception is caught, this function is called + virtual void OnUnhandledException(); + + // and now for something different: this function is called in case of a + // crash (e.g. dereferencing null pointer, division by 0, ...) + virtual void OnFatalException(); +}; + +IMPLEMENT_APP(CMyApp) + +bool CMyApp::Initialize(int& argc, wxChar** argv) +{ + for (int i = 1; i < argc; i++) + if (!IsSwitchChar(argv[i][0])) + fCommandLine = true; + + if (!fCommandLine) + { + // wxApp::Initialize will remove environment-specific parameters, + // so it's too early to call ParseParameters yet + for (int i = 1; i < argc; i++) + { + wxString str = argv[i]; + #ifdef __WXMSW__ + if (str.size() >= 1 && str[0] == '/') + str[0] = '-'; + char pszLower[MAX_PATH]; + strlcpy(pszLower, str.c_str(), sizeof(pszLower)); + strlwr(pszLower); + str = pszLower; + #endif + if (str == "-daemon") + fDaemon = true; + } + } + +#ifdef __WXGTK__ + if (fDaemon || fCommandLine) + { + // Call the original Initialize while suppressing error messages + // and ignoring failure. If unable to initialize GTK, it fails + // near the end so hopefully the last few things don't matter. + { + wxLogNull logNo; + wxApp::Initialize(argc, argv); + } + + if (fDaemon) + { + // Daemonize + pid_t pid = fork(); + if (pid < 0) + { + fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno); + return false; + } + if (pid > 0) + pthread_exit((void*)0); + + pid_t sid = setsid(); + if (sid < 0) + fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno); + } + + return true; + } +#endif + + return wxApp::Initialize(argc, argv); +} + +bool CMyApp::OnInit() +{ +#if defined(__WXMSW__) && defined(__WXDEBUG__) && defined(GUI) + // Disable malfunctioning wxWidgets debug assertion + extern int g_isPainting; + g_isPainting = 10000; +#endif +#ifdef GUI + wxImage::AddHandler(new wxPNGHandler); +#endif +#if defined(__WXMSW__ ) || defined(__WXMAC_OSX__) + SetAppName("Bitcoin"); +#else + SetAppName("bitcoin"); +#endif +#ifdef __WXMSW__ +#if wxUSE_UNICODE + // Hack to set wxConvLibc codepage to UTF-8 on Windows, + // may break if wxMBConv_win32 implementation in strconv.cpp changes. + class wxMBConv_win32 : public wxMBConv + { + public: + long m_CodePage; + size_t m_minMBCharWidth; + }; + if (((wxMBConv_win32*)&wxConvLibc)->m_CodePage == CP_ACP) + ((wxMBConv_win32*)&wxConvLibc)->m_CodePage = CP_UTF8; +#endif +#endif + + // Load locale//LC_MESSAGES/bitcoin.mo language file + g_locale.Init(wxLANGUAGE_DEFAULT, 0); + g_locale.AddCatalogLookupPathPrefix("locale"); +#ifndef __WXMSW__ + g_locale.AddCatalogLookupPathPrefix("/usr/share/locale"); + g_locale.AddCatalogLookupPathPrefix("/usr/local/share/locale"); +#endif + g_locale.AddCatalog("wxstd"); // wxWidgets standard translations, if any + g_locale.AddCatalog("bitcoin"); + + return AppInit(argc, argv); +} + +int CMyApp::OnExit() +{ + Shutdown(NULL); + return wxApp::OnExit(); +} + +bool CMyApp::OnExceptionInMainLoop() +{ + try + { + throw; + } + catch (std::exception& e) + { + PrintException(&e, "CMyApp::OnExceptionInMainLoop()"); + wxLogWarning("Exception %s %s", typeid(e).name(), e.what()); + Sleep(1000); + throw; + } + catch (...) + { + PrintException(NULL, "CMyApp::OnExceptionInMainLoop()"); + wxLogWarning("Unknown exception"); + Sleep(1000); + throw; + } + return true; +} + +void CMyApp::OnUnhandledException() +{ + // this shows how we may let some exception propagate uncaught + try + { + throw; + } + catch (std::exception& e) + { + PrintException(&e, "CMyApp::OnUnhandledException()"); + wxLogWarning("Exception %s %s", typeid(e).name(), e.what()); + Sleep(1000); + throw; + } + catch (...) + { + PrintException(NULL, "CMyApp::OnUnhandledException()"); + wxLogWarning("Unknown exception"); + Sleep(1000); + throw; + } +} + +void CMyApp::OnFatalException() +{ + wxMessageBox(_("Program has crashed and will terminate. "), "Bitcoin", wxOK | wxICON_ERROR); +} -- cgit v1.2.3 From a630da64003ba1831006316a5f27b8a25b788ca2 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 11 May 2011 16:48:51 -0400 Subject: Replace CENT with new constant MIN_TX_FEE, where appropriate. MIN_TX_FEE==CENT remains true (until next commit). --- src/ui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index f2bdd49d55..962a268f39 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -196,7 +196,7 @@ int ThreadSafeMessageBox(const string& message, const string& caption, int style bool ThreadSafeAskFee(int64 nFeeRequired, const string& strCaption, wxWindow* parent) { - if (nFeeRequired < CENT || nFeeRequired <= nTransactionFee || fDaemon) + if (nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon) return true; string strMessage = strprintf( _("This transaction is over the size limit. You can still send it for a fee of %s, " -- cgit v1.2.3 From 223b6f1ba4819e9a146e7aa451d546726d0bc714 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Sun, 15 May 2011 09:11:04 +0200 Subject: make bitcoin include files more modular --- src/ui.cpp | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index 962a268f39..6e28435a35 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -7,6 +7,8 @@ #include #endif +using namespace std; +using namespace boost; DEFINE_EVENT_TYPE(wxEVT_UITHREADCALL) @@ -294,7 +296,7 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent) dResize -= 0.01; #endif wxListCtrl* pplistCtrl[] = {m_listCtrlAll, m_listCtrlSentReceived, m_listCtrlSent, m_listCtrlReceived}; - foreach(wxListCtrl* p, pplistCtrl) + BOOST_FOREACH(wxListCtrl* p, pplistCtrl) { p->InsertColumn(0, "", wxLIST_FORMAT_LEFT, dResize * 0); p->InsertColumn(1, "", wxLIST_FORMAT_LEFT, dResize * 0); @@ -528,7 +530,7 @@ string SingleLine(const string& strIn) { string strOut; bool fOneSpace = false; - foreach(unsigned char c, strIn) + BOOST_FOREACH(unsigned char c, strIn) { if (isspace(c)) { @@ -609,7 +611,7 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) if (nCredit == 0) { int64 nUnmatured = 0; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) nUnmatured += txout.GetCredit(); if (wtx.IsInMainChain()) { @@ -644,7 +646,7 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) // Received by Bitcoin Address if (!fShowReceived) return false; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { if (txout.IsMine()) { @@ -687,11 +689,11 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) else { bool fAllFromMe = true; - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) fAllFromMe = fAllFromMe && txin.IsMine(); bool fAllToMe = true; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) fAllToMe = fAllToMe && txout.IsMine(); if (fAllFromMe && fAllToMe) @@ -776,9 +778,9 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) // Mixed debit transaction, can't break down payees // bool fAllMine = true; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) fAllMine = fAllMine && txout.IsMine(); - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) fAllMine = fAllMine && txin.IsMine(); InsertLine(fNew, nIndex, hash, strSort, colour, @@ -1006,7 +1008,7 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event) string strTop; if (m_listCtrl->GetItemCount()) strTop = (string)m_listCtrl->GetItemText(0); - foreach(uint256 hash, vWalletUpdated) + BOOST_FOREACH(uint256 hash, vWalletUpdated) { map::iterator mi = mapWallet.find(hash); if (mi != mapWallet.end()) @@ -1265,7 +1267,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails if (nNet > 0) { // Credit - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { if (txout.IsMine()) { @@ -1316,7 +1318,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // Coinbase // int64 nUnmatured = 0; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) nUnmatured += txout.GetCredit(); strHTML += _("Credit: "); if (wtx.IsInMainChain()) @@ -1335,11 +1337,11 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails else { bool fAllFromMe = true; - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) fAllFromMe = fAllFromMe && txin.IsMine(); bool fAllToMe = true; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) fAllToMe = fAllToMe && txout.IsMine(); if (fAllFromMe) @@ -1347,7 +1349,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // // Debit // - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { if (txout.IsMine()) continue; @@ -1388,10 +1390,10 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // // Mixed debit transaction // - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) if (txin.IsMine()) strHTML += _("Debit: ") + FormatMoney(-txin.GetDebit()) + "
"; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) if (txout.IsMine()) strHTML += _("Credit: ") + FormatMoney(txout.GetCredit()) + "
"; } @@ -1418,10 +1420,10 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails if (fDebug) { strHTML += "

debug print

"; - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) if (txin.IsMine()) strHTML += "Debit: " + FormatMoney(-txin.GetDebit()) + "
"; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) if (txout.IsMine()) strHTML += "Credit: " + FormatMoney(txout.GetCredit()) + "
"; @@ -1431,7 +1433,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails strHTML += "
Inputs:
"; CRITICAL_BLOCK(cs_mapWallet) { - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) { COutPoint prevout = txin.prevout; map::iterator mi = mapWallet.find(prevout.hash); @@ -2341,7 +2343,7 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit CRITICAL_BLOCK(cs_mapAddressBook) { string strDefaultReceiving = (string)pframeMain->m_textCtrlAddress->GetValue(); - foreach(const PAIRTYPE(string, string)& item, mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(string, string)& item, mapAddressBook) { string strAddress = item.first; string strName = item.second; -- cgit v1.2.3 From 31f293128177e0f53331ddb3f1f5ea22176bf91c Mon Sep 17 00:00:00 2001 From: Jordan Lewis Date: Sun, 15 May 2011 22:45:35 -0500 Subject: Only include certain boost headers if necessary. --- src/ui.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index 6e28435a35..f24449cccb 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -3,6 +3,10 @@ // file license.txt or http://www.opensource.org/licenses/mit-license.php. #include "headers.h" +#include "init.h" +#include "strlcpy.h" +#include +#include #ifdef _MSC_VER #include #endif -- cgit v1.2.3 From bd39b48f19aff6cb6ebad76abc31a95b84243462 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 27 May 2011 01:25:28 +0200 Subject: Handle high DPI a bit more gracefully on Win32. #243 Not ideal, icons for send and address book don't show, just the standard bitcoin icon, and balance is still cut off, but the number is readable. --- src/ui.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index 6e28435a35..ccb8cef8c2 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -18,6 +18,13 @@ CMyTaskBarIcon* ptaskbaricon = NULL; bool fClosedToTray = false; wxLocale g_locale; +#ifdef __WXMSW__ +double nScaleX = 1.0; +double nScaleY = 1.0; +#else +static const double nScaleX = 1.0; +static const double nScaleY = 1.0; +#endif @@ -263,9 +270,10 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent) fOnSetFocusAddress = false; fRefresh = false; m_choiceFilter->SetSelection(0); - double dResize = 1.0; + double dResize = nScaleX; #ifdef __WXMSW__ SetIcon(wxICON(bitcoin)); + SetSize(dResize * GetSize().GetWidth(), nScaleY * GetSize().GetHeight()); #else SetIcon(bitcoin80_xpm); SetBackgroundColour(m_toolBar->GetBackgroundColour()); @@ -1219,6 +1227,9 @@ void CMainFrame::OnListItemActivated(wxListEvent& event) CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetailsDialogBase(parent) { +#ifdef __WXMSW__ + SetSize(nScaleX * GetSize().GetWidth(), nScaleY * GetSize().GetHeight()); +#endif CRITICAL_BLOCK(cs_mapAddressBook) { string strHTML; @@ -1633,6 +1644,8 @@ COptionsDialog::COptionsDialog(wxWindow* parent) : COptionsDialogBase(parent) SelectPage(0); #ifndef __WXMSW__ SetSize(1.0 * GetSize().GetWidth(), 1.2 * GetSize().GetHeight()); +#else + SetSize(nScaleX * GetSize().GetWidth(), nScaleY * GetSize().GetHeight()); #endif #if defined(__WXGTK__) || defined(__WXMAC_OSX__) m_checkBoxStartOnSystemStartup->SetLabel(_("&Start Bitcoin on window system startup")); @@ -1803,6 +1816,8 @@ CAboutDialog::CAboutDialog(wxWindow* parent) : CAboutDialogBase(parent) fontTmp.SetPointSize(8); m_staticTextMain->SetFont(fontTmp); SetSize(GetSize().GetWidth() + 44, GetSize().GetHeight() + 10); +#else + SetSize(nScaleX * GetSize().GetWidth(), nScaleY * GetSize().GetHeight()); #endif } @@ -1837,12 +1852,19 @@ CSendDialog::CSendDialog(wxWindow* parent, const wxString& strAddress) : CSendDi fontTmp.SetPointSize(9); m_staticTextInstructions->SetFont(fontTmp); SetSize(725, 180); +#else + SetSize(nScaleX * GetSize().GetWidth(), nScaleY * GetSize().GetHeight()); #endif // Set Icon - wxIcon iconSend; - iconSend.CopyFromBitmap(wxBitmap(send16noshadow_xpm)); - SetIcon(iconSend); + if (nScaleX == 1.0 && nScaleY == 1.0) // We don't have icons of the proper size otherwise + { + wxIcon iconSend; + iconSend.CopyFromBitmap(wxBitmap(send16noshadow_xpm)); + SetIcon(iconSend); + } + else + SetIcon(wxICON(bitcoin)); // Fixup the tab order m_buttonPaste->MoveAfterInTabOrder(m_buttonCancel); @@ -1992,6 +2014,8 @@ CSendingDialog::CSendingDialog(wxWindow* parent, const CAddress& addrIn, int64 n fWorkDone = false; #ifndef __WXMSW__ SetSize(1.2 * GetSize().GetWidth(), 1.08 * GetSize().GetHeight()); +#else + SetSize(nScaleX * GetSize().GetWidth(), nScaleY * GetSize().GetHeight()); #endif SetTitle(strprintf(_("Sending %s to %s"), FormatMoney(nPrice).c_str(), wtx.mapValue["to"].c_str())); @@ -2315,6 +2339,10 @@ void CSendingDialog::OnReply3(CDataStream& vRecv) CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInitSelected, int nPageIn, bool fDuringSendIn) : CAddressBookDialogBase(parent) { +#ifdef __WXMSW__ + SetSize(nScaleX * GetSize().GetWidth(), nScaleY * GetSize().GetHeight()); +#endif + // Set initially selected page wxNotebookEvent event; event.SetSelection(nPageIn); @@ -2326,9 +2354,14 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit m_buttonCancel->Show(false); // Set Icon - wxIcon iconAddressBook; - iconAddressBook.CopyFromBitmap(wxBitmap(addressbook16_xpm)); - SetIcon(iconAddressBook); + if (nScaleX == 1.0 && nScaleY == 1.0) // We don't have icons of the proper size otherwise + { + wxIcon iconAddressBook; + iconAddressBook.CopyFromBitmap(wxBitmap(addressbook16_xpm)); + SetIcon(iconAddressBook); + } + else + SetIcon(wxICON(bitcoin)); // Init column headers m_listCtrlSending->InsertColumn(0, _("Name"), wxLIST_FORMAT_LEFT, 200); @@ -2844,6 +2877,16 @@ bool CMyApp::OnInit() g_locale.AddCatalog("wxstd"); // wxWidgets standard translations, if any g_locale.AddCatalog("bitcoin"); +#ifdef __WXMSW__ + HDC hdc = GetDC(NULL); + if (hdc) + { + nScaleX = GetDeviceCaps(hdc, LOGPIXELSX) / 96.0; + nScaleY = GetDeviceCaps(hdc, LOGPIXELSY) / 96.0; + ReleaseDC(NULL, hdc); + } +#endif + return AppInit(argc, argv); } -- cgit v1.2.3 From af531f0449eae49e0e048218c9dccb3b3a771704 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 27 May 2011 12:37:18 +0200 Subject: Fix GUI build on UNIX. --- src/ui.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index ccb8cef8c2..cca473d82b 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1863,8 +1863,10 @@ CSendDialog::CSendDialog(wxWindow* parent, const wxString& strAddress) : CSendDi iconSend.CopyFromBitmap(wxBitmap(send16noshadow_xpm)); SetIcon(iconSend); } +#ifdef __WXMSW__ else SetIcon(wxICON(bitcoin)); +#endif // Fixup the tab order m_buttonPaste->MoveAfterInTabOrder(m_buttonCancel); @@ -2360,8 +2362,10 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit iconAddressBook.CopyFromBitmap(wxBitmap(addressbook16_xpm)); SetIcon(iconAddressBook); } +#ifdef __WXMSW__ else SetIcon(wxICON(bitcoin)); +#endif // Init column headers m_listCtrlSending->InsertColumn(0, _("Name"), wxLIST_FORMAT_LEFT, 200); -- cgit v1.2.3 From e89b9f6a2abaa120ff0fc3cea9ae364e8cbd25e4 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 1 Jun 2011 18:27:05 +0200 Subject: move wallet code to separate file This introduces two new source files, keystore.cpp and wallet.cpp with corresponding headers. Code is moved from main and db, in a preparation for a follow-up commit which introduces the classes CWallet and CKeyStore. --- src/ui.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index 0a7d45578f..72e8fe2ec8 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -3,6 +3,7 @@ // file license.txt or http://www.opensource.org/licenses/mit-license.php. #include "headers.h" +#include "db.h" #include "init.h" #include "strlcpy.h" #include -- cgit v1.2.3 From 64c7ee7e6b9c059d99aaa493c74a6703c6b0fc80 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 1 Jun 2011 18:28:20 +0200 Subject: CWallet class * A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore. * A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around. * Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter * CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions. * Some code was moved from CWalletDB to CWallet, such as handling of reserve keys. * Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument. * The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable. * Functions in main.cpp and db.cpp that are not used by other modules are marked static. * The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway. --- src/ui.cpp | 186 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 93 insertions(+), 93 deletions(-) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index 72e8fe2ec8..a49741f54f 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -240,7 +240,7 @@ void SetDefaultReceivingAddress(const string& strAddress) return; if (!mapPubKeys.count(hash160)) return; - CWalletDB().WriteDefaultKey(mapPubKeys[hash160]); + CWalletDB(pwalletMain->strWalletFile).WriteDefaultKey(mapPubKeys[hash160]); pframeMain->m_textCtrlAddress->SetValue(strAddress); } } @@ -290,7 +290,7 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent) dResize = 1.22; SetSize(dResize * GetSize().GetWidth(), 1.15 * GetSize().GetHeight()); #endif - m_staticTextBalance->SetLabel(FormatMoney(GetBalance()) + " "); + m_staticTextBalance->SetLabel(FormatMoney(pwalletMain->GetBalance()) + " "); m_listCtrl->SetFocus(); ptaskbaricon = new CMyTaskBarIcon(); #ifdef __WXMAC_OSX__ @@ -330,7 +330,7 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent) // Fill your address text box vector vchPubKey; - if (CWalletDB("r").ReadDefaultKey(vchPubKey)) + if (CWalletDB(pwalletMain->strWalletFile,"r").ReadDefaultKey(vchPubKey)) m_textCtrlAddress->SetValue(PubKeyToAddress(vchPubKey)); // Fill listctrl with wallet transactions @@ -625,7 +625,7 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) { int64 nUnmatured = 0; BOOST_FOREACH(const CTxOut& txout, wtx.vout) - nUnmatured += txout.GetCredit(); + nUnmatured += pwalletMain->GetCredit(txout); if (wtx.IsInMainChain()) { strDescription = strprintf(_("Generated (%s matures in %d more blocks)"), FormatMoney(nUnmatured).c_str(), wtx.GetBlocksToMaturity()); @@ -661,19 +661,19 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) return false; BOOST_FOREACH(const CTxOut& txout, wtx.vout) { - if (txout.IsMine()) + if (pwalletMain->IsMine(txout)) { vector vchPubKey; - if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey)) + if (ExtractPubKey(txout.scriptPubKey, pwalletMain, vchPubKey)) { - CRITICAL_BLOCK(cs_mapAddressBook) + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { //strDescription += _("Received payment to "); //strDescription += _("Received with address "); strDescription += _("Received with: "); string strAddress = PubKeyToAddress(vchPubKey); - map::iterator mi = mapAddressBook.find(strAddress); - if (mi != mapAddressBook.end() && !(*mi).second.empty()) + map::iterator mi = pwalletMain->mapAddressBook.find(strAddress); + if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) { string strLabel = (*mi).second; strDescription += strAddress.substr(0,12) + "... "; @@ -703,11 +703,11 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) { bool fAllFromMe = true; BOOST_FOREACH(const CTxIn& txin, wtx.vin) - fAllFromMe = fAllFromMe && txin.IsMine(); + fAllFromMe = fAllFromMe && pwalletMain->IsMine(txin); bool fAllToMe = true; BOOST_FOREACH(const CTxOut& txout, wtx.vout) - fAllToMe = fAllToMe && txout.IsMine(); + fAllToMe = fAllToMe && pwalletMain->IsMine(txout); if (fAllFromMe && fAllToMe) { @@ -733,7 +733,7 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) for (int nOut = 0; nOut < wtx.vout.size(); nOut++) { const CTxOut& txout = wtx.vout[nOut]; - if (txout.IsMine()) + if (pwalletMain->IsMine(txout)) continue; string strAddress; @@ -751,9 +751,9 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) } string strDescription = _("To: "); - CRITICAL_BLOCK(cs_mapAddressBook) - if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty()) - strDescription += mapAddressBook[strAddress] + " "; + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) + if (pwalletMain->mapAddressBook.count(strAddress) && !pwalletMain->mapAddressBook[strAddress].empty()) + strDescription += pwalletMain->mapAddressBook[strAddress] + " "; strDescription += strAddress; if (!mapValue["message"].empty()) { @@ -792,9 +792,9 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) // bool fAllMine = true; BOOST_FOREACH(const CTxOut& txout, wtx.vout) - fAllMine = fAllMine && txout.IsMine(); + fAllMine = fAllMine && pwalletMain->IsMine(txout); BOOST_FOREACH(const CTxIn& txin, wtx.vin) - fAllMine = fAllMine && txin.IsMine(); + fAllMine = fAllMine && pwalletMain->IsMine(txin); InsertLine(fNew, nIndex, hash, strSort, colour, strStatus, @@ -821,16 +821,16 @@ void CMainFrame::OnIdle(wxIdleEvent& event) // Collect list of wallet transactions and sort newest first bool fEntered = false; vector > vSorted; - TRY_CRITICAL_BLOCK(cs_mapWallet) + TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { printf("RefreshListCtrl starting\n"); fEntered = true; fRefreshListCtrl = false; - vWalletUpdated.clear(); + pwalletMain->vWalletUpdated.clear(); // Do the newest transactions first - vSorted.reserve(mapWallet.size()); - for (map::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) + vSorted.reserve(pwalletMain->mapWallet.size()); + for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) { const CWalletTx& wtx = (*it).second; unsigned int nTime = UINT_MAX - wtx.GetTxTime(); @@ -849,12 +849,12 @@ void CMainFrame::OnIdle(wxIdleEvent& event) if (fShutdown) return; bool fEntered = false; - TRY_CRITICAL_BLOCK(cs_mapWallet) + TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { fEntered = true; uint256& hash = vSorted[i++].second; - map::iterator mi = mapWallet.find(hash); - if (mi != mapWallet.end()) + map::iterator mi = pwalletMain->mapWallet.find(hash); + if (mi != pwalletMain->mapWallet.end()) InsertTransaction((*mi).second, true); } if (!fEntered || i == 100 || i % 500 == 0) @@ -872,10 +872,10 @@ void CMainFrame::OnIdle(wxIdleEvent& event) static int64 nLastTime; if (GetTime() > nLastTime + 30) { - TRY_CRITICAL_BLOCK(cs_mapWallet) + TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { nLastTime = GetTime(); - for (map::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) + for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) { CWalletTx& wtx = (*it).second; if (wtx.nTimeDisplayed && wtx.nTimeDisplayed != wtx.GetTxTime()) @@ -896,7 +896,7 @@ void CMainFrame::RefreshStatusColumn() if (nTop == nLastTop && pindexLastBest == pindexBest) return; - TRY_CRITICAL_BLOCK(cs_mapWallet) + TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { int nStart = nTop; int nEnd = min(nStart + 100, m_listCtrl->GetItemCount()); @@ -916,8 +916,8 @@ void CMainFrame::RefreshStatusColumn() for (int nIndex = nStart; nIndex < min(nEnd, m_listCtrl->GetItemCount()); nIndex++) { uint256 hash((string)GetItemText(m_listCtrl, nIndex, 1)); - map::iterator mi = mapWallet.find(hash); - if (mi == mapWallet.end()) + map::iterator mi = pwalletMain->mapWallet.find(hash); + if (mi == pwalletMain->mapWallet.end()) { printf("CMainFrame::RefreshStatusColumn() : tx not found in mapWallet\n"); continue; @@ -1014,41 +1014,41 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event) nLastRepaintTime = GetTimeMillis(); // Update listctrl contents - if (!vWalletUpdated.empty()) + if (!pwalletMain->vWalletUpdated.empty()) { - TRY_CRITICAL_BLOCK(cs_mapWallet) + TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { string strTop; if (m_listCtrl->GetItemCount()) strTop = (string)m_listCtrl->GetItemText(0); - BOOST_FOREACH(uint256 hash, vWalletUpdated) + BOOST_FOREACH(uint256 hash, pwalletMain->vWalletUpdated) { - map::iterator mi = mapWallet.find(hash); - if (mi != mapWallet.end()) + map::iterator mi = pwalletMain->mapWallet.find(hash); + if (mi != pwalletMain->mapWallet.end()) InsertTransaction((*mi).second, false); } - vWalletUpdated.clear(); + pwalletMain->vWalletUpdated.clear(); if (m_listCtrl->GetItemCount() && strTop != (string)m_listCtrl->GetItemText(0)) m_listCtrl->ScrollList(0, INT_MIN/2); } } // Balance total - TRY_CRITICAL_BLOCK(cs_mapWallet) + TRY_CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { fPaintedBalance = true; - m_staticTextBalance->SetLabel(FormatMoney(GetBalance()) + " "); + m_staticTextBalance->SetLabel(FormatMoney(pwalletMain->GetBalance()) + " "); // Count hidden and multi-line transactions nTransactionCount = 0; - for (map::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) + for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) { CWalletTx& wtx = (*it).second; nTransactionCount += wtx.nLinesDisplayed; } } } - if (!vWalletUpdated.empty() || !fPaintedBalance) + if (!pwalletMain->vWalletUpdated.empty() || !fPaintedBalance) nNeedRepaint++; // Update status column of visible items only @@ -1074,7 +1074,7 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event) m_statusBar->SetStatusText(strStatus, 2); // Update receiving address - string strDefaultAddress = PubKeyToAddress(vchDefaultKey); + string strDefaultAddress = PubKeyToAddress(pwalletMain->vchDefaultKey); if (m_textCtrlAddress->GetValue() != strDefaultAddress) m_textCtrlAddress->SetValue(strDefaultAddress); } @@ -1183,10 +1183,10 @@ void CMainFrame::OnButtonNew(wxCommandEvent& event) string strName = dialog.GetValue(); // Generate new key - string strAddress = PubKeyToAddress(GetKeyFromKeyPool()); + string strAddress = PubKeyToAddress(pwalletMain->GetKeyFromKeyPool()); // Save - SetAddressBookName(strAddress, strName); + pwalletMain->SetAddressBookName(strAddress, strName); SetDefaultReceivingAddress(strAddress); } @@ -1204,10 +1204,10 @@ void CMainFrame::OnListItemActivated(wxListEvent& event) { uint256 hash((string)GetItemText(m_listCtrl, event.GetIndex(), 1)); CWalletTx wtx; - CRITICAL_BLOCK(cs_mapWallet) + CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { - map::iterator mi = mapWallet.find(hash); - if (mi == mapWallet.end()) + map::iterator mi = pwalletMain->mapWallet.find(hash); + if (mi == pwalletMain->mapWallet.end()) { printf("CMainFrame::OnListItemActivated() : tx not found in mapWallet\n"); return; @@ -1235,7 +1235,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails #ifdef __WXMSW__ SetSize(nScaleX * GetSize().GetWidth(), nScaleY * GetSize().GetHeight()); #endif - CRITICAL_BLOCK(cs_mapAddressBook) + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { string strHTML; strHTML.reserve(4000); @@ -1285,19 +1285,19 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // Credit BOOST_FOREACH(const CTxOut& txout, wtx.vout) { - if (txout.IsMine()) + if (pwalletMain->IsMine(txout)) { vector vchPubKey; - if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey)) + if (ExtractPubKey(txout.scriptPubKey, pwalletMain, vchPubKey)) { string strAddress = PubKeyToAddress(vchPubKey); - if (mapAddressBook.count(strAddress)) + if (pwalletMain->mapAddressBook.count(strAddress)) { strHTML += string() + _("From: ") + _("unknown") + "
"; strHTML += _("To: "); strHTML += HtmlEscape(strAddress); - if (!mapAddressBook[strAddress].empty()) - strHTML += _(" (yours, label: ") + mapAddressBook[strAddress] + ")"; + if (!pwalletMain->mapAddressBook[strAddress].empty()) + strHTML += _(" (yours, label: ") + pwalletMain->mapAddressBook[strAddress] + ")"; else strHTML += _(" (yours)"); strHTML += "
"; @@ -1319,8 +1319,8 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // Online transaction strAddress = wtx.mapValue["to"]; strHTML += _("To: "); - if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty()) - strHTML += mapAddressBook[strAddress] + " "; + if (pwalletMain->mapAddressBook.count(strAddress) && !pwalletMain->mapAddressBook[strAddress].empty()) + strHTML += pwalletMain->mapAddressBook[strAddress] + " "; strHTML += HtmlEscape(strAddress) + "
"; } @@ -1335,7 +1335,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // int64 nUnmatured = 0; BOOST_FOREACH(const CTxOut& txout, wtx.vout) - nUnmatured += txout.GetCredit(); + nUnmatured += pwalletMain->GetCredit(txout); strHTML += _("Credit: "); if (wtx.IsInMainChain()) strHTML += strprintf(_("(%s matures in %d more blocks)"), FormatMoney(nUnmatured).c_str(), wtx.GetBlocksToMaturity()); @@ -1354,11 +1354,11 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails { bool fAllFromMe = true; BOOST_FOREACH(const CTxIn& txin, wtx.vin) - fAllFromMe = fAllFromMe && txin.IsMine(); + fAllFromMe = fAllFromMe && pwalletMain->IsMine(txin); bool fAllToMe = true; BOOST_FOREACH(const CTxOut& txout, wtx.vout) - fAllToMe = fAllToMe && txout.IsMine(); + fAllToMe = fAllToMe && pwalletMain->IsMine(txout); if (fAllFromMe) { @@ -1367,7 +1367,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // BOOST_FOREACH(const CTxOut& txout, wtx.vout) { - if (txout.IsMine()) + if (pwalletMain->IsMine(txout)) continue; if (wtx.mapValue["to"].empty()) @@ -1378,8 +1378,8 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails { string strAddress = Hash160ToAddress(hash160); strHTML += _("To: "); - if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty()) - strHTML += mapAddressBook[strAddress] + " "; + if (pwalletMain->mapAddressBook.count(strAddress) && !pwalletMain->mapAddressBook[strAddress].empty()) + strHTML += pwalletMain->mapAddressBook[strAddress] + " "; strHTML += strAddress; strHTML += "
"; } @@ -1407,11 +1407,11 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // Mixed debit transaction // BOOST_FOREACH(const CTxIn& txin, wtx.vin) - if (txin.IsMine()) - strHTML += _("Debit: ") + FormatMoney(-txin.GetDebit()) + "
"; + if (pwalletMain->IsMine(txin)) + strHTML += _("Debit: ") + FormatMoney(-pwalletMain->GetDebit(txin)) + "
"; BOOST_FOREACH(const CTxOut& txout, wtx.vout) - if (txout.IsMine()) - strHTML += _("Credit: ") + FormatMoney(txout.GetCredit()) + "
"; + if (pwalletMain->IsMine(txout)) + strHTML += _("Credit: ") + FormatMoney(pwalletMain->GetCredit(txout)) + "
"; } } @@ -1437,30 +1437,30 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails { strHTML += "

debug print

"; BOOST_FOREACH(const CTxIn& txin, wtx.vin) - if (txin.IsMine()) - strHTML += "Debit: " + FormatMoney(-txin.GetDebit()) + "
"; + if (pwalletMain->IsMine(txin)) + strHTML += "Debit: " + FormatMoney(-pwalletMain->GetDebit(txin)) + "
"; BOOST_FOREACH(const CTxOut& txout, wtx.vout) - if (txout.IsMine()) - strHTML += "Credit: " + FormatMoney(txout.GetCredit()) + "
"; + if (pwalletMain->IsMine(txout)) + strHTML += "Credit: " + FormatMoney(pwalletMain->GetCredit(txout)) + "
"; strHTML += "
Transaction:
"; strHTML += HtmlEscape(wtx.ToString(), true); strHTML += "
Inputs:
"; - CRITICAL_BLOCK(cs_mapWallet) + CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { BOOST_FOREACH(const CTxIn& txin, wtx.vin) { COutPoint prevout = txin.prevout; - map::iterator mi = mapWallet.find(prevout.hash); - if (mi != mapWallet.end()) + map::iterator mi = pwalletMain->mapWallet.find(prevout.hash); + if (mi != pwalletMain->mapWallet.end()) { const CWalletTx& prev = (*mi).second; if (prevout.n < prev.vout.size()) { strHTML += HtmlEscape(prev.ToString(), true); strHTML += "    " + FormatTxStatus(prev) + ", "; - strHTML = strHTML + "IsMine=" + (prev.vout[prevout.n].IsMine() ? "true" : "false") + "
"; + strHTML = strHTML + "IsMine=" + (pwalletMain->IsMine(prev.vout[prevout.n]) ? "true" : "false") + "
"; } } } @@ -1751,7 +1751,7 @@ void COptionsDialog::OnButtonCancel(wxCommandEvent& event) void COptionsDialog::OnButtonApply(wxCommandEvent& event) { - CWalletDB walletdb; + CWalletDB walletdb(pwalletMain->strWalletFile); int64 nPrevTransactionFee = nTransactionFee; if (ParseMoney(m_textCtrlTransactionFee->GetValue(), nTransactionFee) && nTransactionFee != nPrevTransactionFee) @@ -1928,12 +1928,12 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event) wxMessageBox(_("Error in amount "), _("Send Coins")); return; } - if (nValue > GetBalance()) + if (nValue > pwalletMain->GetBalance()) { wxMessageBox(_("Amount exceeds your balance "), _("Send Coins")); return; } - if (nValue + nTransactionFee > GetBalance()) + if (nValue + nTransactionFee > pwalletMain->GetBalance()) { wxMessageBox(string(_("Total exceeds your balance when the ")) + FormatMoney(nTransactionFee) + _(" transaction fee is included "), _("Send Coins")); return; @@ -1951,7 +1951,7 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event) CScript scriptPubKey; scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG; - string strError = SendMoney(scriptPubKey, nValue, wtx, true); + string strError = pwalletMain->SendMoney(scriptPubKey, nValue, wtx, true); if (strError == "") wxMessageBox(_("Payment sent "), _("Sending...")); else if (strError == "ABORTED") @@ -1983,9 +1983,9 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event) return; } - CRITICAL_BLOCK(cs_mapAddressBook) - if (!mapAddressBook.count(strAddress)) - SetAddressBookName(strAddress, ""); + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) + if (!pwalletMain->mapAddressBook.count(strAddress)) + pwalletMain->SetAddressBookName(strAddress, ""); EndModal(true); } @@ -2169,7 +2169,7 @@ void SendingDialogStartTransfer(void* parg) void CSendingDialog::StartTransfer() { // Make sure we have enough money - if (nPrice + nTransactionFee > GetBalance()) + if (nPrice + nTransactionFee > pwalletMain->GetBalance()) { Error(_("Insufficient funds")); return; @@ -2240,16 +2240,16 @@ void CSendingDialog::OnReply2(CDataStream& vRecv) // Pay if (!Status(_("Creating transaction..."))) return; - if (nPrice + nTransactionFee > GetBalance()) + if (nPrice + nTransactionFee > pwalletMain->GetBalance()) { Error(_("Insufficient funds")); return; } - CReserveKey reservekey; + CReserveKey reservekey(pwalletMain); int64 nFeeRequired; - if (!CreateTransaction(scriptPubKey, nPrice, wtx, reservekey, nFeeRequired)) + if (!pwalletMain->CreateTransaction(scriptPubKey, nPrice, wtx, reservekey, nFeeRequired)) { - if (nPrice + nFeeRequired > GetBalance()) + if (nPrice + nFeeRequired > pwalletMain->GetBalance()) Error(strprintf(_("This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds"), FormatMoney(nFeeRequired).c_str())); else Error(_("Transaction creation failed")); @@ -2287,7 +2287,7 @@ void CSendingDialog::OnReply2(CDataStream& vRecv) return; // Commit - if (!CommitTransaction(wtx, reservekey)) + if (!pwalletMain->CommitTransaction(wtx, reservekey)) { Error(_("The transaction was rejected. This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.")); return; @@ -2381,11 +2381,11 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit m_listCtrlReceiving->SetFocus(); // Fill listctrl with address book data - CRITICAL_BLOCK(cs_mapKeys) - CRITICAL_BLOCK(cs_mapAddressBook) + CRITICAL_BLOCK(pwalletMain->cs_mapKeys) + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { string strDefaultReceiving = (string)pframeMain->m_textCtrlAddress->GetValue(); - BOOST_FOREACH(const PAIRTYPE(string, string)& item, mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) { string strAddress = item.first; string strName = item.second; @@ -2444,7 +2444,7 @@ void CAddressBookDialog::OnListEndLabelEdit(wxListEvent& event) if (event.IsEditCancelled()) return; string strAddress = (string)GetItemText(m_listCtrl, event.GetIndex(), 1); - SetAddressBookName(strAddress, string(event.GetText())); + pwalletMain->SetAddressBookName(strAddress, string(event.GetText())); pframeMain->RefreshListCtrl(); } @@ -2479,7 +2479,7 @@ void CAddressBookDialog::OnButtonDelete(wxCommandEvent& event) if (m_listCtrl->GetItemState(nIndex, wxLIST_STATE_SELECTED)) { string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1); - CWalletDB().EraseName(strAddress); + CWalletDB(pwalletMain->strWalletFile).EraseName(strAddress); m_listCtrl->DeleteItem(nIndex); } } @@ -2539,8 +2539,8 @@ void CAddressBookDialog::OnButtonEdit(wxCommandEvent& event) // Write back if (strAddress != strAddressOrg) - CWalletDB().EraseName(strAddressOrg); - SetAddressBookName(strAddress, strName); + CWalletDB(pwalletMain->strWalletFile).EraseName(strAddressOrg); + pwalletMain->SetAddressBookName(strAddress, strName); m_listCtrl->SetItem(nIndex, 1, strAddress); m_listCtrl->SetItemText(nIndex, strName); pframeMain->RefreshListCtrl(); @@ -2576,11 +2576,11 @@ void CAddressBookDialog::OnButtonNew(wxCommandEvent& event) strName = dialog.GetValue(); // Generate new key - strAddress = PubKeyToAddress(GetKeyFromKeyPool()); + strAddress = PubKeyToAddress(pwalletMain->GetKeyFromKeyPool()); } // Add to list and select it - SetAddressBookName(strAddress, strName); + pwalletMain->SetAddressBookName(strAddress, strName); int nIndex = InsertLine(m_listCtrl, strName, strAddress); SetSelection(m_listCtrl, nIndex); m_listCtrl->SetFocus(); -- cgit v1.2.3 From 4d410cfce967a42cca7db13288b72baec29423d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gimenez?= Date: Mon, 27 Jun 2011 23:22:30 +0200 Subject: Fix AddressBook syncrhonization between a CWallet and CWalletDB This problem was reported independently by laanwj in Issue #350. --- src/ui.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index a49741f54f..2cbcfd5bd8 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1186,7 +1186,8 @@ void CMainFrame::OnButtonNew(wxCommandEvent& event) string strAddress = PubKeyToAddress(pwalletMain->GetKeyFromKeyPool()); // Save - pwalletMain->SetAddressBookName(strAddress, strName); + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) + pwalletMain->SetAddressBookName(strAddress, strName); SetDefaultReceivingAddress(strAddress); } @@ -2444,7 +2445,8 @@ void CAddressBookDialog::OnListEndLabelEdit(wxListEvent& event) if (event.IsEditCancelled()) return; string strAddress = (string)GetItemText(m_listCtrl, event.GetIndex(), 1); - pwalletMain->SetAddressBookName(strAddress, string(event.GetText())); + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) + pwalletMain->SetAddressBookName(strAddress, string(event.GetText())); pframeMain->RefreshListCtrl(); } @@ -2479,7 +2481,8 @@ void CAddressBookDialog::OnButtonDelete(wxCommandEvent& event) if (m_listCtrl->GetItemState(nIndex, wxLIST_STATE_SELECTED)) { string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1); - CWalletDB(pwalletMain->strWalletFile).EraseName(strAddress); + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) + pwalletMain->DelAddressBookName(strAddress); m_listCtrl->DeleteItem(nIndex); } } @@ -2538,9 +2541,12 @@ void CAddressBookDialog::OnButtonEdit(wxCommandEvent& event) } // Write back - if (strAddress != strAddressOrg) - CWalletDB(pwalletMain->strWalletFile).EraseName(strAddressOrg); - pwalletMain->SetAddressBookName(strAddress, strName); + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) + { + if (strAddress != strAddressOrg) + pwalletMain->DelAddressBookName(strAddressOrg); + pwalletMain->SetAddressBookName(strAddress, strName); + } m_listCtrl->SetItem(nIndex, 1, strAddress); m_listCtrl->SetItemText(nIndex, strName); pframeMain->RefreshListCtrl(); @@ -2580,7 +2586,8 @@ void CAddressBookDialog::OnButtonNew(wxCommandEvent& event) } // Add to list and select it - pwalletMain->SetAddressBookName(strAddress, strName); + CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) + pwalletMain->SetAddressBookName(strAddress, strName); int nIndex = InsertLine(m_listCtrl, strName, strAddress); SetSelection(m_listCtrl, nIndex); m_listCtrl->SetFocus(); -- cgit v1.2.3 From e9f61c8787af99c49678ba4bc182b24b8fdba0c4 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 5 Jul 2011 17:42:44 +0200 Subject: Fix synchronization of default key --- src/ui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index 2cbcfd5bd8..9b84fb9e6b 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -240,7 +240,7 @@ void SetDefaultReceivingAddress(const string& strAddress) return; if (!mapPubKeys.count(hash160)) return; - CWalletDB(pwalletMain->strWalletFile).WriteDefaultKey(mapPubKeys[hash160]); + pwalletMain->SetDefaultKey(mapPubKeys[hash160]); pframeMain->m_textCtrlAddress->SetValue(strAddress); } } -- cgit v1.2.3 From fa989f42c1e2a927267ef8839563e21a2cd4b712 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Sat, 9 Jul 2011 10:06:49 +0200 Subject: remove magic number: change threshold for nLockTime to constant --- src/ui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui.cpp') diff --git a/src/ui.cpp b/src/ui.cpp index 9b84fb9e6b..ff0b4afb55 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -522,7 +522,7 @@ string FormatTxStatus(const CWalletTx& wtx) // Status if (!wtx.IsFinal()) { - if (wtx.nLockTime < 500000000) + if (wtx.nLockTime < LOCKTIME_THRESHOLD) return strprintf(_("Open for %d blocks"), nBestHeight - wtx.nLockTime); else return strprintf(_("Open until %s"), DateTimeStr(wtx.nLockTime).c_str()); -- cgit v1.2.3