Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <commdlg.h>
  2.  
  3. #include <kolibri.h>
  4. #include <kos_heap.h>
  5. #include "kos_cdlg.h"
  6.  
  7. using namespace Kolibri;
  8.  
  9. extern HINSTANCE hInstance;
  10.  
  11. struct TThreadDataStruct
  12. {
  13.         void *user;
  14.         void *stack_begin;
  15.         TWindowData *win_data;
  16.         HWND hwnd;
  17.         int flag;
  18.         unsigned int win_time, me_time;
  19.         void *picture;
  20.         unsigned int picture_width, picture_height;
  21.         void *keys;
  22.         unsigned int bmp_data_length;
  23.         unsigned int *bmp_data;
  24.         unsigned int mouse_state;
  25. };
  26.  
  27. struct TOpenFileData
  28. {
  29.         int state;
  30.         char name[1];
  31. };
  32.  
  33. namespace Kolibri
  34. {
  35.         TOpenFileStruct::TOpenFileStruct() : data(0) {}
  36.  
  37.         TOpenFileStruct::~TOpenFileStruct()
  38.         {
  39.                 if (data) {delete[] (char*)data; data = 0;}
  40.         }
  41.  
  42.         void OpenFileInit(TOpenFileStruct &ofs) {ofs.data = 0;}
  43.  
  44.         void OpenFileDelete(TOpenFileStruct &ofs)
  45.         {
  46.                 if (ofs.data) {delete[] (char*)ofs.data; ofs.data = 0;}
  47.         }
  48.  
  49.         bool OpenFileDialog(TOpenFileStruct &ofs)
  50.         {
  51.                 char CustomFilter[300], *name;
  52.                 int size;
  53.                 CustomFilter[0] = 0; CustomFilter[1] = 0;
  54.                 if (!OpenFileSetState(ofs, 0)) return false;
  55.                 OPENFILENAME ofn = {sizeof(OPENFILENAME), ((TThreadDataStruct*)GetThreadData())->hwnd,
  56.                         hInstance, "All files (*.*)\0*.*\0",
  57.                         CustomFilter, sizeof(CustomFilter)-1, 1, NULL, 0, NULL, 0, NULL, NULL,
  58.                         OFN_HIDEREADONLY | OFN_EXPLORER, 0, 0, "", 0, NULL, 0};
  59.                 size = 0;
  60.                 if (ofs.data) size = strlen(((TOpenFileData*)ofs.data)->name) + 1;
  61.                 if (size < 10000) size = 10000;
  62.                 name = new char[size + 1];
  63.                 if (!name) return false;
  64.                 if (ofs.data) strcpy(name, ((TOpenFileData*)ofs.data)->name);
  65.                 else name[0] = 0;
  66.                 ofn.lpstrFile = &name[0]; ofn.nMaxFile = size;
  67.                 size = GetOpenFileName(&ofn) == TRUE;
  68.                 if (OpenFileSetName(ofs, name))
  69.                 {
  70.                         ((TOpenFileData*)ofs.data)->state = (size ? 2 : 1);
  71.                 }
  72.                 else size = 0;
  73.                 delete[] name;
  74.                 return (bool)size;
  75.         }
  76.  
  77.         int OpenFileGetState(const TOpenFileStruct &ofs)
  78.         {
  79.                 return ofs.data ? ((TOpenFileData*)ofs.data)->state : 0;
  80.         }
  81.  
  82.         bool OpenFileSetState(TOpenFileStruct &ofs, int state)
  83.         {
  84.                 if (!ofs.data || !((TOpenFileData*)ofs.data)->state) return !state;
  85.                 if (((TOpenFileData*)ofs.data)->state == state) return true;
  86.                 if (state < 0) return false;
  87.                 ((TOpenFileData*)ofs.data)->state = state;
  88.                 return true;
  89.         }
  90.  
  91.         char *OpenFileGetName(const TOpenFileStruct &ofs)
  92.         {
  93.                 if (!ofs.data) return 0;
  94.                 else return ((TOpenFileData*)ofs.data)->name;
  95.         }
  96.  
  97.         bool OpenFileSetName(TOpenFileStruct &ofs, char *name)
  98.         {
  99.                 if (!ofs.data && !name) return true;
  100.                 int size = (unsigned int)(((TOpenFileData*)0)->name) + 1;
  101.                 int state = 0;
  102.                 if (name) size += strlen(name);
  103.                 if (ofs.data)
  104.                 {
  105.                         state = ((TOpenFileData*)ofs.data)->state;
  106.                         delete[] (char*)ofs.data;
  107.                 }
  108.                 ofs.data = (unsigned int)(new char[size]);
  109.                 if (!ofs.data) return false;
  110.                 ((TOpenFileData*)ofs.data)->state = state;
  111.                 if (name) strcpy(((TOpenFileData*)ofs.data)->name, name);
  112.                 else ((TOpenFileData*)ofs.data)->name[0] = 0;
  113.                 return true;
  114.         }
  115. }