diff --git a/apps/code/app.cpp b/apps/code/app.cpp index 2e7c4fd18..8eeeea845 100644 --- a/apps/code/app.cpp +++ b/apps/code/app.cpp @@ -18,7 +18,7 @@ const Image * App::Descriptor::icon() { } App::Snapshot::Snapshot() { - m_scriptStore.addMandelbrotScript(); + m_scriptStore.addFactorialScript(); } App * App::Snapshot::unpack(Container * container) { diff --git a/apps/code/menu_controller.cpp b/apps/code/menu_controller.cpp index beb669b58..b58cff649 100644 --- a/apps/code/menu_controller.cpp +++ b/apps/code/menu_controller.cpp @@ -1,6 +1,7 @@ #include "menu_controller.h" #include "../i18n.h" #include +#include namespace Code { @@ -17,9 +18,14 @@ MenuController::MenuController(Responder * parentResponder, ScriptStore * script m_consoleController(parentResponder, m_scriptStore), m_scriptParameterController(nullptr, I18n::Message::ScriptOptions, m_scriptStore, this) { - for (int i = 0; i< k_maxNumberOfCells; i++) { - m_cells[i].setMessageFontSize(KDText::FontSize::Large); + for (int i = 0; i < k_maxNumberOfDisplayableScriptCells; i++) { + m_scriptCells[i].setParentResponder(&m_selectableTableView); + m_scriptCells[i].editableTextCell()->textField()->setDelegate(this); + m_scriptCells[i].editableTextCell()->textField()->setDraftTextBuffer(m_draftTextBuffer); + m_scriptCells[i].editableTextCell()->textField()->setAlignment(0.0f, 0.5f); + m_scriptCells[i].editableTextCell()->setMargins(0, 0, 0, Metric::HistoryHorizontalMargin); } + m_selectableTableView.selectCellAtLocation(0, 0); } ConsoleController * MenuController::consoleController() { @@ -31,7 +37,6 @@ View * MenuController::view() { } void MenuController::didBecomeFirstResponder() { - m_selectableTableView.selectCellAtLocation(0, 0); app()->setFirstResponder(&m_selectableTableView); } @@ -41,7 +46,7 @@ bool MenuController::handleEvent(Ion::Events::Event event) { footer()->setSelectedButton(0); return true; } else if (event == Ion::Events::Up) { - if (m_selectableTableView.selectedRow()<0) { + if (m_selectableTableView.selectedRow() < 0) { footer()->setSelectedButton(-1); m_selectableTableView.selectCellAtLocation(0, numberOfRows()-1); app()->setFirstResponder(&m_selectableTableView); @@ -73,6 +78,16 @@ void MenuController::addScript() { m_selectableTableView.selectCellAtLocation(0, numberOfRows()-2); } +void MenuController::renameScriptAtIndex(int i) { + assert(i>=0 && inumberOfScripts()); + EvenOddEditableTextCell * myCell = static_cast(m_selectableTableView.selectedCell()); + myCell->setHighlighted(false); + const char * previousText = myCell->editableTextCell()->textField()->text(); + myCell->editableTextCell()->textField()->setEditing(true); + myCell->editableTextCell()->textField()->setText(previousText); + app()->setFirstResponder(myCell); +} + void MenuController::deleteScriptAtIndex(int i) { m_scriptStore->deleteScript(i); m_selectableTableView.reloadData(); @@ -87,27 +102,99 @@ KDCoordinate MenuController::cellHeight() { return k_rowHeight; } -HighlightCell * MenuController::reusableCell(int index) { - assert(index >= 0); - if (index < m_scriptStore->numberOfScripts()) { - return &m_cells[index]; - } - assert(index == m_scriptStore->numberOfScripts()); - return &m_addNewScriptCell; +KDCoordinate MenuController::rowHeight(int j) { + return cellHeight(); } -int MenuController::reusableCellCount() { - return m_scriptStore->numberOfScripts() + 1; +KDCoordinate MenuController::cumulatedHeightFromIndex(int j) { + return cellHeight() * j; +} + +int MenuController::indexFromCumulatedHeight(KDCoordinate offsetY) { + KDCoordinate height = cellHeight(); + if (height == 0) { + return 0; + } + return (offsetY - 1) / height; +} + +HighlightCell * MenuController::reusableCell(int index, int type) { + assert(index >= 0); + if (type == ScriptCellType) { + assert(index >=0 && index < k_maxNumberOfDisplayableScriptCells); + return &m_scriptCells[index]; + } else { + assert(type == AddScriptCellType && index == 0); + return &m_addNewScriptCell; + } +} + +int MenuController::reusableCellCount(int type) { + if (type == AddScriptCellType) { + return 1; + } + assert(type == ScriptCellType); + return k_maxNumberOfDisplayableScriptCells; +} + +int MenuController::typeAtLocation(int i, int j) { + assert(i==0); + assert(j>=0 && jnumberOfScripts()) { - MessageTableCell * myCell = (MessageTableCell *)cell; - // TODO: store script names - myCell->setMessage(I18n::Message::Console); + EvenOddEditableTextCell * myCell = static_cast(cell); + myCell->editableTextCell()->textField()->setText(m_scriptStore->nameOfScript(index)); + myCell->setEven(index%2 == 0); + } else { + assert(index == m_scriptStore->numberOfScripts()); + Shared::NewFunctionCell * myCell = static_cast(cell); + myCell->setEven(index%2 == 0); } } +bool MenuController::textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) { + return event == Ion::Events::OK || event == Ion::Events::EXE + || event == Ion::Events::Down || event == Ion::Events::Up; +} + +bool MenuController::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) { + return false; +} + +bool MenuController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) { + if (m_scriptStore->renameScript(m_selectableTableView.selectedRow(), text)) { + int currentRow = m_selectableTableView.selectedRow(); + if (event == Ion::Events::Down && currentRow < numberOfRows() - 1) { + m_selectableTableView.selectCellAtLocation(m_selectableTableView.selectedColumn(), currentRow + 1); + } else if (event == Ion::Events::Up && currentRow > 0) { + m_selectableTableView.selectCellAtLocation(m_selectableTableView.selectedColumn(), currentRow - 1); + } + m_selectableTableView.selectedCell()->setHighlighted(true); + app()->setFirstResponder(&m_selectableTableView); + return true; + } else { + // TODO: add pop up to explain to the user that the name is too long. + return false; + } +} + +bool MenuController::textFieldDidAbortEditing(TextField * textField, const char * text) { + m_selectableTableView.selectCellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()); + app()->setFirstResponder(&m_selectableTableView); + return true; +} + +Toolbox * MenuController::toolboxForTextField(TextField * textFied) { + return nullptr; +} + int MenuController::numberOfButtons(ButtonRowController::Position position) const { return 1; } diff --git a/apps/code/menu_controller.h b/apps/code/menu_controller.h index 2464209d7..03d0e8055 100644 --- a/apps/code/menu_controller.h +++ b/apps/code/menu_controller.h @@ -11,13 +11,15 @@ namespace Code { class ScriptParameterController; -class MenuController : public ViewController, public SimpleListViewDataSource, public SelectableTableViewDataSource, public ButtonRowDelegate { +class MenuController : public ViewController, public ListViewDataSource, public SelectableTableViewDataSource, public TextFieldDelegate, public ButtonRowDelegate { public: MenuController(Responder * parentResponder, ScriptStore * scriptStore, ButtonRowController * footer); + ConsoleController * consoleController(); StackViewController * stackViewController(); void configureScript(); void addScript(); + void renameScriptAtIndex(int i); void deleteScriptAtIndex(int i); /* ViewController */ @@ -25,21 +27,39 @@ public: bool handleEvent(Ion::Events::Event event) override; void didBecomeFirstResponder() override; - /* SimpleListViewDataSource */ + /* ListViewDataSource */ int numberOfRows() override; - KDCoordinate cellHeight() override; - HighlightCell * reusableCell(int index) override; - int reusableCellCount() override; + KDCoordinate cellHeight(); + KDCoordinate rowHeight(int j) override; + KDCoordinate cumulatedHeightFromIndex(int j) override; + int indexFromCumulatedHeight(KDCoordinate offsetY) override; + HighlightCell * reusableCell(int index, int type) override; + int reusableCellCount(int type) override; + int typeAtLocation(int i, int j) override; void willDisplayCellForIndex(HighlightCell * cell, int index) override; + /* TextFieldDelegate */ + bool textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) override; + bool textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) override; + bool textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) override; + bool textFieldDidAbortEditing(TextField * textField, const char * text) override; + Toolbox * toolboxForTextField(TextField * textFied) override; + /* ButtonRowDelegate */ int numberOfButtons(ButtonRowController::Position position) const override; Button * buttonAtIndex(int index, ButtonRowController::Position position) const override; + private: - constexpr static int k_maxNumberOfCells = 5; //TODO + static constexpr int k_maxNumberOfDisplayableScriptCells = 7; //TODO + static constexpr int AddScriptCellType = 0; + static constexpr int ScriptCellType = 1; static constexpr KDCoordinate k_rowHeight = 50; //TODO create common parent class with Shared::ListController ScriptStore * m_scriptStore; - MessageTableCell m_cells[k_maxNumberOfCells]; + EvenOddEditableTextCell m_scriptCells[k_maxNumberOfDisplayableScriptCells]; + /* In the initializer list of the MenuController constructor, we initialize + * m_scriptCells by copying k_maxNumberOfDisplayableScriptCells times the + * constructor of an EvenOddEditableTextCell. */ + char m_draftTextBuffer[TextField::maxBufferSize()]; Shared::NewFunctionCell m_addNewScriptCell; Button m_consoleButton; SelectableTableView m_selectableTableView; diff --git a/apps/code/script_parameter_controller.cpp b/apps/code/script_parameter_controller.cpp index ac5541933..1e7ae7d80 100644 --- a/apps/code/script_parameter_controller.cpp +++ b/apps/code/script_parameter_controller.cpp @@ -38,14 +38,21 @@ const char * ScriptParameterController::title() { bool ScriptParameterController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::OK || event == Ion::Events::EXE) { + int i = m_currentScriptIndex; switch (selectedRow()) { case 0: app()->displayModalViewController(&m_editorController, 0.5f, 0.5f); return true; - //TODO other cases - case 3: - m_menuController->deleteScriptAtIndex(m_currentScriptIndex); + case 1: dismissScriptParameterController(); + m_menuController->renameScriptAtIndex(i); + return true; + case 2: + //Auto-import TODO + return true; + case 3: + dismissScriptParameterController(); + m_menuController->deleteScriptAtIndex(i); return true; default: assert(false); diff --git a/apps/code/script_store.cpp b/apps/code/script_store.cpp index 6080071b3..fe9aa89b1 100644 --- a/apps/code/script_store.cpp +++ b/apps/code/script_store.cpp @@ -4,6 +4,8 @@ namespace Code { +constexpr char ScriptStore::k_defaultScriptName[]; + ScriptStore::ScriptStore() : m_numberOfScripts(0), m_lastEditedStringPosition(0) @@ -31,11 +33,13 @@ Script ScriptStore::editableScript(int i) { Script ScriptStore::script(int i) { assert(i >= 0 && i < numberOfScripts()); + cleanFreeSpace(); int beginningOfScriptContent = indexOfScriptContent(i); return Script(&m_history[beginningOfScriptContent], strlen(&m_history[beginningOfScriptContent]) + 1); } Script ScriptStore::script(const char * name) { + cleanFreeSpace(); for (int i=0; i= 0 && i < numberOfScripts()); cleanAndMoveFreeSpaceAfterScriptName(i); - return &m_history[indexOfScript(i)+1]; + return &m_history[indexOfScriptName(i)]; } int ScriptStore::sizeOfEditableNameOfScript(int i) { - int namePosition = indexOfScript(i)+1; - int contentPosition = indexOfScriptContent(i); - return contentPosition-namePosition; + // Compute the size of the name of the script, including the free space of m_history + int sizeOfEditableNameScript = 0; + for (int j=indexOfScriptName(i); j= 0 && i < numberOfScripts()); + cleanAndMoveFreeSpaceAfterScriptName(i); + if (strlen(newName) <= sizeOfEditableNameOfScript(i)) { + copyName(indexOfScriptName(i), newName); + return true; + } + return false; +} + void ScriptStore::deleteScript(int i) { assert (i >= 0 && i < numberOfScripts()); cleanAndMoveFreeSpaceAfterScriptContent(i); int indexOfCharToDelete = indexOfScript(i); - while (m_history[indexOfCharToDelete] != FreeSpaceMarker) { + while (m_history[indexOfCharToDelete] != FreeSpaceMarker && indexOfCharToDelete < k_historySize) { m_history[indexOfCharToDelete] = FreeSpaceMarker; indexOfCharToDelete++; } @@ -127,10 +173,6 @@ const char * ScriptStore::contentOfScript(const char * name) { return script(filename).readOnlyContent(); } -char * ScriptStore::nameOfScript(int i) { - return &m_history[indexOfScript(i)+1]; -} - bool ScriptStore::copyName(int position, const char * name) { if (name) { int len = strlen(name); @@ -140,31 +182,21 @@ bool ScriptStore::copyName(int position, const char * name) { memcpy(&m_history[position], name, len+1); return true; } else { - const char newName[] = "script.py"; - int len = strlen(newName); + int len = strlen(k_defaultScriptName); if (len > sizeOfFreeSpace() - 1) { // We keep at keast one free char. return false; } - memcpy(&m_history[position], &newName, len+1); + memcpy(&m_history[position], k_defaultScriptName, len+1); return true; } } -int ScriptStore::indexOfScriptContent(int i) const { - int indexOfScriptContent = indexOfScript(i)+1; - while (m_history[indexOfScriptContent] != 0 ) { - indexOfScriptContent++; - } - indexOfScriptContent++; - return indexOfScriptContent; -} - int ScriptStore::indexOfScript(int i) const { assert (i >= 0 && i < numberOfScripts()); int currentScriptNumber = 0; int beginningOfScript = 0; - while (m_history[beginningOfScript] == FreeSpaceMarker) { + while (m_history[beginningOfScript] == FreeSpaceMarker && beginningOfScript < k_historySize) { beginningOfScript++; } if (i == 0) { @@ -180,7 +212,7 @@ int ScriptStore::indexOfScript(int i) const { currentScriptNumber++; if (currentScriptNumber == i) { j++; - while (m_history[j] == FreeSpaceMarker) { + while (m_history[j] == FreeSpaceMarker && j < beginningOfScript) { j++; } return j; @@ -192,7 +224,24 @@ int ScriptStore::indexOfScript(int i) const { return 0; } +int ScriptStore::indexOfScriptName(int i) const { + assert (i >= 0 && i < numberOfScripts()); + return indexOfScript(i)+1; +} + + +int ScriptStore::indexOfScriptContent(int i) const { + assert (i >= 0 && i < numberOfScripts()); + int indexOfScriptContent = indexOfScriptName(i); + while (m_history[indexOfScriptContent] != 0 && indexOfScriptContent= 0 && i < numberOfScripts()); int indexOfPrgm = indexOfScriptContent(i); int lastIndexOfScript = indexOfPrgm + strlen(&m_history[indexOfPrgm]); return lastIndexOfScript; @@ -231,13 +280,16 @@ void ScriptStore::cleanFreeSpace() { } int indexOfCharToChangeIntoFreeSpaceMarker = m_lastEditedStringPosition + strlen (&m_history[m_lastEditedStringPosition]) + 1; - while (m_history[indexOfCharToChangeIntoFreeSpaceMarker] != FreeSpaceMarker) { + while (m_history[indexOfCharToChangeIntoFreeSpaceMarker] != FreeSpaceMarker + && indexOfCharToChangeIntoFreeSpaceMarker= 0 && i < numberOfScripts()); int indexOfFreeSpace = indexOfFirstFreeSpaceMarker(); int newFreeSpacePosition = lastIndexOfScript(i) + 1; if (indexOfFreeSpace < newFreeSpacePosition) { @@ -247,8 +299,9 @@ void ScriptStore::moveFreeSpaceAfterScriptContent(int i) { } void ScriptStore::moveFreeSpaceAfterScriptName(int i) { - int newFreeSpacePosition = indexOfScript(i); - while (m_history[newFreeSpacePosition] != 0) { + assert (i >= 0 && i < numberOfScripts()); + int newFreeSpacePosition = indexOfScriptName(i); + while (m_history[newFreeSpacePosition] != 0 && newFreeSpacePosition < k_historySize) { newFreeSpacePosition++; } newFreeSpacePosition++; @@ -256,6 +309,7 @@ void ScriptStore::moveFreeSpaceAfterScriptName(int i) { } void ScriptStore::moveFreeSpaceAtPosition(int i) { + assert (i >= 0 && i < k_historySize); int indexOfFreeSpace = indexOfFirstFreeSpaceMarker(); if (indexOfFreeSpace != i){ @@ -281,13 +335,14 @@ void ScriptStore::moveFreeSpaceAtPosition(int i) { } } - m_lastEditedStringPosition = i-2; - while (m_lastEditedStringPosition > 1 && m_history[m_lastEditedStringPosition-1] != 0) { + m_lastEditedStringPosition = i-1; + while (m_lastEditedStringPosition > 0 + && m_history[m_lastEditedStringPosition-1] != 0 + && m_history[m_lastEditedStringPosition-1] != AutoImportationMarker + && m_history[m_lastEditedStringPosition-1] != NoAutoImportationMarker) + { m_lastEditedStringPosition--; } - if (m_lastEditedStringPosition <= 1) { - m_lastEditedStringPosition = 0; - } } void ScriptStore::cleanAndMoveFreeSpaceAfterScriptContent(int i) { @@ -336,7 +391,7 @@ for x in range(320): return true; } -bool ScriptStore::copyDefaultScriptOnFreeSpace() { +bool ScriptStore::copyFactorialScriptOnFreeSpace() { const char script[] = R"(def factorial(n): if n == 0: return 1 @@ -352,12 +407,23 @@ bool ScriptStore::copyDefaultScriptOnFreeSpace() { return true; } +bool ScriptStore::copyEmptyScriptOnFreeSpace() { + const char script[] = "\0"; + + int len = 1; + if (len > sizeOfFreeSpace() - 1) { // We keep at keast one free char. + return false; + } + memcpy(&m_history[indexOfFirstFreeSpaceMarker()], script, len); + return true; +} + int ScriptStore::indexBeginningFilename(const char * path) { - int beginningFilename = strlen(path)-1; - while (beginningFilename > 0 && path[beginningFilename - 1] != '\\') { - beginningFilename--; - } - return beginningFilename; + int beginningFilename = strlen(path)-1; + while (beginningFilename > 0 && path[beginningFilename - 1] != '\\') { + beginningFilename--; + } + return beginningFilename; } } diff --git a/apps/code/script_store.h b/apps/code/script_store.h index b1a2ebb91..22d3ec9ef 100644 --- a/apps/code/script_store.h +++ b/apps/code/script_store.h @@ -20,11 +20,14 @@ public: Script script(const char * name); /* script(const char * name) looks for a script that has the right name and * returns it. If there is no such script, it returns an empty Script. */ + char * nameOfScript(int i); char * editableNameOfScript(int i); int sizeOfEditableNameOfScript(int i); int numberOfScripts() const; bool addNewScript(); bool addMandelbrotScript(); + bool addFactorialScript(); + bool renameScript(int i, const char * newName); void deleteScript(int i); void deleteAll(); @@ -37,10 +40,11 @@ private: static constexpr char NoAutoImportationMarker = 0x03; /* We made sure that these chars are not used in ion/include/ion/charset.h */ static constexpr int k_historySize = 1024; - char * nameOfScript(int i); + static constexpr char k_defaultScriptName[] = ".py"; bool copyName(int position, const char * name = nullptr); - int indexOfScriptContent(int i) const; int indexOfScript(int i) const; + int indexOfScriptName(int i) const; + int indexOfScriptContent(int i) const; int lastIndexOfScript(int i) const; int indexOfFirstFreeSpaceMarker() const; int sizeOfFreeSpace() const; @@ -51,7 +55,8 @@ private: void cleanAndMoveFreeSpaceAfterScriptContent(int i); void cleanAndMoveFreeSpaceAfterScriptName(int i); bool copyMandelbrotScriptOnFreeSpace(); - bool copyDefaultScriptOnFreeSpace(); + bool copyFactorialScriptOnFreeSpace(); + bool copyEmptyScriptOnFreeSpace(); int indexBeginningFilename(const char * path); char m_history[k_historySize]; /* The m_history variable sequentially stores scripts as text buffers. diff --git a/apps/probability/calculation_controller.cpp b/apps/probability/calculation_controller.cpp index 81b3bca7f..c0fbbcc8f 100644 --- a/apps/probability/calculation_controller.cpp +++ b/apps/probability/calculation_controller.cpp @@ -23,8 +23,8 @@ CalculationController::ContentView::ContentView(Responder * parentResponder, Cal { for (int i = 0; i < k_maxNumberOfEditableFields; i++) { m_calculationCell[i].setParentResponder(parentResponder); - m_calculationCell[i].setTextFieldDelegate(calculationController); - m_calculationCell[i].setTextFieldDraftTextBuffer(m_draftTextBuffer); + m_calculationCell[i].textField()->setDelegate(calculationController); + m_calculationCell[i].textField()->setDraftTextBuffer(m_draftTextBuffer); } } @@ -67,7 +67,7 @@ View * CalculationController::ContentView::subviewAtIndex(int index) { void CalculationController::ContentView::willDisplayEditableCellAtIndex(int index) { char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)]; Complex::convertFloatToText(m_calculation->parameterAtIndex(index), buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits), Constant::ShortNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal); - m_calculationCell[index].setText(buffer); + m_calculationCell[index].textField()->setText(buffer); } void CalculationController::ContentView::layoutSubviews() { diff --git a/apps/shared/editable_cell_table_view_controller.cpp b/apps/shared/editable_cell_table_view_controller.cpp index dd38e5efc..57e6e01d0 100644 --- a/apps/shared/editable_cell_table_view_controller.cpp +++ b/apps/shared/editable_cell_table_view_controller.cpp @@ -47,7 +47,7 @@ void EditableCellTableViewController::tableViewDidChangeSelection(SelectableTabl } if (cellAtLocationIsEditable(previousSelectedCellX, previousSelectedCellY)) { EvenOddEditableTextCell * myCell = (EvenOddEditableTextCell *)t->cellAtLocation(previousSelectedCellX, previousSelectedCellY); - myCell->setEditing(false); + myCell->editableTextCell()->textField()->setEditing(false); if (app()->firstResponder() == myCell->editableTextCell()->textField()) { app()->setFirstResponder(t); } @@ -84,17 +84,17 @@ void EditableCellTableViewController::willDisplayCellAtLocationWithDisplayMode(H if (j == numberOfRows() - 1) { /* Display an empty line only if there is enough space for a new element in * data */ - if (numberOfElements() < maxNumberOfElements() && !myEditableValueCell->isEditing()) { + if (numberOfElements() < maxNumberOfElements() && !myEditableValueCell->editableTextCell()->textField()->isEditing()) { myCell->setEven(j%2 == 0); buffer[0] = 0; - myEditableValueCell->setText(buffer); + myEditableValueCell->editableTextCell()->textField()->setText(buffer); return; } } - if (!myEditableValueCell->isEditing()) { + if (!myEditableValueCell->editableTextCell()->textField()->isEditing()) { myCell->setEven(j%2 == 0); Complex::convertFloatToText(dataAtLocation(i, j), buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, floatDisplayMode); - myEditableValueCell->setText(buffer); + myEditableValueCell->editableTextCell()->textField()->setText(buffer); } return; } else { diff --git a/escher/include/escher/editable_text_cell.h b/escher/include/escher/editable_text_cell.h index 0926ee527..38ba56cb9 100644 --- a/escher/include/escher/editable_text_cell.h +++ b/escher/include/escher/editable_text_cell.h @@ -9,24 +9,23 @@ class EditableTextCell : public HighlightCell, public Responder { public: EditableTextCell(Responder * parentResponder = nullptr, TextFieldDelegate * delegate = nullptr, char * draftTextBuffer = nullptr, KDText::FontSize size = KDText::FontSize::Large, - float horizontalAlignment = 0.0f, float verticalAlignment = 0.5f, KDColor textColor = KDColorBlack, KDColor = KDColorWhite); - void setTextFieldDelegate(TextFieldDelegate * delegate); - void setTextFieldDraftTextBuffer(char * draftTextBuffer); + float horizontalAlignment = 0.0f, float verticalAlignment = 0.5f, KDColor textColor = KDColorBlack, KDColor = KDColorWhite, KDCoordinate topMargin = 0, KDCoordinate rightMargin = 0, KDCoordinate bottomMargin = 0, KDCoordinate leftMargin = 0); TextField * textField(); + void setMargins(KDCoordinate topMargin = 0, KDCoordinate rightMargin = 0, KDCoordinate bottomMargin = 0, KDCoordinate leftMargin = 0); void setHighlighted(bool highlight) override; - const char * text() const; - void setText(const char * textContent); int numberOfSubviews() const override; View * subviewAtIndex(int index) override; void layoutSubviews() override; void didBecomeFirstResponder() override; - bool isEditing(); - void setEditing(bool isEditing); KDSize minimalSizeForOptimalDisplay() const override; private: constexpr static KDCoordinate k_separatorThickness = 1; TextField m_textField; char m_textBody[TextField::maxBufferSize()]; + KDCoordinate m_topMargin; + KDCoordinate m_rightMargin; + KDCoordinate m_bottomMargin; + KDCoordinate m_leftMargin; }; #endif diff --git a/escher/include/escher/even_odd_editable_text_cell.h b/escher/include/escher/even_odd_editable_text_cell.h index f3cb46710..4c24ff60c 100644 --- a/escher/include/escher/even_odd_editable_text_cell.h +++ b/escher/include/escher/even_odd_editable_text_cell.h @@ -7,18 +7,14 @@ class EvenOddEditableTextCell : public EvenOddCell, public Responder { public: - EvenOddEditableTextCell(Responder * parentResponder, TextFieldDelegate * delegate, char * draftTextBuffer, KDText::FontSize size = KDText::FontSize::Large); + EvenOddEditableTextCell(Responder * parentResponder = nullptr, TextFieldDelegate * delegate = nullptr, char * draftTextBuffer = nullptr, KDText::FontSize size = KDText::FontSize::Large, float horizontalAlignment = 1.0f, float verticalAlignment = 0.5f, KDCoordinate topMargin = 0, KDCoordinate rightMargin = 0, KDCoordinate bottomMargin = 0, KDCoordinate leftMargin = 0); EditableTextCell * editableTextCell(); void setEven(bool even) override; void setHighlighted(bool highlight) override; - const char * text() const; - void setText(const char * textContent); int numberOfSubviews() const override; View * subviewAtIndex(int index) override; void layoutSubviews() override; void didBecomeFirstResponder() override; - bool isEditing(); - void setEditing(bool isEditing); private: EditableTextCell m_editableCell; }; diff --git a/escher/include/escher/metric.h b/escher/include/escher/metric.h index ce68dd3ce..a5535e469 100644 --- a/escher/include/escher/metric.h +++ b/escher/include/escher/metric.h @@ -9,6 +9,7 @@ public: constexpr static KDCoordinate CommonRightMargin = 20; constexpr static KDCoordinate CommonTopMargin = 15; constexpr static KDCoordinate CommonBottomMargin = 15; + constexpr static KDCoordinate HistoryHorizontalMargin = 10; constexpr static KDCoordinate ParameterCellHeight = 35; constexpr static KDCoordinate ModalTopMargin = 5; constexpr static KDCoordinate ModalBottomMargin = 18; diff --git a/escher/include/escher/simple_list_view_data_source.h b/escher/include/escher/simple_list_view_data_source.h index ed787b153..16b38a7ec 100644 --- a/escher/include/escher/simple_list_view_data_source.h +++ b/escher/include/escher/simple_list_view_data_source.h @@ -17,4 +17,4 @@ public: int typeAtLocation(int i, int j) override; }; -#endif \ No newline at end of file +#endif diff --git a/escher/src/editable_text_cell.cpp b/escher/src/editable_text_cell.cpp index 707916277..3a012297e 100644 --- a/escher/src/editable_text_cell.cpp +++ b/escher/src/editable_text_cell.cpp @@ -4,19 +4,22 @@ #include EditableTextCell::EditableTextCell(Responder * parentResponder, TextFieldDelegate * delegate, char * draftTextBuffer, - KDText::FontSize size, float horizontalAlignment, float verticalAlignment, KDColor textColor, KDColor backgroundColor) : + KDText::FontSize size, float horizontalAlignment, float verticalAlignment, KDColor textColor, KDColor backgroundColor, KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin) : HighlightCell(), Responder(parentResponder), - m_textField(this, m_textBody, draftTextBuffer, TextField::maxBufferSize(), delegate, true, size, horizontalAlignment, verticalAlignment, textColor, backgroundColor) + m_textField(this, m_textBody, draftTextBuffer, TextField::maxBufferSize(), delegate, true, size, horizontalAlignment, verticalAlignment, textColor, backgroundColor), + m_topMargin(topMargin), + m_rightMargin(rightMargin), + m_bottomMargin(bottomMargin), + m_leftMargin(leftMargin) { } -void EditableTextCell::setTextFieldDelegate(TextFieldDelegate * delegate) { - m_textField.setDelegate(delegate); -} - -void EditableTextCell::setTextFieldDraftTextBuffer(char * draftTextBuffer) { - m_textField.setDraftTextBuffer(draftTextBuffer); +void EditableTextCell::setMargins(KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin) { + m_topMargin = topMargin; + m_rightMargin = rightMargin; + m_bottomMargin = bottomMargin; + m_leftMargin = leftMargin; } TextField * EditableTextCell::textField() { @@ -29,14 +32,6 @@ void EditableTextCell::setHighlighted(bool highlight) { m_textField.setBackgroundColor(backgroundColor); } -const char * EditableTextCell::text() const { - return m_textField.text(); -} - -void EditableTextCell::setText(const char * text) { - m_textField.setText(text); -} - int EditableTextCell::numberOfSubviews() const { return 1; } @@ -47,21 +42,17 @@ View * EditableTextCell::subviewAtIndex(int index) { } void EditableTextCell::layoutSubviews() { - m_textField.setFrame(bounds()); + KDRect cellBounds = bounds(); + m_textField.setFrame(KDRect(cellBounds.x() + m_leftMargin, + cellBounds.y() + m_topMargin, + cellBounds.width() - m_leftMargin - m_rightMargin, + cellBounds.height() - m_topMargin - m_bottomMargin)); } void EditableTextCell::didBecomeFirstResponder() { app()->setFirstResponder(&m_textField); } -bool EditableTextCell::isEditing() { - return m_textField.isEditing(); -} - -void EditableTextCell::setEditing(bool isEditing) { - m_textField.setEditing(isEditing); -} - KDSize EditableTextCell::minimalSizeForOptimalDisplay() const { return m_textField.minimalSizeForOptimalDisplay(); } diff --git a/escher/src/even_odd_editable_text_cell.cpp b/escher/src/even_odd_editable_text_cell.cpp index b39a11eae..cf16822fc 100644 --- a/escher/src/even_odd_editable_text_cell.cpp +++ b/escher/src/even_odd_editable_text_cell.cpp @@ -2,10 +2,10 @@ #include #include -EvenOddEditableTextCell::EvenOddEditableTextCell(Responder * parentResponder, TextFieldDelegate * delegate, char * draftTextBuffer, KDText::FontSize size) : +EvenOddEditableTextCell::EvenOddEditableTextCell(Responder * parentResponder, TextFieldDelegate * delegate, char * draftTextBuffer, KDText::FontSize size, float horizontalAlignment, float verticalAlignment, KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin) : EvenOddCell(), Responder(parentResponder), - m_editableCell(this, delegate, draftTextBuffer, size, 1.0f, 0.5f, KDColorBlack, KDColorWhite) + m_editableCell(this, delegate, draftTextBuffer, size, horizontalAlignment, verticalAlignment, KDColorBlack, KDColorWhite, topMargin, rightMargin, bottomMargin, leftMargin) { } @@ -23,14 +23,6 @@ void EvenOddEditableTextCell::setEven(bool even) { m_editableCell.textField()->setBackgroundColor(backgroundColor()); } -const char * EvenOddEditableTextCell::text() const { - return m_editableCell.text(); -} - -void EvenOddEditableTextCell::setText(const char * textContent) { - m_editableCell.setText(textContent); -} - int EvenOddEditableTextCell::numberOfSubviews() const { return 1; } @@ -47,11 +39,3 @@ void EvenOddEditableTextCell::layoutSubviews() { void EvenOddEditableTextCell::didBecomeFirstResponder() { app()->setFirstResponder(&m_editableCell); } - -bool EvenOddEditableTextCell::isEditing() { - return m_editableCell.isEditing(); -} - -void EvenOddEditableTextCell::setEditing(bool isEditing) { - m_editableCell.setEditing(isEditing); -}