2 * Copyright 2004-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include "AttributeWindow.h"
9 #include "TypeEditors.h"
16 #include <Directory.h>
17 #include <GroupView.h>
18 #include <LayoutBuilder.h>
22 #include <StringView.h>
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "AttributeWindow"
31 static const uint32 kMsgRemoveAttribute
= 'rmat';
34 class EditorTabView
: public BTabView
{
36 EditorTabView(const char *name
,
37 button_width width
= B_WIDTH_FROM_WIDEST
,
38 uint32 flags
= B_WILL_DRAW
| B_FRAME_EVENTS
);
40 void AddRawEditorTab(BView
*view
);
41 void SetTypeEditorTab(BView
*view
);
48 EditorTabView::EditorTabView(const char *name
, button_width width
, uint32 flags
)
49 : BTabView(name
, width
, flags
)
51 SetBorder(B_NO_BORDER
);
56 EditorTabView::AddRawEditorTab(BView
*view
)
58 BTab
* tab
= new BTab(view
);
59 tab
->SetLabel(B_TRANSLATE("Raw editor"));
65 EditorTabView::SetTypeEditorTab(BView
*view
)
68 view
= new BStringView(B_TRANSLATE("Type editor"),
69 B_TRANSLATE("No type editor available"));
70 ((BStringView
*)view
)->SetAlignment(B_ALIGN_CENTER
);
73 BGroupView
* group
= new BGroupView(B_VERTICAL
);
74 BLayoutBuilder::Group
<>(group
, B_VERTICAL
)
75 .SetInsets(B_USE_WINDOW_SPACING
, 0, B_USE_WINDOW_SPACING
, 0)
79 group
->SetName(view
->Name());
89 AttributeWindow::AttributeWindow(BRect _rect
, entry_ref
*ref
,
90 const char *attribute
, const BMessage
*settings
)
91 : ProbeWindow(_rect
, ref
),
92 fAttribute(strdup(attribute
))
94 // Set alternative window title for devices
95 char name
[B_FILE_NAME_LENGTH
];
96 strlcpy(name
, ref
->name
, sizeof(name
));
99 if (entry
.IsDirectory()) {
100 BDirectory
directory(&entry
);
101 if (directory
.InitCheck() == B_OK
&& directory
.IsRootDirectory()) {
102 // use the volume name for root directories
104 if (directory
.GetVolume(&volume
) == B_OK
)
105 volume
.GetName(name
);
108 char buffer
[B_PATH_NAME_LENGTH
];
109 snprintf(buffer
, sizeof(buffer
), "%s: %s", name
, attribute
);
112 BGroupLayout
* layout
= new BGroupLayout(B_VERTICAL
, 0);
117 BMenuBar
*menuBar
= new BMenuBar("");
118 layout
->AddView(menuBar
, 0);
120 BMenu
*menu
= new BMenu(B_TRANSLATE("Attribute"));
122 // the ProbeView save menu items will be inserted here
123 menu
->AddItem(new BMenuItem(B_TRANSLATE("Remove from file"),
124 new BMessage(kMsgRemoveAttribute
)));
125 menu
->AddSeparatorItem();
127 // the ProbeView print menu items will be inserted here
128 menu
->AddSeparatorItem();
130 menu
->AddItem(new BMenuItem(B_TRANSLATE("Close"),
131 new BMessage(B_CLOSE_REQUESTED
), 'W', B_COMMAND_KEY
));
132 menu
->SetTargetForItems(this);
133 menuBar
->AddItem(menu
);
135 // add our interface widgets
137 EditorTabView
*tabView
= new EditorTabView("tabView");
138 layout
->AddView(tabView
, 999);
140 BRect rect
= tabView
->ContainerView()->Bounds();
143 fProbeView
= new ProbeView(ref
, attribute
, settings
);
144 fProbeView
->AddSaveMenuItems(menu
, 0);
145 fProbeView
->AddPrintMenuItems(menu
, menu
->CountItems() - 2);
147 fTypeEditorView
= GetTypeEditorFor(rect
, fProbeView
->Editor());
149 tabView
->SetTypeEditorTab(fTypeEditorView
);
150 tabView
->AddRawEditorTab(fProbeView
);
152 if (fTypeEditorView
== NULL
) {
153 // show the raw editor if we don't have a specialised type editor
159 AttributeWindow::~AttributeWindow()
166 AttributeWindow::MessageReceived(BMessage
*message
)
168 switch (message
->what
) {
169 case kMsgRemoveAttribute
:
173 snprintf(buffer
, sizeof(buffer
),
174 B_TRANSLATE("Do you really want to remove the attribute \"%s\" "
175 "from the file \"%s\"?\n\nYou cannot undo this action."),
176 fAttribute
, Ref().name
);
178 BAlert
* alert
= new BAlert(B_TRANSLATE("DiskProbe request"),
179 buffer
, B_TRANSLATE("Cancel"), B_TRANSLATE("Remove"), NULL
,
180 B_WIDTH_AS_USUAL
, B_WARNING_ALERT
);
181 alert
->SetShortcut(0, B_ESCAPE
);
182 int32 chosen
= alert
->Go();
186 if (node
.InitCheck() == B_OK
)
187 node
.RemoveAttr(fAttribute
);
189 PostMessage(B_QUIT_REQUESTED
);
195 ProbeWindow::MessageReceived(message
);
202 AttributeWindow::QuitRequested()
204 if (fTypeEditorView
!= NULL
)
205 fTypeEditorView
->CommitChanges();
207 bool quit
= fProbeView
->QuitRequested();
211 return ProbeWindow::QuitRequested();
216 AttributeWindow::Contains(const entry_ref
&ref
, const char *attribute
)
218 return ref
== Ref() && attribute
!= NULL
&& !strcmp(attribute
, fAttribute
);