Skip to content

Commit

Permalink
select all working for text entry
Browse files Browse the repository at this point in the history
  • Loading branch information
adlr committed Feb 15, 2014
1 parent 37a3478 commit c8bdd03
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 28 deletions.
12 changes: 9 additions & 3 deletions root_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,27 @@ void RootView::HandlePepperInputEvent(const pp::InputEvent& event) {
case PP_INPUTEVENT_TYPE_CHAR: {
pp::KeyboardInputEvent key_evt(event);
KeyboardInputEvent evt(KeyboardInputEvent::TEXT,
key_evt.GetCharacterText().AsString());
key_evt.GetCharacterText().AsString(),
key_evt.GetModifiers() &
KeyboardInputEvent::kModifiersMask);
OnKeyText(evt);
return;
}
case PP_INPUTEVENT_TYPE_KEYDOWN: {
pp::KeyboardInputEvent key_evt(event);
KeyboardInputEvent evt(KeyboardInputEvent::DOWN,
key_evt.GetKeyCode());
key_evt.GetKeyCode(),
key_evt.GetModifiers() &
KeyboardInputEvent::kModifiersMask);
OnKeyDown(evt);
return;
}
case PP_INPUTEVENT_TYPE_KEYUP: {
pp::KeyboardInputEvent key_evt(event);
KeyboardInputEvent evt(KeyboardInputEvent::UP,
key_evt.GetKeyCode());
key_evt.GetKeyCode(),
key_evt.GetModifiers() &
KeyboardInputEvent::kModifiersMask);
OnKeyUp(evt);
return;
}
Expand Down
85 changes: 65 additions & 20 deletions text_area.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "text_area.h"

#include <cstring>
#include <string>

using std::string;
Expand Down Expand Up @@ -31,14 +32,25 @@ void TextArea::EndEditing() {
}

void TextArea::OnKeyText(const KeyboardInputEvent& event) {
if (event.modifiers() & KeyboardInputEvent::kShortcutMask)
return;
EraseSelection();
text_.insert(selection_start_, event.text());
const string* use = &event.text();
const string newline("\n");
if (!strcmp(event.text().c_str(), "\r"))
use = &newline;
text_.insert(selection_start_, *use);
selection_start_ += event.text().size();
SetNeedsDisplay(false);
}

