You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.4KB

  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. * This WinMain() function can be overridden by an application and
  30. * is provided for compatibility with programs written for other
  31. * operating systems that conform to the ANSI standard entry point
  32. * "main()". This will allow you to build a WIN32 Application
  33. * without any special settings.
  34. *
  35. * Because of problems with the Microsoft Visual C++ header files
  36. * and/or compiler, you cannot have a WinMain function in a DLL.
  37. * I don't know why. Thus, this nifty feature is only available
  38. * if you link to the static library.
  39. *
  40. * Currently the debug version of this library will create a
  41. * console window for your application so you can put printf()
  42. * statements for debugging or informational purposes. Ultimately
  43. * we want to update this to always use the parent's console,
  44. * but at present we have not identified a function or API in
  45. * Microsoft(r) Windows(r) that allows for it.
  46. */
  47. #if defined(WIN32) && !defined(FL_DLL) && !defined (__GNUC__)
  48. # include <windows.h>
  49. # include <stdio.h>
  50. # include <stdlib.h>
  51. # include <FL/fl_utf8.h>
  52. extern int main(int, char *[]);
  53. # ifdef BORLAND5
  54. # define __argc _argc
  55. # define __argv _argv
  56. # endif /* BORLAND5 */
  57. /* static int mbcs2utf(const char *s, int l, char *dst, unsigned dstlen) */
  58. static int mbcs2utf(const char *s, int l, char *dst)
  59. {
  60. static xchar *mbwbuf;
  61. unsigned dstlen = 0;
  62. if (!s) return 0;
  63. dstlen = (l * 6) + 6;
  64. mbwbuf = (xchar*)malloc(dstlen * sizeof(xchar));
  65. l = mbstowcs(mbwbuf, s, l);
  66. /* l = fl_unicode2utf(mbwbuf, l, dst); */
  67. l = fl_utf8fromwc(dst, dstlen, mbwbuf, l);
  68. dst[l] = 0;
  69. free(mbwbuf);
  70. return l;
  71. }
  72. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  73. LPSTR lpCmdLine, int nCmdShow) {
  74. int rc, i;
  75. char **ar;
  76. # ifdef _DEBUG
  77. /*
  78. * If we are using compiling in debug mode, open a console window so
  79. * we can see any printf's, etc...
  80. *
  81. * While we can detect if the program was run from the command-line -
  82. * look at the CMDLINE environment variable, it will be "WIN" for
  83. * programs started from the GUI - the shell seems to run all WIN32
  84. * applications in the background anyways...
  85. */
  86. AllocConsole();
  87. freopen("conin$", "r", stdin);
  88. freopen("conout$", "w", stdout);
  89. freopen("conout$", "w", stderr);
  90. # endif /* _DEBUG */
  91. ar = (char**) malloc(sizeof(char*) * (__argc + 1));
  92. i = 0;
  93. while (i < __argc) {
  94. int l;
  95. unsigned dstlen;
  96. if (__wargv ) {
  97. for (l = 0; __wargv[i] && __wargv[i][l]; l++) {}; /* is this just wstrlen??? */
  98. dstlen = (l * 5) + 1;
  99. ar[i] = (char*) malloc(dstlen);
  100. /* ar[i][fl_unicode2utf(__wargv[i], l, ar[i])] = 0; */
  101. dstlen = fl_utf8fromwc(ar[i], dstlen, __wargv[i], l);
  102. ar[i][dstlen] = 0;
  103. } else {
  104. for (l = 0; __argv[i] && __argv[i][l]; l++) {};
  105. dstlen = (l * 5) + 1;
  106. ar[i] = (char*) malloc(dstlen);
  107. /* ar[i][mbcs2utf(__argv[i], l, ar[i], dstlen)] = 0; */
  108. ar[i][mbcs2utf(__argv[i], l, ar[i])] = 0;
  109. }
  110. i++;
  111. }
  112. ar[__argc] = 0;
  113. /* Run the standard main entry point function... */
  114. rc = main(__argc, ar);
  115. # ifdef _DEBUG
  116. fclose(stdin);
  117. fclose(stdout);
  118. fclose(stderr);
  119. # endif /* _DEBUG */
  120. return rc;
  121. }
  122. #elif defined(__hpux)
  123. /* This code to prevent "empty translation unit" or similar warnings... */
  124. static void dummy(void) {}
  125. #endif /* WIN32 && !FL_DLL && !__GNUC__ */
  126. /*
  127. * End of "$Id: fl_call_main.c 7903 2010-11-28 21:06:39Z matt $".
  128. */