Blame view

epsilon-master/escher/src/expression_layout_field_content_view.cpp 2.64 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <escher/expression_layout_field.h>
  #include <kandinsky/rect.h>
  #include <assert.h>
  
  using namespace Poincare;
  
  ExpressionLayoutField::ContentView::ContentView(ExpressionLayout * expressionLayout) :
    m_cursor(expressionLayout, ExpressionLayoutCursor::Position::Right),
    m_expressionView(0.0f, 0.5f, KDColorBlack, KDColorWhite),
    m_cursorView(),
    m_isEditing(false)
  {
    m_expressionView.setExpressionLayout(expressionLayout);
  }
  
  void ExpressionLayoutField::ContentView::setEditing(bool isEditing) {
    m_isEditing = isEditing;
    markRectAsDirty(bounds());
    layoutSubviews();
  }
  
  void ExpressionLayoutField::ContentView::setBackgroundColor(KDColor c) {
    m_expressionView.setBackgroundColor(c);
  }
  
  void ExpressionLayoutField::ContentView::cursorPositionChanged() {
    layoutCursorSubview();
  }
  
  KDRect ExpressionLayoutField::ContentView::cursorRect() {
    return m_cursorView.frame();
  }
  
  void ExpressionLayoutField::ContentView::clearLayout() {
    m_cursor.clearLayout();
  }
  
  KDSize ExpressionLayoutField::ContentView::minimalSizeForOptimalDisplay() const {
    KDSize evSize = m_expressionView.minimalSizeForOptimalDisplay();
    return KDSize(evSize.width() + ExpressionLayoutCursor::k_cursorWidth, evSize.height());
  }
  
  View * ExpressionLayoutField::ContentView::subviewAtIndex(int index) {
    assert(index >= 0 && index < 2);
    View * m_views[] = {&m_expressionView, &m_cursorView};
    return m_views[index];
  }
  
  void ExpressionLayoutField::ContentView::layoutSubviews() {
    m_expressionView.setFrame(bounds());
    layoutCursorSubview();
  }
  
  void ExpressionLayoutField::ContentView::layoutCursorSubview() {
    if (!m_isEditing) {
      m_cursorView.setFrame(KDRectZero);
      return;
    }
    KDPoint expressionViewOrigin = m_expressionView.absoluteDrawingOrigin();
    ExpressionLayout * pointedLayout = m_cursor.pointedExpressionLayout();
    ExpressionLayoutCursor::Position cursorPosition = m_cursor.position();
    ExpressionLayoutCursor eqCursor = pointedLayout->equivalentCursor(m_cursor);
    if (pointedLayout->hasChild(eqCursor.pointedExpressionLayout())) {
      pointedLayout = eqCursor.pointedExpressionLayout();
      cursorPosition = eqCursor.position();
    }
    KDPoint cursoredExpressionViewOrigin = pointedLayout->absoluteOrigin();
    KDCoordinate cursorX = expressionViewOrigin.x() + cursoredExpressionViewOrigin.x();
    if (cursorPosition == ExpressionLayoutCursor::Position::Right) {
      cursorX += pointedLayout->size().width();
    }
    KDPoint cursorTopLeftPosition(cursorX, expressionViewOrigin.y() + cursoredExpressionViewOrigin.y() + pointedLayout->baseline() - m_cursor.baseline());
    m_cursorView.setFrame(KDRect(cursorTopLeftPosition, ExpressionLayoutCursor::k_cursorWidth, m_cursor.cursorHeight()));
  }