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