| 1 | |
|---|
| 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 |
|---|
| 24 | static char THIS_FILE[] = __FILE__; |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #ifdef _DEBUG |
|---|
| 28 | static HANDLE __hStdOut = NULL; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | void 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 | |
|---|
| 54 | static 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 | |
|---|
| 129 | CCADApp theApp; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | BEGIN_MESSAGE_MAP(CCADApp, CWinApp) |
|---|
| 135 | |
|---|
| 136 | ON_COMMAND(ID_HELP_CHECKFORUPDATES, OnHelpUpdates) |
|---|
| 137 | ON_COMMAND(ID_HELP_LEOCADHOMEPAGE, OnHelpHomePage) |
|---|
| 138 | ON_COMMAND(ID_HELP_SENDEMAIL, OnHelpEmail) |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | ON_UPDATE_COMMAND_UI(ID_FILE_MRU_FILE1, OnUpdateRecentFileMenu) |
|---|
| 142 | ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) |
|---|
| 143 | END_MESSAGE_MAP() |
|---|
| 144 | |
|---|
| 145 | CCADApp::CCADApp() |
|---|
| 146 | { |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | BOOL CCADApp::InitInstance() |
|---|
| 150 | { |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | #if _MFC_VER < 0x0710 |
|---|
| 157 | #ifdef _AFXDLL |
|---|
| 158 | Enable3dControls(); |
|---|
| 159 | #else |
|---|
| 160 | Enable3dControlsStatic(); |
|---|
| 161 | #endif |
|---|
| 162 | #endif |
|---|
| 163 | |
|---|
| 164 | SetRegistryKey(_T("BT Software")); |
|---|
| 165 | |
|---|
| 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 | |
|---|
| 182 | |
|---|
| 183 | CSingleDocTemplate* pDocTemplate; |
|---|
| 184 | pDocTemplate = new CSingleDocTemplate( |
|---|
| 185 | IDR_MAINFRAME, |
|---|
| 186 | RUNTIME_CLASS(CCADDoc), |
|---|
| 187 | RUNTIME_CLASS(CMainFrame), |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | 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 | |
|---|
| 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 | |
|---|
| 256 | int 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 | |
|---|
| 274 | void CCADApp::OnHelpUpdates() |
|---|
| 275 | { |
|---|
| 276 | CheckForUpdates(this); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | void CCADApp::OnHelpHomePage() |
|---|
| 280 | { |
|---|
| 281 | ShellExecute(::GetDesktopWindow(), _T("open"), _T("http://www.leocad.org"), NULL, NULL, SW_NORMAL); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | void CCADApp::OnHelpEmail() |
|---|
| 285 | { |
|---|
| 286 | ShellExecute(::GetDesktopWindow(), _T("open"), _T("mailto:leonardo@centroin.com.br?subject=LeoCAD"), NULL, NULL, SW_NORMAL); |
|---|
| 287 | } |
|---|