From b01bdb48ea5093f6c20b58540f9a326d452cbc17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 18 Aug 2009 12:50:57 +0000 Subject: [PATCH] * The Keymap preferences app will now memorize its window frame and chosen layout. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32491 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/preferences/keymap/KeyboardLayout.cpp | 9 +- src/preferences/keymap/KeyboardLayout.h | 2 + src/preferences/keymap/KeymapWindow.cpp | 170 ++++++++++++++++++---- src/preferences/keymap/KeymapWindow.h | 8 + 4 files changed, 156 insertions(+), 33 deletions(-) diff --git a/src/preferences/keymap/KeyboardLayout.cpp b/src/preferences/keymap/KeyboardLayout.cpp index a04566f3b9..f25530e0e1 100644 --- a/src/preferences/keymap/KeyboardLayout.cpp +++ b/src/preferences/keymap/KeyboardLayout.cpp @@ -31,7 +31,8 @@ KeyboardLayout::KeyboardLayout() fKeys(NULL), fKeyCount(0), fKeyCapacity(0), - fIndicators(5, true) + fIndicators(5, true), + fIsDefault(true) { SetDefault(); } @@ -147,6 +148,9 @@ KeyboardLayout::Load(entry_ref& ref) data[size] = '\0'; status = _InitFrom(data); + if (status == B_OK) + fIsDefault = false; + free(data); return status; @@ -278,6 +282,7 @@ KeyboardLayout::SetDefault() _InitFrom(kIBMLaptop); #endif + fIsDefault = true; } @@ -417,7 +422,7 @@ KeyboardLayout::_GetPair(const parse_state& state, const char*& data, } _Trim(name, false); - _Trim(value, true); + _Trim(value, true); return true; } diff --git a/src/preferences/keymap/KeyboardLayout.h b/src/preferences/keymap/KeyboardLayout.h index c9a71d4d54..985907c133 100644 --- a/src/preferences/keymap/KeyboardLayout.h +++ b/src/preferences/keymap/KeyboardLayout.h @@ -60,6 +60,7 @@ public: status_t Load(entry_ref& ref); void SetDefault(); + bool IsDefault() const { return fIsDefault; } private: enum parse_mode { @@ -112,6 +113,7 @@ private: BSize fDefaultKeySize; int32 fAlternateIndex[3]; BObjectList fIndicators; + bool fIsDefault; }; #endif // KEYBOARD_LAYOUT_H diff --git a/src/preferences/keymap/KeymapWindow.cpp b/src/preferences/keymap/KeymapWindow.cpp index dbfd6c7834..c75bae1e76 100644 --- a/src/preferences/keymap/KeymapWindow.cpp +++ b/src/preferences/keymap/KeymapWindow.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -123,26 +124,13 @@ KeymapWindow::KeymapWindow() fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger, &ref, B_FILE_NODE, false, NULL); - BScreen screen(this); + BRect windowFrame; + BString keyboardLayout; + _LoadSettings(windowFrame, keyboardLayout); + _SetKeyboardLayout(keyboardLayout.String()); - float width = Frame().Width(); - float height = Frame().Height(); - - // Make sure we can fit on screen - if (screen.Frame().Width() < Frame().Width()) - width = screen.Frame().Width(); - if (screen.Frame().Height() < Frame().Height()) - height = screen.Frame().Height(); - - // See if we can use a larger default size - if (screen.Frame().Width() > 1200) { - width = 900; - height = 400; - } - - // TODO: store and restore position and size! - ResizeTo(width, height); - MoveTo(BAlert::AlertPosition(width, height)); + ResizeTo(windowFrame.Width(), windowFrame.Height()); + MoveTo(windowFrame.LeftTop()); // TODO: this might be a bug in the interface kit, but scrolling to // selection does not correctly work unless the window is shown. @@ -168,7 +156,7 @@ KeymapWindow::KeymapWindow() } -KeymapWindow::~KeymapWindow(void) +KeymapWindow::~KeymapWindow() { delete fOpenPanel; delete fSavePanel; @@ -178,6 +166,8 @@ KeymapWindow::~KeymapWindow(void) bool KeymapWindow::QuitRequested() { + _SaveSettings(); + be_app->PostMessage(B_QUIT_REQUESTED); return true; } @@ -231,18 +221,11 @@ KeymapWindow::MessageReceived(BMessage* message) case kChangeKeyboardLayout: { entry_ref ref; - if (message->FindRef("ref", &ref) == B_OK - && fKeyboardLayoutView->GetKeyboardLayout()->Load(ref) - == B_OK) { - fKeyboardLayoutView->SetKeyboardLayout( - fKeyboardLayoutView->GetKeyboardLayout()); - } else { - fKeyboardLayoutView->GetKeyboardLayout()->SetDefault(); - fLayoutMenu->ItemAt(0)->SetMarked(true); - } + BPath path; + if (message->FindRef("ref", &ref) == B_OK) + path.SetTo(&ref); - fKeyboardLayoutView->SetKeyboardLayout( - fKeyboardLayoutView->GetKeyboardLayout()); + _SetKeyboardLayout(path.Path()); break; } @@ -557,6 +540,43 @@ KeymapWindow::_AddKeyboardLayouts(BMenu* menu) } +status_t +KeymapWindow::_SetKeyboardLayout(const char* path) +{ + status_t status = B_OK; + + if (path != NULL && path[0] != '\0') { + status = fKeyboardLayoutView->GetKeyboardLayout()->Load(path); + if (status == B_OK) { + // select item + for (int32 i = fLayoutMenu->CountItems(); i-- > 0;) { + BMenuItem* item = fLayoutMenu->ItemAt(i); + BMessage* message = item->Message(); + entry_ref ref; + if (message->FindRef("ref", &ref) == B_OK) { + BPath layoutPath(&ref); + if (layoutPath == path) { + item->SetMarked(true); + break; + } + } + } + } + } + + if (path == NULL || status != B_OK) { + fKeyboardLayoutView->GetKeyboardLayout()->SetDefault(); + fLayoutMenu->ItemAt(0)->SetMarked(true); + } + + // Refresh currently set layout + fKeyboardLayoutView->SetKeyboardLayout( + fKeyboardLayoutView->GetKeyboardLayout()); + + return status; +} + + /*! Sets the label of the "Switch Shorcuts" button to make it more descriptive what will happen when you press that button. */ @@ -828,3 +848,91 @@ KeymapWindow::_SelectCurrentMap() fUserListView->Select(0L); } } + + +status_t +KeymapWindow::_GetSettings(BFile& file, int mode) const +{ + BPath path; + status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path, + (mode & O_ACCMODE) != O_RDONLY); + if (status != B_OK) + return status; + + path.Append("Keymap settings"); + + return file.SetTo(path.Path(), mode); +} + + +status_t +KeymapWindow::_LoadSettings(BRect& windowFrame, BString& keyboardLayout) +{ + BScreen screen(this); + + windowFrame.Set(-1, -1, 799, 329); + // See if we can use a larger default size + if (screen.Frame().Width() > 1200) { + windowFrame.right = 899; + windowFrame.bottom = 349; + } + + keyboardLayout = ""; + + BFile file; + status_t status = _GetSettings(file, B_READ_ONLY); + if (status == B_OK) { + BMessage settings; + status = settings.Unflatten(&file); + if (status == B_OK) { + BRect frame; + if (settings.FindRect("window frame", &frame) == B_OK) + windowFrame = frame; + + settings.FindString("keyboard layout", &keyboardLayout); + } + } + + if (!screen.Frame().Contains(windowFrame)) { + // Make sure the window is not larger than the screen + if (windowFrame.Width() > screen.Frame().Width()) + windowFrame.right = windowFrame.left + screen.Frame().Width(); + if (windowFrame.Height() > screen.Frame().Height()) + windowFrame.bottom = windowFrame.top + screen.Frame().Height(); + + // Make sure the window is on screen (and center if it isn't) + if (windowFrame.left < screen.Frame().left + || windowFrame.right > screen.Frame().right + || windowFrame.top < screen.Frame().top + || windowFrame.bottom > screen.Frame().bottom) { + windowFrame.OffsetTo(BAlert::AlertPosition(windowFrame.Width(), + windowFrame.Height())); + } + } + + return status; +} + + +status_t +KeymapWindow::_SaveSettings() const +{ + BFile file; + status_t status + = _GetSettings(file, B_WRITE_ONLY | B_ERASE_FILE | B_CREATE_FILE); + if (status != B_OK) + return status; + + BMessage settings('keym'); + settings.AddRect("window frame", Frame()); + + BMenuItem* item = fLayoutMenu->FindMarked(); + entry_ref ref; + if (item != NULL && item->Message()->FindRef("ref", &ref) == B_OK) { + BPath path(&ref); + if (path.InitCheck() == B_OK) + settings.AddString("keyboard layout", path.Path()); + } + + return settings.Flatten(&file); +} diff --git a/src/preferences/keymap/KeymapWindow.h b/src/preferences/keymap/KeymapWindow.h index c6477f8006..cc6cb4b75e 100644 --- a/src/preferences/keymap/KeymapWindow.h +++ b/src/preferences/keymap/KeymapWindow.h @@ -19,6 +19,7 @@ #include "Keymap.h" + class BMenu; class BMenuBar; class BMenuField; @@ -39,6 +40,7 @@ protected: BMenuBar* _CreateMenu(); BView* _CreateMapLists(); void _AddKeyboardLayouts(BMenu* menu); + status_t _SetKeyboardLayout(const char* path); void _UpdateSwitchShortcutButton(); void _UpdateButtons(); @@ -59,6 +61,12 @@ protected: bool _SelectCurrentMap(BListView *list); void _SelectCurrentMap(); + status_t _GetSettings(BFile& file, int mode) const; + status_t _LoadSettings(BRect& frame, + BString& keyboardLayout); + status_t _SaveSettings() const; + +private: BListView* fSystemListView; BListView* fUserListView; BButton* fRevertButton;