Add support for SetExpressionDisplay

This commit is contained in:
Jérôme Laban 2019-05-16 16:39:17 -04:00
commit 799cc4916b
8 changed files with 111 additions and 105 deletions

View file

@ -75,11 +75,20 @@ public:
} }
virtual void SetExpressionDisplay( virtual void SetExpressionDisplay(
std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& /*tokens*/, std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& tokens,
std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& /*commands*/) override std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& commands) override
{ {
printf("Native:SetExpressionDisplay()\n"); printf("Native:SetExpressionDisplay()\n");
auto item = std::make_shared<HISTORYITEM>();
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 virtual void SetParenthesisNumber(unsigned int count) override
@ -182,6 +191,83 @@ const wchar_t* ToWChar(std::wstring& str)
return out; return out;
} }
GetHistoryItemResult* MarshalHistoryItem(std::shared_ptr<CalculationManager::HISTORYITEM>& 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<std::wstring, int> 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<IExpressionCommand> command;
if (SUCCEEDED(historyItem->historyItemVector.spCommands->GetAt(commandId, &command)))
{
commands[commandId] = command.get();
}
}
itemResult->Commands = commands;
return itemResult;
}
void* MarshalHistoryItems(std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>>& 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) void* CalculatorManager_Create(CalculatorManager_CreateParams* pParams)
{ {
auto calcDisplay = new CalcDisplay(*pParams); auto calcDisplay = new CalcDisplay(*pParams);
@ -316,82 +402,6 @@ void CalculatorManager_SetInHistoryItemLoadMode(void* manager, bool isHistoryIte
AsManager(manager)->SetInHistoryItemLoadMode(isHistoryItemLoadMode); AsManager(manager)->SetInHistoryItemLoadMode(isHistoryItemLoadMode);
} }
GetHistoryItemResult* MarshalHistoryItem(std::shared_ptr<CalculationManager::HISTORYITEM>& 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<std::wstring, int> 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<IExpressionCommand> command;
if (SUCCEEDED(historyItem->historyItemVector.spCommands->GetAt(commandId, &command)))
{
commands[commandId] = command.get();
}
}
itemResult->Commands = commands;
return itemResult;
}
void* MarshalHistoryItems(std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>>& 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) void* CalculatorManager_GetHistoryItems(void* manager)
{ {
auto historyItems = AsManager(manager)->GetHistoryItems(); auto historyItems = AsManager(manager)->GetHistoryItems();

View file

@ -6,23 +6,12 @@
#include "headers/Rational.h" #include "headers/Rational.h"
#include "headers/ICalcDisplay.h" #include "headers/ICalcDisplay.h"
struct GetHistoryItemResult;
struct TokenPair { struct GetHistoryItemsResult;
char* Item1;
int Item2;
};
struct ExpressionDisplayData {
int TokenCount;
TokenPair* Tokens;
int CommandCount;
void* Commands;
};
typedef void (*SetPrimaryDisplayFunc)(void* state, const char* text, bool isError); typedef void (*SetPrimaryDisplayFunc)(void* state, const char* text, bool isError);
typedef void (*SetIsInErrorFunc)(void* state, bool isInError); 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 (*SetParenthesisNumberFunc)(void* state, unsigned int count);
typedef void (*OnNoRightParenAddedFunc)(void* state); typedef void (*OnNoRightParenAddedFunc)(void* state);
typedef void (*MaxDigitsReachedFunc)(void* state); typedef void (*MaxDigitsReachedFunc)(void* state);
@ -57,6 +46,9 @@ struct CalculatorManager_CreateParams {
#define DLL_EXPORT __declspec(dllexport) #define DLL_EXPORT __declspec(dllexport)
#endif #endif
GetHistoryItemResult* MarshalHistoryItem(std::shared_ptr<CalculationManager::HISTORYITEM>& historyItem);
void* MarshalHistoryItems(std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>>& historyItems);
extern "C" { extern "C" {
struct GetHistoryItemsResult struct GetHistoryItemsResult

View file

@ -106,7 +106,7 @@ namespace CalculationManager
public delegate void MemoryItemChangedCallbackFunc(IntPtr state, int indexOfMemory); public delegate void MemoryItemChangedCallbackFunc(IntPtr state, int indexOfMemory);
public delegate void OnHistoryItemAddedCallbackFunc(IntPtr state, int addedItemIndex); public delegate void OnHistoryItemAddedCallbackFunc(IntPtr state, int addedItemIndex);
public delegate void OnNoRightParenAddedCallbackFunc(IntPtr state); 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 delegate void SetMemorizedNumbersCallbackFunc(IntPtr state, int count, IntPtr newMemorizedNumbers);
public static GetCEngineStringFunc _getCEngineStringCallback = GetCEngineStringCallback; public static GetCEngineStringFunc _getCEngineStringCallback = GetCEngineStringCallback;
@ -154,10 +154,14 @@ namespace CalculationManager
Debug.WriteLine($"CalculatorManager.OnNoRightParenAddedCallback"); 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; var manager = GCHandle.FromIntPtr((IntPtr)state).Target as CalculatorDisplay;
// manager.SetExpressionDisplay();
var nativeResult = Marshal.PtrToStructure<GetHistoryItemResult>(historyItem);
var itemResult = CalculatorManager.UnmarshalHistoryItemResult(nativeResult);
manager.SetExpressionDisplay(itemResult.historyItemVector.spTokens, itemResult.historyItemVector.spCommands);
Debug.WriteLine($"CalculatorManager.SetExpressionDisplayCallback"); Debug.WriteLine($"CalculatorManager.SetExpressionDisplayCallback");
} }

View file

@ -248,7 +248,7 @@ namespace CalculationManager
return output; return output;
} }
private static HISTORYITEM UnmarshalHistoryItemResult(GetHistoryItemResult historyResultItem) internal static HISTORYITEM UnmarshalHistoryItemResult(GetHistoryItemResult historyResultItem)
{ {
var historyItem = new HISTORYITEM(); var historyItem = new HISTORYITEM();
historyItem.historyItemVector.expression = historyResultItem.expression; historyItem.historyItemVector.expression = historyResultItem.expression;

View file

@ -67,8 +67,8 @@ namespace CalculationManager
public CParentheses(IntPtr pExpressionCommand) => this.pExpressionCommand = pExpressionCommand; public CParentheses(IntPtr pExpressionCommand) => this.pExpressionCommand = pExpressionCommand;
public int GetCommand() => throw new NotImplementedException(); public int GetCommand() => throw new NotImplementedException();
public CalculationManager.CommandType GetCommandType() => throw new NotImplementedException(); public CalculationManager.CommandType GetCommandType() => CommandType.Parentheses;
public void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException(); public void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException();
} }
public class CUnaryCommand : IUnaryCommand public class CUnaryCommand : IUnaryCommand
@ -80,8 +80,8 @@ namespace CalculationManager
public CUnaryCommand(int command) => throw new NotImplementedException(); public CUnaryCommand(int command) => throw new NotImplementedException();
public CUnaryCommand(int command1, int command2) => throw new NotImplementedException(); public CUnaryCommand(int command1, int command2) => throw new NotImplementedException();
public CalculatorList<int> GetCommands() => throw new NotImplementedException(); public CalculatorList<int> GetCommands() => throw new NotImplementedException();
public CalculationManager.CommandType GetCommandType() => throw new NotImplementedException(); public CalculationManager.CommandType GetCommandType() => CommandType.UnaryCommand;
public void SetCommand(int command) => throw new NotImplementedException(); public void SetCommand(int command) => throw new NotImplementedException();
public void SetCommands(int command1, int command2) => throw new NotImplementedException(); public void SetCommands(int command1, int command2) => throw new NotImplementedException();
public void Accept(ISerializeCommandVisitor commandVisitor) => 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 CBinaryCommand(int command) => throw new NotImplementedException();
public void SetCommand(int command) => throw new NotImplementedException(); public void SetCommand(int command) => throw new NotImplementedException();
public int GetCommand() => 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(); public void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException();
} }
@ -117,7 +117,7 @@ namespace CalculationManager
public bool IsSciFmt() => throw new NotImplementedException(); public bool IsSciFmt() => throw new NotImplementedException();
public bool IsDecimalPresent() => throw new NotImplementedException(); public bool IsDecimalPresent() => throw new NotImplementedException();
public string GetToken(char decimalSymbol) => 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 void Accept(ISerializeCommandVisitor commandVisitor) => throw new NotImplementedException();
public string GetString(uint radix, int precision) => throw new NotImplementedException(); public string GetString(uint radix, int precision) => throw new NotImplementedException();
} }

