Numworks Epsilon  1.4.1
Graphing Calculator Operating System
exam_pop_up_controller.cpp
Go to the documentation of this file.
2 #include "apps_container.h"
3 #include "i18n.h"
4 #include "global_preferences.h"
5 #include <assert.h>
6 
8  ViewController(nullptr),
9  m_contentView(this),
10  m_isActivatingExamMode(false),
11  m_delegate(delegate)
12 {
13 }
14 
15 void ExamPopUpController::setActivatingExamMode(bool activatingExamMode) {
16  if (m_isActivatingExamMode != activatingExamMode) {
17  m_isActivatingExamMode = activatingExamMode;
18  m_contentView.setMessages(activatingExamMode);
19  }
20 }
21 
23  return &m_contentView;
24 }
25 
27  if (m_isActivatingExamMode == false) {
29  }
30 }
31 
33  m_contentView.setSelectedButton(0, app());
34 }
35 
37  if (event == Ion::Events::Left && m_contentView.selectedButton() == 1) {
38  m_contentView.setSelectedButton(0, app());
39  return true;
40  }
41  if (event == Ion::Events::Right && m_contentView.selectedButton() == 0) {
42  m_contentView.setSelectedButton(1, app());
43  return true;
44  }
45  return false;
46 }
47 
48 ExamPopUpController::ContentView::ContentView(Responder * parentResponder) :
49  m_cancelButton(parentResponder, I18n::Message::Cancel, Invocation([](void * context, void * sender) {
50  ExamPopUpController * controller = (ExamPopUpController *)context;
51  Container * container = (Container *)controller->app()->container();
52  container->activeApp()->dismissModalViewController();
53  }, parentResponder), KDText::FontSize::Small),
54  m_okButton(parentResponder, I18n::Message::Ok, Invocation([](void * context, void * sender) {
55  ExamPopUpController * controller = (ExamPopUpController *)context;
58  AppsContainer * container = (AppsContainer *)controller->app()->container();
59  if (controller->isActivatingExamMode()) {
60  container->reset();
61  } else {
63  }
64  container->refreshPreferences();
65  container->activeApp()->dismissModalViewController();
66  }, parentResponder), KDText::FontSize::Small),
67  m_warningTextView(KDText::FontSize::Small, I18n::Message::Warning, 0.5, 0.5, KDColorWhite, KDColorBlack),
68  m_messageTextView1(KDText::FontSize::Small, I18n::Message::Default, 0.5, 0.5, KDColorWhite, KDColorBlack),
69  m_messageTextView2(KDText::FontSize::Small, I18n::Message::Default, 0.5, 0.5, KDColorWhite, KDColorBlack),
70  m_messageTextView3(KDText::FontSize::Small, I18n::Message::Default, 0.5, 0.5, KDColorWhite, KDColorBlack)
71 {
72 }
73 
74 void ExamPopUpController::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
75  ctx->fillRect(bounds(), KDColorBlack);
76 }
77 
78 void ExamPopUpController::ContentView::setSelectedButton(int selectedButton, App * app) {
79  m_cancelButton.setHighlighted(selectedButton == 0);
80  m_okButton.setHighlighted(selectedButton == 1);
81  if (selectedButton == 0) {
82  app->setFirstResponder(&m_cancelButton);
83  } else {
84  app->setFirstResponder(&m_okButton);
85  }
86 }
87 
88 int ExamPopUpController::ContentView::selectedButton() {
89  if (m_cancelButton.isHighlighted()) {
90  return 0;
91  }
92  return 1;
93 }
94 
95 void ExamPopUpController::ContentView::setMessages(bool activingExamMode) {
96  if (activingExamMode) {
97  m_messageTextView1.setMessage(I18n::Message::ActiveExamModeMessage1);
98  m_messageTextView2.setMessage(I18n::Message::ActiveExamModeMessage2);
99  m_messageTextView3.setMessage(I18n::Message::ActiveExamModeMessage3);
100  } else {
101  m_messageTextView1.setMessage(I18n::Message::ExitExamMode1);
102  m_messageTextView2.setMessage(I18n::Message::ExitExamMode2);
103  m_messageTextView3.setMessage(I18n::Message::Default);
104  }
105 }
106 
107 int ExamPopUpController::ContentView::numberOfSubviews() const {
108  return 6;
109 }
110 
111 View * ExamPopUpController::ContentView::subviewAtIndex(int index) {
112  switch (index) {
113  case 0:
114  return &m_warningTextView;
115  case 1:
116  return &m_messageTextView1;
117  case 2:
118  return &m_messageTextView2;
119  case 3:
120  return &m_messageTextView3;
121  case 4:
122  return &m_cancelButton;
123  case 5:
124  return &m_okButton;
125  default:
126  assert(false);
127  return nullptr;
128  }
129 }
130 
131 void ExamPopUpController::ContentView::layoutSubviews() {
132  KDCoordinate height = bounds().height();
133  KDCoordinate width = bounds().width();
135  m_warningTextView.setFrame(KDRect(0, k_topMargin, width, textHeight));
136  m_messageTextView1.setFrame(KDRect(0, k_topMargin+k_paragraphHeight+textHeight, width, textHeight));
137  m_messageTextView2.setFrame(KDRect(0, k_topMargin+k_paragraphHeight+2*textHeight, width, textHeight));
138  m_messageTextView3.setFrame(KDRect(0, k_topMargin+k_paragraphHeight+3*textHeight, width, textHeight));
139  m_cancelButton.setFrame(KDRect(k_buttonMargin, height-k_buttonMargin-k_buttonHeight, (width-3*k_buttonMargin)/2, k_buttonHeight));
140  m_okButton.setFrame(KDRect(2*k_buttonMargin+(width-3*k_buttonMargin)/2, height-k_buttonMargin-k_buttonHeight, (width-3*k_buttonMargin)/2, k_buttonHeight));
141 }
Definition: i18n.h:6
ExamPopUpController(ExamPopUpControllerDelegate *delegate)
#define assert(e)
Definition: assert.h:9
int16_t KDCoordinate
Definition: coordinate.h:6
virtual void examDeactivatingPopUpIsDismissed()=0
void setExamMode(ExamMode examMode)
void didBecomeFirstResponder() override
bool handleEvent(Ion::Events::Event event) override
constexpr KDColor KDColorWhite
Definition: color.h:42
void viewDidDisappear() override
constexpr KDColor KDColorBlack
Definition: color.h:41
#define false
Definition: stdbool.h:9
constexpr Event Left
Definition: events.h:61
Definition: app.h:23
void setColor(KDColor c)
Definition: led.cpp:8
Definition: rect.h:26
void fillRect(KDRect rect, KDColor color)
Definition: context_rect.cpp:8
const Container * container() const
Definition: app.cpp:102
void setFirstResponder(Responder *responder)
Definition: app.cpp:62
Definition: view.h:23
constexpr Event Right
Definition: events.h:64
static constexpr KDSize charSize(FontSize size=FontSize::Large)
Definition: text.h:16
void refreshPreferences()
App * app()
Definition: responder.cpp:77
void dismissModalViewController()
Definition: app.cpp:93
App * activeApp()
Definition: container.cpp:34
void setActivatingExamMode(bool activingExamMode)
static GlobalPreferences * sharedGlobalPreferences()
constexpr KDCoordinate height() const
Definition: size.h:11