]> git.saurik.com Git - wxWidgets.git/blame - samples/toolbar/test.cpp
added GetTextExtent test
[wxWidgets.git] / samples / toolbar / test.cpp
CommitLineData
14d1ccd8
JS
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"
8bbe427f
VZ
24#include <wx/log.h>
25
14d1ccd8
JS
26#include "test.h"
27
a4294b78 28#if defined(__WXGTK__) || defined(__WXMOTIF__)
47908e25
RR
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
14d1ccd8
JS
41IMPLEMENT_APP(MyApp)
42
14d1ccd8
JS
43// The `main program' equivalent, creating the windows and returning the
44// main frame
7fb23305 45bool MyApp::OnInit()
14d1ccd8 46{
7fb23305
VZ
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));
14d1ccd8 50
7fb23305
VZ
51 // Give it a status line
52 frame->CreateStatusBar();
14d1ccd8 53
7fb23305
VZ
54 // Give it an icon
55 frame->SetIcon(wxICON(mondrian));
14d1ccd8 56
7fb23305
VZ
57 // Make a menubar
58 wxMenu *tbarMenu = new wxMenu;
59 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBAR, "&Toggle toolbar", "Change the toolbar kind");
60 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button", "");
61 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button", "");
14d1ccd8 62
7fb23305
VZ
63 wxMenu *fileMenu = new wxMenu;
64 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
14d1ccd8 65
7fb23305
VZ
66 wxMenu *helpMenu = new wxMenu;
67 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
14d1ccd8 68
7fb23305 69 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
14d1ccd8 70
7fb23305
VZ
71 menuBar->Append(fileMenu, "&File");
72 menuBar->Append(tbarMenu, "&Toolbar");
73 menuBar->Append(helpMenu, "&Help");
14d1ccd8 74
7fb23305
VZ
75 // Associate the menu bar with the frame
76 frame->SetMenuBar(menuBar);
14d1ccd8 77
7fb23305
VZ
78 // Create the toolbar
79 frame->CreateToolBar(wxNO_BORDER|wxTB_HORIZONTAL|wxTB_FLAT|wxTB_DOCKABLE, ID_TOOLBAR);
14d1ccd8 80
7fb23305 81 frame->GetToolBar()->SetMargins( 2, 2 );
14d1ccd8 82
7fb23305 83 InitToolbar(frame->GetToolBar());
14d1ccd8 84
7fb23305
VZ
85 // Force a resize. This should probably be replaced by a call to a wxFrame
86 // function that lays out default decorations and the remaining content window.
87 wxSizeEvent event(wxSize(-1, -1), frame->GetId());
88 frame->OnSize(event);
89 frame->Show(TRUE);
90
91 frame->SetStatusText("Hello, wxWindows");
92
93 SetTopWindow(frame);
94
95 return TRUE;
14d1ccd8
JS
96}
97
7fb23305 98bool MyApp::InitToolbar(wxToolBar* toolBar, bool small)
14d1ccd8
JS
99{
100 // Set up toolbar
101 wxBitmap* toolBarBitmaps[8];
102
103#ifdef __WXMSW__
104 toolBarBitmaps[0] = new wxBitmap("icon1");
105 toolBarBitmaps[1] = new wxBitmap("icon2");
7fb23305
VZ
106 if ( !small )
107 {
108 toolBarBitmaps[2] = new wxBitmap("icon3");
109 toolBarBitmaps[3] = new wxBitmap("icon4");
110 toolBarBitmaps[4] = new wxBitmap("icon5");
111 toolBarBitmaps[5] = new wxBitmap("icon6");
112 toolBarBitmaps[6] = new wxBitmap("icon7");
113 toolBarBitmaps[7] = new wxBitmap("icon8");
114 }
47908e25
RR
115#else
116 toolBarBitmaps[0] = new wxBitmap( new_xpm );
117 toolBarBitmaps[1] = new wxBitmap( open_xpm );
7fb23305
VZ
118 if ( !small )
119 {
120 toolBarBitmaps[2] = new wxBitmap( save_xpm );
121 toolBarBitmaps[3] = new wxBitmap( copy_xpm );
122 toolBarBitmaps[4] = new wxBitmap( cut_xpm );
123 toolBarBitmaps[5] = new wxBitmap( preview_xpm );
124 toolBarBitmaps[6] = new wxBitmap( print_xpm );
125 toolBarBitmaps[7] = new wxBitmap( help_xpm );
126 }
14d1ccd8
JS
127#endif
128
129#ifdef __WXMSW__
130 int width = 24;
131#else
132 int width = 16;
133#endif
14d1ccd8
JS
134 int currentX = 5;
135
a4294b78 136 toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
14d1ccd8 137 currentX += width + 5;
a4294b78 138 toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
7fb23305
VZ
139
140 if ( !small )
141 {
142 currentX += width + 5;
143 toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
144 currentX += width + 5;
145 toolBar->AddSeparator();
146 toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Disable/Enable print button");
147 currentX += width + 5;
148 toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button");
149 currentX += width + 5;
150 toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
151 currentX += width + 5;
152 toolBar->AddSeparator();
153 toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
154 currentX += width + 5;
155 toolBar->AddSeparator();
156 toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), *(toolBarBitmaps[6]), TRUE, currentX, -1, (wxObject *) NULL, "Help");
157
158 toolBar->EnableTool( wxID_PRINT, FALSE );
159 }
14d1ccd8 160
81d66cf3 161 toolBar->Realize();
14d1ccd8
JS
162
163 // Can delete the bitmaps since they're reference counted
7fb23305
VZ
164 int i, max = small ? 2 : WXSIZEOF(toolBarBitmaps);
165 for (i = 0; i < max; i++)
14d1ccd8 166 delete toolBarBitmaps[i];
13437238
JS
167
168 return TRUE;
14d1ccd8
JS
169}
170
13437238
JS
171// wxID_HELP will be processed for the 'About' menu and the toolbar help button.
172
173BEGIN_EVENT_TABLE(MyFrame, wxFrame)
174 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
402a82c6 175 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
7fb23305 176
67821630
VS
177 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR, MyFrame::OnToggleToolbar)
178 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
179 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
7fb23305 180
e179bd65 181 EVT_MENU(-1, MyFrame::OnToolLeftClick)
777553d2 182 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
13437238
JS
183END_EVENT_TABLE()
184
185// Define my frame constructor
7fb23305
VZ
186MyFrame::MyFrame(wxFrame* parent,
187 wxWindowID id,
188 const wxString& title,
189 const wxPoint& pos,
190 const wxSize& size,
191 long style)
192 : wxFrame(parent, id, title, pos, size, style)
14d1ccd8 193{
e179bd65 194 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
7fb23305
VZ
195 m_smallToolbar = FALSE;
196}
197
198void MyFrame::OnToggleToolbar(wxCommandEvent& event)
199{
200 delete GetToolBar();
201 SetToolBar(NULL);
202 CreateToolBar(wxNO_BORDER|wxTB_HORIZONTAL|wxTB_FLAT|wxTB_DOCKABLE, ID_TOOLBAR);
203
204 m_smallToolbar = !m_smallToolbar;
205 wxGetApp().InitToolbar(GetToolBar(), m_smallToolbar);
14d1ccd8
JS
206}
207
47908e25 208void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
14d1ccd8 209{
13437238 210 Close(TRUE);
14d1ccd8
JS
211}
212
47908e25 213void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
14d1ccd8 214{
1d5b7a0b 215 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
13437238 216}
14d1ccd8 217
13437238
JS
218void MyFrame::OnToolLeftClick(wxCommandEvent& event)
219{
e179bd65
RR
220 wxString str;
221 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
222 m_textWindow->WriteText( str );
223
224 if (event.GetId() == wxID_HELP)
225 {
226 if ((bool)event.GetExtraLong())
227 m_textWindow->WriteText( _T("Help button down now.\n") );
228 else
229 m_textWindow->WriteText( _T("Help button up now.\n") );
230 }
231
232 if (event.GetId() == wxID_COPY)
233 {
7fb23305 234 DoEnablePrint();
e179bd65
RR
235 }
236
237 if (event.GetId() == wxID_CUT)
238 {
7fb23305 239 DoToggleHelp();
e179bd65 240 }
14d1ccd8
JS
241}
242
7fb23305
VZ
243void MyFrame::DoEnablePrint()
244{
245 wxToolBar *tb = GetToolBar();
246 if (tb->GetToolEnabled(wxID_PRINT))
247 tb->EnableTool( wxID_PRINT, FALSE );
248 else
249 tb->EnableTool( wxID_PRINT, TRUE );
250}
251
252void MyFrame::DoToggleHelp()
253{
254 wxToolBar *tb = GetToolBar();
255 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
256}
257
13437238
JS
258void MyFrame::OnToolEnter(wxCommandEvent& event)
259{
81d66cf3 260 if (event.GetSelection() > -1)
13437238
JS
261 {
262 wxString str;
58dea4b0 263 str.Printf(_T("This is tool number %d"), event.GetSelection());
13437238
JS
264 SetStatusText(str);
265 }
266 else
267 SetStatusText("");
268}
14d1ccd8 269