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