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