]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/test.cpp
added GetTextExtent test
[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()
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 *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", "");
62
63 wxMenu *fileMenu = new wxMenu;
64 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
65
66 wxMenu *helpMenu = new wxMenu;
67 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
68
69 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
70
71 menuBar->Append(fileMenu, "&File");
72 menuBar->Append(tbarMenu, "&Toolbar");
73 menuBar->Append(helpMenu, "&Help");
74
75 // Associate the menu bar with the frame
76 frame->SetMenuBar(menuBar);
77
78 // Create the toolbar
79 frame->CreateToolBar(wxNO_BORDER|wxTB_HORIZONTAL|wxTB_FLAT|wxTB_DOCKABLE, ID_TOOLBAR);
80
81 frame->GetToolBar()->SetMargins( 2, 2 );
82
83 InitToolbar(frame->GetToolBar());
84
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;
96 }
97
98 bool MyApp::InitToolbar(wxToolBar* toolBar, bool small)
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");
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 }
115 #else
116 toolBarBitmaps[0] = new wxBitmap( new_xpm );
117 toolBarBitmaps[1] = new wxBitmap( open_xpm );
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 }
127 #endif
128
129 #ifdef __WXMSW__
130 int width = 24;
131 #else
132 int width = 16;
133 #endif
134 int currentX = 5;
135
136 toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
137 currentX += width + 5;
138 toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
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 }
160
161 toolBar->Realize();
162
163 // Can delete the bitmaps since they're reference counted
164 int i, max = small ? 2 : WXSIZEOF(toolBarBitmaps);
165 for (i = 0; i < max; i++)
166 delete toolBarBitmaps[i];
167
168 return TRUE;
169 }
170
171 // wxID_HELP will be processed for the 'About' menu and the toolbar help button.
172
173 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
174 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
175 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
176
177 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR, MyFrame::OnToggleToolbar)
178 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
179 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
180
181 EVT_MENU(-1, MyFrame::OnToolLeftClick)
182 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
183 END_EVENT_TABLE()
184
185 // Define my frame constructor
186 MyFrame::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)
193 {
194 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
195 m_smallToolbar = FALSE;
196 }
197
198 void 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);
206 }
207
208 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
209 {
210 Close(TRUE);
211 }
212
213 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
214 {
215 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
216 }
217
218 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
219 {
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 {
234 DoEnablePrint();
235 }
236
237 if (event.GetId() == wxID_CUT)
238 {
239 DoToggleHelp();
240 }
241 }
242
243 void 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
252 void MyFrame::DoToggleHelp()
253 {
254 wxToolBar *tb = GetToolBar();
255 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
256 }
257
258 void MyFrame::OnToolEnter(wxCommandEvent& event)
259 {
260 if (event.GetSelection() > -1)
261 {
262 wxString str;
263 str.Printf(_T("This is tool number %d"), event.GetSelection());
264 SetStatusText(str);
265 }
266 else
267 SetStatusText("");
268 }
269