View file

@ -11,7 +11,7 @@ namespace CalculatorApp
[Windows.UI.Xaml.Data.Bindable] [Windows.UI.Xaml.Data.Bindable]
public sealed class ExpressionItemTemplateSelector : Windows.UI.Xaml.Controls.DataTemplateSelector 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); DisplayExpressionToken token = (DisplayExpressionToken)(item);
if (token != null) if (token != null)

View file

@ -830,7 +830,7 @@ namespace CalculatorApp.ViewModel
else else
{ {
var expressionToken = new DisplayExpressionToken(currentTokenString, i, isEditable, type); var expressionToken = new DisplayExpressionToken(currentTokenString, i, isEditable, type);
m_ExpressionTokens.Append(expressionToken); m_ExpressionTokens.Add(expressionToken);
} }
} }
} }

View file

@ -969,7 +969,7 @@
IsOperatorCommand="{x:Bind Model.IsOperatorCommand, Mode=OneWay}" IsOperatorCommand="{x:Bind Model.IsOperatorCommand, Mode=OneWay}"
TabIndex="1" /> TabIndex="1" />
<controls:OverflowTextBlock x:Name="expressionText" <controls:OverflowTextBlock x:Name="expressionText"
xamarin:Style="{StaticResource overflowTextBlockStyle}" Style="{StaticResource overflowTextBlockStyle}"
Grid.Row="1" Grid.Row="1"
Margin="6,0,6,0" Margin="6,0,6,0"
VerticalAlignment="Bottom" VerticalAlignment="Bottom"