]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/test.cpp
80aaeff9c1913ab6a770b0a2bede2c588adc833b
[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(TEST_QUIT, "E&xit");
53
54 wxMenu *helpMenu = new wxMenu;
55 helpMenu->Append(TEST_ABOUT, "&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 TestToolBar* toolRibbon = new TestToolBar(frame, -1, wxPoint(0, 0), wxSize(100, 30),
67 wxNO_BORDER|wxTB_FLAT, wxVERTICAL, 1);
68 toolRibbon->SetMargins(5, 5);
69
70 // Tell the frame about it
71 frame->SetToolBar(toolRibbon);
72
73 // Force a resize, just in case.
74 frame->OnSize(wxSizeEvent(wxSize(-1, -1), frame->GetId()));
75 frame->Show(TRUE);
76
77 frame->SetStatusText("Hello, wxWindows");
78
79 SetTopWindow(frame);
80
81 return TRUE;
82 }
83
84 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
85 EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
86 EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
87 EVT_MENU_HIGHLIGHT_ALL(MyFrame::OnMenuHighlight)
88 EVT_CLOSE(MyFrame::OnCloseWindow)
89 END_EVENT_TABLE()
90
91 // Define my frame constructor
92 MyFrame::MyFrame(wxFrame* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
93 const wxSize& size, long style):
94 wxFrame(parent, id, title, pos, size, style)
95 {
96 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
97 }
98
99 void MyFrame::OnQuit(wxCommandEvent& event)
100 {
101 Close(TRUE);
102 }
103
104 void MyFrame::OnAbout(wxCommandEvent& event)
105 {
106 (void)wxMessageBox("wxWindows wxToolBar demo\n", "About wxToolBar");
107 }
108
109 // Intercept menu item selection - only has an effect in Windows
110 void MyFrame::OnMenuHighlight(wxMenuEvent& event)
111 {
112 char *msg = NULL;
113 switch (event.GetMenuId())
114 {
115 case TEST_QUIT:
116 msg = "Quit program";
117 break;
118 case -1:
119 msg = "";
120 break;
121 }
122 if (msg)
123 SetStatusText(msg);
124 }
125
126 // Define the behaviour for the frame closing
127 // - must delete all frames except for the main one.
128 void MyFrame::OnCloseWindow(wxCloseEvent& event)
129 {
130 Destroy();
131 }
132
133 BEGIN_EVENT_TABLE(TestToolBar, wxToolBar95)
134 EVT_PAINT(TestToolBar::OnPaint)
135 END_EVENT_TABLE()
136
137 TestToolBar::TestToolBar(wxFrame* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
138 long style, int direction, int RowsOrColumns):
139 wxToolBar(parent, id, pos, size, style, direction, RowsOrColumns)
140 {
141 // Set up toolbar
142 wxBitmap* toolBarBitmaps[8];
143
144 #ifdef __WXMSW__
145 toolBarBitmaps[0] = new wxBitmap("icon1");
146 toolBarBitmaps[1] = new wxBitmap("icon2");
147 toolBarBitmaps[2] = new wxBitmap("icon3");
148 toolBarBitmaps[3] = new wxBitmap("icon4");
149 toolBarBitmaps[4] = new wxBitmap("icon5");
150 toolBarBitmaps[5] = new wxBitmap("icon6");
151 toolBarBitmaps[6] = new wxBitmap("icon7");
152 toolBarBitmaps[7] = new wxBitmap("icon8");
153 #endif
154 #ifdef __X__
155 // TODO
156 toolBarBitmaps[0] = new wxBitmap(...);
157 toolBarBitmaps[1] = new wxBitmap(...);
158 toolBarBitmaps[2] = new wxBitmap(...);
159 toolBarBitmaps[3] = new wxBitmap(...);
160 toolBarBitmaps[4] = new wxBitmap(...);
161 toolBarBitmaps[5] = new wxBitmap(...);
162 toolBarBitmaps[6] = new wxBitmap(...);
163 toolBarBitmaps[7] = new wxBitmap(...);
164
165 #endif
166
167 #ifdef __WXMSW__
168 int width = 24;
169 #else
170 int width = 16;
171 #endif
172 int offX = 5;
173 int currentX = 5;
174
175 AddTool(0, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "New file");
176 currentX += width + 5;
177 AddTool(1, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Open file");
178 currentX += width + 5;
179 AddTool(2, *(toolBarBitmaps[2]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Save file");
180 currentX += width + 5;
181 AddSeparator();
182 AddTool(3, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Copy");
183 currentX += width + 5;
184 AddTool(4, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Cut");
185 currentX += width + 5;
186 AddTool(5, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Paste");
187 currentX += width + 5;
188 AddSeparator();
189 AddTool(6, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Print");
190 currentX += width + 5;
191 AddSeparator();
192 AddTool(7, *(toolBarBitmaps[7]), wxNullBitmap, TRUE, currentX, -1, NULL, "Help");
193
194 CreateTools();
195
196 // Can delete the bitmaps since they're reference counted
197 int i;
198 for (i = 0; i < 8; i++)
199 delete toolBarBitmaps[i];
200 }
201
202 bool TestToolBar::OnLeftClick(int toolIndex, bool toggled)
203 {
204 char buf[200];
205 sprintf(buf, "Clicked on tool %d", toolIndex);
206 ((wxFrame*) GetParent())->SetStatusText(buf);
207 return TRUE;
208 }
209
210 void TestToolBar::OnMouseEnter(int toolIndex)
211 {
212 char buf[200];
213 if (toolIndex > -1)
214 {
215 sprintf(buf, "This is tool number %d", toolIndex);
216 ((wxFrame*)GetParent())->SetStatusText(buf);
217 }
218 else ((wxFrame*)GetParent())->SetStatusText("");
219 }
220
221 void TestToolBar::OnPaint(wxPaintEvent& event)
222 {
223 wxToolBar::OnPaint(event);
224
225 wxPaintDC dc(this);
226
227 int w, h;
228 GetSize(&w, &h);
229 dc.SetPen(wxBLACK_PEN);
230 dc.SetBrush(wxTRANSPARENT_BRUSH);
231 dc.DrawLine(0, h-1, w, h-1);
232 }
233
234