1 /*
2 * GraphApp - Cross-Platform Graphics Programming Library.
3 *
4 * File: init.c -- library initialisation code.
5 * Platform: Windows Version: 2.23 Date: 1997/09/09
6 *
7 * Version: 1.00 Changes: Original version by Lachlan Patrick.
8 * Version: 2.00 Changes: New object class system.
9 * Version: 2.20 Changes: Added EasyWin support in Borland C.
10 * Version: 2.22 Changes: Now main doesn't use environ pointer.
11 */
12
13 /* Copyright (C) 1993-1998 Lachlan Patrick
14
15 This file is part of GraphApp, a cross-platform C graphics library.
16
17 GraphApp is free software; you can redistribute it and/or modify it
18 under the terms of the GNU Library General Public License.
19 GraphApp is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY.
21
22 See the file COPYLIB.TXT for details.
23 */
24
25 #include "internal.h"
26
27 /*
28 * Windows implementation globals.
29 */
30
31 PROTECTED char * app_name = "GraphApp";
32 PROTECTED int app_initialised = 0;
33 PROTECTED HANDLE this_instance = NULL;
34 PROTECTED HANDLE prev_instance = NULL;
35
36 /*
37 * Functions defined in this file.
38 */
39 static void setappname(char *title);
40 static void getappname(HANDLE Instance);
41
42 int initapp(int argc, char **argv);
43 void exitapp(void);
44 void gabeep(void);
45
46 /*
47 * Initialise application. Returns zero on failure.
48 */
initapp(int argc,char ** argv)49 int initapp(int argc, char **argv)
50 {
51 if (! app_initialised)
52 {
53 app_initialised = 1;
54 init_objects();
55 init_events();
56 init_fonts();
57 init_cursors();
58 init_contexts();
59 init_menus();
60 }
61 return (argc < 1) ? 1 : argc;
62 }
63
64 /*
65 * This function releases all GDI objects.
66 */
67 PROTECTED
app_cleanup(void)68 void app_cleanup(void)
69 {
70 if (app_initialised) {
71 app_initialised = 0;
72 finish_contexts();
73 finish_objects();
74 finish_events();
75 }
76 }
77
78 /*
79 * Standard quit application function.
80 * This function releases all GDI objects and terminates the program.
81 * It can be called at any time from within a program, or at the end,
82 * or else it will be called if all windows are closed.
83 */
exitapp(void)84 void exitapp(void)
85 {
86 app_cleanup();
87 active_windows = 0;
88 exit(0);
89 }
90
91 /*
92 * Run an application program:
93 */
execapp(char * app)94 int execapp(char *app)
95 {
96 #ifdef __MSDOS__
97 if (WinExec(app, SW_SHOW) < 32)
98 return 0;
99 #else
100 if (system(app) != 0)
101 return 0;
102 #endif
103 return 1;
104 }
105
106
107
108 /*
109 * Play error sound
110 */
gabeep(void)111 void gabeep(void)
112 {
113 MessageBeep(MB_ICONASTERISK);
114 }
115
116 /*
117 * Take a name like "PROG.EXE" and set app_name to be "Prog".
118 */
setappname(char * title)119 static void setappname(char *title)
120 {
121 int len;
122 char c;
123
124 if (title[0] == '\0')
125 return;
126 for (len=1; (c=title[len]) != '\0'; len++)
127 title[len] = tolower(c);
128 if ((len-=4)<0)
129 len = 0;
130 if (! string_diff(title+len, ".exe"))
131 title[len] = '\0';
132
133 app_name = new_string(title);
134 }
135
136 /*
137 * Find out what the application is called.
138 */
getappname(HANDLE Instance)139 static void getappname(HANDLE Instance)
140 {
141 char exename[MAX_PATH];
142 char title[MAX_PATH];
143
144 /* find out executable name */
145 GetModuleFileName(Instance, exename, sizeof(exename));
146 GetFileTitle(exename, title, sizeof(title));
147 setappname(title);
148 }
149
150
151 /*
152 * The main Windows entry point is the WinMain function.
153 */
154
155
156
startgraphapp(HINSTANCE Instance,HINSTANCE PrevInstance,int CmdShow)157 void startgraphapp(HINSTANCE Instance, HINSTANCE PrevInstance, int CmdShow)
158 {
159 /*
160 * Save some variables for later.
161 */
162 this_instance = Instance;
163 /* prev_instance = PrevInstance; is always NULL */
164
165 /*
166 * Initialise the graphical interface.
167 */
168 initapp(0, NULL);
169 getappname(Instance);
170 }
171
172