1 /*
2 * $Id: mdi.c,v 1.10 2001/02/13 23:38:06 danny Exp $
3 *
4 * This file is part of Oleo, the GNU spreadsheet.
5 *
6 * Copyright � 1999, 2000, 2001 by the Free Software Foundation, Inc.
7 * Written by Danny Backx <danny@gnu.org>.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program 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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 static char rcsid[] = "$Id: mdi.c,v 1.10 2001/02/13 23:38:06 danny Exp $";
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #ifdef WITH_DMALLOC
31 #include <dmalloc.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 #include "io-term.h"
39 #include "mysql.h"
40 #include "global.h"
41
42 #ifdef HAVE_MOTIF
43 #include "io-motif.h"
44 #endif
45
46 #ifndef False
47 #define False 0
48 #endif
49 #ifndef True
50 #define True 1
51 #endif
52
53 static int maxglobals = 0, nglobals = 0;
54 static struct OleoGlobal *globals = 0;
55
56 #define NGLOBALS_INC 10
57
58 /*
59 * Hack.
60 * This file only works well with Motif/LessTif anyway
61 */
62 #ifndef HAVE_MOTIF
MotifGlobalInitialize()63 void MotifGlobalInitialize()
64 {
65 }
66
MessageAppend()67 void MessageAppend()
68 {
69 }
70 #endif
71
MdiError(int offset1,int offset2,void * ptr)72 void MdiError(int offset1, int offset2, void *ptr)
73 {
74 #if 0
75 fprintf(stderr,
76 "MdiSelectGlobal: no match for offset %d,%d ptr %p\n",
77 offset1, offset2, ptr);
78 #endif
79 MessageAppend(True,
80 "MdiSelectGlobal: no match for offset %d,%d ptr %p\n",
81 offset1, offset2, ptr);
82 }
83
AllocateSubStructures(void)84 static void AllocateSubStructures(void)
85 {
86 PlotInit();
87 AllocateDatabaseGlobal();
88 MotifGlobalInitialize();
89 InitializeGlobals();
90 }
91
92 void
MdiInitialize(void)93 MdiInitialize(void)
94 {
95 globals = (struct OleoGlobal *)
96 calloc(NGLOBALS_INC, sizeof(struct OleoGlobal));
97 maxglobals = NGLOBALS_INC;
98
99 Global = &globals[0];
100 globals[0].valid = 1;
101 nglobals++;
102
103 AllocateSubStructures();
104 }
105
106 /*
107 * MdiOpen()
108 *
109 * Create an additional Global structure
110 */
111 void
MdiOpen()112 MdiOpen()
113 {
114 int i, j, found = 0;
115
116 for (i=0; i<maxglobals; i++)
117 if (globals[i].valid == 0) {
118 found = 1;
119 break;
120 }
121 /* If none free, allocate more entries */
122 if (! found) {
123 i = maxglobals;
124 maxglobals += NGLOBALS_INC;
125 globals = realloc(globals, maxglobals * sizeof(struct OleoGlobal));
126 for (j=i; j<maxglobals; j++)
127 globals[j].valid = 0;
128 }
129
130 /* Grab it */
131 globals[i].valid = 1;
132 nglobals++;
133
134 /* Start using it */
135 Global = &globals[i];
136
137 /* Allocate the sub-structures */
138 AllocateSubStructures();
139 }
140
141 /*
142 * Close the current Global structure
143 */
144 void
MdiClose()145 MdiClose()
146 {
147 MessageAppend(False, "Closing file '%s'", Global->FileName);
148
149 /* Indicate file closed */
150 free(Global->FileName);
151 Global->FileName = 0;
152
153 /* Release this global entry */
154 Global->valid = 0;
155 nglobals--;
156
157 /*
158 * We've close the last open window/file, therefore the
159 * application must end.
160 */
161 if (nglobals <= 0) {
162 #ifdef WITH_DMALLOC
163 dmalloc_shutdown();
164 #endif
165 exit(0);
166 }
167 }
168
169 void
MdiSelectGlobal(int offset1,int offset2,void * ptr)170 MdiSelectGlobal(int offset1, int offset2, void *ptr)
171 {
172 int i;
173 char **p, **q;
174
175 for (i=0; i < maxglobals; i++)
176 if (globals[i].valid) {
177 p = (char **) (((char *)&globals[i]) + offset1);
178 q = (char **) (*p + offset2);
179 if (*q == ptr) {
180 Global = &globals[i];
181 #if 0
182 fprintf(stderr, "MdiSelectGlobal: match for %d,%d,%p -> %p\n",
183 offset1, offset2, ptr, Global);
184 #endif
185 return;
186 }
187 }
188
189 MdiError(offset1, offset2, ptr);
190 }
191
192 int
MdiHasFile(void)193 MdiHasFile(void)
194 {
195 return Global->FileName != NULL;
196 }
197
198 void
MdiQuit(void)199 MdiQuit(void)
200 {
201 }
202