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