Редактирование: WxWidgets FAQ Menus and toolbars

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
Внимание: Вы не представились системе. Ваш IP-адрес будет записан в историю изменений этой страницы.
Правка может быть отменена. Пожалуйста, просмотрите сравнение версий, чтобы убедиться, что это именно те изменения, которые вас интересуют, и нажмите «Записать страницу», чтобы изменения вступили в силу.
Текущая версия Ваш текст
Строка 1: Строка 1:
 +
== Menubar ==
 +
A menubar is one of the most visible parts of the GUI application. It is a group of commands located in various menus. While in console applications you had to remember all those arcane commands, here we have most of the commands grouped into logical parts. There are accepted standards that further reduce the amount of time spending to learn a new application. To implement a menubar in wxWidgets we need to have three things. A wxMenuBar, a wxMenu and a wxMenuItem.
 +
[[image: wxwidgets_faq_menu.jpg | center]]
 +
 +
== Simple menu example ==
 +
Creating a menubar in wxWidgets is very simple. Just a few lines of code.
 +
<source lang="cpp">
 +
#include &lt;wx/wx.h&gt;
 +
 +
#include &lt;wx/menu.h&gt;
 +
 +
class SimpleMenu : public wxFrame
 +
{
 +
public:
 +
    SimpleMenu(const wxString& title);
 +
 +
    void OnQuit(wxCommandEvent& event);
 +
 +
    wxMenuBar *menubar;
 +
    wxMenu *file;
 +
 +
};
 +
</source>
 +
 +
<source lang="cpp">
 +
#include "menu.h"
 +
 +
 +
SimpleMenu::SimpleMenu(const wxString&amp; title)
 +
      : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
 +
{
 +
 +
  menubar = new wxMenuBar;
 +
  file = new wxMenu;
 +
  file->Append(wxID_EXIT, wxT("&Quit"));
 +
  menubar->Append(file, wxT("&File"));
 +
  SetMenuBar(menubar);
 +
 +
  Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
 +
      wxCommandEventHandler(SimpleMenu::OnQuit));
 +
  Centre();
 +
 +
}
 +
 +
void SimpleMenu::OnQuit(wxCommandEvent&amp; WXUNUSED(event))
 +
{
 +
  Close(true);
 +
}
 +
 +
</source>
 +
 +
<source lang="cpp">
 +
#include &lt;wx/wx.h&gt;
 +
 +
class MyApp : public wxApp
 +
{
 +
  public:
 +
    virtual bool OnInit();
 +
};
 +
</source>
 +
 +
<source lang="cpp">
 +
#include "main.h"
 +
#include "menu.h"
 +
 +
IMPLEMENT_APP(MyApp)
 +
 +
bool MyApp::OnInit()
 +
{
 +
 +
    SimpleMenu *menu = new SimpleMenu(wxT("Simple Menu"));
 +
    menu->Show(true);
 +
 +
    return true;
 +
}
 +
</source>
 +
 +
<source lang="cpp">
 +
menubar = new wxMenuBar;
 +
</source>
 +
 +
First we create a menubar object.
 +
 +
<source lang="cpp">
 +
file = new wxMenu;
 +
</source>
 +
 +
Next we create a menu object.
 +
 +
<source lang="cpp">
 +
file->Append(wxID_EXIT, wxT("&Quit"));
 +
</source>
 +
 +
We append a menu item into the menu object. The first parameter is the id of the menu item. The second parameter is the name of the menu item. Here we did not create a wxMenuItem explicitely. It was created by the Append() method behind the scenes. Later on, we will create a wxMenuItem manually.
 +
 +
<source lang="cpp">
 +
  menubar->Append(file, wxT("&File"));
 +
  SetMenuBar(menubar);
 +
</source>
 +
 +
After that, we append a menu into the menubar. The &amp; character creates an accelerator key. The character that follows the &amp; is underlined. This way the menu is accessible via the alt + F shortcut. In the end, we call the SetMenuBar() method. This method belongs to the wxFrame widget. It sets up the menubar.
 +
 +
[[image: wxwidgets_faq_simplemenu.png | center]]
 +
 +
=== Submenus ===
 +
Each menu can also have a submenu. This way we can group similar commnads into groups. For example we can place commands that hide/show various toolbars like personal bar, address bar, status bar or navigation bar into a submenu called toolbars. Within a menu, we can seperate commands with a separator. It is a simple line. It is common practice to separate commands like new, open, save from commands like print, print preview with a single separator. In our example we will see, how we can create submenus and menu separators.
 +
<source lang="cpp">
 +
#include &lt;wx/wx.h&gt;
 +
 +
#include &lt;wx/menu.h&gt;
 +
 +
class SubMenu : public wxFrame
 +
{
 +
public:
 +
  SubMenu(const wxString& title);
 +
 +
  void OnQuit(wxCommandEvent & event);
 +
 +
  wxMenuBar *menubar;
 +
  wxMenu *file;
 +
  wxMenu *imp;
 +
  wxMenuItem *quit;
 +
 +
};
 +
</source>
 +
 +
<source lang="cpp">
 +
#include "menu.h"
 +
 +
 +
