]> git.saurik.com Git - wxWidgets.git/blob - samples/oleauto/oleauto.cpp
follow up parent chain to properly support modal dialog parents, see #15383
[wxWidgets.git] / samples / oleauto / oleauto.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: oleauto.cpp
3 // Purpose: OLE Automation wxWidgets sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 08/12/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 // for all others, include the necessary headers (this file is usually all you
27 // need because it includes almost all "standard" wxWidgets headers
28 #ifndef WX_PRECOMP
29 #include "wx/wx.h"
30 #endif
31
32 #include "wx/msw/ole/automtn.h"
33
34 #ifndef __WINDOWS__
35 #error "Sorry, this sample works under Windows only."
36 #endif
37
38 // ----------------------------------------------------------------------------
39 // ressources
40 // ----------------------------------------------------------------------------
41 // the application icon
42 #ifndef wxHAS_IMAGES_IN_RESOURCES
43 #include "../sample.xpm"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // private classes
48 // ----------------------------------------------------------------------------
49
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp : public wxApp
52 {
53 public:
54 // override base class virtuals
55 // ----------------------------
56
57 // this one is called on application startup and is a good place for the app
58 // initialization (doing it here and not in the ctor allows to have an error
59 // return: if OnInit() returns false, the application terminates)
60 virtual bool OnInit();
61 };
62
63 // Define a new frame type: this is going to be our main frame
64 class MyFrame : public wxFrame
65 {
66 public:
67 // ctor(s)
68 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
69
70 // event handlers (these functions should _not_ be virtual)
71 void OnQuit(wxCommandEvent& event);
72 void OnAbout(wxCommandEvent& event);
73 void OnTest(wxCommandEvent& event);
74
75 private:
76 // any class wishing to process wxWidgets events must use this macro
77 DECLARE_EVENT_TABLE()
78 };
79
80 // ----------------------------------------------------------------------------
81 // constants
82 // ----------------------------------------------------------------------------
83
84 // IDs for the controls and the menu commands
85 enum
86 {
87 // menu items
88 OleAuto_Quit = 1,
89 OleAuto_About,
90 OleAuto_Test,
91
92 // controls start here (the numbers are, of course, arbitrary)
93 OleAuto_Text = 1000
94 };
95
96 // ----------------------------------------------------------------------------
97 // event tables and other macros for wxWidgets
98 // ----------------------------------------------------------------------------
99
100 // the event tables connect the wxWidgets events with the functions (event
101 // handlers) which process them. It can be also done at run-time, but for the
102 // simple menu events like this the static method is much simpler.
103 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
104 EVT_MENU(OleAuto_Quit, MyFrame::OnQuit)
105 EVT_MENU(OleAuto_About, MyFrame::OnAbout)
106 EVT_MENU(OleAuto_Test, MyFrame::OnTest)
107 END_EVENT_TABLE()
108
109 // Create a new application object: this macro will allow wxWidgets to create
110 // the application object during program execution (it's better than using a
111 // static object for many reasons) and also declares the accessor function
112 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
113 // not wxApp)
114 IMPLEMENT_APP(MyApp)
115
116 // ============================================================================
117 // implementation
118 // ============================================================================
119
120 // ----------------------------------------------------------------------------
121 // the application class
122 // ----------------------------------------------------------------------------
123
124 // `Main program' equivalent: the program execution "starts" here
125 bool MyApp::OnInit()
126 {
127 if ( !wxApp::OnInit() )
128 return false;
129
130 // Create the main application window
131 MyFrame *frame = new MyFrame(wxT("OleAuto wxWidgets App"),
132 wxPoint(50, 50), wxSize(450, 340));
133
134 // Show it
135 frame->Show(true);
136
137 // success: wxApp::OnRun() will be called which will enter the main message
138 // loop and the application will run. If we returned false here, the
139 // application would exit immediately.
140 return true;
141 }
142
143 // ----------------------------------------------------------------------------
144 // main frame
145 // ----------------------------------------------------------------------------
146
147 // frame constructor
148 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
149 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
150 {
151 // set the frame icon
152 SetIcon(wxICON(sample));
153
154 // create a menu bar
155 wxMenu *menuFile = new wxMenu;
156
157 menuFile->Append(OleAuto_Test, wxT("&Test Excel Automation..."));
158 menuFile->Append(OleAuto_About, wxT("&About"));
159 menuFile->AppendSeparator();
160 menuFile->Append(OleAuto_Quit, wxT("E&xit"));
161
162 // now append the freshly created menu to the menu bar...
163 wxMenuBar *menuBar = new wxMenuBar;
164 menuBar->Append(menuFile, wxT("&File"));
165
166 // ... and attach this menu bar to the frame
167 SetMenuBar(menuBar);
168
169 #if wxUSE_STATUSBAR
170 // create a status bar just for fun (by default with 1 pane only)
171 CreateStatusBar(2);
172 SetStatusText(wxT("Welcome to wxWidgets!"));
173 #endif // wxUSE_STATUSBAR
174 }
175
176
177 // event handlers
178
179 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
180 {
181 // true is to force the frame to close
182 Close(true);
183 }
184
185 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
186 {
187 wxMessageBox(wxT("This is an OLE Automation sample"),
188 wxT("About OleAuto"), wxOK | wxICON_INFORMATION, this);
189 }
190
191 /* Tests OLE automation by making the active Excel cell bold,
192 * and changing the text.
193 */
194 void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
195 {
196 wxMessageBox(wxT("Excel will be started if it is not running after you have pressed OK button.")
197 wxT("\nThe active cell should then say 'wxWidgets automation test!' in bold."),
198 wxT("Excel start"));
199
200 wxAutomationObject excelObject;
201 if ( !excelObject.GetInstance(wxT("Excel.Application")) )
202 {
203 wxLogError(wxT("Could not create Excel object."));
204 return;
205 }
206
207 // Ensure that Excel is visible
208 if (!excelObject.PutProperty(wxT("Visible"), true))
209 {
210 wxLogError(wxT("Could not make Excel object visible"));
211 }
212 const wxVariant workbooksCountVariant = excelObject.GetProperty(wxT("Workbooks.Count"));
213 if (workbooksCountVariant.IsNull())
214 {
215 wxLogError(wxT("Could not get workbooks count"));
216 return;
217 }
218 const long workbooksCount = workbooksCountVariant;
219 if (workbooksCount == 0)
220 {
221 const wxVariant workbook = excelObject.CallMethod(wxT("Workbooks.Add"));
222 if (workbook.IsNull())
223 {
224 wxLogError(wxT("Could not create new Workbook"));
225 return;
226 }
227 }
228
229 if (!excelObject.PutProperty(wxT("ActiveCell.Value"), wxT("wxWidgets automation test!")))
230 {
231 wxLogError(wxT("Could not set active cell value."));
232 return;
233 }
234 if (!excelObject.PutProperty(wxT("ActiveCell.Font.Bold"), wxVariant(true)) )
235 {
236 wxLogError(wxT("Could not put Bold property to active cell."));
237 return;
238 }
239 }