0

The goal is to connect GTK+ signals to non-static class methods using libsigc++ without gtkmm. I want to use the Glade user interface designer to design a non-trivial UI with many views, comparable to a setup wizard. The UI should be portable (Embedded Linux and Windows). So, I want to reduce the dependencies and use the C GTK+ only without the C++ language binding component gtkmm, but the UI should be written in C++. The MVC pattern should be applied to separate the responsibilities and for decoupling. As a result, my C++ views and other classes have to connect their signal handlers to the GTK+ C signals using g_signal_connect(). Later they have to use g_signal_handlers_disconnect_by_func() to disconnect the handlers. The following example demonstrates the problem:

File: GladeProgram.h

#ifndef _GLADEPROGRAMM_H
#define _GLADEPROGRAMM_H

#include "gtk/gtk.h"

class GladeProgram
{
public:
  GladeProgram(int argc, char *argv[]);
  virtual ~GladeProgram();

  void Run();
  void Exit();
};

#endif // !_GLADEPROGRAMM_H

File: GladeProgram.cpp

#include "GladeProgram.h"
#include "sigc++/sigc++.h"

GladeProgram::GladeProgram(int argc, char *argv[])
{
  gtk_init(&argc, &argv);
}

GladeProgram::~GladeProgram()
{
}

// C-style callback method with object pointer
void onExit(GladeProgram* pProg)
{
  pProg->Exit();
}

void GladeProgram::Run()
{
  GtkBuilder *builder;
  GtkWidget  *window;

  builder = gtk_builder_new_from_file("window_main.glade");

  window  = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));    

  // C-style signal handler with object pointer
  g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(onExit), this);

  // PROBLEM: Howto connect non-static class method GladeProgram::Exit() with libsigc++ ???
  // g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK( ??? ), NULL);

  g_object_unref(builder);

  gtk_widget_show(window);

  gtk_main();
}

void GladeProgram::Exit()
{
  gtk_main_quit();
}

File: Main.cpp

#include <iostream>
#include "GladeProgram.h"

using namespace std;

int main(int argc, char *argv[])
{
  std::cout << "**********************************************************************" << endl;
  std::cout << "*                             GTK+ Test UI                           *" << endl;
  std::cout << "**********************************************************************" << endl;

  GladeProgram prog(argc, argv);

  prog.Run();

  // wait until the Enter key is pressed
  cout << endl << "Press [Enter] to exit." << endl;  
  cin.get();

  return 0;
}

The test program (console program) currently runs on Windows 10 and is compiled with Visual Studio 2017.

Any help is appreciated.

4

0 回答 0