SubMenu::SubMenu(const wxString& title)
 +
      : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
 +
{
 +
 +
  menubar = new wxMenuBar;
 +
  file = new wxMenu;
 +
 +
  file->Append(wxID_ANY, wxT("&New"));
 +
  file->Append(wxID_ANY, wxT("&Open"));
 +
  file->Append(wxID_ANY, wxT("&Save"));
 +
  file->AppendSeparator();
 +
 +
  imp = new wxMenu;
 +
  imp->Append(wxID_ANY, wxT("Import newsfeed list..."));
 +
  imp->Append(wxID_ANY, wxT("Import bookmarks..."));
 +
  imp->Append(wxID_ANY, wxT("Import mail..."));
 +
 +
  file->AppendSubMenu(imp, wxT("I&mport"));
 +
 +
  quit = new wxMenuItem(file, wxID_EXIT, wxT("&Quit\tCtrl+W"));
 +
  file->Append(quit);
 +
 +
  menubar->Append(file, wxT("&File"));
 +
  SetMenuBar(menubar);
 +
 +
  Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
 +
      wxCommandEventHandler(SubMenu::OnQuit));
 +
  Centre();
 +
 +
}
 +
 +
void SubMenu::OnQuit(wxCommandEvent& WXUNUSED(event))
 +
{
 +
  Close(true);
 +
}
 +
 +
</source>
 +
 +
<source lang="cpp">
 +
#include &lt;wx/wx.h&gt;
 +
 +
class MyApp : public wxApp
 +
{
 +
  public:
 +
    virtual bool OnInit();
 +
};
 +
</source>
 +
 +
<source lang="cpp">
 +
#include "main.h"
 +
#include "menu.h"
 +
 +
IMPLEMENT_APP(MyApp)
 +
 +
bool MyApp::OnInit()
 +
{
 +
 +
    SubMenu *smenu = new SubMenu(wxT("Submenu"));
 +
    smenu->Show(true);
 +
 +
    return true;
 +
}
 +
 +
</source>
 +
 +
We created one submenu in a file menu. It is an import submenu, which can be seen in Opera web browser.
 +
 +
<source lang="cpp">
 +
file->AppendSeparator();
 +
</source>
 +
 +
A menu separator line is created calling an <i>AppendSeparator()</i> method.
 +
 +
<source lang="cpp">
 +
imp = new wxMenu;
 +
imp->Append(wxID_ANY, wxT("Import newsfeed list..."));
 +
imp->Append(wxID_ANY, wxT("Import bookmarks..."));
 +
imp->Append(wxID_ANY, wxT("Import mail..."));
 +
 +
file->AppendSubMenu(imp, wxT("I&mport"));
 +
</source>
 +
 +
A submenu is created like a normal menu. It is appended with a <i>AppendSubMenu()</i> method.
 +
 +
[[image: wxwidgets_faq_submenu.png | center]]
 +
 +
== Toolbars ==
 +
Menus group all commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands.
 +
<source lang="cpp">
 +
virtual wxToolBar* CreateToolBar(long style = wxNO_BORDER | wxTB_HORIZONTAL,
 +
              wxWindowID id = -1, const wxString& name = "toolBar")
 +
</source>
 +
 +
To create a toolbar, we call the CreateToolBar() method of the frame widget.
 +
 +
=== A simple toolbar ===
 +
Our first example will create a simple toolbar.
 +
<source lang="cpp">
 +
 +
#include &lt;wx/wx.h&gt;
 +
 +
class Toolbar : public wxFrame
 +
{
 +
public:
 +
    Toolbar(const wxString&amp; title);
 +
 +
    void OnQuit(wxCommandEvent&amp; event);
 +
 +
 +
};
 +
</source>
 +
 +
<source lang="cpp">
 +
#include "toolbar.h"
 +
 +
 +
Toolbar::Toolbar(const wxString&amp; title)
 +
      : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
 +
{
 +
 +
  wxBitmap exit(wxT("exit.png"));
 +
 +
  wxToolBar *toolbar = CreateToolBar();
 +
  toolbar->AddTool(wxID_EXIT, exit, wxT("Exit application"));
 +
  toolbar->Realize();
 +
 +
  Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED,
 +
      wxCommandEventHandler(Toolbar::OnQuit));
 +
 +
  Centre();
 +
 +
}
 +
 +
void Toolbar::OnQuit(wxCommandEvent&amp; WXUNUSED(event))
 +
{
 +
  Close(true);
 +
}
 +
 +
</source>
 +
 +
<source lang="cpp">
 +
#include &lt;wx/wx.h&gt;
 +
 +
class MyApp : public wxApp
 +
{
 +
  public:
 +
    virtual bool OnInit();
 +
};
 +
</source>
 +
 +
<source lang="cpp">
 +
#include "main.h"
 +
#include "toolbar.h"
 +
 +
IMPLEMENT_APP(MyApp)
 +
 +
bool MyApp::OnInit()
 +
{
 +
 +
    Toolbar *toolbar = new Toolbar(wxT("Toolbar"));
 +
    toolbar->Show(true);
 +
 +
    return true;
 +
}
 +
 +
