]> git.saurik.com Git - wxWidgets.git/blame - samples/oleauto/oleauto.cpp
Remove DoSetSizeHints() call from Create()
[wxWidgets.git] / samples / oleauto / oleauto.cpp
CommitLineData
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
8d7dafc9
JS
7// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
8d7dafc9
JS
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
be5a51fb 27// need because it includes almost all "standard" wxWidgets headers
8d7dafc9
JS
28#ifndef WX_PRECOMP
29 #include "wx/wx.h"
30#endif
31
b20edf8b 32#include "wx/msw/ole/automtn.h"
8d7dafc9 33
bb5a9514 34#ifndef __WINDOWS__
8d7dafc9
JS
35#error "Sorry, this sample works under Windows only."
36#endif
37
38// ----------------------------------------------------------------------------
39// ressources
40// ----------------------------------------------------------------------------
41// the application icon
e7092398 42#ifndef wxHAS_IMAGES_IN_RESOURCES
3cb332c1 43 #include "../sample.xpm"
8d7dafc9
JS
44#endif
45
46// ----------------------------------------------------------------------------
47// private classes
48// ----------------------------------------------------------------------------
49
50// Define a new application type, each program should derive a class from wxApp
51class MyApp : public wxApp
52{
53public:
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
64class MyFrame : public wxFrame
65{
66public:
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
75private:
be5a51fb 76 // any class wishing to process wxWidgets events must use this macro
8d7dafc9
JS
77 DECLARE_EVENT_TABLE()
78};
79
80// ----------------------------------------------------------------------------
81// constants
82// ----------------------------------------------------------------------------
83
84// IDs for the controls and the menu commands
85enum
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)
004f4002 93 OleAuto_Text = 1000
8d7dafc9
JS
94};
95
96// ----------------------------------------------------------------------------
be5a51fb 97// event tables and other macros for wxWidgets
8d7dafc9
JS
98// ----------------------------------------------------------------------------
99
be5a51fb 100// the event tables connect the wxWidgets events with the functions (event
8d7dafc9
JS
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.
103BEGIN_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)
107END_EVENT_TABLE()
108
be5a51fb 109// Create a new application object: this macro will allow wxWidgets to create
8d7dafc9
JS
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)
114IMPLEMENT_APP(MyApp)
115
116// ============================================================================
117// implementation
118// ============================================================================
119
120// ----------------------------------------------------------------------------
121// the application class
122// ----------------------------------------------------------------------------
123
124// `Main program' equivalent: the program execution "starts" here
125bool MyApp::OnInit()
126{
45e6e6f8
VZ
127 if ( !wxApp::OnInit() )
128 return false;
129
8d7dafc9 130 // Create the main application window
9a83f860 131 MyFrame *frame = new MyFrame(wxT("OleAuto wxWidgets App"),
8d7dafc9
JS
132 wxPoint(50, 50), wxSize(450, 340));
133
18f42b94 134 // Show it
7cdd78f6 135 frame->Show(true);
8d7dafc9
JS
136
137 // success: wxApp::OnRun() will be called which will enter the main message
7cdd78f6 138 // loop and the application will run. If we returned false here, the
8d7dafc9 139 // application would exit immediately.
7cdd78f6 140 return true;
8d7dafc9
JS
141}
142
143// ----------------------------------------------------------------------------
144// main frame
145// ----------------------------------------------------------------------------
146
147// frame constructor
148MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
7cdd78f6 149 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
8d7dafc9
JS
150{
151 // set the frame icon
3cb332c1 152 SetIcon(wxICON(sample));
8d7dafc9
JS
153
154 // create a menu bar
155 wxMenu *menuFile = new wxMenu;
156
9a83f860 157 menuFile->Append(OleAuto_Test, wxT("&Test Excel Automation..."));
2d143b66 158 menuFile->Append(OleAuto_About, wxT("&About"));
8d7dafc9 159 menuFile->AppendSeparator();
9a83f860 160 menuFile->Append(OleAuto_Quit, wxT("E&xit"));
8d7dafc9
JS
161
162 // now append the freshly created menu to the menu bar...
163 wxMenuBar *menuBar = new wxMenuBar;
9a83f860 164 menuBar->Append(menuFile, wxT("&File"));
8d7dafc9
JS
165
166 // ... and attach this menu bar to the frame
167 SetMenuBar(menuBar);
168
8520f137 169#if wxUSE_STATUSBAR
8d7dafc9
JS
170 // create a status bar just for fun (by default with 1 pane only)
171 CreateStatusBar(2);
9a83f860 172 SetStatusText(wxT("Welcome to wxWidgets!"));
8520f137 173#endif // wxUSE_STATUSBAR
8d7dafc9
JS
174}
175
176
177// event handlers
178
179void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
180{
7cdd78f6
WS
181 // true is to force the frame to close
182 Close(true);
8d7dafc9
JS
183}
184
185void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
186{
9a83f860
VZ
187 wxMessageBox(wxT("This is an OLE Automation sample"),
188 wxT("About OleAuto"), wxOK | wxICON_INFORMATION, this);
8d7dafc9
JS
189}
190
191/* Tests OLE automation by making the active Excel cell bold,
192 * and changing the text.
193 */
194void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
195{
6eefca4f
VZ
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"));
8d7dafc9 199
6eefca4f
VZ
200 wxAutomationObject excelObject;
201 if ( !excelObject.GetInstance(wxT("Excel.Application")) )
2f6c54eb 202 {
6eefca4f
VZ
203 wxLogError(wxT("Could not create Excel object."));
204 return;
2f6c54eb 205 }
56270af4
VZ
206
207 // Ensure that Excel is visible
9a83f860 208 if (!excelObject.PutProperty(wxT("Visible"), true))
56270af4 209 {
6eefca4f 210 wxLogError(wxT("Could not make Excel object visible"));
56270af4 211 }
9a83f860 212 const wxVariant workbooksCountVariant = excelObject.GetProperty(wxT("Workbooks.Count"));
56270af4
VZ
213 if (workbooksCountVariant.IsNull())
214 {
6eefca4f 215 wxLogError(wxT("Could not get workbooks count"));
56270af4
VZ
216 return;
217 }
218 const long workbooksCount = workbooksCountVariant;
219 if (workbooksCount == 0)
220 {
9a83f860 221 const wxVariant workbook = excelObject.CallMethod(wxT("Workbooks.Add"));
56270af4
VZ
222 if (workbook.IsNull())
223 {
6eefca4f 224 wxLogError(wxT("Could not create new Workbook"));
56270af4
VZ
225 return;
226 }
227 }
228
9a83f860 229 if (!excelObject.PutProperty(wxT("ActiveCell.Value"), wxT("wxWidgets automation test!")))
2f6c54eb 230 {
6eefca4f 231 wxLogError(wxT("Could not set active cell value."));
2f6c54eb
VZ
232 return;
233 }
9a83f860 234 if (!excelObject.PutProperty(wxT("ActiveCell.Font.Bold"), wxVariant(true)) )
2f6c54eb 235 {
6eefca4f 236 wxLogError(wxT("Could not put Bold property to active cell."));
2f6c54eb
VZ
237 return;
238 }
8d7dafc9 239}