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