~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/Win32Dialogs/NWin32CustomDialog.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-02 03:28:11 UTC
  • Revision ID: neil.patel@canonical.com-20100902032811-i2m18tfb6pkasnvt
Remove Win EOL chars

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
 
22
22
 
23
 
#include "NKernel.h"
24
 
 
25
 
NAMESPACE_BEGIN
26
 
 
27
 
static enum eFileDialogType {
28
 
    FILE_OPEN_DIALOG,
29
 
    FILE_SAVE_DIALOG,
30
 
};
31
 
 
32
 
ColorDialogOption::ColorDialogOption()
33
 
: m_ReturnColor(0, 0, 0, 0)
34
 
{
35
 
    for(t_u32 i = 0; i < INL_COLOR_DIALOG_CUSTOM_COLOR; i++)
36
 
    {
37
 
        m_CustomColors[i] = (COLORREF) INL_RGB(255, 255, 255);
38
 
    }
39
 
}
40
 
 
41
 
ColorDialogOption::~ColorDialogOption()
42
 
{
43
 
 
44
 
}
45
 
 
46
 
void ColorDialogOption::SetCustomColor(t_u32 index, t_u32 RGBColor)
47
 
{
48
 
    nuxAssert(index < INL_COLOR_DIALOG_CUSTOM_COLOR);
49
 
    m_CustomColors[index] = (COLORREF) RGBColor;
50
 
}
51
 
 
52
 
void ColorDialogOption::SetCustomColor(t_u32 index, BYTE R, BYTE G, BYTE B)
53
 
{
54
 
    nuxAssert(index < INL_COLOR_DIALOG_CUSTOM_COLOR);
55
 
    m_CustomColors[index] = (COLORREF) INL_RGB(R, G, B);
56
 
}
57
 
 
58
 
 
59
 
#define MAX_FILE_CHARACTERS 2*260
60
 
 
61
 
FileDialogOption::FileDialogOption()
62
 
: NumFilters(0)
63
 
, FormattedFilter(0)
64
 
{
65
 
    DialogTitle = TEXT("\0");
66
 
    InitialDirectory = TEXT("\0");
67
 
}
68
 
 
69
 
FileDialogOption::~FileDialogOption()
70
 
{
71
 
 
72
 
}
73
 
 
74
 
void FileDialogOption::AddFilter(const TCHAR* Description, const TCHAR* Filter)
75
 
{
76
 
    nuxAssert(Description);
77
 
    nuxAssert(Filter);
78
 
 
79
 
    FilterDesc.push_back(NString(Description));
80
 
    Filters.push_back(NString(Filter));
81
 
    NumFilters++;
82
 
}
83
 
 
84
 
void FileDialogOption::RemoveFilter()
85
 
{
86
 
    FilterDesc.clear();
87
 
    Filters.clear();
88
 
    NumFilters = 0;
89
 
    if(FormattedFilter)
90
 
    {
91
 
        delete [] FormattedFilter;
92
 
        FormattedFilter = 0;
93
 
    }
94
 
}
95
 
 
96
 
TCHAR* FileDialogOption::GetFormatedFilter()
97
 
{
98
 
    t_size size = 0;
99
 
    for(t_u32 i = 0; i < NumFilters; i++)
100
 
    {
101
 
        size += FilterDesc[i].Length() + Filters[i].Length() + 2; // + 2 for for the NULL char at the end of each string
102
 
    }
103
 
    size += 1;  // + 1 for the final NULL char. The total string is terminated by two NULL char
104
 
 
105
 
    if(FormattedFilter)
106
 
        delete [] FormattedFilter;
107
 
    FormattedFilter = new TCHAR[size];
108
 
    t_size l = 0;
109
 
    for(t_u32 i = 0; i < NumFilters; i++)
110
 
    {
111
 
        Memcpy((void*)(FormattedFilter + l), *FilterDesc[i], FilterDesc[i].Length());
112
 
        l += FilterDesc[i].Length();
113
 
        FormattedFilter[l] = 0;
114
 
        l++;
115
 
        Memcpy((void*)(FormattedFilter + l), *Filters[i], Filters[i].Length());
116
 
        l += Filters[i].Length();
117
 
        FormattedFilter[l] = 0;
118
 
        l++;
119
 
    }
120
 
    FormattedFilter[l] = 0;
121
 
    l++;
122
 
    nuxAssert(size == l);
123
 
 
124
 
    return FormattedFilter;
125
 
}
126
 
 
127
 
void FileDialogOption::SetDialogTitle(const TCHAR* Title)
128
 
{
129
 
    DialogTitle = Title;
130
 
}
131
 
