Numworks Epsilon  1.4.1
Graphing Calculator Operating System
title_bar_view.cpp
Go to the documentation of this file.
1 #include "title_bar_view.h"
2 #include "exam_icon.h"
3 #include "global_preferences.h"
4 extern "C" {
5 #include <assert.h>
6 }
7 #include <poincare.h>
8 
9 using namespace Poincare;
10 
12  View(),
13  m_titleView(KDText::FontSize::Small, I18n::Message::Default, 0.5f, 0.5f, KDColorWhite, Palette::YellowDark),
14  m_preferenceView(KDText::FontSize::Small, 1.0f, 0.5, KDColorWhite, Palette::YellowDark)
15 {
16  m_examModeIconView.setImage(ImageStore::ExamIcon);
17 }
18 
19 void TitleBarView::drawRect(KDContext * ctx, KDRect rect) const {
20  /* As we cheated to layout the title view, we have to fill a very thin
21  * rectangle at the top with the background color. */
22  ctx->fillRect(KDRect(0, 0, bounds().width(), 2), Palette::YellowDark);
23 }
24 
25 void TitleBarView::setTitle(I18n::Message title) {
26  m_titleView.setMessage(title);
27 }
28 
30  return m_batteryView.setChargeState(chargeState);
31 }
32 
34  return m_batteryView.setIsCharging(isCharging);
35 }
36 
38  return m_batteryView.setIsPlugged(isPlugged);
39 }
40 
42  return m_shiftAlphaLockView.setStatus(status);
43 }
44 
45 int TitleBarView::numberOfSubviews() const {
46  return 5;
47 }
48 
49 View * TitleBarView::subviewAtIndex(int index) {
50  if (index == 0) {
51  return &m_titleView;
52  }
53  if (index == 1) {
54  return &m_preferenceView;
55  }
56  if (index == 2) {
57  return &m_examModeIconView;
58  }
59  if (index == 3) {
60  return &m_shiftAlphaLockView;
61  }
62  return &m_batteryView;
63 }
64 
65 void TitleBarView::layoutSubviews() {
66  /* We here cheat to layout the main title. The application title is written
67  * with upper cases. But, as upper letters are on the same baseline as lower
68  * letters, they seem to be slightly above when they are perferctly centered
69  * (because their glyph never cross the baseline). To avoid this effect, we
70  * translate the frame of the title downwards.*/
71  m_titleView.setFrame(KDRect(0, 2, bounds().width(), bounds().height()-2));
73  KDSize batterySize = m_batteryView.minimalSizeForOptimalDisplay();
74  m_batteryView.setFrame(KDRect(bounds().width() - batterySize.width() - Metric::TitleBarExternHorizontalMargin, (bounds().height()- batterySize.height())/2, batterySize));
76  m_examModeIconView.setFrame(KDRect(k_examIconMargin, (bounds().height() - k_examIconHeight)/2, k_examIconWidth, k_examIconHeight));
77  } else {
78  m_examModeIconView.setFrame(KDRectZero);
79  }
80  KDSize shiftAlphaLockSize = m_shiftAlphaLockView.minimalSizeForOptimalDisplay();
81  m_shiftAlphaLockView.setFrame(KDRect(bounds().width()-batterySize.width()-Metric::TitleBarExternHorizontalMargin-k_alphaRightMargin-shiftAlphaLockSize.width(), (bounds().height()- shiftAlphaLockSize.height())/2, shiftAlphaLockSize));
82 }
83 
85  char buffer[13];
86  int numberOfChar = 0;
87  if (Preferences::sharedPreferences()->displayMode() == PrintFloat::Mode::Scientific) {
88  strlcpy(buffer, I18n::translate(I18n::Message::Sci), strlen(I18n::translate(I18n::Message::Sci))+1);
89  numberOfChar += strlen(I18n::translate(I18n::Message::Sci));
90  }
91  if (Preferences::sharedPreferences()->angleUnit() == Expression::AngleUnit::Radian) {
92  strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Rad), strlen(I18n::translate(I18n::Message::Rad))+1);
93  numberOfChar += strlen(I18n::translate(I18n::Message::Rad));
94  } else {
95  strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Deg), strlen(I18n::translate(I18n::Message::Sci))+1);
96  numberOfChar += strlen(I18n::translate(I18n::Message::Deg));
97  }
98  buffer[numberOfChar] = 0;
99  m_preferenceView.setText(buffer);
100  // Layout the exam mode icon if needed
101  layoutSubviews();
102 }
bool isPlugged()
Definition: usb.cpp:12
void refreshPreferences()
static constexpr KDColor YellowDark
Definition: palette.h:8
Definition: i18n.h:6
ShiftAlphaStatus
Definition: events.h:41
void drawRect(KDContext *ctx, KDRect rect) const override
void setText(const char *text) override
void setFrame(KDRect frame)
Definition: view.cpp:125
bool isCharging()
Definition: battery.cpp:16
constexpr KDCoordinate width() const
Definition: size.h:10
size_t strlcpy(char *dst, const char *src, size_t len)
Definition: strlcpy.c:3
bool setIsCharging(bool isCharging)
bool setIsPlugged(bool isPlugged)
bool setChargeState(Ion::Battery::Charge chargeState)
Definition: text.h:8
bool setShiftAlphaLockStatus(Ion::Events::ShiftAlphaStatus status)
Definition: size.h:6
enum Message uint16_t enum Language uint16_t const char * translate(Message m, Language l=(Language) 0)
Definition: i18n.cpp:5
bool setIsCharging(bool isCharging)
KDSize minimalSizeForOptimalDisplay() const override
constexpr KDColor KDColorWhite
Definition: color.h:42
size_t strlen(const char *s)
Definition: strlen.c:3
bool setStatus(Ion::Events::ShiftAlphaStatus status)
bool setIsPlugged(bool isPlugged)
constexpr Poincare::Expression::AngleUnit Radian
Definition: helper.h:4
KDSize minimalSizeForOptimalDisplay() const override
void setMessage(I18n::Message message)
void setImage(const Image *image)
Definition: image_view.cpp:21
void setTitle(I18n::Message title)
constexpr KDRect KDRectZero
Definition: rect.h:70
Definition: rect.h:26
void fillRect(KDRect rect, KDColor color)
Definition: context_rect.cpp:8
Definition: view.h:23
ExamMode examMode() const
static constexpr KDCoordinate TitleBarExternHorizontalMargin
Definition: metric.h:13
KDCoordinate height() const
Definition: rect.h:40
KDRect bounds() const
Definition: view.cpp:157
bool setChargeState(Ion::Battery::Charge chargeState)
static GlobalPreferences * sharedGlobalPreferences()
KDSize minimalSizeForOptimalDisplay() const override
Definition: text_view.cpp:35
constexpr KDCoordinate height() const
Definition: size.h:11
Definition: palette.h:6