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