1 |
// qsampler.cpp |
// qsampler.cpp |
2 |
// |
// |
3 |
/**************************************************************************** |
/**************************************************************************** |
4 |
Copyright (C) 2004-2010, rncbc aka Rui Nuno Capela. All rights reserved. |
Copyright (C) 2004-2017, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
Copyright (C) 2007, 2008 Christian Schoenebeck |
Copyright (C) 2007,2008,2015 Christian Schoenebeck |
6 |
|
|
7 |
This program is free software; you can redistribute it and/or |
This program is free software; you can redistribute it and/or |
8 |
modify it under the terms of the GNU General Public License |
modify it under the terms of the GNU General Public License |
28 |
#include <QLibraryInfo> |
#include <QLibraryInfo> |
29 |
#include <QTranslator> |
#include <QTranslator> |
30 |
#include <QLocale> |
#include <QLocale> |
31 |
|
|
32 |
#if defined(__APPLE__) // Toshi Nagata 20080105 |
#if defined(__APPLE__) // Toshi Nagata 20080105 |
33 |
#include <QDir> |
#include <QDir> |
34 |
#endif |
#endif |
35 |
|
|
36 |
#if QT_VERSION < 0x040300 |
#ifndef CONFIG_PREFIX |
37 |
#define lighter(x) light(x) |
#define CONFIG_PREFIX "/usr/local" |
|
#define darker(x) dark(x) |
|
38 |
#endif |
#endif |
39 |
|
|
40 |
#define CONFIG_QUOTE1(x) #x |
#ifndef CONFIG_DATADIR |
41 |
#define CONFIG_QUOTED(x) CONFIG_QUOTE1(x) |
#define CONFIG_DATADIR CONFIG_PREFIX "/share" |
|
|
|
|
#if defined(DATADIR) |
|
|
#define CONFIG_DATADIR CONFIG_QUOTED(DATADIR) |
|
|
#else |
|
|
#define CONFIG_DATADIR CONFIG_PREFIX "/share" |
|
|
#endif |
|
|
|
|
|
#if defined(LOCALEDIR) |
|
|
#define CONFIG_LOCALEDIR CONFIG_QUOTED(LOCALEDIR) |
|
|
#else |
|
|
#define CONFIG_LOCALEDIR CONFIG_DATADIR "/locale" |
|
42 |
#endif |
#endif |
43 |
|
|
44 |
#if WIN32 |
#if WIN32 |
52 |
// Singleton application instance stuff (Qt/X11 only atm.) |
// Singleton application instance stuff (Qt/X11 only atm.) |
53 |
// |
// |
54 |
|
|
55 |
|
#if QT_VERSION < 0x050000 |
56 |
#if defined(Q_WS_X11) |
#if defined(Q_WS_X11) |
57 |
|
#define CONFIG_X11 |
58 |
|
#endif |
59 |
|
#else |
60 |
|
#if defined(QT_X11EXTRAS_LIB) |
61 |
|
#define CONFIG_X11 |
62 |
|
#endif |
63 |
|
#endif |
64 |
|
|
65 |
|
|
66 |
|
#ifdef CONFIG_X11 |
67 |
|
#ifdef CONFIG_XUNIQUE |
68 |
|
|
69 |
#include <QX11Info> |
#include <QX11Info> |
70 |
|
|
71 |
#include <X11/Xatom.h> |
#include <X11/Xatom.h> |
72 |
#include <X11/Xlib.h> |
#include <X11/Xlib.h> |
73 |
|
|
74 |
#define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique" |
#define QSAMPLER_XUNIQUE "qsamplerApplication" |
75 |
|
|
76 |
|
#if QT_VERSION >= 0x050100 |
77 |
|
|
78 |
|
#include <xcb/xcb.h> |
79 |
|
#include <xcb/xproto.h> |
80 |
|
|
81 |
|
#include <QAbstractNativeEventFilter> |
82 |
|
|
83 |
|
class qsamplerApplication; |
84 |
|
|
85 |
|
class qsamplerXcbEventFilter : public QAbstractNativeEventFilter |
86 |
|
{ |
87 |
|
public: |
88 |
|
|
89 |
|
// Constructor. |
90 |
|
qsamplerXcbEventFilter(qsamplerApplication *pApp) |
91 |
|
: QAbstractNativeEventFilter(), m_pApp(pApp) {} |
92 |
|
|
93 |
|
// XCB event filter (virtual processor). |
94 |
|
bool nativeEventFilter(const QByteArray& eventType, void *message, long *); |
95 |
|
|
96 |
|
private: |
97 |
|
|
98 |
|
// Instance variable. |
99 |
|
qsamplerApplication *m_pApp; |
100 |
|
}; |
101 |
|
|
102 |
#endif |
#endif |
103 |
|
|
104 |
|
#endif // CONFIG_XUNIQUE |
105 |
|
#endif // CONFIG_X11 |
106 |
|
|
107 |
|
|
108 |
class qsamplerApplication : public QApplication |
class qsamplerApplication : public QApplication |
109 |
{ |
{ |
110 |
public: |
public: |
111 |
|
|
112 |
// Constructor. |
// Constructor. |
113 |
qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv), |
qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv), |
114 |
m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0) |
m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0) |
115 |
{ |
{ |
116 |
// Load translation support. |
// Load translation support. |
117 |
QLocale loc; |
QLocale loc; |
123 |
if (m_pQtTranslator->load(sLocName, sLocPath)) { |
if (m_pQtTranslator->load(sLocName, sLocPath)) { |
124 |
QApplication::installTranslator(m_pQtTranslator); |
QApplication::installTranslator(m_pQtTranslator); |
125 |
} else { |
} else { |
126 |
#ifdef RELATIVE_LOCALE_DIR |
#ifdef RELATIVE_LOCALE_DIR |
127 |
sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR; |
sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR; |
128 |
if (m_pQtTranslator->load(sLocName, sLocPath)) { |
if (m_pQtTranslator->load(sLocName, sLocPath)) { |
129 |
QApplication::installTranslator(m_pQtTranslator); |
QApplication::installTranslator(m_pQtTranslator); |
130 |
} else { |
} else { |
131 |
#endif |
#endif |
132 |
delete m_pQtTranslator; |
delete m_pQtTranslator; |
133 |
m_pQtTranslator = 0; |
m_pQtTranslator = 0; |
134 |
#ifdef CONFIG_DEBUG |
#ifdef CONFIG_DEBUG |
135 |
qWarning("Warning: no translation found for '%s' locale: %s/%s.qm", |
qWarning("Warning: no translation found for '%s' locale: %s/%s.qm", |
136 |
loc.name().toUtf8().constData(), |
loc.name().toUtf8().constData(), |
137 |
sLocPath.toUtf8().constData(), |
sLocPath.toUtf8().constData(), |
138 |
sLocName.toUtf8().constData()); |
sLocName.toUtf8().constData()); |
139 |
#endif |
#endif |
140 |
#ifdef RELATIVE_LOCALE_DIR |
#ifdef RELATIVE_LOCALE_DIR |
141 |
} |
} |
142 |
#endif |
#endif |
143 |
} |
} |
144 |
// Try own application translation... |
// Try own application translation... |
145 |
m_pMyTranslator = new QTranslator(this); |
m_pMyTranslator = new QTranslator(this); |
147 |
if (m_pMyTranslator->load(sLocName, sLocPath)) { |
if (m_pMyTranslator->load(sLocName, sLocPath)) { |
148 |
QApplication::installTranslator(m_pMyTranslator); |
QApplication::installTranslator(m_pMyTranslator); |
149 |
} else { |
} else { |
150 |
#ifdef RELATIVE_LOCALE_DIR |
#ifdef RELATIVE_LOCALE_DIR |
151 |
sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR; |
sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR; |
152 |
#else |
#else |
153 |
sLocPath = CONFIG_LOCALEDIR; |
sLocPath = CONFIG_DATADIR "/qsampler/translations"; |
154 |
#endif |
#endif |
155 |
if (m_pMyTranslator->load(sLocName, sLocPath)) { |
if (m_pMyTranslator->load(sLocName, sLocPath)) { |
156 |
QApplication::installTranslator(m_pMyTranslator); |
QApplication::installTranslator(m_pMyTranslator); |
157 |
} else { |
} else { |
158 |
delete m_pMyTranslator; |
delete m_pMyTranslator; |
159 |
m_pMyTranslator = 0; |
m_pMyTranslator = 0; |
160 |
#ifdef CONFIG_DEBUG |
#ifdef CONFIG_DEBUG |
161 |
qWarning("Warning: no translation found for '%s' locale: %s/%s.qm", |
qWarning("Warning: no translation found for '%s' locale: %s/%s.qm", |
162 |
loc.name().toUtf8().constData(), |
loc.name().toUtf8().constData(), |
163 |
sLocPath.toUtf8().constData(), |
sLocPath.toUtf8().constData(), |
164 |
sLocName.toUtf8().constData()); |
sLocName.toUtf8().constData()); |
165 |
#endif |
#endif |
166 |
} |
} |
167 |
} |
} |
168 |
} |
} |
169 |
#if defined(Q_WS_X11) |
#ifdef CONFIG_X11 |
170 |
|
#ifdef CONFIG_XUNIQUE |
171 |
|
// Instance uniqueness initialization... |
172 |
m_pDisplay = QX11Info::display(); |
m_pDisplay = QX11Info::display(); |
173 |
m_aUnique = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false); |
m_aUnique = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false); |
174 |
XGrabServer(m_pDisplay); |
XGrabServer(m_pDisplay); |
175 |
m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique); |
m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique); |
176 |
XUngrabServer(m_pDisplay); |
XUngrabServer(m_pDisplay); |
177 |
|
#if QT_VERSION >= 0x050100 |
178 |
|
m_pXcbEventFilter = new qsamplerXcbEventFilter(this); |
179 |
|
installNativeEventFilter(m_pXcbEventFilter); |
180 |
#endif |
#endif |
181 |
|
#endif // CONFIG_XUNIQUE |
182 |
|
#endif // CONFIG_X11 |
183 |
} |
} |
184 |
|
|
185 |
// Destructor. |
// Destructor. |
186 |
~qsamplerApplication() |
~qsamplerApplication() |
187 |
{ |
{ |
188 |
|
#ifdef CONFIG_X11 |
189 |
|
#ifdef CONFIG_XUNIQUE |
190 |
|
#if QT_VERSION >= 0x050100 |
191 |
|
removeNativeEventFilter(m_pXcbEventFilter); |
192 |
|
delete m_pXcbEventFilter; |
193 |
|
#endif |
194 |
|
#endif // CONFIG_XUNIQUE |
195 |
|
#endif // CONFIG_X11 |
196 |
if (m_pMyTranslator) delete m_pMyTranslator; |
if (m_pMyTranslator) delete m_pMyTranslator; |
197 |
if (m_pQtTranslator) delete m_pQtTranslator; |
if (m_pQtTranslator) delete m_pQtTranslator; |
198 |
} |
} |
201 |
void setMainWidget(QWidget *pWidget) |
void setMainWidget(QWidget *pWidget) |
202 |
{ |
{ |
203 |
m_pWidget = pWidget; |
m_pWidget = pWidget; |
204 |
#if defined(Q_WS_X11) |
#ifdef CONFIG_X11 |
205 |
XGrabServer(m_pDisplay); |
#ifdef CONFIG_XUNIQUE |
206 |
m_wOwner = m_pWidget->winId(); |
m_wOwner = m_pWidget->winId(); |
207 |
XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime); |
if (m_pDisplay && m_wOwner) { |
208 |
XUngrabServer(m_pDisplay); |
XGrabServer(m_pDisplay); |
209 |
#endif |
XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime); |
210 |
|
XUngrabServer(m_pDisplay); |
211 |
|
} |
212 |
|
#endif // CONFIG_XUNIQUE |
213 |
|
#endif // CONFIG_X11 |
214 |
} |
} |
215 |
|
|
216 |
QWidget *mainWidget() const { return m_pWidget; } |
QWidget *mainWidget() const { return m_pWidget; } |
219 |
// and raise its proper main widget... |
// and raise its proper main widget... |
220 |
bool setup() |
bool setup() |
221 |
{ |
{ |
222 |
#if defined(Q_WS_X11) |
#ifdef CONFIG_X11 |
223 |
if (m_wOwner != None) { |
#ifdef CONFIG_XUNIQUE |
224 |
|
if (m_pDisplay && m_wOwner != None) { |
225 |
// First, notify any freedesktop.org WM |
// First, notify any freedesktop.org WM |
226 |
// that we're about to show the main widget... |
// that we're about to show the main widget... |
227 |
Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay); |
Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay); |
244 |
XSync(m_pDisplay, false); |
XSync(m_pDisplay, false); |
245 |
XRaiseWindow(m_pDisplay, m_wOwner); |
XRaiseWindow(m_pDisplay, m_wOwner); |
246 |
// And then, let it get caught on destination |
// And then, let it get caught on destination |
247 |
// by QApplication::x11EventFilter... |
// by QApplication::native/x11EventFilter... |
248 |
QByteArray value = QSAMPLER_XUNIQUE; |
const QByteArray value = QSAMPLER_XUNIQUE; |
249 |
XChangeProperty( |
XChangeProperty( |
250 |
m_pDisplay, |
m_pDisplay, |
251 |
m_wOwner, |
m_wOwner, |
257 |
// Done. |
// Done. |
258 |
return true; |
return true; |
259 |
} |
} |
260 |
#endif |
#endif // CONFIG_XUNIQUE |
261 |
|
#endif // CONFIG_X11 |
262 |
return false; |
return false; |
263 |
} |
} |
264 |
|
|
265 |
#if defined(Q_WS_X11) |
#ifdef CONFIG_X11 |
266 |
bool x11EventFilter(XEvent *pEv) |
#ifdef CONFIG_XUNIQUE |
267 |
|
void x11PropertyNotify(Window w) |
268 |
{ |
{ |
269 |
if (m_pWidget && m_wOwner != None |
if (m_pDisplay && m_pWidget && m_wOwner == w) { |
|
&& pEv->type == PropertyNotify |
|
|
&& pEv->xproperty.window == m_wOwner |
|
|
&& pEv->xproperty.state == PropertyNewValue) { |
|
270 |
// Always check whether our property-flag is still around... |
// Always check whether our property-flag is still around... |
271 |
Atom aType; |
Atom aType; |
272 |
int iFormat = 0; |
int iFormat = 0; |
297 |
if (iItems > 0 && pData) |
if (iItems > 0 && pData) |
298 |
XFree(pData); |
XFree(pData); |
299 |
} |
} |
300 |
|
} |
301 |
|
#if QT_VERSION < 0x050000 |
302 |
|
bool x11EventFilter(XEvent *pEv) |
303 |
|
{ |
304 |
|
if (pEv->type == PropertyNotify |
305 |
|
&& pEv->xproperty.state == PropertyNewValue) |
306 |
|
x11PropertyNotify(pEv->xproperty.window); |
307 |
return QApplication::x11EventFilter(pEv); |
return QApplication::x11EventFilter(pEv); |
308 |
} |
} |
309 |
#endif |
#endif |
310 |
|
#endif // CONFIG_XUNIQUE |
311 |
|
#endif // CONFIG_X11 |
312 |
|
|
313 |
private: |
private: |
314 |
|
|
319 |
// Instance variables. |
// Instance variables. |
320 |
QWidget *m_pWidget; |
QWidget *m_pWidget; |
321 |
|
|
322 |
#if defined(Q_WS_X11) |
#ifdef CONFIG_X11 |
323 |
|
#ifdef CONFIG_XUNIQUE |
324 |
Display *m_pDisplay; |
Display *m_pDisplay; |
325 |
Atom m_aUnique; |
Atom m_aUnique; |
326 |
Window m_wOwner; |
Window m_wOwner; |
327 |
|
#if QT_VERSION >= 0x050100 |
328 |
|
qsamplerXcbEventFilter *m_pXcbEventFilter; |
329 |
#endif |
#endif |
330 |
|
#endif // CONFIG_XUNIQUE |
331 |
|
#endif // CONFIG_X11 |
332 |
}; |
}; |
333 |
|
|
334 |
|
|
335 |
|
#ifdef CONFIG_X11 |
336 |
|
#ifdef CONFIG_XUNIQUE |
337 |
|
#if QT_VERSION >= 0x050100 |
338 |
|
// XCB Event filter (virtual processor). |
339 |
|
bool qsamplerXcbEventFilter::nativeEventFilter ( |
340 |
|
const QByteArray& eventType, void *message, long * ) |
341 |
|
{ |
342 |
|
if (eventType == "xcb_generic_event_t") { |
343 |
|
xcb_property_notify_event_t *pEv |
344 |
|
= static_cast<xcb_property_notify_event_t *> (message); |
345 |
|
if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY |
346 |
|
&& pEv->state == XCB_PROPERTY_NEW_VALUE) |
347 |
|
m_pApp->x11PropertyNotify(pEv->window); |
348 |
|
} |
349 |
|
return false; |
350 |
|
} |
351 |
|
#endif |
352 |
|
#endif // CONFIG_XUNIQUE |
353 |
|
#endif // CONFIG_X11 |
354 |
|
|
355 |
|
|
356 |
//------------------------------------------------------------------------- |
//------------------------------------------------------------------------- |
357 |
// stacktrace - Signal crash handler. |
// stacktrace - Signal crash handler. |
358 |
// |
// |
461 |
// Dark themes grayed/disabled color group fix... |
// Dark themes grayed/disabled color group fix... |
462 |
QPalette pal(app.palette()); |
QPalette pal(app.palette()); |
463 |
if (pal.base().color().value() < 0x7f) { |
if (pal.base().color().value() < 0x7f) { |
464 |
|
#if QT_VERSION >= 0x050000 |
465 |
|
const QColor& color = pal.window().color(); |
466 |
|
const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1; |
467 |
|
for (int i = 0; i < iGroups; ++i) { |
468 |
|
const QPalette::ColorGroup group = QPalette::ColorGroup(i); |
469 |
|
pal.setBrush(group, QPalette::Light, color.lighter(150)); |
470 |
|
pal.setBrush(group, QPalette::Midlight, color.lighter(120)); |
471 |
|
pal.setBrush(group, QPalette::Dark, color.darker(150)); |
472 |
|
pal.setBrush(group, QPalette::Mid, color.darker(120)); |
473 |
|
pal.setBrush(group, QPalette::Shadow, color.darker(200)); |
474 |
|
} |
475 |
|
// pal.setColor(QPalette::Disabled, QPalette::ButtonText, pal.mid().color()); |
476 |
|
#endif |
477 |
pal.setColorGroup(QPalette::Disabled, |
pal.setColorGroup(QPalette::Disabled, |
478 |
pal.windowText().color().darker(), |
pal.windowText().color().darker(), |
479 |
pal.button(), |
pal.button(), |
488 |
} |
} |
489 |
|
|
490 |
// Set default base font... |
// Set default base font... |
|
int iBaseFontSize = app.font().pointSize(); |
|
491 |
if (options.iBaseFontSize > 0) |
if (options.iBaseFontSize > 0) |
492 |
iBaseFontSize = options.iBaseFontSize; |
app.setFont(QFont(app.font().family(), options.iBaseFontSize)); |
|
app.setFont(QFont(app.font().family(), iBaseFontSize)); |
|
493 |
|
|
494 |
// Construct, setup and show the main form. |
// Construct, setup and show the main form. |
495 |
QSampler::MainForm w; |
QSampler::MainForm w; |
507 |
|
|
508 |
|
|
509 |
// end of qsampler.cpp |
// end of qsampler.cpp |
510 |
|
|