Files
haiku-beta6/src/apps/diskprobe/AttributeWindow.cpp
T
Axel Dörfler c7a76e9f26 Moved the attribute editors into a separate file.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6755 a95241bf-73f2-0310-859d-f6bbb57e9c96
2004-02-26 12:41:23 +00:00

265 lines
6.0 KiB
C++

/*
** Copyright 2004, Axel Dörfler, [email protected]. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
#include "AttributeWindow.h"
#include "AttributeEditors.h"
#include "ProbeView.h"
#include <MenuBar.h>
#include <MenuItem.h>
#include <TabView.h>
#include <StringView.h>
#include <Alert.h>
static const uint32 kMsgRemoveAttribute = 'rmat';
class EditorTabView : public BTabView {
public:
EditorTabView(BRect frame, const char *name, button_width width = B_WIDTH_AS_USUAL,
uint32 resizingMode = B_FOLLOW_ALL, uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS);
virtual void FrameResized(float width, float height);
virtual void Select(int32 tab);
void AddRawEditorTab(BView *view);
void SetTypeEditorTab(BView *view);
private:
BView *fRawEditorView;
BView *fTypeEditorView;
BStringView *fNoEditorView;
int32 fRawTab;
};
//-----------------
EditorTabView::EditorTabView(BRect frame, const char *name, button_width width,
uint32 resizingMode, uint32 flags)
: BTabView(frame, name, width, resizingMode, flags),
fRawEditorView(NULL),
fRawTab(-1)
{
ContainerView()->MoveBy(-ContainerView()->Frame().left,
TabHeight() + 1 - ContainerView()->Frame().top);
fNoEditorView = new BStringView(ContainerView()->Bounds(), "Type Editor",
"No type editor available", B_FOLLOW_NONE);
fNoEditorView->ResizeToPreferred();
fNoEditorView->SetAlignment(B_ALIGN_CENTER);
fTypeEditorView = fNoEditorView;
FrameResized(0, 0);
SetTypeEditorTab(NULL);
}
void
EditorTabView::FrameResized(float width, float height)
{
BRect rect = Bounds();
rect.top = ContainerView()->Frame().top;
ContainerView()->ResizeTo(rect.Width(), rect.Height());
BView *view = fTypeEditorView;
if (view == NULL)
view = fNoEditorView;
BPoint point = view->Frame().LeftTop();
if ((view->ResizingMode() & B_FOLLOW_RIGHT) == 0)
point.x = (rect.Width() - view->Bounds().Width()) / 2;
if ((view->ResizingMode() & B_FOLLOW_BOTTOM) == 0)
point.y = (rect.Height() - view->Bounds().Height()) / 2;
view->MoveTo(point);
}
void
EditorTabView::Select(int32 tab)
{
if (tab != fRawTab && fRawEditorView != NULL && !fRawEditorView->IsHidden(fRawEditorView))
fRawEditorView->Hide();
BTabView::Select(tab);
BView *view;
if (tab == fRawTab && fRawEditorView != NULL) {
if (fRawEditorView->IsHidden(fRawEditorView))
fRawEditorView->Show();
view = fRawEditorView;
} else
view = ViewForTab(Selection());
if (view != NULL && (view->ResizingMode() & (B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM)) != 0) {
BRect rect = ContainerView()->Bounds();
BRect frame = view->Frame();
rect.left = frame.left;
rect.top = frame.top;
if ((view->ResizingMode() & B_FOLLOW_RIGHT) == 0)
rect.right = frame.right;
if ((view->ResizingMode() & B_FOLLOW_BOTTOM) == 0)
rect.bottom = frame.bottom;
view->ResizeTo(rect.Width(), rect.Height());
}
}
void
EditorTabView::AddRawEditorTab(BView *view)
{
fRawEditorView = view;
if (view != NULL)
ContainerView()->AddChild(view);
fRawTab = CountTabs();
view = new BView(BRect(0, 0, 5, 5), "Raw Editor", B_FOLLOW_NONE, 0);
view->SetViewColor(ViewColor());
AddTab(view);
}
void
EditorTabView::SetTypeEditorTab(BView *view)
{
if (fTypeEditorView == view)
return;
BTab *tab = TabAt(0);
if (tab != NULL)
tab->SetView(NULL);
fTypeEditorView = view;
if (view == NULL)
view = fNoEditorView;
if (CountTabs() == 0)
AddTab(view);
else
tab->SetView(view);
FrameResized(0, 0);
Select(0);
}
// #pragma mark -
AttributeWindow::AttributeWindow(BRect rect, entry_ref *ref, const char *attribute,
const BMessage *settings)
: ProbeWindow(rect, ref),
fAttribute(strdup(attribute))
{
char buffer[256];
snprintf(buffer, sizeof(buffer), "%s: %s", ref->name, attribute);
SetTitle(buffer);
// add the menu
BMenuBar *menuBar = new BMenuBar(BRect(0, 0, 0, 0), NULL);
AddChild(menuBar);
BMenu *menu = new BMenu("Attribute");
menu->AddItem(new BMenuItem("Save", NULL, 'S', B_COMMAND_KEY));
menu->AddItem(new BMenuItem("Remove from File", new BMessage(kMsgRemoveAttribute)));
menu->AddSeparatorItem();
// the ProbeView file menu items will be inserted here
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem("Close", new BMessage(B_CLOSE_REQUESTED), 'W', B_COMMAND_KEY));
menu->SetTargetForItems(this);
menuBar->AddItem(menu);
// add our interface widgets
BRect rect = Bounds();
rect.top = menuBar->Bounds().Height() + 1;
BView *view = new BView(rect, "main", B_FOLLOW_ALL, 0);
view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
AddChild(view);
rect = view->Bounds();
rect.top += 3;
EditorTabView *tabView = new EditorTabView(rect, "tabView");
rect = tabView->ContainerView()->Bounds();
rect.top += 3;
fProbeView = new ProbeView(rect, ref, attribute, settings);
fProbeView->AddFileMenuItems(menu, menu->CountItems() - 2);
tabView->AddRawEditorTab(fProbeView);
view->AddChild(tabView);
BView *editor = GetTypeEditorFor(rect, fProbeView->Editor());
if (editor != NULL)
tabView->SetTypeEditorTab(editor);
else {
// show the raw editor if we don't have a specialised type editor
tabView->Select(1);
}
fProbeView->UpdateSizeLimits();
}
AttributeWindow::~AttributeWindow()
{
free(fAttribute);
}
void
AttributeWindow::MessageReceived(BMessage *message)
{
switch (message->what) {
case kMsgRemoveAttribute:
{
char buffer[1024];
snprintf(buffer, sizeof(buffer),
"Do you really want to remove the attribute \"%s\" from the file \"%s\"?\n\n"
"The contents of the attribute will get lost if you click on \"Remove\".",
fAttribute, Ref().name);
int32 chosen = (new BAlert("DiskProbe request",
buffer, "Remove", "Cancel", NULL,
B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go();
if (chosen == 0) {
BNode node(&Ref());
if (node.InitCheck() == B_OK)
node.RemoveAttr(fAttribute);
PostMessage(B_QUIT_REQUESTED);
}
break;
}
default:
ProbeWindow::MessageReceived(message);
break;
}
}
bool
AttributeWindow::Contains(const entry_ref &ref, const char *attribute)
{
return ref == Ref() && attribute != NULL && !strcmp(attribute, fAttribute);
}