From d18fc3994c2e4686a400c83d8fe97d470f305f84 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 17 Aug 2010 15:29:08 +0000 Subject: [PATCH] Patch by rossi, but I ended u rewriting half of it : * Save terminal windows positions * Also save their size and workspace, but these aren't used (size is overriden by the menu setting and workspace is annoying) git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38192 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/terminal/TermApp.cpp | 115 ++++++++++++++++++++++++++++--- src/apps/terminal/TermApp.h | 9 ++- src/apps/terminal/TermConst.h | 1 + src/apps/terminal/TermWindow.cpp | 10 ++- src/apps/terminal/TermWindow.h | 3 +- 5 files changed, 125 insertions(+), 13 deletions(-) diff --git a/src/apps/terminal/TermApp.cpp b/src/apps/terminal/TermApp.cpp index a4fded3c01..46c6b69950 100644 --- a/src/apps/terminal/TermApp.cpp +++ b/src/apps/terminal/TermApp.cpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include #include @@ -70,12 +72,14 @@ TermApp::TermApp() if (fWindowNumber > 0) fWindowTitle << " " << fWindowNumber; - int i = fWindowNumber / 16; - int j = fWindowNumber % 16; - int k = (j * 16) + (i * 64) + 50; - int l = (j * 16) + 50; + if (_LoadWindowPosition(&fTermFrame, &fTermWorkspaces) != B_OK) { + int i = fWindowNumber / 16; + int j = fWindowNumber % 16; + int k = (j * 16) + (i * 64) + 50; + int l = (j * 16) + 50; - fTermFrame.Set(k, l, k + 50, k + 50); + fTermFrame.Set(k, l, k + 50, k + 50); + } } @@ -114,7 +118,7 @@ TermApp::ReadyToRun() // init the mouse copy'n'paste clipboard gMouseClipboard = new BClipboard(MOUSE_CLIPBOARD_NAME, true); - status_t status = _MakeTermWindow(fTermFrame); + status_t status = _MakeTermWindow(fTermFrame, fTermWorkspaces); // failed spawn, print stdout and open alert panel // TODO: This alert does never show up. @@ -171,6 +175,10 @@ TermApp::MessageReceived(BMessage* msg) break; } + case MSG_SAVE_WINDOW_POSITION: + _SaveWindowPosition(msg); + break; + case MSG_CHECK_CHILDREN: _HandleChildCleanup(); break; @@ -239,10 +247,101 @@ TermApp::RefsReceived(BMessage* message) status_t -TermApp::_MakeTermWindow(BRect &frame) +TermApp::_GetWindowPositionFile(BFile* file, uint32 openMode) +{ + BPath path; + status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path, true); + if (status != B_OK) + return status; + + status = path.Append("Terminal_windows"); + if (status != B_OK) + return status; + + return file->SetTo(path.Path(), openMode); +} + + +status_t +TermApp::_LoadWindowPosition(BRect* frame, uint32* workspaces) +{ + status_t status = B_ERROR; + BMessage position = BMessage(); + + BFile file; + status = _GetWindowPositionFile(&file, B_READ_ONLY); + if (status != B_OK) + return status; + + status = position.Unflatten(&file); + + file.Unset(); + + if (status != B_OK) + return status; + + status = position.FindRect("rect", fWindowNumber - 1, frame); + if (status != B_OK) + return status; + + int32 _workspaces; + status = position.FindInt32("workspaces", fWindowNumber - 1, &_workspaces); + if (status != B_OK) + return status; + if (modifiers() & B_OPTION_KEY) + *workspaces = _workspaces; + + printf("loading settings ok\n"); + return B_OK; +} + + +status_t +TermApp::_SaveWindowPosition(BMessage* position) +{ + BFile file; + BMessage originalSettings; + + // We append ourself to the existing settings file + // So we have to read it, insert our BMessage, and rewrite it. + + status_t status = _GetWindowPositionFile(&file, B_READ_ONLY); + if (status == B_OK) { + originalSettings.Unflatten(&file); + // No error checking on that : it fails if the settings + // file is missing, but we can create it. + + file.Unset(); + } + + // Append the new settings + BRect rect; + position->FindRect("rect", &rect); + if (originalSettings.ReplaceRect("rect", fWindowNumber - 1, rect) != B_OK) + originalSettings.AddRect("rect", rect); + + int32 workspaces; + position->FindInt32("workspaces", &workspaces); + if (originalSettings.ReplaceInt32("workspaces", fWindowNumber - 1, workspaces) + != B_OK) + originalSettings.AddInt32("workspaces", workspaces); + + // Resave the whole thing + status = _GetWindowPositionFile (&file, + B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); + if (status != B_OK) + return status; + + return originalSettings.Flatten(&file); +} + + +status_t +TermApp::_MakeTermWindow(BRect &frame, uint32 workspaces) { try { - fTermWindow = new TermWindow(frame, fWindowTitle.String(), fArgs); + fTermWindow = new TermWindow(frame, fWindowTitle.String(), workspaces, + fArgs); } catch (int error) { return (status_t)error; } catch (...) { diff --git a/src/apps/terminal/TermApp.h b/src/apps/terminal/TermApp.h index 191d4f0ea6..1cb5570f1b 100644 --- a/src/apps/terminal/TermApp.h +++ b/src/apps/terminal/TermApp.h @@ -34,6 +34,7 @@ #include #include +#include #include class Arguments; @@ -53,7 +54,10 @@ class TermApp : public BApplication { void ArgvReceived(int32 argc, char** argv); private: - status_t _MakeTermWindow(BRect& frame); + status_t _MakeTermWindow(BRect& frame, uint32 workspaces); + status_t _GetWindowPositionFile(BFile* file, uint32 openMode); + status_t _LoadWindowPosition(BRect* frame, uint32* workspaces); + status_t _SaveWindowPosition(BMessage* message); void _SwitchTerm(); void _ActivateTermWindow(team_id id); bool _IsSwitchTarget(team_id id); @@ -73,9 +77,10 @@ class TermApp : public BApplication { bool fStartFullscreen; BString fWindowTitle; int32 fWindowNumber; - + BWindow* fTermWindow; BRect fTermFrame; + uint32 fTermWorkspaces; Arguments *fArgs; }; diff --git a/src/apps/terminal/TermConst.h b/src/apps/terminal/TermConst.h index 0163676a7a..5cea6e7492 100644 --- a/src/apps/terminal/TermConst.h +++ b/src/apps/terminal/TermConst.h @@ -92,6 +92,7 @@ const uint32 MSG_TERMINAL_BUFFER_CHANGED = 'bufc'; const uint32 MSG_SET_TERMNAL_TITLE = 'sett'; const uint32 MSG_QUIT_TERMNAL = 'qutt'; const uint32 MSG_REPORT_MOUSE_EVENT = 'mous'; +const uint32 MSG_SAVE_WINDOW_POSITION = 'swps'; // Preference Read/Write Keys const char* const PREF_HALF_FONT_FAMILY = "Half Font Family"; diff --git a/src/apps/terminal/TermWindow.cpp b/src/apps/terminal/TermWindow.cpp index 225ab2b661..a121042f7a 100644 --- a/src/apps/terminal/TermWindow.cpp +++ b/src/apps/terminal/TermWindow.cpp @@ -135,10 +135,11 @@ private: }; -TermWindow::TermWindow(BRect frame, const char* title, Arguments* args) +TermWindow::TermWindow(BRect frame, const char* title, uint32 workspaces, + Arguments* args) : BWindow(frame, title, B_DOCUMENT_WINDOW, - B_CURRENT_WORKSPACE | B_QUIT_ON_WINDOW_CLOSE), + B_CURRENT_WORKSPACE | B_QUIT_ON_WINDOW_CLOSE, workspaces), fInitialTitle(title), fTabView(NULL), fMenubar(NULL), @@ -257,6 +258,11 @@ TermWindow::QuitRequested() return false; } + BMessage position = BMessage(MSG_SAVE_WINDOW_POSITION); + position.AddRect("rect", Frame()); + position.AddInt32("workspaces", Workspaces()); + be_app->PostMessage(&position); + return BWindow::QuitRequested(); } diff --git a/src/apps/terminal/TermWindow.h b/src/apps/terminal/TermWindow.h index dc3d9a5585..e179b6b57f 100644 --- a/src/apps/terminal/TermWindow.h +++ b/src/apps/terminal/TermWindow.h @@ -48,7 +48,8 @@ class TermViewContainerView; class TermWindow : public BWindow { public: - TermWindow(BRect frame, const char* title, Arguments *args); + TermWindow(BRect frame, const char* title, uint32 workspaces, + Arguments *args); virtual ~TermWindow(); void SetSessionWindowTitle(TermView* termView,