The R Project SVN R

Rev

Rev 51815 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4394 ripley 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
 
83775 kalibera 25
/* Copyright (C) 2023      The R Core Team
26
   Path length limitations
27
 */
28
 
4394 ripley 29
#include "internal.h"
30
 
31
/*
32
 *  Windows implementation globals.
33
 */
34
 
45017 ripley 35
PROTECTED char * app_name = "GraphApp";
36
PROTECTED int    app_initialised = 0;
37
PROTECTED HANDLE this_instance = NULL;
38
PROTECTED HANDLE prev_instance = NULL;
4394 ripley 39
 
40
/*
41
 *  Functions defined in this file.
42
 */
45017 ripley 43
static void setappname(char *title);
44
static void getappname(HANDLE Instance);
4394 ripley 45
 
45017 ripley 46
int initapp(int argc, char **argv);
47
void exitapp(void);
48
void gabeep(void);
4394 ripley 49
 
50
/*
51
 *  Initialise application. Returns zero on failure.
52
 */
53
int initapp(int argc, char **argv)
54
{
45017 ripley 55
    if (! app_initialised)
56
    {
57
	app_initialised = 1;
58
	init_objects();
59
	init_events();
60
	init_fonts();
61
	init_cursors();
62
	init_contexts();
63
	init_menus();
64
    }
65
    return (argc < 1) ? 1 : argc;
4394 ripley 66
}
67
 
68
/*
69
 *  This function releases all GDI objects.
70
 */
71
PROTECTED
72
void app_cleanup(void)
73
{
45017 ripley 74
    if (app_initialised) {
75
	app_initialised = 0;
76
	finish_contexts();
77
	finish_objects();
78
	finish_events();
79
    }
4394 ripley 80
}
81
 
82
/*
83
 *  Standard quit application function.
84
 *  This function releases all GDI objects and terminates the program.
85
 *  It can be called at any time from within a program, or at the end,
86
 *  or else it will be called if all windows are closed.
87
 */
88
void exitapp(void)
89
{
45017 ripley 90
    app_cleanup();
91
    active_windows = 0;
92
    exit(0);
4394 ripley 93
}
94
 
95
/*
96
 *  Run an application program:
97
 */
98
int execapp(char *app)
99
{
45017 ripley 100
#ifdef __MSDOS__
101
    if (WinExec(app, SW_SHOW) < 32)
102
	return 0;
103
#else
104
    if (system(app) != 0)
105
	return 0;
106
#endif
107
    return 1;
4394 ripley 108
}
109
 
110
 
111
 
112
/*
9184 ripley 113
 *  Play error sound
4394 ripley 114
 */
115
void gabeep(void)
116
{
45017 ripley 117
    MessageBeep(MB_ICONASTERISK);
4394 ripley 118
}
119
 
120
/*
121
 *  Take a name like "PROG.EXE" and set app_name to be "Prog".
122
 */
123
static void setappname(char *title)
124
{
45017 ripley 125
    int len;
126
    char c;
4394 ripley 127
 
45017 ripley 128
    if (title[0] == '\0')
129
	return;
130
    for (len=1; (c=title[len]) != '\0'; len++)
131
	title[len] = tolower(c);
132
    if ((len-=4)<0)
133
	len = 0;
134
    if (! string_diff(title+len, ".exe"))
135
	title[len] = '\0';
4394 ripley 136
 
45017 ripley 137
    app_name = new_string(title);
4394 ripley 138
}
139
 
140
/*
141
 *  Find out what the application is called.
142
 */
143
static void getappname(HANDLE Instance)
144
{
83775 kalibera 145
    char *exename = NULL;
146
    char *title = NULL;
147
    DWORD rc;
4394 ripley 148
 
45017 ripley 149
    /* find out executable name */
83775 kalibera 150
 
151
    /* GetModuleFileName doesn't return the needed buffer size. */
152
    DWORD size = 1;
153
    for(;;) {
154
	exename = (char *)malloc(size);
155
	if (!exename)
156
            return;
157
        DWORD rc = GetModuleFileName(Instance, exename, size);
158
        if (rc > 0 && rc < size) /* success */
159
            break;
160
        free(exename);
161
        if (rc != size) /* error */
162
            return;
163
        size *= 2; /* try again with 2x larger buffer */
164
    }
165
 
166
    rc = GetFileTitle(exename, NULL, 0);
167
    if (!rc)
168
	return;
169
    title = (char *)malloc(rc);
170
    if (!title) {
171
	free(exename);
172
	return;
173
    }
174
    DWORD rc1 = GetFileTitle(exename, title, rc);
175
    free(exename);
176
    if (!rc1)
177
	setappname(title);
178
    free(title);
4394 ripley 179
}
180
 
181
 
182
/*
183
 *  The main Windows entry point is the WinMain function.
184
 */
185
 
186
 
187
 
188
void startgraphapp(HINSTANCE Instance, HINSTANCE PrevInstance, int CmdShow)
189
{
45017 ripley 190
    /*
191
     *  Save some variables for later.
192
     */
193
    this_instance = Instance;
194
    /* prev_instance = PrevInstance; is always NULL */
4394 ripley 195
 
45017 ripley 196
    /*
197
     *  Initialise the graphical interface.
198
     */
199
    initapp(0, NULL);
200
    getappname(Instance);
4394 ripley 201
}
202