</source>
 +
 +
In our example, we create a toolbar and one tool button. Clicking on the toolbar button will terminate the application.
 +
 +
<source lang="cpp">
 +
wxToolBar *toolbar = CreateToolBar();
 +
</source>
 +
 +
We create a toolbar.
 +
 +
<source lang="cpp">
 +
 +
toolbar->AddTool(wxID_EXIT, exit, wxT("Exit application"));
 +
</source>
 +
 +
We add a tool to the toolbar.
 +
 +
<source lang="cpp">
 +
toolbar->Realize();
 +
</source>
 +
 +
After we have added the tools, we call the <i>Realize()</i> method.
 +
 +
[[image: wxwidgets_faq_toolbar.png | center]]
 +
 +
=== Toolbars ===
 +
If we want to have more than one toolbar, we must create them in a different way. e.g. other than calling  the <i>CreateToolbar()</i> method.
 +
 +
<source lang="cpp">
 +
#include &lt;wx/wx.h&gt;
 +
 +
class Toolbar : public wxFrame
 +
{
 +
public:
 +
  Toolbar(const wxString&amp; title);
 +
 +
  void OnQuit(wxCommandEvent&amp; event);
 +
 +
  wxToolBar *toolbar1;
 +
  wxToolBar *toolbar2;
 +
 +
};
 +
</source>
 +
 +
<source lang="cpp">
 +
 +
#include "toolbars.h"
 +
 +
 +
Toolbar::Toolbar(const wxString&amp; title)
 +
      : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
 +
{
 +
 +
  wxImage::AddHandler( new wxPNGHandler );
 +
 +
  wxBitmap exit(wxT("exit.png"), wxBITMAP_TYPE_PNG);
 +
  wxBitmap newb(wxT("new.png"), wxBITMAP_TYPE_PNG);
 +
  wxBitmap open(wxT("open.png"), wxBITMAP_TYPE_PNG);
 +
  wxBitmap save(wxT("save.png"), wxBITMAP_TYPE_PNG);
 +
 +
  wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
 +
 +
  toolbar1 = new wxToolBar(this, wxID_ANY);
 +
  toolbar1->AddTool(wxID_ANY, newb, wxT(""));
 +
  toolbar1->AddTool(wxID_ANY, open, wxT(""));
 +
  toolbar1->AddTool(wxID_ANY, save, wxT(""));
 +
  toolbar1->Realize();
 +
 +
  toolbar2 = new wxToolBar(this, wxID_ANY);
 +
  toolbar2->AddTool(wxID_EXIT, exit, wxT("Exit application"));
 +
  toolbar2->Realize();
 +
 +
  vbox->Add(toolbar1, 0, wxEXPAND);
 +
  vbox->Add(toolbar2, 0, wxEXPAND);
 +
 +
  SetSizer(vbox);
 +
 +
  Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED,
 +
      wxCommandEventHandler(Toolbar::OnQuit));
 +
 +
  Centre();
 +
}
 +
 +
void Toolbar::OnQuit(wxCommandEvent&amp; WXUNUSED(event))
 +
{
 +
  Close(true);
 +
}
 +
</source>
 +
 +
<source lang="cpp">
 +
#include &lt;wx/wx.h&gt;
 +
 +
class MyApp : public wxApp
 +
{
 +
  public:
 +
    virtual bool OnInit();
 +
};
 +
 +
</source>
 +
 +
<source lang="cpp">
 +
#include "main.h"
 +
#include "toolbars.h"
 +
 +
IMPLEMENT_APP(MyApp)
 +
 +
bool MyApp::OnInit()
 +
{
 +
    Toolbar *toolbar = new Toolbar(wxT("Toolbar"));
 +
    toolbar->Show(true);
 +
 +
    return true;
 +
}
 +
</source>
 +
 +
In our example, we create two horizontal toolbars. We place them in a vertical box sizer.
 +
 +
<source lang="cpp">
 +
toolbar1 = new wxToolBar(this, wxID_ANY);
 +
...
 +
toolbar2 = new wxToolBar(this, wxID_ANY);
 +
 +
</source>
 +
 +
Here we create two toolbars.
 +
 +
<source lang="cpp">
 +
  vbox->Add(toolbar1, 0, wxEXPAND);
 +
  vbox->Add(toolbar2, 0, wxEXPAND);
 +
</source>
 +
 +
And here we add them to the vertical box sizer.
 +
 +
[[image: wxwidgets_faq_toolbars.png | center]]
 +
 +
[[Категория:WxWidgets]]

Пожалуйста, обратите внимание, что все ваши добавления могут быть отредактированы или удалены другими участниками. Если вы не хотите, чтобы кто-либо изменял ваши тексты, не помещайте их сюда.
Вы также подтверждаете, что являетесь автором вносимых дополнений, или скопировали их из источника, допускающего свободное распространение и изменение своего содержимого (см. Wiki.crossplatform.ru:Авторское право). НЕ РАЗМЕЩАЙТЕ БЕЗ РАЗРЕШЕНИЯ ОХРАНЯЕМЫЕ АВТОРСКИМ ПРАВОМ МАТЕРИАЛЫ!