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
; }
85 void InitString(const wxString
& str
, LPTSTR
*ppStr
, WORD
*pLen
);
87 // the owner of the dialog
90 // the previous window proc of our owner
91 WNDPROC m_oldParentWndProc
;
93 // the find replace data used by the dialog
94 FINDREPLACE m_findReplace
;
96 // registered Message for Dialog
97 static UINT ms_msgFindDialog
;
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 wxZeroMemory(m_findReplace
);
129 // translate the flags: first the dialog creation flags
131 // always set this to be able to set the title
132 int flags
= FR_ENABLEHOOK
;
134 int flagsDialog
= dialog
->GetWindowStyle();
135 if ( flagsDialog
& wxFR_NOMATCHCASE
)
136 flags
|= FR_NOMATCHCASE
;
137 if ( flagsDialog
& wxFR_NOWHOLEWORD
)
138 flags
|= FR_NOWHOLEWORD
;
139 if ( flagsDialog
& wxFR_NOUPDOWN
)
140 flags
|= FR_NOUPDOWN
;
142 // and now the flags governing the initial values of the dialogs controls
143 if ( flagsWX
& wxFR_DOWN
)
145 if ( flagsWX
& wxFR_MATCHCASE
)
146 flags
|= FR_MATCHCASE
;
147 if ( flagsWX
& wxFR_WHOLEWORD
)
148 flags
|= FR_WHOLEWORD
;
150 m_findReplace
.lStructSize
= sizeof(FINDREPLACE
);
151 m_findReplace
.hwndOwner
= GetHwndOf(dialog
->GetParent());
152 m_findReplace
.Flags
= flags
;
154 m_findReplace
.lCustData
= (LPARAM
)dialog
;
155 m_findReplace
.lpfnHook
= wxFindReplaceDialogHookProc
;
158 void wxFindReplaceDialogImpl::InitString(const wxString
& str
,
159 LPTSTR
*ppStr
, WORD
*pLen
)
161 size_t len
= str
.length() + 1;
164 // MSDN docs say that the buffer must be at least 80 chars
168 *ppStr
= new wxChar
[len
];
169 wxStrcpy(*ppStr
, str
);
173 void wxFindReplaceDialogImpl::InitFindWhat(const wxString
& str
)
175 InitString(str
, &m_findReplace
.lpstrFindWhat
, &m_findReplace
.wFindWhatLen
);
178 void wxFindReplaceDialogImpl::InitReplaceWith(const wxString
& str
)
181 &m_findReplace
.lpstrReplaceWith
,
182 &m_findReplace
.wReplaceWithLen
);
185 void wxFindReplaceDialogImpl::SubclassDialog(HWND hwnd
)
189 // check that we don't subclass the parent twice: this would be a bad idea
190 // as then we'd have infinite recursion in wxFindReplaceWindowProc
191 WNDPROC oldParentWndProc
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_WNDPROC
);
193 if ( oldParentWndProc
!= wxFindReplaceWindowProc
)
195 // save old wnd proc elsewhere to access it from
196 // wxFindReplaceWindowProc
197 m_oldParentWndProc
= oldParentWndProc
;
198 (void)::SetWindowLong(hwnd
, GWL_USERDATA
, (LONG
)oldParentWndProc
);
200 // and set the new one
201 (void)::SetWindowLong(hwnd
, GWL_WNDPROC
, (LONG
)wxFindReplaceWindowProc
);
205 wxFindReplaceDialogImpl::~wxFindReplaceDialogImpl()
207 delete [] m_findReplace
.lpstrFindWhat
;
208 delete [] m_findReplace
.lpstrReplaceWith
;
212 ::SetWindowLong(m_hwndOwner
, GWL_WNDPROC
, (LONG
)m_oldParentWndProc
);
216 // ----------------------------------------------------------------------------
217 // Window Proc for handling RegisterWindowMessage(FINDMSGSTRING)
218 // ----------------------------------------------------------------------------
220 LRESULT APIENTRY
wxFindReplaceWindowProc(HWND hwnd
, WXUINT nMsg
,
221 WPARAM wParam
, LPARAM lParam
)
223 if ( nMsg
== wxFindReplaceDialogImpl::GetFindDialogMessage() )
225 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
226 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
228 // map flags from Windows
231 bool replace
= FALSE
;
232 if ( pFR
->Flags
& FR_DIALOGTERM
)
234 evtType
= wxEVT_COMMAND_FIND_CLOSE
;
236 else if ( pFR
->Flags
& FR_FINDNEXT
)
238 evtType
= wxEVT_COMMAND_FIND_NEXT
;
240 else if ( pFR
->Flags
& FR_REPLACE
)
242 evtType
= wxEVT_COMMAND_FIND_REPLACE
;
246 else if ( pFR
->Flags
& FR_REPLACEALL
)
248 evtType
= wxEVT_COMMAND_FIND_REPLACE_ALL
;
254 wxFAIL_MSG( _T("unknown find dialog event") );
260 if ( pFR
->Flags
& FR_DOWN
)
262 if ( pFR
->Flags
& FR_WHOLEWORD
)
263 flags
|= wxFR_WHOLEWORD
;
264 if ( pFR
->Flags
& FR_MATCHCASE
)
265 flags
|= wxFR_MATCHCASE
;
267 wxFindDialogEvent
event(evtType
, dialog
->GetId());
268 event
.SetEventObject(dialog
);
269 event
.SetFlags(flags
);
270 event
.SetFindString(pFR
->lpstrFindWhat
);
273 event
.SetReplaceString(pFR
->lpstrReplaceWith
);
279 WNDPROC wndProc
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_USERDATA
);
282 wxASSERT_MSG( wndProc
!= wxFindReplaceWindowProc
,
283 _T("infinite recursion detected") );
285 return ::CallWindowProc(wndProc
, hwnd
, nMsg
, wParam
, lParam
);
288 // ----------------------------------------------------------------------------
289 // Find/replace dialog hook proc
290 // ----------------------------------------------------------------------------
292 UINT CALLBACK
wxFindReplaceDialogHookProc(HWND hwnd
,
294 WPARAM
WXUNUSED(wParam
),
297 if ( uiMsg
== WM_INITDIALOG
)
299 FINDREPLACE
*pFR
= (FINDREPLACE
*)lParam
;
300 wxFindReplaceDialog
*dialog
= (wxFindReplaceDialog
*)pFR
->lCustData
;
302 ::SetWindowText(hwnd
, dialog
->GetTitle());
304 // don't return FALSE from here or the dialog won't be shown
311 // ============================================================================
312 // wxFindReplaceDialog implementation
313 // ============================================================================
315 // ----------------------------------------------------------------------------
316 // wxFindReplaceDialog ctors/dtor
317 // ----------------------------------------------------------------------------
319 void wxFindReplaceDialog::Init()
322 m_FindReplaceData
= NULL
;
324 // as we're created in the hidden state, bring the internal flag in sync
328 wxFindReplaceDialog::wxFindReplaceDialog(wxWindow
*parent
,
329 wxFindReplaceData
*data
,
330 const wxString
&title
,
332 : wxFindReplaceDialogBase(parent
, data
, title
, flags
)
336 (void)Create(parent
, data
, title
, flags
);
339 wxFindReplaceDialog::~wxFindReplaceDialog()
341 // unsubclass the parent
344 // prevent the base class dtor from trying to hide us!
347 // and from destroying our window
348 m_hWnd
= (WXHWND
)NULL
;
351 bool wxFindReplaceDialog::Create(wxWindow
*parent
,
352 wxFindReplaceData
*data
,
353 const wxString
&title
,
356 m_windowStyle
= flags
;
357 m_FindReplaceData
= data
;
362 // we must have a parent as it will get the messages from us
363 return parent
!= NULL
;
366 // ----------------------------------------------------------------------------
367 // wxFindReplaceData show/hide
368 // ----------------------------------------------------------------------------
370 bool wxFindReplaceDialog::Show(bool show
)
372 if ( !wxWindowBase::Show(show
) )
374 // visibility status didn't change
378 // do we already have the dialog window?
382 (void)::ShowWindow(GetHwnd(), show
? SW_SHOW
: SW_HIDE
);
389 // well, it doesn't exist which is as good as being hidden
393 wxCHECK_MSG( m_FindReplaceData
, FALSE
, _T("call Create() first!") );
395 wxASSERT_MSG( !m_impl
, _T("why don't we have the window then?") );
397 m_impl
= new wxFindReplaceDialogImpl(this, m_FindReplaceData
->GetFlags());
399 m_impl
->InitFindWhat(m_FindReplaceData
->GetFindString());
401 bool replace
= HasFlag(wxFR_REPLACEDIALOG
);
404 m_impl
->InitReplaceWith(m_FindReplaceData
->GetReplaceString());
407 // call the right function to show the dialog which does what we want
408 FINDREPLACE
*pFR
= m_impl
->GetPtrFindReplace();
411 hwnd
= ::ReplaceText(pFR
);
413 hwnd
= ::FindText(pFR
);
417 wxLogError(_("Failed to create the standard find/replace dialog (error code %d)"),
418 ::CommDlgExtendedError());
426 // subclass parent window in order to get FINDMSGSTRING message
427 m_impl
->SubclassDialog(GetHwndOf(m_parent
));
429 if ( !::ShowWindow(hwnd
, SW_SHOW
) )
431 wxLogLastError(_T("ShowWindow(find dialog)"));
434 m_hWnd
= (WXHWND
)hwnd
;
439 // ----------------------------------------------------------------------------
440 // wxFindReplaceDialog title handling
441 // ----------------------------------------------------------------------------
443 // we set the title of this dialog in our jook proc but for now don't crash in
444 // the base class version because of m_hWnd == 0
446 void wxFindReplaceDialog::SetTitle( const wxString
& title
)
451 wxString
wxFindReplaceDialog::GetTitle() const
456 // ----------------------------------------------------------------------------
457 // wxFindReplaceDialog position/size
458 // ----------------------------------------------------------------------------
460 void wxFindReplaceDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
461 int WXUNUSED(width
), int WXUNUSED(height
),
462 int WXUNUSED(sizeFlags
))
464 // ignore - we can't change the size of this standard dialog
468 // NB: of course, both of these functions are completely bogus, but it's better
470 void wxFindReplaceDialog::DoGetSize(int *width
, int *height
) const
472 // the standard dialog size
479 void wxFindReplaceDialog::DoGetClientSize(int *width
, int *height
) const
481 // the standard dialog size
488 #endif // wxUSE_FINDREPLDLG