]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/test.cpp
"wxGDIObject * => &" related changes (see mail to the list)
[wxWidgets.git] / samples / toolbar / test.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: test.cpp
3 // Purpose: wxToolBar sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/toolbar.h"
24 #include <wx/log.h>
25
26 #include "test.h"
27
28 #if defined(__WXGTK__) || defined(__WXMOTIF__)
29 #include "mondrian.xpm"
30 #include "bitmaps/new.xpm"
31 #include "bitmaps/open.xpm"
32 #include "bitmaps/save.xpm"
33 #include "bitmaps/copy.xpm"
34 #include "bitmaps/cut.xpm"
35 // #include "bitmaps/paste.xpm"
36 #include "bitmaps/print.xpm"
37 #include "bitmaps/preview.xpm"
38 #include "bitmaps/help.xpm"
39 #endif
40
41 IMPLEMENT_APP(MyApp)
42
43 // The `main program' equivalent, creating the windows and returning the
44 // main frame
45 bool MyApp::OnInit(void)
46 {
47 // Create the main frame window
48 MyFrame* frame = new MyFrame((wxFrame *) NULL, -1, (const wxString) "wxToolBar Sample",
49 wxPoint(100, 100), wxSize(450, 300));
50
51 // Give it a status line
52 frame->CreateStatusBar();
53
54 // Give it an icon
55 frame->SetIcon(wxICON(mondrian));
56
57 // Make a menubar
58 wxMenu *fileMenu = new wxMenu;
59 fileMenu->Append(wxID_EXIT, "E&xit");
60
61 wxMenu *helpMenu = new wxMenu;
62 helpMenu->Append(wxID_HELP, "&About");
63
64 wxMenuBar* menuBar = new wxMenuBar;
65
66 menuBar->Append(fileMenu, "&File");
67 menuBar->Append(helpMenu, "&Help");
68
69 // Associate the menu bar with the frame
70 frame->SetMenuBar(menuBar);
71
72 // Create the toolbar
73 frame->CreateToolBar(wxNO_BORDER|wxHORIZONTAL|wxTB_FLAT, ID_TOOLBAR);
74
75 InitToolbar(frame->GetToolBar());
76
77 // Force a resize. This should probably be replaced by a call to a wxFrame
78 // function that lays out default decorations and the remaining content window.
79 wxSizeEvent event(wxSize(-1, -1), frame->GetId());
80 frame->OnSize(event);
81 frame->Show(TRUE);
82
83 frame->SetStatusText("Hello, wxWindows");
84
85 SetTopWindow(frame);
86
87 return TRUE;
88 }
89
90 bool MyApp::InitToolbar(wxToolBar* toolBar)
91 {
92 // Set up toolbar
93 wxBitmap* toolBarBitmaps[8];
94
95 #ifdef __WXMSW__
96 toolBarBitmaps[0] = new wxBitmap("icon1");
97 toolBarBitmaps[1] = new wxBitmap("icon2");
98 toolBarBitmaps[2] = new wxBitmap("icon3");
99 toolBarBitmaps[3] = new wxBitmap("icon4");
100 toolBarBitmaps[4] = new wxBitmap("icon5");
101 toolBarBitmaps[5] = new wxBitmap("icon6");
102 toolBarBitmaps[6] = new wxBitmap("icon7");
103 toolBarBitmaps[7] = new wxBitmap("icon8");
104 #else
105 toolBarBitmaps[0] = new wxBitmap( new_xpm );
106 toolBarBitmaps[1] = new wxBitmap( open_xpm );
107 toolBarBitmaps[2] = new wxBitmap( save_xpm );
108 toolBarBitmaps[3] = new wxBitmap( copy_xpm );
109 toolBarBitmaps[4] = new wxBitmap( cut_xpm );
110 // toolBarBitmaps[5] = new wxBitmap( paste_xpm );
111 toolBarBitmaps[5] = new wxBitmap( preview_xpm );
112 toolBarBitmaps[6] = new wxBitmap( print_xpm );
113 toolBarBitmaps[7] = new wxBitmap( help_xpm );
114 #endif
115
116 #ifdef __WXMSW__
117 int width = 24;
118 #else
119 int width = 16;
120 #endif
121 int currentX = 5;
122
123 toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
124 currentX += width + 5;
125 toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
126 currentX += width + 5;
127 toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
128 currentX += width + 5;
129 toolBar->AddSeparator();
130 toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
131 currentX += width + 5;
132 toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
133 currentX += width + 5;
134 toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
135 currentX += width + 5;
136 toolBar->AddSeparator();
137 toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
138 currentX += width + 5;
139 toolBar->AddSeparator();
140 toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Help");
141
142 toolBar->Realize();
143
144 // Can delete the bitmaps since they're reference counted
145 int i;
146 for (i = 0; i < 8; i++)
147 delete toolBarBitmaps[i];
148
149 return TRUE;
150 }
151
152 // wxID_HELP will be processed for the 'About' menu and the toolbar help button.
153
154 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
155 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
156 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
157 EVT_CLOSE(MyFrame::OnCloseWindow)
158 EVT_TOOL_RANGE(wxID_OPEN, wxID_PASTE, MyFrame::OnToolLeftClick)
159 EVT_TOOL_ENTER(wxID_OPEN, MyFrame::OnToolEnter)
160 END_EVENT_TABLE()
161
162 // Define my frame constructor
163 MyFrame::MyFrame(wxFrame* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
164 const wxSize& size, long style):
165 wxFrame(parent, id, title, pos, size, style), m_timer(this)
166 {
167 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
168 }
169
170 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
171 {
172 Close(TRUE);
173 }
174
175 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
176 {
177 // (void)wxMessageBox("wxWindows wxToolBar demo\n", "About wxToolBar");
178 wxLogStatus("Started timer.");
179
180 m_timer.Start(500, TRUE);
181 }
182
183 // Define the behaviour for the frame closing
184 // - must delete all frames except for the main one.
185 void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
186 {
187 Destroy();
188 }
189
190 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
191 {
192 wxString str;
193 str.Printf("Clicked on tool %d", event.GetId());
194 SetStatusText(str);
195 }
196
197 void MyFrame::OnToolEnter(wxCommandEvent& event)
198 {
199 if (event.GetSelection() > -1)
200 {
201 wxString str;
202 str.Printf("This is tool number %d", event.GetSelection());
203 SetStatusText(str);
204 }
205 else
206 SetStatusText("");
207 }
208