#include "stdafx.h"
#include "UI.h"
#include "../Common/ImportExport.h"
#define MAX_LOADSTRING 100
#define IMPORT_FROM_BUTTON 1000
#define EXPORT_TO_BUTTON 1001
#define EXECUTE_BUTTON 1002
#define IMPORT_FROM_EDITBOX 1003
#define EXPORT_TO_EDITBOX 1004
#define EXECUTE_STATUS 1005
HINSTANCE hInst;
HWND ghWnd = NULL;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
char gszInputFile[MAX_PATH];
char gszOutputFile[MAX_PATH];
int gWriteFileFormat = -1;
extern KFbxSdkManager *gSdkManager;
ATOM UIRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void CreateUIControls(HWND hWndParent);
void GetInputFileName(HWND hWndParent);
void GetOutputFileName(HWND hWndParent);
void ExecuteImportExport(HWND hWndParent);
void UI_Printf(const char* msg, ...);
bool ExtExist(const char * filepath, const char * ext);
bool FileExist(const char *filepath);
int APIENTRY _tWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow
)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
HACCEL hAccelTable;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_UI, szWindowClass, MAX_LOADSTRING);
UIRegisterClass(hInstance);
ZeroMemory(gszInputFile, sizeof(gszInputFile) );
ZeroMemory(gszOutputFile, sizeof(gszOutputFile) );
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_UI));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM UIRegisterClass(
HINSTANCE hInstance
)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_UI));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush( GetSysColor(COLOR_3DFACE) );
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_UI);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_UI));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(
HINSTANCE hInstance,
int nCmdShow
)
{
hInst = hInstance;
ghWnd = CreateWindow(
szWindowClass,
"FBX SDK UI Import/Export example",
WS_OVERLAPPED | WS_SYSMENU,
400,
200,
700,
455,
NULL,
NULL,
hInstance,
NULL
);
if (!ghWnd)
{
return FALSE;
}
ShowWindow(ghWnd, nCmdShow);
UpdateWindow(ghWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
int wmId, wmEvent;
switch (message)
{
case WM_CREATE:
{
CreateUIControls(hWnd);
InitializeSdkManager();
}
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IMPORT_FROM_BUTTON:
GetInputFileName(hWnd);
break;
case EXPORT_TO_BUTTON:
GetOutputFileName(hWnd);
break;
case EXECUTE_BUTTON:
ExecuteImportExport(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_DESTROY:
DestroySdkObjects(gSdkManager);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR CALLBACK About(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
HINSTANCE hinst = (HINSTANCE)(LONG_PTR)GetWindowLongPtr(hDlg, GWLP_HINSTANCE);
HICON hIconFBX = (HICON) LoadImage( hinst, MAKEINTRESOURCE(IDI_UI), IMAGE_ICON, 16, 16, LR_DEFAULTSIZE);
::SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconFBX);
}
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void CreateUIControls(
HWND hWndParent
)
{
DWORD dwStyle = WS_CHILD | WS_VISIBLE;
CreateWindowEx(
0,
"BUTTON",
"Import from ...",
dwStyle,
10,
10,
130,
30,
hWndParent,
(HMENU) IMPORT_FROM_BUTTON,
hInst,
NULL
);
CreateWindowEx(
0,
"BUTTON",
"Export to ...",
dwStyle,
10,
50,
130,
30,
hWndParent,
(HMENU) EXPORT_TO_BUTTON,
hInst,
NULL
);
CreateWindowEx(
0,
"BUTTON",
"Execute",
dwStyle,
10,
90,
130,
30,
hWndParent,
(HMENU) EXECUTE_BUTTON,
hInst,
NULL
);
CreateWindowEx(
WS_EX_STATICEDGE,
"EDIT",
" <- select a file to import",
dwStyle|ES_AUTOHSCROLL,
150,
15,
530,
20,
hWndParent,
(HMENU) IMPORT_FROM_EDITBOX,
hInst,
NULL
);
CreateWindowEx(
WS_EX_STATICEDGE,
"EDIT",
" <- select a filename to export",
dwStyle|ES_AUTOHSCROLL,
150,
55,
530,
20,
hWndParent,
(HMENU) EXPORT_TO_EDITBOX,
hInst,
NULL
);
DWORD dwStyle_Edit = ES_AUTOHSCROLL | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL;
CreateWindowEx(
WS_EX_STATICEDGE,
"EDIT",
"",
dwStyle | dwStyle_Edit,
150,
90,
530,
300,
hWndParent,
(HMENU) EXECUTE_STATUS,
hInst,
NULL
);
}
void GetInputFileName(
HWND hWndParent
)
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
char szFile[MAX_PATH];
ZeroMemory(szFile, sizeof(szFile));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWndParent;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = "Select the file to import from ... (use the file type filter)";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
const char *filter = GetReaderOFNFilters();
ofn.lpstrFilter = filter;
if(GetOpenFileName(&ofn) == false)
{
delete filter;
return;
}
delete filter;
SetWindowText( GetDlgItem(hWndParent, IMPORT_FROM_EDITBOX), szFile );
strcpy(gszInputFile, szFile);
}
void GetOutputFileName(
HWND hWndParent
)
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
char szFile[MAX_PATH];
ZeroMemory(szFile, sizeof(szFile));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWndParent;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile)/ sizeof(*szFile);
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = "Select the file to export to ... (use the file type filter)";
ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT;
const char *filter = GetWriterSFNFilters();
ofn.lpstrFilter = filter;
if(GetSaveFileName(&ofn) == false)
{
delete filter;
return;
}
delete filter;
gWriteFileFormat = ofn.nFilterIndex - 1;
const char * ext = GetFileFormatExt( gWriteFileFormat );
if(ExtExist(szFile, ext) == false)
{
strcat(szFile, ext);
}
delete ext;
SetWindowText( GetDlgItem(hWndParent, EXPORT_TO_EDITBOX), szFile);
strcpy(gszOutputFile, szFile);
}
void ExecuteImportExport(
HWND hWndParent
)
{
SetWindowText( GetDlgItem(ghWnd, EXECUTE_STATUS) , "");
if(strlen(gszInputFile) == 0)
{
UI_Printf("Error: No import file name selected.");
return;
}
if(strlen(gszOutputFile) == 0)
{
UI_Printf("Error: No export file name selected.");
return;
}
GetWindowText( GetDlgItem(hWndParent, IMPORT_FROM_EDITBOX), gszInputFile , MAX_PATH);
GetWindowText( GetDlgItem(hWndParent, EXPORT_TO_EDITBOX), gszOutputFile, MAX_PATH);
if(FileExist(gszInputFile) == false)
{
UI_Printf("Error: Import file not found.");
return;
}
HCURSOR oldCursor = SetCursor( LoadCursor(NULL, IDC_WAIT) );
ImportExport(gszInputFile, gszOutputFile, gWriteFileFormat);
SetCursor(oldCursor);
}
void UI_Printf(
const char* pMsg,
...
)
{
if(ghWnd == NULL) return;
char msg[2048];
va_list Arguments;
va_start( Arguments, pMsg);
vsprintf( msg, pMsg, Arguments );
va_end( Arguments );
HWND hWndStatus = GetDlgItem(ghWnd, EXECUTE_STATUS);
int msglen = int(strlen(msg));
if(msglen < 1) return;
msglen = msglen + int(strlen("\r\n")) + 1;
char* msgrn = (char*)GlobalAlloc(GPTR, msglen);
memset(msgrn, 0, msglen);
strcat(msgrn, msg);
strcat(msgrn, "\r\n");
int len = GetWindowTextLength(hWndStatus);
if(len > 0)
{
char* buf = (char*)GlobalAlloc(GPTR, len + 1);
GetDlgItemText(ghWnd, EXECUTE_STATUS, buf, len + 1);
msglen = int(strlen(msgrn)) + len + 1;
char* s = (char*)GlobalAlloc(GPTR, msglen);
memset(s, 0, msglen);
strcat(s, buf);
strcat(s, msgrn);
SetWindowText( hWndStatus , s);
GlobalFree((HANDLE)buf);
GlobalFree((HANDLE)s);
}
else
{
SetWindowText( hWndStatus , msgrn);
}
GlobalFree((HANDLE)msgrn);
SendMessage( hWndStatus, (UINT) EM_SCROLL, SB_BOTTOM, (LPARAM) 0 );
}
bool ExtExist(
const char * filepath,
const char * ext
)
{
int iExtLen = (int)strlen(ext);
int ifpLen = (int)strlen(filepath);
if( ifpLen < iExtLen) return false;
int x = ifpLen - iExtLen;
for(int i=0; i < iExtLen; i++)
{
if(filepath[x] != ext[i] ) return false;
x++;
}
return true;
}
bool FileExist(
const char *filepath
)
{
FILE *fp = fopen( filepath, "r" );
if(fp == NULL) return false;
fclose(fp);
return true;
}