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 if ( !wxCheckWindowWndProc((WXHWND
)hwnd
, (WXFARPROC
)wxFindReplaceWindowProc
) )
202 WNDPROC oldParentWndProc
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_WNDPROC
);
203 // save old wnd proc elsewhere to access it from
204 // wxFindReplaceWindowProc
205 m_oldParentWndProc
= oldParentWndProc
;
206 (void)::SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
)oldParentWndProc
);
208 // and set the new one
209 (void)::SetWindowLong(hwnd
, GWL_WNDPROC
, (LONG
)wxFindReplaceWindowProc
);
213 wxFindReplaceDialogImpl::~wxFindReplaceDialogImpl()
215 delete [] m_findReplace
.lpstrFindWhat
;
216 delete [] m_findReplace
.lpstrReplaceWith
;
220 ::SetWindowLong(m_hwndOwner
, GWL_WNDPROC
, (LONG
)m_oldParentWndProc
);
224 // ----------------------------------------------------------------------------
225 // Window Proc for handling RegisterWindowMessage(FINDMSGSTRING)
226 // ----------------------------------------------------------------------------
228 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
229 WPARAM wParam
, LPARAM lParam
)
231 if ( nMsg
== wxFindReplaceDialogImpl::GetFindDialogMessage() )
233 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
234 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
236 // map flags from Windows
239 bool replace
= FALSE
;
240 if ( pFR
->Flags
& FR_DIALOGTERM
)
242 // we have to notify the dialog that it's being closed by user and
243 // not deleted programmatically as it behaves differently in these
245 dialog
->GetImpl()->SetClosedByUser();
247 evtType
= wxEVT_COMMAND_FIND_CLOSE
;
249 else if ( pFR
->Flags
& FR_FINDNEXT
)
251 evtType
= wxEVT_COMMAND_FIND_NEXT
;
253 else if ( pFR
->Flags
& FR_REPLACE
)
255 evtType
= wxEVT_COMMAND_FIND_REPLACE
;
259 else if ( pFR
->Flags
& FR_REPLACEALL
)
261 evtType
= wxEVT_COMMAND_FIND_REPLACE_ALL
;
267 wxFAIL_MSG( _T("unknown find dialog event") );
273 if ( pFR
->Flags
& FR_DOWN
)
275 if ( pFR
->Flags
& FR_WHOLEWORD
)
276 flags
|= wxFR_WHOLEWORD
;
277 if ( pFR
->Flags
& FR_MATCHCASE
)
278 flags
|= wxFR_MATCHCASE
;
280 wxFindDialogEvent
event(evtType
, dialog
->GetId());
281 event
.SetEventObject(dialog
);
282 event
.SetFlags(flags
);
283 event
.SetFindString(pFR
->lpstrFindWhat
);
286 event
.SetReplaceString(pFR
->lpstrReplaceWith
);
292 WNDPROC wndProc
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_USERDATA
);
295 wxASSERT_MSG( wndProc
!= wxFindReplaceWindowProc
,
296 _T("infinite recursion detected") );
298 return ::CallWindowProc(wndProc
, hwnd
, nMsg
, wParam
, lParam
);
301 // ----------------------------------------------------------------------------
302 // Find/replace dialog hook proc
303 // ----------------------------------------------------------------------------
305 UINT CALLBACK
wxFindReplaceDialogHookProc(HWND hwnd
,
307 WPARAM
WXUNUSED(wParam
),
310 if ( uiMsg
== WM_INITDIALOG
)
312 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
313 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
315 ::SetWindowText(hwnd
, dialog
->GetTitle());
317 // don't return FALSE from here or the dialog won't be shown
324 // ============================================================================
325 // wxFindReplaceDialog implementation
326 // ============================================================================
328 // ----------------------------------------------------------------------------
329 // wxFindReplaceDialog ctors/dtor
330 // ----------------------------------------------------------------------------
332 void wxFindReplaceDialog::Init()
335 m_FindReplaceData
= NULL
;
337 // as we're created in the hidden state, bring the internal flag in sync
341 wxFindReplaceDialog::wxFindReplaceDialog(wxWindow
*parent
,
342 wxFindReplaceData
*data
,
343 const wxString
&title
,
345 : wxFindReplaceDialogBase(parent
, data
, title
, flags
)
349 (void)Create(parent
, data
, title
, flags
);
352 wxFindReplaceDialog::~wxFindReplaceDialog()
354 // the dialog might have been already deleted if the user closed it
355 // manually but in this case we should have got a notification about it and
356 // the flagmust have been set
357 if ( !m_impl
->WasClosedByUser() )
359 // if it wasn't, delete the dialog ourselves
360 if ( !::DestroyWindow(GetHwnd()) )
362 wxLogLastError(_T("DestroyWindow(find dialog)"));
366 // unsubclass the parent
369 // prevent the base class dtor from trying to hide us!
372 // and from destroying our window [again]
373 m_hWnd
= (WXHWND
)NULL
;
376 bool wxFindReplaceDialog::Create(wxWindow
*parent
,
377 wxFindReplaceData
*data
,
378 const wxString
&title
,
381 m_windowStyle
= flags
;
382 m_FindReplaceData
= data
;
387 // we must have a parent as it will get the messages from us
388 return parent
!= NULL
;
391 // ----------------------------------------------------------------------------
392 // wxFindReplaceData show/hide
393 // ----------------------------------------------------------------------------
395 bool wxFindReplaceDialog::Show(bool show
)
397 if ( !wxWindowBase::Show(show
) )
399 // visibility status didn't change
403 // do we already have the dialog window?
407 (void)::ShowWindow(GetHwnd(), show
? SW_SHOW
: SW_HIDE
);
414 // well, it doesn't exist which is as good as being hidden
418 wxCHECK_MSG( m_FindReplaceData
, FALSE
, _T("call Create() first!") );
420 wxASSERT_MSG( !m_impl
, _T("why don't we have the window then?") );
422 m_impl
= new wxFindReplaceDialogImpl(this, m_FindReplaceData
->GetFlags());
424 m_impl
->InitFindWhat(m_FindReplaceData
->GetFindString());
426 bool replace
= HasFlag(wxFR_REPLACEDIALOG
);
429 m_impl
->InitReplaceWith(m_FindReplaceData
->GetReplaceString());
432 // call the right function to show the dialog which does what we want
433 FINDREPLACE
*pFR
= m_impl
->GetPtrFindReplace();
436 hwnd
= ::ReplaceText(pFR
);
438 hwnd
= ::FindText(pFR
);
442 wxLogError(_("Failed to create the standard find/replace dialog (error code %d)"),
443 ::CommDlgExtendedError());
451 // subclass parent window in order to get FINDMSGSTRING message
452 m_impl
->SubclassDialog(GetHwndOf(m_parent
));
454 if ( !::ShowWindow(hwnd
, SW_SHOW
) )
456 wxLogLastError(_T("ShowWindow(find dialog)"));
459 m_hWnd
= (WXHWND
)hwnd
;
464 // ----------------------------------------------------------------------------
465 // wxFindReplaceDialog title handling
466 // ----------------------------------------------------------------------------
468 // we set the title of this dialog in our jook proc but for now don't crash in
469 // the base class version because of m_hWnd == 0
471 void wxFindReplaceDialog::SetTitle( const wxString
& title
)
476 wxString
wxFindReplaceDialog::GetTitle() const
481 // ----------------------------------------------------------------------------
482 // wxFindReplaceDialog position/size
483 // ----------------------------------------------------------------------------
485 void wxFindReplaceDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
486 int WXUNUSED(width
), int WXUNUSED(height
),
487 int WXUNUSED(sizeFlags
))
489 // ignore - we can't change the size of this standard dialog
493 // NB: of course, both of these functions are completely bogus, but it's better
495 void wxFindReplaceDialog::DoGetSize(int *width
, int *height
) const
497 // the standard dialog size
504 void wxFindReplaceDialog::DoGetClientSize(int *width
, int *height
) const
506 // the standard dialog size
513 #endif // wxUSE_FINDREPLDLG