void TextArea::OnKeyDown(const KeyboardInputEvent& event) {
printf("got keydown %d\n", event.keycode());
printf("got keydown %s%s%s%s%d\n",
event.modifiers() & KeyboardInputEvent::kShift ? "shift-" : "",
event.modifiers() & KeyboardInputEvent::kControl ? "ctrl-" : "",
event.modifiers() & KeyboardInputEvent::kAlt ? "alt-" : "",
event.modifiers() & KeyboardInputEvent::kMeta ? "meta-" : "",
event.keycode());
if (event.keycode() == 8 || event.keycode() == 46) {
// backspace, delete
if (selection_size_ > 0) {
Expand Down Expand Up @@ -67,6 +79,12 @@ void TextArea::OnKeyDown(const KeyboardInputEvent& event) {
if (selection_start_ < text_.size())
selection_start_++;
}
if (event.keycode() == 65 &&
(event.modifiers() & KeyboardInputEvent::kControl)) {
// Select all
selection_start_ = 0;
selection_size_ = text_.size();
}
SetNeedsDisplay(false);
}

Expand All @@ -92,7 +110,8 @@ void TextArea::UpdateLeftEdges(cairo_t* cr) {
char str[2] = { text_[i], '\0' };

if (text_[i] == '\n') {
left_edges_[i] = left_edge = 0.0;
left_edges_[i] = left_edge;
left_edge = 0.0;
continue;
}

Expand Down Expand Up @@ -138,16 +157,17 @@ void TextArea::UpdateLeftEdges(cairo_t* cr) {

string TextArea::DebugLeftEdges() {
string ret;
for (size_t i = 0; i < text_.size(); i++) {
for (size_t i = 0; i <= text_.size(); i++) {
char buf[20] = {0};
char str[2] = { text_[i], '\0' };
snprintf(buf, sizeof(buf), " %s(%.2f)", str, left_edges_[i]);
char str[2] = { i < text_.size() ? text_[i] : '$', '\0' };
snprintf(buf, sizeof(buf), " %s(%.2f)[%d]", str, left_edges_[i],
GetRowIndex(i));
ret += buf;
}
return ret;
}

string TextArea::GetLine(size_t start_idx) {
string TextArea::GetLine(size_t start_idx, size_t* out_advance) {
if (text_.size() + 1 != left_edges_.size()) {
printf("Size mismatch!\n");
return "";
Expand All @@ -161,12 +181,16 @@ string TextArea::GetLine(size_t start_idx) {
break;
}
}
if (out_advance)
*out_advance = ret.size();
if (!ret.empty() && *ret.rbegin() == '\n')
ret.resize(ret.size() - 1);
return ret;
}

size_t TextArea::GetRowIndex(size_t index) {
size_t ret = 0;
for (size_t i = 1; i < index; i++) {
for (size_t i = 1; i <= index; i++) {
if (left_edges_[i] <= left_edges_[i - 1])
ret++;
}
Expand All @@ -182,12 +206,6 @@ void TextArea::EraseSelection() {
}

void TextArea::Draw(cairo_t* cr) {
// draw rectangle for clarity
frame_.CairoRectangle(cr);
cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0); // opaque red
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);

stroke_color_.CairoSetSourceRGBA(cr);
cairo_select_font_face(cr, "serif",
CAIRO_FONT_SLANT_NORMAL,
Expand All @@ -200,17 +218,44 @@ void TextArea::Draw(cairo_t* cr) {
cursor.y_ += extents.ascent;

UpdateLeftEdges(cr);
//printf("DBG: %s\n", DebugLeftEdges().c_str());
printf("DBG: %s\n", DebugLeftEdges().c_str());
if (selection_size_) {
// Draw hilight for selection
cairo_save(cr);
for (size_t i = selection_start_; i < selection_size_; i++) {
Point letter_origin(left_edges_[i],
GetRowIndex(i) * extents.height);
double width = left_edges_[i + 1] - left_edges_[i];
if (width <= 0.0)
width = frame_.size_.width_ - left_edges_[i];
Rect box(letter_origin, Size(width, extents.height));
box = box.TranslatedBy(frame_.Left(), frame_.Top());
box.CairoRectangle(cr);
cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 0.2);
cairo_fill(cr);
}
cairo_restore(cr);
}

for (size_t index = 0; index < text_.size(); ) {
string line = GetLine(index);
if (line.empty())
break;
size_t advance = 0;
string line = GetLine(index, &advance);
printf("Line: [%s]. adv: %zu\n", line.c_str(), advance);
cursor.CairoMoveTo(cr);
cairo_show_text(cr, line.c_str());
//printf("DRAW:[%s]\n", line.c_str());
cursor.y_ += extents.height;
index += line.size();
index += advance;
if (advance == 0)
break;
}
frame_.size_.height_ =
(GetRowIndex(text_.size()) + 1) * extents.height;

// draw rectangle for clarity
frame_.CairoRectangle(cr);
cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.5); // translucent red
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);

// Draw cursor if editing
if (IsEditing() && selection_size_ == 0) {
Expand Down
2 changes: 1 addition & 1 deletion text_area.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TextArea : public Graphic {

private:
void UpdateLeftEdges(cairo_t* cr);
std::string GetLine(size_t start_idx);
std::string GetLine(size_t start_idx, size_t* out_advance);
std::string DebugLeftEdges();
size_t GetRowIndex(size_t index);

Expand Down
16 changes: 12 additions & 4 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,26 @@ class KeyboardInputEvent {
enum Type {
TEXT, DOWN, UP
};
KeyboardInputEvent(Type type, uint32_t keycode)
: type_(type), keycode_(keycode) {}
KeyboardInputEvent(Type type, std::string text)
: type_(type), keycode_(0), text_(text) {}
static const uint32_t kShift = 1 << 0;
static const uint32_t kControl = 1 << 1;
static const uint32_t kAlt = 1 << 2;
static const uint32_t kMeta = 1 << 3;
static const uint32_t kShortcutMask = kControl | kAlt | kMeta;
static const uint32_t kModifiersMask = 0xf;
KeyboardInputEvent(Type type, uint32_t keycode, uint32_t modifiers)
: type_(type), keycode_(keycode), modifiers_(modifiers) {}
KeyboardInputEvent(Type type, std::string text, uint32_t modifiers)
: type_(type), keycode_(0), text_(text), modifiers_(modifiers) {}
Type type() const { return type_; }
uint32_t keycode() const { return keycode_; }
const std::string& text() const { return text_; }
uint32_t modifiers() const { return modifiers_; }

private:
Type type_;
uint32_t keycode_;
std::string text_;
uint32_t modifiers_;
};

class ViewDelegate {
Expand Down

0 comments on commit c8bdd03

Please sign in to comment.