diff --git a/src/preferences/keymap/Keymap.cpp b/src/preferences/keymap/Keymap.cpp index da146a9f54..b30bb8e61f 100644 --- a/src/preferences/keymap/Keymap.cpp +++ b/src/preferences/keymap/Keymap.cpp @@ -154,6 +154,14 @@ Keymap::Save(entry_ref &ref) } +bool Keymap::Equals(const Keymap& map) const +{ + // not really efficient but this is the only way i found + // to reliably compare keymaps (used only for apply and revert) + return memcmp( &map.fKeys, &fKeys, sizeof(key_map)) == 0; +} + + /* we need to know if a key is a modifier key to choose a valid key when several are pressed together */ diff --git a/src/preferences/keymap/Keymap.h b/src/preferences/keymap/Keymap.h index 1b98c1990e..a3bd0ca19c 100644 --- a/src/preferences/keymap/Keymap.h +++ b/src/preferences/keymap/Keymap.h @@ -23,6 +23,8 @@ public: bool IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey); void GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** chars, int32* numBytes); status_t Use(); + bool Equals(const Keymap& map) const; + private: char *fChars; key_map fKeys; diff --git a/src/preferences/keymap/KeymapWindow.cpp b/src/preferences/keymap/KeymapWindow.cpp index 8df65fcbb5..862853c0f3 100644 --- a/src/preferences/keymap/KeymapWindow.cpp +++ b/src/preferences/keymap/KeymapWindow.cpp @@ -50,6 +50,8 @@ KeymapWindow::KeymapWindow() : BWindow(BRect(80, 25, 692, 281), "Keymap", B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS ) { + fFirstTime = true; + // Add the menu bar BMenuBar *menubar = AddMenuBar(); @@ -81,6 +83,7 @@ KeymapWindow::KeymapWindow() fRevertButton = new BButton(BRect(442, 200, 515, 220), "revertButton", "Revert", new BMessage(kMsgRevertKeymap)); placeholderView->AddChild(fRevertButton); + UpdateButtons(); BPath path; find_directory(B_USER_SETTINGS_DIRECTORY, &path); @@ -216,7 +219,6 @@ KeymapWindow::AddMaps(BView *placeholderView) fUserListView->SetSelectionMessage(new BMessage(kMsgUserMapSelected)); FillSystemMaps(); - FillUserMaps(); } @@ -299,6 +301,7 @@ KeymapWindow::MessageReceived(BMessage* message) // Deselect item in other BListView fUserListView->DeselectAll(); + UpdateButtons(); } } break; @@ -308,17 +311,28 @@ KeymapWindow::MessageReceived(BMessage* message) static_cast(fUserListView->ItemAt(fUserListView->CurrentSelection())); if (keymapListItem) { fCurrentMap.Load(keymapListItem->KeymapEntry()); + + if (fFirstTime) { + fPreviousMap.Load(keymapListItem->KeymapEntry()); + fAppliedMap.Load(keymapListItem->KeymapEntry()); + fFirstTime = false; + } + fMapView->Invalidate(); // Deselect item in other BListView fSystemListView->DeselectAll(); + UpdateButtons(); } } break; case kMsgUseKeymap: UseKeymap(); + UpdateButtons(); break; - case kMsgRevertKeymap: // do nothing, just like the original + case kMsgRevertKeymap: + RevertKeymap(); + UpdateButtons(); break; default: BWindow::MessageReceived(message); @@ -327,6 +341,43 @@ KeymapWindow::MessageReceived(BMessage* message) } + void +KeymapWindow::UpdateButtons() +{ + fUseButton->SetEnabled(!fCurrentMap.Equals(fAppliedMap)); + fRevertButton->SetEnabled(!fCurrentMap.Equals(fPreviousMap)); +} + + +void +KeymapWindow::RevertKeymap() +{ + //saves previous map to the Key_map file + + printf("REVERT\n"); + BPath path; + if (find_directory(B_USER_SETTINGS_DIRECTORY, &path)!=B_OK) + return; + + path.Append("Key_map"); + + entry_ref ref; + get_ref_for_path(path.Path(), &ref); + + status_t err; + if ((err = fPreviousMap.Save(ref)) != B_OK) { + printf("error when saving : %s", strerror(err)); + return; + } + fPreviousMap.Use(); + fAppliedMap.Load(ref); + + //select and load it (first item in fUserListView is a ref to Key_map) + fUserListView->DeselectAll(); + fUserListView->Select(0); +} + + void KeymapWindow::UseKeymap() { @@ -345,6 +396,7 @@ KeymapWindow::UseKeymap() return; } fCurrentMap.Use(); + fAppliedMap.Load(ref); fUserListView->Select(0); } @@ -407,13 +459,6 @@ KeymapWindow::FillUserMaps() } -BEntry* -KeymapWindow::CurrentMap() -{ - return NULL; -} - - MapView::MapView(BRect rect, const char *name, Keymap* keymap) : BControl(rect, name, NULL, NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW), fCurrentFont(*be_plain_font), diff --git a/src/preferences/keymap/KeymapWindow.h b/src/preferences/keymap/KeymapWindow.h index d965889e28..c1c8659c5c 100644 --- a/src/preferences/keymap/KeymapWindow.h +++ b/src/preferences/keymap/KeymapWindow.h @@ -74,6 +74,16 @@ public: void MessageReceived( BMessage* message ); protected: + BMenuBar *AddMenuBar(); + void AddMaps(BView *placeholderView); + void UseKeymap(); + void RevertKeymap(); + void UpdateButtons(); + + void FillSystemMaps(); + void FillUserMaps(); + + BListView *fSystemListView; BListView *fUserListView; // the map that's currently highlighted @@ -82,18 +92,12 @@ protected: BMenu *fFontMenu; MapView *fMapView; - - BMenuBar *AddMenuBar(); - void AddMaps(BView *placeholderView); - void UseKeymap(); - - void FillSystemMaps(); - void FillUserMaps(); - - BEntry* CurrentMap(); Keymap fCurrentMap; - + Keymap fPreviousMap; + Keymap fAppliedMap; + bool fFirstTime; + BFilePanel *fOpenPanel; // the file panel to open BFilePanel *fSavePanel; // the file panel to save };