1 /* 2 * Copyright 2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "FileTypes.h" 8 #include "FileTypesWindow.h" 9 #include "NewFileTypeWindow.h" 10 11 #include <Button.h> 12 #include <Catalog.h> 13 #include <ControlLook.h> 14 #include <GroupLayout.h> 15 #include <GridLayoutBuilder.h> 16 #include <Locale.h> 17 #include <MenuBar.h> 18 #include <MenuField.h> 19 #include <MenuItem.h> 20 #include <Mime.h> 21 #include <PopUpMenu.h> 22 #include <SpaceLayoutItem.h> 23 #include <String.h> 24 #include <StringView.h> 25 #include <TextControl.h> 26 27 #include <string.h> 28 29 30 #undef TR_CONTEXT 31 #define TR_CONTEXT "New File Type Window" 32 33 34 const uint32 kMsgSupertypeChosen = 'sptc'; 35 const uint32 kMsgNewSupertypeChosen = 'nstc'; 36 37 const uint32 kMsgNameUpdated = 'nmup'; 38 39 const uint32 kMsgAddType = 'atyp'; 40 41 42 NewFileTypeWindow::NewFileTypeWindow(FileTypesWindow* target, 43 const char* currentType) 44 : 45 BWindow(BRect(100, 100, 350, 200), TR("New file type"), B_MODAL_WINDOW, 46 B_NOT_ZOOMABLE | B_NOT_V_RESIZABLE | B_ASYNCHRONOUS_CONTROLS 47 | B_AUTO_UPDATE_SIZE_LIMITS ), 48 fTarget(target) 49 { 50 fSupertypesMenu = new BPopUpMenu("supertypes"); 51 BMenuItem* item; 52 BMessage types; 53 if (BMimeType::GetInstalledSupertypes(&types) == B_OK) { 54 const char* type; 55 int32 i = 0; 56 while (types.FindString("super_types", i++, &type) == B_OK) { 57 fSupertypesMenu->AddItem(item = new BMenuItem(type, 58 new BMessage(kMsgSupertypeChosen))); 59 60 // select super type close to the current type 61 if (currentType != NULL) { 62 if (!strncmp(type, currentType, strlen(type))) 63 item->SetMarked(true); 64 } else if (i == 1) 65 item->SetMarked(true); 66 } 67 68 if (i > 1) 69 fSupertypesMenu->AddSeparatorItem(); 70 } 71 72 fSupertypesMenu->AddItem(new BMenuItem(TR("Add new group"), 73 new BMessage(kMsgNewSupertypeChosen))); 74 BMenuField* typesMenuField = new BMenuField(NULL, fSupertypesMenu); 75 76 BStringView* typesMenuLabel = new BStringView(NULL, TR("Group:")); 77 // Create a separate label view, otherwise things don't line up right 78 typesMenuLabel->SetAlignment(B_ALIGN_LEFT); 79 typesMenuLabel->SetExplicitAlignment( 80 BAlignment(B_ALIGN_LEFT, B_ALIGN_USE_FULL_HEIGHT)); 81 82 fNameControl = new BTextControl(TR("Internal name:"), "", NULL); 83 fNameControl->SetModificationMessage(new BMessage(kMsgNameUpdated)); 84 85 // filter out invalid characters that can't be part of a MIME type name 86 BTextView* nameControlTextView = fNameControl->TextView(); 87 const char* disallowedCharacters = "/<>@,;:\"()[]?="; 88 for (int32 i = 0; disallowedCharacters[i]; i++) { 89 nameControlTextView->DisallowChar(disallowedCharacters[i]); 90 } 91 92 fAddButton = new BButton(TR("Add type"), new BMessage(kMsgAddType)); 93 94 float padding = 3.0f; 95 // if (be_control_look) 96 // padding = be_control_look->DefaultItemSpacing(); 97 98 SetLayout(new BGroupLayout(B_VERTICAL)); 99 AddChild(BGridLayoutBuilder(padding, padding) 100 .SetInsets(padding, padding, padding, padding) 101 .Add(typesMenuLabel, 0, 0) 102 .Add(typesMenuField, 1, 0, 2) 103 .Add(fNameControl->CreateLabelLayoutItem(), 0, 1) 104 .Add(fNameControl->CreateTextViewLayoutItem(), 1, 1, 2) 105 .Add(BSpaceLayoutItem::CreateGlue(), 0, 2) 106 .Add(new BButton(TR("Cancel"), new BMessage(B_QUIT_REQUESTED)), 1, 2) 107 .Add(fAddButton, 2, 2) 108 .SetColumnWeight(0, 3) 109 ); 110 111 BAlignment fullSize = BAlignment(B_ALIGN_USE_FULL_WIDTH, 112 B_ALIGN_USE_FULL_HEIGHT); 113 typesMenuField->MenuBar()->SetExplicitAlignment(fullSize); 114 fNameControl->TextView()->SetExplicitAlignment(fullSize); 115 116 BLayoutItem* nameControlLabelItem = fNameControl->CreateLabelLayoutItem(); 117 nameControlLabelItem->SetExplicitMinSize(nameControlLabelItem->MinSize()); 118 // stops fNameControl's label from truncating under certain conditions 119 120 fAddButton->MakeDefault(true); 121 fNameControl->MakeFocus(true); 122 123 target->PlaceSubWindow(this); 124 } 125 126 127 NewFileTypeWindow::~NewFileTypeWindow() 128 { 129 } 130 131 132 void 133 NewFileTypeWindow::MessageReceived(BMessage* message) 134 { 135 switch (message->what) { 136 case kMsgSupertypeChosen: 137 fAddButton->SetLabel(TR("Add type")); 138 fNameControl->SetLabel(TR("Internal name:")); 139 fNameControl->MakeFocus(true); 140 InvalidateLayout(true); 141 break; 142 143 case kMsgNewSupertypeChosen: 144 fAddButton->SetLabel(TR("Add group")); 145 fNameControl->SetLabel(TR("Group name:")); 146 fNameControl->MakeFocus(true); 147 InvalidateLayout(true); 148 break; 149 150 case kMsgNameUpdated: 151 { 152 bool empty = fNameControl->Text() == NULL 153 || fNameControl->Text()[0] == '\0'; 154 155 if (fAddButton->IsEnabled() == empty) 156 fAddButton->SetEnabled(!empty); 157 break; 158 } 159 160 case kMsgAddType: 161 { 162 BMenuItem* item = fSupertypesMenu->FindMarked(); 163 if (item != NULL) { 164 BString type; 165 if (fSupertypesMenu->IndexOf(item) 166 != fSupertypesMenu->CountItems() - 1) { 167 // add normal type 168 type = item->Label(); 169 type.Append("/"); 170 } 171 172 type.Append(fNameControl->Text()); 173 174 BMimeType mimeType(type.String()); 175 if (mimeType.IsInstalled()) { 176 error_alert(TR("This file type already exists")); 177 break; 178 } 179 180 status_t status = mimeType.Install(); 181 if (status != B_OK) 182 error_alert(TR("Could not install file type"), status); 183 else { 184 BMessage update(kMsgSelectNewType); 185 update.AddString("type", type.String()); 186 187 fTarget.SendMessage(&update); 188 } 189 } 190 PostMessage(B_QUIT_REQUESTED); 191 break; 192 } 193 194 default: 195 BWindow::MessageReceived(message); 196 break; 197 } 198 } 199 200 201 bool 202 NewFileTypeWindow::QuitRequested() 203 { 204 fTarget.SendMessage(kMsgNewTypeWindowClosed); 205 return true; 206 } 207 208 209