]> git.saurik.com Git - wxWidgets.git/blame - samples/oleauto/oleauto.cpp
Fixes to allow compilation with no wchar_t (djgpp probably has a real wchar_t
[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
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
33#include <wx/msw/ole/automtn.h>
34
35#ifndef __WXMSW__
36#error "Sorry, this sample works under Windows only."
37#endif
38
39// ----------------------------------------------------------------------------
40// ressources
41// ----------------------------------------------------------------------------
42// the application icon
618f2efa 43#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
8d7dafc9
JS
44 #include "mondrian.xpm"
45#endif
46
47// ----------------------------------------------------------------------------
48// private classes
49// ----------------------------------------------------------------------------
50
51// Define a new application type, each program should derive a class from wxApp
52class MyApp : public wxApp
53{
54public:
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
65class MyFrame : public wxFrame
66{
67public:
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
76private:
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
86enum
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)
94 OleAuto_Text = 1000,
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.
104BEGIN_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)
108END_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)
115IMPLEMENT_APP(MyApp)
116
117// ============================================================================
118// implementation
119// ============================================================================
120
121// ----------------------------------------------------------------------------
122// the application class
123// ----------------------------------------------------------------------------
124
125// `Main program' equivalent: the program execution "starts" here
126bool MyApp::OnInit()
127{
128 // Create the main application window
be5a51fb 129 MyFrame *frame = new MyFrame(_T("OleAuto wxWidgets App"),
8d7dafc9
JS
130 wxPoint(50, 50), wxSize(450, 340));
131
132 // Show it and tell the application that it's our main window
133 // @@@ what does it do exactly, in fact? is it necessary here?
7cdd78f6 134 frame->Show(true);
8d7dafc9
JS
135 SetTopWindow(frame);
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
152 SetIcon(wxICON(mondrian));
153
154 // create a menu bar
155 wxMenu *menuFile = new wxMenu;
156
600683ca
MB
157 menuFile->Append(OleAuto_Test, _T("&Test Excel Automation..."));
158 menuFile->Append(OleAuto_About, _T("&About..."));
8d7dafc9 159 menuFile->AppendSeparator();
600683ca 160 menuFile->Append(OleAuto_Quit, _T("E&xit"));
8d7dafc9
JS
161
162 // now append the freshly created menu to the menu bar...
163 wxMenuBar *menuBar = new wxMenuBar;
600683ca 164 menuBar->Append(menuFile, _T("&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);
be5a51fb 172 SetStatusText(_T("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{
600683ca
MB
187 wxMessageBox(_T("This is an OLE Automation sample"),
188 _T("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{
be5a51fb 196 wxMessageBox(_T("Please ensure Excel is running, then press OK.\nThe active cell should then say 'wxWidgets automation test!' in bold."));
8d7dafc9 197
2f6c54eb 198 wxAutomationObject excelObject, rangeObject;
600683ca 199 if (!excelObject.GetInstance(_T("Excel.Application")))
2f6c54eb 200 {
600683ca 201 if (!excelObject.CreateInstance(_T("Excel.Application")))
8d7dafc9 202 {
600683ca 203 wxMessageBox(_T("Could not create Excel object."));
2f6c54eb 204 return;
8d7dafc9 205 }
2f6c54eb 206 }
be5a51fb 207 if (!excelObject.PutProperty(_T("ActiveCell.Value"), _T("wxWidgets automation test!")))
2f6c54eb 208 {
600683ca 209 wxMessageBox(_T("Could not set active cell value."));
2f6c54eb
VZ
210 return;
211 }
7cdd78f6 212 if (!excelObject.PutProperty(_T("ActiveCell.Font.Bold"), wxVariant(true)) )
2f6c54eb 213 {
600683ca 214 wxMessageBox(_T("Could not put Bold property to active cell."));
2f6c54eb
VZ
215 return;
216 }
8d7dafc9
JS
217}
218