1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolBar sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/toolbar.h"
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"
43 // The `main program' equivalent, creating the windows and returning the
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));
51 // Give it a status line
52 frame
->CreateStatusBar();
55 frame
->SetIcon(wxICON(mondrian
));
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", "");
63 wxMenu
*fileMenu
= new wxMenu
;
64 fileMenu
->Append(wxID_EXIT
, "E&xit", "Quit toolbar sample" );
66 wxMenu
*helpMenu
= new wxMenu
;
67 helpMenu
->Append(wxID_HELP
, "&About", "About toolbar sample");
69 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
71 menuBar
->Append(fileMenu
, "&File");
72 menuBar
->Append(tbarMenu
, "&Toolbar");
73 menuBar
->Append(helpMenu
, "&Help");
75 // Associate the menu bar with the frame
76 frame
->SetMenuBar(menuBar
);
79 frame
->CreateToolBar(wxNO_BORDER
|wxTB_HORIZONTAL
|wxTB_FLAT
|wxTB_DOCKABLE
, ID_TOOLBAR
);
81 frame
->GetToolBar()->SetMargins( 2, 2 );
83 InitToolbar(frame
->GetToolBar());
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());
91 frame
->SetStatusText("Hello, wxWindows");
98 bool MyApp::InitToolbar(wxToolBar
* toolBar
, bool small
)
101 wxBitmap
* toolBarBitmaps
[8];
104 toolBarBitmaps
[0] = new wxBitmap("icon1");
105 toolBarBitmaps
[1] = new wxBitmap("icon2");
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");
116 toolBarBitmaps
[0] = new wxBitmap( new_xpm
);
117 toolBarBitmaps
[1] = new wxBitmap( open_xpm
);
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
);
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");
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");
158 toolBar
->EnableTool( wxID_PRINT
, FALSE
);
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
];
171 // wxID_HELP will be processed for the 'About' menu and the toolbar help button.
173 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
174 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
175 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
177 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR
, MyFrame::OnToggleToolbar
)
178 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
179 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
181 EVT_MENU(-1, MyFrame::OnToolLeftClick
)
182 EVT_TOOL_ENTER(ID_TOOLBAR
, MyFrame::OnToolEnter
)
185 // Define my frame constructor
186 MyFrame::MyFrame(wxFrame
* parent
,
188 const wxString
& title
,
192 : wxFrame(parent
, id
, title
, pos
, size
, style
)
194 m_textWindow
= new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE
);
195 m_smallToolbar
= FALSE
;
198 void MyFrame::OnToggleToolbar(wxCommandEvent
& event
)
202 CreateToolBar(wxNO_BORDER
|wxTB_HORIZONTAL
|wxTB_FLAT
|wxTB_DOCKABLE
, ID_TOOLBAR
);
204 m_smallToolbar
= !m_smallToolbar
;
205 wxGetApp().InitToolbar(GetToolBar(), m_smallToolbar
);
208 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
213 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
215 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
218 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
221 str
.Printf( _T("Clicked on tool %d\n"), event
.GetId());
222 m_textWindow
->WriteText( str
);
224 if (event
.GetId() == wxID_HELP
)
226 if ((bool)event
.GetExtraLong())
227 m_textWindow
->WriteText( _T("Help button down now.\n") );
229 m_textWindow
->WriteText( _T("Help button up now.\n") );
232 if (event
.GetId() == wxID_COPY
)
237 if (event
.GetId() == wxID_CUT
)
243 void MyFrame::DoEnablePrint()
245 wxToolBar
*tb
= GetToolBar();
246 if (tb
->GetToolEnabled(wxID_PRINT
))
247 tb
->EnableTool( wxID_PRINT
, FALSE
);
249 tb
->EnableTool( wxID_PRINT
, TRUE
);
252 void MyFrame::DoToggleHelp()
254 wxToolBar
*tb
= GetToolBar();
255 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
258 void MyFrame::OnToolEnter(wxCommandEvent
& event
)
260 if (event
.GetSelection() > -1)
263 str
.Printf(_T("This is tool number %d"), event
.GetSelection());