[code] Create a PythonTextArea

This commit is contained in:
Romain Goyet
2018-06-01 17:25:57 +02:00
committed by EmilieNumworks
parent 9e4a1c7d54
commit 88e40266f6
6 changed files with 99 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ app_objs += $(addprefix apps/code/,\
helpers.o\
menu_controller.o\
python_toolbox.o\
python_text_area.o\
sandbox_controller.o\
script.o\
script_node_cell.o\

View File

@@ -2,13 +2,17 @@
#include <poincare.h>
#include <escher/app.h>
namespace Code {
/* EditorView */
constexpr KDText::FontSize editorFontSize = KDText::FontSize::Large;
EditorView::EditorView(Responder * parentResponder) :
Responder(parentResponder),
View(),
m_textArea(parentResponder),
m_gutterView(KDText::FontSize::Large)
m_textArea(parentResponder, editorFontSize),
m_gutterView(editorFontSize)
{
m_textArea.setScrollViewDelegate(this);
}
@@ -92,3 +96,5 @@ KDSize EditorView::GutterView::minimalSizeForOptimalDisplay() const {
int numberOfChars = 2; // TODO: Could be computed
return KDSize(2 * k_margin + numberOfChars * KDText::charSize(m_fontSize).width(), 0);
}
}

View File

@@ -2,7 +2,9 @@
#define CODE_EDITOR_VIEW_H
#include <escher/view.h>
#include <escher/solid_text_area.h>
#include "python_text_area.h"
namespace Code {
class EditorView : public Responder, public View, public ScrollViewDelegate {
public:
@@ -38,8 +40,10 @@ private:
KDCoordinate m_offset;
};
SolidTextArea m_textArea;
PythonTextArea m_textArea;
GutterView m_gutterView;
};
}
#endif

View File

@@ -0,0 +1,53 @@
#include "python_text_area.h"
namespace Code {
/* PythonTextArea */
void PythonTextArea::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
}
/* PythonTextArea::ContentView */
PythonTextArea::ContentView::ContentView(KDText::FontSize fontSize) :
TextArea::ContentView(fontSize)
{
}
void PythonTextArea::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(rect, m_backgroundColor);
KDSize charSize = KDText::charSize(m_fontSize);
// We want to draw even partially visible characters. So we need to round
// down for the top left corner and up for the bottom right one.
Text::Position topLeft(
rect.x()/charSize.width(),
rect.y()/charSize.height()
);
Text::Position bottomRight(
rect.right()/charSize.width() + 1,
rect.bottom()/charSize.height() + 1
);
int y = 0;
size_t x = topLeft.column();
for (Text::Line line : m_text) {
if (y >= topLeft.line() && y <= bottomRight.line() && topLeft.column() < (int)line.length()) {
//drawString(line.text(), 0, y*charHeight); // Naive version
ctx->drawString(
line.text() + topLeft.column(),
KDPoint(x*charSize.width(), y*charSize.height()),
m_fontSize,
m_textColor,
m_backgroundColor,
min(line.length() - topLeft.column(), bottomRight.column() - topLeft.column())
);
}
y++;
}
}
}

View File

@@ -0,0 +1,28 @@
#ifndef CODE_PYTHON_TEXT_AREA_H
#define CODE_PYTHON_TEXT_AREA_H
#include <escher/text_area.h>
namespace Code {
class PythonTextArea : public TextArea {
public:
PythonTextArea(Responder * parentResponder, KDText::FontSize fontSize) :
TextArea(parentResponder, &m_contentView, fontSize),
m_contentView(fontSize)
{
}
protected:
class ContentView : public TextArea::ContentView {
public:
ContentView(KDText::FontSize fontSize);
void drawRect(KDContext * ctx, KDRect rect) const override;
};
private:
const ContentView * nonEditableContentView() const override { return &m_contentView; }
ContentView m_contentView;
};
}
#endif

View File

@@ -3,7 +3,7 @@
#include <escher/text_area.h>
/* SimpleTextArea is a text area that draws text in a single solid color over a
/* SolidTextArea is a text area that draws text in a single solid color over a
* solid background. */
class SolidTextArea : public TextArea {
@@ -15,8 +15,8 @@ public:
protected:
class ContentView : public TextArea::ContentView {
public:
ContentView(KDText::FontSize size, KDColor textColor, KDColor backgroundColor) :
TextArea::ContentView(size),
ContentView(KDText::FontSize fontSize, KDColor textColor, KDColor backgroundColor) :
TextArea::ContentView(fontSize),
m_textColor(textColor),
m_backgroundColor(backgroundColor)
{