Skip to content
Advertisement

gtkmm, how to set a tool bar?

I want to have my toolbar at the top of the window, not vertically expanding and I want the entry to expand horizontally. Here a code :

main.cc

// g++ test2.cc `pkg-config gtkmm-3.0 --libs --cflags` -std=c++11
#include <gtkmm.h>

int main( int argc, char **argv)
{
  Glib::RefPtr< Gtk::Application >  app = Gtk::Application::create( "My.ToolBar.Drive.Me.Crazy" );

  /*window*/
  Gtk::Window * W1 = new Gtk::Window();
  W1->set_default_size(800, 600);

  /*box*/
  Gtk::Box * X1  = new Gtk::Box( Gtk::Orientation::ORIENTATION_VERTICAL );

  /*toolbar*/
  Gtk::Toolbar * T1 = new Gtk::Toolbar();

  /*button*/
  Gtk::ToolButton * B1 = new Gtk::ToolButton(Gtk::Stock::GO_BACK);

  /*button*/
  Gtk::ToolButton * B2 = new Gtk::ToolButton(Gtk::Stock::GO_FORWARD);

  /*tool item for the entry*/
  Gtk::ToolItem * I1 = new Gtk::ToolItem();

  /*entry*/
  Gtk::Entry * E1 = new Gtk::Entry();

  /*button*/
  Gtk::ToolButton * B3 = new Gtk::ToolButton(Gtk::Stock::GO_BACK);


  T1->append( *B1 );
  T1->append( *B2 );
  T1->append( *I1 );
  T1->append( *B3 );

  I1->add( *E1);

  X1->pack_start( *T1, true, true );

  W1->add( *X1 );

  W1->show_all();

  app->run( * W1 );

  delete B1;
  delete B2;
  delete B3;
  delete I1;
  delete E1;
  delete X1;
  delete W1;
}

I don’t understand what is the difference between expand and fill, there are two functions, set_hexpand() and set_vexpand() whatever I tried (the last two hours) with these functions I always get the same result as with code above.

Advertisement

Answer

1.- For X1, set expand to false.
2.- Make your toolitem also expandable.
3.- Add a expander to X1.

// c++ main.cpp -std=c++11 `pkg-config gtkmm-3.0 --libs --cflags`
#include <gtkmm.h>

int main( int argc, char **argv) {
  Glib::RefPtr< Gtk::Application >  app = Gtk::Application::create( "My.ToolBar.Drive.Me.Crazy" );
  /*window*/
  Gtk::Window * W1 = new Gtk::Window();
  W1->set_border_width (10);

  /*box*/
  Gtk::Box * X1  = new Gtk::Box( Gtk::ORIENTATION_VERTICAL );

  /*toolbar*/
  Gtk::Toolbar * T1 = new Gtk::Toolbar();

  /*button*/
  Gtk::ToolButton * B1 = new Gtk::ToolButton(Gtk::Stock::GO_BACK);

  /*button*/
  Gtk::ToolButton * B2 = new Gtk::ToolButton(Gtk::Stock::GO_FORWARD);

  /*tool item for the entry*/
  Gtk::ToolItem * I1 = new Gtk::ToolItem();
  I1->set_expand (); // true is default

  /*entry*/
  Gtk::Entry * E1 = new Gtk::Entry();

  /*button*/
  Gtk::ToolButton * B3 = new Gtk::ToolButton(Gtk::Stock::GO_BACK);

  /* vertical expander for the window */
  Gtk::Box *expander = Gtk::manage (new Gtk::Box (Gtk::Orientation::ORIENTATION_VERTICAL));

  T1->append( *B1 );
  T1->append( *B2 );
  I1->add (*E1);
  T1->append( *I1 );
  T1->append( *B3 );



  X1->pack_start( *T1, false, true );
  X1->pack_start( *expander, true, true );

  W1->add( *X1 );

  W1->show_all();

  app->run( * W1 );

  delete B1;
  delete B2;
  delete B3;
  delete I1;
  delete E1;
  delete X1;
  delete W1;
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement