1 // studiox.cpp : Defines the class behaviors for the application.
2 //
3 
4 #include "studiox_includes.h"
5 #include "studioxDoc.h"
6 #include "splash_screen.h"
7 
8 #include "studiox_includes.h"
9 #include "studiox.h"
10 #include "clone_repo_dialog.h"
11 
12 #ifdef _DEBUG
13 #define new DEBUG_NEW
14 #endif
15 
16 /* some fonts used by this app */
17 
18 CFont TitleFont;
19 CFont MediumFont;
20 CFont NormalFont;
21 CFont TinyFont;
22 CFont ViewHeaderFont;
23 CFont AsteriskFont;
24 UINT  StudioClipboardFormat = 0;
25 
26 // CstudioxApp
27 
BEGIN_MESSAGE_MAP(CstudioxApp,CWinApp)28 BEGIN_MESSAGE_MAP(CstudioxApp, CWinApp)
29 	ON_COMMAND(ID_APP_ABOUT, &CstudioxApp::OnAppAbout)
30     ON_COMMAND(ID_HELP_USER_GUIDE, &CstudioxApp::OnUsersGuide)
31 	// Standard file based document commands
32 	ON_COMMAND(ID_PROJECT_NEW, &CstudioxApp::OnProjectNew)
33     ON_COMMAND(ID_FILE_NEW, &CstudioxApp::OnProjectNew)
34 	ON_COMMAND(ID_PROJECT_OPEN, &CstudioxApp::OnProjectOpen)
35     ON_COMMAND(ID_FILE_OPEN, &CstudioxApp::OnProjectOpen)
36     ON_COMMAND(ID_PROJECT_SAVE, &CstudioxApp::OnProjectSave)
37     ON_COMMAND(ID_FILE_SAVE, &CstudioxApp::OnProjectSave)
38     ON_COMMAND(ID_PROJECT_SAVE_AS, &CstudioxApp::OnProjectSaveAs)
39     ON_COMMAND(ID_PROJECT_CLOSE, &CstudioxApp::OnProjectClose)
40     ON_COMMAND(ID_FILE_CLOSE, &CstudioxApp::OnProjectClose)
41     ON_COMMAND(ID_PROJECT_IMPORT, &CstudioxApp::OnProjectMerge)
42 	// Standard print setup command
43 END_MESSAGE_MAP()
44 
45 
46 
47 // CstudioxApp construction
48 
49 CstudioxApp::CstudioxApp()
50 {
51 	// TODO: add construction code here,
52 	// Place all significant initialization in InitInstance
53    #if defined (REPORT_MEMORY_LEAKS)
54     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
55    #endif
56 }
57 
58 
59 // The one and only CstudioxApp object
60 
61 CstudioxApp theApp;
62 
63 
CreateMainFrame()64 CFrameWnd *CstudioxApp::CreateMainFrame()
65 {
66     LoadIcon(IDR_MAINFRAME);
67 
68 	CFrameWnd* pFrame = new CMainFrame();
69 
70 	// create new from resource
71 	if (!pFrame->LoadFrame(IDR_MAINFRAME,
72 			WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,   // default frame styles
73 			NULL, NULL))
74 	{
75 		TRACE(traceAppMsg, 0, "Warning: couldn't create a frame.\n");
76 		// frame will be deleted in PostNcDestroy cleanup
77 		return NULL;
78 	}
79 
80 	// it worked !
81 	return pFrame;
82 }
83 
84 // CstudioxApp initialization
85 CString splash_class_name;
86 CString studiox_version_string;
87 
DeleteSystemFonts()88 void CstudioxApp::DeleteSystemFonts()
89 {
90     TitleFont.DeleteObject();
91     MediumFont.DeleteObject();
92     NormalFont.DeleteObject();
93     AsteriskFont.DeleteObject();
94     TinyFont.DeleteObject();
95     ViewHeaderFont.DeleteObject();
96 }
97 
CreateSystemFonts()98 void CstudioxApp::CreateSystemFonts()
99 {
100     /* Create the fonts used by app */
101 
102 // this formula can be used to convert point size to logical units
103 // lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
104 
105     HDC dc = ::GetDC(NULL);
106 
107     // Get number of pixels per logical inch along the screen height
108     int dpi = GetDeviceCaps(dc, LOGPIXELSY);
109     int text_scaler = GetTextScaler();
110 
111     TitleFont.CreateFont(TITLE_FONT_HEIGHT, 0, 0, 0,
112         FW_BOLD, FALSE, FALSE, 0,
113         ANSI_CHARSET,
114         OUT_DEFAULT_PRECIS,
115         CLIP_DEFAULT_PRECIS,
116         CLEARTYPE_QUALITY,
117         DEFAULT_PITCH | FF_SWISS, _T("Arial"));
118 
119     // Convert point size to logic unit
120     int height = -MulDiv(MEDIUM_FONT_PT_SIZE, dpi, 72);
121     height = MulDiv(height, text_scaler, DEFAULT_TEXT_SCALER);
122     MediumFont.CreateFont(height, 0, 0, 0,
123         FW_SEMIBOLD, FALSE, FALSE, 0,
124         ANSI_CHARSET,
125         OUT_TT_PRECIS,
126         CLIP_DEFAULT_PRECIS,
127         CLEARTYPE_QUALITY,
128         DEFAULT_PITCH | FF_SWISS, _T("Arial"));
129 
130     height = -MulDiv(NORMAL_FONT_PT_SIZE, dpi, 72);
131     height = MulDiv(height, text_scaler, DEFAULT_TEXT_SCALER);
132     NormalFont.CreateFont(height, 0, 0, 0,
133         FW_NORMAL, FALSE, FALSE, 0,
134         ANSI_CHARSET,
135         OUT_TT_PRECIS,
136         CLIP_DEFAULT_PRECIS,
137         CLEARTYPE_QUALITY,
138         DEFAULT_PITCH | FF_SWISS, _T("Arial"));
139 
140     height = -MulDiv(ASTERISK_FONT_PT_SIZE, dpi, 72);
141     height = MulDiv(height, text_scaler, DEFAULT_TEXT_SCALER);
142     AsteriskFont.CreateFont(height, 0, 0, 0,
143         FW_NORMAL, FALSE, FALSE, 0,
144         ANSI_CHARSET,
145         OUT_TT_PRECIS,
146         CLIP_DEFAULT_PRECIS,
147         CLEARTYPE_QUALITY,
148         DEFAULT_PITCH | FF_SWISS, _T("Arial"));
149 
150     TinyFont.CreateFont(TYNY_FONT_HEIGHT, 0, 0, 0,
151         FW_BOLD, FALSE, FALSE, 0,
152         ANSI_CHARSET,
153         OUT_TT_PRECIS,
154         CLIP_DEFAULT_PRECIS,
155         CLEARTYPE_QUALITY,
156         DEFAULT_PITCH | FF_DONTCARE, NULL);
157 
158     height = -MulDiv(VIEW_HEADER_FONT_PT_SIZE, dpi, 72);
159     height = MulDiv(height, text_scaler, DEFAULT_TEXT_SCALER);
160     ViewHeaderFont.CreateFont(height, 0, 0, 0,
161         FW_SEMIBOLD, FALSE, FALSE, 0,
162         ANSI_CHARSET,
163         OUT_TT_PRECIS,
164         CLIP_DEFAULT_PRECIS,
165         CLEARTYPE_QUALITY,
166         DEFAULT_PITCH | FF_SWISS, _T("Arial"));
167 
168     ReleaseDC(NULL, dc);
169 }
170 
InitInstance()171 BOOL CstudioxApp::InitInstance()
172 {
173 	// InitCommonControlsEx() is required on Windows XP if an application
174 	// manifest specifies use of ComCtl32.dll version 6 or later to enable
175 	// visual styles.  Otherwise, any window creation will fail.
176 	INITCOMMONCONTROLSEX InitCtrls;
177 	InitCtrls.dwSize = sizeof(InitCtrls);
178 	// Set this to include all the common control classes you want to use
179 	// in your application.
180 	InitCtrls.dwICC = ICC_WIN95_CLASSES;
181 	InitCommonControlsEx(&InitCtrls);
182 
183 	CWinApp::InitInstance();
184 
185 	// Initialize OLE libraries
186 	if (!AfxOleInit())
187 	{
188 		AfxMessageBox(IDP_OLE_INIT_FAILED);
189 		return FALSE;
190 	}
191 	AfxEnableControlContainer();
192 
193 	//LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)
194 	// Register the application's document templates.  Document templates
195 	//  serve as the connection between documents, frame windows and views
196 
197     CreateSystemFonts();
198 
199     /* Initialize our INI_INFO structure */
200     ReadIniInfo();
201 
202     /* Register out clipboard format */
203     StudioClipboardFormat = RegisterClipboardFormat(_T("GUIX_Studio_Widget_Info"));
204 
205     /* Create the MainFrame, but don't show it yet */
206     m_pMainWnd = CreateMainFrame();
207 
208     if (!m_pMainWnd)
209     {
210         return FALSE;
211     }
212 
213     studiox_version_string.Format(_T("%d.%d.%d.%d"),
214             GUIX_MAJOR_VERSION,
215             GUIX_MINOR_VERSION,
216             GUIX_PATCH_VERSION,
217             STUDIOX_VERSION_NUMBER);
218 
219 #if defined(STUDIO_ENGINEERING_RELEASE)
220     studiox_version_string += CString("-engineering");
221 #endif
222 
223     CString fulltitle = _T("Eclipse ThreadX GUIX Studio ");
224     fulltitle += studiox_version_string;
225 
226     m_pMainWnd->SetWindowText(fulltitle);
227 
228     splash_class_name = AfxRegisterWndClass(
229       CS_VREDRAW | CS_HREDRAW,
230       ::LoadCursor(NULL, IDC_ARROW),
231       CreateSolidBrush(RGB(0, 0, 0)),
232       ::LoadIcon(NULL, IDI_APPLICATION));
233 
234     /* Read command info. */
235     CMainFrame *pMain = (CMainFrame *) AfxGetMainWnd();
236     CCommandInfo *pCmdInfo = pMain->GetCmdInfo();
237 
238     if(m_lpCmdLine[0])
239     {
240         pCmdInfo -> ReadCommandLineInfo(m_lpCmdLine);
241 
242         CString guix_repo_local_path = pCmdInfo->GetGuixRepoLocalPath();
243         if (!guix_repo_local_path.IsEmpty())
244         {
245             if (guix_repo_local_path.GetLength() >= MAX_PATH)
246             {
247                 return FALSE;
248             }
249             char local_path[MAX_PATH];
250             memset(local_path, 0, MAX_PATH);
251             HANDLE file_map_handle = pCmdInfo->GetGuixCloneWritePipeHandle();
252             strcpy_s(local_path, MAX_PATH, CStringA(guix_repo_local_path).GetString());
253             CloneGUIXRepo(local_path, file_map_handle);
254 
255             pMain->OnClose();
256 
257             return TRUE;
258         }
259     }
260 
261     /* Display the Splash screen */
262 #if !defined(_DEBUG)
263 //#if 1
264 
265     if(!pCmdInfo->IsNoGui())
266     {
267         int tag_status;
268 
269         CRect childrect(0, 0, 425, 283);
270         splash_screen *splash = new splash_screen(TRUE);
271         splash->CreateEx(0, splash_class_name, NULL,
272             WS_POPUP|WS_VISIBLE|WS_BORDER, childrect, NULL, 0, NULL);
273         tag_status = splash->RunModalLoop();
274         splash->DestroyWindow();
275         delete splash;
276 
277         if (tag_status != 0)
278         {
279             return FALSE;
280         }
281     }
282 #endif
283 	// The one and only window has been initialized, so show and update it
284 
285     if (!pCmdInfo->IsNoGui())
286     {
287         m_pMainWnd->ShowWindow(SW_SHOW);
288         m_pMainWnd->UpdateWindow();
289     }
290 
291     //Initialize GDI+ resources
292     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
293 
294     if (m_lpCmdLine[0])
295     {
296         /* Open the log file. */
297         if (pCmdInfo->GetLogFile())
298         {
299             fprintf(pCmdInfo->GetLogFile(), "\nProject File: %s\\%s\n", (char*)CW2A(pCmdInfo->GetProjectPath()), (char*)CW2A(pCmdInfo->GetProjectName()));
300         }
301 
302         /* Open project. */
303         if (GetProjectView()->OpenProject(pCmdInfo->GetProjectPath(), pCmdInfo->GetProjectName()))
304         {
305 
306             CString output_path = pCmdInfo->GetOutputPath();
307             if (!output_path.IsEmpty())
308             {
309                 studiox_project *project = GetOpenProject();
310                 if (project)
311                 {
312                     project->mHeader.resource_path = output_path;
313                     project->mHeader.source_path = output_path;
314                     project->mHeader.header_path = output_path;
315                 }
316             }
317             if (pCmdInfo->IsXmlMode())
318             {
319                 /* Open the log file. */
320                 if (pCmdInfo->GetLogFile())
321                 {
322                     fprintf(pCmdInfo->GetLogFile(), "Generating Resource File.\n");
323                 }
324 
325                 if (pCmdInfo->IsBinaryMode())
326                 {
327                     binary_resource_gen *generater = new binary_resource_gen(GetOpenProject(), BINARY_FILE_FORMAT_BIN_STANDALONE);
328                     generater->GenerateBinaryFile(0);
329                     delete generater;
330                 }
331                 else
332                 {
333                     resource_gen* generater = new resource_gen(GetOpenProject());
334                     generater->GenerateResourceFile(0);
335                     delete generater;
336                 }
337 
338                 if (pCmdInfo->GetLogFile())
339                 {
340                     fprintf(pCmdInfo->GetLogFile(), "Resource File Generation Completed.\n");
341                 }
342             }
343             else
344             {
345 
346                 CString pathname = pCmdInfo->GetStringImportFileName();
347 
348                 if (!pathname.IsEmpty())
349                 {
350                     /* Open the log file. */
351                     if (pCmdInfo->GetLogFile())
352                     {
353                         fprintf(pCmdInfo->GetLogFile(), "Improting String Data.\n");
354                     }
355 
356                     //import string
357                     BOOL successed = FALSE;
358                     CString extention = PathFindExtension(pathname);
359                     extention.MakeLower();
360 
361                     if (extention == ".csv")
362                     {
363                         csv_read_write reader;
364                         if (reader.ImportCsvFile(GetOpenProject(), pathname))
365                         {
366                             successed = TRUE;
367                         }
368                     }
369                     else
370                     {
371                         xliff_read_write reader;
372 
373                         if (reader.ImportXliffFile(GetOpenProject(), pathname))
374                         {
375                             successed = TRUE;
376                         }
377                     }
378 
379                     if (successed)
380                     {
381                         GetOpenProject()->SetModified();
382                         Notify("String Data Import Completed.");
383                     }
384                     else
385                     {
386                         ErrorMsg("String Data Import Failed!");
387                     }
388                 }
389 
390                 if (pCmdInfo->IsNoGui())
391                 {
392 
393                     /* Generate resource files. */
394                     if (pCmdInfo->GenResource())
395                     {
396                         if (pCmdInfo->IsBinaryMode())
397                         {
398                             GetProjectView()->OnGenBinary();
399                         }
400                         else
401                         {
402                             GetProjectView()->OnGenResources();
403                         }
404                     }
405 
406                     /* Generate specification files. */
407                     if (pCmdInfo->GenSpecification())
408                     {
409                         GetProjectView()->OnGenApplication();
410                     }
411                 }
412             }
413         }
414 
415         /* Close the log file. */
416         if(pCmdInfo ->GetLogFile())
417         {
418             fclose(pCmdInfo ->GetLogFile());
419         }
420 
421         if(pCmdInfo->IsNoGui())
422         {
423             pMain->OnClose();
424         }
425     }
426     else
427     {
428         ((CMainFrame *)AfxGetMainWnd())->DisableMenus();
429         GetTargetView()->InitialDisplay();
430     }
431 
432 	// call DragAcceptFiles only if there's a suffix
433 	//  In an SDI app, this should occur after ProcessShellCommand
434 	return TRUE;
435 }
436 
ExitInstance()437 int CstudioxApp::ExitInstance()
438 {
439     GdiplusShutdown(gdiplusToken);
440 
441     return CWinApp::ExitInstance();
442 }
443 
444 
445 // App command to run the dialog
OnAppAbout()446 void CstudioxApp::OnAppAbout()
447 {
448     CRect childrect(0, 0, 425, 283);
449     splash_screen *splash = new splash_screen(FALSE);
450     splash->CreateEx(0, splash_class_name, NULL,
451         WS_POPUP|WS_VISIBLE|WS_BORDER, childrect, m_pMainWnd, 0, NULL);
452     splash->RunModalLoop();
453     splash->DestroyWindow();
454     delete splash;
455 }
456 
OnUsersGuide()457 void CstudioxApp::OnUsersGuide()
458 {
459     ShellExecute(NULL, _T("open"), _T("https://github.com/eclipse-threadx/rtos-docs/blob/main/rtos-docs/guix/about-guix-studio.md"), NULL, NULL, SW_SHOW);
460 }
461 
OnProjectNew()462 void CstudioxApp::OnProjectNew()
463 {
464     UndoManager()->Reset();
465 
466     CMainFrame *pMain = (CMainFrame *) m_pMainWnd;
467     pMain->GetProjectView()->NewProject();
468 }
469 
OnProjectOpen()470 void CstudioxApp::OnProjectOpen()
471 {
472     UndoManager()->Reset();
473     CMainFrame *pMain = (CMainFrame *) m_pMainWnd;
474     pMain->GetProjectView()->OpenProject();
475 
476 }
477 
OnProjectMerge()478 void CstudioxApp::OnProjectMerge()
479 {
480     UndoManager()->Reset();
481     CMainFrame *pMain = (CMainFrame *) m_pMainWnd;
482     pMain->GetProjectView()->MergeProject();
483 }
484 
OnProjectClose()485 void CstudioxApp::OnProjectClose()
486 {
487     UndoManager()->Reset();
488 
489     CMainFrame *pMain = (CMainFrame *) m_pMainWnd;
490     pMain->GetProjectView()->CloseProject(TRUE);
491 }
492 
OnProjectSave()493 void CstudioxApp::OnProjectSave()
494 {
495     UndoManager()->Reset();
496 
497     if (GetOpenProject())
498     {
499         GetOpenProject()->Save();
500     }
501 }
502 
OnProjectSaveAs()503 void CstudioxApp::OnProjectSaveAs()
504 {
505     UndoManager()->Reset();
506     studiox_project *project = GetOpenProject();
507     if (project)
508     {
509        project->SaveAs();
510     }
511 }
512 // CstudioxApp message handlers
513