Skip to content

Commit

Permalink
Added a undo helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
dalefugier committed Nov 21, 2024
1 parent 92c75f4 commit f6a650a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
76 changes: 76 additions & 0 deletions cpp/SampleCommands/SampleFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// 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.
/// </summary>
bool IsRhinoReparented()
{
HWND hParent = GetRealParent(RhinoApp().MainWnd());
HWND hDesktop = ::GetDesktopWindow();
return hParent != hDesktop;
}


/// <summary>
/// Begin a CRhinoDoc undo record.
/// </summary>
/// <param name="doc">The active document.</param>
/// <param name="pszDescription">The undo description.</param>
CRhinoDocUndoRecordHelper::CRhinoDocUndoRecordHelper(CRhinoDoc& doc, const wchar_t* pszDescription)
{
m_docRuntimeSerialNumber = doc.RuntimeSerialNumber();
m_undoRecordSerialNumber = doc.BeginUndoRecord(pszDescription);
}

/// <summary>
/// Begin a CRhinoDoc undo record.
/// </summary>
/// <param name="docRuntimeSerialNumber">The active document's runtime serial number.</param>
/// <param name="pszDescription">The undo description.</param>
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);
}
}

/// <summary>
/// Class destructor, ends the undo record.
/// </summary>
CRhinoDocUndoRecordHelper::~CRhinoDocUndoRecordHelper()
{
EndUndoRecord();
}

/// <summary>
/// Ends the undo record.
/// </summary>
/// <returns>true if the undo record was ended.</returns>
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;
}

47 changes: 47 additions & 0 deletions cpp/SampleCommands/SampleFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,50 @@ bool IsRhinoRunningAsExe();
/// Returns true if Rhino has input focus.
/// </summary>
bool RhinoHasFocus();

/// <summary>
/// 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.
/// </summary>
bool IsRhinoReparented();


/// <summary>
/// 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.
/// </summary>
class CRhinoDocUndoRecordHelper
{
public:
/// <summary>
/// Begin a CRhinoDoc undo record.
/// </summary>
/// <param name="doc">The active document.</param>
/// <param name="pszDescription">The undo description.</param>
CRhinoDocUndoRecordHelper(CRhinoDoc& doc, const wchar_t* pszDescription);

/// <summary>
/// Begin a CRhinoDoc undo record.
/// </summary>
/// <param name="docRuntimeSerialNumber">The active document's runtime serial number.</param>
/// <param name="pszDescription">The undo description.</param>
CRhinoDocUndoRecordHelper(unsigned int docRuntimeSerialNumber, const wchar_t* pszDescription);

/// <summary>
/// Class destructor, ends the undo record.
/// </summary>
~CRhinoDocUndoRecordHelper();

/// <summary>
/// Ends the undo record.
/// </summary>
/// <returns>true if the undo record was ended.</returns>
/// <remarks>The class destructor calls this method.</returns>
bool EndUndoRecord();

private:
unsigned int m_docRuntimeSerialNumber = 0;
unsigned int m_undoRecordSerialNumber = 0;
};
2 changes: 1 addition & 1 deletion cpp/SampleCommands/cmdSampleAddNurbsCircle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f6a650a

Please sign in to comment.