]>
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 | // ---------------------------------------------------------------------------- | |
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 | |
be5a51fb | 32 | // need because it includes almost all "standard" wxWidgets headers |
8d7dafc9 JS |
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 | |
618f2efa | 47 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) |
8d7dafc9 JS |
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: | |
be5a51fb | 81 | // any class wishing to process wxWidgets events must use this macro |
8d7dafc9 JS |
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 | // ---------------------------------------------------------------------------- | |
be5a51fb | 102 | // event tables and other macros for wxWidgets |
8d7dafc9 JS |
103 | // ---------------------------------------------------------------------------- |
104 | ||
be5a51fb | 105 | // the event tables connect the wxWidgets events with the functions (event |
8d7dafc9 JS |
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 | ||
be5a51fb | 114 | // Create a new application object: this macro will allow wxWidgets to create |
8d7dafc9 JS |
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 | |
be5a51fb | 133 | MyFrame *frame = new MyFrame(_T("OleAuto wxWidgets App"), |
8d7dafc9 JS |
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? | |
7cdd78f6 | 138 | frame->Show(true); |
8d7dafc9 JS |
139 | SetTopWindow(frame); |
140 | ||
141 | // success: wxApp::OnRun() will be called which will enter the main message | |
7cdd78f6 | 142 | // loop and the application will run. If we returned false here, the |
8d7dafc9 | 143 | // application would exit immediately. |
7cdd78f6 | 144 | return true; |
8d7dafc9 JS |
145 | } |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
148 | // main frame | |
149 | // ---------------------------------------------------------------------------- | |
150 | ||
151 | // frame constructor | |
152 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
7cdd78f6 | 153 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) |
8d7dafc9 JS |
154 | { |
155 | // set the frame icon | |
156 | SetIcon(wxICON(mondrian)); | |
157 | ||
158 | // create a menu bar | |
159 | wxMenu *menuFile = new wxMenu; | |
160 | ||
600683ca MB |
161 | menuFile->Append(OleAuto_Test, _T("&Test Excel Automation...")); |
162 | menuFile->Append(OleAuto_About, _T("&About...")); | |
8d7dafc9 | 163 | menuFile->AppendSeparator(); |
600683ca | 164 | menuFile->Append(OleAuto_Quit, _T("E&xit")); |
8d7dafc9 JS |
165 | |
166 | // now append the freshly created menu to the menu bar... | |
167 | wxMenuBar *menuBar = new wxMenuBar; | |
600683ca | 168 | menuBar->Append(menuFile, _T("&File")); |
8d7dafc9 JS |
169 | |
170 | // ... and attach this menu bar to the frame | |
171 | SetMenuBar(menuBar); | |
172 | ||
8520f137 | 173 | #if wxUSE_STATUSBAR |
8d7dafc9 JS |
174 | // create a status bar just for fun (by default with 1 pane only) |
175 | CreateStatusBar(2); | |
be5a51fb | 176 | SetStatusText(_T("Welcome to wxWidgets!")); |
8520f137 | 177 | #endif // wxUSE_STATUSBAR |
8d7dafc9 JS |
178 | } |
179 | ||
180 | ||
181 | // event handlers | |
182 | ||
183 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
184 | { | |
7cdd78f6 WS |
185 | // true is to force the frame to close |
186 | Close(true); | |
8d7dafc9 JS |
187 | } |
188 | ||
189 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
190 | { | |
600683ca MB |
191 | wxMessageBox(_T("This is an OLE Automation sample"), |
192 | _T("About OleAuto"), wxOK | wxICON_INFORMATION, this); | |
8d7dafc9 JS |
193 | } |
194 | ||
195 | /* Tests OLE automation by making the active Excel cell bold, | |
196 | * and changing the text. | |
197 | */ | |
198 | void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event)) | |
199 | { | |
be5a51fb | 200 | wxMessageBox(_T("Please ensure Excel is running, then press OK.\nThe active cell should then say 'wxWidgets automation test!' in bold.")); |
8d7dafc9 | 201 | |
2f6c54eb | 202 | wxAutomationObject excelObject, rangeObject; |
600683ca | 203 | if (!excelObject.GetInstance(_T("Excel.Application"))) |
2f6c54eb | 204 | { |
600683ca | 205 | if (!excelObject.CreateInstance(_T("Excel.Application"))) |
8d7dafc9 | 206 | { |
600683ca | 207 | wxMessageBox(_T("Could not create Excel object.")); |
2f6c54eb | 208 | return; |
8d7dafc9 | 209 | } |
2f6c54eb | 210 | } |
be5a51fb | 211 | if (!excelObject.PutProperty(_T("ActiveCell.Value"), _T("wxWidgets automation test!"))) |
2f6c54eb | 212 | { |
600683ca | 213 | wxMessageBox(_T("Could not set active cell value.")); |
2f6c54eb VZ |
214 | return; |
215 | } | |
7cdd78f6 | 216 | if (!excelObject.PutProperty(_T("ActiveCell.Font.Bold"), wxVariant(true)) ) |
2f6c54eb | 217 | { |
600683ca | 218 | wxMessageBox(_T("Could not put Bold property to active cell.")); |
2f6c54eb VZ |
219 | return; |
220 | } | |
8d7dafc9 JS |
221 | } |
222 |