add better windows7/vista look by nico_w
[novacoin.git] / src / qt / bitcoin.cpp
1 /*
2  * W.J. van der Laan 2011
3  */
4 #include "bitcoingui.h"
5 #include "clientmodel.h"
6 #include "walletmodel.h"
7 #include "qtwin.h"
8
9 #include "headers.h"
10 #include "init.h"
11
12 #include <QApplication>
13 #include <QMessageBox>
14 #include <QThread>
15 #include <QLocale>
16 #include <QTranslator>
17
18 // Need a global reference for the notifications to find the GUI
19 BitcoinGUI *guiref;
20
21 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
22 {
23     // Message from main thread
24     if(guiref)
25     {
26         guiref->error(QString::fromStdString(caption),
27                       QString::fromStdString(message));
28     }
29     else
30     {
31         QMessageBox::critical(0, QString::fromStdString(caption),
32             QString::fromStdString(message),
33             QMessageBox::Ok, QMessageBox::Ok);
34     }
35     return 4;
36 }
37
38 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
39 {
40     // Message from network thread
41     if(guiref)
42     {
43         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
44                                    Q_ARG(QString, QString::fromStdString(caption)),
45                                    Q_ARG(QString, QString::fromStdString(message)));
46     }
47     else
48     {
49         printf("%s: %s\n", caption.c_str(), message.c_str());
50         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
51     }
52     return 4;
53 }
54
55 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
56 {
57     if(!guiref)
58         return false;
59     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
60         return true;
61     bool payFee = false;
62
63     // Call slot on GUI thread.
64     // If called from another thread, use a blocking QueuedConnection.
65     Qt::ConnectionType connectionType = Qt::DirectConnection;
66     if(QThread::currentThread() != QCoreApplication::instance()->thread())
67     {
68         connectionType = Qt::BlockingQueuedConnection;
69     }
70
71     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
72                                Q_ARG(qint64, nFeeRequired),
73                                Q_ARG(bool*, &payFee));
74
75     return payFee;
76 }
77
78 void CalledSetStatusBar(const std::string& strText, int nField)
79 {
80     // Only used for built-in mining, which is disabled, simple ignore
81 }
82
83 void UIThreadCall(boost::function0<void> fn)
84 {
85     // Only used for built-in mining, which is disabled, simple ignore
86 }
87
88 void MainFrameRepaint()
89 {
90 }
91
92 /*
93    Translate string to current locale using Qt.
94  */
95 std::string _(const char* psz)
96 {
97     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
98 }
99
100 int main(int argc, char *argv[])
101 {
102     Q_INIT_RESOURCE(bitcoin);
103     QApplication app(argc, argv);
104
105     // Load language file for system locale
106     QString locale = QLocale::system().name();
107     QTranslator translator;
108     translator.load("bitcoin_"+locale);
109     app.installTranslator(&translator);
110
111     app.setQuitOnLastWindowClosed(false);
112
113     try
114     {
115         if(AppInit2(argc, argv))
116         {
117             {
118                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
119                 // calling shutdown.
120                 BitcoinGUI window;
121                 ClientModel clientModel(pwalletMain);
122                 WalletModel walletModel(pwalletMain);
123
124                 guiref = &window;
125                 window.setClientModel(&clientModel);
126                 window.setWalletModel(&walletModel);
127
128 #ifdef Q_WS_WIN32
129                 // Windows-specific customization
130                 window.setAttribute(Qt::WA_TranslucentBackground);
131                 window.setAttribute(Qt::WA_NoSystemBackground, false);
132                 QPalette pal = window.palette();
133                 QColor bg = pal.window().color();
134                 bg.setAlpha(0);
135                 pal.setColor(QPalette::Window, bg);
136                 window.setPalette(pal);
137                 window.ensurePolished();
138                 window.setAttribute(Qt::WA_StyledBackground, false);
139 #endif
140                 if (QtWin::isCompositionEnabled())
141                 {
142                     QtWin::extendFrameIntoClientArea(&window);
143                     window.setContentsMargins(0, 0, 0, 0);
144                 }
145
146                 window.show();
147
148                 app.exec();
149
150                 guiref = 0;
151             }
152             Shutdown(NULL);
153         }
154         else
155         {
156             return 1;
157         }
158     } catch (std::exception& e) {
159         PrintException(&e, "Runaway exception");
160     } catch (...) {
161         PrintException(NULL, "Runaway exception");
162     }
163     return 0;
164 }