btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / debugger / user_interface / gui / utility_windows / VariableEditWindow.cpp
blob42312dc12e694b7a521f3629413f613cf38ff8b5
1 /*
2 * Copyright 2015, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
5 #include "VariableEditWindow.h"
7 #include <Button.h>
8 #include <LayoutBuilder.h>
9 #include <String.h>
10 #include <StringView.h>
12 #include "AppMessageCodes.h"
13 #include "TableCellValueEditor.h"
14 #include "Value.h"
15 #include "ValueNode.h"
18 enum {
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),
28 fCancelButton(NULL),
29 fSaveButton(NULL),
30 fTarget(target),
31 fNode(node),
32 fInitialValue(initialValue),
33 fNewValue(NULL),
34 fEditor(editor)
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();
55 VariableEditWindow*
56 VariableEditWindow::Create(Value* initialValue, ValueNode* node,
57 TableCellValueEditor* editor, BHandler* target)
59 VariableEditWindow* self = new VariableEditWindow(initialValue, node,
60 editor, target);
62 try {
63 self->_Init();
64 } catch (...) {
65 delete self;
66 throw;
69 return self;
74 void
75 VariableEditWindow::_Init()
77 BString label;
78 BString initialValue;
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))
87 .End()
88 .AddGroup(B_HORIZONTAL, 4.0f)
89 .Add(new BStringView("newLabel", "New value:"))
90 .Add(fEditor->GetView())
91 .End()
92 .AddGroup(B_HORIZONTAL, 4.0f)
93 .AddGlue()
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))))
98 .End();
100 fCancelButton->SetTarget(this);
101 fSaveButton->SetTarget(this);
102 fSaveButton->MakeDefault(true);
103 fEditor->GetView()->MakeFocus(true);
107 void
108 VariableEditWindow::Show()
110 CenterOnScreen();
111 BWindow::Show();
115 bool
116 VariableEditWindow::QuitRequested()
118 fEditor->GetView()->RemoveSelf();
120 BMessenger messenger(fTarget);
121 messenger.SendMessage(MSG_VARIABLE_EDIT_WINDOW_CLOSED);
123 return BWindow::QuitRequested();
127 void
128 VariableEditWindow::MessageReceived(BMessage* message)
130 switch (message->what) {
131 case MSG_VARIABLE_VALUE_CHANGED:
133 Value* value;
134 if (message->FindPointer("value",
135 reinterpret_cast<void**>(&value)) == B_OK) {
136 if (fNewValue != NULL)
137 fNewValue->ReleaseReference();
139 fNewValue = value;
141 break;
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);
155 break;
157 default:
158 BWindow::MessageReceived(message);
159 break;
164 void
165 VariableEditWindow::TableCellEditBeginning()
170 void
171 VariableEditWindow::TableCellEditCancelled()
173 PostMessage(B_QUIT_REQUESTED);
177 void
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();