2 * Copyright 2015, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
5 #include "VariableEditWindow.h"
8 #include <LayoutBuilder.h>
10 #include <StringView.h>
12 #include "AppMessageCodes.h"
13 #include "TableCellValueEditor.h"
15 #include "ValueNode.h"
19 MSG_VARIABLE_VALUE_CHANGED
= 'vavc'
23 VariableEditWindow::VariableEditWindow(Value
* initialValue
, ValueNode
* node
,
24 TableCellValueEditor
* editor
, BHandler
* target
)
26 BWindow(BRect(), "Edit value",
27 B_FLOATING_WINDOW
, B_AUTO_UPDATE_SIZE_LIMITS
| B_CLOSE_ON_ESCAPE
),
32 fInitialValue(initialValue
),
36 fNode
->AcquireReference();
37 fInitialValue
->AcquireReference();
38 fEditor
->AcquireReference();
39 fEditor
->AddListener(this);
43 VariableEditWindow::~VariableEditWindow()
45 fNode
->ReleaseReference();
46 fInitialValue
->ReleaseReference();
47 if (fNewValue
!= NULL
)
48 fNewValue
->ReleaseReference();
50 fEditor
->RemoveListener(this);
51 fEditor
->ReleaseReference();
56 VariableEditWindow::Create(Value
* initialValue
, ValueNode
* node
,
57 TableCellValueEditor
* editor
, BHandler
* target
)
59 VariableEditWindow
* self
= new VariableEditWindow(initialValue
, node
,
75 VariableEditWindow::_Init()
79 fInitialValue
->ToString(initialValue
);
80 label
.SetToFormat("Initial value for '%s': %s\n",
81 fNode
->Name().String(), initialValue
.String());
83 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
84 .SetInsets(B_USE_DEFAULT_SPACING
)
85 .AddGroup(B_HORIZONTAL
, 4.0f
)
86 .Add(new BStringView("initialLabel", label
))
88 .AddGroup(B_HORIZONTAL
, 4.0f
)
89 .Add(new BStringView("newLabel", "New value:"))
90 .Add(fEditor
->GetView())
92 .AddGroup(B_HORIZONTAL
, 4.0f
)
94 .Add((fCancelButton
= new BButton("Cancel",
95 new BMessage(B_QUIT_REQUESTED
))))
96 .Add((fSaveButton
= new BButton("Save",
97 new BMessage(MSG_WRITE_VARIABLE_VALUE
))))
100 fCancelButton
->SetTarget(this);
101 fSaveButton
->SetTarget(this);
102 fSaveButton
->MakeDefault(true);
103 fEditor
->GetView()->MakeFocus(true);
108 VariableEditWindow::Show()
116 VariableEditWindow::QuitRequested()
118 fEditor
->GetView()->RemoveSelf();
120 BMessenger
messenger(fTarget
);
121 messenger
.SendMessage(MSG_VARIABLE_EDIT_WINDOW_CLOSED
);
123 return BWindow::QuitRequested();
128 VariableEditWindow::MessageReceived(BMessage
* message
)
130 switch (message
->what
) {
131 case MSG_VARIABLE_VALUE_CHANGED
:
134 if (message
->FindPointer("value",
135 reinterpret_cast<void**>(&value
)) == B_OK
) {
136 if (fNewValue
!= NULL
)
137 fNewValue
->ReleaseReference();
143 case MSG_WRITE_VARIABLE_VALUE
:
145 BMessage
message(MSG_WRITE_VARIABLE_VALUE
);
146 message
.AddPointer("node", fNode
);
147 message
.AddPointer("value", fNewValue
);
149 // acquire a reference on behalf of the target
150 BReference
<Value
> valueReference(fNewValue
);
151 if (BMessenger(fTarget
).SendMessage(&message
) == B_OK
) {
152 valueReference
.Detach();
153 PostMessage(B_QUIT_REQUESTED
);
158 BWindow::MessageReceived(message
);
165 VariableEditWindow::TableCellEditBeginning()
171 VariableEditWindow::TableCellEditCancelled()
173 PostMessage(B_QUIT_REQUESTED
);
178 VariableEditWindow::TableCellEditEnded(Value
* newValue
)
180 BReference
<Value
> valueReference(newValue
);
181 BMessage
message(MSG_VARIABLE_VALUE_CHANGED
);
182 message
.AddPointer("value", newValue
);
183 if (PostMessage(&message
) == B_OK
)
184 valueReference
.Detach();