1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wx_exe.cpp
3 // Purpose:     Sample showing how to use wx from a DLL
4 // Author:      Vaclav Slavik
5 // Created:     2009-12-03
6 // Copyright:   (c) 2009 Vaclav Slavik
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 // ============================================================================
11 // declarations
12 // ============================================================================
13 
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17 
18 #include "wx/wxprec.h"
19 
20 
21 #include "my_dll.h"
22 
23 #include "wx/app.h"
24 #include "wx/frame.h"
25 #include "wx/panel.h"
26 #include "wx/sizer.h"
27 #include "wx/stattext.h"
28 #include "wx/button.h"
29 
30 #ifndef __WINDOWS__
31     #error "This sample is Windows-only"
32 #endif
33 
34 #ifdef WXUSINGDLL
35     #error "This sample doesn't work with DLL build of wxWidgets"
36 #endif
37 
38 // ----------------------------------------------------------------------------
39 // GUI classes
40 // ----------------------------------------------------------------------------
41 
42 static const int ID_RUN_DLL = wxNewId();
43 
44 class MainFrame : public wxFrame
45 {
46 public:
47     MainFrame();
48 
49     void OnRunDLL(wxCommandEvent& event);
50 
51     wxDECLARE_EVENT_TABLE();
52 };
53 
54 
55 class MainApp : public wxApp
56 {
57 public:
58     virtual bool OnInit() wxOVERRIDE;
59     virtual int OnExit() wxOVERRIDE;
60 };
61 
62 
63 // ============================================================================
64 // implementation
65 // ============================================================================
66 
67 // ----------------------------------------------------------------------------
68 // MainFrame
69 // ----------------------------------------------------------------------------
70 
wxBEGIN_EVENT_TABLE(MainFrame,wxFrame)71 wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
72     EVT_BUTTON(ID_RUN_DLL, MainFrame::OnRunDLL)
73 wxEND_EVENT_TABLE()
74 
75 MainFrame::MainFrame()
76     : wxFrame(NULL, wxID_ANY, "Main wx app",
77               wxDefaultPosition, wxSize(400, 300))
78 {
79     wxPanel *p = new wxPanel(this, wxID_ANY);
80     wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
81 
82     sizer->Add
83            (
84                new wxStaticText
85                    (
86                        p, wxID_ANY,
87                        wxString::Format
88                        (
89                            "Main wxApp instance is %p (%s),\n"
90                            "thread ID %ld.\n",
91                            wxApp::GetInstance(),
92                            wxVERSION_STRING,
93                            wxThread::GetCurrentId()
94                        )
95                    ),
96                wxSizerFlags(1).Expand().Border(wxALL, 10)
97            );
98 
99     sizer->Add
100            (
101                new wxButton(p, ID_RUN_DLL, "Run GUI from DLL"),
102                wxSizerFlags(0).Right().Border(wxALL, 10)
103            );
104 
105     p->SetSizerAndFit(sizer);
106 
107     wxSizer *fsizer = new wxBoxSizer(wxVERTICAL);
108     fsizer->Add(p, wxSizerFlags(1).Expand());
109     SetSizerAndFit(fsizer);
110 }
111 
OnRunDLL(wxCommandEvent & WXUNUSED (event))112 void MainFrame::OnRunDLL(wxCommandEvent& WXUNUSED(event))
113 {
114     run_wx_gui_from_dll("child instance");
115 }
116 
117 
118 // ----------------------------------------------------------------------------
119 // MainApp
120 // ----------------------------------------------------------------------------
121 
OnInit()122 bool MainApp::OnInit()
123 {
124     if ( !wxApp::OnInit() )
125         return false;
126 
127     wxFrame *f = new MainFrame();
128     f->Show(true);
129 
130     return true;
131 }
132 
OnExit()133 int MainApp::OnExit()
134 {
135     wx_dll_cleanup();
136     return wxApp::OnExit();
137 }
138 
139 
140 wxIMPLEMENT_APP(MainApp);
141