[niepce: 13/22] The main frame is help weak by the application. Reorder a bit the initialization and the controller



commit 06d145542fa10e749110f90647f24d2747ce120a
Author: Hubert Figuière <hub figuiere net>
Date:   Fri Jul 25 22:36:32 2014 +0200

    The main frame is help weak by the application.
    Reorder a bit the initialization and the controller chain.
    
    This should solve the crash on quit.

 camerawire/src/cwapplication.cpp    |    5 ++-
 magellan/src/mgapplication.cpp      |    5 ++-
 src/fwk/toolkit/appframe.hpp        |    6 ++++
 src/fwk/toolkit/application.cpp     |   25 +++++++++--------
 src/fwk/toolkit/application.hpp     |    6 ++--
 src/fwk/toolkit/controller.cpp      |   12 ++++++--
 src/fwk/toolkit/controller.hpp      |   48 +++++++++++++++++-----------------
 src/fwk/toolkit/frame.cpp           |    9 +-----
 src/niepce/ui/niepceapplication.cpp |    7 +++--
 src/niepce/ui/niepcewindow.hpp      |    1 -
 10 files changed, 67 insertions(+), 57 deletions(-)
---
diff --git a/camerawire/src/cwapplication.cpp b/camerawire/src/cwapplication.cpp
index 8c4796b..f9ae294 100644
--- a/camerawire/src/cwapplication.cpp
+++ b/camerawire/src/cwapplication.cpp
@@ -43,8 +43,9 @@ namespace cw {
 
   fwk::AppFrame::Ptr CwApplication::makeMainFrame()
   {
-    m_main_frame = fwk::AppFrame::Ptr(new CwWindow);
-    return m_main_frame;
+    auto ptr = fwk::AppFrame::Ptr(new CwWindow);
+    m_main_frame = ptr;
+    return ptr;
   }
 
 void CwApplication::on_action_preferences()
diff --git a/magellan/src/mgapplication.cpp b/magellan/src/mgapplication.cpp
index 5506b0e..af78f75 100644
--- a/magellan/src/mgapplication.cpp
+++ b/magellan/src/mgapplication.cpp
@@ -43,8 +43,9 @@ namespace mg {
 
   fwk::AppFrame::Ptr MgApplication::makeMainFrame()
   {
-    m_main_frame = fwk::AppFrame::Ptr(new MgWindow);
-    return m_main_frame;
+    auto ptr = fwk::AppFrame::Ptr(new MgWindow);
+    m_main_frame = ptr;
+    return ptr;
   }
 
   void MgApplication::on_action_preferences()
diff --git a/src/fwk/toolkit/appframe.hpp b/src/fwk/toolkit/appframe.hpp
index 84fc7b3..86dd660 100644
--- a/src/fwk/toolkit/appframe.hpp
+++ b/src/fwk/toolkit/appframe.hpp
@@ -13,12 +13,18 @@ class AppFrame
 {
 public:
   typedef std::shared_ptr<AppFrame> Ptr;
+  typedef std::weak_ptr<AppFrame> WeakPtr;
 
   AppFrame(const std::string & layout_cfg_key = "")
     : Frame(new Gtk::ApplicationWindow(), layout_cfg_key)
     {
     }
 
+  virtual void on_ready()
+    {
+      gtkWindow().show();
+    }
+
   Glib::RefPtr<Gio::Menu> get_menu() const
     { return m_menu; }
 
diff --git a/src/fwk/toolkit/application.cpp b/src/fwk/toolkit/application.cpp
index b735ec3..9185658 100644
--- a/src/fwk/toolkit/application.cpp
+++ b/src/fwk/toolkit/application.cpp
@@ -97,18 +97,23 @@ int Application::main(const Application::Ptr & app,
     auto settings = Gtk::Settings::get_default();
     settings->set_property("gtk-application-prefer-dark-theme", use_dark);
 
-    auto window = app->makeMainFrame();
-    app->_add(window, false);
-    app->_ready();
-    app->m_gtkapp->run(window->gtkWindow(), argc, argv);
+    app->m_gtkapp->run(argc, argv);
+
+    DBG_OUT("end run");
+    app->terminate();
 
     return 0;
 }
 
 void Application::on_startup()
 {
-    set_menubar(m_main_frame->get_menu());
     init_actions();
+
+    auto window = makeMainFrame();
+    _add(window, true);
+    _ready();
+
+    set_menubar(window->get_menu());
 }
 
 void Application::init_actions()
@@ -157,18 +162,14 @@ void Application::init_actions()
 
 void Application::terminate()
 {
-    using std::placeholders::_1;
-    std::for_each(m_subs.begin(), m_subs.end(),
-                  std::bind(&Controller::terminate, _1));
-    std::for_each(m_subs.begin(), m_subs.end(),
-                  std::bind(&Controller::clearParent, _1));
-    m_subs.clear();
+    DBG_OUT("terminating");
+    Controller::terminate();
+    DBG_OUT("done terminating");
 }
 
 
 void Application::quit()
 {
-    terminate();
     m_gtkapp->quit();
 }
 
diff --git a/src/fwk/toolkit/application.hpp b/src/fwk/toolkit/application.hpp
index 9a9661b..eae821c 100644
--- a/src/fwk/toolkit/application.hpp
+++ b/src/fwk/toolkit/application.hpp
@@ -91,12 +91,12 @@ protected:
 
     void init_actions();
 
-    const AppFrame::Ptr & get_main_frame() const
-        { return m_main_frame; }
+    const AppFrame::Ptr get_main_frame() const
+        { return AppFrame::Ptr(m_main_frame); }
     /** bound the the GtkApplication startup signal */
     void on_startup();
 
-    AppFrame::Ptr                   m_main_frame;
+    AppFrame::WeakPtr            m_main_frame;
 private:
     Configuration                m_config;
     UndoHistory                  m_undo;
diff --git a/src/fwk/toolkit/controller.cpp b/src/fwk/toolkit/controller.cpp
index 55e8124..c2b4d94 100644
--- a/src/fwk/toolkit/controller.cpp
+++ b/src/fwk/toolkit/controller.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/toolkit/controller.cpp
  *
- * Copyright (C) 2007-2009 Hubert Figuiere
+ * Copyright (C) 2007-2014 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -34,7 +34,7 @@ Controller::Controller()
 
 Controller::~Controller()
 {
-//    DBG_OUT("destroy Controllers");
+    DBG_DTOR;
 }
 
 
@@ -48,7 +48,7 @@ Controller::add(const Controller::Ptr & sub)
 
 void Controller::remove(const Ptr & sub)
 {
-    std::list<Ptr>::iterator iter = std::find(m_subs.begin(), 
+    std::list<Ptr>::iterator iter = std::find(m_subs.begin(),
                                               m_subs.end(), sub);
     if(iter != m_subs.end()) {
         (*iter)->clearParent();
@@ -63,6 +63,12 @@ bool Controller::canTerminate()
 
 void Controller::terminate()
 {
+    DBG_OUT("terminating controller");
+    using std::placeholders::_1;
+    std::for_each(m_subs.begin(), m_subs.end(),
+                  std::bind(&Controller::terminate, _1));
+    clearParent();
+    m_subs.clear();
 }
 
 void Controller::_added()
diff --git a/src/fwk/toolkit/controller.hpp b/src/fwk/toolkit/controller.hpp
index cd84b78..2e6dbb1 100644
--- a/src/fwk/toolkit/controller.hpp
+++ b/src/fwk/toolkit/controller.hpp
@@ -40,37 +40,37 @@ class Controller
     , public sigc::trackable
 {
 public:
-               typedef std::shared_ptr<Controller> Ptr;
-               typedef std::weak_ptr<Controller> WeakPtr;
+    typedef std::shared_ptr<Controller> Ptr;
+    typedef std::weak_ptr<Controller> WeakPtr;
 
-               Controller();
-               virtual ~Controller();
+    Controller();
+    virtual ~Controller();
 
-               /** add a subcontroller to this one */
-               void add(const Ptr & sub);
-               /** clear the parent. Usually called by the parent when unparenting */
-               void clearParent()
+    /** add a subcontroller to this one */
+    void add(const Ptr & sub);
+    /** clear the parent. Usually called by the parent when unparenting */
+    void clearParent()
         { m_parent.reset(); }
-               void remove(const Ptr & sub);
-               
-               virtual bool canTerminate();
-               /** signal that the controller needs to terminate */
-               virtual void terminate();
-
-               /** called when everything is ready 
-                * subclasses should reimplement if needed
-                */
-               virtual void on_ready();
+    void remove(const Ptr & sub);
+
+    virtual bool canTerminate();
+    /** signal that the controller needs to terminate */
+    virtual void terminate();
+
+    /** called when everything is ready
+     * subclasses should reimplement if needed
+     */
+    virtual void on_ready();
 protected:
-               /** called when the controller has been added to a parent. */
-               virtual void _added();
+    /** called when the controller has been added to a parent. */
+    virtual void _added();
 
-               void _ready();
+    void _ready();
 
-               WeakPtr          m_parent;
-               std::list<Ptr> m_subs; /**< sub controllers */
+    WeakPtr          m_parent;
+    std::list<Ptr> m_subs; /**< sub controllers */
 
-               DataBinderPool m_databinders;
+    DataBinderPool m_databinders;
 };
 
 }
diff --git a/src/fwk/toolkit/frame.cpp b/src/fwk/toolkit/frame.cpp
index fc35ece..411470a 100644
--- a/src/fwk/toolkit/frame.cpp
+++ b/src/fwk/toolkit/frame.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/toolkit/application.cpp
  *
- * Copyright (C) 2007-2013 Hubert Figuiere
+ * Copyright (C) 2007-2014 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -43,15 +43,10 @@ Frame::Frame(Gtk::Window* win, const std::string & layout_cfg_key)
 }
 
 Frame::Frame(const std::string & layout_cfg_key)
-    : m_window(new Gtk::Window()),
-      m_builder(nullptr),
-      m_layout_cfg_key(layout_cfg_key)
+    : Frame(new Gtk::Window(), layout_cfg_key)
 {
-    connectSignals();
-    frameRectFromConfig();
 }
 
-
 Frame::Frame(const std::string & gladeFile,
              const Glib::ustring & widgetName,
              const std::string & layout_cfg_key)
diff --git a/src/niepce/ui/niepceapplication.cpp b/src/niepce/ui/niepceapplication.cpp
index aaf27c9..49d9f84 100644
--- a/src/niepce/ui/niepceapplication.cpp
+++ b/src/niepce/ui/niepceapplication.cpp
@@ -58,8 +58,9 @@ Application::Ptr NiepceApplication::create(int & argc, char** & argv)
 
 AppFrame::Ptr NiepceApplication::makeMainFrame()
 {
-    m_main_frame = AppFrame::Ptr(new NiepceWindow);
-    return m_main_frame;
+    auto ptr = AppFrame::Ptr(new NiepceWindow);
+    m_main_frame = ptr;
+    return ptr;
 }
 
 void NiepceApplication::on_action_file_open()
@@ -86,7 +87,7 @@ void NiepceApplication::on_action_preferences()
     DBG_OUT("on_preferences");
 
     auto dlg(new PreferencesDialog());
-    dlg->run_modal(m_main_frame);
+    dlg->run_modal(AppFrame::Ptr(m_main_frame));
 
     DBG_OUT("end on_preferences");
 }
diff --git a/src/niepce/ui/niepcewindow.hpp b/src/niepce/ui/niepcewindow.hpp
index 9964c88..a1b80ca 100644
--- a/src/niepce/ui/niepcewindow.hpp
+++ b/src/niepce/ui/niepcewindow.hpp
@@ -68,7 +68,6 @@ private:
     void create_initial_labels();
     void on_lib_notification(const eng::LibNotification & n);
 
-    void init_ui();
     void init_actions();
 
     // UI to open library


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]