]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fdrepdlg.cpp
simplify code so it always returns the same object
[wxWidgets.git] / src / msw / fdrepdlg.cpp
CommitLineData
c41b00c9
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/fdrepdlg.cpp
3// Purpose: wxFindReplaceDialog class
8db37e06
VZ
4// Author: Markus Greither and Vadim Zeitlin
5// Modified by:
c41b00c9 6// Created: 23/03/2001
57bd4c60 7// RCS-ID: $Id$
c41b00c9 8// Copyright: (c) Markus Greither
65571936 9// Licence: wxWindows licence
c41b00c9
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c41b00c9
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#if wxUSE_FINDREPLDLG
28
29#ifndef WX_PRECOMP
57bd4c60 30 #include "wx/msw/wrapcdlg.h"
c41b00c9
VZ
31 #include "wx/intl.h"
32 #include "wx/log.h"
33#endif
34
c41b00c9
VZ
35#include "wx/fdrepdlg.h"
36
ccddcbac
RD
37#include "wx/msw/mslu.h"
38
c41b00c9
VZ
39// ----------------------------------------------------------------------------
40// functions prototypes
41// ----------------------------------------------------------------------------
42
975b6bcf
VZ
43UINT_PTR CALLBACK wxFindReplaceDialogHookProc(HWND hwnd,
44 UINT uiMsg,
45 WPARAM wParam,
46 LPARAM lParam);
c41b00c9
VZ
47
48// ----------------------------------------------------------------------------
49// wxWin macros
50// ----------------------------------------------------------------------------
51
52IMPLEMENT_DYNAMIC_CLASS(wxFindReplaceDialog, wxDialog)
53
c41b00c9
VZ
54// ----------------------------------------------------------------------------
55// wxFindReplaceDialogImpl: the internals of wxFindReplaceDialog
56// ----------------------------------------------------------------------------
57
58class WXDLLEXPORT wxFindReplaceDialogImpl
59{
60public:
61 wxFindReplaceDialogImpl(wxFindReplaceDialog *dialog, int flagsWX);
62 ~wxFindReplaceDialogImpl();
63
64 void InitFindWhat(const wxString& str);
65 void InitReplaceWith(const wxString& str);
66
c41b00c9
VZ
67 // only for passing to ::FindText or ::ReplaceText
68 FINDREPLACE *GetPtrFindReplace() { return &m_findReplace; }
69
14fca738 70 // set/query the "closed by user" flag
cbe874bd 71 void SetClosedByUser() { m_wasClosedByUser = true; }
14fca738
VZ
72 bool WasClosedByUser() const { return m_wasClosedByUser; }
73
c41b00c9 74private:
5acec112
VZ
75 // called from window procedure for ms_msgFindDialog
76 static bool FindMessageHandler(wxWindow *win,
77 WXUINT nMsg,
78 WPARAM wParam,
79 LPARAM lParam);
c41b00c9 80
5acec112
VZ
81 // copy string str contents to ppStr and fill pLen with its length
82 void InitString(const wxString& str, LPTSTR *ppStr, WORD *pLen);
c41b00c9 83
c41b00c9
VZ
84
85 // the find replace data used by the dialog
86 FINDREPLACE m_findReplace;
87
cbe874bd 88 // true if the user closed us, false otherwise
14fca738
VZ
89 bool m_wasClosedByUser;
90
c41b00c9
VZ
91 // registered Message for Dialog
92 static UINT ms_msgFindDialog;
22f3361e 93
c0c133e1 94 wxDECLARE_NO_COPY_CLASS(wxFindReplaceDialogImpl);
c41b00c9
VZ
95};
96
97UINT wxFindReplaceDialogImpl::ms_msgFindDialog = 0;
98
99// ============================================================================
100// implementation
101// ============================================================================
102
103// ----------------------------------------------------------------------------
104// wxFindReplaceDialogImpl
105// ----------------------------------------------------------------------------
106
107wxFindReplaceDialogImpl::wxFindReplaceDialogImpl(wxFindReplaceDialog *dialog,
108 int flagsWX)
109{
110 // get the identifier for the find dialog message if we don't have it yet
111 if ( !ms_msgFindDialog )
112 {
113 ms_msgFindDialog = ::RegisterWindowMessage(FINDMSGSTRING);
114
115 if ( !ms_msgFindDialog )
116 {
9a83f860 117 wxLogLastError(wxT("RegisterWindowMessage(FINDMSGSTRING)"));
c41b00c9 118 }
c41b00c9 119
5acec112
VZ
120 wxWindow::MSWRegisterMessageHandler
121 (
122 ms_msgFindDialog,
123 &wxFindReplaceDialogImpl::FindMessageHandler
124 );
125 }
c41b00c9 126
cbe874bd 127 m_wasClosedByUser = false;
14fca738 128
c41b00c9
VZ
129 wxZeroMemory(m_findReplace);
130
761989ff 131 // translate the flags: first the dialog creation flags
c41b00c9
VZ
132
133 // always set this to be able to set the title
134 int flags = FR_ENABLEHOOK;
135
761989ff
VZ
136 int flagsDialog = dialog->GetWindowStyle();
137 if ( flagsDialog & wxFR_NOMATCHCASE)
c41b00c9 138 flags |= FR_NOMATCHCASE;
761989ff 139 if ( flagsDialog & wxFR_NOWHOLEWORD)
c41b00c9 140 flags |= FR_NOWHOLEWORD;
761989ff 141 if ( flagsDialog & wxFR_NOUPDOWN)
c41b00c9 142 flags |= FR_NOUPDOWN;
761989ff
VZ
143
144 // and now the flags governing the initial values of the dialogs controls
c41b00c9
VZ
145 if ( flagsWX & wxFR_DOWN)
146 flags |= FR_DOWN;
761989ff
VZ
147 if ( flagsWX & wxFR_MATCHCASE)
148 flags |= FR_MATCHCASE;
149 if ( flagsWX & wxFR_WHOLEWORD )
150 flags |= FR_WHOLEWORD;
c41b00c9
VZ
151
152 m_findReplace.lStructSize = sizeof(FINDREPLACE);
153 m_findReplace.hwndOwner = GetHwndOf(dialog->GetParent());
154 m_findReplace.Flags = flags;
155
156 m_findReplace.lCustData = (LPARAM)dialog;
157 m_findReplace.lpfnHook = wxFindReplaceDialogHookProc;
158}
159
160void wxFindReplaceDialogImpl::InitString(const wxString& str,
161 LPTSTR *ppStr, WORD *pLen)
162{
163 size_t len = str.length() + 1;
164 if ( len < 80 )
165 {
166 // MSDN docs say that the buffer must be at least 80 chars
167 len = 80;
168 }
169
170 *ppStr = new wxChar[len];
171 wxStrcpy(*ppStr, str);
5c519b6c 172 *pLen = (WORD)len;
c41b00c9
VZ
173}
174
175void wxFindReplaceDialogImpl::InitFindWhat(const wxString& str)
176{
177 InitString(str, &m_findReplace.lpstrFindWhat, &m_findReplace.wFindWhatLen);
178}
179
180void wxFindReplaceDialogImpl::InitReplaceWith(const wxString& str)
181{
182 InitString(str,
183 &m_findReplace.lpstrReplaceWith,
184 &m_findReplace.wReplaceWithLen);
185}
186
c41b00c9
VZ
187wxFindReplaceDialogImpl::~wxFindReplaceDialogImpl()
188{
189 delete [] m_findReplace.lpstrFindWhat;
190 delete [] m_findReplace.lpstrReplaceWith;
c41b00c9
VZ
191}
192
193// ----------------------------------------------------------------------------
5acec112 194// handler for FINDMSGSTRING message
c41b00c9
VZ
195// ----------------------------------------------------------------------------
196
5acec112
VZ
197bool
198wxFindReplaceDialogImpl::FindMessageHandler(wxWindow * WXUNUSED(win),
199 WXUINT WXUNUSED_UNLESS_DEBUG(nMsg),
200 WPARAM WXUNUSED(wParam),
201 LPARAM lParam)
c41b00c9 202{
ea392212
VZ
203#if wxUSE_UNICODE_MSLU
204 static unsigned long s_lastMsgFlags = 0;
205
206 // This flag helps us to identify the bogus ANSI message
207 // sent by UNICOWS.DLL (see below)
208 // while we're sending our message to the dialog
209 // we ignore possible messages sent in between
210 static bool s_blockMsg = false;
211#endif // wxUSE_UNICODE_MSLU
212
9a83f860 213 wxASSERT_MSG( nMsg == ms_msgFindDialog, wxT("unexpected message received") );
5acec112
VZ
214
215 FINDREPLACE *pFR = (FINDREPLACE *)lParam;
ea392212
VZ
216
217#if wxUSE_UNICODE_MSLU
5acec112
VZ
218 // This is a hack for a MSLU problem: Versions up to 1.0.4011
219 // of UNICOWS.DLL send the correct UNICODE item after button press
4c51a665 220 // and a bogus ANSI mode item right after this, so let's ignore
5acec112
VZ
221 // the second bogus message
222 if ( wxUsingUnicowsDll() && s_lastMsgFlags == pFR->Flags )
223 {
224 s_lastMsgFlags = 0;
225 return 0;
226 }
227 s_lastMsgFlags = pFR->Flags;
ea392212
VZ
228#endif // wxUSE_UNICODE_MSLU
229
5acec112 230 wxFindReplaceDialog *dialog = (wxFindReplaceDialog *)pFR->lCustData;
c41b00c9 231
5acec112
VZ
232 // map flags from Windows
233 wxEventType evtType;
c41b00c9 234
5acec112
VZ
235 bool replace = false;
236 if ( pFR->Flags & FR_DIALOGTERM )
237 {
238 // we have to notify the dialog that it's being closed by user and
239 // not deleted programmatically as it behaves differently in these
240 // 2 cases
241 dialog->GetImpl()->SetClosedByUser();
14fca738 242
5acec112
VZ
243 evtType = wxEVT_COMMAND_FIND_CLOSE;
244 }
245 else if ( pFR->Flags & FR_FINDNEXT )
246 {
247 evtType = wxEVT_COMMAND_FIND_NEXT;
248 }
249 else if ( pFR->Flags & FR_REPLACE )
250 {
251 evtType = wxEVT_COMMAND_FIND_REPLACE;
c41b00c9 252
5acec112
VZ
253 replace = true;
254 }
255 else if ( pFR->Flags & FR_REPLACEALL )
256 {
257 evtType = wxEVT_COMMAND_FIND_REPLACE_ALL;
c41b00c9 258
5acec112
VZ
259 replace = true;
260 }
261 else
262 {
9a83f860 263 wxFAIL_MSG( wxT("unknown find dialog event") );
c41b00c9 264
5acec112
VZ
265 return 0;
266 }
c41b00c9 267
5acec112
VZ
268 wxUint32 flags = 0;
269 if ( pFR->Flags & FR_DOWN )
270 flags |= wxFR_DOWN;
271 if ( pFR->Flags & FR_WHOLEWORD )
272 flags |= wxFR_WHOLEWORD;
273 if ( pFR->Flags & FR_MATCHCASE )
274 flags |= wxFR_MATCHCASE;
275
276 wxFindDialogEvent event(evtType, dialog->GetId());
277 event.SetEventObject(dialog);
278 event.SetFlags(flags);
279 event.SetFindString(pFR->lpstrFindWhat);
280 if ( replace )
281 {
282 event.SetReplaceString(pFR->lpstrReplaceWith);
283 }
c41b00c9 284
ea392212 285#if wxUSE_UNICODE_MSLU
5acec112 286 s_blockMsg = true;
ea392212
VZ
287#endif // wxUSE_UNICODE_MSLU
288
5acec112 289 dialog->Send(event);
ea392212
VZ
290
291#if wxUSE_UNICODE_MSLU
5acec112 292 s_blockMsg = false;
ea392212 293#endif // wxUSE_UNICODE_MSLU
c41b00c9 294
5acec112 295 return true;
761989ff 296}
c41b00c9
VZ
297
298// ----------------------------------------------------------------------------
299// Find/replace dialog hook proc
300// ----------------------------------------------------------------------------
301
975b6bcf
VZ
302UINT_PTR CALLBACK
303wxFindReplaceDialogHookProc(HWND hwnd,
304 UINT uiMsg,
305 WPARAM WXUNUSED(wParam),
306 LPARAM lParam)
c41b00c9
VZ
307{
308 if ( uiMsg == WM_INITDIALOG )
309 {
310 FINDREPLACE *pFR = (FINDREPLACE *)lParam;
311 wxFindReplaceDialog *dialog = (wxFindReplaceDialog *)pFR->lCustData;
312
017dc06b 313 ::SetWindowText(hwnd, dialog->GetTitle().t_str());
c41b00c9
VZ
314
315 // don't return FALSE from here or the dialog won't be shown
316 return TRUE;
317 }
318
319 return 0;
320}
321
322// ============================================================================
323// wxFindReplaceDialog implementation
324// ============================================================================
325
326// ----------------------------------------------------------------------------
327// wxFindReplaceDialog ctors/dtor
328// ----------------------------------------------------------------------------
329
330void wxFindReplaceDialog::Init()
331{
332 m_impl = NULL;
333 m_FindReplaceData = NULL;
334
335 // as we're created in the hidden state, bring the internal flag in sync
cbe874bd 336 m_isShown = false;
c41b00c9
VZ
337}
338
339wxFindReplaceDialog::wxFindReplaceDialog(wxWindow *parent,
340 wxFindReplaceData *data,
761989ff
VZ
341 const wxString &title,
342 int flags)
208c5141 343 : wxFindReplaceDialogBase(parent, data, title, flags)
c41b00c9
VZ
344{
345 Init();
346
761989ff 347 (void)Create(parent, data, title, flags);
c41b00c9
VZ
348}
349
350wxFindReplaceDialog::~wxFindReplaceDialog()
351{
86d87075 352 if ( m_impl )
14fca738 353 {
86d87075
VZ
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
356 // and the flag must have been set
357 if ( !m_impl->WasClosedByUser() )
14fca738 358 {
86d87075
VZ
359 // if it wasn't, delete the dialog ourselves
360 if ( !::DestroyWindow(GetHwnd()) )
361 {
9a83f860 362 wxLogLastError(wxT("DestroyWindow(find dialog)"));
86d87075 363 }
14fca738 364 }
14fca738 365
86d87075
VZ
366 // unsubclass the parent
367 delete m_impl;
368 }
761989ff
VZ
369
370 // prevent the base class dtor from trying to hide us!
cbe874bd 371 m_isShown = false;
761989ff 372
14fca738 373 // and from destroying our window [again]
44d5b352 374 m_hWnd = (WXHWND)NULL;
c41b00c9
VZ
375}
376
377bool wxFindReplaceDialog::Create(wxWindow *parent,
378 wxFindReplaceData *data,
761989ff
VZ
379 const wxString &title,
380 int flags)
c41b00c9 381{
761989ff 382 m_windowStyle = flags;
c41b00c9
VZ
383 m_FindReplaceData = data;
384 m_parent = parent;
385
386 SetTitle(title);
387
388 // we must have a parent as it will get the messages from us
389 return parent != NULL;
390}
391
c41b00c9
VZ
392// ----------------------------------------------------------------------------
393// wxFindReplaceData show/hide
394// ----------------------------------------------------------------------------
395
396bool wxFindReplaceDialog::Show(bool show)
397{
398 if ( !wxWindowBase::Show(show) )
399 {
400 // visibility status didn't change
cbe874bd 401 return false;
c41b00c9
VZ
402 }
403
404 // do we already have the dialog window?
405 if ( m_hWnd )
406 {
407 // yes, just use it
761989ff
VZ
408 (void)::ShowWindow(GetHwnd(), show ? SW_SHOW : SW_HIDE);
409
cbe874bd 410 return true;
c41b00c9
VZ
411 }
412
413 if ( !show )
414 {
415 // well, it doesn't exist which is as good as being hidden
cbe874bd 416 return true;
c41b00c9
VZ
417 }
418
9a83f860 419 wxCHECK_MSG( m_FindReplaceData, false, wxT("call Create() first!") );
c41b00c9 420
9a83f860 421 wxASSERT_MSG( !m_impl, wxT("why don't we have the window then?") );
c41b00c9 422
761989ff 423 m_impl = new wxFindReplaceDialogImpl(this, m_FindReplaceData->GetFlags());
c41b00c9
VZ
424
425 m_impl->InitFindWhat(m_FindReplaceData->GetFindString());
426
761989ff
VZ
427 bool replace = HasFlag(wxFR_REPLACEDIALOG);
428 if ( replace )
c41b00c9 429 {
761989ff 430 m_impl->InitReplaceWith(m_FindReplaceData->GetReplaceString());
c41b00c9
VZ
431 }
432
433 // call the right function to show the dialog which does what we want
761989ff
VZ
434 FINDREPLACE *pFR = m_impl->GetPtrFindReplace();
435 HWND hwnd;
436 if ( replace )
437 hwnd = ::ReplaceText(pFR);
438 else
439 hwnd = ::FindText(pFR);
440
441 if ( !hwnd )
c41b00c9
VZ
442 {
443 wxLogError(_("Failed to create the standard find/replace dialog (error code %d)"),
444 ::CommDlgExtendedError());
445
5276b0a5 446 wxDELETE(m_impl);
c41b00c9 447
cbe874bd 448 return false;
c41b00c9
VZ
449 }
450
761989ff 451 if ( !::ShowWindow(hwnd, SW_SHOW) )
c41b00c9 452 {
9a83f860 453 wxLogLastError(wxT("ShowWindow(find dialog)"));
c41b00c9
VZ
454 }
455
761989ff
VZ
456 m_hWnd = (WXHWND)hwnd;
457
cbe874bd 458 return true;
c41b00c9
VZ
459}
460
461// ----------------------------------------------------------------------------
462// wxFindReplaceDialog title handling
463// ----------------------------------------------------------------------------
464
465// we set the title of this dialog in our jook proc but for now don't crash in
466// the base class version because of m_hWnd == 0
467
468void wxFindReplaceDialog::SetTitle( const wxString& title)
469{
470 m_title = title;
471}
472
473wxString wxFindReplaceDialog::GetTitle() const
474{
475 return m_title;
476}
477
478// ----------------------------------------------------------------------------
479// wxFindReplaceDialog position/size
480// ----------------------------------------------------------------------------
481
482void wxFindReplaceDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
483 int WXUNUSED(width), int WXUNUSED(height),
484 int WXUNUSED(sizeFlags))
485{
486 // ignore - we can't change the size of this standard dialog
487 return;
488}
489
490// NB: of course, both of these functions are completely bogus, but it's better
491// than nothing
492void wxFindReplaceDialog::DoGetSize(int *width, int *height) const
493{
494 // the standard dialog size
495 if ( width )
496 *width = 225;
497 if ( height )
498 *height = 324;
499}
500
501void wxFindReplaceDialog::DoGetClientSize(int *width, int *height) const
502{
503 // the standard dialog size
504 if ( width )
505 *width = 219;
506 if ( height )
507 *height = 299;
508}
509
510#endif // wxUSE_FINDREPLDLG