Blame view

build6/epsilon-master/apps/code/console_line_cell.cpp 2.95 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 "console_line_cell.h"
  #include "console_controller.h"
  #include <kandinsky/point.h>
  #include <kandinsky/coordinate.h>
  #include <apps/i18n.h>
  
  namespace Code {
  
  ConsoleLineCell::ScrollableConsoleLineView::ConsoleLineView::ConsoleLineView() :
    HighlightCell(),
    m_line(nullptr)
  {
  }
  
  void ConsoleLineCell::ScrollableConsoleLineView::ConsoleLineView::setLine(ConsoleLine * line) {
    m_line = line;
  }
  
  void ConsoleLineCell::ScrollableConsoleLineView::ConsoleLineView::drawRect(KDContext * ctx, KDRect rect) const {
    ctx->fillRect(bounds(), KDColorWhite);
    ctx->drawString(m_line->text(), KDPointZero, ConsoleController::k_fontSize, textColor(m_line), isHighlighted()? Palette::Select : KDColorWhite);
  }
  
  KDSize ConsoleLineCell::ScrollableConsoleLineView::ConsoleLineView::minimalSizeForOptimalDisplay() const {
    return KDText::stringSize(m_line->text(), ConsoleController::k_fontSize);
  }
  
  ConsoleLineCell::ScrollableConsoleLineView::ScrollableConsoleLineView(Responder * parentResponder) :
    ScrollableView(parentResponder, &m_consoleLineView, this),
    m_consoleLineView()
  {
  }
  
  KDSize ConsoleLineCell::ScrollableConsoleLineView::minimalSizeForOptimalDisplay() const {
    return m_consoleLineView.minimalSizeForOptimalDisplay();
  }
  
  ConsoleLineCell::ConsoleLineCell(Responder * parentResponder) :
    HighlightCell(),
    Responder(parentResponder),
    m_promptView(ConsoleController::k_fontSize, I18n::Message::ConsolePrompt, 0, 0.5),
    m_scrollableView(this),
    m_line()
  {
  }
  
  void ConsoleLineCell::setLine(ConsoleLine line) {
    m_line = line;
    m_scrollableView.consoleLineView()->setLine(&m_line);
    m_promptView.setTextColor(textColor(&m_line));
    reloadCell();
  }
  
  void ConsoleLineCell::setHighlighted(bool highlight) {
    HighlightCell::setHighlighted(highlight);
    m_scrollableView.consoleLineView()->setHighlighted(highlight);
  }
  
  void ConsoleLineCell::reloadCell() {
    layoutSubviews();
    HighlightCell::reloadCell();
    m_scrollableView.reloadScroll();
  }
  
  int ConsoleLineCell::numberOfSubviews() const {
    if (m_line.isCommand()) {
      return 2;
    }
    assert(m_line.isResult());
    return 1;
  }
  
  View * ConsoleLineCell::subviewAtIndex(int index) {
    if (m_line.isCommand()) {
      assert(index >= 0 && index < 2);
      View * views[] = {&m_promptView, &m_scrollableView};
      return views[index];
    }
    assert(m_line.isResult());
    assert(index == 0);
    return &m_scrollableView;
  }
  
  void ConsoleLineCell::layoutSubviews() {
    if (m_line.isCommand()) {
      KDSize promptSize = KDText::stringSize(I18n::translate(I18n::Message::ConsolePrompt), ConsoleController::k_fontSize);
      m_promptView.setFrame(KDRect(KDPointZero, promptSize.width(), bounds().height()));
      m_scrollableView.setFrame(KDRect(KDPoint(promptSize.width(), 0), bounds().width() - promptSize.width(), bounds().height()));
      return;
    }
    assert(m_line.isResult());
    m_promptView.setFrame(KDRectZero);
    m_scrollableView.setFrame(bounds());
  }
  
  void ConsoleLineCell::didBecomeFirstResponder() {
    app()->setFirstResponder(&m_scrollableView);
  }
  
  }