From 53f936a0a7bab0496eefc0679e4f13c8a58ca308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 25 Mar 2009 14:43:21 +0000 Subject: [PATCH] * The Keymap now has a listener mechanism for changes. * If the current Keymap is changed, the selection and use/revert buttons now actually work. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@29700 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/preferences/keymap/Keymap.cpp | 37 +++++++++++++++++- src/preferences/keymap/Keymap.h | 11 ++++++ src/preferences/keymap/KeymapWindow.cpp | 51 ++++++++++++++++--------- 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/src/preferences/keymap/Keymap.cpp b/src/preferences/keymap/Keymap.cpp index a3b269568a..3221b3bcbd 100644 --- a/src/preferences/keymap/Keymap.cpp +++ b/src/preferences/keymap/Keymap.cpp @@ -55,6 +55,34 @@ print_key(char *chars, int32 offset) } +// #pragma mark - + + +Keymap::Keymap() + : + fChars(NULL), + fCharsSize(0), + fModificationMessage(NULL) +{ +} + + +Keymap::~Keymap() +{ + delete fModificationMessage; +} + + +void +Keymap::SetTarget(BMessenger target, BMessage* modificationMessage) +{ + delete fModificationMessage; + + fTarget = target; + fModificationMessage = modificationMessage; +} + + void Keymap::DumpKeymap() { @@ -179,11 +207,13 @@ Keymap::Save(entry_ref& ref) bool -Keymap::Equals(const Keymap& map) const +Keymap::Equals(const Keymap& other) 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; + return fCharsSize == other.fCharsSize + && !memcmp(&other.fKeys, &fKeys, sizeof(key_map)) + && !memcmp(other.fChars, fChars, fCharsSize); } @@ -421,6 +451,9 @@ Keymap::SetKey(uint32 keyCode, uint32 modifiers, int8 deadKey, return; memcpy(&fChars[offset + 1], bytes, numBytes); + + if (fModificationMessage != NULL) + fTarget.SendMessage(fModificationMessage); } diff --git a/src/preferences/keymap/Keymap.h b/src/preferences/keymap/Keymap.h index f82af02958..1f5e6afeae 100644 --- a/src/preferences/keymap/Keymap.h +++ b/src/preferences/keymap/Keymap.h @@ -12,12 +12,20 @@ #include #include +#include class Keymap { public: + Keymap(); + ~Keymap(); + + void SetTarget(BMessenger target, + BMessage* modificationMessage); + status_t Load(entry_ref& ref); status_t Save(entry_ref& ref); + void DumpKeymap(); bool IsModifierKey(uint32 keyCode); uint8 IsDeadKey(uint32 keyCode, uint32 modifiers); @@ -43,6 +51,9 @@ private: key_map fKeys; uint32 fCharsSize; char fName[B_FILE_NAME_LENGTH]; + + BMessenger fTarget; + BMessage* fModificationMessage; }; diff --git a/src/preferences/keymap/KeymapWindow.cpp b/src/preferences/keymap/KeymapWindow.cpp index ccc7173535..236c8ed183 100644 --- a/src/preferences/keymap/KeymapWindow.cpp +++ b/src/preferences/keymap/KeymapWindow.cpp @@ -36,17 +36,17 @@ static const uint32 kMsgMenuFileOpen = 'mMFO'; static const uint32 kMsgMenuFileSave = 'mMFS'; static const uint32 kMsgMenuFileSaveAs = 'mMFA'; + static const uint32 kMsgMenuEditUndo = 'mMEU'; -static const uint32 kMsgMenuEditCut = 'mMEX'; -static const uint32 kMsgMenuEditCopy = 'mMEC'; -static const uint32 kMsgMenuEditPaste = 'mMEV'; -static const uint32 kMsgMenuEditClear = 'mMEL'; -static const uint32 kMsgMenuEditSelectAll = 'mMEA'; + static const uint32 kMsgMenuFontChanged = 'mMFC'; + static const uint32 kMsgSystemMapSelected = 'SmST'; static const uint32 kMsgUserMapSelected = 'UmST'; + static const uint32 kMsgUseKeymap = 'UkyM'; static const uint32 kMsgRevertKeymap = 'Rvrt'; +static const uint32 kMsgKeymapUpdated = 'upkM'; KeymapWindow::KeymapWindow() @@ -82,6 +82,7 @@ KeymapWindow::KeymapWindow() .SetInsets(10, 10, 10, 10))); fKeyboardLayoutView->SetTarget(fTextControl->TextView()); + fTextControl->MakeFocus(); AddCommonFilter(new KeymapMessageFilter(B_PROGRAMMED_DELIVERY, B_ANY_SOURCE, &fCurrentMap)); // TODO: this does not work for some reason, investigate! @@ -89,6 +90,8 @@ KeymapWindow::KeymapWindow() _UpdateButtons(); + fCurrentMap.SetTarget(this, new BMessage(kMsgKeymapUpdated)); + // Make sure the user keymap directory exists BPath path; find_directory(B_USER_SETTINGS_DIRECTORY, &path); @@ -252,9 +255,14 @@ KeymapWindow::MessageReceived(BMessage* message) case kMsgUserMapSelected: { + int32 index = fUserListView->CurrentSelection(); + if (index == 0) { + // we can safely ignore the "(Current)" item + break; + } + KeymapListItem *keymapListItem = - static_cast(fUserListView->ItemAt( - fUserListView->CurrentSelection())); + static_cast(fUserListView->ItemAt(index)); if (keymapListItem != NULL) { fCurrentMap.Load(keymapListItem->KeymapEntry()); @@ -282,6 +290,12 @@ KeymapWindow::MessageReceived(BMessage* message) _UpdateButtons(); break; + case kMsgKeymapUpdated: + _UpdateButtons(); + fSystemListView->DeselectAll(); + fUserListView->Select(0L); + break; + default: BWindow::MessageReceived(message); break; @@ -381,7 +395,7 @@ KeymapWindow::_CreateMapLists() fUserListView, 0, false, true); // '(Current)' - KeymapListItem *currentKeymapItem + KeymapListItem* currentKeymapItem = static_cast(fUserListView->FirstItem()); if (currentKeymapItem != NULL) fUserListView->AddItem(currentKeymapItem); @@ -410,29 +424,32 @@ KeymapWindow::_UpdateButtons() } +//! Saves previous map to the "Key_map" file. void KeymapWindow::_RevertKeymap() { - //saves previous map to the Key_map file - BPath path; - if (find_directory(B_USER_SETTINGS_DIRECTORY, &path)!=B_OK) + 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)); + status_t status = fPreviousMap.Save(ref); + if (status != B_OK) { + printf("error when saving keymap: %s", strerror(status)); return; } fPreviousMap.Use(); fAppliedMap.Load(ref); + // TODO: add = operator + fCurrentMap.Load(ref); + fKeyboardLayoutView->SetKeymap(&fCurrentMap); + fCurrentMapName = _GetActiveKeymapName(); if (!_SelectCurrentMap(fSystemListView) @@ -499,12 +516,12 @@ KeymapWindow::_FillUserMaps() 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); - + fUserListView->AddItem(new KeymapListItem(ref, "(Current)")); fCurrentMapName = _GetActiveKeymapName();