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 // ----------------------------------------------------------------------------
21 #pragma implementation "mswfdrepdlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/msw/private.h"
40 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
44 #include "wx/fdrepdlg.h"
46 // ----------------------------------------------------------------------------
47 // functions prototypes
48 // ----------------------------------------------------------------------------
50 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
51 WPARAM wParam
, LPARAM lParam
);
53 UINT CALLBACK
wxFindReplaceDialogHookProc(HWND hwnd
,
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxFindReplaceDialog
, wxDialog
)
64 // ----------------------------------------------------------------------------
65 // wxFindReplaceDialogImpl: the internals of wxFindReplaceDialog
66 // ----------------------------------------------------------------------------
68 class WXDLLEXPORT wxFindReplaceDialogImpl
71 wxFindReplaceDialogImpl(wxFindReplaceDialog
*dialog
, int flagsWX
);
72 ~wxFindReplaceDialogImpl();
74 void InitFindWhat(const wxString
& str
);
75 void InitReplaceWith(const wxString
& str
);
77 void SubclassDialog(HWND hwnd
);
79 static UINT
GetFindDialogMessage() { return ms_msgFindDialog
; }
81 // only for passing to ::FindText or ::ReplaceText
82 FINDREPLACE
*GetPtrFindReplace() { return &m_findReplace
; }
84 // set/query the "closed by user" flag
85 void SetClosedByUser() { m_wasClosedByUser
= TRUE
; }
86 bool WasClosedByUser() const { return m_wasClosedByUser
; }
89 void InitString(const wxString
& str
, LPTSTR
*ppStr
, WORD
*pLen
);
91 // the owner of the dialog
94 // the previous window proc of our owner
95 WNDPROC m_oldParentWndProc
;
97 // the find replace data used by the dialog
98 FINDREPLACE m_findReplace
;
100 // TRUE if the user closed us, FALSE otherwise
101 bool m_wasClosedByUser
;
103 // registered Message for Dialog
104 static UINT ms_msgFindDialog
;
107 UINT
wxFindReplaceDialogImpl::ms_msgFindDialog
= 0;
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
114 // wxFindReplaceDialogImpl
115 // ----------------------------------------------------------------------------
117 wxFindReplaceDialogImpl::wxFindReplaceDialogImpl(wxFindReplaceDialog
*dialog
,
120 // get the identifier for the find dialog message if we don't have it yet
121 if ( !ms_msgFindDialog
)
123 ms_msgFindDialog
= ::RegisterWindowMessage(FINDMSGSTRING
);
125 if ( !ms_msgFindDialog
)
127 wxLogLastError(_T("RegisterWindowMessage(FINDMSGSTRING)"));
132 m_oldParentWndProc
= NULL
;
134 m_wasClosedByUser
= FALSE
;
136 wxZeroMemory(m_findReplace
);
138 // translate the flags: first the dialog creation flags
140 // always set this to be able to set the title
141 int flags
= FR_ENABLEHOOK
;
143 int flagsDialog
= dialog
->GetWindowStyle();
144 if ( flagsDialog
& wxFR_NOMATCHCASE
)
145 flags
|= FR_NOMATCHCASE
;
146 if ( flagsDialog
& wxFR_NOWHOLEWORD
)
147 flags
|= FR_NOWHOLEWORD
;
148 if ( flagsDialog
& wxFR_NOUPDOWN
)
149 flags
|= FR_NOUPDOWN
;
151 // and now the flags governing the initial values of the dialogs controls
152 if ( flagsWX
& wxFR_DOWN
)
154 if ( flagsWX
& wxFR_MATCHCASE
)
155 flags
|= FR_MATCHCASE
;
156 if ( flagsWX
& wxFR_WHOLEWORD
)
157 flags
|= FR_WHOLEWORD
;
159 m_findReplace
.lStructSize
= sizeof(FINDREPLACE
);
160 m_findReplace
.hwndOwner
= GetHwndOf(dialog
->GetParent());
161 m_findReplace
.Flags
= flags
;
163 m_findReplace
.lCustData
= (LPARAM
)dialog
;
164 m_findReplace
.lpfnHook
= wxFindReplaceDialogHookProc
;
167 void wxFindReplaceDialogImpl::InitString(const wxString
& str
,
168 LPTSTR
*ppStr
, WORD
*pLen
)
170 size_t len
= str
.length() + 1;
173 // MSDN docs say that the buffer must be at least 80 chars
177 *ppStr
= new wxChar
[len
];
178 wxStrcpy(*ppStr
, str
);
182 void wxFindReplaceDialogImpl::InitFindWhat(const wxString
& str
)
184 InitString(str
, &m_findReplace
.lpstrFindWhat
, &m_findReplace
.wFindWhatLen
);
187 void wxFindReplaceDialogImpl::InitReplaceWith(const wxString
& str
)
190 &m_findReplace
.lpstrReplaceWith
,
191 &m_findReplace
.wReplaceWithLen
);
194 void wxFindReplaceDialogImpl::SubclassDialog(HWND hwnd
)
198 // check that we don't subclass the parent twice: this would be a bad idea
199 // as then we'd have infinite recursion in wxFindReplaceWindowProc
200 WNDPROC oldParentWndProc
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_WNDPROC
);
202 if ( oldParentWndProc
!= wxFindReplaceWindowProc
)
204 // save old wnd proc elsewhere to access it from
205 // wxFindReplaceWindowProc
206 m_oldParentWndProc
= oldParentWndProc
;
207 (void)::SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
)oldParentWndProc
);
209 // and set the new one
210 (void)::SetWindowLong(hwnd
, GWL_WNDPROC
, (LONG
)wxFindReplaceWindowProc
);
214 wxFindReplaceDialogImpl::~wxFindReplaceDialogImpl()
216 delete [] m_findReplace
.lpstrFindWhat
;
217 delete [] m_findReplace
.lpstrReplaceWith
;
221 ::SetWindowLong(m_hwndOwner
, GWL_WNDPROC
, (LONG
)m_oldParentWndProc
);
225 // ----------------------------------------------------------------------------
226 // Window Proc for handling RegisterWindowMessage(FINDMSGSTRING)
227 // ----------------------------------------------------------------------------
229 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
230 WPARAM wParam
, LPARAM lParam
)
232 if ( nMsg
== wxFindReplaceDialogImpl::GetFindDialogMessage() )
234 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
235 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
237 // map flags from Windows
240 bool replace
= FALSE
;
241 if ( pFR
->Flags
& FR_DIALOGTERM
)
243 // we have to notify the dialog that it's being closed by user and
244 // not deleted programmatically as it behaves differently in these
246 dialog
->GetImpl()->SetClosedByUser();
248 evtType
= wxEVT_COMMAND_FIND_CLOSE
;
250 else if ( pFR
->Flags
& FR_FINDNEXT
)
252 evtType
= wxEVT_COMMAND_FIND_NEXT
;
254 else if ( pFR
->Flags
& FR_REPLACE
)
256 evtType
= wxEVT_COMMAND_FIND_REPLACE
;
260 else if ( pFR
->Flags
& FR_REPLACEALL
)
262 evtType
= wxEVT_COMMAND_FIND_REPLACE_ALL
;
268 wxFAIL_MSG( _T("unknown find dialog event") );
274 if ( pFR
->Flags
& FR_DOWN
)
276 if ( pFR
->Flags
& FR_WHOLEWORD
)
277 flags
|= wxFR_WHOLEWORD
;
278 if ( pFR
->Flags
& FR_MATCHCASE
)
279 flags
|= wxFR_MATCHCASE
;
281 wxFindDialogEvent
event(evtType
, dialog
->GetId());
282 event
.SetEventObject(dialog
);
283 event
.SetFlags(flags
);
284 event
.SetFindString(pFR
->lpstrFindWhat
);
287 event
.SetReplaceString(pFR
->lpstrReplaceWith
);
293 WNDPROC wndProc
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_USERDATA
);
296 wxASSERT_MSG( wndProc
!= wxFindReplaceWindowProc
,
297 _T("infinite recursion detected") );
299 return ::CallWindowProc(wndProc
, hwnd
, nMsg
, wParam
, lParam
);
302 // ----------------------------------------------------------------------------
303 // Find/replace dialog hook proc
304 // ----------------------------------------------------------------------------
306 UINT CALLBACK
wxFindReplaceDialogHookProc(HWND hwnd
,
308 WPARAM
WXUNUSED(wParam
),
311 if ( uiMsg
== WM_INITDIALOG
)
313 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
314 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
316 ::SetWindowText(hwnd
, dialog
->GetTitle());
318 // don't return FALSE from here or the dialog won't be shown
325 // ============================================================================
326 // wxFindReplaceDialog implementation
327 // ============================================================================
329 // ----------------------------------------------------------------------------
330 // wxFindReplaceDialog ctors/dtor
331 // ----------------------------------------------------------------------------
333 void wxFindReplaceDialog::Init()
336 m_FindReplaceData
= NULL
;
338 // as we're created in the hidden state, bring the internal flag in sync
342 wxFindReplaceDialog::wxFindReplaceDialog(wxWindow
*parent
,
343 wxFindReplaceData
*data
,
344 const wxString
&title
,
346 : wxFindReplaceDialogBase(parent
, data
, title
, flags
)
350 (void)Create(parent
, data
, title
, flags
);
353 wxFindReplaceDialog::~wxFindReplaceDialog()
355 // the dialog might have been already deleted if the user closed it
356 // manually but in this case we should have got a notification about it and
357 // the flagmust have been set
358 if ( !m_impl
->WasClosedByUser() )
360 // if it wasn't, delete the dialog ourselves
361 if ( !::DestroyWindow(GetHwnd()) )
363 wxLogLastError(_T("DestroyWindow(find dialog)"));
367 // unsubclass the parent
370 // prevent the base class dtor from trying to hide us!
373 // and from destroying our window [again]
374 m_hWnd
= (WXHWND
)NULL
;
377 bool wxFindReplaceDialog::Create(wxWindow
*parent
,
378 wxFindReplaceData
*data
,
379 const wxString
&title
,
382 m_windowStyle
= flags
;
383 m_FindReplaceData
= data
;
388 // we must have a parent as it will get the messages from us
389 return parent
!= NULL
;
392 // ----------------------------------------------------------------------------
393 // wxFindReplaceData show/hide
394 // ----------------------------------------------------------------------------
396 bool wxFindReplaceDialog::Show(bool show
)
398 if ( !wxWindowBase::Show(show
) )
400 // visibility status didn't change
404 // do we already have the dialog window?
408 (void)::ShowWindow(GetHwnd(), show
? SW_SHOW
: SW_HIDE
);
415 // well, it doesn't exist which is as good as being hidden
419 wxCHECK_MSG( m_FindReplaceData
, FALSE
, _T("call Create() first!") );
421 wxASSERT_MSG( !m_impl
, _T("why don't we have the window then?") );
423 m_impl
= new wxFindReplaceDialogImpl(this, m_FindReplaceData
->GetFlags());
425 m_impl
->InitFindWhat(m_FindReplaceData
->GetFindString());
427 bool replace
= HasFlag(wxFR_REPLACEDIALOG
);
430 m_impl
->InitReplaceWith(m_FindReplaceData
->GetReplaceString());
433 // call the right function to show the dialog which does what we want
434 FINDREPLACE
*pFR
= m_impl
->GetPtrFindReplace();
437 hwnd
= ::ReplaceText(pFR
);
439 hwnd
= ::FindText(pFR
);
443 wxLogError(_("Failed to create the standard find/replace dialog (error code %d)"),
444 ::CommDlgExtendedError());
452 // subclass parent window in order to get FINDMSGSTRING message
453 m_impl
->SubclassDialog(GetHwndOf(m_parent
));
455 if ( !::ShowWindow(hwnd
, SW_SHOW
) )
457 wxLogLastError(_T("ShowWindow(find dialog)"));
460 m_hWnd
= (WXHWND
)hwnd
;
465 // ----------------------------------------------------------------------------
466 // wxFindReplaceDialog title handling
467 // ----------------------------------------------------------------------------
469 // we set the title of this dialog in our jook proc but for now don't crash in
470 // the base class version because of m_hWnd == 0
472 void wxFindReplaceDialog::SetTitle( const wxString
& title
)
477 wxString
wxFindReplaceDialog::GetTitle() const
482 // ----------------------------------------------------------------------------
483 // wxFindReplaceDialog position/size
484 // ----------------------------------------------------------------------------
486 void wxFindReplaceDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
487 int WXUNUSED(width
), int WXUNUSED(height
),
488 int WXUNUSED(sizeFlags
))
490 // ignore - we can't change the size of this standard dialog
494 // NB: of course, both of these functions are completely bogus, but it's better
496 void wxFindReplaceDialog::DoGetSize(int *width
, int *height
) const
498 // the standard dialog size
505 void wxFindReplaceDialog::DoGetClientSize(int *width
, int *height
) const
507 // the standard dialog size
514 #endif // wxUSE_FINDREPLDLG