0

I'm trying to add an info bar to a dialog when my program is not connected to the databse. The info bar shows, however, if I close the dialog and open it again, every time there is a new button. Here's my code:

Window.hpp

class Window: public Gtk::Window {
private:
/// more things
  Gtk::InfoBar info;
  Gtk::Container* infoBarContainer;
  Gtk::Label info_label;
protected:
  void onConnectedBody(void);
  void onNotConnectedBody(void);
  void onInfoBarResponse(int response);
  void onMenuDbConnect(void);
  void onMenuDbDisconnect(void);
public:
  /// more things...
};

Window.cpp

Window::Window(
) :
 infoBarContainer(0),
 info_label("")
{
  // more things
  info.add_button(Gtk::Stock::OK, 0);
  infoBarContainer = dynamic_cast<Gtk::Container*>(info.get_content_area());
  if( infoBarContainer ) infoBarContainer->add(info_label);      
}

Window::onMenuDbConnect(){
  if( controller->Connected() ) {
    onConnectedBody();
  } else {
    onNotConnectedBody();
  }
}

Window::onConnectedBody(){
 Gtk::Dialog dialog("Connected to database",true,true);
  dialog.set_resizable( false );
  dialog.set_has_separator(true);
  Gtk::Button close;
  Gtk::VBox* vbox = dialog.get_vbox();
  Gtk::Label label;
  label.set_markup("<b>Already connected.</b>"); 
  vbox->pack_start(label,false,false);
  close.set_label("Close");
  close.signal_clicked().connect(sigc::mem_fun(*this,&Window::onButtonClose));
  dialog.set_size_request(250,80);
  vbox = 0;
  close.set_can_default( true );
  dialog.add_action_widget(close,1);
  close.grab_default();

  dialog.show_all_children();
  dialog.run();    
}
Window::onNotConnectedBody(){
 Gtk::Dialog dialog("Connect to database",true,true);
 dialog.set_resizable( false );
 dialog.set_has_separator(true);
 Gtk::Button close;
 Gtk::VBox* b = dialog.get_vbox();

 info.signal_response().connect(sigc::mem_fun(*this,&Window::onInfoBarResponse) ); 
 info_label.set_text("Remember to fill all fields");
 b->pack_start(info,Gtk::PACK_SHRINK);

 Gtk::Label label;
 label.set_markup("<b>Database:</b>");
 Gtk::Label label_host;
 label_host.set_markup("<b>Host:</b>");
 Gtk::Label label_user;
 label_user.set_markup("<b>User:</b>");  
 Gtk::Label label_pass;
 label_pass.set_markup("<b>Pass:</b>");  

 label.set_justify(Gtk::JUSTIFY_LEFT);
 label_host.set_justify(Gtk::JUSTIFY_LEFT);
 label_user.set_justify(Gtk::JUSTIFY_LEFT);
 label_pass.set_justify(Gtk::JUSTIFY_LEFT);

 entry.set_max_length(10);
 entry.set_text("Figures");
 entry_host.set_text("localhost");
 label.set_markup("<b>Database:</b>");

 b->pack_start(label,false,false);
 b->pack_start(entry,false,false);
 b->pack_start(label_host,false,false);
 b->pack_start(entry_host,false,false);
 b->pack_start(label_user,false,false);
 b->pack_start(entry_user,false,false);

  b->pack_start(label_pass,false,false);
 b->pack_start(entry_pass,false,false);

 close.set_label("Connect");
 close.signal_clicked().connect(sigc::mem_fun(*this,&Window::onButtonDbConnect));
 close.set_can_default( true );
 dialog.add_action_widget(close,1);
 close.grab_default();
 dialog.set_size_request(250,250);
 dialog.show_all_children();
 dialog.run();
}
void Window::onInfoBarResponse(int response) {
  info.hide();
}

So, what I want is to show the info bar whenever I enter the onMenuDbConnect and is not yet connected to the database. If it is connected, no info bar is shown. The other components of my program, like the DB controller, works perfectly.

Any ideas? Thaanks.

Adri C.S.
  • 2,909
  • 5
  • 36
  • 63
  • I can't recreate what you describe. I copied your code (and added what was needed to make it a working program) and if I returned `true` in onMenuDBConnect, a different dialog showed with no infobar. If I return `false`, a connection dialog appears with an infobar. – Christian Smith Sep 28 '12 at 02:18
  • Thanks for your reply. Yes, I made a mistake and uploaded another version of the code. With the one I've uploaded, an info bar shows, however with no text on it and no 'OK' button(at least in my case), so that's a problem too. If I remove the initialization of the info bar and its container in the constructor and put them inside onNotConnectedBody, the problem I described in the question arises. I'll post the code in pastebin: http://pastebin.com/AGDN44CC – Adri C.S. Sep 29 '12 at 15:10
  • With your new code (I assumed it was the bottom code from the pastebin post) the infobar keeps adding buttons. I moved the initialization of the infobar stuff from the class itself to Window::onNotConnectedBody() and it seems to work now. (I also made sure it compiled with gtk-3.0, but am not sure if that is relevant) – Christian Smith Oct 01 '12 at 14:11
  • Hi!. I moved the initialization into Window::onNotConnectedBody() but it still adds buttons. This is driving me crazy. My Gtk version is 2.4, by the way. – Adri C.S. Oct 04 '12 at 14:41

0 Answers0