]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/test.cpp
Updated the toolbar sample to reflect updated event handling.
[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 "test.h"
25
26 IMPLEMENT_APP(MyApp)
27
28 #ifdef __X__
29 // TODO: include XBM or XPM icons for X apps
30 #endif
31
32 // The `main program' equivalent, creating the windows and returning the
33 // main frame
34 bool MyApp::OnInit(void)
35 {
36 // Create the main frame window
37 MyFrame* frame = new MyFrame(NULL, -1, "wxToolBar Sample", wxPoint(100, 100), wxSize(450, 300));
38
39 // Give it a status line
40 frame->CreateStatusBar();
41
42 // Give it an icon
43 #ifdef __WXMSW__
44 frame->SetIcon(wxIcon("mondrian"));
45 #endif
46 #ifdef __X__
47 frame->SetIcon(wxIcon("mondrian.xbm"));
48 #endif
49
50 // Make a menubar
51 wxMenu *fileMenu = new wxMenu;
52 fileMenu->Append(wxID_EXIT, "E&xit");
53
54 wxMenu *helpMenu = new wxMenu;
55 helpMenu->Append(wxID_HELP, "&About");
56
57 wxMenuBar* menuBar = new wxMenuBar;
58
59 menuBar->Append(fileMenu, "&File");
60 menuBar->Append(helpMenu, "&Help");
61
62 // Associate the menu bar with the frame
63 frame->SetMenuBar(menuBar);
64
65 // Create the toolbar
66 wxToolBar* toolBar = new wxToolBar(frame, -1, wxPoint(0, 0), wxSize(100, 30),
67 wxNO_BORDER|wxTB_FLAT, wxVERTICAL, 1);
68 toolBar->SetMargins(5, 5);
69
70 InitToolbar(toolBar);
71
72 // Tell the frame about it
73 frame->SetToolBar(toolBar);
74
75 // Force a resize. This should probably be replaced by a call to a wxFrame
76 // function that lays out default decorations and the remaining content window.
77 frame->OnSize(wxSizeEvent(wxSize(-1, -1), frame->GetId()));
78 frame->Show(TRUE);
79
80 frame->SetStatusText("Hello, wxWindows");
81
82 SetTopWindow(frame);
83
84 return TRUE;
85 }
86
87 bool MyApp::InitToolbar(wxToolBar* toolBar)
88 {
89 // Set up toolbar
90 wxBitmap* toolBarBitmaps[8];
91
92 #ifdef __WXMSW__
93 toolBarBitmaps[0] = new wxBitmap("icon1");
94 toolBarBitmaps[1] = new wxBitmap("icon2");
95 toolBarBitmaps[2] = new wxBitmap("icon3");
96 toolBarBitmaps[3] = new wxBitmap("icon4");
97 toolBarBitmaps[4] = new wxBitmap("icon5");
98 toolBarBitmaps[5] = new wxBitmap("icon6");
99 toolBarBitmaps[6] = new wxBitmap("icon7");
100 toolBarBitmaps[7] = new wxBitmap("icon8");
101 #endif
102 #ifdef __X__
103 // TODO
104 toolBarBitmaps[0] = new wxBitmap(...);
105 toolBarBitmaps[1] = new wxBitmap(...);
106 toolBarBitmaps[2] = new wxBitmap(...);
107 toolBarBitmaps[3] = new wxBitmap(...);
108 toolBarBitmaps[4] = new wxBitmap(...);
109 toolBarBitmaps[5] = new wxBitmap(...);
110 toolBarBitmaps[6] = new wxBitmap(...);
111 toolBarBitmaps[7] = new wxBitmap(...);
112 #endif
113
114 #ifdef __WXMSW__
115 int width = 24;
116 #else
117 int width = 16;
118 #endif
119 int offX = 5;
120 int currentX = 5;
121
122 toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "New file");
123 currentX += width + 5;
124 toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Open file");
125 currentX += width + 5;
126 toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Save file");
127 currentX += width + 5;
128 toolBar->AddSeparator();
129 toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Copy");
130 currentX += width + 5;
131 toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Cut");
132 currentX += width + 5;
133 toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Paste");
134 currentX += width + 5;
135 toolBar->AddSeparator();
136 toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Print");
137 currentX += width + 5;
138 toolBar->AddSeparator();
139 toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), wxNullBitmap, FALSE, currentX, -1, NULL, "Help");
140
141 toolBar->CreateTools();
142
143 // Can delete the bitmaps since they're reference counted
144 int i;
145 for (i = 0; i < 8; i++)
146 delete toolBarBitmaps[i];
147
148 return TRUE;
149 }
150
151 // wxID_HELP will be processed for the 'About' menu and the toolbar help button.
152
153 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
154 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
155 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
156 EVT_CLOSE(MyFrame::OnCloseWindow)
157 EVT_TOOL_RANGE(wxID_OPEN, wxID_PASTE, MyFrame::OnToolLeftClick)
158 EVT_TOOL_ENTER_RANGE(wxID_OPEN, wxID_PASTE, MyFrame::OnToolEnter)
159 END_EVENT_TABLE()
160
161 // Define my frame constructor
162 MyFrame::MyFrame(wxFrame* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
163 const wxSize& size, long style):
164 wxFrame(parent, id, title, pos, size, style)
165 {
166 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
167 }
168
169 void MyFrame::OnQuit(wxCommandEvent& event)
170 {
171 Close(TRUE);
172 }
173
174 void MyFrame::OnAbout(wxCommandEvent& event)
175 {
176 (void)wxMessageBox("wxWindows wxToolBar demo\n", "About wxToolBar");
177 }
178
179 // Define the behaviour for the frame closing
180 // - must delete all frames except for the main one.
181 void MyFrame::OnCloseWindow(wxCloseEvent& event)
182 {
183 Destroy();
184 }
185
186 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
187 {
188 wxString str;
189 str.Printf("Clicked on tool %d", event.GetId());
190 SetStatusText(str);
191 }
192
193 void MyFrame::OnToolEnter(wxCommandEvent& event)
194 {
195 if (event.GetId() > -1)
196 {
197 wxString str;
198 str.Printf("This is tool number %d", event.GetId());
199 SetStatusText(str);
200 }
201 else
202 SetStatusText("");
203 }
204