void FileDialogOption::SetInitialDirectory(const TCHAR* Directory)
132
 
{
133
 
    InitialDirectory = Directory;
134
 
}
135
 
 
136
 
bool Win32ColorDialog(HWND hWnd, ColorDialogOption& cdo)
137
 
{
138
 
    CHOOSECOLOR ColorOptions;
139
 
 
140
 
    Memset(&ColorOptions, 0, sizeof(CHOOSECOLOR));
141
 
    ColorOptions.lStructSize = sizeof(CHOOSECOLOR);
142
 
    ColorOptions.hwndOwner = hWnd;
143
 
    ColorOptions.hInstance = 0;
144
 
    ColorOptions.lpCustColors = cdo.m_CustomColors;
145
 
    ColorOptions.Flags = CC_ANYCOLOR | CC_FULLOPEN;
146
 
 
147
 
    if( ChooseColor(&ColorOptions) )
148
 
    {
149
 
        cdo.m_ReturnColor = Color(GetRValue(ColorOptions.rgbResult),GetGValue(ColorOptions.rgbResult),GetBValue(ColorOptions.rgbResult));
150
 
        return TRUE;
151
 
    };
152
 
 
153
 
    return FALSE;
154
 
}
155
 
 
156
 
 
157
 
static bool _Win32FileDialog(HWND hWnd, eFileDialogType DialogType, FileDialogOption& fdo)
158
 
{
159
 
    nuxAssert((DialogType == FILE_OPEN_DIALOG) || (DialogType == FILE_SAVE_DIALOG));
160
 
 
161
 
    OPENFILENAME SaveFileOption;
162
 
 
163
 
    TCHAR DrivePathFileName[MAX_FILE_CHARACTERS];       // buffer for OpenFileName.lpstrFile
164
 
    TCHAR FileName[MAX_FILE_CHARACTERS];                // buffer for OpenFileName.lpstrFileTitle
165
 
 
166
 
    DrivePathFileName[0] = 0;
167
 
    Memset(&SaveFileOption, 0, sizeof(OPENFILENAME));
168
 
    SaveFileOption.lStructSize = sizeof(OPENFILENAME); 
169
 
    // (See remark at the end of this file!)
170
 
    SaveFileOption.hwndOwner = hWnd;
171
 
 
172
 
    SaveFileOption.lpstrFilter = fdo.GetFormatedFilter();
173
 
    SaveFileOption.nFilterIndex = 0;
174
 
 
175
 
    // On return, this buffer contains the drive designator, path, file name, and extension of the selected file.
176
 
    SaveFileOption.lpstrFile = DrivePathFileName;
177
 
    SaveFileOption.nMaxFile = MAX_FILE_CHARACTERS;
178
 
 
179
 
    // On return, this buffer receives the file name and extension (without path information) of the selected file.
180
 
    SaveFileOption.lpstrFileTitle = FileName;
181
 
    SaveFileOption.nMaxFileTitle = MAX_FILE_CHARACTERS;
182
 
 
183
 
    SaveFileOption.lpstrInitialDir = fdo.GetInitialDirectory();
184
 
 
185
 
    SaveFileOption.lpstrTitle = fdo.GetDialogTitle();
186
 
 
187
 
    SaveFileOption.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
188
 
    SaveFileOption.lpstrDefExt = TEXT("");
189
 
 
190
 
 
191
 
    if(DialogType == FILE_OPEN_DIALOG)
192
 
    {
193
 
        if(::GetOpenFileName(&SaveFileOption))
194
 
        {
195
 
            fdo.ReturnFileName = NFileName(SaveFileOption.lpstrFile);
196
 
            return TRUE;
197
 
        }
198
 
    }
199
 
 
200
 
    if(DialogType == FILE_SAVE_DIALOG)
201
 
    {
202
 
        if(::GetSaveFileName(&SaveFileOption))
203
 
        {
204
 
            fdo.ReturnFileName = NFileName(SaveFileOption.lpstrFile);
205
 
            return TRUE;
206
 
        }
207
 
    }
208
 
 
209
 
    t_u32 error = ::CommDlgExtendedError();
210
 
    return FALSE;
211
 
}
212
 
 
213
 
bool Win32SaveFileDialog(HWND hWnd, FileDialogOption& fdo)
214
 
{
215
 
    return (bool)_Win32FileDialog(hWnd, FILE_SAVE_DIALOG, fdo);
216
 
}
217
 
 
218
 
bool Win32OpenFileDialog(HWND hWnd, FileDialogOption& fdo)
219
 
