Ok I managed this now as you can see below. Moreover you can overwrite on_delete_event too to react on a closing window. But there are even unlogical things I don't understand: cout doesn't work in the "signal methods". It's possible to set a variable but what's the difference to cout.
But the next problem is even more ridiculous. You will see why:
Code:
#include "GUIMenu.h"
GUIMenu::GUIMenu() :
vTopBox(false, 10),
hButtonBox(false, 10),
hbox1(false, 10),
hbox2(false, 10),
hMatrixBox(false, 10),
buttonInput("Adjazenzmatrix eingeben"),
buttonClose("Schliessen"),
buttonBerechnungen("Berechnungen ausgeben"),
frameButton("Optionsmenü"),
frameMatrix(""),
labelSpin("Knotenanzahl: "),
labelMatrix("Adjazenzmatrix: "),
adjustment(3.0, 3.0, 15.0, 1.0, 5.0, 0.0), //float value, lower, upper, step_increment page_increment, page_size
spinbutton(adjustment),
alignmentRight(Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, 0.0, 0.0) {
isMatrixWindowOpen = false;
set_title("Graphenrechner");
set_border_width(0);
set_resizable(false);
labelSpin.set_alignment(Gtk::ALIGN_LEFT);
spinbutton.set_wrap();
//hbox1.set_border_width(10);
//hbox2.set_border_width(10);
hMatrixBox.set_border_width(5);
hButtonBox.set_border_width(5);
vTopBox.set_border_width(5);
hbox1.pack_start(labelSpin);
hbox1.pack_start(spinbutton);
hbox1.pack_start(buttonInput);
hbox1.pack_start(buttonBerechnungen);
alignmentRight.add(buttonClose);
hbox2.pack_start(alignmentRight, Gtk::PACK_EXPAND_WIDGET, 50);
hButtonBox.pack_start(hbox1);
hButtonBox.pack_start(hbox2);
frameButton.add(hButtonBox);
vTopBox.pack_start(frameButton);
//untere Hälfte einfügen
//hMatrixBox.pack_start(labelMatrix);
//frameMatrix.add(hMatrixBox);
//vTopBox.pack_start(frameMatrix);
//Top-Container dem Fenster hinzufügen
add(vTopBox);
//Signal setzen
buttonInput.signal_clicked().connect(sigc::mem_fun(*this, &GUIMenu::onInputClick) );
buttonClose.signal_clicked().connect(sigc::mem_fun(*this, &GUIMenu::onExitClick) );
matrixWindow.signal_delete_event().connect(sigc::mem_fun(*this, &GUIMenu::onMatrixClose) );
show_all_children();
}
GUIMenu::~GUIMenu() {
}
void GUIMenu::onExitClick() {
exit(0);
}
bool GUIMenu::onMatrixClose(GdkEventAny *event) {
isMatrixWindowOpen = false;
return false;
}
void GUIMenu::onInputClick() {
if (!isMatrixWindowOpen) {
createMatrixWindow();
isMatrixWindowOpen = true;
}
}
void GUIMenu::onMatrixClick(int j, int i, Gtk::Button *button, Gtk::Button *buttonMirror) {
if (i != j) {
if (button->get_label().compare(" 0 ") == 0) {
button->set_label(" 1 ");
button->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("green"));
if (buttonMirror != button) {
buttonMirror->set_label(" 1 ");
buttonMirror->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("green"));
}
}
else {
button->set_label(" 0 ");
button->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("grey"));
if (buttonMirror != button) {
buttonMirror->set_label(" 0 ");
buttonMirror->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("grey"));
}
}
}
}
//private
void GUIMenu::createTable(Gtk::Table &tab) {
//Es muss auch eine Matrix von Zeigern auf Gtk::Button erstellt werden,
//damit man bei ungerichteten Graphen die entgegengesetzten Buttons
//aus dem Programm ansprechen kann.
int knotenAnzahl = spinbutton.get_value_as_int();
Gtk::Button *pButton[knotenAnzahl][knotenAnzahl];
for (int i = 0; i < knotenAnzahl; i++) {
for (int j = i; j < knotenAnzahl; j++) {
pButton[i][j] = Gtk::manage(new Gtk::Button(" 0 "));
if (i == j) {
pButton[i][j]->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("red"));
tab.attach(*pButton[i][j], j, j+1, i, i+1);
pButton[i][j]->signal_clicked().connect(sigc::bind<int, int>
(sigc::mem_fun(*this, &GUIMenu::onMatrixClick), j, i, pButton[i][j], pButton[i][j]));
}
else {
pButton[j][i] = Gtk::manage(new Gtk::Button(" 0 "));
pButton[i][j]->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("grey"));
pButton[j][i]->modify_bg(Gtk::STATE_NORMAL, Gdk::Color("grey"));
tab.attach(*pButton[i][j], j, j+1, i, i+1);
tab.attach(*pButton[j][i], i, i+1, j, j+1);
pButton[i][j]->signal_clicked().connect(sigc::bind<int, int>
(sigc::mem_fun(*this, &GUIMenu::onMatrixClick), j, i, pButton[i][j], pButton[j][i]));
//Umgekehrt genauso, da man sonst die Buttons auf der anderen Seite nicht klicken kann
pButton[j][i]->signal_clicked().connect(sigc::bind<int, int>
(sigc::mem_fun(*this, &GUIMenu::onMatrixClick), j, i, pButton[i][j], pButton[j][i]));
}
}
}
}
//private
void GUIMenu::createMatrixWindow() {
Gtk::Table table;
Gtk::HBox hbox;
createTable(table);
matrixWindow.set_title("Adjazenzmatrix");
hbox.pack_start(table);
matrixWindow.add(hbox);
matrixWindow.set_resizable(false);
matrixWindow.show_all();
Gtk::Main::run(matrixWindow);
}
buttonInput is connected to the method onInputClick(). You see the variable isMatrixWindowOpen. It's set to false in the ctor.
When I click the button which opens the matrix window, the variable shoule be and will be set to true, so that only one window can stay open at the same time.
onMatrixClose set it to false, but this method won't be even called so you can ignore it now.
When I run the program and I click "buttonInput" once, the matrix window will be opened as wished. One should think that no problems with a second window will appear if I would click the button again, because isMatrixWindowOpen is set to false, but you never guess which console error I suddendly get:
(Main:5126): Gtk-WARNING **: Attempting to add a widget with type gtkmm__GtkHBox to a gtkmm__GtkWindow, but as a GtkBin subclass a gtkmm__GtkWindow can only contain one widget at a time; it already contains a widget of type gtkmm__GtkHBox
How does this crap happen? "createMatrixWindow" WILL NOT be called in this case. So how does this message come?
Maybe it's some similar thing like the issue that you can't cout anything out of the signal methods, but already these weird program behaviours conflicts with my logic.
Either I'm blind or something similar, or gtkmm seems for me to be not technically matured yet.
Markus