You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.1KB

  1. #include "osdialog.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <shlobj.h>
  6. int osdialog_message(osdialog_message_level level, osdialog_message_buttons buttons, const char *message) {
  7. UINT type = 0;
  8. switch (level) {
  9. default:
  10. case OSDIALOG_INFO: type |= MB_ICONINFORMATION; break;
  11. case OSDIALOG_WARNING: type |= MB_ICONWARNING; break;
  12. case OSDIALOG_ERROR: type |= MB_ICONERROR; break;
  13. }
  14. switch (buttons) {
  15. default:
  16. case OSDIALOG_OK: type |= MB_OK; break;
  17. case OSDIALOG_OK_CANCEL: type |= MB_OKCANCEL; break;
  18. case OSDIALOG_YES_NO: type |= MB_YESNO; break;
  19. }
  20. int result = MessageBox(NULL, message, "", type);
  21. switch (result) {
  22. case IDOK:
  23. case IDYES:
  24. return 1;
  25. default:
  26. return 0;
  27. }
  28. }
  29. char *osdialog_file(osdialog_file_action action, const char *path, const char *filename, osdialog_filters *filters) {
  30. if (action == OSDIALOG_OPEN_DIR) {
  31. // open directory dialog
  32. TCHAR szDir[MAX_PATH] = "";
  33. BROWSEINFO bInfo;
  34. ZeroMemory(&bInfo, sizeof(bInfo));
  35. bInfo.hwndOwner = NULL;
  36. bInfo.pidlRoot = NULL;
  37. bInfo.pszDisplayName = szDir;
  38. bInfo.lpszTitle = NULL;
  39. bInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
  40. bInfo.lpfn = NULL;
  41. bInfo.lParam = 0;
  42. bInfo.iImage = -1;
  43. LPITEMIDLIST lpItem = SHBrowseForFolder(&bInfo);
  44. if (lpItem) {
  45. SHGetPathFromIDList(lpItem, szDir);
  46. return strdup(szDir);
  47. }
  48. else {
  49. return NULL;
  50. }
  51. }
  52. else {
  53. // open or save file dialog
  54. OPENFILENAME ofn;
  55. ZeroMemory(&ofn, sizeof(ofn));
  56. char strFile[_MAX_PATH] = "";
  57. if (filename)
  58. snprintf(strFile, sizeof(strFile), "%s", filename);
  59. char *strInitialDir = path ? strdup(path) : NULL;
  60. ofn.lStructSize = sizeof(ofn);
  61. ofn.lpstrFile = strFile;
  62. ofn.nMaxFile = sizeof(strFile);
  63. ofn.lpstrInitialDir = strInitialDir;
  64. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
  65. if (filters) {
  66. char fBuf[4096];
  67. int fLen = 0;
  68. for (; filters; filters = filters->next) {
  69. fLen += snprintf(fBuf + fLen, sizeof(fBuf) - fLen, "%s", filters->name);
  70. fLen++;
  71. for (osdialog_filter_patterns *patterns = filters->patterns; patterns; patterns = patterns->next) {
  72. fLen += snprintf(fBuf + fLen, sizeof(fBuf) - fLen, "*.%s", patterns->pattern);
  73. if (patterns->next)
  74. fLen += snprintf(fBuf + fLen, sizeof(fBuf) - fLen, ";");
  75. }
  76. fLen++;
  77. }
  78. ofn.lpstrFilter = fBuf;
  79. ofn.nFilterIndex = 1;
  80. }
  81. BOOL success;
  82. if (action == OSDIALOG_OPEN)
  83. success = GetOpenFileName(&ofn);
  84. else
  85. success = GetSaveFileName(&ofn);
  86. if (strInitialDir)
  87. free(strInitialDir);
  88. return success ? strdup(strFile) : NULL;
  89. }
  90. }
  91. int osdialog_color_picker(osdialog_color *color, int opacity) {
  92. if (!color)
  93. return 0;
  94. CHOOSECOLOR cc;
  95. ZeroMemory(&cc, sizeof(cc));
  96. COLORREF c = RGB(color->r, color->g, color->b);
  97. static COLORREF acrCustClr[16];
  98. cc.lStructSize = sizeof(cc);
  99. cc.lpCustColors = (LPDWORD) acrCustClr;
  100. cc.rgbResult = c;
  101. cc.Flags = CC_FULLOPEN | CC_ANYCOLOR | CC_RGBINIT;
  102. if (ChooseColor(&cc)) {
  103. color->r = GetRValue(cc.rgbResult);
  104. color->g = GetGValue(cc.rgbResult);
  105. color->b = GetBValue(cc.rgbResult);
  106. color->a = 255;
  107. return 1;
  108. }
  109. return 0;
  110. }