/[svn]/qsampler/trunk/src/main.cpp
ViewVC logotype

Diff of /qsampler/trunk/src/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1461 by schoenebeck, Sun Oct 28 23:30:36 2007 UTC revision 1890 by capela, Tue Apr 28 09:06:43 2009 UTC
# Line 1  Line 1 
1  // main.cpp  // main.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2004-2009, rncbc aka Rui Nuno Capela. All rights reserved.
5       Copyright (C) 2007, 2008 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
# Line 19  Line 20 
20    
21  *****************************************************************************/  *****************************************************************************/
22    
 #include <QApplication>  
 #include <QTextCodec>  
 #include <QTranslator>  
   
23  #include "qsamplerAbout.h"  #include "qsamplerAbout.h"
24  #include "qsamplerOptions.h"  #include "qsamplerOptions.h"
25  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
26    
27    #include <QApplication>
28    #include <QLibraryInfo>
29    #include <QTranslator>
30    #include <QLocale>
31    #if defined(__APPLE__)  // Toshi Nagata 20080105
32    #include <QDir>
33    #endif
34    
35    #if QT_VERSION < 0x040300
36    #define lighter(x)      light(x)
37    #define darker(x)       dark(x)
38    #endif
39    
40    
41    //-------------------------------------------------------------------------
42    // Singleton application instance stuff (Qt/X11 only atm.)
43    //
44    
45    #if defined(Q_WS_X11)
46    
47    #include <QX11Info>
48    
49    #include <X11/Xatom.h>
50    #include <X11/Xlib.h>
51    
52    #define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"
53    
54    #endif
55    
56    class qsamplerApplication : public QApplication
57    {
58    public:
59    
60            // Constructor.
61            qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),
62                    m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)    
63            {
64                    // Load translation support.
65                    QLocale loc;
66                    if (loc.language() != QLocale::C) {
67                            // Try own Qt translation...
68                            m_pQtTranslator = new QTranslator(this);
69                            QString sLocName = "qt_" + loc.name();
70                            QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
71                            if (m_pQtTranslator->load(sLocName, sLocPath)) {
72                                    QApplication::installTranslator(m_pQtTranslator);
73                            } else {
74                                    delete m_pQtTranslator;
75                                    m_pQtTranslator = 0;
76                    #ifdef CONFIG_DEBUG
77                                    qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
78                                            loc.name().toUtf8().constData(),
79                                            sLocPath.toUtf8().constData(),
80                                            sLocName.toUtf8().constData());
81                    #endif
82                            }
83                            // Try own application translation...
84                            m_pMyTranslator = new QTranslator(this);
85                            sLocName = "qsampler_" + loc.name();
86                            if (m_pMyTranslator->load(sLocName, sLocPath)) {
87                                    QApplication::installTranslator(m_pMyTranslator);
88                            } else {
89                                    sLocPath = CONFIG_PREFIX "/share/locale";
90                                    if (m_pMyTranslator->load(sLocName, sLocPath)) {
91                                            QApplication::installTranslator(m_pMyTranslator);
92                                    } else {
93                                            delete m_pMyTranslator;
94                                            m_pMyTranslator = 0;
95                    #ifdef CONFIG_DEBUG
96                                            qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
97                                                    loc.name().toUtf8().constData(),
98                                                    sLocPath.toUtf8().constData(),
99                                                    sLocName.toUtf8().constData());
100                    #endif
101                                    }
102                            }
103                    }
104            #if defined(Q_WS_X11)
105                    m_pDisplay = QX11Info::display();
106                    m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);
107                    XGrabServer(m_pDisplay);
108                    m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
109                    XUngrabServer(m_pDisplay);
110            #endif
111            }
112    
113            // Destructor.
114            ~qsamplerApplication()
115            {
116                    if (m_pMyTranslator) delete m_pMyTranslator;
117                    if (m_pQtTranslator) delete m_pQtTranslator;
118            }
119    
120            // Main application widget accessors.
121            void setMainWidget(QWidget *pWidget)
122            {
123                    m_pWidget = pWidget;
124            #if defined(Q_WS_X11)
125                    XGrabServer(m_pDisplay);
126                    m_wOwner = m_pWidget->winId();
127                    XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
128                    XUngrabServer(m_pDisplay);
129            #endif
130            }
131    
132            QWidget *mainWidget() const { return m_pWidget; }
133    
134            // Check if another instance is running,
135        // and raise its proper main widget...
136            bool setup()
137            {
138            #if defined(Q_WS_X11)
139                    if (m_wOwner != None) {
140                            // First, notify any freedesktop.org WM
141                            // that we're about to show the main widget...
142                            Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay);
143                            int iScreen = XScreenNumberOfScreen(pScreen);
144                            XEvent ev;
145                            memset(&ev, 0, sizeof(ev));
146                            ev.xclient.type = ClientMessage;
147                            ev.xclient.display = m_pDisplay;
148                            ev.xclient.window = m_wOwner;
149                            ev.xclient.message_type = XInternAtom(m_pDisplay, "_NET_ACTIVE_WINDOW", false);
150                            ev.xclient.format = 32;
151                            ev.xclient.data.l[0] = 0; // Source indication.
152                            ev.xclient.data.l[1] = 0; // Timestamp.
153                            ev.xclient.data.l[2] = 0; // Requestor's currently active window (none)
154                            ev.xclient.data.l[3] = 0;
155                            ev.xclient.data.l[4] = 0;
156                            XSelectInput(m_pDisplay, m_wOwner, StructureNotifyMask);
157                            XSendEvent(m_pDisplay, RootWindow(m_pDisplay, iScreen), false,
158                                    (SubstructureNotifyMask | SubstructureRedirectMask), &ev);
159                            XSync(m_pDisplay, false);
160                            XRaiseWindow(m_pDisplay, m_wOwner);
161                            // And then, let it get caught on destination
162                            // by QApplication::x11EventFilter...
163                            QByteArray value = QSAMPLER_XUNIQUE;
164                            XChangeProperty(
165                                    m_pDisplay,
166                                    m_wOwner,
167                                    m_aUnique,
168                                    m_aUnique, 8,
169                                    PropModeReplace,
170                                    (unsigned char *) value.data(),
171                                    value.length());
172                            // Done.
173                            return true;
174                    }
175            #endif
176                    return false;
177            }
178    
179    #if defined(Q_WS_X11)
180            bool x11EventFilter(XEvent *pEv)
181            {
182                    if (m_pWidget && m_wOwner != None
183                            && pEv->type == PropertyNotify
184                            && pEv->xproperty.window == m_wOwner
185                            && pEv->xproperty.state == PropertyNewValue) {
186                            // Always check whether our property-flag is still around...
187                            Atom aType;
188                            int iFormat = 0;
189                            unsigned long iItems = 0;
190                            unsigned long iAfter = 0;
191                            unsigned char *pData = 0;
192                            if (XGetWindowProperty(
193                                            m_pDisplay,
194                                            m_wOwner,
195                                            m_aUnique,
196                                            0, 1024,
197                                            false,
198                                            m_aUnique,
199                                            &aType,
200                                            &iFormat,
201                                            &iItems,
202                                            &iAfter,
203                                            &pData) == Success
204                                    && aType == m_aUnique && iItems > 0 && iAfter == 0) {
205                                    // Avoid repeating it-self...
206                                    XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);
207                                    // Just make it always shows up fine...
208                                    m_pWidget->show();
209                                    m_pWidget->raise();
210                                    m_pWidget->activateWindow();
211                            }
212                            // Free any left-overs...
213                            if (iItems > 0 && pData)
214                                    XFree(pData);
215                    }
216                    return QApplication::x11EventFilter(pEv);
217            }
218    #endif
219            
220    private:
221    
222            // Translation support.
223            QTranslator *m_pQtTranslator;
224            QTranslator *m_pMyTranslator;
225    
226            // Instance variables.
227            QWidget *m_pWidget;
228    
229    #if defined(Q_WS_X11)
230            Display *m_pDisplay;
231            Atom     m_aUnique;
232            Window   m_wOwner;
233    #endif
234    };
235    
236    
237  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
238  // main - The main program trunk.  // main - The main program trunk.
239  //  //
240    
241  int main ( int argc, char **argv )  int main ( int argc, char **argv )
242  {  {
243      QApplication app(argc, argv);          Q_INIT_RESOURCE(qsampler);
244    
245            qsamplerApplication app(argc, argv);
246    
247      // Load translation support.          #if defined(__APPLE__)  //  Toshi Nagata 20080105
248      QTranslator translator(0);          {
249      QString sLocale = QTextCodec::codecForLocale()->name();                  //  Set the plugin path to @exetutable_path/../plugins
250      if (sLocale != "C") { //TODO: not sure if "C" locale name exists in Qt4                  QDir dir(QApplication::applicationDirPath());
251          QString sLocName = "qsampler_" + sLocale;                  dir.cdUp();  // "Contents" directory
252          if (!translator.load(sLocName, ".")) {                  QApplication::setLibraryPaths(QStringList(dir.absolutePath() + "/plugins"));
253              QString sLocPath = CONFIG_PREFIX "/share/locale";  
254              if (!translator.load(sLocName, sLocPath))                  //  Set the PATH environment variable to include @executable_path/../../..
255                  fprintf(stderr, "Warning: no locale found: %s/%s.qm\n", sLocPath.toLatin1().data(), sLocName.toLatin1().data());                  dir.cdUp();
256          }                  dir.cdUp();
257          app.installTranslator(&translator);                  QString path(getenv("PATH"));
258      }                  path = dir.absolutePath() + ":" + path;
259                    setenv("PATH", path.toUtf8().constData(), 1);
260      // Construct default settings; override with command line arguments.          }
261      qsamplerOptions options;          #endif
262      if (!options.parse_args(app.argc(), app.argv())) {  
263          app.quit();          // Construct default settings; override with command line arguments.
264          return 1;          QSampler::Options options;
265      }          if (!options.parse_args(app.arguments())) {
266                    app.quit();
267      // Construct, setup and show the main form.                  return 1;
268      QSampler::MainForm w;          }
269          app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));  
270      w.setup(&options);          // Have another instance running?
271      w.show();          if (app.setup()) {
272                    app.quit();
273                    return 2;
274            }
275    
276            // Dark themes grayed/disabled color group fix...
277            QPalette pal(app.palette());
278            if (pal.base().color().value() < 0x7f) {
279                    pal.setColorGroup(QPalette::Disabled,
280                            pal.windowText().color().darker(),
281                            pal.button(),
282                            pal.light(),
283                            pal.dark(),
284                            pal.mid(),
285                            pal.text().color().darker(),
286                            pal.text().color().lighter(),
287                            pal.base(),
288                            pal.window());
289                    app.setPalette(pal);
290            }
291    
292            // Set default base font...
293            int iBaseFontSize = app.font().pointSize();
294            if (options.iBaseFontSize > 0)
295                    iBaseFontSize = options.iBaseFontSize;
296            app.setFont(QFont(app.font().family(), iBaseFontSize));
297    
298            // Construct, setup and show the main form.
299            QSampler::MainForm w;
300            w.setup(&options);
301            w.show();
302    
303      // Register the quit signal/slot.          // Settle this one as application main widget...
304      // app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));          app.setMainWidget(&w);
305    
306      return app.exec();          // Register the quit signal/slot.
307            // app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
308    
309            return app.exec();
310  }  }
311    
312    
313  // end of main.cpp  // end of main.cpp

Legend:
Removed from v.1461  
changed lines
  Added in v.1890

  ViewVC Help
Powered by ViewVC