COSA
An Object-Oriented Platform for Arduino Programming
Textbox.cpp
Go to the documentation of this file.
1 
21 #include <Canvas.h>
23 
24 int
26 {
27  // Save the current context and setup our context
28  Canvas::Context* saved = m_canvas->set_context(this);
29 
30  // Handle some special characters, line-feed
31  uint16_t x, y;
32  get_cursor(x, y);
33  uint8_t scale = m_text_scale;
34  uint16_t width = (x + (scale * (m_font->WIDTH + m_font->SPACING)) - m_text_port.x);
35  if ((c == '\n') || (width > m_text_port.width)) {
36  uint16_t font_height = scale * (m_font->HEIGHT);
37  uint16_t line_height = scale * (m_font->HEIGHT + m_font->LINE_SPACING);
38  uint16_t y = m_cursor.y + line_height;
39  if (y + font_height > m_text_port.y + m_text_port.height) {
40  y = m_text_port.y;
41  }
42  x = m_text_port.x;
44  set_cursor(x, y);
45  m_canvas->fill_rect(m_text_port.width, line_height);
46  set_pen_color(saved);
47  }
48 
49  // Carriage return
50  else if (c == '\r') {
52  }
53 
54  // Form-feed
55  else if (c == '\f') {
59  set_pen_color(saved);
60  }
61 
62  // Draw other characters
63  else m_canvas->draw_char(c);
64 
65  // Restore the previous canvas state; context
66  m_canvas->set_context(saved);
67  return (c);
68 }
uint16_t width
Definition: Canvas.hh:125
Font * m_font
Current font.
Definition: Canvas.hh:304
uint16_t y
Definition: Canvas.hh:124
pos16_t m_cursor
Current cursor position.
Definition: Canvas.hh:305
virtual void draw_char(uint16_t x, uint16_t y, char c)
Definition: Canvas.cpp:378
uint16_t y
Definition: Canvas.hh:116
const uint8_t SPACING
Definition: Font.hh:37
uint16_t x
Definition: Canvas.hh:123
const uint8_t WIDTH
Definition: Font.hh:35
const uint8_t HEIGHT
Definition: Font.hh:36
virtual void fill_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition: Canvas.cpp:151
uint8_t LINE_SPACING
Definition: Font.hh:38
Context * set_context(Context *context)
Definition: Canvas.hh:411
Canvas * m_canvas
Definition: Canvas.hh:326
Canvas::rect16_t m_text_port
Definition: Textbox.hh:113
uint8_t m_text_scale
Current text scale.
Definition: Canvas.hh:303
color16_t get_canvas_color() const
Definition: Canvas.hh:164
color16_t set_pen_color(color16_t color)
Definition: Canvas.hh:195
virtual int putchar(char c)
Definition: Textbox.cpp:25
void get_cursor(uint16_t &x, uint16_t &y) const
Definition: Canvas.hh:271
void set_cursor(uint16_t x, uint16_t y)
Definition: Canvas.hh:282
uint16_t height
Definition: Canvas.hh:126