Pawel:
> If it interests you to sketch up a rough guess in code that would be
> really good, because I am not completely sure I've got what you mean..

I've cobbled something together to give a flavour of what I meant.
This example uses a single global std::vector variable. The vector
is never copied. The proxy holds a pointer to a window, which could
be NULL/0, but apart from initializing the pointer when the window
is [eventually] constructed, there is no further resource management.
When a window is closed, it is not removed from the proxy, nor the
proxy from the vector, and the windows remain valid but hidden until
the final one is closed and they are deleted during program shutdown.

If you make this much more complicated, you will need to either add
reference counting, or make deep copies of the windows and proxies.
Take care with extending the copy constructor, assignment operator
and the destructor. In this example, these are all mainly symbolic.
Quite honestly, unless you have some specific requirement for this,
I can't see that it is worth all of the extra effort.

Let's hope the code survives the mailer without reformatting...
D.

#include <Fl/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>

#include <cstdio>
#include <vector>

class MyWindow : public Fl_Window
{
  private:
    static void create_cb_(Fl_Widget* widget, void* data);
    void create_cb(Fl_Widget* widget, void* data);
    Fl_Button* m_button;
  public:
    MyWindow(int X, int Y, int W, int H, const char* T=0)
    : Fl_Window(X, Y, W, H, T)
    {
      m_button = new Fl_Button(10, 10, W-20, H-20, "Another?");
      m_button->callback(create_cb_, (void*)this);
      end();
    }
};

class MyProxy {
  private:
    static int xy;
    MyWindow* m_window;
  public:
    MyProxy()
    : m_window(0)
    {}

    MyProxy(const MyProxy& other)
    : m_window(other.m_window)
    {}

    MyProxy& operator=(const MyProxy& other)
    {
      if (this != &other) {
        m_window = other.m_window;
      }
      return *this;
    }

    ~MyProxy()
    {}

    MyWindow* myWindow()
    {
      if (m_window == 0) {
        m_window = new MyWindow(xy, xy, 200, 100, "MyWindow");
        xy += 10;
      }
      return m_window;
    }
};
int MyProxy::xy = 100;

std::vector<MyProxy> vectorProxies;

void MyWindow::create_cb_(Fl_Widget* widget, void* data)
{
  MyWindow* myWindow = (MyWindow*)data;
  myWindow->create_cb(widget, data);
}

void MyWindow::create_cb(Fl_Widget* widget, void* data)
{
  MyProxy localProxy;
  vectorProxies.push_back(localProxy);
  MyProxy& savedProxy = vectorProxies.back();
  // localProxy.m_window == 0 and savedProxy.m_window == 0
  savedProxy.myWindow()->show();
  // localProxy.m_window == 0 and savedProxy.m_window != 0
  // localProxy destructor called at end of scope,
  // but copy in vector lives on to fight another day
}

int main(int argc, char* argv[])
{
  MyProxy* firstProxy = new MyProxy();
  MyWindow* firstWindow = firstProxy->myWindow();
  firstWindow->show(argc, argv);
  return Fl::run();
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to