Advertisement
Fernando_Fiore

wxAuiMDIParentFrame with splitter and toolbar

Aug 6th, 2021
1,155
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name:        minimal.cpp
  3. // Purpose:     Minimal wxWidgets sample
  4. // Author:      Julian Smart
  5. // Modified by: fioresoft.net
  6. // Created:     04/01/98
  7. // Copyright:   (c) Julian Smart
  8. // Licence:     wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. // ============================================================================
  12. // declarations
  13. // ============================================================================
  14.  
  15. // ----------------------------------------------------------------------------
  16. // headers
  17. // ----------------------------------------------------------------------------
  18.  
  19. // For compilers that support precompilation, includes "wx/wx.h".
  20. #include "wx/wxprec.h"
  21.  
  22. #ifdef __BORLANDC__
  23.     #pragma hdrstop
  24. #endif
  25.  
  26. // for all others, include the necessary headers (this file is usually all you
  27. // need because it includes almost all "standard" wxWidgets headers)
  28. #ifndef WX_PRECOMP
  29.     #include "wx/wx.h"
  30. #endif
  31. #include "wx/aui/tabmdi.h"
  32. #include "wx/splitter.h"
  33. #include "bitmaps/new.xpm"
  34. #include "bitmaps/open.xpm"
  35. #include "bitmaps/save.xpm"
  36. #include "bitmaps/bold.xpm"
  37. #include "bitmaps/italic.xpm"
  38. #include "bitmaps/underline.xpm"
  39.  
  40. #ifdef wxUSE_TOOLBARS
  41. #undef wxUSE_TOOLBARS
  42. #endif
  43.  
  44. // ----------------------------------------------------------------------------
  45. // resources
  46. // ----------------------------------------------------------------------------
  47.  
  48. // the application icon (under Windows it is in resources and even
  49. // though we could still include the XPM here it would be unused)
  50. #ifndef wxHAS_IMAGES_IN_RESOURCES
  51.     #include "../sample.xpm"
  52. #endif
  53.  
  54. // ----------------------------------------------------------------------------
  55. // private classes
  56. // ----------------------------------------------------------------------------
  57.  
  58. // Define a new application type, each program should derive a class from wxApp
  59. class MyApp : public wxApp
  60. {
  61. public:
  62.     // override base class virtuals
  63.     // ----------------------------
  64.  
  65.     // this one is called on application startup and is a good place for the app
  66.     // initialization (doing it here and not in the ctor allows to have an error
  67.     // return: if OnInit() returns false, the application terminates)
  68.     virtual bool OnInit() wxOVERRIDE;
  69. };
  70.  
  71. class MyChild : public wxAuiMDIChildFrame
  72. {
  73. public:
  74.     MyChild(wxAuiMDIParentFrame *parent,const wxString& title) :wxAuiMDIChildFrame(parent, wxID_ANY, title,wxDefaultPosition,
  75.         wxDefaultSize,wxDEFAULT_FRAME_STYLE)
  76.     {
  77.         wxTextCtrl* text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
  78.     }
  79. };
  80.  
  81. // Define a new frame type: this is going to be our main frame
  82. class MyFrame : public wxAuiMDIParentFrame
  83. {
  84.     wxSplitterWindow* hsplitter;
  85. public:
  86.     // ctor(s)
  87.     MyFrame(const wxString& title);
  88.  
  89.     // event handlers (these functions should _not_ be virtual)
  90.     void OnQuit(wxCommandEvent& event);
  91.     void OnAbout(wxCommandEvent& event);
  92.     void OnNew(wxCommandEvent& event);
  93.     void OnCascade(wxCommandEvent& event);
  94.     void OnTile(wxCommandEvent& event);
  95.     void OnClose(wxCommandEvent& event);
  96.     void OnCloseAll(wxCommandEvent& event);
  97.     void OnFrameClose(wxCloseEvent& event);
  98.     void OnSize(wxSizeEvent& event);
  99.  
  100. private:
  101.     // any class wishing to process wxWidgets events must use this macro
  102.     wxDECLARE_EVENT_TABLE();
  103. };
  104.  
  105. // ----------------------------------------------------------------------------
  106. // constants
  107. // ----------------------------------------------------------------------------
  108.  
  109. // IDs for the controls and the menu commands
  110. enum
  111. {
  112.     // menu items
  113.     Minimal_Quit = wxID_EXIT,
  114.  
  115.     // it is important for the id corresponding to the "About" command to have
  116.     // this standard value as otherwise it won't be handled properly under Mac
  117.     // (where it is special and put into the "Apple" menu)
  118.     Minimal_About = wxID_ABOUT
  119. };
  120.  
  121. // ----------------------------------------------------------------------------
  122. // event tables and other macros for wxWidgets
  123. // ----------------------------------------------------------------------------
  124.  
  125. // the event tables connect the wxWidgets events with the functions (event
  126. // handlers) which process them. It can be also done at run-time, but for the
  127. // simple menu events like this the static method is much simpler.
  128. wxBEGIN_EVENT_TABLE(MyFrame, wxAuiMDIParentFrame)
  129.     EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
  130.     EVT_MENU(Minimal_About, MyFrame::OnAbout)
  131.     EVT_MENU(wxID_NEW,MyFrame::OnNew)
  132.     EVT_MENU(wxID_MDI_WINDOW_CASCADE,MyFrame::OnCascade)
  133.     EVT_MENU(wxID_MDI_WINDOW_TILE_HORZ,MyFrame::OnTile)
  134.     EVT_MENU(wxID_CLOSE,MyFrame::OnClose)
  135.     EVT_MENU(wxID_CLOSE_ALL,MyFrame::OnCloseAll)
  136.     EVT_CLOSE(MyFrame::OnFrameClose)
  137.     EVT_SIZE(MyFrame::OnSize)
  138. wxEND_EVENT_TABLE()
  139.  
  140. // Create a new application object: this macro will allow wxWidgets to create
  141. // the application object during program execution (it's better than using a
  142. // static object for many reasons) and also implements the accessor function
  143. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  144. // not wxApp)
  145. wxIMPLEMENT_APP(MyApp);
  146.  
  147. // ============================================================================
  148. // implementation
  149. // ============================================================================
  150.  
  151. // ----------------------------------------------------------------------------
  152. // the application class
  153. // ----------------------------------------------------------------------------
  154.  
  155. // 'Main program' equivalent: the program execution "starts" here
  156. bool MyApp::OnInit()
  157. {
  158.     // call the base class initialization method, currently it only parses a
  159.     // few common command-line options but it could be do more in the future
  160.     if ( !wxApp::OnInit() )
  161.         return false;
  162.  
  163.     // create the main application window
  164.     MyFrame *frame = new MyFrame("Minimal wxWidgets App");
  165.  
  166.     // and show it (the frames, unlike simple controls, are not shown when
  167.     // created initially)
  168.     frame->Show(true);
  169.  
  170.     // success: wxApp::OnRun() will be called which will enter the main message
  171.     // loop and the application will run. If we returned false here, the
  172.     // application would exit immediately.
  173.     return true;
  174. }
  175.  
  176. // ----------------------------------------------------------------------------
  177. // main frame
  178. // ----------------------------------------------------------------------------
  179.  
  180. // frame constructor
  181. MyFrame::MyFrame(const wxString& title)
  182.     : wxAuiMDIParentFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE),
  183.     hsplitter(nullptr)//,panel(nullptr),edit(nullptr)
  184. {
  185.     // set the frame icon
  186.     SetIcon(wxICON(sample));
  187.  
  188. #if wxUSE_MENUBAR
  189.     // create a menu bar
  190.     wxMenu* fileMenu = new wxMenu;
  191.  
  192.     // the "About" item should be in the help menu
  193.     wxMenu* helpMenu = new wxMenu;
  194.     helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");
  195.  
  196.     fileMenu->Append(wxID_NEW, wxT("&New"));
  197.     fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
  198.  
  199.     // now append the freshly created menu to the menu bar...
  200.     wxMenuBar* menuBar = new wxMenuBar();
  201.     menuBar->Append(fileMenu, "&File");
  202.     menuBar->Append(helpMenu, "&Help");
  203.  
  204.     wxMenu* menu = GetWindowMenu();
  205.     menu->Append(wxID_MDI_WINDOW_TILE_HORZ, wxT("&Tile"));
  206.     menu->Append(wxID_MDI_WINDOW_CASCADE, wxT("&Cascade"));
  207.  
  208.     // ... and attach this menu bar to the frame
  209.     SetMenuBar(menuBar);
  210. #else // !wxUSE_MENUBAR
  211.     // If menus are not available add a button to access the about box
  212.     wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
  213.     wxButton* aboutBtn = new wxButton(this, wxID_ANY, "About...");
  214.     aboutBtn->Bind(wxEVT_BUTTON, &MyFrame::OnAbout, this);
  215.     sizer->Add(aboutBtn, wxSizerFlags().Center());
  216.     SetSizer(sizer);
  217. #endif // wxUSE_MENUBAR/!wxUSE_MENUBAR
  218.  
  219. #if wxUSE_STATUSBAR
  220.     // create a status bar just for fun (by default with 1 pane only)
  221.     CreateStatusBar(2);
  222.     SetStatusText("Welcome to wxWidgets!");
  223. #endif // wxUSE_STATUSBAR
  224. #if 0//wxUSE_TOOLBAR
  225.     wxToolBar* tb = CreateToolBar();
  226.     wxBitmap bmpNew(new_xpm);
  227.     wxBitmap bmpOpen(open_xpm);
  228.     wxBitmap bmpSave(save_xpm);
  229.     wxBitmap bmpBold(bold_xpm);
  230.     wxBitmap bmpItalic(italic_xpm);
  231.     wxBitmap bmpUnderline(underline_xpm);
  232.  
  233.     tb->AddTool(wxID_NEW, wxT("new"), bmpNew, wxT("new file"));
  234.     tb->AddTool(wxID_OPEN, wxT("open"), bmpOpen, wxT("open file"));
  235.     tb->AddTool(wxID_SAVE, wxT("save"), bmpSave, wxT("save file"));
  236.     tb->AddSeparator();
  237.     tb->AddTool(wxID_BOLD, wxT("bold"), bmpBold, wxT("bold"));
  238.     tb->AddTool(wxID_ITALIC, wxT("italic"), bmpItalic, wxT("italic"));
  239.     tb->AddTool(wxID_UNDERLINE, wxT("underline"), bmpUnderline, wxT("underline"));
  240.  
  241.     SetToolBar(tb);
  242.     tb->Realize();
  243. #endif
  244.     const wxString choices[] = { "hello","damn","world" };
  245.  
  246.     hsplitter = new wxSplitterWindow(\
  247.         this,
  248.         -1,
  249.         wxDefaultPosition,
  250.         wxDefaultSize,
  251.         wxSP_LIVE_UPDATE | wxSP_3D | wxSP_NO_XP_THEME);
  252.  
  253.     wxAuiMDIClientWindow* client = GetClientWindow();
  254.     client->SetParent(hsplitter);
  255.     hsplitter->SplitHorizontally(new wxListBox(hsplitter, wxID_ANY, wxDefaultPosition, wxDefaultSize,3,choices),
  256.         client);
  257.     hsplitter->SetSashGravity(0.5);
  258. }
  259.  
  260.  
  261. // event handlers
  262.  
  263. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  264. {
  265.     // true is to force the frame to close
  266.     Close(true);
  267. }
  268.  
  269. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  270. {
  271.     wxMessageBox(wxString::Format
  272.                  (
  273.                     "Welcome to %s!\n"
  274.                     "\n"
  275.                     "This is the minimal wxWidgets sample\n"
  276.                     "running under %s.",
  277.                     wxVERSION_STRING,
  278.                     wxGetOsDescription()
  279.                  ),
  280.                  "About wxWidgets minimal sample",
  281.                  wxOK | wxICON_INFORMATION,
  282.                  this);
  283. }
  284.  
  285. void MyFrame::OnNew(wxCommandEvent& event)
  286. {
  287.     static int i = 0;
  288.  
  289.     MyChild* p = new MyChild(this, wxString::Format(wxT("child %d"), ++i));
  290.     p->Show(true);
  291. }
  292.  
  293. void MyFrame::OnCascade(wxCommandEvent& event)
  294. {
  295.     this->Cascade();
  296. }
  297.  
  298. void MyFrame::OnTile(wxCommandEvent& event)
  299. {
  300.     this->Tile();
  301. }
  302.  
  303. void MyFrame::OnClose(wxCommandEvent& event)
  304. {
  305.     wxAuiMDIChildFrame* child = GetActiveChild();
  306.     bool res = child->Close();
  307.     wxASSERT(res);
  308. }
  309.  
  310. void MyFrame::OnCloseAll(wxCommandEvent& event)
  311. {
  312.     wxWindowList &list = this->GetChildren();
  313.     for (wxWindowList::iterator it = list.begin(); it != list.end(); it++) {
  314.         (*it)->Close();
  315.     }
  316. }
  317.  
  318. void MyFrame::OnFrameClose(wxCloseEvent& event)
  319. {
  320.     wxWindowList& list = this->GetChildren();
  321.     for (wxWindowList::iterator it = list.begin(); it != list.end(); it++) {
  322.         (*it)->Close();
  323.     }
  324.     GetClientWindow()->SetParent(this);
  325.     event.Skip();
  326. }
  327.  
  328. void MyFrame::OnSize(wxSizeEvent& event)
  329. {
  330.     if (hsplitter) {
  331.         wxRect rc = GetClientRect();
  332.         hsplitter->SetSize(rc);
  333.     }
  334.     event.Skip();
  335. }
Advertisement
RAW Paste Data Copied
Advertisement