From f6a650a7219aa888f35a92a825b8649da3a57966 Mon Sep 17 00:00:00 2001 From: Dale Fugier Date: Thu, 21 Nov 2024 13:54:50 -0800 Subject: [PATCH] Added a undo helper class --- cpp/SampleCommands/SampleFunctions.cpp | 76 +++++++++++++++++++ cpp/SampleCommands/SampleFunctions.h | 47 ++++++++++++ .../cmdSampleAddNurbsCircle.cpp | 2 +- 3 files changed, 124 insertions(+), 1 deletion(-) diff --git a/cpp/SampleCommands/SampleFunctions.cpp b/cpp/SampleCommands/SampleFunctions.cpp index a8b82018..d0447b0b 100644 --- a/cpp/SampleCommands/SampleFunctions.cpp +++ b/cpp/SampleCommands/SampleFunctions.cpp @@ -388,3 +388,79 @@ bool RhinoHasFocus() } return false; } + +static HWND GetRealParent(HWND hWnd) +{ + // To obtain a window's owner window, instead of using GetParent, + // use GetWindow with the GW_OWNER flag. + HWND hWndOwner = ::GetWindow(hWnd, GW_OWNER); + if (NULL != hWndOwner) + return hWndOwner; + + // Obtain the parent window and not the owner + return GetAncestor(hWnd, GA_PARENT); +} + +/// +/// Returns true if the Rhino main window has been re-parented to some other application window. +/// Returns true if the Rhino main window parent is the Windows Desktop. +/// +bool IsRhinoReparented() +{ + HWND hParent = GetRealParent(RhinoApp().MainWnd()); + HWND hDesktop = ::GetDesktopWindow(); + return hParent != hDesktop; +} + + +/// +/// Begin a CRhinoDoc undo record. +/// +/// The active document. +/// The undo description. +CRhinoDocUndoRecordHelper::CRhinoDocUndoRecordHelper(CRhinoDoc& doc, const wchar_t* pszDescription) +{ + m_docRuntimeSerialNumber = doc.RuntimeSerialNumber(); + m_undoRecordSerialNumber = doc.BeginUndoRecord(pszDescription); +} + +/// +/// Begin a CRhinoDoc undo record. +/// +/// The active document's runtime serial number. +/// The undo description. +CRhinoDocUndoRecordHelper::CRhinoDocUndoRecordHelper(unsigned int docRuntimeSerialNumber, const wchar_t* pszDescription) +{ + CRhinoDoc* pDoc = CRhinoDoc::FromRuntimeSerialNumber(docRuntimeSerialNumber); + if (pDoc) + { + m_docRuntimeSerialNumber = pDoc->RuntimeSerialNumber(); + m_undoRecordSerialNumber = pDoc->BeginUndoRecord(pszDescription); + } +} + +/// +/// Class destructor, ends the undo record. +/// +CRhinoDocUndoRecordHelper::~CRhinoDocUndoRecordHelper() +{ + EndUndoRecord(); +} + +/// +/// Ends the undo record. +/// +/// true if the undo record was ended. +bool CRhinoDocUndoRecordHelper::EndUndoRecord() +{ + bool rc = (m_docRuntimeSerialNumber > 0 && m_undoRecordSerialNumber > 0); + if (rc) + { + CRhinoDoc* pDoc = CRhinoDoc::FromRuntimeSerialNumber(m_docRuntimeSerialNumber); + if (pDoc) + rc = pDoc->EndUndoRecord(m_undoRecordSerialNumber); + m_docRuntimeSerialNumber = m_undoRecordSerialNumber = 0; + } + return rc; +} + diff --git a/cpp/SampleCommands/SampleFunctions.h b/cpp/SampleCommands/SampleFunctions.h index 41740fce..d8fe4602 100644 --- a/cpp/SampleCommands/SampleFunctions.h +++ b/cpp/SampleCommands/SampleFunctions.h @@ -156,3 +156,50 @@ bool IsRhinoRunningAsExe(); /// Returns true if Rhino has input focus. /// bool RhinoHasFocus(); + +/// +/// Returns true if the Rhino main window has been re-parented to some other application window. +/// Returns true if the Rhino main window parent is the Windows Desktop. +/// +bool IsRhinoReparented(); + + +/// +/// CRhinoDoc::BeginUndoRecord and CRhinoDoc::EndUndoRecord helper. +/// Undo record will be ended when classes goes out of scope. +/// Useful in modeless user interface code that modifies document objects. +/// Not useful Rhino command, as Rhino's command handler tracks undo records. +/// +class CRhinoDocUndoRecordHelper +{ +public: + /// + /// Begin a CRhinoDoc undo record. + /// + /// The active document. + /// The undo description. + CRhinoDocUndoRecordHelper(CRhinoDoc& doc, const wchar_t* pszDescription); + + /// + /// Begin a CRhinoDoc undo record. + /// + /// The active document's runtime serial number. + /// The undo description. + CRhinoDocUndoRecordHelper(unsigned int docRuntimeSerialNumber, const wchar_t* pszDescription); + + /// + /// Class destructor, ends the undo record. + /// + ~CRhinoDocUndoRecordHelper(); + + /// + /// Ends the undo record. + /// + /// true if the undo record was ended. + /// The class destructor calls this method. + bool EndUndoRecord(); + +private: + unsigned int m_docRuntimeSerialNumber = 0; + unsigned int m_undoRecordSerialNumber = 0; +}; diff --git a/cpp/SampleCommands/cmdSampleAddNurbsCircle.cpp b/cpp/SampleCommands/cmdSampleAddNurbsCircle.cpp index 9a812f8c..2a3fb28f 100644 --- a/cpp/SampleCommands/cmdSampleAddNurbsCircle.cpp +++ b/cpp/SampleCommands/cmdSampleAddNurbsCircle.cpp @@ -42,7 +42,7 @@ CRhinoCommand::result CCommandSampleAddNurbsCircle::RunCommand(const CRhinoComma int degree = 2; int order = degree + 1; int cv_count = 9; - int knot_count = cv_count + degree - 1; + //int knot_count = cv_count + degree - 1; // Make a rational, degree 2 NURBS curve with 9 control points ON_NurbsCurve nc(dimension, bIsRational, order, cv_count);