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__)
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
;
106 DECLARE_NO_COPY_CLASS(wxFindReplaceDialogImpl
)
109 UINT
wxFindReplaceDialogImpl::ms_msgFindDialog
= 0;
111 // ============================================================================
113 // ============================================================================
115 // ----------------------------------------------------------------------------
116 // wxFindReplaceDialogImpl
117 // ----------------------------------------------------------------------------
119 wxFindReplaceDialogImpl::wxFindReplaceDialogImpl(wxFindReplaceDialog
*dialog
,
122 // get the identifier for the find dialog message if we don't have it yet
123 if ( !ms_msgFindDialog
)
125 ms_msgFindDialog
= ::RegisterWindowMessage(FINDMSGSTRING
);
127 if ( !ms_msgFindDialog
)
129 wxLogLastError(_T("RegisterWindowMessage(FINDMSGSTRING)"));
134 m_oldParentWndProc
= NULL
;
136 m_wasClosedByUser
= FALSE
;
138 wxZeroMemory(m_findReplace
);
140 // translate the flags: first the dialog creation flags
142 // always set this to be able to set the title
143 int flags
= FR_ENABLEHOOK
;
145 int flagsDialog
= dialog
->GetWindowStyle();
146 if ( flagsDialog
& wxFR_NOMATCHCASE
)
147 flags
|= FR_NOMATCHCASE
;
148 if ( flagsDialog
& wxFR_NOWHOLEWORD
)
149 flags
|= FR_NOWHOLEWORD
;
150 if ( flagsDialog
& wxFR_NOUPDOWN
)
151 flags
|= FR_NOUPDOWN
;
153 // and now the flags governing the initial values of the dialogs controls
154 if ( flagsWX
& wxFR_DOWN
)
156 if ( flagsWX
& wxFR_MATCHCASE
)
157 flags
|= FR_MATCHCASE
;
158 if ( flagsWX
& wxFR_WHOLEWORD
)
159 flags
|= FR_WHOLEWORD
;
161 m_findReplace
.lStructSize
= sizeof(FINDREPLACE
);
162 m_findReplace
.hwndOwner
= GetHwndOf(dialog
->GetParent());
163 m_findReplace
.Flags
= flags
;
165 m_findReplace
.lCustData
= (LPARAM
)dialog
;
166 m_findReplace
.lpfnHook
= wxFindReplaceDialogHookProc
;
169 void wxFindReplaceDialogImpl::InitString(const wxString
& str
,
170 LPTSTR
*ppStr
, WORD
*pLen
)
172 size_t len
= str
.length() + 1;
175 // MSDN docs say that the buffer must be at least 80 chars
179 *ppStr
= new wxChar
[len
];
180 wxStrcpy(*ppStr
, str
);
184 void wxFindReplaceDialogImpl::InitFindWhat(const wxString
& str
)
186 InitString(str
, &m_findReplace
.lpstrFindWhat
, &m_findReplace
.wFindWhatLen
);
189 void wxFindReplaceDialogImpl::InitReplaceWith(const wxString
& str
)
192 &m_findReplace
.lpstrReplaceWith
,
193 &m_findReplace
.wReplaceWithLen
);
196 void wxFindReplaceDialogImpl::SubclassDialog(HWND hwnd
)
200 // check that we don't subclass the parent twice: this would be a bad idea
201 // as then we'd have infinite recursion in wxFindReplaceWindowProc
202 if ( !wxCheckWindowWndProc((WXHWND
)hwnd
, (WXFARPROC
)wxFindReplaceWindowProc
) )
204 WNDPROC oldParentWndProc
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_WNDPROC
);
205 // save old wnd proc elsewhere to access it from
206 // wxFindReplaceWindowProc
207 m_oldParentWndProc
= oldParentWndProc
;
208 (void)::SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
)oldParentWndProc
);
210 // and set the new one
211 (void)::SetWindowLong(hwnd
, GWL_WNDPROC
, (LONG
)wxFindReplaceWindowProc
);
215 wxFindReplaceDialogImpl::~wxFindReplaceDialogImpl()
217 delete [] m_findReplace
.lpstrFindWhat
;
218 delete [] m_findReplace
.lpstrReplaceWith
;
222 ::SetWindowLong(m_hwndOwner
, GWL_WNDPROC
, (LONG
)m_oldParentWndProc
);
226 // ----------------------------------------------------------------------------
227 // Window Proc for handling RegisterWindowMessage(FINDMSGSTRING)
228 // ----------------------------------------------------------------------------
230 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
231 WPARAM wParam
, LPARAM lParam
)
233 if ( nMsg
== wxFindReplaceDialogImpl::GetFindDialogMessage() )
235 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
236 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
238 // map flags from Windows
241 bool replace
= FALSE
;
242 if ( pFR
->Flags
& FR_DIALOGTERM
)
244 // we have to notify the dialog that it's being closed by user and
245 // not deleted programmatically as it behaves differently in these
247 dialog
->GetImpl()->SetClosedByUser();
249 evtType
= wxEVT_COMMAND_FIND_CLOSE
;
251 else if ( pFR
->Flags
& FR_FINDNEXT
)
253 evtType
= wxEVT_COMMAND_FIND_NEXT
;
255 else if ( pFR
->Flags
& FR_REPLACE
)
257 evtType
= wxEVT_COMMAND_FIND_REPLACE
;
261 else if ( pFR
->Flags
& FR_REPLACEALL
)
263 evtType
= wxEVT_COMMAND_FIND_REPLACE_ALL
;
269 wxFAIL_MSG( _T("unknown find dialog event") );
275 if ( pFR
->Flags
& FR_DOWN
)
277 if ( pFR
->Flags
& FR_WHOLEWORD
)
278 flags
|= wxFR_WHOLEWORD
;
279 if ( pFR
->Flags
& FR_MATCHCASE
)
280 flags
|= wxFR_MATCHCASE
;
282 wxFindDialogEvent
event(evtType
, dialog
->GetId());
283 event
.SetEventObject(dialog
);
284 event
.SetFlags(flags
);
285 event
.SetFindString(pFR
->lpstrFindWhat
);
288 event
.SetReplaceString(pFR
->lpstrReplaceWith
);
294 WNDPROC wndProc
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_USERDATA
);
297 wxASSERT_MSG( wndProc
!= wxFindReplaceWindowProc
,
298 _T("infinite recursion detected") );
300 return ::CallWindowProc(wndProc
, hwnd
, nMsg
, wParam
, lParam
);
303 // ----------------------------------------------------------------------------
304 // Find/replace dialog hook proc
305 // ----------------------------------------------------------------------------
307 UINT CALLBACK
wxFindReplaceDialogHookProc(HWND hwnd
,
309 WPARAM
WXUNUSED(wParam
),
312 if ( uiMsg
== WM_INITDIALOG
)
314 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
315 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
317 ::SetWindowText(hwnd
, dialog
->GetTitle());
319 // don't return FALSE from here or the dialog won't be shown
326 // ============================================================================
327 // wxFindReplaceDialog implementation
328 // ============================================================================
330 // ----------------------------------------------------------------------------
331 // wxFindReplaceDialog ctors/dtor
332 // ----------------------------------------------------------------------------
334 void wxFindReplaceDialog::Init()
337 m_FindReplaceData
= NULL
;
339 // as we're created in the hidden state, bring the internal flag in sync
343 wxFindReplaceDialog::wxFindReplaceDialog(wxWindow
*parent
,
344 wxFindReplaceData
*data
,
345 const wxString
&title
,
347 : wxFindReplaceDialogBase(parent
, data
, title
, flags
)
351 (void)Create(parent
, data
, title
, flags
);
354 wxFindReplaceDialog::~wxFindReplaceDialog()
356 // the dialog might have been already deleted if the user closed it
357 // manually but in this case we should have got a notification about it and
358 // the flagmust have been set
359 if ( !m_impl
->WasClosedByUser() )
361 // if it wasn't, delete the dialog ourselves
362 if ( !::DestroyWindow(GetHwnd()) )
364 wxLogLastError(_T("DestroyWindow(find dialog)"));
368 // unsubclass the parent
371 // prevent the base class dtor from trying to hide us!
374 // and from destroying our window [again]
375 m_hWnd
= (WXHWND
)NULL
;
378 bool wxFindReplaceDialog::Create(wxWindow
*parent
,
379 wxFindReplaceData
*data
,
380 const wxString
&title
,
383 m_windowStyle
= flags
;
384 m_FindReplaceData
= data
;
389 // we must have a parent as it will get the messages from us
390 return parent
!= NULL
;
393 // ----------------------------------------------------------------------------
394 // wxFindReplaceData show/hide
395 // ----------------------------------------------------------------------------
397 bool wxFindReplaceDialog::Show(bool show
)
399 if ( !wxWindowBase::Show(show
) )
401 // visibility status didn't change
405 // do we already have the dialog window?
409 (void)::ShowWindow(GetHwnd(), show
? SW_SHOW
: SW_HIDE
);
416 // well, it doesn't exist which is as good as being hidden
420 wxCHECK_MSG( m_FindReplaceData
, FALSE
, _T("call Create() first!") );
422 wxASSERT_MSG( !m_impl
, _T("why don't we have the window then?") );
424 m_impl
= new wxFindReplaceDialogImpl(this, m_FindReplaceData
->GetFlags());
426 m_impl
->InitFindWhat(m_FindReplaceData
->GetFindString());
428 bool replace
= HasFlag(wxFR_REPLACEDIALOG
);
431 m_impl
->InitReplaceWith(m_FindReplaceData
->GetReplaceString());
434 // call the right function to show the dialog which does what we want
435 FINDREPLACE
*pFR
= m_impl
->GetPtrFindReplace();
438 hwnd
= ::ReplaceText(pFR
);
440 hwnd
= ::FindText(pFR
);
444 wxLogError(_("Failed to create the standard find/replace dialog (error code %d)"),
445 ::CommDlgExtendedError());
453 // subclass parent window in order to get FINDMSGSTRING message
454 m_impl
->SubclassDialog(GetHwndOf(m_parent
));
456 if ( !::ShowWindow(hwnd
, SW_SHOW
) )
458 wxLogLastError(_T("ShowWindow(find dialog)"));
461 m_hWnd
= (WXHWND
)hwnd
;
466 // ----------------------------------------------------------------------------
467 // wxFindReplaceDialog title handling
468 // ----------------------------------------------------------------------------
470 // we set the title of this dialog in our jook proc but for now don't crash in
471 // the base class version because of m_hWnd == 0
473 void wxFindReplaceDialog::SetTitle( const wxString
& title
)
478 wxString
wxFindReplaceDialog::GetTitle() const
483 // ----------------------------------------------------------------------------
484 // wxFindReplaceDialog position/size
485 // ----------------------------------------------------------------------------
487 void wxFindReplaceDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
488 int WXUNUSED(width
), int WXUNUSED(height
),
489 int WXUNUSED(sizeFlags
))
491 // ignore - we can't change the size of this standard dialog
495 // NB: of course, both of these functions are completely bogus, but it's better
497 void wxFindReplaceDialog::DoGetSize(int *width
, int *height
) const
499 // the standard dialog size
506 void wxFindReplaceDialog::DoGetClientSize(int *width
, int *height
) const
508 // the standard dialog size
515 #endif // wxUSE_FINDREPLDLG