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