summaryrefslogtreecommitdiff
path: root/win/Leocad.cpp
blob: f792a4372fd85b4b0bfd841446f2caca58182be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// LeoCAD.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "LeoCAD.h"

#include "MainFrm.h"
#include "CADDoc.h"
#include "CADView.h"
#include <wininet.h>
#include "project.h"
#include "globals.h"
#include "system.h"
#include "pieceinf.h" // TODO: remove

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifdef _DEBUG
static HANDLE __hStdOut = NULL;

// Use wprintf like TRACE0, TRACE1, ... (The arguments are the same as printf)
void wprintf(char *fmt, ...)
{
	if(!__hStdOut)
	{
		AllocConsole();
		SetConsoleTitle("Debug Window");
		__hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
		COORD co = {80, 25};
		SetConsoleScreenBufferSize(__hStdOut, co);
	}

	char s[300];
	va_list argptr;
	va_start(argptr, fmt);
	vsprintf(s, fmt, argptr);
	va_end(argptr);
	strcat(s, "\n");
	DWORD cCharsWritten;
	WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten, NULL);
}
#endif

/////////////////////////////////////////////////////////////////////////////
// CCADApp

BEGIN_MESSAGE_MAP(CCADApp, CWinApp)
	//{{AFX_MSG_MAP(CCADApp)
	ON_COMMAND(ID_HELP_CHECKFORUPDATES, OnHelpUpdates)
	ON_COMMAND(ID_HELP_LEOCADHOMEPAGE, OnHelpHomePage)
	ON_COMMAND(ID_HELP_SENDEMAIL, OnHelpEmail)
	//}}AFX_MSG_MAP
	// Standard print setup command
	ON_UPDATE_COMMAND_UI(ID_FILE_MRU_FILE1, OnUpdateRecentFileMenu)
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCADApp construction

CCADApp::CCADApp()
{
	m_hMutex = NULL;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CCADApp object

CCADApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CCADApp initialization

BOOL CCADApp::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	SetRegistryKey(_T("BT Software"));
//	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

  if (!GL_Initialize (NULL))
    return FALSE;

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CCADDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CCADView));
	AddDocTemplate(pDocTemplate);

	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

	project = new Project;

	UINT cmdshow = m_nCmdShow;
	m_nCmdShow = SW_HIDE;
	pDocTemplate->OpenDocumentFile(NULL);

	char app[LC_MAXPATH], *ptr;
	GetModuleFileName (NULL, app, LC_MAXPATH);
	ptr = strrchr(app,'\\');
	if (ptr)
		*(++ptr) = 0;

	if (!project->Initialize(__argc, __targv, app, NULL))
		return false;


	// TODO: move this to the project.
	CString cfg = GetProfileString("Settings","Groups","");
	if (!cfg.IsEmpty())
	{
		UINT j, i;
		CWaitCursor wait;
		CFile f;
		if (f.Open(cfg, CFile::modeRead|CFile::shareDenyWrite) != 0)
		if (f.GetLength() != 0)
		{
			CArchive ar(&f, CArchive::load | CArchive::bNoFlushOnDelete);
			char tmp[33];
			float ver;
			CString str;
			UINT n;
			ar.Read (tmp, 32);
			ar >> ver;
			if (ver == 0.1f)
				ar >> i;
			else
			{
				BYTE b;
				ar >> b;
				for (j = 0; j < b; j++)
				{
					ar.Read (tmp, 33);

					if (ver > 0.2f)
						ar >> i;
				}
			}
			if (ver > 0.2f)
			{
				CImageList iml;
				iml.Read(&ar);
			}

			ar >> n;
			for (i = 0; i < n; i++)
			{
				DWORD dw;
				char name[9];
				ar.Read (name, 9);

				if (ver == 0.1f)
				{
					char tmp[65];
					BYTE b;
					ar.Read (tmp, 65);
					ar >> b;
					dw = 1 << b;
				}
				else
					ar >> dw;

				for (int j = 0; j < project->GetPieceLibraryCount(); j++)
				{
					PieceInfo* pInfo = project->GetPieceInfo(j);

					if (strcmp (pInfo->m_strName, name) == 0)
					{
						pInfo->m_nGroups = dw;
						break;
					}
				}
			}
			ar.Close();
			f.Close();
		}
	}


