]> git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
MacHandleControlClick extended by mouseStillDown state for supporting things like...
[wxWidgets.git] / src / common / utilscmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utilscmn.cpp
3 // Purpose: Miscellaneous utility functions and classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "utils.h"
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 #ifndef WX_PRECOMP
32 #include "wx/defs.h"
33 #include "wx/string.h"
34 #include "wx/utils.h"
35 #include "wx/intl.h"
36 #include "wx/log.h"
37
38 #if wxUSE_GUI
39 #include "wx/app.h"
40 #include "wx/window.h"
41 #include "wx/frame.h"
42 #include "wx/menu.h"
43 #include "wx/msgdlg.h"
44 #include "wx/textdlg.h"
45 #include "wx/textctrl.h" // for wxTE_PASSWORD
46 #if wxUSE_ACCEL
47 #include "wx/menuitem.h"
48 #include "wx/accel.h"
49 #endif // wxUSE_ACCEL
50 #endif // wxUSE_GUI
51 #endif // WX_PRECOMP
52
53 #ifndef __WIN16__
54 #include "wx/process.h"
55 #include "wx/txtstrm.h"
56 #endif
57
58 #include <ctype.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #if !defined(__WATCOMC__)
64 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
65 #include <errno.h>
66 #endif
67 #endif
68
69 #if wxUSE_GUI
70 #include "wx/colordlg.h"
71 #include "wx/fontdlg.h"
72 #include "wx/notebook.h"
73 #include "wx/frame.h"
74 #include "wx/statusbr.h"
75 #endif // wxUSE_GUI
76
77 #include <time.h>
78
79 #ifndef __MWERKS__
80 #include <sys/types.h>
81 #include <sys/stat.h>
82 #endif
83
84 #ifdef __SALFORDC__
85 #include <clib.h>
86 #endif
87
88 #ifdef __WXMSW__
89 #include "wx/msw/private.h"
90 #endif
91
92 // ----------------------------------------------------------------------------
93 // common data
94 // ----------------------------------------------------------------------------
95
96 #if WXWIN_COMPATIBILITY_2_2
97 const wxChar *wxInternalErrorStr = wxT("wxWindows Internal Error");
98 const wxChar *wxFatalErrorStr = wxT("wxWindows Fatal Error");
99 #endif // WXWIN_COMPATIBILITY_2_2
100
101 // ============================================================================
102 // implementation
103 // ============================================================================
104
105 wxChar *
106 copystring (const wxChar *s)
107 {
108 if (s == NULL) s = wxT("");
109 size_t len = wxStrlen (s) + 1;
110
111 wxChar *news = new wxChar[len];
112 memcpy (news, s, len * sizeof(wxChar)); // Should be the fastest
113
114 return news;
115 }
116
117 // Id generation
118 static long wxCurrentId = 100;
119
120 long
121 wxNewId (void)
122 {
123 return wxCurrentId++;
124 }
125
126 long
127 wxGetCurrentId(void) { return wxCurrentId; }
128
129 void
130 wxRegisterId (long id)
131 {
132 if (id >= wxCurrentId)
133 wxCurrentId = id + 1;
134 }
135
136 // ----------------------------------------------------------------------------
137 // String <-> Number conversions (deprecated)
138 // ----------------------------------------------------------------------------
139
140 #if WXWIN_COMPATIBILITY_2_4
141
142 WXDLLEXPORT_DATA(const wxChar *) wxFloatToStringStr = wxT("%.2f");
143 WXDLLEXPORT_DATA(const wxChar *) wxDoubleToStringStr = wxT("%.2f");
144
145 void
146 StringToFloat (const wxChar *s, float *number)
147 {
148 if (s && *s && number)
149 *number = (float) wxStrtod (s, (wxChar **) NULL);
150 }
151
152 void
153 StringToDouble (const wxChar *s, double *number)
154 {
155 if (s && *s && number)
156 *number = wxStrtod (s, (wxChar **) NULL);
157 }
158
159 wxChar *
160 FloatToString (float number, const wxChar *fmt)
161 {
162 static wxChar buf[256];
163
164 wxSprintf (buf, fmt, number);
165 return buf;
166 }
167
168 wxChar *
169 DoubleToString (double number, const wxChar *fmt)
170 {
171 static wxChar buf[256];
172
173 wxSprintf (buf, fmt, number);
174 return buf;
175 }
176
177 void
178 StringToInt (const wxChar *s, int *number)
179 {
180 if (s && *s && number)
181 *number = (int) wxStrtol (s, (wxChar **) NULL, 10);
182 }
183
184 void
185 StringToLong (const wxChar *s, long *number)
186 {
187 if (s && *s && number)
188 *number = wxStrtol (s, (wxChar **) NULL, 10);
189 }
190
191 wxChar *
192 IntToString (int number)
193 {
194 static wxChar buf[20];
195
196 wxSprintf (buf, wxT("%d"), number);
197 return buf;
198 }
199
200 wxChar *
201 LongToString (long number)
202 {
203 static wxChar buf[20];
204
205 wxSprintf (buf, wxT("%ld"), number);
206 return buf;
207 }
208
209 #endif // WXWIN_COMPATIBILITY_2_4
210
211 // Array used in DecToHex conversion routine.
212 static wxChar hexArray[] = wxT("0123456789ABCDEF");
213
214 // Convert 2-digit hex number to decimal
215 int wxHexToDec(const wxString& buf)
216 {
217 int firstDigit, secondDigit;
218
219 if (buf.GetChar(0) >= wxT('A'))
220 firstDigit = buf.GetChar(0) - wxT('A') + 10;
221 else
222 firstDigit = buf.GetChar(0) - wxT('0');
223
224 if (buf.GetChar(1) >= wxT('A'))
225 secondDigit = buf.GetChar(1) - wxT('A') + 10;
226 else
227 secondDigit = buf.GetChar(1) - wxT('0');
228
229 return (firstDigit & 0xF) * 16 + (secondDigit & 0xF );
230 }
231
232 // Convert decimal integer to 2-character hex string
233 void wxDecToHex(int dec, wxChar *buf)
234 {
235 int firstDigit = (int)(dec/16.0);
236 int secondDigit = (int)(dec - (firstDigit*16.0));
237 buf[0] = hexArray[firstDigit];
238 buf[1] = hexArray[secondDigit];
239 buf[2] = 0;
240 }
241
242 // Convert decimal integer to 2-character hex string
243 wxString wxDecToHex(int dec)
244 {
245 wxChar buf[3];
246 wxDecToHex(dec, buf);
247 return wxString(buf);
248 }
249
250 #if WXWIN_COMPATIBILITY_2
251 bool
252 StringMatch (const wxChar *str1, const wxChar *str2, bool subString, bool exact)
253 {
254 if (str1 == NULL || str2 == NULL)
255 return FALSE;
256 if (str1 == str2)
257 return TRUE;
258
259 if (subString)
260 {
261 int len1 = wxStrlen (str1);
262 int len2 = wxStrlen (str2);
263 int i;
264
265 // Search for str1 in str2
266 // Slow .... but acceptable for short strings
267 for (i = 0; i <= len2 - len1; i++)
268 {
269 if (wxStrnicmp (str1, str2 + i, len1) == 0)
270 return TRUE;
271 }
272 }
273 else if (exact)
274 {
275 if (wxStricmp (str1, str2) == 0)
276 return TRUE;
277 }
278 else
279 {
280 int len1 = wxStrlen (str1);
281 int len2 = wxStrlen (str2);
282
283 if (wxStrnicmp (str1, str2, wxMin (len1, len2)) == 0)
284 return TRUE;
285 }
286
287 return FALSE;
288 }
289 #endif
290
291 // Return the current date/time
292 // [volatile]
293 wxString wxNow()
294 {
295 time_t now = time((time_t *) NULL);
296 char *date = ctime(&now);
297 date[24] = '\0';
298 return wxString::FromAscii(date);
299 }
300
301 #if wxUSE_GUI
302
303 #if wxUSE_MENUS
304
305 // ----------------------------------------------------------------------------
306 // Menu accelerators related functions
307 // ----------------------------------------------------------------------------
308
309 wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out)
310 {
311 wxString s = wxMenuItem::GetLabelFromText(in);
312 if ( out )
313 {
314 // go smash their buffer if it's not big enough - I love char * params
315 memcpy(out, s.c_str(), s.length() * sizeof(wxChar));
316 }
317 else
318 {
319 out = copystring(s);
320 }
321
322 return out;
323 }
324
325 wxString wxStripMenuCodes(const wxString& in)
326 {
327 wxString out;
328
329 size_t len = in.length();
330 out.reserve(len);
331
332 for ( size_t n = 0; n < len; n++ )
333 {
334 wxChar ch = in[n];
335 if ( ch == _T('&') )
336 {
337 // skip it, it is used to introduce the accel char (or to quote
338 // itself in which case it should still be skipped): note that it
339 // can't be the last character of the string
340 if ( ++n == len )
341 {
342 wxLogDebug(_T("Invalid menu string '%s'"), in.c_str());
343 }
344 else
345 {
346 // use the next char instead
347 ch = in[n];
348 }
349 }
350 else if ( ch == _T('\t') )
351 {
352 // everything after TAB is accel string, exit the loop
353 break;
354 }
355
356 out += ch;
357 }
358
359 return out;
360 }
361
362 #endif // wxUSE_MENUS
363
364 // ----------------------------------------------------------------------------
365 // Window search functions
366 // ----------------------------------------------------------------------------
367
368 /*
369 * If parent is non-NULL, look through children for a label or title
370 * matching the specified string. If NULL, look through all top-level windows.
371 *
372 */
373
374 wxWindow *
375 wxFindWindowByLabel (const wxString& title, wxWindow * parent)
376 {
377 return wxWindow::FindWindowByLabel( title, parent );
378 }
379
380
381 /*
382 * If parent is non-NULL, look through children for a name
383 * matching the specified string. If NULL, look through all top-level windows.
384 *
385 */
386
387 wxWindow *
388 wxFindWindowByName (const wxString& name, wxWindow * parent)
389 {
390 return wxWindow::FindWindowByName( name, parent );
391 }
392
393 // Returns menu item id or -1 if none.
394 int
395 wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
396 {
397 #if wxUSE_MENUS
398 wxMenuBar *menuBar = frame->GetMenuBar ();
399 if ( menuBar )
400 return menuBar->FindMenuItem (menuString, itemString);
401 #endif // wxUSE_MENUS
402
403 return -1;
404 }
405
406 // Try to find the deepest child that contains 'pt'.
407 // We go backwards, to try to allow for controls that are spacially
408 // within other controls, but are still siblings (e.g. buttons within
409 // static boxes). Static boxes are likely to be created _before_ controls
410 // that sit inside them.
411 wxWindow* wxFindWindowAtPoint(wxWindow* win, const wxPoint& pt)
412 {
413 if (!win->IsShown())
414 return NULL;
415
416 // Hack for wxNotebook case: at least in wxGTK, all pages
417 // claim to be shown, so we must only deal with the selected one.
418 #if wxUSE_NOTEBOOK
419 if (win->IsKindOf(CLASSINFO(wxNotebook)))
420 {
421 wxNotebook* nb = (wxNotebook*) win;
422 int sel = nb->GetSelection();
423 if (sel >= 0)
424 {
425 wxWindow* child = nb->GetPage(sel);
426 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
427 if (foundWin)
428 return foundWin;
429 }
430 }
431 #endif
432
433 /* Doesn't work
434 // Frame case
435 else if (win->IsKindOf(CLASSINFO(wxFrame)))
436 {
437 // Pseudo-children that may not be mentioned in the child list
438 wxWindowList extraChildren;
439 wxFrame* frame = (wxFrame*) win;
440 if (frame->GetStatusBar())
441 extraChildren.Append(frame->GetStatusBar());
442 if (frame->GetToolBar())
443 extraChildren.Append(frame->GetToolBar());
444
445 wxNode* node = extraChildren.GetFirst();
446 while (node)
447 {
448 wxWindow* child = (wxWindow*) node->GetData();
449 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
450 if (foundWin)
451 return foundWin;
452 node = node->Next();
453 }
454 }
455 */
456
457 wxWindowList::Node *node = win->GetChildren().GetLast();
458 while (node)
459 {
460 wxWindow* child = node->GetData();
461 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
462 if (foundWin)
463 return foundWin;
464 node = node->GetPrevious();
465 }
466
467 wxPoint pos = win->GetPosition();
468 wxSize sz = win->GetSize();
469 if (win->GetParent())
470 {
471 pos = win->GetParent()->ClientToScreen(pos);
472 }
473
474 wxRect rect(pos, sz);
475 if (rect.Inside(pt))
476 return win;
477 else
478 return NULL;
479 }
480
481 wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt)
482 {
483 // Go backwards through the list since windows
484 // on top are likely to have been appended most
485 // recently.
486 wxWindowList::Node *node = wxTopLevelWindows.GetLast();
487 while (node)
488 {
489 wxWindow* win = node->GetData();
490 wxWindow* found = wxFindWindowAtPoint(win, pt);
491 if (found)
492 return found;
493 node = node->GetPrevious();
494 }
495 return NULL;
496 }
497
498 #endif // wxUSE_GUI
499
500 /*
501 On Fri, 21 Jul 1995, Paul Craven wrote:
502
503 > Is there a way to find the path of running program's executable? I can get
504 > my home directory, and the current directory, but I don't know how to get the
505 > executable directory.
506 >
507
508 The code below (warty as it is), does what you want on most Unix,
509 DOS, and Mac platforms (it's from the ALS Prolog main).
510
511 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
512 ||==== Voice: +1 (617)965-9191 Newton Centre,
513 || FAX: +1 (617)965-1636 MA 02159 USA
514 Email: ken@als.com WWW: http://www.als.com
515 ------------------------------------------------------------------------
516 */
517
518 // This code is commented out but it may be integrated with wxWin at
519 // a later date, after testing. Thanks Ken!
520 #if 0
521
522 /*--------------------------------------------------------------------*
523 | whereami is given a filename f in the form: whereami(argv[0])
524 | It returns the directory in which the executable file (containing
525 | this code [main.c] ) may be found. A dot will be returned to indicate
526 | the current directory.
527 *--------------------------------------------------------------------*/
528
529 static void
530 whereami(name)
531 char *name;
532 {
533 register char *cutoff = NULL; /* stifle -Wall */
534 register char *s;
535 register char *t;
536 int cc;
537 char ebuf[4096];
538
539 /*
540 * See if the file is accessible either through the current directory
541 * or through an absolute path.
542 */
543
544 if (access(name, R_OK) == 0) {
545
546 /*-------------------------------------------------------------*
547 * The file was accessible without any other work. But the current
548 * working directory might change on us, so if it was accessible
549 * through the cwd, then we should get it for later accesses.
550 *-------------------------------------------------------------*/
551
552 t = imagedir;
553 if (!absolute_pathname(name)) {
554 #if defined(__DOS__) || defined(__WIN32__)
555 int drive;
556 char *newrbuf;
557
558 newrbuf = imagedir;
559 #ifndef __DJGPP__
560 if (*(name + 1) == ':') {
561 if (*name >= 'a' && *name <= 'z')
562 drive = (int) (*name - 'a' + 1);
563 else
564 drive = (int) (*name - 'A' + 1);
565 *newrbuf++ = *name;
566 *newrbuf++ = *(name + 1);
567 *newrbuf++ = DIR_SEPARATOR;
568 }
569 else {
570 drive = 0;
571 *newrbuf++ = DIR_SEPARATOR;
572 }
573 if (getcwd(newrbuf, drive) == 0) { /* } */
574 #else
575 if (getcwd(newrbuf, 1024) == 0) { /* } */
576 #endif
577 #else /* DOS */
578 #ifdef HAVE_GETWD
579 if (getwd(imagedir) == 0) { /* } */
580 #else /* !HAVE_GETWD */
581 if (getcwd(imagedir, 1024) == 0) {
582 #endif /* !HAVE_GETWD */
583 #endif /* DOS */
584 fatal_error(FE_GETCWD, 0);
585 }
586 for (; *t; t++) /* Set t to end of buffer */
587 ;
588 if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already
589 * last char
590 */
591 cutoff = t - 1;
592 else {
593 cutoff = t; /* otherwise put one in */
594 *t++ = DIR_SEPARATOR;
595 }
596 }
597 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
598 else
599 (*t++ = DIR_SEPARATOR);
600 #endif
601
602 /*-------------------------------------------------------------*
603 * Copy the rest of the string and set the cutoff if it was not
604 * already set. If the first character of name is a slash, cutoff
605 * is not presently set but will be on the first iteration of the
606 * loop below.
607 *-------------------------------------------------------------*/
608
609 for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) {
610 if (*s == DIR_SEPARATOR)
611 cutoff = t;
612 if (!(*t++ = *s++))
613 break;
614 }
615
616 }
617 else {
618
619 /*-------------------------------------------------------------*
620 * Get the path list from the environment. If the path list is
621 * inaccessible for any reason, leave with fatal error.
622 *-------------------------------------------------------------*/
623
624 #ifdef __MAC__
625 if ((s = getenv("Commands")) == (char *) 0)
626 #else
627 if ((s = getenv("PATH")) == (char *) 0)
628 #endif
629 fatal_error(FE_PATH, 0);
630
631 /*
632 * Copy path list into ebuf and set the source pointer to the
633 * beginning of this buffer.
634 */
635
636 strcpy(ebuf, s);
637 s = ebuf;
638
639 for (;;) {
640 t = imagedir;
641 while (*s && *s != PATH_SEPARATOR)
642 *t++ = *s++;
643 if (t > imagedir && *(t - 1) == DIR_SEPARATOR)
644 ; /* do nothing -- slash already is in place */
645 else
646 *t++ = DIR_SEPARATOR; /* put in the slash */
647 cutoff = t - 1; /* set cutoff */
648 strcpy(t, name);
649 if (access(imagedir, R_OK) == 0)
650 break;
651
652 if (*s)
653 s++; /* advance source pointer */
654 else
655 fatal_error(FE_INFND, 0);
656 }
657
658 }
659
660 /*-------------------------------------------------------------*
661 | At this point the full pathname should exist in imagedir and
662 | cutoff should be set to the final slash. We must now determine
663 | whether the file name is a symbolic link or not and chase it down
664 | if it is. Note that we reuse ebuf for getting the link.
665 *-------------------------------------------------------------*/
666
667 #ifdef HAVE_SYMLINK
668 while ((cc = readlink(imagedir, ebuf, 512)) != -1) {
669 ebuf[cc] = 0;
670 s = ebuf;
671 if (*s == DIR_SEPARATOR) {
672 t = imagedir;
673 }
674 else {
675 t = cutoff + 1;
676 }
677 for (;;) {
678 if (*s == DIR_SEPARATOR)
679 cutoff = t; /* mark the last slash seen */
680 if (!(*t++ = *s++)) /* copy the character */
681 break;
682 }
683 }
684
685 #endif /* HAVE_SYMLINK */
686
687 strcpy(imagename, cutoff + 1); /* keep the image name */
688 *(cutoff + 1) = 0; /* chop off the filename part */
689 }
690
691 #endif
692
693 #if wxUSE_GUI
694
695 // ----------------------------------------------------------------------------
696 // GUI helpers
697 // ----------------------------------------------------------------------------
698
699 /*
700 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
701 * since otherwise the generic code may be pulled in unnecessarily.
702 */
703
704 #if wxUSE_MSGDLG
705
706 int wxMessageBox(const wxString& message, const wxString& caption, long style,
707 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
708 {
709 wxMessageDialog dialog(parent, message, caption, style);
710
711 int ans = dialog.ShowModal();
712 switch ( ans )
713 {
714 case wxID_OK:
715 return wxOK;
716 case wxID_YES:
717 return wxYES;
718 case wxID_NO:
719 return wxNO;
720 case wxID_CANCEL:
721 return wxCANCEL;
722 }
723
724 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
725
726 return wxCANCEL;
727 }
728
729 #endif // wxUSE_MSGDLG
730
731 #if wxUSE_TEXTDLG
732
733 wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
734 const wxString& defaultValue, wxWindow *parent,
735 int x, int y, bool WXUNUSED(centre) )
736 {
737 wxString str;
738 wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y));
739 if (dialog.ShowModal() == wxID_OK)
740 {
741 str = dialog.GetValue();
742 }
743
744 return str;
745 }
746
747 wxString wxGetPasswordFromUser(const wxString& message,
748 const wxString& caption,
749 const wxString& defaultValue,
750 wxWindow *parent)
751 {
752 wxString str;
753 wxTextEntryDialog dialog(parent, message, caption, defaultValue,
754 wxOK | wxCANCEL | wxTE_PASSWORD);
755 if ( dialog.ShowModal() == wxID_OK )
756 {
757 str = dialog.GetValue();
758 }
759
760 return str;
761 }
762
763 #endif // wxUSE_TEXTDLG
764
765 #if wxUSE_COLOURDLG
766
767 wxColour wxGetColourFromUser(wxWindow *parent, const wxColour& colInit)
768 {
769 wxColourData data;
770 data.SetChooseFull(TRUE);
771 if ( colInit.Ok() )
772 {
773 data.SetColour((wxColour &)colInit); // const_cast
774 }
775
776 wxColour colRet;
777 wxColourDialog dialog(parent, &data);
778 if ( dialog.ShowModal() == wxID_OK )
779 {
780 colRet = dialog.GetColourData().GetColour();
781 }
782 //else: leave it invalid
783
784 return colRet;
785 }
786
787 #endif // wxUSE_COLOURDLG
788
789 #if wxUSE_FONTDLG
790
791 wxFont wxGetFontFromUser(wxWindow *parent, const wxFont& fontInit)
792 {
793 wxFontData data;
794 if ( fontInit.Ok() )
795 {
796 data.SetInitialFont(fontInit);
797 }
798
799 wxFont fontRet;
800 wxFontDialog dialog(parent, data);
801 if ( dialog.ShowModal() == wxID_OK )
802 {
803 fontRet = dialog.GetFontData().GetChosenFont();
804 }
805 //else: leave it invalid
806
807 return fontRet;
808 }
809
810 #endif // wxUSE_FONTDLG
811 // ----------------------------------------------------------------------------
812 // missing C RTL functions (FIXME shouldn't be here at all)
813 // ----------------------------------------------------------------------------
814
815 #if defined( __MWERKS__ ) && !defined(__MACH__)
816 char *strdup(const char *s)
817 {
818 return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ;
819 }
820 int isascii( int c )
821 {
822 return ( c >= 0 && c < 128 ) ;
823 }
824 #endif // __MWERKS__
825
826 // ----------------------------------------------------------------------------
827 // wxSafeYield and supporting functions
828 // ----------------------------------------------------------------------------
829
830 void wxEnableTopLevelWindows(bool enable)
831 {
832 wxWindowList::Node *node;
833 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
834 node->GetData()->Enable(enable);
835 }
836
837 wxWindowDisabler::wxWindowDisabler(wxWindow *winToSkip)
838 {
839 // remember the top level windows which were already disabled, so that we
840 // don't reenable them later
841 m_winDisabled = NULL;
842
843 wxWindowList::Node *node;
844 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
845 {
846 wxWindow *winTop = node->GetData();
847 if ( winTop == winToSkip )
848 continue;
849
850 if ( winTop->IsEnabled() )
851 {
852 winTop->Disable();
853 }
854 else
855 {
856 if ( !m_winDisabled )
857 {
858 m_winDisabled = new wxWindowList;
859 }
860
861 m_winDisabled->Append(winTop);
862 }
863 }
864 }
865
866 wxWindowDisabler::~wxWindowDisabler()
867 {
868 wxWindowList::Node *node;
869 for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() )
870 {
871 wxWindow *winTop = node->GetData();
872 if ( !m_winDisabled || !m_winDisabled->Find(winTop) )
873 {
874 winTop->Enable();
875 }
876 //else: had been already disabled, don't reenable
877 }
878
879 delete m_winDisabled;
880 }
881
882 // Yield to other apps/messages and disable user input to all windows except
883 // the given one
884 bool wxSafeYield(wxWindow *win, bool onlyIfNeeded)
885 {
886 wxWindowDisabler wd(win);
887
888 bool rc;
889 if (onlyIfNeeded)
890 rc = wxYieldIfNeeded();
891 else
892 rc = wxYield();
893
894 return rc;
895 }
896
897 // ----------------------------------------------------------------------------
898 // misc functions
899 // ----------------------------------------------------------------------------
900
901 // Don't synthesize KeyUp events holding down a key and producing KeyDown
902 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
903 // in utilsgtk.cpp.
904 #ifndef __WXGTK__
905 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
906 {
907 return TRUE; // detectable auto-repeat is the only mode MSW supports
908 }
909 #endif // !wxGTK
910
911 #endif // wxUSE_GUI
912
913 const wxChar *wxGetInstallPrefix()
914 {
915 wxString prefix;
916
917 if ( wxGetEnv(wxT("WXPREFIX"), &prefix) )
918 return prefix.c_str();
919
920 #ifdef wxINSTALL_PREFIX
921 return wxT(wxINSTALL_PREFIX);
922 #else
923 return wxT("");
924 #endif
925 }
926
927 wxString wxGetDataDir()
928 {
929 wxString format = wxGetInstallPrefix();
930 format << wxFILE_SEP_PATH
931 << wxT("share") << wxFILE_SEP_PATH
932 << wxT("wx") << wxFILE_SEP_PATH
933 << wxT("%i.%i");
934 wxString dir;
935 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
936 return dir;
937 }
938
939
940 // ----------------------------------------------------------------------------
941 // network and user id functions
942 // ----------------------------------------------------------------------------
943
944 // Get Full RFC822 style email address
945 bool wxGetEmailAddress(wxChar *address, int maxSize)
946 {
947 wxString email = wxGetEmailAddress();
948 if ( !email )
949 return FALSE;
950
951 wxStrncpy(address, email, maxSize - 1);
952 address[maxSize - 1] = wxT('\0');
953
954 return TRUE;
955 }
956
957 wxString wxGetEmailAddress()
958 {
959 wxString email;
960
961 wxString host = wxGetFullHostName();
962 if ( !!host )
963 {
964 wxString user = wxGetUserId();
965 if ( !!user )
966 {
967 email << user << wxT('@') << host;
968 }
969 }
970
971 return email;
972 }
973
974 wxString wxGetUserId()
975 {
976 static const int maxLoginLen = 256; // FIXME arbitrary number
977
978 wxString buf;
979 bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen);
980 buf.UngetWriteBuf();
981
982 if ( !ok )
983 buf.Empty();
984
985 return buf;
986 }
987
988 wxString wxGetUserName()
989 {
990 static const int maxUserNameLen = 1024; // FIXME arbitrary number
991
992 wxString buf;
993 bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen);
994 buf.UngetWriteBuf();
995
996 if ( !ok )
997 buf.Empty();
998
999 return buf;
1000 }
1001
1002 wxString wxGetHostName()
1003 {
1004 static const size_t hostnameSize = 257;
1005
1006 wxString buf;
1007 bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1008
1009 buf.UngetWriteBuf();
1010
1011 if ( !ok )
1012 buf.Empty();
1013
1014 return buf;
1015 }
1016
1017 wxString wxGetFullHostName()
1018 {
1019 static const size_t hostnameSize = 257;
1020
1021 wxString buf;
1022 bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize);
1023
1024 buf.UngetWriteBuf();
1025
1026 if ( !ok )
1027 buf.Empty();
1028
1029 return buf;
1030 }
1031
1032 wxString wxGetHomeDir()
1033 {
1034 wxString home;
1035 wxGetHomeDir(&home);
1036
1037 return home;
1038 }
1039
1040 #if 0
1041
1042 wxString wxGetCurrentDir()
1043 {
1044 wxString dir;
1045 size_t len = 1024;
1046 bool ok;
1047 do
1048 {
1049 ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL;
1050 dir.UngetWriteBuf();
1051
1052 if ( !ok )
1053 {
1054 if ( errno != ERANGE )
1055 {
1056 wxLogSysError(_T("Failed to get current directory"));
1057
1058 return wxEmptyString;
1059 }
1060 else
1061 {
1062 // buffer was too small, retry with a larger one
1063 len *= 2;
1064 }
1065 }
1066 //else: ok
1067 } while ( !ok );
1068
1069 return dir;
1070 }
1071
1072 #endif // 0
1073
1074 // ----------------------------------------------------------------------------
1075 // wxExecute
1076 // ----------------------------------------------------------------------------
1077
1078 // wxDoExecuteWithCapture() helper: reads an entire stream into one array
1079 //
1080 // returns TRUE if ok, FALSE if error
1081 #if wxUSE_STREAMS
1082 static bool ReadAll(wxInputStream *is, wxArrayString& output)
1083 {
1084 wxCHECK_MSG( is, FALSE, _T("NULL stream in wxExecute()?") );
1085
1086 // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state
1087 is->Reset();
1088
1089 wxTextInputStream tis(*is);
1090
1091 bool cont = TRUE;
1092 while ( cont )
1093 {
1094 wxString line = tis.ReadLine();
1095 if ( is->Eof() )
1096 break;
1097
1098 if ( !*is )
1099 {
1100 cont = FALSE;
1101 }
1102 else
1103 {
1104 output.Add(line);
1105 }
1106 }
1107
1108 return cont;
1109 }
1110 #endif // wxUSE_STREAMS
1111
1112 // this is a private function because it hasn't a clean interface: the first
1113 // array is passed by reference, the second by pointer - instead we have 2
1114 // public versions of wxExecute() below
1115 static long wxDoExecuteWithCapture(const wxString& command,
1116 wxArrayString& output,
1117 wxArrayString* error)
1118 {
1119 #ifdef __WIN16__
1120 wxFAIL_MSG("Sorry, this version of wxExecute not implemented on WIN16.");
1121
1122 return 0;
1123 #else // !Win16
1124 // create a wxProcess which will capture the output
1125 wxProcess *process = new wxProcess;
1126 process->Redirect();
1127
1128 long rc = wxExecute(command, wxEXEC_SYNC, process);
1129
1130 #if wxUSE_STREAMS
1131 if ( rc != -1 )
1132 {
1133 if ( !ReadAll(process->GetInputStream(), output) )
1134 rc = -1;
1135
1136 if ( error )
1137 {
1138 if ( !ReadAll(process->GetErrorStream(), *error) )
1139 rc = -1;
1140 }
1141
1142 }
1143 #endif // wxUSE_STREAMS
1144
1145 delete process;
1146
1147 return rc;
1148 #endif // IO redirection supoprted
1149 }
1150
1151 long wxExecute(const wxString& command, wxArrayString& output)
1152 {
1153 return wxDoExecuteWithCapture(command, output, NULL);
1154 }
1155
1156 long wxExecute(const wxString& command,
1157 wxArrayString& output,
1158 wxArrayString& error)
1159 {
1160 return wxDoExecuteWithCapture(command, output, &error);
1161 }
1162
1163 // ----------------------------------------------------------------------------
1164 // wxApp::Yield() wrappers for backwards compatibility
1165 // ----------------------------------------------------------------------------
1166
1167 bool wxYield()
1168 {
1169 #if wxUSE_GUI
1170 return wxTheApp && wxTheApp->Yield();
1171 #else
1172 return FALSE;
1173 #endif
1174 }
1175
1176 bool wxYieldIfNeeded()
1177 {
1178 #if wxUSE_GUI
1179 return wxTheApp && wxTheApp->Yield(TRUE);
1180 #else
1181 return FALSE;
1182 #endif
1183 }
1184