From 799cc4916be4ab04f4897c7f83b1da9c951d599c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Laban?= Date: Thu, 16 May 2019 16:39:17 -0400 Subject: [PATCH] Add support for SetExpressionDisplay --- src/CalcManager/CCalcManager.cpp | 166 ++++++++++-------- src/CalcManager/CCalcManager.h | 20 +-- .../CalcManager/CalculatorManager.Interop.cs | 10 +- .../CalcManager/CalculatorManager.cs | 2 +- .../CalcManager/ExpressionCommandInterface.cs | 12 +- .../ExpressionItemTemplateSelector.cs | 2 +- .../ViewModels/StandardCalculatorViewModel.cs | 2 +- src/Calculator.Shared/Views/Calculator.xaml | 2 +- 8 files changed, 111 insertions(+), 105 deletions(-) diff --git a/src/CalcManager/CCalcManager.cpp b/src/CalcManager/CCalcManager.cpp index 9f8393e2..eab5ad20 100644 --- a/src/CalcManager/CCalcManager.cpp +++ b/src/CalcManager/CCalcManager.cpp @@ -75,11 +75,20 @@ public: } virtual void SetExpressionDisplay( - std::shared_ptr>> const& /*tokens*/, - std::shared_ptr>> const& /*commands*/) override + std::shared_ptr>> const& tokens, + std::shared_ptr>> const& commands) override { printf("Native:SetExpressionDisplay()\n"); + auto item = std::make_shared(); + item->historyItemVector.expression = L""; + item->historyItemVector.result = L""; + item->historyItemVector.spCommands = commands; + item->historyItemVector.spTokens = tokens; + + auto pItem = MarshalHistoryItem(item); + + _params.SetExpressionDisplay(_params.CalculatorState, pItem); } virtual void SetParenthesisNumber(unsigned int count) override @@ -182,6 +191,83 @@ const wchar_t* ToWChar(std::wstring& str) return out; } +GetHistoryItemResult* MarshalHistoryItem(std::shared_ptr& historyItem) +{ + auto itemResult = new GetHistoryItemResult{}; + + itemResult->expression = ToWChar(historyItem->historyItemVector.expression); + itemResult->result = ToWChar(historyItem->historyItemVector.result); + + unsigned int tokenCount; + historyItem->historyItemVector.spTokens->GetSize(&tokenCount); + itemResult->TokenCount = tokenCount; + + // + // Marshal Tokens + // + auto tokenStrings = new const wchar_t* [tokenCount] {}; + auto tokenValues = new int32_t[tokenCount]{}; + + // DBGPRINT(L"TokenCount: %d (int32_t: %d)\n", tokenCount, sizeof(int32_t)); + + for (uint32_t j = 0; j < tokenCount; j++) + { + std::pair pair; + + if (SUCCEEDED(historyItem->historyItemVector.spTokens->GetAt(j, &pair))) + { + tokenStrings[j] = ToWChar(pair.first); + tokenValues[j] = (int32_t)pair.second; + // DBGPRINT(L"\tPair: %ws;%d\n", pair.first.data(), tokenValues[j]); + } + } + + itemResult->TokenStrings = tokenStrings; + itemResult->TokenValues = tokenValues; + + // + // Marshal Commands + // + unsigned int commandCount; + historyItem->historyItemVector.spCommands->GetSize(&commandCount); + itemResult->CommandCount = commandCount; + + auto commands = new void* [commandCount] {}; + + for (uint32_t commandId = 0; commandId < commandCount; commandId++) + { + std::shared_ptr command; + if (SUCCEEDED(historyItem->historyItemVector.spCommands->GetAt(commandId, &command))) + { + commands[commandId] = command.get(); + } + } + + itemResult->Commands = commands; + + return itemResult; +} + +void* MarshalHistoryItems(std::vector>& historyItems) +{ + auto result = new GetHistoryItemsResult{}; + + result->ItemsCount = (int32_t)historyItems.size(); + + auto resultsArray = new GetHistoryItemResult*[result->ItemsCount]; + result->HistoryItems = (void*)resultsArray; + + for (size_t i = 0; i < historyItems.size(); i++) + { + auto historyItem = historyItems[i]; + + resultsArray[i] = MarshalHistoryItem(historyItem); + } + + return result; +} + + void* CalculatorManager_Create(CalculatorManager_CreateParams* pParams) { auto calcDisplay = new CalcDisplay(*pParams); @@ -316,82 +402,6 @@ void CalculatorManager_SetInHistoryItemLoadMode(void* manager, bool isHistoryIte AsManager(manager)->SetInHistoryItemLoadMode(isHistoryItemLoadMode); } -GetHistoryItemResult* MarshalHistoryItem(std::shared_ptr& historyItem) -{ - auto itemResult = new GetHistoryItemResult{}; - - itemResult->expression = ToWChar(historyItem->historyItemVector.expression); - itemResult->result = ToWChar(historyItem->historyItemVector.result); - - unsigned int tokenCount; - historyItem->historyItemVector.spTokens->GetSize(&tokenCount); - itemResult->TokenCount = tokenCount; - - // - // Marshal Tokens - // - auto tokenStrings = new const wchar_t* [tokenCount] {}; - auto tokenValues = new int32_t[tokenCount]{}; - - // DBGPRINT(L"TokenCount: %d (int32_t: %d)\n", tokenCount, sizeof(int32_t)); - - for (uint32_t j = 0; j < tokenCount; j++) - { - std::pair pair; - - if (SUCCEEDED(historyItem->historyItemVector.spTokens->GetAt(j, &pair))) - { - tokenStrings[j] = ToWChar(pair.first); - tokenValues[j] = (int32_t)pair.second; - // DBGPRINT(L"\tPair: %ws;%d\n", pair.first.data(), tokenValues[j]); - } - } - - itemResult->TokenStrings = tokenStrings; - itemResult->TokenValues = tokenValues; - - // - // Marshal Commands - // - unsigned int commandCount; - historyItem->historyItemVector.spCommands->GetSize(&commandCount); - itemResult->CommandCount = commandCount; - - auto commands = new void*[commandCount]{}; - - for (uint32_t commandId = 0; commandId < commandCount; commandId++) - { - std::shared_ptr command; - if (SUCCEEDED(historyItem->historyItemVector.spCommands->GetAt(commandId, &command))) - { - commands[commandId] = command.get(); - } - } - - itemResult->Commands = commands; - - return itemResult; -} - -void* MarshalHistoryItems(std::vector>& historyItems) -{ - auto result = new GetHistoryItemsResult{}; - - result->ItemsCount = (int32_t)historyItems.size(); - - auto resultsArray = new GetHistoryItemResult*[result->ItemsCount]; - result->HistoryItems = (void*)resultsArray; - - for (size_t i = 0; i < historyItems.size(); i++) - { - auto historyItem = historyItems[i]; - - resultsArray[i] = MarshalHistoryItem(historyItem); - } - - return result; -} - void* CalculatorManager_GetHistoryItems(void* manager) { auto historyItems = AsManager(manager)->GetHistoryItems(); diff --git a/src/CalcManager/CCalcManager.h b/src/CalcManager/CCalcManager.h index 76cf983d..4eb4e663 100644 --- a/src/CalcManager/CCalcManager.h +++ b/src/CalcManager/CCalcManager.h @@ -6,23 +6,12 @@ #include "headers/Rational.h" #include "headers/ICalcDisplay.h" - -struct TokenPair { - char* Item1; - int Item2; -}; - -struct ExpressionDisplayData { - int TokenCount; - TokenPair* Tokens; - - int CommandCount; - void* Commands; -}; +struct GetHistoryItemResult; +struct GetHistoryItemsResult; typedef void (*SetPrimaryDisplayFunc)(void* state, const char* text, bool isError); typedef void (*SetIsInErrorFunc)(void* state, bool isInError); -typedef void (*SetExpressionDisplayFunc)(void* state, ExpressionDisplayData* data); +typedef void (*SetExpressionDisplayFunc)(void* state, GetHistoryItemResult* data); typedef void (*SetParenthesisNumberFunc)(void* state, unsigned int count); typedef void (*OnNoRightParenAddedFunc)(void* state); typedef void (*MaxDigitsReachedFunc)(void* state); @@ -57,6 +46,9 @@ struct CalculatorManager_CreateParams { #define DLL_EXPORT __declspec(dllexport) #endif +GetHistoryItemResult* MarshalHistoryItem(std::shared_ptr& historyItem); +void* MarshalHistoryItems(std::vector>& historyItems); + extern "C" { struct GetHistoryItemsResult diff --git a/src/Calculator.Shared/CalcManager/CalculatorManager.Interop.cs b/src/Calculator.Shared/CalcManager/CalculatorManager.Interop.cs index d66d1a09..11e382ab 100644 --- a/src/Calculator.Shared/CalcManager/CalculatorManager.Interop.cs +++ b/src/Calculator.Shared/CalcManager/CalculatorManager.Interop.cs @@ -106,7 +106,7 @@ namespace CalculationManager public delegate void MemoryItemChangedCallbackFunc(IntPtr state, int indexOfMemory); public delegate void OnHistoryItemAddedCallbackFunc(IntPtr state, int addedItemIndex); public delegate void OnNoRightParenAddedCallbackFunc(IntPtr state); - public delegate void SetExpressionDisplayCallbackFunc(IntPtr state); + public delegate void SetExpressionDisplayCallbackFunc(IntPtr state, IntPtr data); public delegate void SetMemorizedNumbersCallbackFunc(IntPtr state, int count, IntPtr newMemorizedNumbers); public static GetCEngineStringFunc _getCEngineStringCallback = GetCEngineStringCallback; @@ -154,10 +154,14 @@ namespace CalculationManager Debug.WriteLine($"CalculatorManager.OnNoRightParenAddedCallback"); } - public static void SetExpressionDisplayCallback(IntPtr state) + public static void SetExpressionDisplayCallback(IntPtr state, IntPtr historyItem) { var manager = GCHandle.FromIntPtr((IntPtr)state).Target as CalculatorDisplay; - // manager.SetExpressionDisplay(); + + var nativeResult = Marshal.PtrToStructure(historyItem); + var itemResult = CalculatorManager.UnmarshalHistoryItemResult(nativeResult); + + manager.SetExpressionDisplay(itemResult.historyItemVector.spTokens, itemResult.historyItemVector.spCommands); Debug.WriteLine($"CalculatorManager.SetExpressionDisplayCallback"); } diff --git a/src/Calculator.Shared/CalcManager/CalculatorManager.cs b/src/Calculator.Shared/CalcManager/CalculatorManager.cs index f76b1ad2..a3937fae 100644 --- a/src/Calculator.Shared/CalcManager/CalculatorManager.cs +++ b/src/Calculator.Shared/CalcManager/CalculatorManager.cs @@ -248,7 +248,7 @@ namespace CalculationManager return output; } - private static HISTORYITEM UnmarshalHistoryItemResult(GetHistoryItemResult historyResultItem) + internal static HISTORYITEM UnmarshalHistoryItemResult(GetHistoryItemResult historyResultItem) { var historyItem = new HISTORYITEM(); historyItem.historyItemVector.expression = historyResultItem.expression; diff --git a/src/Calculator.Shared/CalcManager/ExpressionCommandInterface.cs b/src/Calculator.Shared/CalcManager/ExpressionCommandInterface.cs index fd052726..6a9eaae9 100644 --- a/src/Calculator.Shared/CalcManager/ExpressionCommandInterface.cs +++ b/src/Calculator.Shared/CalcManager/ExpressionCommandInterface.cs @@ -67,8 +67,8 @@ namespace CalculationManager public CParentheses(IntPtr pExpressionCommand) => this.pExpressionCommand = pExpressionCommand; public int GetCommand() => throw new NotImplementedException(); - public CalculationManager.CommandType GetCommandType() => throw new NotImplementedException(); - public void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException(); + public CalculationManager.CommandType GetCommandType() => CommandType.Parentheses; + public void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException(); } public class CUnaryCommand : IUnaryCommand @@ -80,8 +80,8 @@ namespace CalculationManager public CUnaryCommand(int command) => throw new NotImplementedException(); public CUnaryCommand(int command1, int command2) => throw new NotImplementedException(); public CalculatorList GetCommands() => throw new NotImplementedException(); - public CalculationManager.CommandType GetCommandType() => throw new NotImplementedException(); - public void SetCommand(int command) => throw new NotImplementedException(); + public CalculationManager.CommandType GetCommandType() => CommandType.UnaryCommand; + public void SetCommand(int command) => throw new NotImplementedException(); public void SetCommands(int command1, int command2) => throw new NotImplementedException(); public void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException(); } @@ -95,7 +95,7 @@ namespace CalculationManager public CBinaryCommand(int command) => throw new NotImplementedException(); public void SetCommand(int command) => throw new NotImplementedException(); public int GetCommand() => throw new NotImplementedException(); - public CommandType GetCommandType() => throw new NotImplementedException(); + public CalculationManager.CommandType GetCommandType() => CommandType.BinaryCommand; public void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException(); } @@ -117,7 +117,7 @@ namespace CalculationManager public bool IsSciFmt() => throw new NotImplementedException(); public bool IsDecimalPresent() => throw new NotImplementedException(); public string GetToken(char decimalSymbol) => throw new NotImplementedException(); - public CalculationManager.CommandType GetCommandType() => throw new NotImplementedException(); + public CalculationManager.CommandType GetCommandType() => CommandType.OperandCommand; public void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException(); public string GetString(uint radix, int precision) => throw new NotImplementedException(); } diff --git a/src/Calculator.Shared/Converters/ExpressionItemTemplateSelector.cs b/src/Calculator.Shared/Converters/ExpressionItemTemplateSelector.cs index c1e56077..6e061742 100644 --- a/src/Calculator.Shared/Converters/ExpressionItemTemplateSelector.cs +++ b/src/Calculator.Shared/Converters/ExpressionItemTemplateSelector.cs @@ -11,7 +11,7 @@ namespace CalculatorApp [Windows.UI.Xaml.Data.Bindable] public sealed class ExpressionItemTemplateSelector : Windows.UI.Xaml.Controls.DataTemplateSelector { - Windows.UI.Xaml.DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container) + protected override Windows.UI.Xaml.DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container) { DisplayExpressionToken token = (DisplayExpressionToken)(item); if (token != null) diff --git a/src/Calculator.Shared/ViewModels/StandardCalculatorViewModel.cs b/src/Calculator.Shared/ViewModels/StandardCalculatorViewModel.cs index 92b64cc2..2e27efba 100644 --- a/src/Calculator.Shared/ViewModels/StandardCalculatorViewModel.cs +++ b/src/Calculator.Shared/ViewModels/StandardCalculatorViewModel.cs @@ -830,7 +830,7 @@ namespace CalculatorApp.ViewModel else { var expressionToken = new DisplayExpressionToken(currentTokenString, i, isEditable, type); - m_ExpressionTokens.Append(expressionToken); + m_ExpressionTokens.Add(expressionToken); } } } diff --git a/src/Calculator.Shared/Views/Calculator.xaml b/src/Calculator.Shared/Views/Calculator.xaml index 4e5f78a7..c86ca1a7 100644 --- a/src/Calculator.Shared/Views/Calculator.xaml +++ b/src/Calculator.Shared/Views/Calculator.xaml @@ -969,7 +969,7 @@ IsOperatorCommand="{x:Bind Model.IsOperatorCommand, Mode=OneWay}" TabIndex="1" />