toolbar.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: toolbar.cpp
  3. // Purpose: wxToolBar sample
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 04/01/98
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #ifndef WX_PRECOMP
  22. #include "wx/wx.h"
  23. #endif
  24. #include "wx/toolbar.h"
  25. #include "wx/log.h"
  26. #include "wx/image.h"
  27. #include "wx/filedlg.h"
  28. #include "wx/colordlg.h"
  29. #include "wx/srchctrl.h"
  30. // define this to use XPMs everywhere (by default, BMPs are used under Win)
  31. // BMPs use less space, but aren't compiled into the executable on other platforms
  32. #ifdef __WINDOWS__
  33. #define USE_XPM_BITMAPS 0
  34. #else
  35. #define USE_XPM_BITMAPS 1
  36. #endif
  37. // If this is 1, the sample will test an extra toolbar identical to the
  38. // main one, but not managed by the frame. This can test subtle differences
  39. // in the way toolbars are handled, especially on Mac where there is one
  40. // native, 'installed' toolbar.
  41. #define USE_UNMANAGED_TOOLBAR 0
  42. // Define this as 0 for the platforms not supporting controls in toolbars
  43. #define USE_CONTROLS_IN_TOOLBAR 1
  44. // ----------------------------------------------------------------------------
  45. // resources
  46. // ----------------------------------------------------------------------------
  47. #ifndef wxHAS_IMAGES_IN_RESOURCES
  48. #include "../sample.xpm"
  49. #endif
  50. #if USE_XPM_BITMAPS
  51. #include "bitmaps/new.xpm"
  52. #include "bitmaps/open.xpm"
  53. #include "bitmaps/save.xpm"
  54. #include "bitmaps/copy.xpm"
  55. #include "bitmaps/cut.xpm"
  56. #include "bitmaps/preview.xpm" // paste XPM
  57. #include "bitmaps/print.xpm"
  58. #include "bitmaps/help.xpm"
  59. #endif // USE_XPM_BITMAPS
  60. enum Positions
  61. {
  62. TOOLBAR_LEFT,
  63. TOOLBAR_TOP,
  64. TOOLBAR_RIGHT,
  65. TOOLBAR_BOTTOM
  66. };
  67. // ----------------------------------------------------------------------------
  68. // classes
  69. // ----------------------------------------------------------------------------
  70. // Define a new application
  71. class MyApp : public wxApp
  72. {
  73. public:
  74. bool OnInit();
  75. };
  76. // Define a new frame
  77. class MyFrame: public wxFrame
  78. {
  79. public:
  80. MyFrame(wxFrame *parent,
  81. wxWindowID id = wxID_ANY,
  82. const wxString& title = wxT("wxToolBar Sample"),
  83. const wxPoint& pos = wxDefaultPosition,
  84. const wxSize& size = wxDefaultSize,
  85. long style = wxDEFAULT_FRAME_STYLE|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE);
  86. virtual ~MyFrame();
  87. void PopulateToolbar(wxToolBarBase* toolBar);
  88. void RecreateToolbar();
  89. void OnQuit(wxCommandEvent& event);
  90. void OnAbout(wxCommandEvent& event);
  91. void OnSize(wxSizeEvent& event);
  92. void OnToggleToolbar(wxCommandEvent& event);
  93. void OnToggleAnotherToolbar(wxCommandEvent& event);
  94. void OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event));
  95. void OnToggleToolbarSize(wxCommandEvent& event);
  96. void OnChangeOrientation(wxCommandEvent& event);
  97. void OnToggleToolbarRows(wxCommandEvent& event);
  98. void OnToggleTooltips(wxCommandEvent& event);
  99. void OnToggleCustomDisabled(wxCommandEvent& event);
  100. void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); }
  101. void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); }
  102. void OnInsertPrint(wxCommandEvent& event);
  103. void OnChangeToolTip(wxCommandEvent& event);
  104. void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); }
  105. void OnToggleSearch(wxCommandEvent& event);
  106. void OnToggleRadioBtn(wxCommandEvent& event);
  107. void OnToolbarStyle(wxCommandEvent& event);
  108. void OnToolbarBgCol(wxCommandEvent& event);
  109. void OnToolbarCustomBitmap(wxCommandEvent& event);
  110. void OnToolLeftClick(wxCommandEvent& event);
  111. void OnToolRightClick(wxCommandEvent& event);
  112. void OnToolDropdown(wxCommandEvent& event);
  113. void OnCombo(wxCommandEvent& event);
  114. void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
  115. void OnUpdateToggleHorzText(wxUpdateUIEvent& event);
  116. void OnUpdateToggleRadioBtn(wxUpdateUIEvent& event)
  117. { event.Enable( m_tbar != NULL ); }
  118. private:
  119. void DoEnablePrint();
  120. void DoDeletePrint();
  121. void DoToggleHelp();
  122. void LayoutChildren();
  123. bool m_smallToolbar,
  124. m_horzText,
  125. m_useCustomDisabled,
  126. m_showTooltips;
  127. size_t m_rows; // 1 or 2 only
  128. // the number of print buttons we have (they're added/removed dynamically)
  129. size_t m_nPrint;
  130. // store toolbar position for future use
  131. Positions m_toolbarPosition;
  132. wxTextCtrl *m_textWindow;
  133. wxPanel *m_panel;
  134. #if USE_UNMANAGED_TOOLBAR
  135. wxToolBar *m_extraToolBar;
  136. #endif
  137. wxToolBar *m_tbar;
  138. // the path to the custom bitmap for the test toolbar tool
  139. wxString m_pathBmp;
  140. // the search tool, initially NULL
  141. wxToolBarToolBase *m_searchTool;
  142. wxDECLARE_EVENT_TABLE();
  143. };
  144. // ----------------------------------------------------------------------------
  145. // constants
  146. // ----------------------------------------------------------------------------
  147. const int ID_TOOLBAR = 500;
  148. static const long TOOLBAR_STYLE = wxTB_FLAT | wxTB_DOCKABLE | wxTB_TEXT;
  149. enum
  150. {
  151. // toolbar menu items
  152. IDM_TOOLBAR_TOGGLE_TOOLBAR = 200,
  153. IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
  154. IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
  155. IDM_TOOLBAR_TOGGLETOOLBARSIZE,
  156. IDM_TOOLBAR_TOGGLETOOLBARROWS,
  157. IDM_TOOLBAR_TOGGLETOOLTIPS,
  158. IDM_TOOLBAR_TOGGLECUSTOMDISABLED,
  159. IDM_TOOLBAR_SHOW_TEXT,
  160. IDM_TOOLBAR_SHOW_ICONS,
  161. IDM_TOOLBAR_SHOW_BOTH,
  162. IDM_TOOLBAR_BG_COL,
  163. IDM_TOOLBAR_CUSTOM_PATH,
  164. IDM_TOOLBAR_TOP_ORIENTATION,
  165. IDM_TOOLBAR_LEFT_ORIENTATION,
  166. IDM_TOOLBAR_BOTTOM_ORIENTATION,
  167. IDM_TOOLBAR_RIGHT_ORIENTATION,
  168. IDM_TOOLBAR_OTHER_1,
  169. IDM_TOOLBAR_OTHER_2,
  170. IDM_TOOLBAR_OTHER_3,
  171. IDM_TOOLBAR_OTHER_4,
  172. // tools menu items
  173. IDM_TOOLBAR_ENABLEPRINT,
  174. IDM_TOOLBAR_DELETEPRINT,
  175. IDM_TOOLBAR_INSERTPRINT,
  176. IDM_TOOLBAR_TOGGLEHELP,
  177. IDM_TOOLBAR_TOGGLESEARCH,
  178. IDM_TOOLBAR_TOGGLERADIOBTN1,
  179. IDM_TOOLBAR_TOGGLERADIOBTN2,
  180. IDM_TOOLBAR_TOGGLERADIOBTN3,
  181. IDM_TOOLBAR_CHANGE_TOOLTIP,
  182. ID_COMBO = 1000
  183. };
  184. // ----------------------------------------------------------------------------
  185. // event tables
  186. // ----------------------------------------------------------------------------
  187. // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
  188. // help button.
  189. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  190. EVT_SIZE(MyFrame::OnSize)
  191. EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
  192. EVT_MENU(wxID_HELP, MyFrame::OnAbout)
  193. EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR, MyFrame::OnToggleToolbar)
  194. EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, MyFrame::OnToggleAnotherToolbar)
  195. EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, MyFrame::OnToggleHorizontalText)
  196. EVT_MENU_RANGE(IDM_TOOLBAR_TOP_ORIENTATION, IDM_TOOLBAR_RIGHT_ORIENTATION, MyFrame::OnChangeOrientation)
  197. EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
  198. EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
  199. EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS, MyFrame::OnToggleTooltips)
  200. EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED, MyFrame::OnToggleCustomDisabled)
  201. EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
  202. EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
  203. EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
  204. EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
  205. EVT_MENU(IDM_TOOLBAR_TOGGLESEARCH, MyFrame::OnToggleSearch)
  206. EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1, IDM_TOOLBAR_TOGGLERADIOBTN3,
  207. MyFrame::OnToggleRadioBtn)
  208. EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP, MyFrame::OnChangeToolTip)
  209. EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT, IDM_TOOLBAR_SHOW_BOTH,
  210. MyFrame::OnToolbarStyle)
  211. EVT_MENU(IDM_TOOLBAR_BG_COL, MyFrame::OnToolbarBgCol)
  212. EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH, MyFrame::OnToolbarCustomBitmap)
  213. EVT_MENU(wxID_ANY, MyFrame::OnToolLeftClick)
  214. EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
  215. EVT_TOOL_RCLICKED(wxID_ANY, MyFrame::OnToolRightClick)
  216. EVT_TOOL_DROPDOWN(wxID_ANY, MyFrame::OnToolDropdown)
  217. EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
  218. EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
  219. EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1,
  220. IDM_TOOLBAR_TOGGLERADIOBTN3,
  221. MyFrame::OnUpdateToggleRadioBtn)
  222. EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
  223. MyFrame::OnUpdateToggleHorzText)
  224. wxEND_EVENT_TABLE()
  225. // ============================================================================
  226. // implementation
  227. // ============================================================================
  228. // ----------------------------------------------------------------------------
  229. // MyApp
  230. // ----------------------------------------------------------------------------
  231. IMPLEMENT_APP(MyApp)
  232. // The `main program' equivalent, creating the windows and returning the
  233. // main frame
  234. bool MyApp::OnInit()
  235. {
  236. if ( !wxApp::OnInit() )
  237. return false;
  238. // Create the main frame window
  239. MyFrame* frame = new MyFrame((wxFrame *) NULL, wxID_ANY,
  240. wxT("wxToolBar Sample"),
  241. wxPoint(100, 100), wxSize(650, 350));
  242. frame->Show(true);
  243. #if wxUSE_STATUSBAR
  244. frame->SetStatusText(wxT("Hello, wxWidgets"));
  245. #endif
  246. wxInitAllImageHandlers();
  247. return true;
  248. }
  249. void MyFrame::RecreateToolbar()
  250. {
  251. #ifdef __WXWINCE__
  252. // On Windows CE, we should not delete the
  253. // previous toolbar in case it contains the menubar.
  254. // We'll try to accommodate this usage in due course.
  255. wxToolBar* toolBar = CreateToolBar();
  256. #else
  257. // delete and recreate the toolbar
  258. wxToolBarBase *toolBar = GetToolBar();
  259. long style = toolBar ? toolBar->GetWindowStyle() : TOOLBAR_STYLE;
  260. if (toolBar && m_searchTool && m_searchTool->GetToolBar() == NULL)
  261. {
  262. // see ~MyFrame()
  263. toolBar->AddTool(m_searchTool);
  264. }
  265. m_searchTool = NULL;
  266. delete toolBar;
  267. SetToolBar(NULL);
  268. style &= ~(wxTB_HORIZONTAL | wxTB_VERTICAL | wxTB_BOTTOM | wxTB_RIGHT | wxTB_HORZ_LAYOUT);
  269. switch( m_toolbarPosition )
  270. {
  271. case TOOLBAR_LEFT:
  272. style |= wxTB_LEFT;
  273. break;
  274. case TOOLBAR_TOP:
  275. style |= wxTB_TOP;
  276. break;
  277. case TOOLBAR_RIGHT:
  278. style |= wxTB_RIGHT;
  279. break;
  280. case TOOLBAR_BOTTOM:
  281. style |= wxTB_BOTTOM;
  282. break;
  283. }
  284. if ( m_showTooltips )
  285. style &= ~wxTB_NO_TOOLTIPS;
  286. else
  287. style |= wxTB_NO_TOOLTIPS;
  288. if ( style & wxTB_TEXT && !(style & wxTB_NOICONS) && m_horzText )
  289. style |= wxTB_HORZ_LAYOUT;
  290. toolBar = CreateToolBar(style, ID_TOOLBAR);
  291. #endif
  292. PopulateToolbar(toolBar);
  293. }
  294. void MyFrame::PopulateToolbar(wxToolBarBase* toolBar)
  295. {
  296. // Set up toolbar
  297. enum
  298. {
  299. Tool_new,
  300. Tool_open,
  301. Tool_save,
  302. Tool_copy,
  303. Tool_cut,
  304. Tool_paste,
  305. Tool_print,
  306. Tool_help,
  307. Tool_Max
  308. };
  309. wxBitmap toolBarBitmaps[Tool_Max];
  310. #if USE_XPM_BITMAPS
  311. #define INIT_TOOL_BMP(bmp) \
  312. toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
  313. #else // !USE_XPM_BITMAPS
  314. #define INIT_TOOL_BMP(bmp) \
  315. toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
  316. #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
  317. INIT_TOOL_BMP(new);
  318. INIT_TOOL_BMP(open);
  319. INIT_TOOL_BMP(save);
  320. INIT_TOOL_BMP(copy);
  321. INIT_TOOL_BMP(cut);
  322. INIT_TOOL_BMP(paste);
  323. INIT_TOOL_BMP(print);
  324. INIT_TOOL_BMP(help);
  325. int w = toolBarBitmaps[Tool_new].GetWidth(),
  326. h = toolBarBitmaps[Tool_new].GetHeight();
  327. if ( !m_smallToolbar )
  328. {
  329. w *= 2;
  330. h *= 2;
  331. for ( size_t n = Tool_new; n < WXSIZEOF(toolBarBitmaps); n++ )
  332. {
  333. toolBarBitmaps[n] =
  334. wxBitmap(toolBarBitmaps[n].ConvertToImage().Scale(w, h));
  335. }
  336. }
  337. // this call is actually unnecessary as the toolbar will adjust its tools
  338. // size to fit the biggest icon used anyhow but it doesn't hurt neither
  339. toolBar->SetToolBitmapSize(wxSize(w, h));
  340. toolBar->AddTool(wxID_NEW, wxT("New"),
  341. toolBarBitmaps[Tool_new], wxNullBitmap, wxITEM_DROPDOWN,
  342. wxT("New file"), wxT("This is help for new file tool"));
  343. wxMenu* menu = new wxMenu;
  344. menu->Append(wxID_ANY, wxT("&First dummy item"));
  345. menu->Append(wxID_ANY, wxT("&Second dummy item"));
  346. menu->AppendSeparator();
  347. menu->Append(wxID_EXIT, wxT("Exit"));
  348. toolBar->SetDropdownMenu(wxID_NEW, menu);
  349. toolBar->AddTool(wxID_OPEN, wxT("Open"),
  350. toolBarBitmaps[Tool_open], wxNullBitmap, wxITEM_NORMAL,
  351. wxT("Open file"), wxT("This is help for open file tool"));
  352. #if USE_CONTROLS_IN_TOOLBAR
  353. // adding a combo to a vertical toolbar is not very smart
  354. if ( !toolBar->IsVertical() )
  355. {
  356. wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO, wxEmptyString, wxDefaultPosition, wxSize(100,-1) );
  357. combo->Append(wxT("This"));
  358. combo->Append(wxT("is a"));
  359. combo->Append(wxT("combobox"));
  360. combo->Append(wxT("in a"));
  361. combo->Append(wxT("toolbar"));
  362. toolBar->AddControl(combo, wxT("Combo Label"));
  363. }
  364. #endif // USE_CONTROLS_IN_TOOLBAR
  365. toolBar->AddTool(wxID_SAVE, wxT("Save"), toolBarBitmaps[Tool_save], wxT("Toggle button 1"), wxITEM_CHECK);
  366. toolBar->AddSeparator();
  367. toolBar->AddTool(wxID_COPY, wxT("Copy"), toolBarBitmaps[Tool_copy], wxT("Toggle button 2"), wxITEM_CHECK);
  368. toolBar->AddTool(wxID_CUT, wxT("Cut"), toolBarBitmaps[Tool_cut], wxT("Toggle/Untoggle help button"));
  369. toolBar->AddTool(wxID_PASTE, wxT("Paste"), toolBarBitmaps[Tool_paste], wxT("Paste"));
  370. toolBar->AddSeparator();
  371. if ( m_useCustomDisabled )
  372. {
  373. wxBitmap bmpDisabled(w, h);
  374. {
  375. wxMemoryDC dc;
  376. dc.SelectObject(bmpDisabled);
  377. dc.DrawBitmap(toolBarBitmaps[Tool_print], 0, 0);
  378. wxPen pen(*wxRED, 5);
  379. dc.SetPen(pen);
  380. dc.DrawLine(0, 0, w, h);
  381. }
  382. toolBar->AddTool(wxID_PRINT, wxT("Print"), toolBarBitmaps[Tool_print],
  383. bmpDisabled);
  384. }
  385. else
  386. {
  387. toolBar->AddTool(wxID_PRINT, wxT("Print"), toolBarBitmaps[Tool_print],
  388. wxT("Delete this tool. This is a very long tooltip to test whether it does the right thing when the tooltip is more than Windows can cope with."));
  389. }
  390. // add a stretchable space before the "Help" button to make it
  391. // right-aligned
  392. toolBar->AddStretchableSpace();
  393. toolBar->AddTool(wxID_HELP, wxT("Help"), toolBarBitmaps[Tool_help], wxT("Help button"), wxITEM_CHECK);
  394. if ( !m_pathBmp.empty() )
  395. {
  396. // create a tool with a custom bitmap for testing
  397. wxImage img(m_pathBmp);
  398. if ( img.IsOk() )
  399. {
  400. if ( img.GetWidth() > w && img.GetHeight() > h )
  401. img = img.GetSubImage(wxRect(0, 0, w, h));
  402. toolBar->AddSeparator();
  403. toolBar->AddTool(wxID_ANY, wxT("Custom"), img);
  404. }
  405. }
  406. // after adding the buttons to the toolbar, must call Realize() to reflect
  407. // the changes
  408. toolBar->Realize();
  409. toolBar->SetRows(toolBar->IsVertical() ? toolBar->GetToolsCount() / m_rows
  410. : m_rows);
  411. }
  412. // ----------------------------------------------------------------------------
  413. // MyFrame
  414. // ----------------------------------------------------------------------------
  415. // Define my frame constructor
  416. MyFrame::MyFrame(wxFrame* parent,
  417. wxWindowID id,
  418. const wxString& title,
  419. const wxPoint& pos,
  420. const wxSize& size,
  421. long style)
  422. : wxFrame(parent, id, title, pos, size, style)
  423. {
  424. m_tbar = NULL;
  425. m_smallToolbar = true;
  426. m_horzText = false;
  427. m_useCustomDisabled = false;
  428. m_showTooltips = true;
  429. m_searchTool = NULL;
  430. m_rows = 1;
  431. m_nPrint = 1;
  432. #if wxUSE_STATUSBAR
  433. // Give it a status line
  434. CreateStatusBar();
  435. #endif
  436. // Give it an icon
  437. SetIcon(wxICON(sample));
  438. // Make a menubar
  439. wxMenu *tbarMenu = new wxMenu;
  440. tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR,
  441. wxT("Toggle &toolbar\tCtrl-Z"),
  442. wxT("Show or hide the toolbar"));
  443. tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
  444. wxT("Toggle &another toolbar\tCtrl-A"),
  445. wxT("Show/hide another test toolbar"));
  446. tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
  447. wxT("Toggle hori&zontal text\tCtrl-H"),
  448. wxT("Show text under/alongside the icon"));
  449. tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
  450. wxT("&Toggle toolbar size\tCtrl-S"),
  451. wxT("Toggle between big/small toolbar"));
  452. tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS,
  453. wxT("Toggle number of &rows\tCtrl-R"),
  454. wxT("Toggle number of toolbar rows between 1 and 2"));
  455. tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS,
  456. wxT("Show &tooltips\tCtrl-L"),
  457. wxT("Show tooltips for the toolbar tools"));
  458. tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED,
  459. wxT("Use c&ustom disabled images\tCtrl-U"),
  460. wxT("Switch between using system-generated and custom disabled images"));
  461. tbarMenu->AppendSeparator();
  462. tbarMenu->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION,
  463. wxT("Set toolbar at the top of the window"),
  464. wxT("Set toolbar at the top of the window"));
  465. tbarMenu->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION,
  466. wxT("Set toolbar at the left of the window"),
  467. wxT("Set toolbar at the left of the window"));
  468. tbarMenu->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION,
  469. wxT("Set toolbar at the bottom of the window"),
  470. wxT("Set toolbar at the bottom of the window"));
  471. tbarMenu->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION,
  472. wxT("Set toolbar at the right edge of the window"),
  473. wxT("Set toolbar at the right edge of the window"));
  474. tbarMenu->AppendSeparator();
  475. tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT, wxT("Show &text\tCtrl-Alt-T"));
  476. tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS, wxT("Show &icons\tCtrl-Alt-I"));
  477. tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH, wxT("Show &both\tCtrl-Alt-B"));
  478. tbarMenu->AppendSeparator();
  479. tbarMenu->Append(IDM_TOOLBAR_BG_COL, wxT("Choose bac&kground colour..."));
  480. tbarMenu->Append(IDM_TOOLBAR_CUSTOM_PATH, wxT("Custom &bitmap...\tCtrl-B"));
  481. wxMenu *toolMenu = new wxMenu;
  482. toolMenu->Append(IDM_TOOLBAR_ENABLEPRINT, wxT("&Enable print button\tCtrl-E"));
  483. toolMenu->Append(IDM_TOOLBAR_DELETEPRINT, wxT("&Delete print button\tCtrl-D"));
  484. toolMenu->Append(IDM_TOOLBAR_INSERTPRINT, wxT("&Insert print button\tCtrl-I"));
  485. toolMenu->Append(IDM_TOOLBAR_TOGGLEHELP, wxT("Toggle &help button\tCtrl-T"));
  486. toolMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLESEARCH, wxT("Toggle &search field\tCtrl-F"));
  487. toolMenu->AppendSeparator();
  488. toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN1, wxT("Toggle &1st radio button\tCtrl-1"));
  489. toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN2, wxT("Toggle &2nd radio button\tCtrl-2"));
  490. toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN3, wxT("Toggle &3rd radio button\tCtrl-3"));
  491. toolMenu->AppendSeparator();
  492. toolMenu->Append(IDM_TOOLBAR_CHANGE_TOOLTIP, wxT("Change tooltip of \"New\""));
  493. wxMenu *fileMenu = new wxMenu;
  494. fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"), wxT("Quit toolbar sample") );
  495. wxMenu *helpMenu = new wxMenu;
  496. helpMenu->Append(wxID_HELP, wxT("&About"), wxT("About toolbar sample"));
  497. wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
  498. menuBar->Append(fileMenu, wxT("&File"));
  499. menuBar->Append(tbarMenu, wxT("&Toolbar"));
  500. menuBar->Append(toolMenu, wxT("Tool&s"));
  501. menuBar->Append(helpMenu, wxT("&Help"));
  502. // Associate the menu bar with the frame
  503. SetMenuBar(menuBar);
  504. menuBar->Check(IDM_TOOLBAR_TOGGLE_TOOLBAR, true);
  505. menuBar->Check(IDM_TOOLBAR_SHOW_BOTH, true);
  506. menuBar->Check(IDM_TOOLBAR_TOGGLETOOLTIPS, true);
  507. menuBar->Check(IDM_TOOLBAR_TOP_ORIENTATION, true );
  508. m_toolbarPosition = TOOLBAR_TOP;
  509. // Create the toolbar
  510. RecreateToolbar();
  511. m_panel = new wxPanel(this, wxID_ANY);
  512. #if USE_UNMANAGED_TOOLBAR
  513. m_extraToolBar = new wxToolBar(m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_TEXT|wxTB_FLAT|wxTB_TOP);
  514. PopulateToolbar(m_extraToolBar);
  515. #endif
  516. m_textWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
  517. wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
  518. m_panel->SetSizer(sizer);
  519. #if USE_UNMANAGED_TOOLBAR
  520. if (m_extraToolBar)
  521. sizer->Add(m_extraToolBar, 0, wxEXPAND, 0);
  522. #endif
  523. sizer->Add(m_textWindow, 1, wxEXPAND, 0);
  524. }
  525. MyFrame::~MyFrame()
  526. {
  527. if ( m_searchTool && !m_searchTool->GetToolBar() )
  528. {
  529. // we currently can't delete a toolbar tool ourselves, so we have to
  530. // attach it to the toolbar just for it to be deleted, this is pretty
  531. // ugly and will need to be changed
  532. GetToolBar()->AddTool(m_searchTool);
  533. }
  534. }
  535. void MyFrame::LayoutChildren()
  536. {
  537. wxSize size = GetClientSize();
  538. int offset;
  539. if ( m_tbar )
  540. {
  541. m_tbar->SetSize(0, 0, wxDefaultCoord, size.y);
  542. offset = m_tbar->GetSize().x;
  543. }
  544. else
  545. {
  546. offset = 0;
  547. }
  548. m_panel->SetSize(offset, 0, size.x - offset, size.y);
  549. }
  550. void MyFrame::OnSize(wxSizeEvent& event)
  551. {
  552. if ( m_tbar )
  553. {
  554. LayoutChildren();
  555. }
  556. else
  557. {
  558. event.Skip();
  559. }
  560. }
  561. void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event))
  562. {
  563. wxToolBar *tbar = GetToolBar();
  564. if ( !tbar )
  565. {
  566. RecreateToolbar();
  567. }
  568. else
  569. {
  570. // notice that there is no need to call SetToolBar(NULL) here (although
  571. // this it is harmless to do and it must be called if you do not delete
  572. // the toolbar but keep it for later reuse), just delete the toolbar
  573. // directly and it will reset the associated frame toolbar pointer
  574. delete tbar;
  575. }
  576. }
  577. void MyFrame::OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event))
  578. {
  579. m_horzText = !m_horzText;
  580. RecreateToolbar();
  581. }
  582. void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event))
  583. {
  584. if ( m_tbar )
  585. {
  586. wxDELETE(m_tbar);
  587. }
  588. else
  589. {
  590. long style = GetToolBar() ? GetToolBar()->GetWindowStyle()
  591. : TOOLBAR_STYLE;
  592. style &= ~wxTB_HORIZONTAL;
  593. style |= wxTB_VERTICAL;
  594. m_tbar = new wxToolBar(this, wxID_ANY,
  595. wxDefaultPosition, wxDefaultSize,
  596. style);
  597. m_tbar->SetMargins(4, 4);
  598. m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_1, wxT("First"), wxBITMAP(new));
  599. m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_2, wxT("Second"), wxBITMAP(open));
  600. m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_3, wxT("Third"), wxBITMAP(save));
  601. m_tbar->AddSeparator();
  602. m_tbar->AddTool(wxID_HELP, wxT("Help"), wxBITMAP(help));
  603. m_tbar->AddTool(IDM_TOOLBAR_OTHER_4, wxT("Disabled"), wxBITMAP(cut), wxBITMAP(paste));
  604. m_tbar->EnableTool(IDM_TOOLBAR_OTHER_4, false);
  605. m_tbar->Realize();
  606. }
  607. LayoutChildren();
  608. }
  609. void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
  610. {
  611. m_smallToolbar = !m_smallToolbar;
  612. RecreateToolbar();
  613. }
  614. void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
  615. {
  616. // m_rows may be only 1 or 2
  617. m_rows = 3 - m_rows;
  618. wxToolBar* const toolBar = GetToolBar();
  619. toolBar->SetRows(toolBar->IsVertical() ? toolBar->GetToolsCount() / m_rows
  620. : m_rows);
  621. //RecreateToolbar(); -- this is unneeded
  622. }
  623. void MyFrame::OnToggleTooltips(wxCommandEvent& WXUNUSED(event))
  624. {
  625. m_showTooltips = !m_showTooltips;
  626. RecreateToolbar();
  627. }
  628. void MyFrame::OnToggleCustomDisabled(wxCommandEvent& WXUNUSED(event))
  629. {
  630. m_useCustomDisabled = !m_useCustomDisabled;
  631. RecreateToolbar();
  632. }
  633. void MyFrame::OnChangeOrientation(wxCommandEvent& event)
  634. {
  635. switch( event.GetId() )
  636. {
  637. case IDM_TOOLBAR_LEFT_ORIENTATION:
  638. m_toolbarPosition = TOOLBAR_LEFT;
  639. break;
  640. case IDM_TOOLBAR_TOP_ORIENTATION:
  641. m_toolbarPosition = TOOLBAR_TOP;
  642. break;
  643. case IDM_TOOLBAR_RIGHT_ORIENTATION:
  644. m_toolbarPosition = TOOLBAR_RIGHT;
  645. break;
  646. case IDM_TOOLBAR_BOTTOM_ORIENTATION:
  647. m_toolbarPosition = TOOLBAR_BOTTOM;
  648. break;
  649. }
  650. RecreateToolbar();
  651. }
  652. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  653. {
  654. Close(true);
  655. }
  656. void MyFrame::OnAbout(wxCommandEvent& event)
  657. {
  658. if ( event.IsChecked() )
  659. m_textWindow->WriteText( wxT("Help button down now.\n") );
  660. else
  661. m_textWindow->WriteText( wxT("Help button up now.\n") );
  662. (void)wxMessageBox(wxT("wxWidgets toolbar sample"), wxT("About wxToolBar"));
  663. }
  664. void MyFrame::OnToolLeftClick(wxCommandEvent& event)
  665. {
  666. wxString str;
  667. str.Printf( wxT("Clicked on tool %d\n"), event.GetId());
  668. m_textWindow->WriteText( str );
  669. if (event.GetId() == wxID_COPY)
  670. {
  671. DoEnablePrint();
  672. }
  673. if (event.GetId() == wxID_CUT)
  674. {
  675. DoToggleHelp();
  676. }
  677. if (event.GetId() == wxID_PRINT)
  678. {
  679. DoDeletePrint();
  680. }
  681. }
  682. void MyFrame::OnToolRightClick(wxCommandEvent& event)
  683. {
  684. m_textWindow->AppendText(
  685. wxString::Format(wxT("Tool %d right clicked.\n"),
  686. (int) event.GetInt()));
  687. }
  688. void MyFrame::OnCombo(wxCommandEvent& event)
  689. {
  690. wxLogStatus(wxT("Combobox string '%s' selected"), event.GetString().c_str());
  691. }
  692. void MyFrame::DoEnablePrint()
  693. {
  694. if ( !m_nPrint )
  695. return;
  696. wxToolBarBase *tb = GetToolBar();
  697. tb->EnableTool(wxID_PRINT, !tb->GetToolEnabled(wxID_PRINT));
  698. }
  699. void MyFrame::DoDeletePrint()
  700. {
  701. if ( !m_nPrint )
  702. return;
  703. wxToolBarBase *tb = GetToolBar();
  704. tb->DeleteTool( wxID_PRINT );
  705. m_nPrint--;
  706. }
  707. void MyFrame::DoToggleHelp()
  708. {
  709. wxToolBarBase *tb = GetToolBar();
  710. tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
  711. }
  712. void MyFrame::OnToggleSearch(wxCommandEvent& WXUNUSED(event))
  713. {
  714. wxToolBarBase * const tb = GetToolBar();
  715. if ( !m_searchTool )
  716. {
  717. wxSearchCtrl * const srch = new wxSearchCtrl(tb, wxID_ANY, "needle");
  718. srch->SetMinSize(wxSize(80, -1));
  719. m_searchTool = tb->AddControl(srch);
  720. }
  721. else // tool already exists
  722. {
  723. wxControl * const win = m_searchTool->GetControl();
  724. if ( m_searchTool->GetToolBar() )
  725. {
  726. // attached now, remove it
  727. win->Hide();
  728. tb->RemoveTool(m_searchTool->GetId());
  729. }
  730. else // tool exists in detached state, attach it back
  731. {
  732. tb->AddTool(m_searchTool);
  733. win->Show();
  734. }
  735. }
  736. tb->Realize();
  737. }
  738. void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
  739. {
  740. event.Enable( m_textWindow->CanCopy() );
  741. }
  742. void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent& event)
  743. {
  744. wxToolBar *tbar = GetToolBar();
  745. event.Enable( tbar &&
  746. tbar->HasFlag(wxTB_TEXT) &&
  747. !tbar->HasFlag(wxTB_NOICONS) );
  748. }
  749. void MyFrame::OnChangeToolTip(wxCommandEvent& WXUNUSED(event))
  750. {
  751. GetToolBar()->SetToolShortHelp(wxID_NEW, wxT("New toolbar button"));
  752. }
  753. void MyFrame::OnToolbarStyle(wxCommandEvent& event)
  754. {
  755. long style = GetToolBar()->GetWindowStyle();
  756. style &= ~(wxTB_NOICONS | wxTB_HORZ_TEXT);
  757. switch ( event.GetId() )
  758. {
  759. case IDM_TOOLBAR_SHOW_TEXT:
  760. style |= wxTB_NOICONS | (m_horzText ? wxTB_HORZ_TEXT : wxTB_TEXT);
  761. break;
  762. case IDM_TOOLBAR_SHOW_ICONS:
  763. // nothing to do
  764. break;
  765. case IDM_TOOLBAR_SHOW_BOTH:
  766. style |= (m_horzText ? wxTB_HORZ_TEXT : wxTB_TEXT);
  767. }
  768. GetToolBar()->SetWindowStyle(style);
  769. }
  770. void MyFrame::OnToolbarBgCol(wxCommandEvent& WXUNUSED(event))
  771. {
  772. wxColour col = wxGetColourFromUser
  773. (
  774. this,
  775. GetToolBar()->GetBackgroundColour(),
  776. "Toolbar background colour"
  777. );
  778. if ( col.IsOk() )
  779. {
  780. GetToolBar()->SetBackgroundColour(col);
  781. GetToolBar()->Refresh();
  782. }
  783. }
  784. void MyFrame::OnToolbarCustomBitmap(wxCommandEvent& WXUNUSED(event))
  785. {
  786. m_pathBmp = wxLoadFileSelector("custom bitmap", "");
  787. RecreateToolbar();
  788. }
  789. void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
  790. {
  791. m_nPrint++;
  792. wxToolBarBase *tb = GetToolBar();
  793. tb->InsertTool(0, wxID_PRINT, wxT("New print"),
  794. wxBITMAP(print), wxNullBitmap,
  795. wxITEM_NORMAL,
  796. wxT("Delete this tool"),
  797. wxT("This button was inserted into the toolbar"));
  798. // must call Realize() after adding a new button
  799. tb->Realize();
  800. }
  801. void MyFrame::OnToggleRadioBtn(wxCommandEvent& event)
  802. {
  803. if ( m_tbar )
  804. {
  805. m_tbar->ToggleTool(IDM_TOOLBAR_OTHER_1 +
  806. event.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1, true);
  807. }
  808. }
  809. void MyFrame::OnToolDropdown(wxCommandEvent& event)
  810. {
  811. wxString str;
  812. str.Printf( wxT("Dropdown on tool %d\n"), event.GetId());
  813. m_textWindow->WriteText( str );
  814. event.Skip();
  815. }