root / trunk / win / Leocad.cpp

Revision 724, 7.0 kB (checked in by Leo, 7 months ago)

Small cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// LeoCAD.cpp : Defines the class behaviors for the application.
2//
3
4#include "lc_global.h"
5#include "LeoCAD.h"
6
7#include "MainFrm.h"
8#include "CADDoc.h"
9#include "CADView.h"
10#include <wininet.h>
11#include <process.h>
12#include "project.h"
13#include "globals.h"
14#include "system.h"
15#include "mainwnd.h"
16#include "library.h"
17#include "keyboard.h"
18#include "lc_application.h"
19#include "preview.h"
20
21#ifdef _DEBUG
22#define new DEBUG_NEW
23#undef THIS_FILE
24static char THIS_FILE[] = __FILE__;
25#endif
26
27#ifdef _DEBUG
28static HANDLE __hStdOut = NULL;
29
30// Use wprintf like TRACE0, TRACE1, ... (The arguments are the same as printf)
31void wprintf(char *fmt, ...)
32{
33    if(!__hStdOut)
34    {
35        AllocConsole();
36        SetConsoleTitle("Debug Window");
37        __hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
38        COORD co = {80, 25};
39        SetConsoleScreenBufferSize(__hStdOut, co);
40    }
41
42    char s[300];
43    va_list argptr;
44    va_start(argptr, fmt);
45    vsprintf(s, fmt, argptr);
46    va_end(argptr);
47    strcat(s, "\n");
48    DWORD cCharsWritten;
49    WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten, NULL);
50}
51#endif
52
53// If Data is NULL this function will only display a message if there are updates.
54static void CheckForUpdates(void* Data)
55{
56    HINTERNET session = InternetOpen("LeoCAD", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0) ;
57   
58    char szSizeBuffer[32];
59    DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
60    DWORD dwFileSize;
61    DWORD dwBytesRead;
62    CString Contents;
63
64    HINTERNET hHttpFile = InternetOpenUrl(session, "http://www.leocad.org/updates.txt", NULL, 0, 0, 0);
65
66    if (hHttpFile)
67    {
68        if(HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL))
69        {
70            dwFileSize = atol(szSizeBuffer);
71            LPSTR szContents = Contents.GetBuffer(dwFileSize+1);
72            szContents[dwFileSize] = 0;
73
74            if (InternetReadFile(hHttpFile, szContents, dwFileSize, &dwBytesRead))
75            {
76                float ver;
77                int lib;
78
79                if (sscanf(szContents, "%f %d", &ver, &lib) == 2)
80                {
81                    CString str;
82                    bool Update = false;
83
84                    if (ver > LC_VERSION_MAJOR + (float)LC_VERSION_MINOR/100 + (float)LC_VERSION_PATCH/1000)
85                    {
86                        str.Format("There's a newer version of LeoCAD available for download (%0.3f).\n", ver);
87                        Update = true;
88                    }
89                    else
90                        str = "You are using the latest version of LeoCAD.\n";
91
92                    if (lib > lcGetPiecesLibrary()->GetPieceCount ())
93                    {
94                        str += "There are new pieces available.\n\n";
95                        Update = true;
96                    }
97                    else
98                        str += "There are no new pieces available at this time.\n";
99
100                    if (Data || Update)
101                    {
102                        if (Update)
103                        {
104                            str += "Would you like to visit the LeoCAD website now?\n";
105
106                            if (AfxMessageBox(str, MB_YESNO | MB_ICONQUESTION) == IDYES)
107                            {
108                                ShellExecute(::GetDesktopWindow(), _T("open"), _T("http://www.leocad.org"), NULL, NULL, SW_NORMAL);
109                            }
110                        }
111                        else
112                            AfxMessageBox(str, MB_OK | MB_ICONINFORMATION);
113                    }
114                }
115                else if (Data != NULL)
116                    AfxMessageBox("Unknown file information.");
117            }
118            InternetCloseHandle(hHttpFile);
119        }
120        else if (Data != NULL)
121            AfxMessageBox("Could not connect.");
122    }
123    else if (Data != NULL)
124        AfxMessageBox("Could not connect.");
125
126    InternetCloseHandle(session);
127}
128
129CCADApp theApp;
130
131/////////////////////////////////////////////////////////////////////////////
132// CCADApp
133
134BEGIN_MESSAGE_MAP(CCADApp, CWinApp)
135    //{{AFX_MSG_MAP(CCADApp)
136    ON_COMMAND(ID_HELP_CHECKFORUPDATES, OnHelpUpdates)
137    ON_COMMAND(ID_HELP_LEOCADHOMEPAGE, OnHelpHomePage)
138    ON_COMMAND(ID_HELP_SENDEMAIL, OnHelpEmail)
139    //}}AFX_MSG_MAP
140    // Standard print setup command
141    ON_UPDATE_COMMAND_UI(ID_FILE_MRU_FILE1, OnUpdateRecentFileMenu)
142    ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
143END_MESSAGE_MAP()
144
145CCADApp::CCADApp()
146{
147}
148
149BOOL CCADApp::InitInstance()
150{
151    // Standard initialization
152    // If you are not using these features and wish to reduce the size
153    //  of your final executable, you should remove from the following
154    //  the specific initialization routines you do not need.
155
156#if _MFC_VER < 0x0710
157#ifdef _AFXDLL
158    Enable3dControls();         // Call this when using MFC in a shared DLL
159#else
160    Enable3dControlsStatic();   // Call this when linking to MFC statically
161#endif
162#endif
163
164    SetRegistryKey(_T("BT Software"));
165//  LoadStdProfileSettings();  // Load standard INI file options (including MRU)
166
167    InitKeyboardShortcuts();
168
169    char app[LC_MAXPATH], *ptr;
170    GetModuleFileName(NULL, app, LC_MAXPATH);
171    ptr = strrchr(app,'\\');
172    if (ptr)
173        *(++ptr) = 0;
174
175    g_App = new lcApplication();
176    main_window = new MainWnd();
177
178    if (!g_App->Initialize(__argc, __targv, app))
179        return false;
180
181    // Register the application's document templates.  Document templates
182    //  serve as the connection between documents, frame windows and views.
183    CSingleDocTemplate* pDocTemplate;
184    pDocTemplate = new CSingleDocTemplate(
185        IDR_MAINFRAME,
186        RUNTIME_CLASS(CCADDoc),
187        RUNTIME_CLASS(CMainFrame),       // main SDI frame window
188        RUNTIME_CLASS(CCADView));
189    AddDocTemplate(pDocTemplate);
190
191    EnableShellOpen();
192    RegisterShellFileTypes(TRUE);
193
194    UINT cmdshow = m_nCmdShow;
195    m_nCmdShow = SW_HIDE;
196    pDocTemplate->OpenDocumentFile(NULL);
197
198    // Show something in the piece preview window.
199    PieceInfo* Info = lcGetPiecesLibrary()->FindPieceInfo("3005");
200    if (!Info)
201        Info = lcGetPiecesLibrary()->GetPieceInfo(0);
202
203    if (Info)
204        g_App->m_PiecePreview->SetSelection(Info);
205
206/*
207    char out[_MAX_PATH];
208    GetTempPath(_MAX_PATH, out);
209    strcat(out, "~LC*.lcd");
210
211    WIN32_FIND_DATA fd;
212    HANDLE fh = FindFirstFile(out, &fd);
213    if (fh != INVALID_HANDLE_VALUE)
214    {
215        if (char *ptr = strrchr(out, '\\')) *(ptr+1) = 0;
216        strcat(out, fd.cFileName);
217        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)
218        {
219            if (AfxMessageBox(_T("Delete file ?"), MB_YESNO) == IDYES)
220                DeleteFile(out);
221        }
222        else
223        {
224            cmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen;
225            cmdInfo.m_strFileName = out;
226        }
227    }
228
229//  if (cmdInfo.m_strFileName.IsEmpty())
230//      ParseCommandLine(cmdInfo);
231*/
232
233    // The one and only window has been initialized, so show and update it.
234    int status = theApp.GetProfileInt("Settings", "Window Status", -1);
235    if (status != -1)
236        m_pMainWnd->ShowWindow(status);
237    else
238        m_pMainWnd->ShowWindow(cmdshow);
239    m_pMainWnd->UpdateWindow();
240    lcGetActiveProject()->HandleNotify(LC_ACTIVATE, 1);
241    lcGetActiveProject()->UpdateInterface();
242
243    main_window->UpdateMRU();
244
245    // Enable drag/drop open
246    m_pMainWnd->DragAcceptFiles();
247
248    lcGetActiveProject()->UpdateAllViews();
249
250    if (AfxGetApp()->GetProfileInt("Settings", "CheckUpdates", 1))
251        _beginthread(CheckForUpdates, 0, NULL);
252
253    return TRUE;
254}
255
256int CCADApp::ExitInstance()
257{
258    delete main_window;
259    main_window = NULL;
260
261    g_App->Shutdown();
262
263    delete g_App;
264    g_App = NULL;
265
266#ifdef _DEBUG
267    if (__hStdOut != NULL)
268        FreeConsole();
269#endif
270
271    return CWinApp::ExitInstance();
272}
273
274void CCADApp::OnHelpUpdates()
275{
276    CheckForUpdates(this);
277}
278
279void CCADApp::OnHelpHomePage()
280{
281    ShellExecute(::GetDesktopWindow(), _T("open"), _T("http://www.leocad.org"), NULL, NULL, SW_NORMAL);
282}
283
284void CCADApp::OnHelpEmail()
285{
286    ShellExecute(::GetDesktopWindow(), _T("open"), _T("mailto:leonardo@centroin.com.br?subject=LeoCAD"), NULL, NULL, SW_NORMAL);
287}
Note: See TracBrowser for help on using the browser.