From 4fac07a0887406d944d78403f8a3b01a4e6154be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 12 Nov 2006 22:24:39 +0000 Subject: [PATCH] * started work on saving program settins, window frame is remembered for now * fixed some annoying bugs: - the snapping prevented some of the proximity checks from working while editing a path or using the transform box, now the Manipulators themself are responsible for mouse snapping and can do so at the time it works best for them - pressing the Esc key while using the transform box did reset the transformation, but didn't exit the transform mode (return to path editing) * improved layout of some views when the window is resized git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19266 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/icon-o-matic/CanvasView.cpp | 85 ++++++-------- src/apps/icon-o-matic/CanvasView.h | 5 +- src/apps/icon-o-matic/IconEditorApp.cpp | 107 ++++++++++++++++-- src/apps/icon-o-matic/IconEditorApp.h | 16 +++ src/apps/icon-o-matic/Jamfile | 1 + src/apps/icon-o-matic/MainWindow.cpp | 25 ++++ src/apps/icon-o-matic/MainWindow.h | 4 +- .../generic/gui/stateview/StateView.cpp | 6 + .../generic/gui/stateview/StateView.h | 2 + .../generic/support/support_ui.cpp | 48 +++++--- src/apps/icon-o-matic/gui/StyleView.cpp | 3 + src/apps/icon-o-matic/gui/SwatchGroup.cpp | 1 + .../import_export/flat_icon/RDefExporter.cpp | 7 +- .../icon-o-matic/shape/PathManipulator.cpp | 22 +++- .../transformable/TransformBox.cpp | 4 + 15 files changed, 249 insertions(+), 87 deletions(-) diff --git a/src/apps/icon-o-matic/CanvasView.cpp b/src/apps/icon-o-matic/CanvasView.cpp index ac716cd707..bc9cd1d2af 100644 --- a/src/apps/icon-o-matic/CanvasView.cpp +++ b/src/apps/icon-o-matic/CanvasView.cpp @@ -126,20 +126,48 @@ CanvasView::MouseDown(BPoint where) if (!IsFocus()) MakeFocus(true); - _FilterMouse(&where); - StateView::MouseDown(where); } - -// MouseMoved +// FilterMouse void -CanvasView::MouseMoved(BPoint where, uint32 transit, - const BMessage* dragMessage) +CanvasView::FilterMouse(BPoint* where) const { - _FilterMouse(&where); + switch (fMouseFilterMode) { - StateView::MouseMoved(where, transit, dragMessage); + case SNAPPING_64: + ConvertToCanvas(where); + where->x = floorf(where->x + 0.5); + where->y = floorf(where->y + 0.5); + ConvertFromCanvas(where); + break; + + case SNAPPING_32: + ConvertToCanvas(where); + where->x /= 2.0; + where->y /= 2.0; + where->x = floorf(where->x + 0.5); + where->y = floorf(where->y + 0.5); + where->x *= 2.0; + where->y *= 2.0; + ConvertFromCanvas(where); + break; + + case SNAPPING_16: + ConvertToCanvas(where); + where->x /= 4.0; + where->y /= 4.0; + where->x = floorf(where->x + 0.5); + where->y = floorf(where->y + 0.5); + where->x *= 4.0; + where->y *= 4.0; + ConvertFromCanvas(where); + break; + + case SNAPPING_OFF: + default: + break; + } } // #pragma mark - @@ -387,47 +415,6 @@ CanvasView::_DrawInto(BView* view, BRect updateRect) StateView::Draw(view, updateRect); } -// _FilterMouse -void -CanvasView::_FilterMouse(BPoint* where) const -{ - switch (fMouseFilterMode) { - - case SNAPPING_64: - ConvertToCanvas(where); - where->x = floorf(where->x + 0.5); - where->y = floorf(where->y + 0.5); - ConvertFromCanvas(where); - break; - - case SNAPPING_32: - ConvertToCanvas(where); - where->x /= 2.0; - where->y /= 2.0; - where->x = floorf(where->x + 0.5); - where->y = floorf(where->y + 0.5); - where->x *= 2.0; - where->y *= 2.0; - ConvertFromCanvas(where); - break; - - case SNAPPING_16: - ConvertToCanvas(where); - where->x /= 4.0; - where->y /= 4.0; - where->x = floorf(where->x + 0.5); - where->y = floorf(where->y + 0.5); - where->x *= 4.0; - where->y *= 4.0; - ConvertFromCanvas(where); - break; - - case SNAPPING_OFF: - default: - break; - } -} - // _MakeBackground void CanvasView::_MakeBackground() diff --git a/src/apps/icon-o-matic/CanvasView.h b/src/apps/icon-o-matic/CanvasView.h index edc27aa978..2b1ef00789 100644 --- a/src/apps/icon-o-matic/CanvasView.h +++ b/src/apps/icon-o-matic/CanvasView.h @@ -36,8 +36,7 @@ class CanvasView : public StateView, virtual void Draw(BRect updateRect); virtual void MouseDown(BPoint where); - virtual void MouseMoved(BPoint where, uint32 transit, - const BMessage* dragMessage); + virtual void FilterMouse(BPoint* where) const; // Scrollable interface protected: @@ -80,8 +79,6 @@ class CanvasView : public StateView, void _DrawInto(BView* view, BRect updateRect); - void _FilterMouse(BPoint* where) const; - void _MakeBackground(); private: diff --git a/src/apps/icon-o-matic/IconEditorApp.cpp b/src/apps/icon-o-matic/IconEditorApp.cpp index 149a06e5b9..24d6e8e44b 100644 --- a/src/apps/icon-o-matic/IconEditorApp.cpp +++ b/src/apps/icon-o-matic/IconEditorApp.cpp @@ -8,15 +8,17 @@ #include "IconEditorApp.h" +#include +#include +#include + #include #include #include #include #include -#include -#include -#include +#include "support_settings.h" #include "AutoLocker.h" #include "BitmapExporter.h" @@ -44,7 +46,11 @@ IconEditorApp::IconEditorApp() fDocument(new Document("test")), fOpenPanel(NULL), - fSavePanel(NULL) + fSavePanel(NULL), + + fLastOpenPath(""), + fLastSavePath(""), + fLastExportPath("") { } @@ -67,6 +73,8 @@ bool IconEditorApp::QuitRequested() { // TODO: ask main window if quitting is ok + _StoreSettings(); + fMainWindow->Lock(); fMainWindow->Quit(); fMainWindow = NULL; @@ -214,6 +222,9 @@ IconEditorApp::ReadyToRun() // create main window fMainWindow = new MainWindow(this, fDocument); + + _RestoreSettings(); + fMainWindow->Show(); } @@ -329,11 +340,31 @@ IconEditorApp::_Open(const entry_ref& ref, bool append) if (mainWindowLocked) fMainWindow->SetIcon(NULL); - fDocument->MakeEmpty(); + // incorporate the loaded icon into the document + // (either replace it or append to it) + if (append) { + // preserve the entry_refs + entry_ref saveRef; + if (fDocument->Ref()) + saveRef = *fDocument->Ref(); + entry_ref exportRef; + if (fDocument->ExportRef()) + exportRef = *fDocument->ExportRef(); - fDocument->SetIcon(icon); + fDocument->MakeEmpty(); + fDocument->SetIcon(icon); - if (!append) { + // restore refs after "append" + if (saveRef.name) + fDocument->SetRef(saveRef); + if (exportRef.name) + fDocument->SetExportRef(exportRef); + } else { + fDocument->MakeEmpty(); + fDocument->SetIcon(icon); + + // document got replaced, but we have at + // least one ref already switch (refMode) { case REF_MESSAGE: fDocument->SetRef(ref); @@ -443,3 +474,65 @@ IconEditorApp::_SyncPanels(BFilePanel* from, BFilePanel* to) from->Window()->Unlock(); } } + +// _LastFilePath +const char* +IconEditorApp::_LastFilePath(path_kind which) +{ + const char* path = NULL; + + switch (which) { + case LAST_PATH_OPEN: + if (fLastOpenPath.Length() > 0) + path = fLastOpenPath.String(); + else if (fLastSavePath.Length() > 0) + path = fLastSavePath.String(); + else if (fLastExportPath.Length() > 0) + path = fLastExportPath.String(); + break; + case LAST_PATH_SAVE: + if (fLastSavePath.Length() > 0) + path = fLastSavePath.String(); + else if (fLastExportPath.Length() > 0) + path = fLastExportPath.String(); + else if (fLastOpenPath.Length() > 0) + path = fLastOpenPath.String(); + break; + case LAST_PATH_EXPORT: + if (fLastExportPath.Length() > 0) + path = fLastExportPath.String(); + else if (fLastSavePath.Length() > 0) + path = fLastSavePath.String(); + else if (fLastOpenPath.Length() > 0) + path = fLastOpenPath.String(); + break; + } + if (!path) + path = "/boot/home"; + + return path; +} + +// #pragma mark - + +// _StoreSettings +void +IconEditorApp::_StoreSettings() +{ + BMessage settings('stns'); + + fMainWindow->StoreSettings(&settings); + + save_settings(&settings, "Icon-O-Matic"); +} + +// _RestoreSettings +void +IconEditorApp::_RestoreSettings() +{ + BMessage settings('stns'); + load_settings(&settings, "Icon-O-Matic"); + + fMainWindow->RestoreSettings(&settings); +} + diff --git a/src/apps/icon-o-matic/IconEditorApp.h b/src/apps/icon-o-matic/IconEditorApp.h index c1c6a8be70..79f49d28a2 100644 --- a/src/apps/icon-o-matic/IconEditorApp.h +++ b/src/apps/icon-o-matic/IconEditorApp.h @@ -10,6 +10,7 @@ #define ICON_EDITOR_APP_H #include +#include class BFilePanel; class Document; @@ -49,6 +50,12 @@ enum { EXPORT_MODE_ICON_RDEF, }; +typedef enum { + LAST_PATH_OPEN = 0, + LAST_PATH_SAVE, + LAST_PATH_EXPORT, +} path_kind; + class IconEditorApp : public BApplication { public: IconEditorApp(); @@ -73,11 +80,20 @@ class IconEditorApp : public BApplication { void _SyncPanels(BFilePanel* from, BFilePanel* to); + const char* _LastFilePath(path_kind which); + + void _StoreSettings(); + void _RestoreSettings(); + MainWindow* fMainWindow; Document* fDocument; BFilePanel* fOpenPanel; BFilePanel* fSavePanel; + + BString fLastOpenPath; + BString fLastSavePath; + BString fLastExportPath; }; #endif // ICON_EDITOR_APP_H diff --git a/src/apps/icon-o-matic/Jamfile b/src/apps/icon-o-matic/Jamfile index 293174bc45..04c48ef2f6 100644 --- a/src/apps/icon-o-matic/Jamfile +++ b/src/apps/icon-o-matic/Jamfile @@ -188,6 +188,7 @@ Application Icon-O-Matic : RWLocker.cpp support.cpp support_ui.cpp + support_settings.cpp # gui GradientControl.cpp diff --git a/src/apps/icon-o-matic/MainWindow.cpp b/src/apps/icon-o-matic/MainWindow.cpp index aa3218c614..843f27a903 100644 --- a/src/apps/icon-o-matic/MainWindow.cpp +++ b/src/apps/icon-o-matic/MainWindow.cpp @@ -24,6 +24,8 @@ # include #endif +#include "support_ui.h" + #include "AddPathsCommand.h" #include "AddShapesCommand.h" #include "AddStylesCommand.h" @@ -445,6 +447,28 @@ MainWindow::SetIcon(Icon* icon) // #pragma mark - +// StoreSettings +void +MainWindow::StoreSettings(BMessage* archive) +{ + if (archive->ReplaceRect("main window frame", Frame()) < B_OK) + archive->AddRect("main window frame", Frame()); +} + +// RestoreSettings +void +MainWindow::RestoreSettings(BMessage* archive) +{ + BRect frame; + if (archive->FindRect("main window frame", &frame) == B_OK) { + make_sure_frame_is_on_screen(frame, this); + MoveTo(frame.LeftTop()); + ResizeTo(frame.Width(), frame.Height()); + } +} + +// #pragma mark - + // _Init void MainWindow::_Init() @@ -795,6 +819,7 @@ MainWindow::_CreateGUI(BRect bounds) bounds.right = fSwatchGroup->Frame().left - 1; bounds.bottom = fCanvasView->Frame().top - 1; fStyleView = new StyleView(bounds); + fStyleView->SetResizingMode(B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT); bg->AddChild(fStyleView); // path list view diff --git a/src/apps/icon-o-matic/MainWindow.h b/src/apps/icon-o-matic/MainWindow.h index 78979cf493..d623eb0122 100644 --- a/src/apps/icon-o-matic/MainWindow.h +++ b/src/apps/icon-o-matic/MainWindow.h @@ -56,9 +56,11 @@ class MainWindow : public BWindow, // MainWindow void MakeEmpty(); - void SetIcon(Icon* icon); + void StoreSettings(BMessage* archive); + void RestoreSettings(BMessage* archive); + private: void _Init(); void _CreateGUI(BRect frame); diff --git a/src/apps/icon-o-matic/generic/gui/stateview/StateView.cpp b/src/apps/icon-o-matic/generic/gui/stateview/StateView.cpp index c7f6c5d733..657173c709 100644 --- a/src/apps/icon-o-matic/generic/gui/stateview/StateView.cpp +++ b/src/apps/icon-o-matic/generic/gui/stateview/StateView.cpp @@ -397,6 +397,12 @@ StateView::HandleKeyUp(uint32 key, uint32 modifiers) return false; } +// FilterMouse +void +StateView::FilterMouse(BPoint* where) const +{ +} + // StateForDragMessage ViewState* StateView::StateForDragMessage(const BMessage* message) diff --git a/src/apps/icon-o-matic/generic/gui/stateview/StateView.h b/src/apps/icon-o-matic/generic/gui/stateview/StateView.h index 7fb694ded6..25975c2957 100644 --- a/src/apps/icon-o-matic/generic/gui/stateview/StateView.h +++ b/src/apps/icon-o-matic/generic/gui/stateview/StateView.h @@ -51,6 +51,8 @@ class StateView : public BView { const mouse_info* MouseInfo() const { return &fMouseInfo; } + virtual void FilterMouse(BPoint* where) const; + virtual ViewState* StateForDragMessage(const BMessage* message); void SetLocker(RWLocker* locker); diff --git a/src/apps/icon-o-matic/generic/support/support_ui.cpp b/src/apps/icon-o-matic/generic/support/support_ui.cpp index 5ef1297792..7042f96ece 100644 --- a/src/apps/icon-o-matic/generic/support/support_ui.cpp +++ b/src/apps/icon-o-matic/generic/support/support_ui.cpp @@ -134,24 +134,36 @@ make_color_drop_message(rgb_color color, BBitmap* bitmap) void make_sure_frame_is_on_screen(BRect& frame, BWindow* window) { - BScreen screen(window); - if (frame.IsValid() && screen.IsValid()) { - if (!screen.Frame().Contains(frame)) { - // make sure frame fits in the screen - if (frame.Width() > screen.Frame().Width()) - frame.right -= frame.Width() - screen.Frame().Width() + 10.0; - if (frame.Height() > screen.Frame().Height()) - frame.bottom -= frame.Height() - screen.Frame().Height() + 30.0; - // frame is now at the most the size of the screen - if (frame.right > screen.Frame().right) - frame.OffsetBy(-(frame.right - screen.Frame().right), 0.0); - if (frame.bottom > screen.Frame().bottom) - frame.OffsetBy(0.0, -(frame.bottom - screen.Frame().bottom)); - if (frame.left < screen.Frame().left) - frame.OffsetBy((screen.Frame().left - frame.left), 0.0); - if (frame.top < screen.Frame().top) - frame.OffsetBy(0.0, (screen.Frame().top - frame.top)); - } + if (!frame.IsValid()) + return; + + BRect screenFrame; + if (window) { + BScreen screen(window); + if (!screen.IsValid()) + return; + screenFrame = screen.Frame(); + } else { + BScreen screen(B_MAIN_SCREEN_ID); + if (!screen.IsValid()) + return; + screenFrame = screen.Frame(); + } + if (!screenFrame.Contains(frame)) { + // make sure frame fits in the screen + if (frame.Width() > screenFrame.Width()) + frame.right -= frame.Width() - screenFrame.Width() + 10.0; + if (frame.Height() > screenFrame.Height()) + frame.bottom -= frame.Height() - screenFrame.Height() + 30.0; + // frame is now at the most the size of the screen + if (frame.right > screenFrame.right) + frame.OffsetBy(-(frame.right - screenFrame.right), 0.0); + if (frame.bottom > screenFrame.bottom) + frame.OffsetBy(0.0, -(frame.bottom - screenFrame.bottom)); + if (frame.left < screenFrame.left) + frame.OffsetBy((screenFrame.left - frame.left), 0.0); + if (frame.top < screenFrame.top) + frame.OffsetBy(0.0, (screenFrame.top - frame.top)); } } diff --git a/src/apps/icon-o-matic/gui/StyleView.cpp b/src/apps/icon-o-matic/gui/StyleView.cpp index e875cef168..7d923b6bd0 100644 --- a/src/apps/icon-o-matic/gui/StyleView.cpp +++ b/src/apps/icon-o-matic/gui/StyleView.cpp @@ -87,6 +87,7 @@ StyleView::StyleView(BRect frame) fStyleType->MenuBar()->GetPreferredSize(&width, &height); fStyleType->MenuBar()->ResizeTo(width, height); fStyleType->ResizeTo(frame.Width(), height + 6); + fStyleType->SetResizingMode(B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT); #endif // __HAIKU__ // gradient type @@ -133,6 +134,7 @@ StyleView::StyleView(BRect frame) fGradientType->MenuBar()->GetPreferredSize(&width, &height); fGradientType->MenuBar()->ResizeTo(width, height); fGradientType->ResizeTo(frame.Width(), height + 6); + fGradientType->SetResizingMode(B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT); // create gradient control frame.top = fGradientType->Frame().bottom + 8; @@ -146,6 +148,7 @@ StyleView::StyleView(BRect frame) fGradientControl->ResizeTo(width, height); fGradientControl->FrameResized(width, height); fGradientControl->MoveTo(frame.left, frame.top); + fGradientControl->SetResizingMode(B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT); AddChild(fGradientControl); #endif // __HAIKU__ diff --git a/src/apps/icon-o-matic/gui/SwatchGroup.cpp b/src/apps/icon-o-matic/gui/SwatchGroup.cpp index bd69583d13..8ca4e05086 100644 --- a/src/apps/icon-o-matic/gui/SwatchGroup.cpp +++ b/src/apps/icon-o-matic/gui/SwatchGroup.cpp @@ -119,6 +119,7 @@ SwatchGroup::SwatchGroup(BRect frame) fColorField->MoveTo(0, fBottomSwatchViews->Frame().bottom + 3); fColorSlider->MoveTo(0, fColorField->Frame().bottom + 1); fAlphaSlider->MoveTo(0, fColorSlider->Frame().bottom + 1); + fAlphaSlider->SetResizingMode(B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); // configure self ResizeTo(width, fAlphaSlider->Frame().bottom + 4); diff --git a/src/apps/icon-o-matic/import_export/flat_icon/RDefExporter.cpp b/src/apps/icon-o-matic/import_export/flat_icon/RDefExporter.cpp index 9e4c039b10..128b0782b4 100644 --- a/src/apps/icon-o-matic/import_export/flat_icon/RDefExporter.cpp +++ b/src/apps/icon-o-matic/import_export/flat_icon/RDefExporter.cpp @@ -107,7 +107,8 @@ RDefExporter::_Export(const uint8* source, size_t sourceSize, BPositionIO* strea } } // last line (up to 32 values) - if (ret >= B_OK) { + bool endQuotes = sourceSize > 0; + if (ret >= B_OK && sourceSize > 0) { for (size_t i = 0; i < sourceSize; i++) { sprintf(buffer, "%.2X", b[i]); size = strlen(buffer); @@ -122,8 +123,8 @@ RDefExporter::_Export(const uint8* source, size_t sourceSize, BPositionIO* strea } } if (ret >= B_OK) { - // finish (-> sourceSize - 1) - sprintf(buffer, "\"\n};\n"); + // finish + sprintf(buffer, endQuotes ? "\"\n};\n" : "};\n"); size = strlen(buffer); written = stream->Write(buffer, size); if (written != (ssize_t)size) { diff --git a/src/apps/icon-o-matic/shape/PathManipulator.cpp b/src/apps/icon-o-matic/shape/PathManipulator.cpp index 853b544ed4..ca2ccc11ad 100644 --- a/src/apps/icon-o-matic/shape/PathManipulator.cpp +++ b/src/apps/icon-o-matic/shape/PathManipulator.cpp @@ -413,12 +413,17 @@ PathManipulator::MouseDown(BPoint where) fMode = TRANSLATE_POINTS; } + // apply the canvas view mouse filter depending on current mode + if (fMode == ADD_POINT || fMode == TRANSLATE_POINTS) + fCanvasView->FilterMouse(&where); + BPoint canvasWhere = where; fCanvasView->ConvertToCanvas(&canvasWhere); // maybe we're changing some point, so we construct the // "ChangePointCommand" here so that the point is remembered // in its current state + // apply the canvas view mouse filter depending on current mode delete fChangePointCommand; fChangePointCommand = NULL; switch (fMode) { @@ -539,6 +544,9 @@ PathManipulator::MouseDown(BPoint where) void PathManipulator::MouseMoved(BPoint where) { + fCanvasView->FilterMouse(&where); + // NOTE: only filter mouse coords in mouse moved, no other + // mouse function BPoint canvasWhere = where; fCanvasView->ConvertToCanvas(&canvasWhere); @@ -870,19 +878,19 @@ PathManipulator::HandleKeyDown(uint32 key, uint32 modifiers, Command** _command) // commit case B_RETURN: if (fTransformBox) { - _SetModeForMousePos(fLastCanvasPos); - } else + _SetTransformBox(NULL); + }// else // _Perform(); break; // cancel case B_ESCAPE: if (fTransformBox) { fTransformBox->Cancel(); - _SetModeForMousePos(fLastCanvasPos); + _SetTransformBox(NULL); } else if (fFallBackMode == NEW_PATH) { fFallBackMode = SELECT_POINTS; - _SetModeForMousePos(fLastCanvasPos); - } else + _SetTransformBox(NULL); + }// else // _Cancel(); break; case 't': @@ -1165,6 +1173,10 @@ PathManipulator::_SetTransformBox(TransformPointsBox* transformBox) fTransformBox = transformBox; + // TODO: this is weird, fMode should only be set in _SetMode, not + // here as well, also this method could be called this way + // _SetModeForMousePos -> _SetMode -> _SetTransformBox + // and then below it does _SetModeForMousePos again... if (fTransformBox) { fTransformBox->MouseMoved(fLastCanvasPos); if (fMode != TRANSFORM_POINTS) { diff --git a/src/apps/icon-o-matic/transformable/TransformBox.cpp b/src/apps/icon-o-matic/transformable/TransformBox.cpp index 51a8b5cd40..9504c599e7 100644 --- a/src/apps/icon-o-matic/transformable/TransformBox.cpp +++ b/src/apps/icon-o-matic/transformable/TransformBox.cpp @@ -137,6 +137,8 @@ TransformBox::Draw(BView* into, BRect updateRect) bool TransformBox::MouseDown(BPoint where) { + fView->FilterMouse(&where); + // NOTE: filter mouse here and in MouseMoved only TransformToCanvas(where); fDragging = true; @@ -155,6 +157,8 @@ TransformBox::MouseDown(BPoint where) void TransformBox::MouseMoved(BPoint where) { + fView->FilterMouse(&where); + // NOTE: filter mouse here and in MouseDown only TransformToCanvas(where); if (fMousePos != where) {