]> git.saurik.com Git - wxWidgets.git/blame - samples/exec/exec.cpp
Fixed small bug in tab code WRT to having only
[wxWidgets.git] / samples / exec / exec.cpp
CommitLineData
69c33c6c
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: exec.cpp
3// Purpose: exec sample demonstrates wxExecute and related functions
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 15.01.00
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "exec.cpp"
22 #pragma interface "exec.cpp"
23#endif
24
25// For compilers that support precompilation, includes "wx/wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
29 #pragma hdrstop
30#endif
31
32// for all others, include the necessary headers (this file is usually all you
33// need because it includes almost all "standard" wxWindows headers
34#ifndef WX_PRECOMP
35 #include "wx/app.h"
36 #include "wx/frame.h"
37 #include "wx/utils.h"
a8f0faf3
GRG
38 #include "wx/menu.h"
39 #include "wx/msgdlg.h"
40 #include "wx/textdlg.h"
69c33c6c
VZ
41#endif
42
43#include "wx/process.h"
44
d93c719a
VZ
45#ifdef __WINDOWS__
46 #include "wx/dde.h"
47#endif // __WINDOWS__
48
69c33c6c
VZ
49// ----------------------------------------------------------------------------
50// private classes
51// ----------------------------------------------------------------------------
52
53// Define a new application type, each program should derive a class from wxApp
54class MyApp : public wxApp
55{
56public:
57 // override base class virtuals
58 // ----------------------------
59
60 // this one is called on application startup and is a good place for the app
61 // initialization (doing it here and not in the ctor allows to have an error
62 // return: if OnInit() returns false, the application terminates)
63 virtual bool OnInit();
64};
65
66// Define a new frame type: this is going to be our main frame
67class MyFrame : public wxFrame
68{
69public:
70 // ctor(s)
71 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
72
73 // event handlers (these functions should _not_ be virtual)
74 void OnQuit(wxCommandEvent& event);
75
76 void OnSyncExec(wxCommandEvent& event);
77 void OnAsyncExec(wxCommandEvent& event);
78 void OnShell(wxCommandEvent& event);
d93c719a 79 void OnDDEExec(wxCommandEvent& event);
69c33c6c
VZ
80
81 void OnAbout(wxCommandEvent& event);
82
83private:
84 wxString m_cmdLast;
85
86 // any class wishing to process wxWindows events must use this macro
87 DECLARE_EVENT_TABLE()
88};
89
90// This is the handler for process termination events
91class MyProcess : public wxProcess
92{
93public:
94 MyProcess(wxFrame *parent, const wxString& cmd)
95 : wxProcess(parent), m_cmd(cmd)
96 {
97 m_parent = parent;
98 }
99
100 // instead of overriding this virtual function we might as well process the
101 // event from it in the frame class - this might be more convenient in some
102 // cases
103 virtual void OnTerminate(int pid, int status);
104
105private:
106 wxFrame *m_parent;
107 wxString m_cmd;
108};
109
110// ----------------------------------------------------------------------------
111// constants
112// ----------------------------------------------------------------------------
113
114// IDs for the controls and the menu commands
115enum
116{
117 // menu items
118 Exec_Quit = 100,
119 Exec_SyncExec = 200,
120 Exec_AsyncExec,
121 Exec_Shell,
d93c719a 122 Exec_DDEExec,
69c33c6c
VZ
123 Exec_About = 300
124};
125
d93c719a
VZ
126static const wxChar *DIALOG_TITLE = _T("Exec sample");
127
69c33c6c
VZ
128// ----------------------------------------------------------------------------
129// event tables and other macros for wxWindows
130// ----------------------------------------------------------------------------
131
132// the event tables connect the wxWindows events with the functions (event
133// handlers) which process them. It can be also done at run-time, but for the
134// simple menu events like this the static method is much simpler.
135BEGIN_EVENT_TABLE(MyFrame, wxFrame)
136 EVT_MENU(Exec_Quit, MyFrame::OnQuit)
137
138 EVT_MENU(Exec_SyncExec, MyFrame::OnSyncExec)
139 EVT_MENU(Exec_AsyncExec, MyFrame::OnAsyncExec)
140 EVT_MENU(Exec_Shell, MyFrame::OnShell)
d93c719a 141 EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec)
69c33c6c
VZ
142
143 EVT_MENU(Exec_About, MyFrame::OnAbout)
144END_EVENT_TABLE()
145
146// Create a new application object: this macro will allow wxWindows to create
147// the application object during program execution (it's better than using a
148// static object for many reasons) and also declares the accessor function
149// wxGetApp() which will return the reference of the right type (i.e. MyApp and
150// not wxApp)
151IMPLEMENT_APP(MyApp)
152
153// ============================================================================
154// implementation
155// ============================================================================
156
157// ----------------------------------------------------------------------------
158// the application class
159// ----------------------------------------------------------------------------
160
161// `Main program' equivalent: the program execution "starts" here
162bool MyApp::OnInit()
163{
164 // Create the main application window
165 MyFrame *frame = new MyFrame(_T("Exec wxWindows sample"),
166 wxDefaultPosition, wxSize(500, 140));
167
168 // Show it and tell the application that it's our main window
169 frame->Show(TRUE);
170 SetTopWindow(frame);
171
172 // success: wxApp::OnRun() will be called which will enter the main message
173 // loop and the application will run. If we returned FALSE here, the
174 // application would exit immediately.
175 return TRUE;
176}
177
178// ----------------------------------------------------------------------------
179// main frame
180// ----------------------------------------------------------------------------
181
182// frame constructor
183MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
184 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
185{
186#ifdef __WXMAC__
187 // we need this in order to allow the about menu relocation, since ABOUT is
188 // not the default id of the about menu
189 wxApp::s_macAboutMenuItemId = Exec_About;
190#endif
191
192 // set the frame icon
e680a378 193#ifndef __WXGTK__
69c33c6c 194 SetIcon(wxICON(mondrian));
e680a378 195#endif
69c33c6c
VZ
196
197 // create a menu bar
198 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
199 menuFile->Append(Exec_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
200
201 wxMenu *execMenu = new wxMenu;
202 execMenu->Append(Exec_SyncExec, _T("Sync &execution...\tCtrl-E"),
203 _T("Launch a program and return when it terminates"));
204 execMenu->Append(Exec_AsyncExec, _T("&Async execution...\tCtrl-A"),
205 _T("Launch a program and return immediately"));
206 execMenu->Append(Exec_Shell, _T("Execute &shell command...\tCtrl-S"),
207 _T("Launch a shell and execute a command in it"));
208
d93c719a
VZ
209#ifdef __WINDOWS__
210 execMenu->AppendSeparator();
211 execMenu->Append(Exec_DDEExec, _T("Execute command via &DDE...\tCtrl-D"));
212#endif
213
69c33c6c
VZ
214 wxMenu *helpMenu = new wxMenu(_T(""), wxMENU_TEAROFF);
215 helpMenu->Append(Exec_About, _T("&About...\tF1"), _T("Show about dialog"));
216
217 // now append the freshly created menu to the menu bar...
218 wxMenuBar *menuBar = new wxMenuBar();
219 menuBar->Append(menuFile, _T("&File"));
220 menuBar->Append(execMenu, _T("&Exec"));
221 menuBar->Append(helpMenu, _T("&Help"));
222
223 // ... and attach this menu bar to the frame
224 SetMenuBar(menuBar);
225
226#if wxUSE_STATUSBAR
227 // create a status bar just for fun (by default with 1 pane only)
e680a378 228 CreateStatusBar();
69c33c6c
VZ
229 SetStatusText(_T("Welcome to wxWindows!"));
230#endif // wxUSE_STATUSBAR
231}
232
233
234// event handlers
235
236void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
237{
238 // TRUE is to force the frame to close
239 Close(TRUE);
240}
241
242void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
243{
244