Blame view

build6/epsilon-master/apps/code/editor_view.cpp 2.52 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  #include "editor_view.h"
  #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, editorFontSize),
    m_gutterView(editorFontSize)
  {
    m_textArea.setScrollViewDelegate(this);
  }
  
  void EditorView::scrollViewDidChangeOffset(ScrollViewDataSource * scrollViewDataSource) {
    m_gutterView.setOffset(scrollViewDataSource->offset().y());
  }
  
  int EditorView::numberOfSubviews() const {
    return 2;
  }
  
  View * EditorView::subviewAtIndex(int index) {
    View * subviews[] = {&m_textArea, &m_gutterView};
    return subviews[index];
  }
  
  void EditorView::didBecomeFirstResponder() {
    app()->setFirstResponder(&m_textArea);
  }
  
  void EditorView::layoutSubviews() {
    m_gutterView.setOffset(0);
    KDCoordinate gutterWidth = m_gutterView.minimalSizeForOptimalDisplay().width();
    m_gutterView.setFrame(KDRect(0, 0, gutterWidth, bounds().height()));
  
    m_textArea.setFrame(KDRect(
      gutterWidth,
      0,
      bounds().width()-gutterWidth,
      bounds().height()
    ));
  }
  
  /* EditorView::GutterView */
  
  EditorView::GutterView::GutterView(KDText::FontSize fontSize) :
    View(),
    m_fontSize(fontSize),
    m_offset(0)
  {
  }
  
  void EditorView::GutterView::drawRect(KDContext * ctx, KDRect rect) const {
    KDColor textColor = KDColor::RGB24(0x919EA4);
    KDColor backgroundColor = KDColor::RGB24(0xE4E6E7);
  
    ctx->fillRect(rect, backgroundColor);
  
    KDSize charSize = KDText::charSize(m_fontSize);
  
    KDCoordinate firstLine = m_offset / charSize.height();
    KDCoordinate firstLinePixelOffset = m_offset - firstLine * charSize.height();
  
    char lineNumber[4];
    int numberOfLines = bounds().height() / charSize.height() + 1;
    for (int i=0; i<numberOfLines; i++) {
      Poincare::Integer line(i + firstLine + 1);
      line.writeTextInBuffer(lineNumber, 4);
      KDCoordinate leftPadding = (2 - strlen(lineNumber)) * charSize.width();
      ctx->drawString(
        lineNumber,
        KDPoint(k_margin + leftPadding, i*charSize.height() - firstLinePixelOffset),
        m_fontSize,
        textColor,
        backgroundColor
      );
    }
  }
  
  void EditorView::GutterView::setOffset(KDCoordinate offset) {
    if (m_offset == offset) {
      return;
    }
    m_offset = offset;
    markRectAsDirty(bounds());
  }
  
  
  KDSize EditorView::GutterView::minimalSizeForOptimalDisplay() const {
    int numberOfChars = 2; // TODO: Could be computed
    return KDSize(2 * k_margin + numberOfChars * KDText::charSize(m_fontSize).width(), 0);
  }
  
  }