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