Ticket #5420: Shortcuts-HIG-Update-Rev2.patch
File Shortcuts-HIG-Update-Rev2.patch, 17.4 KB (added by , 13 years ago) |
---|
-
src/preferences/shortcuts/ResizableButton.h
1 /*2 * Copyright 1999-2009 Haiku Inc. All rights reserved.3 * Distributed under the terms of the MIT License.4 *5 * Authors:6 * Jeremy Friesner7 */8 #ifndef ResizableButton_h9 #define ResizableButton_h10 11 12 #include <Button.h>13 14 15 class ResizableButton : public BButton {16 public:17 ResizableButton(BRect parentFrame, BRect frame,18 const char* name, const char* label,19 BMessage* message);20 21 virtual void ChangeToNewSize(float newWidth, float newHeight);22 private:23 BRect fPercentages;24 };25 26 27 #endif28 -
src/preferences/shortcuts/Jamfile
6 6 Preference Shortcuts : 7 7 main.cpp 8 8 MetaKeyStateMap.cpp 9 ResizableButton.cpp10 9 ShortcutsApp.cpp 11 10 ShortcutsSpec.cpp 12 11 ShortcutsWindow.cpp -
src/preferences/shortcuts/ShortcutsWindow.cpp
15 15 16 16 #include <Alert.h> 17 17 #include <Application.h> 18 #include <Button.h> 18 19 #include <Catalog.h> 19 20 #include <Clipboard.h> 21 #include <ControlLook.h> 20 22 #include <File.h> 23 #include <FilePanel.h> 21 24 #include <FindDirectory.h> 22 25 #include <Input.h> 23 26 #include <Locale.h> 27 #include <Message.h> 24 28 #include <Menu.h> 25 29 #include <MenuBar.h> 26 30 #include <MenuItem.h> 27 31 #include <MessageFilter.h> 28 32 #include <Path.h> 29 33 #include <PopUpMenu.h> 34 #include <Screen.h> 30 35 #include <ScrollBar.h> 31 36 #include <ScrollView.h> 32 37 #include <String.h> 38 #include <SupportDefs.h> 33 39 34 40 #include "ColumnListView.h" 35 41 … … 41 47 42 48 43 49 // Window sizing constraints 44 #define MIN_WIDTH 600 45 #define MIN_HEIGHT 130 46 #define MAX_WIDTH 65535 47 #define MAX_HEIGHT 65535 50 #define MAX_WIDTH 10000 51 #define MAX_HEIGHT 10000 52 // SetSizeLimits does not provide a mechanism for specifying an 53 // unrestricted maximum. 10,000 seems to be the most common value used 54 // in other Haiku system applications. 48 55 49 // Default window position 50 #define WINDOW_START_X 30 51 #define WINDOW_START_Y 100 56 #define WINDOW_SETTINGS_FILE_NAME "Shortcuts_window_settings" 57 // Because the "shortcuts_settings" file (SHORTCUTS_SETTING_FILE_NAME) is 58 // already used as a communications method between this configurator and 59 // the "shortcut_catcher" input_server filter, it should not be overloaded 60 // with window position information. Instead, a separate file is used. 52 61 53 62 #undef B_TRANSLATE_CONTEXT 54 63 #define B_TRANSLATE_CONTEXT "ShortcutsWindow" … … 56 65 #define ERROR "Shortcuts error" 57 66 #define WARNING "Shortcuts warning" 58 67 59 // Global constants for Shortcuts60 #define V_SPACING 5 // vertical spacing between GUI components61 68 62 63 69 // Creates a pop-up-menu that reflects the possible states of the specified 64 70 // meta-key. 65 71 static BPopUpMenu* … … 94 100 95 101 ShortcutsWindow::ShortcutsWindow() 96 102 : 97 BWindow(BRect(WINDOW_START_X, WINDOW_START_Y, WINDOW_START_X + MIN_WIDTH, 98 WINDOW_START_Y + MIN_HEIGHT * 2), B_TRANSLATE_SYSTEM_NAME("Shortcuts"), 99 B_DOCUMENT_WINDOW, 0L), 103 BWindow(BRect(0, 0, 0, 0), B_TRANSLATE_SYSTEM_NAME("Shortcuts"), 104 B_TITLED_WINDOW, 0L), 100 105 fSavePanel(NULL), 101 106 fOpenPanel(NULL), 102 107 fSelectPanel(NULL), … … 104 109 fLastOpenWasAppend(false) 105 110 { 106 111 ShortcutsSpec::InitializeMetaMaps(); 112 113 float spacing = be_control_look->DefaultItemSpacing(); 107 114 108 SetSizeLimits(MIN_WIDTH, MAX_WIDTH, MIN_HEIGHT, MAX_HEIGHT); 115 BView* top = new BView(Bounds(), NULL, B_FOLLOW_ALL_SIDES, 0); 116 top->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 117 AddChild(top); 118 109 119 BMenuBar* menuBar = new BMenuBar(BRect(0, 0, 0, 0), "Menu Bar"); 110 120 111 121 BMenu* fileMenu = new BMenu(B_TRANSLATE("File")); 112 fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Open KeySet" B_UTF8_ELLIPSIS), 122 fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Open KeySet" B_UTF8_ELLIPSIS), 113 123 new BMessage(OPEN_KEYSET), 'O')); 114 124 fileMenu->AddItem(new BMenuItem( 115 B_TRANSLATE("Append KeySet" B_UTF8_ELLIPSIS), 125 B_TRANSLATE("Append KeySet" B_UTF8_ELLIPSIS), 116 126 new BMessage(APPEND_KEYSET), 'A')); 117 fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Revert to saved"), 127 fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Revert to saved"), 118 128 new BMessage(REVERT_KEYSET), 'A')); 119 129 fileMenu->AddItem(new BSeparatorItem); 120 130 fileMenu->AddItem(new BMenuItem( 121 B_TRANSLATE("Save KeySet as" B_UTF8_ELLIPSIS), 131 B_TRANSLATE("Save KeySet as" B_UTF8_ELLIPSIS), 122 132 new BMessage(SAVE_KEYSET_AS), 'S')); 123 133 fileMenu->AddItem(new BSeparatorItem); 124 134 fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Quit"), 125 135 new BMessage(B_QUIT_REQUESTED), 'Q')); 126 136 menuBar->AddItem(fileMenu); 127 137 128 AddChild(menuBar);138 top->AddChild(menuBar); 129 139 130 font_height fh;131 be_plain_font->GetHeight(&fh);132 float vButtonHeight = ceil(fh.ascent) + ceil(fh.descent) + 5.0f;133 134 140 BRect tableBounds = Bounds(); 135 141 tableBounds.top = menuBar->Bounds().bottom + 1; 136 142 tableBounds.right -= B_V_SCROLL_BAR_WIDTH; 137 tableBounds.bottom -= (B_H_SCROLL_BAR_HEIGHT + V_SPACING + vButtonHeight + 138 V_SPACING * 2); 139 143 tableBounds.bottom -= B_H_SCROLL_BAR_HEIGHT; 144 140 145 BScrollView* containerView; 141 fColumnListView = new ColumnListView(tableBounds, &containerView, NULL, 142 B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE, 146 fColumnListView = new ColumnListView(tableBounds, &containerView, NULL, 147 B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE, 143 148 B_SINGLE_SELECTION_LIST, true, true, true, B_NO_BORDER); 144 145 fColumnListView->SetEditMessage(new BMessage(HOTKEY_ITEM_MODIFIED), 149 150 fColumnListView->SetEditMessage(new BMessage(HOTKEY_ITEM_MODIFIED), 146 151 BMessenger(this)); 147 148 const float metaWidth = 50.0f;149 152 153 float minListWidth = 0; 154 // A running total is kept as the columns are created. 155 float cellWidth = be_plain_font->StringWidth("Either") + 20; 156 // ShortcutsSpec does not seem to translate the string "Either". 157 150 158 for (int i = 0; i < ShortcutsSpec::NUM_META_COLUMNS; i++) { 159 const char* name = ShortcutsSpec::GetColumnName(i); 160 float headerWidth = be_plain_font->StringWidth(name) + 20; 161 float width = max_c(headerWidth, cellWidth); 162 minListWidth += width + 1; 163 151 164 fColumnListView->AddColumn( 152 new CLVColumn(ShortcutsSpec::GetColumnName(i), CreateMetaPopUp(i), 153 metaWidth, CLV_SORT_KEYABLE)); 165 new CLVColumn(name, CreateMetaPopUp(i), width, CLV_SORT_KEYABLE)); 154 166 } 155 167 168 float keyCellWidth = be_plain_font->StringWidth("Caps Lock") + 20; 156 169 fColumnListView->AddColumn(new CLVColumn(B_TRANSLATE("Key"), 157 CreateKeysPopUp(), 60, CLV_SORT_KEYABLE)); 170 CreateKeysPopUp(), keyCellWidth, CLV_SORT_KEYABLE)); 171 minListWidth += keyCellWidth + 1; 158 172 159 173 BPopUpMenu* popup = new BPopUpMenu(NULL, false); 160 174 popup->AddItem(new BMenuItem( … … 177 191 popup->AddItem(new BMenuItem(B_TRANSLATE("*Beep"), NULL)); 178 192 fColumnListView->AddColumn(new CLVColumn(B_TRANSLATE("Application"), popup, 179 193 323.0, CLV_SORT_KEYABLE)); 194 minListWidth += 323.0 + 1; 195 minListWidth += B_V_SCROLL_BAR_WIDTH; 180 196 181 197 fColumnListView->SetSortFunction(ShortcutsSpec::MyCompare); 182 AddChild(containerView);198 top->AddChild(containerView); 183 199 184 200 fColumnListView->SetSelectionMessage(new BMessage(HOTKEY_ITEM_SELECTED)); 185 201 fColumnListView->SetTarget(this); 186 202 187 BRect buttonBounds = Bounds(); 188 buttonBounds.left += V_SPACING; 189 buttonBounds.right = ((buttonBounds.right - buttonBounds.left) / 2.0f) 190 + buttonBounds.left; 191 buttonBounds.bottom -= V_SPACING * 2; 192 buttonBounds.top = buttonBounds.bottom - vButtonHeight; 193 buttonBounds.right -= B_V_SCROLL_BAR_WIDTH; 194 float origRight = buttonBounds.right; 195 buttonBounds.right = (buttonBounds.left + origRight) * 0.40f - 196 (V_SPACING / 2); 197 AddChild(fAddButton = new ResizableButton(Bounds(), buttonBounds, "add", 198 B_TRANSLATE("Add new shortcut"), new BMessage(ADD_HOTKEY_ITEM))); 199 buttonBounds.left = buttonBounds.right + V_SPACING; 200 buttonBounds.right = origRight; 201 AddChild(fRemoveButton = new ResizableButton(Bounds(), buttonBounds, 202 "remove", B_TRANSLATE("Remove selected shortcut"), 203 new BMessage(REMOVE_HOTKEY_ITEM))); 204 203 fAddButton = new BButton(BRect(0, 0, 0, 0), "add", 204 B_TRANSLATE("Add new shortcut"), new BMessage(ADD_HOTKEY_ITEM), 205 B_FOLLOW_BOTTOM); 206 fAddButton->ResizeToPreferred(); 207 fAddButton->MoveBy(spacing, 208 Bounds().bottom - fAddButton->Bounds().bottom - spacing); 209 top->AddChild(fAddButton); 210 211 fRemoveButton = new BButton(BRect(0, 0, 0, 0), "remove", 212 B_TRANSLATE("Remove selected shortcut"), 213 new BMessage(REMOVE_HOTKEY_ITEM), B_FOLLOW_BOTTOM); 214 fRemoveButton->ResizeToPreferred(); 215 fRemoveButton->MoveBy(fAddButton->Frame().right + spacing, 216 Bounds().bottom - fRemoveButton->Bounds().bottom - spacing); 217 top->AddChild(fRemoveButton); 218 205 219 fRemoveButton->SetEnabled(false); 206 220 207 float offset = (buttonBounds.right - buttonBounds.left) / 2.0f; 208 BRect saveButtonBounds = buttonBounds; 209 saveButtonBounds.right = Bounds().right - B_V_SCROLL_BAR_WIDTH - offset; 210 saveButtonBounds.left = buttonBounds.right + V_SPACING + offset; 211 AddChild(fSaveButton = new ResizableButton(Bounds(), saveButtonBounds, 212 "save", B_TRANSLATE("Save & apply"), new BMessage(SAVE_KEYSET))); 213 221 fSaveButton = new BButton(BRect(0, 0, 0, 0), "save", 222 B_TRANSLATE("Save & apply"), new BMessage(SAVE_KEYSET), 223 B_FOLLOW_BOTTOM | B_FOLLOW_RIGHT); 224 fSaveButton->ResizeToPreferred(); 225 fSaveButton->MoveBy(Bounds().right - fSaveButton->Bounds().right - spacing, 226 Bounds().bottom - fSaveButton->Bounds().bottom - spacing); 227 top->AddChild(fSaveButton); 228 214 229 fSaveButton->SetEnabled(false); 215 230 216 entry_ref ref; 217 if (_GetSettingsFile(&ref)) { 231 containerView->ResizeBy(0, 232 -(fAddButton->Bounds().bottom + 2 * spacing + 2)); 233 234 float minButtonBarWidth = fRemoveButton->Frame().right 235 + fSaveButton->Bounds().right + 2 * spacing; 236 float minWidth = max_c(minListWidth, minButtonBarWidth); 237 238 float menuBarHeight = menuBar->Bounds().bottom; 239 float buttonBarHeight = Bounds().bottom - containerView->Frame().bottom; 240 float minHeight = menuBarHeight + 200 + buttonBarHeight; 241 242 SetSizeLimits(minWidth, MAX_WIDTH, minHeight, MAX_HEIGHT); 243 // SetSizeLimits() will resize the window to the minimum size. 244 245 CenterOnScreen(); 246 247 entry_ref windowSettingsRef; 248 if (_GetWindowSettingsFile(&windowSettingsRef)) { 249 // The window settings file is not accepted via B_REFS_RECEIVED; this 250 // is a behind-the-scenes file that the user will never see or 251 // interact with. 252 BFile windowSettingsFile(&windowSettingsRef, B_READ_ONLY); 253 BMessage loadMsg; 254 if (loadMsg.Unflatten(&windowSettingsFile) == B_OK) 255 _LoadWindowSettings(loadMsg); 256 } 257 258 entry_ref keySetRef; 259 if (_GetSettingsFile(&keySetRef)) { 218 260 BMessage msg(B_REFS_RECEIVED); 219 msg.AddRef("refs", & ref);261 msg.AddRef("refs", &keySetRef); 220 262 msg.AddString("startupRef", "please"); 221 PostMessage(&msg); // Tell ourself to load this file if it exists. 263 PostMessage(&msg); 264 // Tell ourselves to load this file if it exists. 222 265 } 223 266 Show(); 224 267 } … … 270 313 } 271 314 } 272 315 273 if (ret) 316 if (ret) { 274 317 fColumnListView->DeselectAll(); 318 319 // Save the window position. 320 entry_ref ref; 321 if (_GetWindowSettingsFile(&ref)) { 322 BEntry entry(&ref); 323 _SaveWindowSettings(entry); 324 } 325 } 326 275 327 return ret; 276 328 } 277 329 … … 339 391 } 340 392 341 393 394 // Gets the filesystem location of the "Shortcuts_window_settings" file. 395 bool 396 ShortcutsWindow::_GetWindowSettingsFile(entry_ref* eref) 397 { 398 BPath path; 399 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) 400 return false; 401 else 402 path.Append(WINDOW_SETTINGS_FILE_NAME); 403 404 return BEntry(path.Path(), true).GetRef(eref) == B_OK; 405 } 406 407 408 // Saves the application settings file to (saveEntry). Because this is a 409 // non-essential file, errors are ignored when writing the settings. 410 void 411 ShortcutsWindow::_SaveWindowSettings(BEntry& saveEntry) 412 { 413 BFile saveTo(&saveEntry, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 414 if (saveTo.InitCheck() != B_OK) 415 return; 416 417 BMessage saveMsg; 418 saveMsg.AddRect("window frame", Frame()); 419 420 for (int i = 0; i < fColumnListView->CountColumns(); i++) { 421 CLVColumn* column = fColumnListView->ColumnAt(i); 422 saveMsg.AddFloat("column width", column->Width()); 423 } 424 425 saveMsg.Flatten(&saveTo); 426 } 427 428 429 // Loads the application settings file from (loadMsg) and resizes the interface 430 // to match the previously saved settings. Because this is a non-essential 431 // file, errors are ignored when loading the settings. 432 void 433 ShortcutsWindow::_LoadWindowSettings(const BMessage& loadMsg) 434 { 435 BRect frame; 436 if (loadMsg.FindRect("window frame", &frame) == B_OK) { 437 // Ensure the frame does not resize below the computed minimum. 438 float width = max_c(Bounds().right, frame.right - frame.left); 439 float height = max_c(Bounds().bottom, frame.bottom - frame.top); 440 ResizeTo(width, height); 441 442 // Ensure the frame is not placed outside of the screen. 443 BScreen screen(this); 444 float left = min_c(screen.Frame().right - width, frame.left); 445 float top = min_c(screen.Frame().bottom - height, frame.top); 446 MoveTo(left, top); 447 } 448 449 for (int i = 0; i < fColumnListView->CountColumns(); i++) { 450 CLVColumn* column = fColumnListView->ColumnAt(i); 451 float columnWidth; 452 if (loadMsg.FindFloat("column width", i, &columnWidth) == B_OK) 453 column->SetWidth(max_c(column->Width(), columnWidth)); 454 } 455 } 456 457 342 458 // Creates a new entry and adds it to the GUI. (defaultCommand) will be the 343 459 // text in the entry, or NULL if no text is desired. 344 460 void … … 660 776 661 777 662 778 void 663 ShortcutsWindow::FrameResized(float w, float h)664 {665 fAddButton->ChangeToNewSize(w, h);666 fRemoveButton->ChangeToNewSize(w, h);667 fSaveButton->ChangeToNewSize(w, h);668 }669 670 671 void672 779 ShortcutsWindow::DispatchMessage(BMessage* msg, BHandler* handler) 673 780 { 674 781 switch(msg->what) { -
src/preferences/shortcuts/ShortcutsSpec.cpp
360 360 if (text == NULL) 361 361 return; 362 362 363 _CacheViewFont(owner); 364 // Ensure that sViewFont is configured before using it to calculate 365 // widths. The lack of this call was causing the initial display of 366 // columns to be incorrect, with a "jump" as all the columns correct 367 // themselves upon the first column resize. 368 363 369 float textWidth = sViewFont.StringWidth(text); 364 370 BPoint point; 365 371 rgb_color lowColor = color; -
src/preferences/shortcuts/ShortcutsWindow.h
10 10 11 11 12 12 #include <Entry.h> 13 #include <FilePanel.h>14 #include <Message.h>15 #include <Point.h>16 13 #include <Window.h> 17 14 18 15 #include "ColumnListView.h" 19 #include "ResizableButton.h"20 16 17 class BButton; 18 class BFilePanel; 19 class BMessage; 21 20 22 21 // This class defines our preferences/configuration window. 23 22 class ShortcutsWindow : public BWindow { … … 27 26 28 27 virtual void DispatchMessage(BMessage* msg, BHandler* handler); 29 28 virtual void Quit(); 30 virtual void FrameResized(float w, float h); 31 virtual void MessageReceived(BMessage * msg); 29 virtual void MessageReceived(BMessage* msg); 32 30 virtual bool QuitRequested(); 33 31 34 32 // BMessage 'what' codes, representing commands understood by this Window. … … 57 55 void _AddNewSpec(const char* defaultCommand); 58 56 void _MarkKeySetModified(); 59 57 bool _LoadKeySet(const BMessage& loadMsg); 60 bool _SaveKeySet(BEntry 58 bool _SaveKeySet(BEntry& saveEntry); 61 59 bool _GetSettingsFile(entry_ref* ref); 60 void _LoadWindowSettings(const BMessage& loadMsg); 61 void _SaveWindowSettings(BEntry& saveEntry); 62 bool _GetWindowSettingsFile(entry_ref* ref); 62 63 63 ResizableButton*fAddButton;64 ResizableButton*fRemoveButton;65 ResizableButton*fSaveButton;64 BButton* fAddButton; 65 BButton* fRemoveButton; 66 BButton* fSaveButton; 66 67 ColumnListView* fColumnListView; 67 68 BFilePanel* fSavePanel; // for saving settings 68 69 BFilePanel* fOpenPanel; // for loading settings -
src/preferences/shortcuts/ResizableButton.cpp
1 /*2 * Copyright 1999-2009 Haiku Inc. All rights reserved.3 * Distributed under the terms of the MIT License.4 *5 * Authors:6 * Jeremy Friesner7 */8 9 10 #include "ResizableButton.h"11 12 13 ResizableButton::ResizableButton(BRect parentFrame, BRect frame,14 const char* name, const char* label, BMessage* message)15 :16 BButton(frame, name, label, message, B_FOLLOW_BOTTOM)17 {18 float width = parentFrame.right - parentFrame.left;19 float height = parentFrame.bottom - parentFrame.top;20 fPercentages.left = frame.left / width;21 fPercentages.top = frame.top / height;22 fPercentages.right = frame.right / width;23 fPercentages.bottom = frame.bottom / height;24 }25 26 27 void28 ResizableButton::ChangeToNewSize(float newWidth, float newHeight)29 {30 float newX = fPercentages.left* newWidth;31 float newW = (fPercentages.right* newWidth) - newX;32 BRect b = Frame();33 MoveBy(newX - b.left, 0);34 ResizeTo(newW, b.bottom - b.top);35 Invalidate();36 }37