1 /*
2 * Copyright 2015, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
5
6 #include "TableCellTextControlEditor.h"
7
8 #include "Value.h"
9 #include "ValueFormatter.h"
10
11
12 enum {
13 MSG_INPUT_VALIDATION_NEEDED = 'ivne',
14 MSG_TEXT_VALUE_CHANGED = 'tevc'
15 };
16
17
TableCellTextControlEditor(::Value * initialValue,ValueFormatter * formatter)18 TableCellTextControlEditor::TableCellTextControlEditor(::Value* initialValue,
19 ValueFormatter* formatter)
20 :
21 TableCellFormattedValueEditor(initialValue, formatter),
22 BTextControl("", "", NULL)
23 {
24 }
25
26
~TableCellTextControlEditor()27 TableCellTextControlEditor::~TableCellTextControlEditor()
28 {
29 }
30
31
32 status_t
Init()33 TableCellTextControlEditor::Init()
34 {
35 BMessage* message = new(std::nothrow) BMessage(
36 MSG_INPUT_VALIDATION_NEEDED);
37 if (message == NULL)
38 return B_NO_MEMORY;
39
40 SetMessage(message);
41
42 message = new(std::nothrow) BMessage(MSG_TEXT_VALUE_CHANGED);
43 if (message == NULL)
44 return B_NO_MEMORY;
45
46 SetModificationMessage(message);
47
48 return B_OK;
49 }
50
51
52 BView*
GetView()53 TableCellTextControlEditor::GetView()
54 {
55 return this;
56 }
57
58
59 void
AttachedToWindow()60 TableCellTextControlEditor::AttachedToWindow()
61 {
62 BTextControl::AttachedToWindow();
63
64 SetTarget(this);
65
66 BString output;
67
68 if (GetValueFormatter()->FormatValue(InitialValue(), output) == B_OK)
69 SetText(output);
70
71 NotifyEditBeginning();
72 }
73
74
75 void
MessageReceived(BMessage * message)76 TableCellTextControlEditor::MessageReceived(BMessage* message)
77 {
78 switch (message->what) {
79 case MSG_TEXT_VALUE_CHANGED:
80 {
81 // TODO: highlight the input view in some way to show
82 // invalid inputs
83
84 // fall through
85 }
86 case MSG_INPUT_VALIDATION_NEEDED:
87 {
88 if (ValidateInput()) {
89 ::Value* value = NULL;
90 status_t error = GetValueForInput(value);
91 if (error != B_OK)
92 break;
93
94 BReference< ::Value> valueReference(value, true);
95 NotifyEditCompleted(value);
96 }
97 break;
98 }
99 default:
100 BTextControl::MessageReceived(message);
101 break;
102 }
103 }
104