1 /*
2  * "$Id: fl_call_main.c 7903 2010-11-28 21:06:39Z matt $"
3  *
4  * Copyright 1998-2010 by Bill Spitzak and others.
5  *
6  * fl_call_main() calls main() for you Windows people.  Needs to be done in C
7  * because Borland C++ won't let you call main() from C++.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA.
23  *
24  * Please report all bugs and problems on the following page:
25  *
26  *     http://www.fltk.org/str.php
27  */
28 
29 /*
30  * This WinMain() function can be overridden by an application and
31  * is provided for compatibility with programs written for other
32  * operating systems that conform to the ANSI standard entry point
33  * "main()".  This will allow you to build a WIN32 Application
34  * without any special settings.
35  *
36  * Because of problems with the Microsoft Visual C++ header files
37  * and/or compiler, you cannot have a WinMain function in a DLL.
38  * I don't know why.  Thus, this nifty feature is only available
39  * if you link to the static library.
40  *
41  * Currently the debug version of this library will create a
42  * console window for your application so you can put printf()
43  * statements for debugging or informational purposes.  Ultimately
44  * we want to update this to always use the parent's console,
45  * but at present we have not identified a function or API in
46  * Microsoft(r) Windows(r) that allows for it.
47  */
48 
49 #if defined(WIN32) && !defined(FL_DLL) && !defined (__GNUC__)
50 
51 #  include <windows.h>
52 #  include <stdio.h>
53 #  include <stdlib.h>
54 #  include <FL/fl_utf8.h>
55 
56 extern int main(int, char *[]);
57 
58 #  ifdef BORLAND5
59 #    define __argc _argc
60 #    define __argv _argv
61 #  endif /* BORLAND5 */
62 
63 /* static int mbcs2utf(const char *s, int l, char *dst, unsigned dstlen) */
mbcs2utf(const char * s,int l,char * dst)64 static int mbcs2utf(const char *s, int l, char *dst)
65 {
66   static xchar *mbwbuf;
67   unsigned dstlen = 0;
68   if (!s) return 0;
69   dstlen = (l * 6) + 6;
70   mbwbuf = (xchar*)malloc(dstlen * sizeof(xchar));
71   l = mbstowcs(mbwbuf, s, l);
72 /* l = fl_unicode2utf(mbwbuf, l, dst); */
73   l = fl_utf8fromwc(dst, dstlen, mbwbuf, l);
74   dst[l] = 0;
75   free(mbwbuf);
76   return l;
77 }
78 
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)79 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
80                              LPSTR lpCmdLine, int nCmdShow) {
81   int rc, i;
82   char **ar;
83 
84 #  ifdef _DEBUG
85  /*
86   * If we are using compiling in debug mode, open a console window so
87   * we can see any printf's, etc...
88   *
89   * While we can detect if the program was run from the command-line -
90   * look at the CMDLINE environment variable, it will be "WIN" for
91   * programs started from the GUI - the shell seems to run all WIN32
92   * applications in the background anyways...
93   */
94 
95   AllocConsole();
96   freopen("conin$", "r", stdin);
97   freopen("conout$", "w", stdout);
98   freopen("conout$", "w", stderr);
99 #  endif /* _DEBUG */
100 
101   ar = (char**) malloc(sizeof(char*) * (__argc + 1));
102   i = 0;
103   while (i < __argc) {
104     int l;
105     unsigned dstlen;
106     if (__wargv ) {
107       for (l = 0; __wargv[i] && __wargv[i][l]; l++) {}; /* is this just wstrlen??? */
108       dstlen = (l * 5) + 1;
109       ar[i] = (char*) malloc(dstlen);
110 /*    ar[i][fl_unicode2utf(__wargv[i], l, ar[i])] = 0; */
111       dstlen = fl_utf8fromwc(ar[i], dstlen, __wargv[i], l);
112       ar[i][dstlen] = 0;
113     } else {
114       for (l = 0; __argv[i] && __argv[i][l]; l++) {};
115       dstlen = (l * 5) + 1;
116       ar[i] = (char*) malloc(dstlen);
117 /*      ar[i][mbcs2utf(__argv[i], l, ar[i], dstlen)] = 0; */
118       ar[i][mbcs2utf(__argv[i], l, ar[i])] = 0;
119     }
120     i++;
121   }
122   ar[__argc] = 0;
123   /* Run the standard main entry point function... */
124   rc = main(__argc, ar);
125 
126 #  ifdef _DEBUG
127   fclose(stdin);
128   fclose(stdout);
129   fclose(stderr);
130 #  endif /* _DEBUG */
131 
132   return rc;
133 }
134 
135 #elif defined(__hpux)
136 /* This code to prevent "empty translation unit" or similar warnings... */
dummy(void)137 static void dummy(void) {}
138 #endif /* WIN32 && !FL_DLL && !__GNUC__ */
139 
140 /*
141  * End of "$Id: fl_call_main.c 7903 2010-11-28 21:06:39Z matt $".
142  */
143 
144