toolbar.cpp 30 KB

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