Blame view

build3/escher/src/tab_view.cpp 2.57 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 <escher/tab_view.h>
  #include <escher/tab_view_controller.h>
  extern "C" {
  #include <assert.h>
  }
  
  TabView::TabView() :
    View(),
    m_numberOfTabs(0),
    m_activeTabIndex(-1),
    m_selectedTabIndex(-1)
  {
  }
  
  int TabView::numberOfTabs() const {
    return m_numberOfTabs;
  }
  
  void TabView::drawRect(KDContext * ctx, KDRect rect) const {
    KDCoordinate height = bounds().height();
    KDCoordinate width = bounds().width();
    // Draw a separator with the content
    ctx->fillRect(KDRect(0, height-k_activeTabHeight, width, k_activeTabHeight), KDColorWhite);
  }
  
  void TabView::addTab(ViewController * controller) {
    assert(m_numberOfTabs < k_maxNumberOfTabs);
    uint8_t tabIndex = m_numberOfTabs;
    m_cells[tabIndex].setNamedController(controller);
    m_numberOfTabs++;
    markRectAsDirty(bounds());
  }
  
  void TabView::setActiveIndex(int index) {
    assert(index < m_numberOfTabs);
    if (m_activeTabIndex == index) {
      return;
    }
    if (m_activeTabIndex >= 0) {
      m_cells[m_activeTabIndex].setActive(false);
    }
    m_activeTabIndex = index;
    m_cells[m_activeTabIndex].setActive(true);
  }
  
  void TabView::setSelectedIndex(int index) {
    assert(index < m_numberOfTabs);
    if (m_selectedTabIndex == index) {
      return;
    }
    if (m_selectedTabIndex >= 0) {
      m_cells[m_selectedTabIndex].setSelected(false);
    }
    m_selectedTabIndex = index;
    if (m_selectedTabIndex >= 0) {
      m_cells[m_selectedTabIndex].setSelected(true);
    }
  }
  
  int TabView::numberOfSubviews() const {
    return m_numberOfTabs;
  }
  
  View * TabView::subviewAtIndex(int index) {
    assert(index < m_numberOfTabs);
    return &m_cells[index];
  }
  
  void TabView::layoutSubviews() {
    KDCoordinate emptyWidth = bounds().width();
    for (int i=0; i<m_numberOfTabs; i++) {
      emptyWidth -= m_cells[i].minimalSizeForOptimalDisplay().width();
    }
    KDCoordinate widthUsed = 0;
    for (int i=0; i<m_numberOfTabs; i++) {
      KDCoordinate tabWidth = m_cells[i].minimalSizeForOptimalDisplay().width() + emptyWidth/m_numberOfTabs;
      /* Avoid a unused one-pixel-width vertical on the left due to rounding error */
      if (i == m_numberOfTabs - 1) {
        tabWidth = bounds().width() - widthUsed;
      }
      KDRect cellFrame = KDRect(
          widthUsed, 0,
          tabWidth, m_frame.height() - k_activeTabHeight
          );
      m_cells[i].setFrame(cellFrame);
      widthUsed += tabWidth;
    }
  }
  
  #if ESCHER_VIEW_LOGGING
  const char * TabView::className() const {
    return "TabView";
  }
  
  void TabView::logAttributes(std::ostream &os) const {
    View::logAttributes(os);
    os << " numberOfTabs=\"" << (int)m_numberOfTabs << "\"";
    os << " activeTabIndex=\"" << (int)m_activeTabIndex << "\"";
  }
  #endif