]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/MainHelper.h
...
[cpPlugins.git] / lib / cpBaseQtApplication / MainHelper.h
1 #ifndef __cpBaseQtApplication__MainHelper__h__
2 #define __cpBaseQtApplication__MainHelper__h__
3
4 #include <cstdlib>
5 #include <QApplication>
6
7 // -------------------------------------------------------------------------
8 #define cpBaseQtApplication_Main( _window_class_ )              \
9   int main( int argc, char* argv[] )                            \
10   {                                                             \
11     QApplication a( argc, argv );                               \
12     _window_class_ w( argc, argv, &a );                         \
13     w.show( );                                                  \
14     return( a.exec( ) );                                        \
15   }
16
17 // -------------------------------------------------------------------------
18 #include <cpPlugins/Config.h>
19
20 #ifdef cpPlugins_OS_Windows
21
22 #  include <memory>
23 #  include <vector>
24 #  include <windows.h>
25 #  include <shellapi.h>
26
27 // -------------------------------------------------------------------------
28 namespace cpBaseQtApplication
29 {
30   /**
31    */
32   class Win32CommandLineConverter
33   {
34   private:
35     std::unique_ptr< char*[ ] > argv_;
36     std::vector< std::unique_ptr< char[ ] > > storage_;
37
38   public:
39     Win32CommandLineConverter( )
40       {
41         LPWSTR cmd_line = GetCommandLineW( );
42         int argc;
43         LPWSTR* w_argv = CommandLineToArgvW( cmd_line, &argc );
44         argv_ = std::unique_ptr< char*[ ] >( new char*[ argc ] );
45         storage_.reserve( argc );
46         for( int i = 0; i < argc; ++i )
47         {
48           storage_.push_back( ConvertWArg( w_argv[ i ] ) );
49           argv_[ i ] = storage_.back( ).get( );
50
51         } // rof
52         LocalFree( w_argv );
53       }
54     int argc( ) const
55       {
56         return( static_cast< int >(storage_.size( ) ) );
57       }
58     char** argv( ) const
59       {
60         return( argv_.get( ) );
61       }
62     static std::unique_ptr< char[ ] > ConvertWArg( LPWSTR w_arg )
63       {
64         int size = WideCharToMultiByte(
65           CP_UTF8, 0, w_arg, -1, nullptr, 0, nullptr, nullptr
66           );
67         std::unique_ptr< char[ ] > ret( new char[ size ] );
68         WideCharToMultiByte(
69           CP_UTF8, 0, w_arg, -1, ret.get( ), size, nullptr, nullptr
70           );
71         return( ret );
72       }
73   };
74
75 } // ecapseman
76
77 // -------------------------------------------------------------------------
78 #  define cpBaseQtApplication_MainComplement                            \
79   int CALLBACK WinMain( HINSTANCE i, HINSTANCE p, LPSTR c, int s )      \
80   {                                                                     \
81     cpBaseQtApplication::Win32CommandLineConverter cmd_line;            \
82     return( main( cmd_line.argc( ), cmd_line.argv( ) ) );               \
83   }
84 #else  // cpPlugins_OS_Windows
85 #  define cpBaseQtApplication_MainComplement
86 #endif // cpPlugins_OS_Windows
87
88 #endif // __cpBaseQtApplication__MainHelper__h__
89
90 // eof - $RCSfile$