Skip to content

Commit

Permalink
Merge pull request #233 from mcorino/develop
Browse files Browse the repository at this point in the history
adding persistence and improved config support
  • Loading branch information
mcorino committed Jan 5, 2024
2 parents e7b6b54 + d94798c commit af7a4df
Show file tree
Hide file tree
Showing 42 changed files with 2,217 additions and 204 deletions.
28 changes: 23 additions & 5 deletions ext/wxruby3/include/wxruby-Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static int wxrb_CountConfig(VALUE key, VALUE value, VALUE rbCounter)
}

static VALUE g_cConfigBase;
static VALUE g_cConfigWx;
static VALUE g_cConfig;

/*
Expand Down Expand Up @@ -88,12 +89,18 @@ class wxRbHashConfig : public wxConfigBase

virtual ~wxRbHashConfig()
{
DATA_PTR(m_cfgInstance) = 0; // make sure it never get's deleted twice
if (!NIL_P(m_cfgInstance))
{
DATA_PTR(m_cfgInstance) = 0; // make sure it never get's deleted twice
}
}

// Get wrapped Ruby ConfigBase instance
// Get wrapped Ruby Config instance
VALUE GetRubyConfig() const { return m_cfgInstance; }

// Reset wrapped Ruby Config instance
void ResetRubyConfig() { m_cfgInstance = Qnil; }

// implement inherited pure virtual functions
virtual void SetPath(const wxString& strPath) override { DoSetPath(strPath, true /* create missing components */); }
virtual const wxString& GetPath() const override { return m_strPath; }
Expand Down Expand Up @@ -806,7 +813,7 @@ class wxRbHashConfig : public wxConfigBase

void SetRootPath()
{
m_strPath.Empty();
m_strPath.Clear();
m_cfgGroup = m_cfgHash;
m_cfgGroupKeys = Qnil;
}
Expand All @@ -815,7 +822,7 @@ class wxRbHashConfig : public wxConfigBase
// if path doesn't exist and createMissingComponents == false
bool DoSetPath(const wxString& strPath, bool createMissingComponents)
{
if ( strPath.empty() )
if ( strPath.IsEmpty() || strPath == cfgSepStr)
{
SetRootPath();
return true;
Expand Down Expand Up @@ -913,7 +920,8 @@ static const char * __iv_Config_data = "@data";

WXRUBY_EXPORT bool wxRuby_IsRubyConfig(VALUE rbConfig)
{
return rb_obj_is_kind_of(rbConfig, g_cConfig) == Qtrue;
return rb_obj_is_kind_of(rbConfig, g_cConfig) == Qtrue ||
rb_obj_is_kind_of(rbConfig, g_cConfigWx) == Qtrue;
}

// Wrap a Ruby hash for input type mapping
Expand All @@ -933,6 +941,12 @@ WXRUBY_EXPORT wxConfigBase* wxRuby_Ruby2ConfigBase(VALUE rbConfig)
// return wrapper
return config;
}
else if (rb_obj_is_kind_of(rbConfig, g_cConfigWx) == Qtrue)
{
wxConfigBase* cfg;
Data_Get_Struct(rbConfig, wxConfigBase, cfg);
return cfg;
}
return nullptr;
}

Expand All @@ -946,6 +960,10 @@ WXRUBY_EXPORT VALUE wxRuby_ConfigBase2Ruby(wxConfigBase* config)
{
return hsh_config->GetRubyConfig();
}
else
{
return Data_Wrap_Struct(g_cConfigWx, 0, 0, config);
}
}
return Qnil;
}
Expand Down
79 changes: 79 additions & 0 deletions ext/wxruby3/include/wxruby-Persistence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2023 M.J.N. Corino, The Netherlands
//
// This software is released under the MIT license.

/*
* WxRuby3 persistence classes
*/

#ifndef _WXRUBY_PERSISTENCE_HASH_H
#define _WXRUBY_PERSISTENCE_HASH_H

#include <wx/persist.h>
#include <wx/config.h>

#include <map>

/*
This class serves as a base for any Ruby defined persistence manager in order to provide
customized save and restore methods for Ruby values but also as a replacement for the
default global persistence manager instance.
*/
class WxRubyPersistenceManager : public wxPersistenceManager
{
private:
typedef std::map<VALUE, VALUE> rb_object_to_rb_po_map_t;
rb_object_to_rb_po_map_t rb_object_po_map_;

public:
WxRubyPersistenceManager() : wxPersistenceManager() {}

bool SaveRubyValue(const wxPersistentObject& who, const wxString& name, VALUE value);

VALUE RestoreRubyValue(const wxPersistentObject& who, const wxString& name);


bool DoSaveRubyValue(const wxPersistentObject& who, const wxString& name, VALUE value);

VALUE DoRestoreRubyValue(const wxPersistentObject& who, const wxString& name);

void RegisterRbPO(VALUE rb_obj, VALUE rb_po)
{
rb_object_po_map_[rb_obj] = rb_po;
}

VALUE FindRbPO(VALUE rb_obj)
{
VALUE rb_po = Qnil;
if (rb_object_po_map_.count(rb_obj) > 0)
{
rb_po = rb_object_po_map_[rb_obj];
}
return rb_po;
}

VALUE UnregisterRbPO(VALUE rb_obj)
{
VALUE rb_po = Qnil;
if (rb_object_po_map_.count(rb_obj) > 0)
{
rb_po = rb_object_po_map_[rb_obj];
rb_object_po_map_.erase(rb_obj);
}
return rb_po;
}

void GC_markPO();

static void UnregisterPersistentObject(VALUE rb_obj);
};

class WxRubyPersistentObject : public wxPersistentObject
{
public:
virtual ~WxRubyPersistentObject();
protected:
WxRubyPersistentObject(VALUE rb_obj);
};

#endif /* _WXRUBY_PERSISTENCE_HASH_H */
6 changes: 6 additions & 0 deletions ext/wxruby3/swig/memory_management.i
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ GC_NEVER(kls);
%define GC_MANAGE_AS_UNTRACKED(kls)
%enddef

// Strategy for objects that are GC marked through customized, tailored, mechanisms outside
// of the standard SWIG object tracking option.
// The different naming is mostly to allow doc gen to properly recognize these objects.
%define GC_MANAGE_AS_MARKED(kls)
%enddef

// Sizers attached to windows are automatically destroyed by wxWidgets,
// so they should not be deleted.
//
Expand Down
16 changes: 16 additions & 0 deletions lib/wx/core/book_ctrl_base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2023 M.J.N. Corino, The Netherlands
#
# This software is released under the MIT license.

module Wx

class BookCtrlBase

# create PersistentObject for toplevel windows (incl. Dialog and Frame)
def create_persistent_object
PersistentBookCtrl.new(self)
end

end

end
Loading

0 comments on commit af7a4df

Please sign in to comment.