Link to home
Start Free TrialLog in
Avatar of imedghar
imedghar

asked on

wxToolBar in wxwindows

Hi,
I´m programming an editor but with own toolbar (not these from windows), but I dont Know how to create an empty toolbar, and how to add Bitmaps to my own toolbar !!

Thank you in advance for your help.
here is the programm (written with DevC++) wher I have only a menu bar:

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

// the application icon (under Windows and OS/2 it is in resources)
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
    #include "mondrian.xpm"
#endif

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
            long style = wxDEFAULT_FRAME_STYLE);

    // event handlers (these functions should _not_ be virtual)
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);

private:
    DECLARE_EVENT_TABLE()
};


// IDs for the controls and the menu commands
enum
{
    // menu items
    Minimal_Quit = 1,
    Minimal_About = wxID_ABOUT
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
    EVT_MENU(Minimal_About, MyFrame::OnAbout)
END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

// ============================================================================
// implementation
// ============================================================================


// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
    // create the main application window
    MyFrame *frame = new MyFrame(_T("ModPlan Workbench"),
                                 wxPoint(50, 50), wxSize(650, 540));
    frame->Show(TRUE);
    return TRUE;
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
       : wxFrame(NULL, -1, title, pos, size, style)
{


#if wxUSE_MENUS
    // create a menu bar
    wxMenu *menuFile = new wxMenu;
    wxMenu *helpMenu = new wxMenu;
    helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));

    menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));

    // now append the freshly created menu to the menu bar...
    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(menuFile, _T("&File"));
    menuBar->Append(helpMenu, _T("&Help"));

    // ... and attach this menu bar to the frame
    SetMenuBar(menuBar);
#endif // wxUSE_MENUS

//CreateMyToolbar();
    wxToolBar *toolBar = new wxToolBar();

// ??????????????????

#if wxUSE_STATUSBAR
    // create a status bar just for fun (by default with 1 pane only)
    CreateStatusBar(2);
    SetStatusText(_T("Welcome to ModPlan Workbech!"));
#endif // wxUSE_STATUSBAR
}


// event handlers

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    // TRUE is to force the frame to close
    Close(TRUE);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxString msg;
    msg.Printf( _T("ModPlan.\n")
                _T("Project (PG463) at Dortmund university"), NULL);

    wxMessageBox(msg, _T("About Minimal"), wxOK | wxICON_INFORMATION, this);
}
Avatar of Dariuzk
Dariuzk

Hi  imedghar,

here is your toolbar example:


#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

// the application icon (under Windows and OS/2 it is in resources)
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
    #include "mondrian.xpm"
#endif

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
            long style = wxDEFAULT_FRAME_STYLE);

    // event handlers (these functions should _not_ be virtual)
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    void OnToolEnter(wxCommandEvent& event);

private:
    DECLARE_EVENT_TABLE()
};


// IDs for the controls and the menu commands
enum
{
    // menu items
    Minimal_Quit = 1,
    Minimal_About = wxID_ABOUT,
    tool1
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
    EVT_MENU(Minimal_About, MyFrame::OnAbout)
    EVT_TOOL(tool1, MyFrame::OnToolEnter)
END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

PATH="/opt/wx/2.5/bin:.:$PATH"
// ============================================================================
// implementation
// ============================================================================


// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
    // create the main application window
    MyFrame *frame = new MyFrame(_T("ModPlan Workbench"),
                                 wxPoint(50, 50), wxSize(650, 540));
    frame->Show(TRUE);
    return TRUE;
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
       : wxFrame(NULL, -1, title, pos, size, style)
{


#if wxUSE_MENUS
    // create a menu bar
    wxMenu *menuFile = new wxMenu;
    wxMenu *helpMenu = new wxMenu;
    helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));

    menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));

    // now append the freshly created menu to the menu bar...
    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(menuFile, _T("&File"));
    menuBar->Append(helpMenu, _T("&Help"));

    // ... and attach this menu bar to the frame
    SetMenuBar(menuBar);
#endif // wxUSE_MENUS

#if wxUSE_TOOLBAR
//CreateMyToolbar();
     // Create the toolbar
  CreateToolBar(wxNO_BORDER|wxHORIZONTAL|wxTB_FLAT, -1);//ID_TOOLBAR);
  GetToolBar()->SetMargins( 2, 2 );
  //wxToolBar *toolBar = new wxToolBar();
  GetToolBar()->AddTool(tool1, wxBitmap::wxBitmap(_T("test1.xpm"), wxBITMAP_TYPE_XPM), wxNullBitmap, false, 0, -1, (wxObject *) NULL, _T("New file"));
  GetToolBar()->AddSeparator();
  GetToolBar()->Realize();
  //  SetToolBar(toolBar);

// ??????????????????
#endif // wxUSE_TOOLBAR

#if wxUSE_STATUSBAR
    // create a status bar just for fun (by default with 1 pane only)
    CreateStatusBar(2);
    SetStatusText(_T("Welcome to ModPlan Workbech!"));
#endif // wxUSE_STATUSBAR
}


// event handlers

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    // TRUE is to force the frame to close
    Close(TRUE);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxString msg;
    msg.Printf( _T("ModPlan.\n")
                _T("Project (PG463) at Dortmund university"), NULL);

    wxMessageBox(msg, _T("About Minimal"), wxOK | wxICON_INFORMATION, this);
}

void MyFrame::OnToolEnter(wxCommandEvent& event)
{
  if (event.GetSelection() > -1)
  {
    wxString str;
    str.Printf(_T("This is tool number %d"), event.GetSelection());
    SetStatusText(str);
  }
  else
    SetStatusText(_T(""));

}
in your wxFrame class construcor add this:
//ToolBar
       
      CreateToolBar(wxNO_BORDER|wxHORIZONTAL|wxTB_FLAT, -1);
        wxToolBar    *toolBar  = GetToolBar();
      toolBar->SetMargins( 2, 2 );
      toolBar->AddTool(tool1, wxBitmap::wxBitmap(_T("test1.xpm"), wxBITMAP_TYPE_XPM), wxNullBitmap, false, 0, -1, (wxObject *) NULL, _T("New file"));
      toolBar->AddSeparator();
      toolBar->Realize();
Avatar of imedghar

ASKER

Hi Dariuzk,

thanks for your help, but there is something else, which doesnt work:
it gives Error bei  PATH="/opt/wx/2.5/bin:.:$PATH"  . what do you mean with it ??
I take this line away , it works and gives this Dialogsmessage:
Can´t load bitmap ´wxBITMAP_STD_COLOURS´from resources! Check .rc file !!!

so Ichecked the Toolbar_private.rc file (generated from devC++) , and there is:
// THIS FILE WILL BE OVERWRITTEN BY DEV-C++!
// DO NOT EDIT!
A ICON MOVEABLE PURE LOADONCALL DISCARDABLE "Toolbar.ico"

I hope some one can help me with this. Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Dariuzk
Dariuzk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial