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