]>
git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utility functions and classes
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "utils.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
40 #include "wx/window.h"
43 #include "wx/msgdlg.h"
44 #include "wx/textdlg.h"
45 #include "wx/textctrl.h" // for wxTE_PASSWORD
47 #include "wx/menuitem.h"
54 #include "wx/process.h"
55 #include "wx/txtstrm.h"
63 #if !defined(__WATCOMC__)
64 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
70 #include "wx/colordlg.h"
71 #include "wx/fontdlg.h"
72 #include "wx/notebook.h"
74 #include "wx/statusbr.h"
80 #include <sys/types.h>
89 #include "wx/msw/private.h"
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
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
101 // ============================================================================
103 // ============================================================================
106 copystring (const wxChar
*s
)
108 if (s
== NULL
) s
= wxT("");
109 size_t len
= wxStrlen (s
) + 1;
111 wxChar
*news
= new wxChar
[len
];
112 memcpy (news
, s
, len
* sizeof(wxChar
)); // Should be the fastest
118 static long wxCurrentId
= 100;
123 return wxCurrentId
++;
127 wxGetCurrentId(void) { return wxCurrentId
; }
130 wxRegisterId (long id
)
132 if (id
>= wxCurrentId
)
133 wxCurrentId
= id
+ 1;
137 StringToFloat (const wxChar
*s
, float *number
)
139 if (s
&& *s
&& number
)
140 *number
= (float) wxStrtod (s
, (wxChar
**) NULL
);
144 StringToDouble (const wxChar
*s
, double *number
)
146 if (s
&& *s
&& number
)
147 *number
= wxStrtod (s
, (wxChar
**) NULL
);
151 FloatToString (float number
, const wxChar
*fmt
)
153 static wxChar buf
[256];
155 wxSprintf (buf
, fmt
, number
);
160 DoubleToString (double number
, const wxChar
*fmt
)
162 static wxChar buf
[256];
164 wxSprintf (buf
, fmt
, number
);
169 StringToInt (const wxChar
*s
, int *number
)
171 if (s
&& *s
&& number
)
172 *number
= (int) wxStrtol (s
, (wxChar
**) NULL
, 10);
176 StringToLong (const wxChar
*s
, long *number
)
178 if (s
&& *s
&& number
)
179 *number
= wxStrtol (s
, (wxChar
**) NULL
, 10);
183 IntToString (int number
)
185 static wxChar buf
[20];
187 wxSprintf (buf
, wxT("%d"), number
);
192 LongToString (long number
)
194 static wxChar buf
[20];
196 wxSprintf (buf
, wxT("%ld"), number
);
200 // Array used in DecToHex conversion routine.
201 static wxChar hexArray
[] = wxT("0123456789ABCDEF");
203 // Convert 2-digit hex number to decimal
204 int wxHexToDec(const wxString
& buf
)
206 int firstDigit
, secondDigit
;
208 if (buf
.GetChar(0) >= wxT('A'))
209 firstDigit
= buf
.GetChar(0) - wxT('A') + 10;
211 firstDigit
= buf
.GetChar(0) - wxT('0');
213 if (buf
.GetChar(1) >= wxT('A'))
214 secondDigit
= buf
.GetChar(1) - wxT('A') + 10;
216 secondDigit
= buf
.GetChar(1) - wxT('0');
218 return (firstDigit
& 0xF) * 16 + (secondDigit
& 0xF );
221 // Convert decimal integer to 2-character hex string
222 void wxDecToHex(int dec
, wxChar
*buf
)
224 int firstDigit
= (int)(dec
/16.0);
225 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
226 buf
[0] = hexArray
[firstDigit
];
227 buf
[1] = hexArray
[secondDigit
];
231 // Convert decimal integer to 2-character hex string
232 wxString
wxDecToHex(int dec
)
235 wxDecToHex(dec
, buf
);
236 return wxString(buf
);
239 #if WXWIN_COMPATIBILITY_2
241 StringMatch (const wxChar
*str1
, const wxChar
*str2
, bool subString
, bool exact
)
243 if (str1
== NULL
|| str2
== NULL
)
250 int len1
= wxStrlen (str1
);
251 int len2
= wxStrlen (str2
);
254 // Search for str1 in str2
255 // Slow .... but acceptable for short strings
256 for (i
= 0; i
<= len2
- len1
; i
++)
258 if (wxStrnicmp (str1
, str2
+ i
, len1
) == 0)
264 if (wxStricmp (str1
, str2
) == 0)
269 int len1
= wxStrlen (str1
);
270 int len2
= wxStrlen (str2
);
272 if (wxStrnicmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
280 // Return the current date/time
284 time_t now
= time((time_t *) NULL
);
285 char *date
= ctime(&now
);
287 return wxString::FromAscii(date
);
294 // ----------------------------------------------------------------------------
295 // Menu accelerators related functions
296 // ----------------------------------------------------------------------------
298 wxChar
*wxStripMenuCodes(const wxChar
*in
, wxChar
*out
)
300 wxString s
= wxMenuItem::GetLabelFromText(in
);
303 // go smash their buffer if it's not big enough - I love char * params
304 memcpy(out
, s
.c_str(), s
.length() * sizeof(wxChar
));
314 wxString
wxStripMenuCodes(const wxString
& in
)
318 size_t len
= in
.length();
321 for ( size_t n
= 0; n
< len
; n
++ )
326 // skip it, it is used to introduce the accel char (or to quote
327 // itself in which case it should still be skipped): note that it
328 // can't be the last character of the string
331 wxLogDebug(_T("Invalid menu string '%s'"), in
.c_str());
335 // use the next char instead
339 else if ( ch
== _T('\t') )
341 // everything after TAB is accel string, exit the loop
351 #endif // wxUSE_MENUS
353 // ----------------------------------------------------------------------------
354 // Window search functions
355 // ----------------------------------------------------------------------------
358 * If parent is non-NULL, look through children for a label or title
359 * matching the specified string. If NULL, look through all top-level windows.
364 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
366 return wxWindow::FindWindowByLabel( title
, parent
);
371 * If parent is non-NULL, look through children for a name
372 * matching the specified string. If NULL, look through all top-level windows.
377 wxFindWindowByName (const wxString
& name
, wxWindow
* parent
)
379 return wxWindow::FindWindowByName( name
, parent
);
382 // Returns menu item id or -1 if none.
384 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
387 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
389 return menuBar
->FindMenuItem (menuString
, itemString
);
390 #endif // wxUSE_MENUS
395 // Try to find the deepest child that contains 'pt'.
396 // We go backwards, to try to allow for controls that are spacially
397 // within other controls, but are still siblings (e.g. buttons within
398 // static boxes). Static boxes are likely to be created _before_ controls
399 // that sit inside them.
400 wxWindow
* wxFindWindowAtPoint(wxWindow
* win
, const wxPoint
& pt
)
405 // Hack for wxNotebook case: at least in wxGTK, all pages
406 // claim to be shown, so we must only deal with the selected one.
408 if (win
->IsKindOf(CLASSINFO(wxNotebook
)))
410 wxNotebook
* nb
= (wxNotebook
*) win
;
411 int sel
= nb
->GetSelection();
414 wxWindow
* child
= nb
->GetPage(sel
);
415 wxWindow
* foundWin
= wxFindWindowAtPoint(child
, pt
);
424 else if (win->IsKindOf(CLASSINFO(wxFrame)))
426 // Pseudo-children that may not be mentioned in the child list
427 wxWindowList extraChildren;
428 wxFrame* frame = (wxFrame*) win;
429 if (frame->GetStatusBar())
430 extraChildren.Append(frame->GetStatusBar());
431 if (frame->GetToolBar())
432 extraChildren.Append(frame->GetToolBar());
434 wxNode* node = extraChildren.First();
437 wxWindow* child = (wxWindow*) node->Data();
438 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
446 wxNode
* node
= win
->GetChildren().Last();
449 wxWindow
* child
= (wxWindow
*) node
->Data();
450 wxWindow
* foundWin
= wxFindWindowAtPoint(child
, pt
);
453 node
= node
->Previous();
456 wxPoint pos
= win
->GetPosition();
457 wxSize sz
= win
->GetSize();
458 if (win
->GetParent())
460 pos
= win
->GetParent()->ClientToScreen(pos
);
463 wxRect
rect(pos
, sz
);
470 wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
)
472 // Go backwards through the list since windows
473 // on top are likely to have been appended most
475 wxNode
* node
= wxTopLevelWindows
.Last();
478 wxWindow
* win
= (wxWindow
*) node
->Data();
479 wxWindow
* found
= wxFindWindowAtPoint(win
, pt
);
482 node
= node
->Previous();
490 On Fri, 21 Jul 1995, Paul Craven wrote:
492 > Is there a way to find the path of running program's executable? I can get
493 > my home directory, and the current directory, but I don't know how to get the
494 > executable directory.
497 The code below (warty as it is), does what you want on most Unix,
498 DOS, and Mac platforms (it's from the ALS Prolog main).
500 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
501 ||==== Voice: +1 (617)965-9191 Newton Centre,
502 || FAX: +1 (617)965-1636 MA 02159 USA
503 Email: ken@als.com WWW: http://www.als.com
504 ------------------------------------------------------------------------
507 // This code is commented out but it may be integrated with wxWin at
508 // a later date, after testing. Thanks Ken!
511 /*--------------------------------------------------------------------*
512 | whereami is given a filename f in the form: whereami(argv[0])
513 | It returns the directory in which the executable file (containing
514 | this code [main.c] ) may be found. A dot will be returned to indicate
515 | the current directory.
516 *--------------------------------------------------------------------*/
522 register char *cutoff
= NULL
; /* stifle -Wall */
529 * See if the file is accessible either through the current directory
530 * or through an absolute path.
533 if (access(name
, R_OK
) == 0) {
535 /*-------------------------------------------------------------*
536 * The file was accessible without any other work. But the current
537 * working directory might change on us, so if it was accessible
538 * through the cwd, then we should get it for later accesses.
539 *-------------------------------------------------------------*/
542 if (!absolute_pathname(name
)) {
543 #if defined(__DOS__) || defined(__WIN32__)
549 if (*(name
+ 1) == ':') {
550 if (*name
>= 'a' && *name
<= 'z')
551 drive
= (int) (*name
- 'a' + 1);
553 drive
= (int) (*name
- 'A' + 1);
555 *newrbuf
++ = *(name
+ 1);
556 *newrbuf
++ = DIR_SEPARATOR
;
560 *newrbuf
++ = DIR_SEPARATOR
;
562 if (getcwd(newrbuf
, drive
) == 0) { /* } */
564 if (getcwd(newrbuf
, 1024) == 0) { /* } */
568 if (getwd(imagedir
) == 0) { /* } */
569 #else /* !HAVE_GETWD */
570 if (getcwd(imagedir
, 1024) == 0) {
571 #endif /* !HAVE_GETWD */
573 fatal_error(FE_GETCWD
, 0);
575 for (; *t
; t
++) /* Set t to end of buffer */
577 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
582 cutoff
= t
; /* otherwise put one in */
583 *t
++ = DIR_SEPARATOR
;
586 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
588 (*t
++ = DIR_SEPARATOR
);
591 /*-------------------------------------------------------------*
592 * Copy the rest of the string and set the cutoff if it was not
593 * already set. If the first character of name is a slash, cutoff
594 * is not presently set but will be on the first iteration of the
596 *-------------------------------------------------------------*/
598 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
599 if (*s
== DIR_SEPARATOR
)
608 /*-------------------------------------------------------------*
609 * Get the path list from the environment. If the path list is
610 * inaccessible for any reason, leave with fatal error.
611 *-------------------------------------------------------------*/
614 if ((s
= getenv("Commands")) == (char *) 0)
616 if ((s
= getenv("PATH")) == (char *) 0)
618 fatal_error(FE_PATH
, 0);
621 * Copy path list into ebuf and set the source pointer to the
622 * beginning of this buffer.
630 while (*s
&& *s
!= PATH_SEPARATOR
)
632 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
633 ; /* do nothing -- slash already is in place */
635 *t
++ = DIR_SEPARATOR
; /* put in the slash */
636 cutoff
= t
- 1; /* set cutoff */
638 if (access(imagedir
, R_OK
) == 0)
642 s
++; /* advance source pointer */
644 fatal_error(FE_INFND
, 0);
649 /*-------------------------------------------------------------*
650 | At this point the full pathname should exist in imagedir and
651 | cutoff should be set to the final slash. We must now determine
652 | whether the file name is a symbolic link or not and chase it down
653 | if it is. Note that we reuse ebuf for getting the link.
654 *-------------------------------------------------------------*/
657 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
660 if (*s
== DIR_SEPARATOR
) {
667 if (*s
== DIR_SEPARATOR
)
668 cutoff
= t
; /* mark the last slash seen */
669 if (!(*t
++ = *s
++)) /* copy the character */
674 #endif /* HAVE_SYMLINK */
676 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
677 *(cutoff
+ 1) = 0; /* chop off the filename part */
684 // ----------------------------------------------------------------------------
686 // ----------------------------------------------------------------------------
689 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
690 * since otherwise the generic code may be pulled in unnecessarily.
695 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
696 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
698 wxMessageDialog
dialog(parent
, message
, caption
, style
);
700 int ans
= dialog
.ShowModal();
713 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
718 #endif // wxUSE_MSGDLG
722 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
723 const wxString
& defaultValue
, wxWindow
*parent
,
724 int x
, int y
, bool WXUNUSED(centre
) )
727 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
728 if (dialog
.ShowModal() == wxID_OK
)
730 str
= dialog
.GetValue();
736 wxString
wxGetPasswordFromUser(const wxString
& message
,
737 const wxString
& caption
,
738 const wxString
& defaultValue
,
742 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
,
743 wxOK
| wxCANCEL
| wxTE_PASSWORD
);
744 if ( dialog
.ShowModal() == wxID_OK
)
746 str
= dialog
.GetValue();
752 #endif // wxUSE_TEXTDLG
756 wxColour
wxGetColourFromUser(wxWindow
*parent
, const wxColour
& colInit
)
759 data
.SetChooseFull(TRUE
);
762 data
.SetColour((wxColour
&)colInit
); // const_cast
766 wxColourDialog
dialog(parent
, &data
);
767 if ( dialog
.ShowModal() == wxID_OK
)
769 colRet
= dialog
.GetColourData().GetColour();
771 //else: leave it invalid
776 #endif // wxUSE_COLOURDLG
780 wxFont
wxGetFontFromUser(wxWindow
*parent
, const wxFont
& fontInit
)
785 data
.SetInitialFont(fontInit
);
789 wxFontDialog
dialog(parent
, data
);
790 if ( dialog
.ShowModal() == wxID_OK
)
792 fontRet
= dialog
.GetFontData().GetChosenFont();
794 //else: leave it invalid
799 #endif // wxUSE_FONTDLG
800 // ----------------------------------------------------------------------------
801 // missing C RTL functions (FIXME shouldn't be here at all)
802 // ----------------------------------------------------------------------------
806 char *strdup(const char *s
)
808 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
813 return ( c
>= 0 && c
< 128 ) ;
817 // ----------------------------------------------------------------------------
818 // wxSafeYield and supporting functions
819 // ----------------------------------------------------------------------------
821 void wxEnableTopLevelWindows(bool enable
)
823 wxWindowList::Node
*node
;
824 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
825 node
->GetData()->Enable(enable
);
828 wxWindowDisabler::wxWindowDisabler(wxWindow
*winToSkip
)
830 // remember the top level windows which were already disabled, so that we
831 // don't reenable them later
832 m_winDisabled
= NULL
;
834 wxWindowList::Node
*node
;
835 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
837 wxWindow
*winTop
= node
->GetData();
838 if ( winTop
== winToSkip
)
841 if ( winTop
->IsEnabled() )
847 if ( !m_winDisabled
)
849 m_winDisabled
= new wxWindowList
;
852 m_winDisabled
->Append(winTop
);
857 wxWindowDisabler::~wxWindowDisabler()
859 wxWindowList::Node
*node
;
860 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
862 wxWindow
*winTop
= node
->GetData();
863 if ( !m_winDisabled
|| !m_winDisabled
->Find(winTop
) )
867 //else: had been already disabled, don't reenable
870 delete m_winDisabled
;
873 // Yield to other apps/messages and disable user input to all windows except
875 bool wxSafeYield(wxWindow
*win
)
877 wxWindowDisabler
wd(win
);
884 // ----------------------------------------------------------------------------
886 // ----------------------------------------------------------------------------
888 // Don't synthesize KeyUp events holding down a key and producing KeyDown
889 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
892 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
894 return TRUE
; // detectable auto-repeat is the only mode MSW supports
900 const wxChar
*wxGetInstallPrefix()
904 if ( wxGetEnv(wxT("WXPREFIX"), &prefix
) )
905 return prefix
.c_str();
907 #ifdef wxINSTALL_PREFIX
908 return wxT(wxINSTALL_PREFIX
);
914 wxString
wxGetDataDir()
916 wxString format
= wxGetInstallPrefix();
917 format
<< wxFILE_SEP_PATH
918 << wxT("share") << wxFILE_SEP_PATH
919 << wxT("wx") << wxFILE_SEP_PATH
922 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
927 // ----------------------------------------------------------------------------
928 // network and user id functions
929 // ----------------------------------------------------------------------------
931 // Get Full RFC822 style email address
932 bool wxGetEmailAddress(wxChar
*address
, int maxSize
)
934 wxString email
= wxGetEmailAddress();
938 wxStrncpy(address
, email
, maxSize
- 1);
939 address
[maxSize
- 1] = wxT('\0');
944 wxString
wxGetEmailAddress()
948 wxString host
= wxGetFullHostName();
951 wxString user
= wxGetUserId();
954 email
<< user
<< wxT('@') << host
;
961 wxString
wxGetUserId()
963 static const int maxLoginLen
= 256; // FIXME arbitrary number
966 bool ok
= wxGetUserId(buf
.GetWriteBuf(maxLoginLen
), maxLoginLen
);
975 wxString
wxGetUserName()
977 static const int maxUserNameLen
= 1024; // FIXME arbitrary number
980 bool ok
= wxGetUserName(buf
.GetWriteBuf(maxUserNameLen
), maxUserNameLen
);
989 wxString
wxGetHostName()
991 static const size_t hostnameSize
= 257;
994 bool ok
= wxGetHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
1004 wxString
wxGetFullHostName()
1006 static const size_t hostnameSize
= 257;
1009 bool ok
= wxGetFullHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
1011 buf
.UngetWriteBuf();
1019 wxString
wxGetHomeDir()
1022 wxGetHomeDir(&home
);
1029 wxString
wxGetCurrentDir()
1036 ok
= getcwd(dir
.GetWriteBuf(len
+ 1), len
) != NULL
;
1037 dir
.UngetWriteBuf();
1041 if ( errno
!= ERANGE
)
1043 wxLogSysError(_T("Failed to get current directory"));
1045 return wxEmptyString
;
1049 // buffer was too small, retry with a larger one
1061 // ----------------------------------------------------------------------------
1063 // ----------------------------------------------------------------------------
1065 // wxDoExecuteWithCapture() helper: reads an entire stream into one array
1067 // returns TRUE if ok, FALSE if error
1068 static bool ReadAll(wxInputStream
*is
, wxArrayString
& output
)
1070 wxCHECK_MSG( is
, FALSE
, _T("NULL stream in wxExecute()?") );
1072 // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state
1075 wxTextInputStream
tis(*is
);
1080 wxString line
= tis
.ReadLine();
1097 // this is a private function because it hasn't a clean interface: the first
1098 // array is passed by reference, the second by pointer - instead we have 2
1099 // public versions of wxExecute() below
1100 static long wxDoExecuteWithCapture(const wxString
& command
,
1101 wxArrayString
& output
,
1102 wxArrayString
* error
)
1105 wxFAIL_MSG("Sorry, this version of wxExecute not implemented on WIN16.");
1109 // create a wxProcess which will capture the output
1110 wxProcess
*process
= new wxProcess
;
1111 process
->Redirect();
1113 long rc
= wxExecute(command
, wxEXEC_SYNC
, process
);
1118 if ( !ReadAll(process
->GetInputStream(), output
) )
1123 if ( !ReadAll(process
->GetErrorStream(), *error
) )
1128 #endif // wxUSE_STREAMS
1133 #endif // IO redirection supoprted
1136 long wxExecute(const wxString
& command
, wxArrayString
& output
)
1138 return wxDoExecuteWithCapture(command
, output
, NULL
);
1141 long wxExecute(const wxString
& command
,
1142 wxArrayString
& output
,
1143 wxArrayString
& error
)
1145 return wxDoExecuteWithCapture(command
, output
, &error
);
1148 // ----------------------------------------------------------------------------
1149 // wxApp::Yield() wrappers for backwards compatibility
1150 // ----------------------------------------------------------------------------
1155 return wxTheApp
&& wxTheApp
->Yield();
1161 bool wxYieldIfNeeded()
1164 return wxTheApp
&& wxTheApp
->Yield(TRUE
);