Previous 15994 Revisions Next

r10022 Thursday 13th January, 2011 at 11:33:53 UTC by Miodrag Milanović
Fixed compiling tools, needed files moved to wimgtool folder.
[src/mess/tools/imgtool/windows]opcntrl.c* opcntrl.h* wimgtool.mak winutils.c* winutils.h*

src/mess/tools/imgtool/windows/winutils.c
r0r10022
1//============================================================
2//
3//  winutils.c - Generic Win32 utility code
4//
5//============================================================
6
7// Needed for better file dialog
8#ifdef _WIN32_WINNT
9#undef _WIN32_WINNT
10#endif // _WIN32_WINNT
11#define _WIN32_WINNT 0x500
12
13// standard windows headers
14#define WIN32_LEAN_AND_MEAN
15#include <windows.h>
16#include <commdlg.h>
17#include <tchar.h>
18#include <stdlib.h>
19
20// MESS headers
21#include "winutils.h"
22#include "strconv.h"
23
24// stupid hack; not sure why this is needed
25#ifdef const
26#undef const
27#endif // const
28
29
30
31//============================================================
32//  win_get_file_name_dialog - sanitize all of the ugliness
33//  in invoking GetOpenFileName() and GetSaveFileName()
34//============================================================
35
36BOOL win_get_file_name_dialog(win_open_file_name *ofn)
37{
38   BOOL result = FALSE;
39   BOOL dialog_result;
40   OSVERSIONINFO vers;
41   OPENFILENAME os_ofn;
42   DWORD os_ofn_size;
43   LPTSTR t_filter = NULL;
44   LPTSTR t_file = NULL;
45   DWORD t_file_size = 0;
46   LPTSTR t_initial_directory = NULL;
47   LPTSTR buffer;
48   char *utf8_file;
49   int i;
50
51   // determine the version of Windows
52   memset(&vers, 0, sizeof(vers));
53   vers.dwOSVersionInfoSize = sizeof(vers);
54   GetVersionEx(&vers);
55
56   // based on the version of Windows, determine the correct struct size
57   if (vers.dwMajorVersion >= 5)
58   {
59      os_ofn_size = sizeof(os_ofn);
60   }
61   else
62   {
63#ifdef __GNUC__
64      os_ofn_size = 76;   // MinGW does not define OPENFILENAME_NT4
65#else
66      os_ofn_size = sizeof(OPENFILENAME_NT4);
67#endif
68   }
69
70   // do we have to translate the filter?
71   if (ofn->filter != NULL)
72   {
73      buffer = tstring_from_utf8(ofn->filter);
74      if (!buffer)
75         goto done;
76
77      // convert a pipe-char delimited string into a NUL delimited string
78      t_filter = (LPTSTR) alloca((_tcslen(buffer) + 2) * sizeof(*t_filter));
79      for (i = 0; buffer[i] != '\0'; i++)
80         t_filter[i] = (buffer[i] != '|') ? buffer[i] : '\0';
81      t_filter[i++] = '\0';
82      t_filter[i++] = '\0';
83      osd_free(buffer);
84   }
85
86   // do we need to translate the file parameter?
87   if (ofn->filename != NULL)
88   {
89      buffer = tstring_from_utf8(ofn->filename);
90      if (!buffer)
91         goto done;
92
93      t_file_size = MAX(_tcslen(buffer) + 1, MAX_PATH);
94      t_file = (LPTSTR) alloca(t_file_size * sizeof(*t_file));
95      _tcscpy(t_file, buffer);
96      osd_free(buffer);
97   }
98
99   // do we need to translate the initial directory?
100   if (ofn->initial_directory != NULL)
101   {
102      t_initial_directory = tstring_from_utf8(ofn->initial_directory);
103      if (t_initial_directory == NULL)
104         goto done;
105   }
106
107   // translate our custom structure to a native Win32 structure
108   memset(&os_ofn, 0, sizeof(os_ofn));
109   os_ofn.lStructSize = os_ofn_size;
110   os_ofn.hwndOwner = ofn->owner;
111   os_ofn.hInstance = ofn->instance;
112   os_ofn.lpstrFilter = t_filter;
113   os_ofn.nFilterIndex = ofn->filter_index;
114   os_ofn.lpstrFile = t_file;
115   os_ofn.nMaxFile = t_file_size;
116   os_ofn.lpstrInitialDir = t_initial_directory;
117   os_ofn.Flags = ofn->flags;
118   os_ofn.lCustData = ofn->custom_data;
119   os_ofn.lpfnHook = ofn->hook;
120   os_ofn.lpTemplateName = ofn->template_name;
121
122   // invoke the correct Win32 call
123   switch(ofn->type)
124   {
125      case WIN_FILE_DIALOG_OPEN:
126         dialog_result = GetOpenFileName(&os_ofn);
127         break;
128
129      case WIN_FILE_DIALOG_SAVE:
130         dialog_result = GetSaveFileName(&os_ofn);
131         break;
132
133      default:
134         // should not reach here
135         dialog_result = FALSE;
136         break;
137   }
138
139   // copy data out
140   ofn->filter_index = os_ofn.nFilterIndex;
141   ofn->flags = os_ofn.Flags;
142
143   // copy file back out into passed structure
144   if (t_file != NULL)
145   {
146      utf8_file = utf8_from_tstring(t_file);
147      if (!utf8_file)
148         goto done;
149
150      snprintf(ofn->filename, ARRAY_LENGTH(ofn->filename), "%s", utf8_file);
151      osd_free(utf8_file);
152   }
153
154   // we've completed the process
155   result = dialog_result;
156
157done:
158   if (t_initial_directory != NULL)
159      osd_free(t_initial_directory);
160   return result;
161}
162
163
164
165//============================================================
166//  win_scroll_window
167//============================================================
168
169void win_scroll_window(HWND window, WPARAM wparam, int scroll_bar, int scroll_delta_line)
170{
171   SCROLLINFO si;
172   int scroll_pos;
173
174   // retrieve vital info about the scroll bar
175   memset(&si, 0, sizeof(si));
176   si.cbSize = sizeof(si);
177   si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
178   GetScrollInfo(window, scroll_bar, &si);
179
180   scroll_pos = si.nPos;
181
182   // change scroll_pos in accordance with this message
183   switch(LOWORD(wparam))
184   {
185      case SB_BOTTOM:
186         scroll_pos = si.nMax;
187         break;
188      case SB_LINEDOWN:
189         scroll_pos += scroll_delta_line;
190         break;
191      case SB_LINEUP:
192         scroll_pos -= scroll_delta_line;
193         break;
194      case SB_PAGEDOWN:
195         scroll_pos += scroll_delta_line;
196         break;
197      case SB_PAGEUP:
198         scroll_pos -= scroll_delta_line;
199         break;
200      case SB_THUMBPOSITION:
201      case SB_THUMBTRACK:
202         scroll_pos = HIWORD(wparam);
203         break;
204      case SB_TOP:
205         scroll_pos = si.nMin;
206         break;
207   }
208
209   // max out the scroll bar value
210   if (scroll_pos < si.nMin)
211      scroll_pos = si.nMin;
212   else if (scroll_pos > (si.nMax - si.nPage))
213      scroll_pos = (si.nMax - si.nPage);
214
215   // if the value changed, set the scroll position
216   if (scroll_pos != si.nPos)
217   {
218      SetScrollPos(window, scroll_bar, scroll_pos, TRUE);
219      ScrollWindowEx(window, 0, si.nPos - scroll_pos, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE);
220   }
221}
222
223
224
225//============================================================
226//  win_append_menu_utf8
227//============================================================
228
229BOOL win_append_menu_utf8(HMENU menu, UINT flags, UINT_PTR id, const char *item)
230{
231   BOOL result;
232   const TCHAR *t_item = (const TCHAR*)item;
233   TCHAR *t_str = NULL;
234
235   // only convert string when it's no a bitmap
236   if (!(flags & MF_BITMAP) && item) {
237      t_str = tstring_from_utf8(item);
238      t_item = t_str;
239   }
240
241   result = AppendMenu(menu, flags, id, t_item);
242
243   if (t_str)
244      osd_free(t_str);
245
246   return result;
247}
src/mess/tools/imgtool/windows/opcntrl.c
r0r10022
1//============================================================
2//
3//  opcntrl.c - Code for handling option resolutions in
4//  Win32 controls
5//
6//  This code was factored out of menu.c when Windows Imgtool
7//  started to have dynamic controls
8//
9//============================================================
10
11#include <windows.h>
12#include <commctrl.h>
13#include <stdio.h>
14#include <tchar.h>
15#include <assert.h>
16
17#include "opcntrl.h"
18#include "strconv.h"
19#include "winutf8.h"
20
21
22static const TCHAR guide_prop[] = TEXT("opcntrl_guide");
23static const TCHAR spec_prop[] = TEXT("opcntrl_optspec");
24static const TCHAR value_prop[] = TEXT("opcntrl_value");
25
26
27
28static int get_option_count(const option_guide *guide,
29   const char *optspec)
30{
31   struct OptionRange ranges[128];
32   int count = 0, i;
33
34   option_resolution_listranges(optspec, guide->parameter,
35      ranges, ARRAY_LENGTH(ranges));
36
37   for (i = 0; ranges[i].min >= 0; i++)
38      count += ranges[i].max - ranges[i].min + 1;
39   return count;
40}
41
42
43
44static BOOL prepare_combobox(HWND control, const option_guide *guide,
45   const char *optspec)
46{
47   struct OptionRange ranges[128];
48   int default_value, default_index, current_index, option_count;
49   int i, j, k;
50   BOOL has_option;
51   TCHAR buf1[256];
52   TCHAR buf2[256];
53   LPTSTR tempstr;
54
55   SendMessage(control, CB_GETLBTEXT, SendMessage(control, CB_GETCURSEL, 0, 0), (LPARAM) buf1);
56   SendMessage(control, CB_RESETCONTENT, 0, 0);
57   has_option = guide && optspec;
58
59   if (has_option)
60   {
61      if ((guide->option_type != OPTIONTYPE_INT) && (guide->option_type != OPTIONTYPE_ENUM_BEGIN))
62         goto unexpected;
63
64      option_resolution_listranges(optspec, guide->parameter,
65         ranges, ARRAY_LENGTH(ranges));
66      option_resolution_getdefault(optspec, guide->parameter, &default_value);
67
68      option_count = 0;
69      default_index = -1;
70      current_index = -1;
71
72      for (i = 0; ranges[i].min >= 0; i++)
73      {
74         for (j = ranges[i].min; j <= ranges[i].max; j++)
75         {
76            if (guide->option_type == OPTIONTYPE_INT)
77            {
78               _sntprintf(buf2, ARRAY_LENGTH(buf2), TEXT("%d"), j);
79               SendMessage(control, CB_ADDSTRING, 0, (LPARAM) buf2);
80            }
81            else if (guide->option_type == OPTIONTYPE_ENUM_BEGIN)
82            {
83               for (k = 1; guide[k].option_type == OPTIONTYPE_ENUM_VALUE; k++)
84               {
85                  if (guide[k].parameter == j)
86                     break;
87               }
88               if (guide[k].option_type != OPTIONTYPE_ENUM_VALUE)
89                  goto unexpected;
90               tempstr = tstring_from_utf8(guide[k].display_name);
91               SendMessage(control, CB_ADDSTRING, 0, (LPARAM) tempstr);
92               osd_free(tempstr);
93            }
94            else
95               goto unexpected;
96
97            SendMessage(control, CB_SETITEMDATA, option_count, j);
98
99            if (j == default_value)
100               default_index = option_count;
101            if (!_tcscmp(buf1, buf2))
102               current_index = option_count;
103            option_count++;
104         }
105      }
106
107      // if there is only one option, it is effectively disabled
108      if (option_count <= 1)
109         has_option = FALSE;
110
111      if (current_index >= 0)
112         SendMessage(control, CB_SETCURSEL, current_index, 0);
113      else if (default_index >= 0)
114         SendMessage(control, CB_SETCURSEL, default_index, 0);
115   }
116   else
117   {
118      // this item is non applicable
119      SendMessage(control, CB_ADDSTRING, 0, (LPARAM) TEXT("N/A"));
120      SendMessage(control, CB_SETCURSEL, 0, 0);
121   }
122   EnableWindow(control, has_option);
123   return TRUE;
124
125unexpected:
126   assert(FALSE);
127   return FALSE;
128}
129
130
131
132static BOOL check_combobox(HWND control)
133{
134   return TRUE;
135}
136
137
138
139static BOOL prepare_editbox(HWND control, const option_guide *guide,
140   const char *optspec)
141{
142   optreserr_t err = OPTIONRESOLUTION_ERROR_SUCCESS;
143   char buf[32];
144   int val, has_option, option_count;
145
146   has_option = guide && optspec;
147   buf[0] = '\0';
148
149   if (has_option)
150   {
151      switch(guide->option_type)
152      {
153         case OPTIONTYPE_STRING:
154            break;
155
156         case OPTIONTYPE_INT:
157            err = option_resolution_getdefault(optspec, guide->parameter, &val);
158            if (err)
159               goto done;
160            _snprintf(buf, ARRAY_LENGTH(buf), "%d", val);
161            break;
162
163         default:
164            err = OPTIONRESOLTUION_ERROR_INTERNAL;
165            goto done;
166      }
167   }
168
169   if (has_option)
170   {
171      option_count = get_option_count(guide, optspec);
172      if (option_count <= 1)
173         has_option = FALSE;
174   }
175
176done:
177   assert(err != OPTIONRESOLTUION_ERROR_INTERNAL);
178   win_set_window_text_utf8(control, buf);
179   EnableWindow(control, !err && has_option);
180   return err == OPTIONRESOLUTION_ERROR_SUCCESS;
181}
182
183
184
185static BOOL check_editbox(HWND control)
186{
187   char buf[256];
188   const option_guide *guide;
189   const char *optspec;
190   optreserr_t err;
191   HANDLE h;
192   void *val;
193   int i_val;
194
195
196   guide = (const option_guide *) GetProp(control, guide_prop);
197   optspec = (const char *) GetProp(control, spec_prop);
198
199   win_get_window_text_utf8(control, buf, ARRAY_LENGTH(buf));
200
201   switch(guide->option_type)
202   {
203      case OPTIONTYPE_INT:
204         i_val = atoi(buf);
205         err = option_resolution_isvalidvalue(optspec, guide->parameter, i_val);
206         if (err)
207         {
208            h = GetProp(control, value_prop);
209            val = (void*) h;
210            _snprintf(buf, ARRAY_LENGTH(buf), "%d", val);
211            win_set_window_text_utf8(control, buf);
212         }
213         else
214         {
215            SetProp(control, value_prop, (HANDLE) i_val);
216         }
217         break;
218
219      default:
220         err = OPTIONRESOLTUION_ERROR_INTERNAL;
221         goto done;
222   }
223
224done:
225   assert(err != OPTIONRESOLTUION_ERROR_INTERNAL);
226   return (err != OPTIONRESOLUTION_ERROR_SUCCESS);
227}
228
229
230
231BOOL win_prepare_option_control(HWND control, const option_guide *guide,
232   const char *optspec)
233{
234   BOOL rc = FALSE;
235   TCHAR class_name[32];
236
237   SetProp(control, guide_prop, (HANDLE) guide);
238   SetProp(control, spec_prop, (HANDLE) optspec);
239   GetClassName(control, class_name, sizeof(class_name)
240      / sizeof(class_name[0]));
241
242   if (!_tcsicmp(class_name, TEXT("ComboBox")))
243      rc = prepare_combobox(control, guide, optspec);
244   else if (!_tcsicmp(class_name, TEXT("Edit")))
245      rc = prepare_editbox(control, guide, optspec);
246
247   return rc;
248}
249
250
251
252BOOL win_check_option_control(HWND control)
253{
254   BOOL rc = FALSE;
255   TCHAR class_name[32];
256
257   GetClassName(control, class_name, sizeof(class_name)
258      / sizeof(class_name[0]));
259
260   if (!_tcsicmp(class_name, TEXT("ComboBox")))
261      rc = check_combobox(control);
262   else if (!_tcsicmp(class_name, TEXT("Edit")))
263      rc = check_editbox(control);
264
265   return rc;
266}
267
268
269
270BOOL win_adjust_option_control(HWND control, int delta)
271{
272   const option_guide *guide;
273   const char *optspec;
274   struct OptionRange ranges[128];
275   char buf[64];
276   int val, original_val, i;
277   BOOL changed = FALSE;
278
279   guide = (const option_guide *) GetProp(control, guide_prop);
280   optspec = (const char *) GetProp(control, spec_prop);
281
282   assert(guide->option_type == OPTIONTYPE_INT);
283
284   if (delta == 0)
285      return TRUE;
286
287   option_resolution_listranges(optspec, guide->parameter,
288      ranges, ARRAY_LENGTH(ranges));
289
290   win_get_window_text_utf8(control, buf, ARRAY_LENGTH(buf));
291   original_val = atoi(buf);
292   val = original_val + delta;
293
294   for (i = 0; ranges[i].min >= 0; i++)
295   {
296      if (ranges[i].min > val)
297      {
298         if ((delta < 0) && (i > 0))
299            val = ranges[i-1].max;
300         else
301            val = ranges[i].min;
302         changed = TRUE;
303         break;
304      }
305   }
306   if (!changed && (i > 0) && (ranges[i-1].max < val))
307      val = ranges[i-1].max;
308
309   if (val != original_val)
310   {
311      _snprintf(buf, ARRAY_LENGTH(buf), "%d", val);
312      win_set_window_text_utf8(control, buf);
313   }
314   return TRUE;
315}
316
317
318
319optreserr_t win_add_resolution_parameter(HWND control, option_resolution *resolution)
320{
321   const option_guide *guide;
322   char buf[256];
323   optreserr_t err;
324   const char *text;
325   const char *old_text;
326   int i;
327
328   if (!win_get_window_text_utf8(control, buf, ARRAY_LENGTH(buf)))
329   {
330      err = OPTIONRESOLTUION_ERROR_INTERNAL;
331      return err;
332   }
333   text = buf;
334
335   guide = (const option_guide *) GetProp(control, guide_prop);
336   if (!guide)
337   {
338      err = OPTIONRESOLTUION_ERROR_INTERNAL;
339      return err;
340   }
341
342   if (guide->option_type == OPTIONTYPE_ENUM_BEGIN)
343   {
344      /* need to convert display name to identifier */
345      old_text = text;
346      text = NULL;
347
348      for (i = 1; guide[i].option_type == OPTIONTYPE_ENUM_VALUE; i++)
349      {
350         if (!strcmp(guide[i].display_name, old_text))
351         {
352            text = guide[i].identifier;
353            break;
354         }
355      }
356   }
357
358   if (text)
359   {
360      err = option_resolution_add_param(resolution, guide->identifier, text);
361      if (err)
362         return err;
363   }
364
365   err = OPTIONRESOLUTION_ERROR_SUCCESS;
366
367   return err;
368}
369
370
371
src/mess/tools/imgtool/windows/winutils.h
r0r10022
1//============================================================
2//
3//  winutils.h - Generic Win32 utility code in MESS
4//
5//============================================================
6
7#ifndef WINUTILS_H
8#define WINUTILS_H
9
10#include <windows.h>
11#include "osdcore.h"
12
13
14//============================================================
15//  FILE DIALOG WRAPPERS
16//============================================================
17
18enum _win_file_dialog_type
19{
20   WIN_FILE_DIALOG_OPEN = 1,
21   WIN_FILE_DIALOG_SAVE
22};
23typedef enum _win_file_dialog_type win_file_dialog_type;
24
25typedef struct _win_open_file_name win_open_file_name;
26struct _win_open_file_name
27{
28   win_file_dialog_type   type;            // type of file dialog
29   HWND               owner;            // owner of the dialog
30   HINSTANCE            instance;         // instance
31   const char *         filter;            // pipe char ("|") delimited strings
32   DWORD               filter_index;      // index into filter
33   char               filename[MAX_PATH];   // filename buffer
34   const char *         initial_directory;   // initial directory for dialog
35   DWORD               flags;            // standard flags
36   LPARAM               custom_data;      // custom data for dialog hook
37   LPOFNHOOKPROC         hook;            // custom dialog hook
38   LPCTSTR               template_name;      // custom dialog template
39};
40
41BOOL win_get_file_name_dialog(win_open_file_name *ofn);
42
43
44
45//============================================================
46//  MISC
47//============================================================
48
49void win_scroll_window(HWND window, WPARAM wparam, int scroll_bar, int scroll_delta_line);
50
51BOOL win_append_menu_utf8(HMENU menu, UINT flags, UINT_PTR id, const char *item);
52
53#endif // WINUTILS_H
src/mess/tools/imgtool/windows/opcntrl.h
r0r10022
1//============================================================
2//
3//  opcntrl.h - Code for handling option resolutions in
4//  Win32 controls
5//
6//============================================================
7
8#ifndef OPCNTRL_H
9#define OPCNTRL_H
10
11#include <windows.h>
12#include "opresolv.h"
13
14BOOL win_prepare_option_control(HWND control, const option_guide *guide,
15   const char *optspec);
16BOOL win_check_option_control(HWND control);
17BOOL win_adjust_option_control(HWND control, int delta);
18optreserr_t win_add_resolution_parameter(HWND control, option_resolution *resolution);
19
20#endif // OPCNTRL_H
src/mess/tools/imgtool/windows/wimgtool.mak
r10021r10022
2525WIMGTOOL_OBJS = \
2626   $(MESS_TOOLS)/pile.o                  \
2727   $(MESS_TOOLS)/toolerr.o                  \
28   $(MESSOBJ)/osd/windows/opcntrl.o         \
29   $(MESSOBJ)/osd/windows/winutils.o         \
28   $(MESS_TOOLS)/imgtool/windows/opcntrl.o      \
29   $(MESS_TOOLS)/imgtool/windows/winutils.o   \
3030   $(MESS_TOOLS)/imgtool/stubs.o            \
3131   $(MESS_TOOLS)/imgtool/windows/wmain.o      \
3232   $(MESS_TOOLS)/imgtool/windows/wimgtool.o   \

Previous 15994 Revisions Next


© 1998-2018 The MESS Team