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