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