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