{
220
 
    return (bool)_Win32FileDialog(hWnd, FILE_OPEN_DIALOG, fdo);
221
 
}
222
 
 
223
 
NAMESPACE_END
224
 
 
 
23
#include "NKernel.h"
 
24
 
 
25
NAMESPACE_BEGIN
 
26
 
 
27
static enum eFileDialogType {
 
28
    FILE_OPEN_DIALOG,
 
29
    FILE_SAVE_DIALOG,
 
30
};
 
31
 
 
32
ColorDialogOption::ColorDialogOption()
 
33
: m_ReturnColor(0, 0, 0, 0)
 
34
{
 
35
    for(t_u32 i = 0; i < INL_COLOR_DIALOG_CUSTOM_COLOR; i++)
 
36
    {
 
37
        m_CustomColors[i] = (COLORREF) INL_RGB(255, 255, 255);
 
38
    }
 
39
}
 
40
 
 
41
ColorDialogOption::~ColorDialogOption()
 
42
{
 
43
 
 
44
}
 
45
 
 
46
void ColorDialogOption::SetCustomColor(t_u32 index, t_u32 RGBColor)
 
47
{
 
48
    nuxAssert(index < INL_COLOR_DIALOG_CUSTOM_COLOR);
 
49
    m_CustomColors[index] = (COLORREF) RGBColor;
 
50
}
 
51
 
 
52
void ColorDialogOption::SetCustomColor(t_u32 index, BYTE R, BYTE G, BYTE B)
 
53
{
 
54
    nuxAssert(index < INL_COLOR_DIALOG_CUSTOM_COLOR);
 
55
    m_CustomColors[index] = (COLORREF) INL_RGB(R, G, B);
 
56
}
 
57
 
 
58
 
 
59
#define MAX_FILE_CHARACTERS 2*260
 
60
 
 
61
FileDialogOption::FileDialogOption()
 
62
: NumFilters(0)
 
63
, FormattedFilter(0)
 
64
{
 
65
    DialogTitle = TEXT("\0");
 
66
    InitialDirectory = TEXT("\0");
 
67
}
 
68
 
 
69
FileDialogOption::~FileDialogOption()
 
70
{
 
71
 
 
72
}
 
73
 
 
74
void FileDialogOption::AddFilter(const TCHAR* Description, const TCHAR* Filter)
 
75
{
 
76
    nuxAssert(Description);
 
77
    nuxAssert(Filter);
 
78
 
 
79
    FilterDesc.push_back(NString(Description));
 
80
    Filters.push_back(NString(Filter));
 
81
    NumFilters++;
 
82
}
 
83
 
 
84
void FileDialogOption::RemoveFilter()
 
85
{
 
86
    FilterDesc.clear();
 
87
    Filters.clear();
 
88
    NumFilters = 0;
 
89
    if(FormattedFilter)
 
90
    {
 
91
        delete [] FormattedFilter;
 
92
        FormattedFilter = 0;
 
93
    }
 
94
}
 
95
 
 
96
TCHAR* FileDialogOption::GetFormatedFilter()
 
97
{
 
98
    t_size size = 0;
 
99
    for(t_u32 i = 0; i < NumFilters; i++)
 
100
    {
 
101
        size += FilterDesc[i].Length() + Filters[i].Length() + 2; // + 2 for for the NULL char at the end of each string
 
102
    }
 
103
    size += 1;  // + 1 for the final NULL char. The total string is terminated by two NULL char
 
104
 
 
105
    if(FormattedFilter)
 
106
        delete [] FormattedFilter;
 
107
    FormattedFilter = new TCHAR[size];
 
108
    t_size l = 0;
 
109
    for(t_u32 i = 0; i < NumFilters; i++)
 
110
    {
 
111
        Memcpy((void*)(FormattedFilter + l), *FilterDesc[i], FilterDesc[i].Length());
 
112
        l += FilterDesc[i].Length();
 
113
        FormattedFilter[l] = 0;
 
114
        l++;
 
115
        Memcpy((void*)(FormattedFilter + l), *Filters[i], Filters[i].Length());
 
116
        l += Filters[i].Length();
 
117
        FormattedFilter[l] = 0;
 
118
        l++;
 
119
    }
 
120
    FormattedFilter[l] = 0;
 
121
    l++;
 
122
    nuxAssert(size == l);
 
123
 
 
124
    return FormattedFilter;
 
125
}
 
126
 
 
127
void FileDialogOption::SetDialogTitle(const TCHAR* Title)
 
128
{
 
129
    DialogTitle = Title;
 
130
}
 
131
void FileDialogOption::SetInitialDirectory(const TCHAR* Directory)
 