/*
	m_hMutex = CreateMutex(NULL, FALSE, _T("LeoCAD_Mutex"));
	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
//		ParseCommandLine(cmdInfo);
	}
	else
	{
		char out[_MAX_PATH];
		GetTempPath (_MAX_PATH, out);
		strcat (out, "~LC*.lcd");

		WIN32_FIND_DATA fd;
		HANDLE fh = FindFirstFile(out, &fd);
		if (fh != INVALID_HANDLE_VALUE)
		{
			if (char *ptr = strrchr (out, '\\')) *(ptr+1) = 0;
			strcat (out, fd.cFileName);
			if (AfxMessageBox (_T("LeoCAD found a file that was being edited while the program exited unexpectdly. Do you want to load it ?"), MB_YESNO) == IDNO)
			{
				if (AfxMessageBox (_T("Delete file ?"), MB_YESNO) == IDYES)
					DeleteFile (out);
			}
			else
			{
				cmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen;
				cmdInfo.m_strFileName = out;
			}
		}

//		if (cmdInfo.m_strFileName.IsEmpty())
//			ParseCommandLine(cmdInfo);
	}
*/

	// The one and only window has been initialized, so show and update it.
	int status = theApp.GetProfileInt("Settings", "Window Status", -1);
	if (status != -1)
		m_pMainWnd->ShowWindow(status);
	else
		m_pMainWnd->ShowWindow(cmdshow);
	m_pMainWnd->UpdateWindow();
	project->HandleNotify(LC_ACTIVATE, 1);

	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();

	return TRUE;
}

int CCADApp::ExitInstance() 
{
	if (m_hMutex != NULL)
		ReleaseMutex(m_hMutex);

	if (project)
	{
		project->HandleNotify(LC_ACTIVATE, 0);
		delete project;
	}

#ifdef _DEBUG
	if (__hStdOut != NULL)
		FreeConsole();
#endif

	GL_Shutdown ();

	return CWinApp::ExitInstance();
}

/////////////////////////////////////////////////////////////////////////////
// CCADApp commands

void CCADApp::OnHelpUpdates() 
{
	HINTERNET session = InternetOpen("LeoCAD", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0) ;
	
	char szSizeBuffer[32];
	DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer); 
	DWORD dwFileSize;
	DWORD dwBytesRead;
	CString Contents;

	HINTERNET hHttpFile = InternetOpenUrl(session, "http://www.leocad.org", NULL, 0, 0, 0);

	if (hHttpFile)
	{
		if(HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL))
		{	 
			dwFileSize = atol(szSizeBuffer);
			LPSTR szContents = Contents.GetBuffer(dwFileSize);
			
			if (InternetReadFile(hHttpFile, szContents, dwFileSize, &dwBytesRead))
			{
				float ver, lib;
				if (sscanf (szContents, "%f %f", &ver, &lib) == 2)
				{
					CString str;

					if (ver > LC_APP_VERSION)
						str.Format("There's a new version (%0.2f) on the home page.\n", ver);
					else
						str = "You are using the latest version of the program.\n";

					if (lib > project->GetPieceLibraryCount())
						str += "There's an updated piece library.";
					else
						str += "There are no new parts.";

					AfxMessageBox(str);
				}
				else
					AfxMessageBox("Unknown information.");
			}
			InternetCloseHandle(hHttpFile);
		}
	}
	else
		AfxMessageBox("Could not connect.");

	InternetCloseHandle(session);
}

void CCADApp::OnHelpHomePage() 
{
	ShellExecute(::GetDesktopWindow(), _T("open"), _T("http://www.leocad.org"), NULL, NULL, SW_NORMAL); 
}

void CCADApp::OnHelpEmail() 
{
	ShellExecute(::GetDesktopWindow(), _T("open"), _T("mailto:leonardo@centroin.com.br?subject=LeoCAD"), NULL, NULL, SW_NORMAL);
}