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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
34 #include "wx/msw/wrapcdlg.h"
35 #include "wx/fdrepdlg.h"
37 // ----------------------------------------------------------------------------
38 // functions prototypes
39 // ----------------------------------------------------------------------------
41 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
42 WPARAM wParam
, LPARAM lParam
);
44 UINT_PTR CALLBACK
wxFindReplaceDialogHookProc(HWND hwnd
,
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxFindReplaceDialog
, wxDialog
)
55 // ----------------------------------------------------------------------------
56 // wxFindReplaceDialogImpl: the internals of wxFindReplaceDialog
57 // ----------------------------------------------------------------------------
59 class WXDLLEXPORT wxFindReplaceDialogImpl
62 wxFindReplaceDialogImpl(wxFindReplaceDialog
*dialog
, int flagsWX
);
63 ~wxFindReplaceDialogImpl();
65 void InitFindWhat(const wxString
& str
);
66 void InitReplaceWith(const wxString
& str
);
68 void SubclassDialog(HWND hwnd
);
70 static UINT
GetFindDialogMessage() { return ms_msgFindDialog
; }
72 // only for passing to ::FindText or ::ReplaceText
73 FINDREPLACE
*GetPtrFindReplace() { return &m_findReplace
; }
75 // set/query the "closed by user" flag
76 void SetClosedByUser() { m_wasClosedByUser
= true; }
77 bool WasClosedByUser() const { return m_wasClosedByUser
; }
80 void InitString(const wxString
& str
, LPTSTR
*ppStr
, WORD
*pLen
);
82 // the owner of the dialog
85 // the previous window proc of our owner
86 WNDPROC m_oldParentWndProc
;
88 // the find replace data used by the dialog
89 FINDREPLACE m_findReplace
;
91 // true if the user closed us, false otherwise
92 bool m_wasClosedByUser
;
94 // registered Message for Dialog
95 static UINT ms_msgFindDialog
;
97 DECLARE_NO_COPY_CLASS(wxFindReplaceDialogImpl
)
100 UINT
wxFindReplaceDialogImpl::ms_msgFindDialog
= 0;
102 // ============================================================================
104 // ============================================================================
106 // ----------------------------------------------------------------------------
107 // wxFindReplaceDialogImpl
108 // ----------------------------------------------------------------------------
110 wxFindReplaceDialogImpl::wxFindReplaceDialogImpl(wxFindReplaceDialog
*dialog
,
113 // get the identifier for the find dialog message if we don't have it yet
114 if ( !ms_msgFindDialog
)
116 ms_msgFindDialog
= ::RegisterWindowMessage(FINDMSGSTRING
);
118 if ( !ms_msgFindDialog
)
120 wxLogLastError(_T("RegisterWindowMessage(FINDMSGSTRING)"));
125 m_oldParentWndProc
= NULL
;
127 m_wasClosedByUser
= false;
129 wxZeroMemory(m_findReplace
);
131 // translate the flags: first the dialog creation flags
133 // always set this to be able to set the title
134 int flags
= FR_ENABLEHOOK
;
136 int flagsDialog
= dialog
->GetWindowStyle();
137 if ( flagsDialog
& wxFR_NOMATCHCASE
)
138 flags
|= FR_NOMATCHCASE
;
139 if ( flagsDialog
& wxFR_NOWHOLEWORD
)
140 flags
|= FR_NOWHOLEWORD
;
141 if ( flagsDialog
& wxFR_NOUPDOWN
)
142 flags
|= FR_NOUPDOWN
;
144 // and now the flags governing the initial values of the dialogs controls
145 if ( flagsWX
& wxFR_DOWN
)
147 if ( flagsWX
& wxFR_MATCHCASE
)
148 flags
|= FR_MATCHCASE
;
149 if ( flagsWX
& wxFR_WHOLEWORD
)
150 flags
|= FR_WHOLEWORD
;
152 m_findReplace
.lStructSize
= sizeof(FINDREPLACE
);
153 m_findReplace
.hwndOwner
= GetHwndOf(dialog
->GetParent());
154 m_findReplace
.Flags
= flags
;
156 m_findReplace
.lCustData
= (LPARAM
)dialog
;
157 m_findReplace
.lpfnHook
= wxFindReplaceDialogHookProc
;
160 void wxFindReplaceDialogImpl::InitString(const wxString
& str
,
161 LPTSTR
*ppStr
, WORD
*pLen
)
163 size_t len
= str
.length() + 1;
166 // MSDN docs say that the buffer must be at least 80 chars
170 *ppStr
= new wxChar
[len
];
171 wxStrcpy(*ppStr
, str
);
175 void wxFindReplaceDialogImpl::InitFindWhat(const wxString
& str
)
177 InitString(str
, &m_findReplace
.lpstrFindWhat
, &m_findReplace
.wFindWhatLen
);
180 void wxFindReplaceDialogImpl::InitReplaceWith(const wxString
& str
)
183 &m_findReplace
.lpstrReplaceWith
,
184 &m_findReplace
.wReplaceWithLen
);
187 void wxFindReplaceDialogImpl::SubclassDialog(HWND hwnd
)
191 // check that we don't subclass the parent twice: this would be a bad idea
192 // as then we'd have infinite recursion in wxFindReplaceWindowProc
193 wxCHECK_RET( wxGetWindowProc(hwnd
) != wxFindReplaceWindowProc
,
194 _T("can't have more than one find dialog currently") );
196 // set the new one and save the old as user data to allow access to it
197 // from wxFindReplaceWindowProc
198 m_oldParentWndProc
= wxSetWindowProc(hwnd
, wxFindReplaceWindowProc
);
200 wxSetWindowUserData(hwnd
, (void *)m_oldParentWndProc
);
203 wxFindReplaceDialogImpl::~wxFindReplaceDialogImpl()
205 delete [] m_findReplace
.lpstrFindWhat
;
206 delete [] m_findReplace
.lpstrReplaceWith
;
211 wxSetWindowProc(m_hwndOwner
, m_oldParentWndProc
);
215 // ----------------------------------------------------------------------------
216 // Window Proc for handling RegisterWindowMessage(FINDMSGSTRING)
217 // ----------------------------------------------------------------------------
219 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
220 WPARAM wParam
, LPARAM lParam
)
222 #if wxUSE_UNICODE_MSLU
223 static unsigned long s_lastMsgFlags
= 0;
225 // This flag helps us to identify the bogus ANSI message
226 // sent by UNICOWS.DLL (see below)
227 // while we're sending our message to the dialog
228 // we ignore possible messages sent in between
229 static bool s_blockMsg
= false;
230 #endif // wxUSE_UNICODE_MSLU
232 if ( nMsg
== wxFindReplaceDialogImpl::GetFindDialogMessage() )
234 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
236 #if wxUSE_UNICODE_MSLU
237 // This is a hack for a MSLU problem: Versions up to 1.0.4011
238 // of UNICOWS.DLL send the correct UNICODE item after button press
239 // and a bogus ANSI mode item right after this, so lets ignore
240 // the second bogus message
241 if ( wxUsingUnicowsDll() && s_lastMsgFlags
== pFR
->Flags
)
246 s_lastMsgFlags
= pFR
->Flags
;
247 #endif // wxUSE_UNICODE_MSLU
249 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
251 // map flags from Windows
254 bool replace
= false;
255 if ( pFR
->Flags
& FR_DIALOGTERM
)
257 // we have to notify the dialog that it's being closed by user and
258 // not deleted programmatically as it behaves differently in these
260 dialog
->GetImpl()->SetClosedByUser();
262 evtType
= wxEVT_COMMAND_FIND_CLOSE
;
264 else if ( pFR
->Flags
& FR_FINDNEXT
)
266 evtType
= wxEVT_COMMAND_FIND_NEXT
;
268 else if ( pFR
->Flags
& FR_REPLACE
)
270 evtType
= wxEVT_COMMAND_FIND_REPLACE
;
274 else if ( pFR
->Flags
& FR_REPLACEALL
)
276 evtType
= wxEVT_COMMAND_FIND_REPLACE_ALL
;
282 wxFAIL_MSG( _T("unknown find dialog event") );
288 if ( pFR
->Flags
& FR_DOWN
)
290 if ( pFR
->Flags
& FR_WHOLEWORD
)
291 flags
|= wxFR_WHOLEWORD
;
292 if ( pFR
->Flags
& FR_MATCHCASE
)
293 flags
|= wxFR_MATCHCASE
;
295 wxFindDialogEvent
event(evtType
, dialog
->GetId());
296 event
.SetEventObject(dialog
);
297 event
.SetFlags(flags
);
298 event
.SetFindString(pFR
->lpstrFindWhat
);
301 event
.SetReplaceString(pFR
->lpstrReplaceWith
);
304 #if wxUSE_UNICODE_MSLU
306 #endif // wxUSE_UNICODE_MSLU
310 #if wxUSE_UNICODE_MSLU
312 #endif // wxUSE_UNICODE_MSLU
314 #if wxUSE_UNICODE_MSLU
315 else if ( !s_blockMsg
)
317 #endif // wxUSE_UNICODE_MSLU
319 WNDPROC wndProc
= (WNDPROC
)wxGetWindowUserData(hwnd
);
322 wxASSERT_MSG( wndProc
!= wxFindReplaceWindowProc
,
323 _T("infinite recursion detected") );
325 return ::CallWindowProc(wndProc
, hwnd
, nMsg
, wParam
, lParam
);
328 // ----------------------------------------------------------------------------
329 // Find/replace dialog hook proc
330 // ----------------------------------------------------------------------------
333 wxFindReplaceDialogHookProc(HWND hwnd
,
335 WPARAM
WXUNUSED(wParam
),
338 if ( uiMsg
== WM_INITDIALOG
)
340 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
341 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
343 ::SetWindowText(hwnd
, dialog
->GetTitle());
345 // don't return FALSE from here or the dialog won't be shown
352 // ============================================================================
353 // wxFindReplaceDialog implementation
354 // ============================================================================
356 // ----------------------------------------------------------------------------
357 // wxFindReplaceDialog ctors/dtor
358 // ----------------------------------------------------------------------------
360 void wxFindReplaceDialog::Init()
363 m_FindReplaceData
= NULL
;
365 // as we're created in the hidden state, bring the internal flag in sync
369 wxFindReplaceDialog::wxFindReplaceDialog(wxWindow
*parent
,
370 wxFindReplaceData
*data
,
371 const wxString
&title
,
373 : wxFindReplaceDialogBase(parent
, data
, title
, flags
)
377 (void)Create(parent
, data
, title
, flags
);
380 wxFindReplaceDialog::~wxFindReplaceDialog()
382 // the dialog might have been already deleted if the user closed it
383 // manually but in this case we should have got a notification about it and
384 // the flagmust have been set
385 if ( !m_impl
->WasClosedByUser() )
387 // if it wasn't, delete the dialog ourselves
388 if ( !::DestroyWindow(GetHwnd()) )
390 wxLogLastError(_T("DestroyWindow(find dialog)"));
394 // unsubclass the parent
397 // prevent the base class dtor from trying to hide us!
400 // and from destroying our window [again]
401 m_hWnd
= (WXHWND
)NULL
;
404 bool wxFindReplaceDialog::Create(wxWindow
*parent
,
405 wxFindReplaceData
*data
,
406 const wxString
&title
,
409 m_windowStyle
= flags
;
410 m_FindReplaceData
= data
;
415 // we must have a parent as it will get the messages from us
416 return parent
!= NULL
;
419 // ----------------------------------------------------------------------------
420 // wxFindReplaceData show/hide
421 // ----------------------------------------------------------------------------
423 bool wxFindReplaceDialog::Show(bool show
)
425 if ( !wxWindowBase::Show(show
) )
427 // visibility status didn't change
431 // do we already have the dialog window?
435 (void)::ShowWindow(GetHwnd(), show
? SW_SHOW
: SW_HIDE
);
442 // well, it doesn't exist which is as good as being hidden
446 wxCHECK_MSG( m_FindReplaceData
, false, _T("call Create() first!") );
448 wxASSERT_MSG( !m_impl
, _T("why don't we have the window then?") );
450 m_impl
= new wxFindReplaceDialogImpl(this, m_FindReplaceData
->GetFlags());
452 m_impl
->InitFindWhat(m_FindReplaceData
->GetFindString());
454 bool replace
= HasFlag(wxFR_REPLACEDIALOG
);
457 m_impl
->InitReplaceWith(m_FindReplaceData
->GetReplaceString());
460 // call the right function to show the dialog which does what we want
461 FINDREPLACE
*pFR
= m_impl
->GetPtrFindReplace();
464 hwnd
= ::ReplaceText(pFR
);
466 hwnd
= ::FindText(pFR
);
470 wxLogError(_("Failed to create the standard find/replace dialog (error code %d)"),
471 ::CommDlgExtendedError());
479 // subclass parent window in order to get FINDMSGSTRING message
480 m_impl
->SubclassDialog(GetHwndOf(m_parent
));
482 if ( !::ShowWindow(hwnd
, SW_SHOW
) )
484 wxLogLastError(_T("ShowWindow(find dialog)"));
487 m_hWnd
= (WXHWND
)hwnd
;
492 // ----------------------------------------------------------------------------
493 // wxFindReplaceDialog title handling
494 // ----------------------------------------------------------------------------
496 // we set the title of this dialog in our jook proc but for now don't crash in
497 // the base class version because of m_hWnd == 0
499 void wxFindReplaceDialog::SetTitle( const wxString
& title
)
504 wxString
wxFindReplaceDialog::GetTitle() const
509 // ----------------------------------------------------------------------------
510 // wxFindReplaceDialog position/size
511 // ----------------------------------------------------------------------------
513 void wxFindReplaceDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
514 int WXUNUSED(width
), int WXUNUSED(height
),
515 int WXUNUSED(sizeFlags
))
517 // ignore - we can't change the size of this standard dialog
521 // NB: of course, both of these functions are completely bogus, but it's better
523 void wxFindReplaceDialog::DoGetSize(int *width
, int *height
) const
525 // the standard dialog size
532 void wxFindReplaceDialog::DoGetClientSize(int *width
, int *height
) const
534 // the standard dialog size
541 #endif // wxUSE_FINDREPLDLG