1d11ec082SAxel Dörfler /*
2527b14e9SAxel Dörfler * Copyright 2004-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
386daa6f2SAxel Dörfler * Distributed under the terms of the MIT License.
4d11ec082SAxel Dörfler */
5d11ec082SAxel Dörfler
6d11ec082SAxel Dörfler
7d11ec082SAxel Dörfler #include "AttributeWindow.h"
8d11ec082SAxel Dörfler #include "ProbeView.h"
9527b14e9SAxel Dörfler #include "TypeEditors.h"
10d11ec082SAxel Dörfler
114fba3522SIngo Weinhold #include <stdio.h>
124fba3522SIngo Weinhold #include <stdlib.h>
134fba3522SIngo Weinhold
14a021189bSStephan Aßmus #include <Alert.h>
15a021189bSStephan Aßmus #include <Catalog.h>
16a021189bSStephan Aßmus #include <Directory.h>
17*a1e032c8SAdrien Destugues #include <GroupView.h>
18744a3927SHumdinger #include <LayoutBuilder.h>
19a021189bSStephan Aßmus #include <Locale.h>
20d11ec082SAxel Dörfler #include <MenuBar.h>
21d11ec082SAxel Dörfler #include <MenuItem.h>
227d7f16acSAxel Dörfler #include <StringView.h>
23a021189bSStephan Aßmus #include <TabView.h>
2486daa6f2SAxel Dörfler #include <Volume.h>
25d11ec082SAxel Dörfler
26d11ec082SAxel Dörfler
27546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
28546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "AttributeWindow"
29a021189bSStephan Aßmus
30a021189bSStephan Aßmus
3136a79516SAxel Dörfler static const uint32 kMsgRemoveAttribute = 'rmat';
327d7f16acSAxel Dörfler
337d7f16acSAxel Dörfler
347d7f16acSAxel Dörfler class EditorTabView : public BTabView {
357d7f16acSAxel Dörfler public:
36*a1e032c8SAdrien Destugues EditorTabView(const char *name,
37*a1e032c8SAdrien Destugues button_width width = B_WIDTH_FROM_WIDEST,
38a021189bSStephan Aßmus uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS);
397d7f16acSAxel Dörfler
407d7f16acSAxel Dörfler void AddRawEditorTab(BView *view);
417d7f16acSAxel Dörfler void SetTypeEditorTab(BView *view);
427d7f16acSAxel Dörfler };
437d7f16acSAxel Dörfler
447d7f16acSAxel Dörfler
457d7f16acSAxel Dörfler // -----------------
467d7f16acSAxel Dörfler
477d7f16acSAxel Dörfler
EditorTabView(const char * name,button_width width,uint32 flags)48*a1e032c8SAdrien Destugues EditorTabView::EditorTabView(const char *name, button_width width, uint32 flags)
49*a1e032c8SAdrien Destugues : BTabView(name, width, flags)
507d7f16acSAxel Dörfler {
51*a1e032c8SAdrien Destugues SetBorder(B_NO_BORDER);
527d7f16acSAxel Dörfler }
537d7f16acSAxel Dörfler
547d7f16acSAxel Dörfler
557d7f16acSAxel Dörfler void
AddRawEditorTab(BView * view)567d7f16acSAxel Dörfler EditorTabView::AddRawEditorTab(BView *view)
577d7f16acSAxel Dörfler {
58*a1e032c8SAdrien Destugues BTab* tab = new BTab(view);
59*a1e032c8SAdrien Destugues tab->SetLabel(B_TRANSLATE("Raw editor"));
60*a1e032c8SAdrien Destugues AddTab(view, tab);
617d7f16acSAxel Dörfler }
627d7f16acSAxel Dörfler
637d7f16acSAxel Dörfler
647d7f16acSAxel Dörfler void
SetTypeEditorTab(BView * view)657d7f16acSAxel Dörfler EditorTabView::SetTypeEditorTab(BView *view)
667d7f16acSAxel Dörfler {
67*a1e032c8SAdrien Destugues if (view == NULL) {
68*a1e032c8SAdrien Destugues view = new BStringView(B_TRANSLATE("Type editor"),
69*a1e032c8SAdrien Destugues B_TRANSLATE("No type editor available"));
70*a1e032c8SAdrien Destugues ((BStringView*)view)->SetAlignment(B_ALIGN_CENTER);
717d7f16acSAxel Dörfler }
72*a1e032c8SAdrien Destugues
73*a1e032c8SAdrien Destugues BGroupView* group = new BGroupView(B_VERTICAL);
74744a3927SHumdinger BLayoutBuilder::Group<>(group, B_VERTICAL)
75744a3927SHumdinger .SetInsets(B_USE_WINDOW_SPACING, 0, B_USE_WINDOW_SPACING, 0)
76744a3927SHumdinger .Add(view)
77744a3927SHumdinger .AddGlue(25.0f);
78*a1e032c8SAdrien Destugues
79*a1e032c8SAdrien Destugues group->SetName(view->Name());
80*a1e032c8SAdrien Destugues
81*a1e032c8SAdrien Destugues AddTab(group);
82871fa04fSAxel Dörfler Select(0);
83871fa04fSAxel Dörfler }
847d7f16acSAxel Dörfler
857d7f16acSAxel Dörfler
867d7f16acSAxel Dörfler // #pragma mark -
877d7f16acSAxel Dörfler
887d7f16acSAxel Dörfler
AttributeWindow(BRect _rect,entry_ref * ref,const char * attribute,const BMessage * settings)89*a1e032c8SAdrien Destugues AttributeWindow::AttributeWindow(BRect _rect, entry_ref *ref,
90*a1e032c8SAdrien Destugues const char *attribute, const BMessage *settings)
91758b1d0eSIngo Weinhold : ProbeWindow(_rect, ref),
92d11ec082SAxel Dörfler fAttribute(strdup(attribute))
93d11ec082SAxel Dörfler {
9486daa6f2SAxel Dörfler // Set alternative window title for devices
9586daa6f2SAxel Dörfler char name[B_FILE_NAME_LENGTH];
9686daa6f2SAxel Dörfler strlcpy(name, ref->name, sizeof(name));
9786daa6f2SAxel Dörfler
9886daa6f2SAxel Dörfler BEntry entry(ref);
9986daa6f2SAxel Dörfler if (entry.IsDirectory()) {
10086daa6f2SAxel Dörfler BDirectory directory(&entry);
10186daa6f2SAxel Dörfler if (directory.InitCheck() == B_OK && directory.IsRootDirectory()) {
10286daa6f2SAxel Dörfler // use the volume name for root directories
10386daa6f2SAxel Dörfler BVolume volume;
10486daa6f2SAxel Dörfler if (directory.GetVolume(&volume) == B_OK)
10586daa6f2SAxel Dörfler volume.GetName(name);
10686daa6f2SAxel Dörfler }
10786daa6f2SAxel Dörfler }
10886daa6f2SAxel Dörfler char buffer[B_PATH_NAME_LENGTH];
10986daa6f2SAxel Dörfler snprintf(buffer, sizeof(buffer), "%s: %s", name, attribute);
110d11ec082SAxel Dörfler SetTitle(buffer);
111d11ec082SAxel Dörfler
112*a1e032c8SAdrien Destugues BGroupLayout* layout = new BGroupLayout(B_VERTICAL, 0);
113*a1e032c8SAdrien Destugues SetLayout(layout);
114*a1e032c8SAdrien Destugues
115d11ec082SAxel Dörfler // add the menu
116d11ec082SAxel Dörfler
117*a1e032c8SAdrien Destugues BMenuBar *menuBar = new BMenuBar("");
118*a1e032c8SAdrien Destugues layout->AddView(menuBar, 0);
119d11ec082SAxel Dörfler
120a021189bSStephan Aßmus BMenu *menu = new BMenu(B_TRANSLATE("Attribute"));
121d11ec082SAxel Dörfler
1226781cbdcSAxel Dörfler // the ProbeView save menu items will be inserted here
123a021189bSStephan Aßmus menu->AddItem(new BMenuItem(B_TRANSLATE("Remove from file"),
124a021189bSStephan Aßmus new BMessage(kMsgRemoveAttribute)));
12536a79516SAxel Dörfler menu->AddSeparatorItem();
12636a79516SAxel Dörfler
1276781cbdcSAxel Dörfler // the ProbeView print menu items will be inserted here
128d11ec082SAxel Dörfler menu->AddSeparatorItem();
129d11ec082SAxel Dörfler
130*a1e032c8SAdrien Destugues menu->AddItem(new BMenuItem(B_TRANSLATE("Close"),
131*a1e032c8SAdrien Destugues new BMessage(B_CLOSE_REQUESTED), 'W', B_COMMAND_KEY));
132d11ec082SAxel Dörfler menu->SetTargetForItems(this);
133d11ec082SAxel Dörfler menuBar->AddItem(menu);
134d11ec082SAxel Dörfler
135d11ec082SAxel Dörfler // add our interface widgets
136d11ec082SAxel Dörfler
137*a1e032c8SAdrien Destugues EditorTabView *tabView = new EditorTabView("tabView");
138*a1e032c8SAdrien Destugues layout->AddView(tabView, 999);
139d11ec082SAxel Dörfler
140*a1e032c8SAdrien Destugues BRect rect = tabView->ContainerView()->Bounds();
1417d7f16acSAxel Dörfler rect.top += 3;
1427d7f16acSAxel Dörfler
143*a1e032c8SAdrien Destugues fProbeView = new ProbeView(ref, attribute, settings);
144c0ced84aSAxel Dörfler fProbeView->AddSaveMenuItems(menu, 0);
145c0ced84aSAxel Dörfler fProbeView->AddPrintMenuItems(menu, menu->CountItems() - 2);
146c0ced84aSAxel Dörfler
147*a1e032c8SAdrien Destugues fTypeEditorView = GetTypeEditorFor(rect, fProbeView->Editor());
148*a1e032c8SAdrien Destugues
149*a1e032c8SAdrien Destugues tabView->SetTypeEditorTab(fTypeEditorView);
150*a1e032c8SAdrien Destugues tabView->AddRawEditorTab(fProbeView);
151*a1e032c8SAdrien Destugues
152*a1e032c8SAdrien Destugues if (fTypeEditorView == NULL) {
153*a1e032c8SAdrien Destugues // show the raw editor if we don't have a specialised type editor
154*a1e032c8SAdrien Destugues tabView->Select(1);
155*a1e032c8SAdrien Destugues }
156d11ec082SAxel Dörfler }
157d11ec082SAxel Dörfler
158d11ec082SAxel Dörfler
~AttributeWindow()159d11ec082SAxel Dörfler AttributeWindow::~AttributeWindow()
160d11ec082SAxel Dörfler {
161d11ec082SAxel Dörfler free(fAttribute);
162d11ec082SAxel Dörfler }
163d11ec082SAxel Dörfler
164d11ec082SAxel Dörfler
16536a79516SAxel Dörfler void
MessageReceived(BMessage * message)16636a79516SAxel Dörfler AttributeWindow::MessageReceived(BMessage *message)
16736a79516SAxel Dörfler {
16836a79516SAxel Dörfler switch (message->what) {
16936a79516SAxel Dörfler case kMsgRemoveAttribute:
17036a79516SAxel Dörfler {
17136a79516SAxel Dörfler char buffer[1024];
172aed35104SHumdinger
17336a79516SAxel Dörfler snprintf(buffer, sizeof(buffer),
174*a1e032c8SAdrien Destugues B_TRANSLATE("Do you really want to remove the attribute \"%s\" "
175*a1e032c8SAdrien Destugues "from the file \"%s\"?\n\nYou cannot undo this action."),
17636a79516SAxel Dörfler fAttribute, Ref().name);
17736a79516SAxel Dörfler
178aed35104SHumdinger BAlert* alert = new BAlert(B_TRANSLATE("DiskProbe request"),
179a021189bSStephan Aßmus buffer, B_TRANSLATE("Cancel"), B_TRANSLATE("Remove"), NULL,
180aed35104SHumdinger B_WIDTH_AS_USUAL, B_WARNING_ALERT);
181aed35104SHumdinger alert->SetShortcut(0, B_ESCAPE);
182aed35104SHumdinger int32 chosen = alert->Go();
183aed35104SHumdinger
184768673c6SAxel Dörfler if (chosen == 1) {
18536a79516SAxel Dörfler BNode node(&Ref());
18636a79516SAxel Dörfler if (node.InitCheck() == B_OK)
18736a79516SAxel Dörfler node.RemoveAttr(fAttribute);
18836a79516SAxel Dörfler
18936a79516SAxel Dörfler PostMessage(B_QUIT_REQUESTED);
19036a79516SAxel Dörfler }
19136a79516SAxel Dörfler break;
19236a79516SAxel Dörfler }
19336a79516SAxel Dörfler
19436a79516SAxel Dörfler default:
19536a79516SAxel Dörfler ProbeWindow::MessageReceived(message);
19636a79516SAxel Dörfler break;
19736a79516SAxel Dörfler }
19836a79516SAxel Dörfler }
19936a79516SAxel Dörfler
20036a79516SAxel Dörfler
201d11ec082SAxel Dörfler bool
QuitRequested()2020adaff85SAxel Dörfler AttributeWindow::QuitRequested()
2030adaff85SAxel Dörfler {
204c1381da9SAxel Dörfler if (fTypeEditorView != NULL)
205fa78c88eSAxel Dörfler fTypeEditorView->CommitChanges();
206fa78c88eSAxel Dörfler
2070adaff85SAxel Dörfler bool quit = fProbeView->QuitRequested();
2080adaff85SAxel Dörfler if (!quit)
2090adaff85SAxel Dörfler return false;
2100adaff85SAxel Dörfler
2110adaff85SAxel Dörfler return ProbeWindow::QuitRequested();
2120adaff85SAxel Dörfler }
2130adaff85SAxel Dörfler
2140adaff85SAxel Dörfler
2150adaff85SAxel Dörfler bool
Contains(const entry_ref & ref,const char * attribute)216d11ec082SAxel Dörfler AttributeWindow::Contains(const entry_ref &ref, const char *attribute)
217d11ec082SAxel Dörfler {
218d11ec082SAxel Dörfler return ref == Ref() && attribute != NULL && !strcmp(attribute, fAttribute);
219d11ec082SAxel Dörfler }
220d11ec082SAxel Dörfler
221