1d11ec082SAxel Dörfler /* 2d11ec082SAxel Dörfler ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3d11ec082SAxel Dörfler ** Distributed under the terms of the OpenBeOS License. 4d11ec082SAxel Dörfler */ 5d11ec082SAxel Dörfler 6d11ec082SAxel Dörfler 7d11ec082SAxel Dörfler #include "AttributeWindow.h" 8c7a76e9fSAxel Dörfler #include "AttributeEditors.h" 9d11ec082SAxel Dörfler #include "ProbeView.h" 10d11ec082SAxel Dörfler 11d11ec082SAxel Dörfler #include <MenuBar.h> 12d11ec082SAxel Dörfler #include <MenuItem.h> 137d7f16acSAxel Dörfler #include <TabView.h> 147d7f16acSAxel Dörfler #include <StringView.h> 1536a79516SAxel Dörfler #include <Alert.h> 16d11ec082SAxel Dörfler 17d11ec082SAxel Dörfler 1836a79516SAxel Dörfler static const uint32 kMsgRemoveAttribute = 'rmat'; 197d7f16acSAxel Dörfler 207d7f16acSAxel Dörfler 217d7f16acSAxel Dörfler class EditorTabView : public BTabView { 227d7f16acSAxel Dörfler public: 237d7f16acSAxel Dörfler EditorTabView(BRect frame, const char *name, button_width width = B_WIDTH_AS_USUAL, 247d7f16acSAxel Dörfler uint32 resizingMode = B_FOLLOW_ALL, uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS); 257d7f16acSAxel Dörfler 267d7f16acSAxel Dörfler virtual void FrameResized(float width, float height); 277d7f16acSAxel Dörfler virtual void Select(int32 tab); 287d7f16acSAxel Dörfler 297d7f16acSAxel Dörfler void AddRawEditorTab(BView *view); 307d7f16acSAxel Dörfler void SetTypeEditorTab(BView *view); 317d7f16acSAxel Dörfler 327d7f16acSAxel Dörfler private: 337d7f16acSAxel Dörfler BView *fRawEditorView; 347d7f16acSAxel Dörfler BView *fTypeEditorView; 357d7f16acSAxel Dörfler BStringView *fNoEditorView; 367d7f16acSAxel Dörfler int32 fRawTab; 377d7f16acSAxel Dörfler }; 387d7f16acSAxel Dörfler 397d7f16acSAxel Dörfler 407d7f16acSAxel Dörfler //----------------- 417d7f16acSAxel Dörfler 427d7f16acSAxel Dörfler 437d7f16acSAxel Dörfler EditorTabView::EditorTabView(BRect frame, const char *name, button_width width, 447d7f16acSAxel Dörfler uint32 resizingMode, uint32 flags) 457d7f16acSAxel Dörfler : BTabView(frame, name, width, resizingMode, flags), 467d7f16acSAxel Dörfler fRawEditorView(NULL), 477d7f16acSAxel Dörfler fRawTab(-1) 487d7f16acSAxel Dörfler { 497d7f16acSAxel Dörfler ContainerView()->MoveBy(-ContainerView()->Frame().left, 507d7f16acSAxel Dörfler TabHeight() + 1 - ContainerView()->Frame().top); 517d7f16acSAxel Dörfler fNoEditorView = new BStringView(ContainerView()->Bounds(), "Type Editor", 527d7f16acSAxel Dörfler "No type editor available", B_FOLLOW_NONE); 537d7f16acSAxel Dörfler fNoEditorView->ResizeToPreferred(); 547d7f16acSAxel Dörfler fNoEditorView->SetAlignment(B_ALIGN_CENTER); 557d7f16acSAxel Dörfler fTypeEditorView = fNoEditorView; 567d7f16acSAxel Dörfler 577d7f16acSAxel Dörfler FrameResized(0, 0); 587d7f16acSAxel Dörfler 597d7f16acSAxel Dörfler SetTypeEditorTab(NULL); 607d7f16acSAxel Dörfler } 617d7f16acSAxel Dörfler 627d7f16acSAxel Dörfler 637d7f16acSAxel Dörfler void 647d7f16acSAxel Dörfler EditorTabView::FrameResized(float width, float height) 657d7f16acSAxel Dörfler { 667d7f16acSAxel Dörfler BRect rect = Bounds(); 677d7f16acSAxel Dörfler rect.top = ContainerView()->Frame().top; 687d7f16acSAxel Dörfler 697d7f16acSAxel Dörfler ContainerView()->ResizeTo(rect.Width(), rect.Height()); 707d7f16acSAxel Dörfler 717d7f16acSAxel Dörfler BView *view = fTypeEditorView; 727d7f16acSAxel Dörfler if (view == NULL) 737d7f16acSAxel Dörfler view = fNoEditorView; 747d7f16acSAxel Dörfler 757d7f16acSAxel Dörfler BPoint point = view->Frame().LeftTop(); 767d7f16acSAxel Dörfler if ((view->ResizingMode() & B_FOLLOW_RIGHT) == 0) 777d7f16acSAxel Dörfler point.x = (rect.Width() - view->Bounds().Width()) / 2; 787d7f16acSAxel Dörfler if ((view->ResizingMode() & B_FOLLOW_BOTTOM) == 0) 797d7f16acSAxel Dörfler point.y = (rect.Height() - view->Bounds().Height()) / 2; 807d7f16acSAxel Dörfler 817d7f16acSAxel Dörfler view->MoveTo(point); 827d7f16acSAxel Dörfler } 837d7f16acSAxel Dörfler 847d7f16acSAxel Dörfler 857d7f16acSAxel Dörfler void 867d7f16acSAxel Dörfler EditorTabView::Select(int32 tab) 877d7f16acSAxel Dörfler { 887d7f16acSAxel Dörfler if (tab != fRawTab && fRawEditorView != NULL && !fRawEditorView->IsHidden(fRawEditorView)) 897d7f16acSAxel Dörfler fRawEditorView->Hide(); 907d7f16acSAxel Dörfler 917d7f16acSAxel Dörfler BTabView::Select(tab); 927d7f16acSAxel Dörfler 937d7f16acSAxel Dörfler BView *view; 947d7f16acSAxel Dörfler if (tab == fRawTab && fRawEditorView != NULL) { 957d7f16acSAxel Dörfler if (fRawEditorView->IsHidden(fRawEditorView)) 967d7f16acSAxel Dörfler fRawEditorView->Show(); 977d7f16acSAxel Dörfler view = fRawEditorView; 987d7f16acSAxel Dörfler } else 997d7f16acSAxel Dörfler view = ViewForTab(Selection()); 1007d7f16acSAxel Dörfler 1017d7f16acSAxel Dörfler if (view != NULL && (view->ResizingMode() & (B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM)) != 0) { 1027d7f16acSAxel Dörfler BRect rect = ContainerView()->Bounds(); 1037d7f16acSAxel Dörfler 1047d7f16acSAxel Dörfler BRect frame = view->Frame(); 1057d7f16acSAxel Dörfler rect.left = frame.left; 1067d7f16acSAxel Dörfler rect.top = frame.top; 1077d7f16acSAxel Dörfler if ((view->ResizingMode() & B_FOLLOW_RIGHT) == 0) 1087d7f16acSAxel Dörfler rect.right = frame.right; 1097d7f16acSAxel Dörfler if ((view->ResizingMode() & B_FOLLOW_BOTTOM) == 0) 1107d7f16acSAxel Dörfler rect.bottom = frame.bottom; 1117d7f16acSAxel Dörfler 1127d7f16acSAxel Dörfler view->ResizeTo(rect.Width(), rect.Height()); 1137d7f16acSAxel Dörfler } 1147d7f16acSAxel Dörfler } 1157d7f16acSAxel Dörfler 1167d7f16acSAxel Dörfler 1177d7f16acSAxel Dörfler void 1187d7f16acSAxel Dörfler EditorTabView::AddRawEditorTab(BView *view) 1197d7f16acSAxel Dörfler { 1207d7f16acSAxel Dörfler fRawEditorView = view; 1217d7f16acSAxel Dörfler if (view != NULL) 1227d7f16acSAxel Dörfler ContainerView()->AddChild(view); 1237d7f16acSAxel Dörfler 1247d7f16acSAxel Dörfler fRawTab = CountTabs(); 1257d7f16acSAxel Dörfler 1267d7f16acSAxel Dörfler view = new BView(BRect(0, 0, 5, 5), "Raw Editor", B_FOLLOW_NONE, 0); 1277d7f16acSAxel Dörfler view->SetViewColor(ViewColor()); 1287d7f16acSAxel Dörfler AddTab(view); 1297d7f16acSAxel Dörfler } 1307d7f16acSAxel Dörfler 1317d7f16acSAxel Dörfler 1327d7f16acSAxel Dörfler void 1337d7f16acSAxel Dörfler EditorTabView::SetTypeEditorTab(BView *view) 1347d7f16acSAxel Dörfler { 1357d7f16acSAxel Dörfler if (fTypeEditorView == view) 1367d7f16acSAxel Dörfler return; 1377d7f16acSAxel Dörfler 1387d7f16acSAxel Dörfler BTab *tab = TabAt(0); 1397d7f16acSAxel Dörfler if (tab != NULL) 1407d7f16acSAxel Dörfler tab->SetView(NULL); 1417d7f16acSAxel Dörfler 1427d7f16acSAxel Dörfler fTypeEditorView = view; 1437d7f16acSAxel Dörfler 1447d7f16acSAxel Dörfler if (view == NULL) 1457d7f16acSAxel Dörfler view = fNoEditorView; 1467d7f16acSAxel Dörfler 1477d7f16acSAxel Dörfler if (CountTabs() == 0) 1487d7f16acSAxel Dörfler AddTab(view); 1497d7f16acSAxel Dörfler else 1507d7f16acSAxel Dörfler tab->SetView(view); 1517d7f16acSAxel Dörfler 1527d7f16acSAxel Dörfler FrameResized(0, 0); 153*871fa04fSAxel Dörfler 154d858695dSAxel Dörfler #ifdef COMPILE_FOR_R5 155*871fa04fSAxel Dörfler if (Window() != NULL) { 156*871fa04fSAxel Dörfler // With R5's BTabView, calling select without being 157*871fa04fSAxel Dörfler // attached to a window crashes... 1587d7f16acSAxel Dörfler Select(0); 1597d7f16acSAxel Dörfler } 160*871fa04fSAxel Dörfler #else 161*871fa04fSAxel Dörfler Select(0); 162*871fa04fSAxel Dörfler #endif 163*871fa04fSAxel Dörfler } 1647d7f16acSAxel Dörfler 1657d7f16acSAxel Dörfler 1667d7f16acSAxel Dörfler // #pragma mark - 1677d7f16acSAxel Dörfler 1687d7f16acSAxel Dörfler 1697fff3ed0SAxel Dörfler AttributeWindow::AttributeWindow(BRect rect, entry_ref *ref, const char *attribute, 1707fff3ed0SAxel Dörfler const BMessage *settings) 171d11ec082SAxel Dörfler : ProbeWindow(rect, ref), 172d11ec082SAxel Dörfler fAttribute(strdup(attribute)) 173d11ec082SAxel Dörfler { 174d11ec082SAxel Dörfler char buffer[256]; 175d11ec082SAxel Dörfler snprintf(buffer, sizeof(buffer), "%s: %s", ref->name, attribute); 176d11ec082SAxel Dörfler SetTitle(buffer); 177d11ec082SAxel Dörfler 178d11ec082SAxel Dörfler // add the menu 179d11ec082SAxel Dörfler 180d11ec082SAxel Dörfler BMenuBar *menuBar = new BMenuBar(BRect(0, 0, 0, 0), NULL); 181d11ec082SAxel Dörfler AddChild(menuBar); 182d11ec082SAxel Dörfler 183d11ec082SAxel Dörfler BMenu *menu = new BMenu("Attribute"); 184d11ec082SAxel Dörfler 1856781cbdcSAxel Dörfler // the ProbeView save menu items will be inserted here 18636a79516SAxel Dörfler menu->AddItem(new BMenuItem("Remove from File", new BMessage(kMsgRemoveAttribute))); 18736a79516SAxel Dörfler menu->AddSeparatorItem(); 18836a79516SAxel Dörfler 1896781cbdcSAxel Dörfler // the ProbeView print menu items will be inserted here 190d11ec082SAxel Dörfler menu->AddSeparatorItem(); 191d11ec082SAxel Dörfler 192d11ec082SAxel Dörfler menu->AddItem(new BMenuItem("Close", new BMessage(B_CLOSE_REQUESTED), 'W', B_COMMAND_KEY)); 193d11ec082SAxel Dörfler menu->SetTargetForItems(this); 194d11ec082SAxel Dörfler menuBar->AddItem(menu); 195d11ec082SAxel Dörfler 196d11ec082SAxel Dörfler // add our interface widgets 197d11ec082SAxel Dörfler 198d11ec082SAxel Dörfler BRect rect = Bounds(); 199d11ec082SAxel Dörfler rect.top = menuBar->Bounds().Height() + 1; 200d11ec082SAxel Dörfler 2017d7f16acSAxel Dörfler BView *view = new BView(rect, "main", B_FOLLOW_ALL, 0); 2027d7f16acSAxel Dörfler view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 2037d7f16acSAxel Dörfler AddChild(view); 2047d7f16acSAxel Dörfler 2057d7f16acSAxel Dörfler rect = view->Bounds(); 2067d7f16acSAxel Dörfler rect.top += 3; 2077d7f16acSAxel Dörfler 2087d7f16acSAxel Dörfler EditorTabView *tabView = new EditorTabView(rect, "tabView"); 2097d7f16acSAxel Dörfler 2107d7f16acSAxel Dörfler rect = tabView->ContainerView()->Bounds(); 2117d7f16acSAxel Dörfler rect.top += 3; 2127d7f16acSAxel Dörfler fProbeView = new ProbeView(rect, ref, attribute, settings); 2137d7f16acSAxel Dörfler tabView->AddRawEditorTab(fProbeView); 2147d7f16acSAxel Dörfler 2157d7f16acSAxel Dörfler view->AddChild(tabView); 2167d7f16acSAxel Dörfler 217fa78c88eSAxel Dörfler fTypeEditorView = GetTypeEditorFor(rect, fProbeView->Editor()); 218fa78c88eSAxel Dörfler if (fTypeEditorView != NULL) 219fa78c88eSAxel Dörfler tabView->SetTypeEditorTab(fTypeEditorView); 2207d7f16acSAxel Dörfler else { 2217d7f16acSAxel Dörfler // show the raw editor if we don't have a specialised type editor 2227d7f16acSAxel Dörfler tabView->Select(1); 2237d7f16acSAxel Dörfler } 2247d7f16acSAxel Dörfler 225c0ced84aSAxel Dörfler fProbeView->AddSaveMenuItems(menu, 0); 226c0ced84aSAxel Dörfler fProbeView->AddPrintMenuItems(menu, menu->CountItems() - 2); 227c0ced84aSAxel Dörfler 2287d7f16acSAxel Dörfler fProbeView->UpdateSizeLimits(); 229d11ec082SAxel Dörfler } 230d11ec082SAxel Dörfler 231d11ec082SAxel Dörfler 232d11ec082SAxel Dörfler AttributeWindow::~AttributeWindow() 233d11ec082SAxel Dörfler { 234d11ec082SAxel Dörfler free(fAttribute); 235d11ec082SAxel Dörfler } 236d11ec082SAxel Dörfler 237d11ec082SAxel Dörfler 23836a79516SAxel Dörfler void 23936a79516SAxel Dörfler AttributeWindow::MessageReceived(BMessage *message) 24036a79516SAxel Dörfler { 24136a79516SAxel Dörfler switch (message->what) { 24236a79516SAxel Dörfler case kMsgRemoveAttribute: 24336a79516SAxel Dörfler { 24436a79516SAxel Dörfler char buffer[1024]; 24536a79516SAxel Dörfler snprintf(buffer, sizeof(buffer), 24636a79516SAxel Dörfler "Do you really want to remove the attribute \"%s\" from the file \"%s\"?\n\n" 247768673c6SAxel Dörfler "You cannot undo this action.", 24836a79516SAxel Dörfler fAttribute, Ref().name); 24936a79516SAxel Dörfler 25036a79516SAxel Dörfler int32 chosen = (new BAlert("DiskProbe request", 251768673c6SAxel Dörfler buffer, "Cancel", "Remove", NULL, 25236a79516SAxel Dörfler B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go(); 253768673c6SAxel Dörfler if (chosen == 1) { 25436a79516SAxel Dörfler BNode node(&Ref()); 25536a79516SAxel Dörfler if (node.InitCheck() == B_OK) 25636a79516SAxel Dörfler node.RemoveAttr(fAttribute); 25736a79516SAxel Dörfler 25836a79516SAxel Dörfler PostMessage(B_QUIT_REQUESTED); 25936a79516SAxel Dörfler } 26036a79516SAxel Dörfler break; 26136a79516SAxel Dörfler } 26236a79516SAxel Dörfler 26336a79516SAxel Dörfler default: 26436a79516SAxel Dörfler ProbeWindow::MessageReceived(message); 26536a79516SAxel Dörfler break; 26636a79516SAxel Dörfler } 26736a79516SAxel Dörfler } 26836a79516SAxel Dörfler 26936a79516SAxel Dörfler 270d11ec082SAxel Dörfler bool 2710adaff85SAxel Dörfler AttributeWindow::QuitRequested() 2720adaff85SAxel Dörfler { 273c1381da9SAxel Dörfler if (fTypeEditorView != NULL) 274fa78c88eSAxel Dörfler fTypeEditorView->CommitChanges(); 275fa78c88eSAxel Dörfler 2760adaff85SAxel Dörfler bool quit = fProbeView->QuitRequested(); 2770adaff85SAxel Dörfler if (!quit) 2780adaff85SAxel Dörfler return false; 2790adaff85SAxel Dörfler 2800adaff85SAxel Dörfler return ProbeWindow::QuitRequested(); 2810adaff85SAxel Dörfler } 2820adaff85SAxel Dörfler 2830adaff85SAxel Dörfler 2840adaff85SAxel Dörfler bool 285d11ec082SAxel Dörfler AttributeWindow::Contains(const entry_ref &ref, const char *attribute) 286d11ec082SAxel Dörfler { 287d11ec082SAxel Dörfler return ref == Ref() && attribute != NULL && !strcmp(attribute, fAttribute); 288d11ec082SAxel Dörfler } 289d11ec082SAxel Dörfler 290