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