https://git.reactos.org/?p=reactos.git;a=commitdiff;h=bbb33a6ed52e9adf4af07…
commit bbb33a6ed52e9adf4af07f7d3cfdd74baa76bfbb
Author: Katayama Hirofumi MZ <katayama.hirofumi.mz(a)gmail.com>
AuthorDate: Fri Sep 22 10:48:13 2023 +0900
Commit: GitHub <noreply(a)github.com>
CommitDate: Fri Sep 22 10:48:13 2023 +0900
[MSPAINT] Don't show error message twice (#5715)
- Reduce display of error message on IDM_EDITCOPYTO and IDM_EDITPASTEFROM.
- Introduce LockBitmap/UnlockBitmap mechanism for ImageModel and SelectionModel.
CORE-19181, CORE-19182
---
base/applications/mspaint/history.cpp | 34 ++++++++++++++++++++--------
base/applications/mspaint/history.h | 2 ++
base/applications/mspaint/main.cpp | 6 ++---
base/applications/mspaint/selectionmodel.cpp | 12 ++++++++++
base/applications/mspaint/selectionmodel.h | 2 ++
base/applications/mspaint/winproc.cpp | 18 ++++++---------
6 files changed, 50 insertions(+), 24 deletions(-)
diff --git a/base/applications/mspaint/history.cpp
b/base/applications/mspaint/history.cpp
index 4451b06f927..e44eca7dc4d 100644
--- a/base/applications/mspaint/history.cpp
+++ b/base/applications/mspaint/history.cpp
@@ -261,27 +261,41 @@ void ImageModel::Clamp(POINT& pt) const
HBITMAP ImageModel::CopyBitmap()
{
- // NOTE: An app cannot select a bitmap into more than one device context at a time.
- ::SelectObject(m_hDrawingDC, m_hbmOld); // De-select
- HBITMAP ret = CopyDIBImage(m_hBms[m_currInd]);
- m_hbmOld = ::SelectObject(m_hDrawingDC, m_hBms[m_currInd]); // Re-select
+ HBITMAP hBitmap = LockBitmap();
+ HBITMAP ret = CopyDIBImage(hBitmap);
+ UnlockBitmap(hBitmap);
return ret;
}
BOOL ImageModel::IsBlackAndWhite()
{
- ::SelectObject(m_hDrawingDC, m_hbmOld); // De-select
- BOOL bBlackAndWhite = IsBitmapBlackAndWhite(m_hBms[m_currInd]);
- m_hbmOld = ::SelectObject(m_hDrawingDC, m_hBms[m_currInd]); // Re-select
+ HBITMAP hBitmap = LockBitmap();
+ BOOL bBlackAndWhite = IsBitmapBlackAndWhite(hBitmap);
+ UnlockBitmap(hBitmap);
return bBlackAndWhite;
}
void ImageModel::PushBlackAndWhite()
{
- ::SelectObject(m_hDrawingDC, m_hbmOld); // De-select
- HBITMAP hNewBitmap = ConvertToBlackAndWhite(m_hBms[m_currInd]);
- m_hbmOld = ::SelectObject(m_hDrawingDC, m_hBms[m_currInd]); // Re-select
+ HBITMAP hBitmap = LockBitmap();
+ HBITMAP hNewBitmap = ConvertToBlackAndWhite(hBitmap);
+ UnlockBitmap(hBitmap);
if (hNewBitmap)
PushImageForUndo(hNewBitmap);
}
+
+HBITMAP ImageModel::LockBitmap()
+{
+ // NOTE: An app cannot select a bitmap into more than one device context at a time.
+ ::SelectObject(m_hDrawingDC, m_hbmOld); // De-select
+ HBITMAP hbmLocked = m_hBms[m_currInd];
+ m_hBms[m_currInd] = NULL;
+ return hbmLocked;
+}
+
+void ImageModel::UnlockBitmap(HBITMAP hbmLocked)
+{
+ m_hBms[m_currInd] = hbmLocked;
+ m_hbmOld = ::SelectObject(m_hDrawingDC, hbmLocked); // Re-select
+}
diff --git a/base/applications/mspaint/history.h b/base/applications/mspaint/history.h
index 5bee4105240..fb369dff72c 100644
--- a/base/applications/mspaint/history.h
+++ b/base/applications/mspaint/history.h
@@ -31,6 +31,8 @@ public:
int GetWidth() const;
int GetHeight() const;
HBITMAP CopyBitmap();
+ HBITMAP LockBitmap();
+ void UnlockBitmap(HBITMAP hbmLocked);
void InvertColors();
void FlipHorizontally();
void FlipVertically();
diff --git a/base/applications/mspaint/main.cpp b/base/applications/mspaint/main.cpp
index ba7ad18c213..dda9db7618e 100644
--- a/base/applications/mspaint/main.cpp
+++ b/base/applications/mspaint/main.cpp
@@ -117,9 +117,9 @@ BOOL OpenMailer(HWND hWnd, LPCWSTR pszPathName)
strFileTitle += L".png";
// Save it to the temporary file
- HBITMAP hbm = imageModel.CopyBitmap();
- BOOL ret = SaveDIBToFile(hbm, g_szMailTempFile, FALSE,
Gdiplus::ImageFormatPNG);
- ::DeleteObject(hbm);
+ HBITMAP hbmLocked = imageModel.LockBitmap();
+ BOOL ret = SaveDIBToFile(hbmLocked, g_szMailTempFile, FALSE,
Gdiplus::ImageFormatPNG);
+ imageModel.UnlockBitmap(hbmLocked);
if (!ret)
{
g_szMailTempFile[0] = UNICODE_NULL;
diff --git a/base/applications/mspaint/selectionmodel.cpp
b/base/applications/mspaint/selectionmodel.cpp
index a5e81d5c41a..b46a72263b9 100644
--- a/base/applications/mspaint/selectionmodel.cpp
+++ b/base/applications/mspaint/selectionmodel.cpp
@@ -528,3 +528,15 @@ void SelectionModel::SwapWidthAndHeight()
m_rc.right = m_rc.left + cy;
m_rc.bottom = m_rc.top + cx;
}
+
+HBITMAP SelectionModel::LockBitmap()
+{
+ HBITMAP hbm = m_hbmColor;
+ m_hbmColor = NULL;
+ return hbm;
+}
+
+void SelectionModel::UnlockBitmap(HBITMAP hbmLocked)
+{
+ m_hbmColor = hbmLocked;
+}
diff --git a/base/applications/mspaint/selectionmodel.h
b/base/applications/mspaint/selectionmodel.h
index b47a0d40821..febc6072263 100644
--- a/base/applications/mspaint/selectionmodel.h
+++ b/base/applications/mspaint/selectionmodel.h
@@ -40,6 +40,8 @@ public:
void DeleteSelection();
HBITMAP CopyBitmap();
+ HBITMAP LockBitmap();
+ void UnlockBitmap(HBITMAP hbmLocked);
void GetSelectionContents(HDC hDCImage);
void DrawFramePoly(HDC hDCImage);
void DrawBackground(HDC hDCImage);
diff --git a/base/applications/mspaint/winproc.cpp
b/base/applications/mspaint/winproc.cpp
index 8f9e0ca9b31..130c81ebbd9 100644
--- a/base/applications/mspaint/winproc.cpp
+++ b/base/applications/mspaint/winproc.cpp
@@ -741,13 +741,13 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM
lParam, BOOL& bH
selectionModel.TakeOff();
{
- HBITMAP hbm = selectionModel.CopyBitmap();
- if (hbm)
+ HBITMAP hbmLocked = selectionModel.LockBitmap();
+ if (hbmLocked)
{
- HGLOBAL hGlobal = BitmapToClipboardDIB(hbm);
+ HGLOBAL hGlobal = BitmapToClipboardDIB(hbmLocked);
if (hGlobal)
::SetClipboardData(CF_DIB, hGlobal);
- ::DeleteObject(hbm);
+ selectionModel.UnlockBitmap(hbmLocked);
}
}
@@ -865,10 +865,9 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM
lParam, BOOL& bH
WCHAR szFileName[MAX_LONG_PATH] = L"*.png";
if (GetSaveFileName(szFileName, _countof(szFileName)))
{
- HBITMAP hbm = selectionModel.CopyBitmap();
- if (!SaveDIBToFile(hbm, szFileName, FALSE))
- ShowError(IDS_SAVEERROR, szFileName);
- ::DeleteObject(hbm);
+ HBITMAP hbmLocked = selectionModel.LockBitmap();
+ SaveDIBToFile(hbmLocked, szFileName, FALSE);
+ selectionModel.UnlockBitmap(hbmLocked);
}
break;
}
@@ -880,8 +879,6 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM
lParam, BOOL& bH
HBITMAP hbmNew = DoLoadImageFile(m_hWnd, szFileName, FALSE);
if (hbmNew)
InsertSelectionFromHBITMAP(hbmNew, m_hWnd);
- else
- ShowError(IDS_LOADERRORTEXT, szFileName);
}
break;
}
@@ -1008,7 +1005,6 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM
lParam, BOOL& bH
imageModel.PushImageForUndo(selectionModel.CopyBitmap());
selectionModel.HideSelection();
break;
-
case IDM_VIEWTOOLBOX:
registrySettings.ShowToolBox = !toolBoxContainer.IsWindowVisible();
toolBoxContainer.ShowWindow(registrySettings.ShowToolBox ? SW_SHOWNOACTIVATE
: SW_HIDE);