I tried to write a similar program to that I found at: https://github.com/GNOME/gtkmm-documentation/tree/master/examples/book/application/app_and_win_menus

The problem is that menu does not show itself but menu items do when I click the place the menu bar should appear. How to make menu bar visible? Below is my program. I would appreciate any help because I'm struggling for a long time with the case.



main.cpp

#include <iostream>
#include <gtkmm.h>

#include "simwdg.h"



int main ( int argc, char **argv )
{
    Glib::RefPtr<SimApp> app = (Glib::RefPtr<SimApp>)SimApp::create ( );

    app->run(argc, argv);

    return 0;
}

-------------------

simwdg.h

#ifndef SIMMENU_H
#define SIMMENU_H

#include <gtkmm.h>


class SimAppWnd;

class SimApp : public Gtk::Application
{
public:
    SimApp ( );
    virtual ~SimApp();

public:
    virtual void on_startup();
    virtual void on_activate();

    static Glib::RefPtr<SimApp> create();

public:
    bool on_delete(GdkEventAny* event);
    void on_file_menu();
    void on_file_menu_new();
    void on_file_menu_close();

private:
    SimAppWnd *wnd;
};



class SimAppWnd : public Gtk::ApplicationWindow
{
public:
    SimAppWnd();
    virtual ~SimAppWnd();

private:
    Gtk::DrawingArea darea;

public:
    bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
    void on_file_menu_close();
};

#endif // SIMMENU_H

------------------

simwdg.cpp

#include <iostream>

#include "simwdg.h"



SimApp::SimApp ( ) : Gtk::Application ( "KJ.Projects.SimApp" )
{
    Glib::set_application_name ( "App" );
}

SimApp::~SimApp()
{

}

void SimApp::on_startup()
{
Glib::RefPtr<Gio::Menu> menu = ( Glib::RefPtr<Gio::Menu> ) Gio::Menu::create(), wmenu = ( Glib::RefPtr<Gio::Menu> ) Gio::Menu::create(), fmenu = ( Glib::RefPtr<Gio::Menu> ) Gio::Menu::create();

    std::cout << "Startup called" << std::endl;
    Gio::Application::on_startup();
add_action ( "filenew", sigc::mem_fun ( *this, &SimApp::on_file_menu_new ) ); add_action ( "fileclose", sigc::mem_fun ( *this, &SimApp::on_file_menu_close ) );

    menu->append ( "New", "app.filenew" );
    set_app_menu ( menu );

    fmenu->append("Close", "app.fileclose");
    wmenu->append_submenu("File", fmenu);
    set_menubar(wmenu);
}

void SimApp::on_activate()
{
    std::cout << "Activate called" << std::endl;
    Gio::Application::on_activate();

    wnd = new SimAppWnd;
    add_window ( *wnd );
wnd->signal_delete_event().connect ( sigc::mem_fun ( *this, &SimApp::on_delete ) );
    hold();
    wnd->show_all();
}

Glib::RefPtr< SimApp > SimApp::create ( )
{
    std::cout<<"Create application"<<std::endl;
    return Glib::RefPtr<SimApp> ( new SimApp() );
}

bool SimApp::on_delete ( GdkEventAny* event )
{
Gtk::ApplicationWindow *win= ( Gtk::ApplicationWindow * ) get_active_window();

    win->set_show_menubar ( false );
    release();
    return false;
}

SimAppWnd::SimAppWnd()
{
    Gtk::ApplicationWindow();
    set_title ( "Simulation" );
    set_default_size ( 300, 300 );
}

SimAppWnd::~SimAppWnd()
{

}

void SimApp::on_file_menu()
{
    std::cout << "File menu" << std::endl;
}

void SimApp::on_file_menu_new()
{
    std::cout << "(app) File menu new" << std::endl;
}

bool SimAppWnd::on_draw ( const Cairo::RefPtr< Cairo::Context >& cr )
{
    Gtk::Allocation allocation = get_allocation();
    const int width = allocation.get_width();
    const int height = allocation.get_height();

std::cout << "On draw: " << "w: " << width << " h: " << height << std::endl;

    // coordinates for the center of the window
//     int xc, yc;
//     xc = width / 2;
//     yc = height / 2;
//
//     cr->set_line_width ( 10.0 );
//
//     // draw red lines out from the center of the window
//     cr->set_source_rgb ( 0.8, 0.0, 0.0 );
//     cr->move_to ( 0, 0 );
//     cr->line_to ( xc, yc );
//     cr->line_to ( 0, height );
//     cr->move_to ( xc, yc );
//     cr->line_to ( width, yc );
//     cr->stroke();

    return true;
}

void SimApp::on_file_menu_close()
{
    std::cout << "(app) File menu close" << std::endl;
}


--
Regards
Krzysztof

_______________________________________________
gtk-list mailing list
gtk-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-list

Reply via email to