29#include <shobjidl_core.h>
42 const size_t n0 = wstrPath.find_last_of(L
"\\");
44 std::wstring wstrResult = (n0 == std::string::npos)? wstrPath:
45 wstrPath.substr(n0 + 1, wstrPath.size() - n0);
47 const size_t n1 = wstrResult.find_last_of(L
".");
49 if(n1 != std::string::npos)
50 wstrResult = wstrResult.substr(0, n1);
63void MinDragRect(HWND hwnd, WPARAM wParam, RECT* pRect,
int w,
int h){
67 GetClientRect(hwnd, &cr);
68 GetWindowRect(hwnd, &wr);
72 const int bw = (wr.right - wr.left) - (cr.right - cr.left);
73 const int bh = (wr.bottom - wr.top) - (cr.bottom - cr.top);
77 const int dw = max(w, pRect->right - pRect->left - bw) + bw;
78 const int dh = max(h, pRect->bottom - pRect->top - bh) + bh;
84 pRect->left = pRect->right - dw;
88 pRect->right = pRect->left + dw;
92 pRect->top = pRect->bottom - dh;
96 pRect->bottom = pRect->top + dh;
100 pRect->top = pRect->bottom - dh;
101 pRect->right = pRect->left + dw;
105 pRect->top = pRect->bottom - dh;
106 pRect->left = pRect->right - dw;
109 case WMSZ_BOTTOMRIGHT:
110 pRect->bottom = pRect->top + dh;
111 pRect->right = pRect->left + dw;
114 case WMSZ_BOTTOMLEFT:
115 pRect->bottom = pRect->top + dh;
116 pRect->left = pRect->right - dw;
124#pragma region Initialization
132 const char appname[] =
"Sorting Network Verify and Draw";
134 WNDCLASSEX wndClass = {0};
136 wndClass.cbSize =
sizeof(WNDCLASSEX);
137 wndClass.style = CS_HREDRAW | CS_VREDRAW;
138 wndClass.lpfnWndProc =
WndProc;
139 wndClass.cbClsExtra = 0;
140 wndClass.cbWndExtra = 0;
141 wndClass.hInstance = hInst;
142 wndClass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
143 wndClass.hCursor = LoadCursor(
nullptr, IDC_ARROW);
144 wndClass.hbrBackground =
nullptr;
145 wndClass.lpszMenuName =
nullptr;
146 wndClass.lpszClassName = appname;
147 wndClass.hIconSm = 0;
149 RegisterClassEx(&wndClass);
151 const DWORD dwStyle = WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX |
152 WS_THICKFRAME | WS_SYSMENU;
153 const DWORD dwStyleEx = WS_EX_APPWINDOW | WS_EX_DLGMODALFRAME;
159 r.left = 0; r.right = w;
160 r.top = 0; r.bottom = h + GetSystemMetrics(SM_CYMENU);
161 AdjustWindowRectEx(&r, dwStyle, FALSE, dwStyleEx);
163 const HWND hwnd = CreateWindowEx(dwStyleEx, appname, appname, dwStyle,
164 CW_USEDEFAULT, CW_USEDEFAULT, r.right - r.left, r.bottom - r.top,
165 nullptr,
nullptr, hInst,
nullptr);
167 ShowWindow(hwnd, nShow);
175 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
176 ULONG_PTR gdiplusToken;
177 Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput,
nullptr);
181#pragma endregion Initialization
198 Gdiplus::ImageCodecInfo* pCodecInfo =
nullptr;
199 if(FAILED(Gdiplus::GetImageEncodersSize(&num, &n)))
return E_FAIL;
200 if(n == 0)
return E_FAIL;
202 pCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(n));
203 if(pCodecInfo ==
nullptr)
return E_FAIL;
204 if(FAILED(GetImageEncoders(num, n, pCodecInfo)))
return E_FAIL;
206 for(UINT j=0; j<num && hr!=S_OK; j++)
207 if(wcscmp(pCodecInfo[j].MimeType, format) == 0){
208 *pClsid = pCodecInfo[j].Clsid;
226 std::wstring& wstrName)
228 COMDLG_FILTERSPEC filetypes[1];
229 std::wstring wstrTitle = L
"Export ";
230 std::wstring wstrDefaultExtension;
236 filetypes[0] = {L
"PNG Files", L
"*.png"};
237 wstrTitle += L
"PNG"; wstrDefaultExtension = L
"png";
241 filetypes[0] = {L
"SVG Files", L
"*.svg"};
242 wstrTitle += L
"SVG"; wstrDefaultExtension = L
"svg";
246 filetypes[0] = {L
"TeX Files", L
"*.tex"};
247 wstrTitle += L
"TeX"; wstrDefaultExtension = L
"tex";
251 wstrTitle += L
" Image";
255 CComPtr<IFileSaveDialog> pDlg;
256 CComPtr<IShellItem> pItem;
257 LPWSTR pwsz =
nullptr;
261 HRESULT hr = pDlg.CoCreateInstance(__uuidof(FileSaveDialog));
264 pDlg->SetFileTypes(_countof(filetypes), filetypes);
265 pDlg->SetTitle(wstrTitle.c_str());
266 pDlg->SetFileName(wstrName.c_str());
267 pDlg->SetDefaultExtension(wstrDefaultExtension.c_str());
269 hr = pDlg->Show(hwnd);
272 hr = pDlg->GetResult(&pItem);
275 hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pwsz);
279 case eExport::Png: hr = pNet->
ExportToPNG(pwsz);
break;
280 case eExport::Svg: hr = pNet->
ExportToSVG(pwsz);
break;
281 case eExport::TeX: hr = pNet->
ExportToTex(pwsz);
break;
304 std::wstring& wstrName)
306 COMDLG_FILTERSPEC filetypes[] = {
307 {L
"TXT Files", L
"*.txt"}
312 CComPtr<IFileOpenDialog> pDlg;
313 CComPtr<IShellItem> pItem;
314 LPWSTR pwsz =
nullptr;
316 hr = pDlg.CoCreateInstance(__uuidof(FileOpenDialog));
319 pDlg->SetFileTypes(_countof(filetypes), filetypes);
320 pDlg->SetTitle(L
"Open Comparator Network");
321 pDlg->SetDefaultExtension(L
"txt");
323 hr = pDlg->Show(hwnd);
326 hr = pDlg->GetResult(&pItem);
329 hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pwsz);
333 hr = pNet->
Read(pwsz)? S_OK: E_FAIL;
344#pragma endregion Save
349#pragma region Create menu functions
355 HMENU hMenu = CreateMenu();
360 AppendMenuW(hMenu, MF_SEPARATOR, 0,
nullptr);
363 AppendMenuW(hParent, MF_POPUP, (UINT_PTR)hMenu, L
"&File");
375 HMENU hMenu = CreateMenu();
381 AppendMenuW(hParent, MF_POPUP, (UINT_PTR)hMenu, L
"Export");
388 HMENU hMenu = CreateMenu();
393 AppendMenuW(hMenu, MF_SEPARATOR, 0,
nullptr);
396 AppendMenuW(hMenu, MF_SEPARATOR, 0,
nullptr);
399 AppendMenuW(hParent, MF_POPUP, (UINT_PTR)hMenu, L
"Generate");
406 HMENU hMenu = CreateMenu();
411 AppendMenuW(hParent, MF_POPUP, (UINT_PTR)hMenu, L
"&View");
418 HMENU hMenu = CreateMenu();
420 AppendMenuW(hMenu, MF_STRING,
IDM_HELP_HELP, L
"Display help...");
422 AppendMenuW(hParent, MF_POPUP, (UINT_PTR)hMenu, L
"&Help");
425#pragma endregion Create menu functions
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Window procedure.
Interface for the sorting network CSortingNetwork.
void MinDragRect(HWND hwnd, WPARAM wParam, RECT *pRect, int w, int h)
Enforce minimum drag rectangle.
void CreateFileMenu(HMENU hParent)
Create File menu.
HRESULT Load(HWND hwnd, CComparatorNetwork *pNet, std::wstring &wstrName)
Load comparator network.
void CreateExportMenu(HMENU hParent)
Create File menu.
std::wstring FileNameBase(const std::wstring &wstrPath)
HRESULT ExportImage(const eExport t, HWND hwnd, CRenderableComparatorNet *pNet, std::wstring &wstrName)
Export.
void CreateGenerateMenu(HMENU hParent)
Create Generate menu.
HRESULT GetEncoderClsid(const WCHAR *format, CLSID *pClsid)
Get encoder CLSID.
ULONG_PTR InitGDIPlus()
Initialize GDI+.
void CreateHelpMenu(HMENU hParent)
Create Help menu.
void InitWindow(HINSTANCE hInst, INT nShow, WNDPROC WndProc)
Initialize window.
void CreateViewMenu(HMENU hParent)
Create View menu.
Interface for some helpful Windows-specific functions.
#define IDM_GENERATE_MINBUBBLE
Menu id for Generate min-bubblesort.
#define IDM_GENERATE_MAXBUBBLE
Menu id for Generate max-bubblesort.
#define IDM_FILE_EXPORT_PNG
Menu id for Export PNG.
#define IDM_FILE_OPEN
Menu id for Generate.
#define IDM_GENERATE_BUBBLE
Menu id for Generate min-bubblesort.
#define IDM_GENERATE_BITONIC
Menu id for Generate bitonic.
#define IDM_FILE_VERIFY
Menu id for Verify.
#define IDM_FILE_QUIT
Menu id for Quit.
#define IDM_FILE_EXPORT_TEX
Menu id for Export TeX.
#define IDM_HELP_HELP
Menu id for display help.
#define IDM_VIEW_HORIZONTAL
Menu id for horizontal view.
#define IDM_FILE_EXPORT_SVG
Menu id for Export SVG.
#define IDM_GENERATE_PAIRWISE
Menu id for Generate pairwise.
#define IDM_VIEW_VERTICAL
Menu id for vertical view.
#define IDM_GENERATE_ODDEVEN
Menu id for Generate odd-even.
#define IDM_HELP_ABOUT
Menu id for display About info.
virtual bool Read(LPWSTR)
Read from file.
Renderable comparator network.
HRESULT ExportToSVG(LPWSTR)
Export in SVG format.
HRESULT ExportToTex(LPWSTR)
Export in TeX format.
HRESULT ExportToPNG(LPWSTR)
Export in PNG format.