Ticket #5420: Shortcuts-HIG-Update.patch

File Shortcuts-HIG-Update.patch, 30.0 KB (added by brianluft, 13 years ago)

Proposed patch to correct HIG compliance issues

  • 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 Friesner
    7  */
    8 #ifndef ResizableButton_h
    9 #define ResizableButton_h
    10 
    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 #endif
    28 
  • src/preferences/shortcuts/Jamfile

     
    66Preference Shortcuts :
    77    main.cpp
    88    MetaKeyStateMap.cpp
    9     ResizableButton.cpp
    109    ShortcutsApp.cpp
    1110    ShortcutsSpec.cpp
    1211    ShortcutsWindow.cpp
     
    1918    MouseWatcher.cpp
    2019    PrefilledBitmap.cpp
    2120    ScrollViewCorner.cpp
    22    
     21
    2322    : be $(HAIKU_LOCALE_LIBS) tracker libshortcuts_shared.a $(TARGET_LIBSTDC++)
    2423    : Shortcuts.rdef
    2524;
  • src/preferences/shortcuts/ShortcutsWindow.cpp

     
    1515
    1616#include <Alert.h>
    1717#include <Application.h>
     18#include <Button.h>
    1819#include <Catalog.h>
    1920#include <Clipboard.h>
    2021#include <File.h>
     22#include <FilePanel.h>
    2123#include <FindDirectory.h>
    2224#include <Input.h>
    2325#include <Locale.h>
     26#include <Message.h>
    2427#include <Menu.h>
    2528#include <MenuBar.h>
    2629#include <MenuItem.h>
    2730#include <MessageFilter.h>
    2831#include <Path.h>
    2932#include <PopUpMenu.h>
     33#include <Screen.h>
    3034#include <ScrollBar.h>
    3135#include <ScrollView.h>
    3236#include <String.h>
     37#include <SupportDefs.h>
    3338
    3439#include "ColumnListView.h"
    3540
     
    3944#include "ShortcutsFilterConstants.h"
    4045#include "ShortcutsSpec.h"
    4146
     47#define MAX_WIDTH 10000
     48#define MAX_HEIGHT 10000
     49    // SetSizeLimits does not provide a mechanism for specifying an
     50    // unrestricted maximum.  10,000 seems to be the most common value used
     51    // in other Haiku system applications.
     52#define SPACING 5
     53    // Vertical and horizontal spacing between GUI components.
    4254
    43 // Window sizing constraints
    44 #define MIN_WIDTH   600
    45 #define MIN_HEIGHT  130
    46 #define MAX_WIDTH   65535
    47 #define MAX_HEIGHT  65535
    48 
    49 // Default window position
    50 #define WINDOW_START_X 30
    51 #define WINDOW_START_Y 100
    52 
    5355#undef B_TRANSLATE_CONTEXT
    5456#define B_TRANSLATE_CONTEXT "ShortcutsWindow"
    5557
    5658#define ERROR "Shortcuts error"
    5759#define WARNING "Shortcuts warning"
    5860
    59 // Global constants for Shortcuts
    60 #define V_SPACING 5 // vertical spacing between GUI components
     61#define WINDOW_SETTINGS_FILE_NAME "Shortcuts_window_settings"
     62    // Because the "shortcuts_settings" file (SHORTCUTS_SETTING_FILE_NAME) is
     63    // already used as a communications method between this configurator and
     64    // the "shortcut_catcher" input_server filter, it should not be overloaded
     65    // with window position information.  Instead, a separate file is used.
    6166
    62 
    63 // Creates a pop-up-menu that reflects the possible states of the specified
     67// Creates a pop-up-menu that reflects the possible states of the specified
    6468// meta-key.
    6569static BPopUpMenu*
    6670CreateMetaPopUp(int col)
     
    6872    MetaKeyStateMap& map = GetNthKeyMap(col);
    6973    BPopUpMenu * popup = new BPopUpMenu(NULL, false);
    7074    int numStates = map.GetNumStates();
    71    
    72     for (int i = 0; i < numStates; i++) 
     75
     76    for (int i = 0; i < numStates; i++)
    7377        popup->AddItem(new BMenuItem(map.GetNthStateDesc(i), NULL));
    74    
     78
    7579    return popup;
    7680}
    7781
     
    8488    int numKeys = GetNumKeyIndices();
    8589    for (int i = 0; i < numKeys; i++) {
    8690        const char* next = GetKeyName(i);
    87        
    88         if (next) 
     91
     92        if (next)
    8993            popup->AddItem(new BMenuItem(next, NULL));
    9094    }
    9195    return popup;
     
    9498
    9599ShortcutsWindow::ShortcutsWindow()
    96100    :
    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),
    100     fSavePanel(NULL),
    101     fOpenPanel(NULL),
    102     fSelectPanel(NULL),
    103     fKeySetModified(false),         
     101    BWindow(BRect(0, 0, 0, 0), B_TRANSLATE_SYSTEM_NAME("Shortcuts"),
     102        B_TITLED_WINDOW, 0L),
     103    fSavePanel(NULL),
     104    fOpenPanel(NULL),
     105    fSelectPanel(NULL),
     106    fKeySetModified(false),
    104107    fLastOpenWasAppend(false)
    105108{
    106109    ShortcutsSpec::InitializeMetaMaps();
    107110
    108     SetSizeLimits(MIN_WIDTH, MAX_WIDTH, MIN_HEIGHT, MAX_HEIGHT);
     111    BView* top = new BView(Bounds(), NULL, B_FOLLOW_ALL_SIDES, 0);
     112    top->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     113    AddChild(top);
     114
    109115    BMenuBar* menuBar = new BMenuBar(BRect(0, 0, 0, 0), "Menu Bar");
    110116
    111117    BMenu* fileMenu = new BMenu(B_TRANSLATE("File"));
    112     fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Open KeySet" B_UTF8_ELLIPSIS), 
     118    fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Open KeySet" B_UTF8_ELLIPSIS),
    113119        new BMessage(OPEN_KEYSET), 'O'));
    114120    fileMenu->AddItem(new BMenuItem(
    115         B_TRANSLATE("Append KeySet" B_UTF8_ELLIPSIS), 
     121        B_TRANSLATE("Append KeySet" B_UTF8_ELLIPSIS),
    116122        new BMessage(APPEND_KEYSET), 'A'));
    117     fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Revert to saved"), 
     123    fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Revert to saved"),
    118124        new BMessage(REVERT_KEYSET), 'A'));
    119125    fileMenu->AddItem(new BSeparatorItem);
    120126    fileMenu->AddItem(new BMenuItem(
    121         B_TRANSLATE("Save KeySet as" B_UTF8_ELLIPSIS), 
     127        B_TRANSLATE("Save KeySet as" B_UTF8_ELLIPSIS),
    122128        new BMessage(SAVE_KEYSET_AS), 'S'));
    123129    fileMenu->AddItem(new BSeparatorItem);
    124130    fileMenu->AddItem(new BMenuItem(B_TRANSLATE("Quit"),
    125131        new BMessage(B_QUIT_REQUESTED), 'Q'));
    126132    menuBar->AddItem(fileMenu);
    127133
    128     AddChild(menuBar);
     134    top->AddChild(menuBar);
    129135
    130     font_height fh;
    131     be_plain_font->GetHeight(&fh);
    132     float vButtonHeight = ceil(fh.ascent) + ceil(fh.descent) + 5.0f;
    133 
    134136    BRect tableBounds = Bounds();
    135137    tableBounds.top = menuBar->Bounds().bottom + 1;
    136138    tableBounds.right -= B_V_SCROLL_BAR_WIDTH;
    137     tableBounds.bottom -= (B_H_SCROLL_BAR_HEIGHT + V_SPACING + vButtonHeight +
    138         V_SPACING * 2);
    139    
     139    tableBounds.bottom -= B_H_SCROLL_BAR_HEIGHT;
     140
    140141    BScrollView* containerView;
    141     fColumnListView = new ColumnListView(tableBounds, &containerView, NULL, 
    142         B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE, 
     142    fColumnListView = new ColumnListView(tableBounds, &containerView, NULL,
     143        B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE,
    143144        B_SINGLE_SELECTION_LIST, true, true, true, B_NO_BORDER);
    144    
    145     fColumnListView->SetEditMessage(new BMessage(HOTKEY_ITEM_MODIFIED), 
     145
     146    fColumnListView->SetEditMessage(new BMessage(HOTKEY_ITEM_MODIFIED),
    146147        BMessenger(this));
    147    
    148     const float metaWidth = 50.0f;
    149148
     149    float minListWidth = 0;
     150        // A running total is kept as the columns are created.
     151    float cellWidth = be_plain_font->StringWidth("Either") + 20;
     152        // ShortcutsSpec does not seem to translate the string "Either".
     153
    150154    for (int i = 0; i < ShortcutsSpec::NUM_META_COLUMNS; i++) {
     155        const char* name = ShortcutsSpec::GetColumnName(i);
     156        float headerWidth = be_plain_font->StringWidth(name) + 20;
     157        float width = max_c(headerWidth, cellWidth);
     158        minListWidth += width + 1;
     159
    151160        fColumnListView->AddColumn(
    152             new CLVColumn(ShortcutsSpec::GetColumnName(i), CreateMetaPopUp(i),
    153             metaWidth, CLV_SORT_KEYABLE));
     161            new CLVColumn(name, CreateMetaPopUp(i), width, CLV_SORT_KEYABLE));
    154162    }
    155163
     164    float keyCellWidth = be_plain_font->StringWidth("Caps Lock") + 20;
    156165    fColumnListView->AddColumn(new CLVColumn(B_TRANSLATE("Key"),
    157         CreateKeysPopUp(), 60, CLV_SORT_KEYABLE));
     166        CreateKeysPopUp(), keyCellWidth, CLV_SORT_KEYABLE));
     167    minListWidth += keyCellWidth + 1;
    158168
    159169    BPopUpMenu* popup = new BPopUpMenu(NULL, false);
    160170    popup->AddItem(new BMenuItem(
     
    177187    popup->AddItem(new BMenuItem(B_TRANSLATE("*Beep"), NULL));
    178188    fColumnListView->AddColumn(new CLVColumn(B_TRANSLATE("Application"), popup,
    179189        323.0, CLV_SORT_KEYABLE));
     190    minListWidth += 323.0 + 1;
     191    minListWidth += B_V_SCROLL_BAR_WIDTH;
    180192
    181193    fColumnListView->SetSortFunction(ShortcutsSpec::MyCompare);
    182     AddChild(containerView);
     194    top->AddChild(containerView);
    183195
    184196    fColumnListView->SetSelectionMessage(new BMessage(HOTKEY_ITEM_SELECTED));
    185197    fColumnListView->SetTarget(this);
    186198
    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    
     199    fAddButton = new BButton(BRect(0, 0, 0, 0), "add",
     200        B_TRANSLATE("Add new shortcut"), new BMessage(ADD_HOTKEY_ITEM),
     201        B_FOLLOW_BOTTOM);
     202    fAddButton->ResizeToPreferred();
     203    fAddButton->MoveBy(SPACING,
     204        Bounds().bottom - fAddButton->Bounds().bottom - SPACING);
     205    top->AddChild(fAddButton);
     206
     207    fRemoveButton = new BButton(BRect(0, 0, 0, 0), "remove",
     208        B_TRANSLATE("Remove selected shortcut"),
     209        new BMessage(REMOVE_HOTKEY_ITEM), B_FOLLOW_BOTTOM);
     210    fRemoveButton->ResizeToPreferred();
     211    fRemoveButton->MoveBy(fAddButton->Frame().right + SPACING,
     212        Bounds().bottom - fRemoveButton->Bounds().bottom - SPACING);
     213    top->AddChild(fRemoveButton);
     214
    205215    fRemoveButton->SetEnabled(false);
    206216
    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    
     217    fSaveButton = new BButton(BRect(0, 0, 0, 0), "save",
     218        B_TRANSLATE("Save & apply"), new BMessage(SAVE_KEYSET),
     219        B_FOLLOW_BOTTOM | B_FOLLOW_RIGHT);
     220    fSaveButton->ResizeToPreferred();
     221    fSaveButton->MoveBy(Bounds().right - fSaveButton->Bounds().right - SPACING,
     222        Bounds().bottom - fSaveButton->Bounds().bottom - SPACING);
     223    top->AddChild(fSaveButton);
     224
    214225    fSaveButton->SetEnabled(false);
    215226
    216     entry_ref ref; 
    217     if (_GetSettingsFile(&ref)) {
     227    containerView->ResizeBy(0,
     228        -(fAddButton->Bounds().bottom + 2 * SPACING + 2));
     229
     230    float minButtonBarWidth = fRemoveButton->Frame().right
     231        + fSaveButton->Bounds().right + 2 * SPACING;
     232    float minWidth = max_c(minListWidth, minButtonBarWidth);
     233
     234    float menuBarHeight = menuBar->Bounds().bottom;
     235    float buttonBarHeight = Bounds().bottom - containerView->Frame().bottom;
     236    float minHeight = menuBarHeight + 200 + buttonBarHeight;
     237
     238    SetSizeLimits(minWidth, MAX_WIDTH, minHeight, MAX_HEIGHT);
     239        // SetSizeLimits() will resize the window to the minimum size.
     240
     241    CenterOnScreen();
     242
     243    entry_ref windowSettingsRef;
     244    if (_GetWindowSettingsFile(&windowSettingsRef)) {
     245        // The window settings file is not accepted via B_REFS_RECEIVED; this
     246        // is a behind-the-scenes file that the user will never see or
     247        // interact with.
     248        BFile windowSettingsFile(&windowSettingsRef, B_READ_ONLY);
     249        BMessage loadMsg;
     250        if (loadMsg.Unflatten(&windowSettingsFile) == B_OK)
     251            _LoadWindowSettings(loadMsg);
     252    }
     253
     254    entry_ref keySetRef;
     255    if (_GetSettingsFile(&keySetRef)) {
    218256        BMessage msg(B_REFS_RECEIVED);
    219         msg.AddRef("refs", &ref);
     257        msg.AddRef("refs", &keySetRef);
    220258        msg.AddString("startupRef", "please");
    221         PostMessage(&msg); // Tell ourself to load this file if it exists.
     259        PostMessage(&msg);
     260            // Tell ourselves to load this file if it exists.
    222261    }
    223262    Show();
    224263}
     
    239278    bool ret = true;
    240279
    241280    if (fKeySetModified) {
    242         BAlert* alert = new BAlert(WARNING, 
     281        BAlert* alert = new BAlert(WARNING,
    243282            B_TRANSLATE("Really quit without saving your changes?"),
    244283            B_TRANSLATE("Don't save"), B_TRANSLATE("Cancel"),
    245284            B_TRANSLATE("Save"));
     
    253292                // up the file requester
    254293                if (fLastSaved.InitCheck() == B_OK) {
    255294                    if (_SaveKeySet(fLastSaved) == false) {
    256                         (new BAlert(ERROR, 
     295                        (new BAlert(ERROR,
    257296                            B_TRANSLATE("Shortcuts was unable to save your "
    258                                 "KeySet file!"), 
     297                                "KeySet file!"),
    259298                            B_TRANSLATE("Oh no")))->Go();
    260299                        ret = true; //quit anyway
    261300                    }
     
    270309        }
    271310    }
    272311
    273     if (ret)
     312    if (ret) {
    274313        fColumnListView->DeselectAll();
     314
     315        // Save the window position.
     316        entry_ref ref;
     317        if (_GetWindowSettingsFile(&ref)) {
     318            BEntry entry(&ref);
     319            _SaveWindowSettings(entry);
     320        }
     321    }
     322
    275323    return ret;
    276324}
    277325
     
    282330    BPath path;
    283331    if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
    284332        return false;
    285     else 
     333    else
    286334        path.Append(SHORTCUTS_SETTING_FILE_NAME);
    287335
    288336    if (BEntry(path.Path(), true).GetRef(eref) == B_OK)
     
    303351    BMessage saveMsg;
    304352    for (int i = 0; i < fColumnListView->CountItems(); i++) {
    305353        BMessage next;
    306         if (((ShortcutsSpec*)fColumnListView->ItemAt(i))->Archive(&next) 
     354        if (((ShortcutsSpec*)fColumnListView->ItemAt(i))->Archive(&next)
    307355            == B_OK)
    308356            saveMsg.AddMessage("spec", &next);
    309357        else
    310358            printf("Error archiving ShortcutsSpec #%i!\n", i);
    311359    }
    312    
     360
    313361    bool ret = (saveMsg.Flatten(&saveTo) == B_OK);
    314    
     362
    315363    if (ret) {
    316364        fKeySetModified = false;
    317365        fSaveButton->SetEnabled(false);
     
    321369}
    322370
    323371
    324 // Appends new entries from the file specified in the "spec" entry of 
     372// Appends new entries from the file specified in the "spec" entry of
    325373// (loadMsg). Returns true iff successful.
    326374bool
    327375ShortcutsWindow::_LoadKeySet(const BMessage& loadMsg)
     
    330378    BMessage msg;
    331379    while (loadMsg.FindMessage("spec", i++, &msg) == B_OK) {
    332380        ShortcutsSpec* spec = (ShortcutsSpec*)ShortcutsSpec::Instantiate(&msg);
    333         if (spec != NULL) 
     381        if (spec != NULL)
    334382            fColumnListView->AddItem(spec);
    335         else 
     383        else
    336384            printf("_LoadKeySet: Error parsing spec!\n");
    337385    }
    338386    return true;
    339387}
    340388
    341389
    342 // Creates a new entry and adds it to the GUI. (defaultCommand) will be the
     390bool
     391ShortcutsWindow::_GetWindowSettingsFile(entry_ref* eref)
     392{
     393    BPath path;
     394    if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
     395        return false;
     396    else
     397        path.Append(WINDOW_SETTINGS_FILE_NAME);
     398
     399    return BEntry(path.Path(), true).GetRef(eref) == B_OK;
     400}
     401
     402
     403// Saves the application settings file to (saveEntry).  Because this is a
     404// non-essential file, we ignore errors when writing the settings.
     405void
     406ShortcutsWindow::_SaveWindowSettings(BEntry& saveEntry)
     407{
     408    BFile saveTo(&saveEntry, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
     409    if (saveTo.InitCheck() != B_OK)
     410        return;
     411
     412    BMessage saveMsg;
     413    saveMsg.AddRect("window frame", Frame());
     414
     415    for (int i = 0; i < fColumnListView->CountColumns(); i++) {
     416        CLVColumn* column = fColumnListView->ColumnAt(i);
     417        saveMsg.AddFloat("column width", column->Width());
     418    }
     419
     420    saveMsg.Flatten(&saveTo);
     421}
     422
     423
     424// Loads the application settings file from (loadMsg) and resizes the interface
     425// to match the previously saved settings.  Because this is a non-essential
     426// file, we ignore errors when loading the settings.
     427void
     428ShortcutsWindow::_LoadWindowSettings(const BMessage& loadMsg)
     429{
     430    BRect frame;
     431    if (loadMsg.FindRect("window frame", &frame) == B_OK) {
     432        // Ensure the frame does not resize below the computed minimum.
     433        float width = max_c(Bounds().right, frame.right - frame.left);
     434        float height = max_c(Bounds().bottom, frame.bottom - frame.top);
     435        ResizeTo(width, height);
     436
     437        // Ensure the frame is not placed outside of the screen.
     438        BScreen screen(this);
     439        float left = min_c(screen.Frame().right - width, frame.left);
     440        float top = min_c(screen.Frame().bottom - height, frame.top);
     441        MoveTo(left, top);
     442    }
     443
     444    for (int i = 0; i < fColumnListView->CountColumns(); i++) {
     445        CLVColumn* column = fColumnListView->ColumnAt(i);
     446        float columnWidth;
     447        if (loadMsg.FindFloat("column width", i, &columnWidth) == B_OK)
     448            column->SetWidth(max_c(column->Width(), columnWidth));
     449    }
     450}
     451
     452
     453// Creates a new entry and adds it to the GUI. (defaultCommand) will be the
    343454// text in the entry, or NULL if no text is desired.
    344455void
    345456ShortcutsWindow::_AddNewSpec(const char* defaultCommand)
     
    354465
    355466        if (defaultCommand)
    356467            spec->SetCommand(defaultCommand);
    357     } else 
     468    } else
    358469        spec = new ShortcutsSpec(defaultCommand ? defaultCommand : "");
    359470
    360471    fColumnListView->AddItem(spec);
     
    367478ShortcutsWindow::MessageReceived(BMessage* msg)
    368479{
    369480    switch(msg->what) {
    370         case OPEN_KEYSET: 
    371         case APPEND_KEYSET: 
     481        case OPEN_KEYSET:
     482        case APPEND_KEYSET:
    372483            fLastOpenWasAppend = (msg->what == APPEND_KEYSET);
    373             if (fOpenPanel) 
     484            if (fOpenPanel)
    374485                fOpenPanel->Show();
    375486            else {
    376487                BMessenger m(this);
    377488                fOpenPanel = new BFilePanel(B_OPEN_PANEL, &m, NULL, 0, false);
    378489                fOpenPanel->Show();
    379490            }
    380             fOpenPanel->SetButtonLabel(B_DEFAULT_BUTTON, fLastOpenWasAppend ? 
     491            fOpenPanel->SetButtonLabel(B_DEFAULT_BUTTON, fLastOpenWasAppend ?
    381492                B_TRANSLATE("Append") : B_TRANSLATE("Open"));
    382493            break;
    383494
     
    404515                BEntry entry(&ref);
    405516                if (entry.InitCheck() == B_OK) {
    406517                    BPath path(&entry);
    407                    
     518
    408519                    if (path.InitCheck() == B_OK) {
    409520                        // Add a new item with the given path.
    410521                        BString str(path.Path());
     
    427538                BMessage fileMsg;
    428539                {
    429540                    BFile file(&ref, B_READ_ONLY);
    430                     if ((file.InitCheck() != B_OK) 
     541                    if ((file.InitCheck() != B_OK)
    431542                        || (fileMsg.Unflatten(&file) != B_OK)) {
    432543                        if (isStartMsg) {
    433544                            // use this to save to anyway
    434545                            fLastSaved = BEntry(&ref);
    435546                            break;
    436547                        } else {
    437                             (new BAlert(ERROR, 
     548                            (new BAlert(ERROR,
    438549                                B_TRANSLATE("Shortcuts was couldn't open your "
    439550                                "KeySet file!"), B_TRANSLATE("OK")))->Go(NULL);
    440551                            break;
    441552                        }
    442553                    }
    443554                }
    444  
     555
    445556                if (fLastOpenWasAppend == false) {
    446557                    // Clear the menu...
    447558                    while (ShortcutsSpec* item
     
    454565                    if (isStartMsg) fLastSaved = BEntry(&ref);
    455566                    fSaveButton->SetEnabled(isStartMsg == false);
    456567
    457                     // If we just loaded in the Shortcuts settings file, then 
     568                    // If we just loaded in the Shortcuts settings file, then
    458569                    // no need to tell the user to save on exit.
    459570                    entry_ref eref;
    460571                    _GetSettingsFile(&eref);
    461572                    if (ref == eref) fKeySetModified = false;
    462573                } else {
    463                     (new BAlert(ERROR, 
     574                    (new BAlert(ERROR,
    464575                        B_TRANSLATE("Shortcuts was unable to parse your "
    465                         "KeySet file!"), 
     576                        "KeySet file!"),
    466577                        B_TRANSLATE("OK")))->Go(NULL);
    467578                    break;
    468579                }
     
    480591                    BEntry ent(&aref);
    481592                    if (ent.InitCheck() == B_OK) {
    482593                        BPath path;
    483                         if ((ent.GetPath(&path) == B_OK) 
     594                        if ((ent.GetPath(&path) == B_OK)
    484595                            && (((ShortcutsSpec *)
    485596                            fColumnListView->ItemAt(csel))->
    486597                            ProcessColumnTextString(ShortcutsSpec::
    487598                            STRING_COLUMN_INDEX, path.Path()))) {
    488                            
     599
    489600                            fColumnListView->InvalidateItem(csel);
    490601                            _MarkKeySetModified();
    491602                        }
     
    501612
    502613            const char * name;
    503614            entry_ref entry;
    504             if ((msg->FindString("name", &name) == B_OK) 
     615            if ((msg->FindString("name", &name) == B_OK)
    505616                && (msg->FindRef("directory", &entry) == B_OK)) {
    506617                BDirectory dir(&entry);
    507618                BEntry saveTo(&dir, name, true);
    508                 showSaveError = ((saveTo.InitCheck() != B_OK) 
     619                showSaveError = ((saveTo.InitCheck() != B_OK)
    509620                || (_SaveKeySet(saveTo) == false));
    510621            } else if (fLastSaved.InitCheck() == B_OK) {
    511622                // We've saved this before, save over previous file.
     
    513624            } else PostMessage(SAVE_KEYSET_AS); // open the save requester...
    514625
    515626            if (showSaveError) {
    516                 (new BAlert(ERROR, 
    517                     B_TRANSLATE("Shortcuts wasn't able to save your keyset."), 
     627                (new BAlert(ERROR,
     628                    B_TRANSLATE("Shortcuts wasn't able to save your keyset."),
    518629                    B_TRANSLATE("OK")))->Go(NULL);
    519630            }
    520631            break;
     
    527638            else {
    528639                BMessage msg(SAVE_KEYSET);
    529640                BMessenger messenger(this);
    530                 fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger, NULL, 0, 
     641                fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger, NULL, 0,
    531642                    false, &msg);
    532643                fSavePanel->Show();
    533644            }
     
    542653        {
    543654            int index = fColumnListView->CurrentSelection();
    544655            if (index >= 0) {
    545                 CLVListItem* item = (CLVListItem*) 
     656                CLVListItem* item = (CLVListItem*)
    546657                    fColumnListView->ItemAt(index);
    547658                fColumnListView->RemoveItem(index);
    548659                delete item;
    549660                _MarkKeySetModified();
    550661
    551662                // Rules for new selection: If there is an item at (index),
    552                 // select it. Otherwise, if there is an item at (index-1), 
     663                // select it. Otherwise, if there is an item at (index-1),
    553664                // select it. Otherwise, select nothing.
    554665                int num = fColumnListView->CountItems();
    555666                if (num > 0) {
     
    558669                    else {
    559670                        if (index > 0)
    560671                            index--;
    561                         if (index < num) 
     672                        if (index < num)
    562673                            fColumnListView->Select(index);
    563674                    }
    564675                }
     
    581692        {
    582693            int32 row, column;
    583694
    584             if ((msg->FindInt32("row", &row) == B_OK) 
     695            if ((msg->FindInt32("row", &row) == B_OK)
    585696                && (msg->FindInt32("column", &column) == B_OK)) {
    586697                int32 key;
    587698                const char* bytes;
     
    595706                        repaintNeeded = item->ProcessColumnMouseClick(column);
    596707                    } else if ((msg->FindString("bytes", &bytes) == B_OK)
    597708                        && (msg->FindInt32("key", &key) == B_OK)) {
    598                         repaintNeeded = item->ProcessColumnKeyStroke(column, 
     709                        repaintNeeded = item->ProcessColumnKeyStroke(column,
    599710                            bytes, key);
    600                     } else if (msg->FindInt32("unmappedkey", &key) == 
     711                    } else if (msg->FindInt32("unmappedkey", &key) ==
    601712                        B_OK) {
    602                         repaintNeeded = ((column == item->KEY_COLUMN_INDEX) 
    603                             && ((key > 0xFF) || (GetKeyName(key) != NULL)) 
    604                             && (item->ProcessColumnKeyStroke(column, NULL, 
     713                        repaintNeeded = ((column == item->KEY_COLUMN_INDEX)
     714                            && ((key > 0xFF) || (GetKeyName(key) != NULL))
     715                            && (item->ProcessColumnKeyStroke(column, NULL,
    605716                            key)));
    606717                    } else if (msg->FindString("text", &bytes) == B_OK) {
    607718                        if ((bytes[0] == '(')&&(bytes[1] == 'C')) {
     
    614725                                    NULL, 0, false, &msg);
    615726                                fSelectPanel->Show();
    616727                            }
    617                             fSelectPanel->SetButtonLabel(B_DEFAULT_BUTTON, 
     728                            fSelectPanel->SetButtonLabel(B_DEFAULT_BUTTON,
    618729                                B_TRANSLATE("Select"));
    619730                        } else {
    620731                            repaintNeeded = item->ProcessColumnTextString(
    621732                                column, bytes);
    622733                        }
    623734                    }
    624                    
     735
    625736                    if (repaintNeeded) {
    626737                        fColumnListView->InvalidateItem(row);
    627738                        _MarkKeySetModified();
     
    660771
    661772
    662773void
    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 void
    672774ShortcutsWindow::DispatchMessage(BMessage* msg, BHandler* handler)
    673775{
    674776    switch(msg->what) {
    675777        case B_COPY:
    676778        case B_CUT:
    677779            if (be_clipboard->Lock()) {
    678                 int32 row = fColumnListView->CurrentSelection(); 
     780                int32 row = fColumnListView->CurrentSelection();
    679781                int32 column = fColumnListView->GetSelectedColumn();
    680                 if ((row >= 0) 
     782                if ((row >= 0)
    681783                    && (column == ShortcutsSpec::STRING_COLUMN_INDEX)) {
    682784                    ShortcutsSpec* spec = (ShortcutsSpec*)
    683785                        fColumnListView->ItemAt(row);
    684786                    if (spec) {
    685787                        BMessage* data = be_clipboard->Data();
    686788                        data->RemoveName("text/plain");
    687                         data->AddData("text/plain", B_MIME_TYPE, 
    688                             spec->GetCellText(column), 
     789                        data->AddData("text/plain", B_MIME_TYPE,
     790                            spec->GetCellText(column),
    689791                            strlen(spec->GetCellText(column)));
    690792                        be_clipboard->Commit();
    691                        
     793
    692794                        if (msg->what == B_CUT) {
    693795                            spec->ProcessColumnTextString(column, "");
    694796                            _MarkKeySetModified();
     
    705807                BMessage* data = be_clipboard->Data();
    706808                const char* text;
    707809                ssize_t textLen;
    708                 if (data->FindData("text/plain", B_MIME_TYPE, (const void**) 
     810                if (data->FindData("text/plain", B_MIME_TYPE, (const void**)
    709811                    &text, &textLen) == B_OK) {
    710                     int32 row = fColumnListView->CurrentSelection(); 
     812                    int32 row = fColumnListView->CurrentSelection();
    711813                    int32 column = fColumnListView->GetSelectedColumn();
    712                     if ((row >= 0) 
     814                    if ((row >= 0)
    713815                        && (column == ShortcutsSpec::STRING_COLUMN_INDEX)) {
    714                         ShortcutsSpec* spec = (ShortcutsSpec*) 
     816                        ShortcutsSpec* spec = (ShortcutsSpec*)
    715817                            fColumnListView->ItemAt(row);
    716818                        if (spec) {
    717819                            for (ssize_t i = 0; i < textLen; i++) {
     
    726828                be_clipboard->Unlock();
    727829            }
    728830            break;
    729        
     831
    730832        default:
    731833            BWindow::DispatchMessage(msg, handler);
    732834            break;
  • src/preferences/shortcuts/ShortcutsSpec.cpp

     
    360360    if (text == NULL)
    361361        return;
    362362
     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
    363369    float textWidth = sViewFont.StringWidth(text);
    364370    BPoint point;
    365371    rgb_color lowColor = color;
  • src/preferences/shortcuts/ShortcutsWindow.h

     
    1010
    1111
    1212#include <Entry.h>
    13 #include <FilePanel.h>
    14 #include <Message.h>
    15 #include <Point.h>
    1613#include <Window.h>
    1714
    1815#include "ColumnListView.h"
    19 #include "ResizableButton.h"
    2016
     17class BButton;
     18class BFilePanel;
     19class BMessage;
    2120
     21
    2222// This class defines our preferences/configuration window.
    2323class ShortcutsWindow : public BWindow {
    2424public:
     
    2727
    2828    virtual void            DispatchMessage(BMessage* msg, BHandler* handler);
    2929    virtual void            Quit();
    30     virtual void            FrameResized(float w, float h);
    31     virtual void            MessageReceived(BMessage * msg);
     30    virtual void            MessageReceived(BMessage* msg);
    3231    virtual bool            QuitRequested();
    3332
    3433    // BMessage 'what' codes, representing commands understood by this Window.
    3534    enum {
    3635        ADD_HOTKEY_ITEM = 'SpKy',   // Add a new hotkey entry to the GUI list.
    3736        REMOVE_HOTKEY_ITEM,         // Remove a hotkey entry from the GUI list.
    38         HOTKEY_ITEM_SELECTED,       // Give the "focus bar" to the specified 
     37        HOTKEY_ITEM_SELECTED,       // Give the "focus bar" to the specified
    3938                                    // entry.
    40         HOTKEY_ITEM_MODIFIED,       // Update the state of an entry to reflect 
     39        HOTKEY_ITEM_MODIFIED,       // Update the state of an entry to reflect
    4140                                    // user's changes.
    42         OPEN_KEYSET,                // Bring up a File requester to load new 
     41        OPEN_KEYSET,                // Bring up a File requester to load new
    4342                                    // settings.
    44         APPEND_KEYSET,              // Bring up a File requester to append 
     43        APPEND_KEYSET,              // Bring up a File requester to append
    4544                                    // settings.
    46         REVERT_KEYSET,              // Dump the current state and re-read 
     45        REVERT_KEYSET,              // Dump the current state and re-read
    4746                                    // settings from disk.
    4847        SAVE_KEYSET,                // Save the current settings to disk
    49         SAVE_KEYSET_AS,             // Bring up a File requester to save 
     48        SAVE_KEYSET_AS,             // Bring up a File requester to save
    5049                                    // current settings.
    51         SELECT_APPLICATION,         // Set the current entry to point to the 
     50        SELECT_APPLICATION,         // Set the current entry to point to the
    5251                                    // given file.
    5352    };
    5453private:
     
    5756            void                _AddNewSpec(const char* defaultCommand);
    5857            void                _MarkKeySetModified();
    5958            bool                _LoadKeySet(const BMessage& loadMsg);
    60             bool                _SaveKeySet(BEntry & saveEntry);
     59            bool                _SaveKeySet(BEntry& saveEntry);
    6160            bool                _GetSettingsFile(entry_ref* ref);
     61            void                _LoadWindowSettings(const BMessage& loadMsg);
     62            void                _SaveWindowSettings(BEntry& saveEntry);
     63            bool                _GetWindowSettingsFile(entry_ref* ref);
    6264
    63             ResizableButton*    fAddButton;
    64             ResizableButton*    fRemoveButton;
    65             ResizableButton*    fSaveButton;
     65            BButton*            fAddButton;
     66            BButton*            fRemoveButton;
     67            BButton*            fSaveButton;
    6668            ColumnListView*     fColumnListView;
    6769            BFilePanel*         fSavePanel;     // for saving settings
    6870            BFilePanel*         fOpenPanel;     // for loading settings
    6971            BFilePanel*         fSelectPanel;   // for selecting apps to launch
    70            
     72
    7173            // Points to the settings file to save to
    7274            BEntry              fLastSaved;
    73            
     75
    7476            // true iff changes were made since last load or save
    7577            bool                fKeySetModified;
    76            
     78
    7779            // true iff the file-requester's ref should be appended to current
    7880            bool                fLastOpenWasAppend;
    7981};
  • 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 Friesner
    7  */
    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 void
    28 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