1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/fdrepdlg.cpp
3 // Purpose: wxFindReplaceDialog class
4 // Author: Markus Greither and Vadim Zeitlin
8 // Copyright: (c) Markus Greither
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "mswfdrepdlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/msw/wrapcdlg.h"
39 #include "wx/fdrepdlg.h"
41 // ----------------------------------------------------------------------------
42 // functions prototypes
43 // ----------------------------------------------------------------------------
45 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
46 WPARAM wParam
, LPARAM lParam
);
48 UINT_PTR CALLBACK
wxFindReplaceDialogHookProc(HWND hwnd
,
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxFindReplaceDialog
, wxDialog
)
59 // ----------------------------------------------------------------------------
60 // wxFindReplaceDialogImpl: the internals of wxFindReplaceDialog
61 // ----------------------------------------------------------------------------
63 class WXDLLEXPORT wxFindReplaceDialogImpl
66 wxFindReplaceDialogImpl(wxFindReplaceDialog
*dialog
, int flagsWX
);
67 ~wxFindReplaceDialogImpl();
69 void InitFindWhat(const wxString
& str
);
70 void InitReplaceWith(const wxString
& str
);
72 void SubclassDialog(HWND hwnd
);
74 static UINT
GetFindDialogMessage() { return ms_msgFindDialog
; }
76 // only for passing to ::FindText or ::ReplaceText
77 FINDREPLACE
*GetPtrFindReplace() { return &m_findReplace
; }
79 // set/query the "closed by user" flag
80 void SetClosedByUser() { m_wasClosedByUser
= true; }
81 bool WasClosedByUser() const { return m_wasClosedByUser
; }
84 void InitString(const wxString
& str
, LPTSTR
*ppStr
, WORD
*pLen
);
86 // the owner of the dialog
89 // the previous window proc of our owner
90 WNDPROC m_oldParentWndProc
;
92 // the find replace data used by the dialog
93 FINDREPLACE m_findReplace
;
95 // true if the user closed us, false otherwise
96 bool m_wasClosedByUser
;
98 // registered Message for Dialog
99 static UINT ms_msgFindDialog
;
101 DECLARE_NO_COPY_CLASS(wxFindReplaceDialogImpl
)
104 UINT
wxFindReplaceDialogImpl::ms_msgFindDialog
= 0;
106 // ============================================================================
108 // ============================================================================
110 // ----------------------------------------------------------------------------
111 // wxFindReplaceDialogImpl
112 // ----------------------------------------------------------------------------
114 wxFindReplaceDialogImpl::wxFindReplaceDialogImpl(wxFindReplaceDialog
*dialog
,
117 // get the identifier for the find dialog message if we don't have it yet
118 if ( !ms_msgFindDialog
)
120 ms_msgFindDialog
= ::RegisterWindowMessage(FINDMSGSTRING
);
122 if ( !ms_msgFindDialog
)
124 wxLogLastError(_T("RegisterWindowMessage(FINDMSGSTRING)"));
129 m_oldParentWndProc
= NULL
;
131 m_wasClosedByUser
= false;
133 wxZeroMemory(m_findReplace
);
135 // translate the flags: first the dialog creation flags
137 // always set this to be able to set the title
138 int flags
= FR_ENABLEHOOK
;
140 int flagsDialog
= dialog
->GetWindowStyle();
141 if ( flagsDialog
& wxFR_NOMATCHCASE
)
142 flags
|= FR_NOMATCHCASE
;
143 if ( flagsDialog
& wxFR_NOWHOLEWORD
)
144 flags
|= FR_NOWHOLEWORD
;
145 if ( flagsDialog
& wxFR_NOUPDOWN
)
146 flags
|= FR_NOUPDOWN
;
148 // and now the flags governing the initial values of the dialogs controls
149 if ( flagsWX
& wxFR_DOWN
)
151 if ( flagsWX
& wxFR_MATCHCASE
)
152 flags
|= FR_MATCHCASE
;
153 if ( flagsWX
& wxFR_WHOLEWORD
)
154 flags
|= FR_WHOLEWORD
;
156 m_findReplace
.lStructSize
= sizeof(FINDREPLACE
);
157 m_findReplace
.hwndOwner
= GetHwndOf(dialog
->GetParent());
158 m_findReplace
.Flags
= flags
;
160 m_findReplace
.lCustData
= (LPARAM
)dialog
;
161 m_findReplace
.lpfnHook
= wxFindReplaceDialogHookProc
;
164 void wxFindReplaceDialogImpl::InitString(const wxString
& str
,
165 LPTSTR
*ppStr
, WORD
*pLen
)
167 size_t len
= str
.length() + 1;
170 // MSDN docs say that the buffer must be at least 80 chars
174 *ppStr
= new wxChar
[len
];
175 wxStrcpy(*ppStr
, str
);
179 void wxFindReplaceDialogImpl::InitFindWhat(const wxString
& str
)
181 InitString(str
, &m_findReplace
.lpstrFindWhat
, &m_findReplace
.wFindWhatLen
);
184 void wxFindReplaceDialogImpl::InitReplaceWith(const wxString
& str
)
187 &m_findReplace
.lpstrReplaceWith
,
188 &m_findReplace
.wReplaceWithLen
);
191 void wxFindReplaceDialogImpl::SubclassDialog(HWND hwnd
)
195 // check that we don't subclass the parent twice: this would be a bad idea
196 // as then we'd have infinite recursion in wxFindReplaceWindowProc
197 if ( !wxCheckWindowWndProc((WXHWND
)hwnd
, (WXFARPROC
)wxFindReplaceWindowProc
) )
199 // set the new one and save the old as user data to allow access to it
200 // from wxFindReplaceWindowProc
201 m_oldParentWndProc
= wxSetWindowProc(hwnd
, wxFindReplaceWindowProc
);
203 wxSetWindowUserData(hwnd
, (void *)m_oldParentWndProc
);
207 wxFindReplaceDialogImpl::~wxFindReplaceDialogImpl()
209 delete [] m_findReplace
.lpstrFindWhat
;
210 delete [] m_findReplace
.lpstrReplaceWith
;
215 wxSetWindowProc(m_hwndOwner
, m_oldParentWndProc
);
219 // ----------------------------------------------------------------------------
220 // Window Proc for handling RegisterWindowMessage(FINDMSGSTRING)
221 // ----------------------------------------------------------------------------
223 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
224 WPARAM wParam
, LPARAM lParam
)
226 #if wxUSE_UNICODE_MSLU
227 static unsigned long s_lastMsgFlags
= 0;
229 // This flag helps us to identify the bogus ANSI message
230 // sent by UNICOWS.DLL (see below)
231 // while we're sending our message to the dialog
232 // we ignore possible messages sent in between
233 static bool s_blockMsg
= false;
234 #endif // wxUSE_UNICODE_MSLU
236 if ( nMsg
== wxFindReplaceDialogImpl::GetFindDialogMessage() )
238 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
240 #if wxUSE_UNICODE_MSLU
241 // This is a hack for a MSLU problem: Versions up to 1.0.4011
242 // of UNICOWS.DLL send the correct UNICODE item after button press
243 // and a bogus ANSI mode item right after this, so lets ignore
244 // the second bogus message
245 if ( s_lastMsgFlags
== pFR
->Flags
)
250 s_lastMsgFlags
= pFR
->Flags
;
251 #endif // wxUSE_UNICODE_MSLU
253 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
255 // map flags from Windows
258 bool replace
= false;
259 if ( pFR
->Flags
& FR_DIALOGTERM
)
261 // we have to notify the dialog that it's being closed by user and
262 // not deleted programmatically as it behaves differently in these
264 dialog
->GetImpl()->SetClosedByUser();
266 evtType
= wxEVT_COMMAND_FIND_CLOSE
;
268 else if ( pFR
->Flags
& FR_FINDNEXT
)
270 evtType
= wxEVT_COMMAND_FIND_NEXT
;
272 else if ( pFR
->Flags
& FR_REPLACE
)
274 evtType
= wxEVT_COMMAND_FIND_REPLACE
;
278 else if ( pFR
->Flags
& FR_REPLACEALL
)
280 evtType
= wxEVT_COMMAND_FIND_REPLACE_ALL
;
286 wxFAIL_MSG( _T("unknown find dialog event") );
292 if ( pFR
->Flags
& FR_DOWN
)
294 if ( pFR
->Flags
& FR_WHOLEWORD
)
295 flags
|= wxFR_WHOLEWORD
;
296 if ( pFR
->Flags
& FR_MATCHCASE
)
297 flags
|= wxFR_MATCHCASE
;
299 wxFindDialogEvent
event(evtType
, dialog
->GetId());
300 event
.SetEventObject(dialog
);
301 event
.SetFlags(flags
);
302 event
.SetFindString(pFR
->lpstrFindWhat
);
305 event
.SetReplaceString(pFR
->lpstrReplaceWith
);
308 #if wxUSE_UNICODE_MSLU
310 #endif // wxUSE_UNICODE_MSLU
314 #if wxUSE_UNICODE_MSLU
316 #endif // wxUSE_UNICODE_MSLU
318 #if wxUSE_UNICODE_MSLU
319 else if ( !s_blockMsg
)
321 #endif // wxUSE_UNICODE_MSLU
323 WNDPROC wndProc
= (WNDPROC
)wxGetWindowUserData(hwnd
);
326 wxASSERT_MSG( wndProc
!= wxFindReplaceWindowProc
,
327 _T("infinite recursion detected") );
329 return ::CallWindowProc(wndProc
, hwnd
, nMsg
, wParam
, lParam
);
332 // ----------------------------------------------------------------------------
333 // Find/replace dialog hook proc
334 // ----------------------------------------------------------------------------
337 wxFindReplaceDialogHookProc(HWND hwnd
,
339 WPARAM
WXUNUSED(wParam
),
342 if ( uiMsg
== WM_INITDIALOG
)
344 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
345 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
347 ::SetWindowText(hwnd
, dialog
->GetTitle());
349 // don't return FALSE from here or the dialog won't be shown
356 // ============================================================================
357 // wxFindReplaceDialog implementation
358 // ============================================================================
360 // ----------------------------------------------------------------------------
361 // wxFindReplaceDialog ctors/dtor
362 // ----------------------------------------------------------------------------
364 void wxFindReplaceDialog::Init()
367 m_FindReplaceData
= NULL
;
369 // as we're created in the hidden state, bring the internal flag in sync
373 wxFindReplaceDialog::wxFindReplaceDialog(wxWindow
*parent
,
374 wxFindReplaceData
*data
,
375 const wxString
&title
,
377 : wxFindReplaceDialogBase(parent
, data
, title
, flags
)
381 (void)Create(parent
, data
, title
, flags
);
384 wxFindReplaceDialog::~wxFindReplaceDialog()
386 // the dialog might have been already deleted if the user closed it
387 // manually but in this case we should have got a notification about it and
388 // the flagmust have been set
389 if ( !m_impl
->WasClosedByUser() )
391 // if it wasn't, delete the dialog ourselves
392 if ( !::DestroyWindow(GetHwnd()) )
394 wxLogLastError(_T("DestroyWindow(find dialog)"));
398 // unsubclass the parent
401 // prevent the base class dtor from trying to hide us!
404 // and from destroying our window [again]
405 m_hWnd
= (WXHWND
)NULL
;
408 bool wxFindReplaceDialog::Create(wxWindow
*parent
,
409 wxFindReplaceData
*data
,
410 const wxString
&title
,
413 m_windowStyle
= flags
;
414 m_FindReplaceData
= data
;
419 // we must have a parent as it will get the messages from us
420 return parent
!= NULL
;
423 // ----------------------------------------------------------------------------
424 // wxFindReplaceData show/hide
425 // ----------------------------------------------------------------------------
427 bool wxFindReplaceDialog::Show(bool show
)
429 if ( !wxWindowBase::Show(show
) )
431 // visibility status didn't change
435 // do we already have the dialog window?
439 (void)::ShowWindow(GetHwnd(), show
? SW_SHOW
: SW_HIDE
);
446 // well, it doesn't exist which is as good as being hidden
450 wxCHECK_MSG( m_FindReplaceData
, false, _T("call Create() first!") );
452 wxASSERT_MSG( !m_impl
, _T("why don't we have the window then?") );
454 m_impl
= new wxFindReplaceDialogImpl(this, m_FindReplaceData
->GetFlags());
456 m_impl
->InitFindWhat(m_FindReplaceData
->GetFindString());
458 bool replace
= HasFlag(wxFR_REPLACEDIALOG
);
461 m_impl
->InitReplaceWith(m_FindReplaceData
->GetReplaceString());
464 // call the right function to show the dialog which does what we want
465 FINDREPLACE
*pFR
= m_impl
->GetPtrFindReplace();
468 hwnd
= ::ReplaceText(pFR
);
470 hwnd
= ::FindText(pFR
);
474 wxLogError(_("Failed to create the standard find/replace dialog (error code %d)"),
475 ::CommDlgExtendedError());
483 // subclass parent window in order to get FINDMSGSTRING message
484 m_impl
->SubclassDialog(GetHwndOf(m_parent
));
486 if ( !::ShowWindow(hwnd
, SW_SHOW
) )
488 wxLogLastError(_T("ShowWindow(find dialog)"));
491 m_hWnd
= (WXHWND
)hwnd
;
496 // ----------------------------------------------------------------------------
497 // wxFindReplaceDialog title handling
498 // ----------------------------------------------------------------------------
500 // we set the title of this dialog in our jook proc but for now don't crash in
501 // the base class version because of m_hWnd == 0
503 void wxFindReplaceDialog::SetTitle( const wxString
& title
)
508 wxString
wxFindReplaceDialog::GetTitle() const
513 // ----------------------------------------------------------------------------
514 // wxFindReplaceDialog position/size
515 // ----------------------------------------------------------------------------
517 void wxFindReplaceDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
518 int WXUNUSED(width
), int WXUNUSED(height
),
519 int WXUNUSED(sizeFlags
))
521 // ignore - we can't change the size of this standard dialog
525 // NB: of course, both of these functions are completely bogus, but it's better
527 void wxFindReplaceDialog::DoGetSize(int *width
, int *height
) const
529 // the standard dialog size
536 void wxFindReplaceDialog::DoGetClientSize(int *width
, int *height
) const
538 // the standard dialog size
545 #endif // wxUSE_FINDREPLDLG