132
{
 
133
    InitialDirectory = Directory;
 
134
}
 
135
 
 
136
bool Win32ColorDialog(HWND hWnd, ColorDialogOption& cdo)
 
137
{
 
138
    CHOOSECOLOR ColorOptions;
 
139
 
 
140
    Memset(&ColorOptions, 0, sizeof(CHOOSECOLOR));
 
141
    ColorOptions.lStructSize = sizeof(CHOOSECOLOR);
 
142
    ColorOptions.hwndOwner = hWnd;
 
143
    ColorOptions.hInstance = 0;
 
144
    ColorOptions.lpCustColors = cdo.m_CustomColors;
 
145
    ColorOptions.Flags = CC_ANYCOLOR | CC_FULLOPEN;
 
146
 
 
147
    if( ChooseColor(&ColorOptions) )
 
148
    {
 
149
        cdo.m_ReturnColor = Color(GetRValue(ColorOptions.rgbResult),GetGValue(ColorOptions.rgbResult),GetBValue(ColorOptions.rgbResult));
 
150
        return TRUE;
 
151
    };
 
152
 
 
153
    return FALSE;
 
154
}
 
155
 
 
156
 
 
157
static bool _Win32FileDialog(HWND hWnd, eFileDialogType DialogType, FileDialogOption& fdo)
 
158
{
 
159
    nuxAssert((DialogType == FILE_OPEN_DIALOG) || (DialogType == FILE_SAVE_DIALOG));
 
160
 
 
161
    OPENFILENAME SaveFileOption;
 
162
 
 
163
    TCHAR DrivePathFileName[MAX_FILE_CHARACTERS];       // buffer for OpenFileName.lpstrFile
 
164
    TCHAR FileName[MAX_FILE_CHARACTERS];                // buffer for OpenFileName.lpstrFileTitle
 
165
 
 
166
    DrivePathFileName[0] = 0;
 
167
    Memset(&SaveFileOption, 0, sizeof(OPENFILENAME));
 
168
    SaveFileOption.lStructSize = sizeof(OPENFILENAME); 
 
169
    // (See remark at the end of this file!)
 
170
    SaveFileOption.hwndOwner = hWnd;
 
171
 
 
172
    SaveFileOption.lpstrFilter = fdo.GetFormatedFilter();
 
173
    SaveFileOption.nFilterIndex = 0;
 
174
 
 
175
    // On return, this buffer contains the drive designator, path, file name, and extension of the selected file.
 
176
    SaveFileOption.lpstrFile = DrivePathFileName;
 
177
    SaveFileOption.nMaxFile = MAX_FILE_CHARACTERS;
 
178
 
 
179
    // On return, this buffer receives the file name and extension (without path information) of the selected file.
 
180
    SaveFileOption.lpstrFileTitle = FileName;
 
181
    SaveFileOption.nMaxFileTitle = MAX_FILE_CHARACTERS;
 
182
 
 
183
    SaveFileOption.lpstrInitialDir = fdo.GetInitialDirectory();
 
184
 
 
185
    SaveFileOption.lpstrTitle = fdo.GetDialogTitle();
 
186
 
 
187
    SaveFileOption.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
 
188
    SaveFileOption.lpstrDefExt = TEXT("");
 
189
 
 
190
 
 
191
    if(DialogType == FILE_OPEN_DIALOG)
 
192
    {
 
193
        if(::GetOpenFileName(&SaveFileOption))
 
194
        {
 
195
            fdo.ReturnFileName = NFileName(SaveFileOption.lpstrFile);
 
196
            return TRUE;
 
197
        }
 
198
    }
 
199
 
 
200
    if(DialogType == FILE_SAVE_DIALOG)
 
201
    {
 
202
        if(::GetSaveFileName(&SaveFileOption))
 
203
        {
 
204
            fdo.ReturnFileName = NFileName(SaveFileOption.lpstrFile);
 
205
            return TRUE;
 
206
        }
 
207
    }
 
208
 
 
209
    t_u32 error = ::CommDlgExtendedError();
 
210
    return FALSE;
 
211
}
 
212
 
 
213
bool Win32SaveFileDialog(HWND hWnd, FileDialogOption& fdo)
 
214
{
 
215
    return (bool)_Win32FileDialog(hWnd, FILE_SAVE_DIALOG, fdo);
 
216
}
 
217
 
 
218
bool Win32OpenFileDialog(HWND hWnd, FileDialogOption& fdo)
 
219
{
 
220
    return (bool)_Win32FileDialog(hWnd, FILE_OPEN_DIALOG, fdo);
 
221
}
 
222
 
 
223
NAMESPACE_END
 
224