1 /*
2  *    TTTTTTTTTTTTTT  EEEEEEEEEEEEEE  OOOOOOOOOOOOOO
3  *    TTTTTTTTTTTTTT  EEEEEEEEEEEEEE  OOOOOOOOOOOOOO
4  *          TT        EE              OO          OO
5  *          TT        EE              OO          OO
6  *          TT        EE              OO          OO
7  *          TT        EEEEEEEEEE      OO          OO
8  *          TT        EEEEEEEEEE      OO          OO
9  *          TT        EE              OO          OO
10  *          TT        EE              OO          OO
11  *          TT        EE              OO          OO
12  *          TT        EEEEEEEEEEEEEE  OOOOOOOOOOOOOO
13  *          TT        EEEEEEEEEEEEEE  OOOOOOOOOOOOOO
14  *
15  *                  L'�mulateur Thomson TO8
16  *
17  *  Copyright (C) 1997-2017 Gilles F�tis, Eric Botcazou, Alexandre Pukall,
18  *                          J�r�mie Guillaume, Fran�ois Mouret
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2 of the License, or
23  *  (at your option) any later version.
24  *
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *
30  *  You should have received a copy of the GNU General Public License
31  *  along with this program; if not, write to the Free Software
32  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33  */
34 
35 /*
36  *  Module     : win/wgui/wmemo.c
37  *  Version    : 1.8.4
38  *  Cr�� par   : Eric Botcazou 28/11/2000
39  *  Modifi� par: Eric Botcazou 28/10/2003
40  *               Fran�ois Mouret 17/09/2006 28/08/2011 18/03/2012
41  *                               24/10/2012 10/05/2014
42  *
43  *  Gestion des cartouches.
44  */
45 
46 
47 #ifndef SCAN_DEPEND
48    #include <stdio.h>
49    #include <stdlib.h>
50    #include <unistd.h>
51    #include <string.h>
52 #endif
53 
54 #include "teo.h"
55 #include "std.h"
56 #include "errors.h"
57 #include "media/memo.h"
58 #include "win/gui.h"
59 
60 static int entry_max = 0;
61 static int combo_index = 0;
62 static struct STRING_LIST *path_list = NULL;
63 static struct STRING_LIST *name_list = NULL;
64 
65 
66 
67 /* update_params:
68  *  Sauve les param�tres d'une cartouche.
69  */
update_params(HWND hWnd)70 static void update_params (HWND hWnd)
71 {
72     combo_index = SendDlgItemMessage(hWnd,
73                                      IDC_MEMO7_COMBO,
74                                      CB_GETCURSEL,
75                                      0,
76                                      0);
77     if (combo_index == 0)
78     {
79         Button_Enable(GetDlgItem (hWnd, IDC_MEMO7_EJECT_BUTTON), FALSE);
80     }
81     else
82     {
83         Button_Enable(GetDlgItem (hWnd, IDC_MEMO7_EJECT_BUTTON), TRUE);
84     }
85 }
86 
87 
88 
89 /* load_memo:
90  *  Charge une cartouche.
91  */
load_memo(HWND hWnd,char * filename)92 static int load_memo (HWND hWnd, char *filename)
93 {
94     int ret = memo_Load(filename);
95 
96     if (ret < 0)
97         MessageBox(hWnd, teo_error_msg, PROGNAME_STR,
98                    MB_OK | MB_ICONINFORMATION);
99 
100     return ret;
101 }
102 
103 
104 
105 /* add_combo_entry:
106  *  Ajoute une entr�e dans le combobox si inexistante
107  *  et s�lectionne l'entr�e demand�e.
108  */
add_combo_entry(HWND hWnd,const char * name,const char * path)109 static void add_combo_entry (HWND hWnd, const char *name, const char *path)
110 {
111     int path_index = std_StringListIndex (path_list, (char *)path);
112     int name_index = std_StringListIndex (name_list, (char *)name);
113 
114     if ((path_index < 0) || (name_index != path_index))
115     {
116         path_list = std_StringListAppend (path_list, (char *)path);
117         name_list = std_StringListAppend (name_list, (char *)name);
118         SendDlgItemMessage(hWnd,
119                            IDC_MEMO7_COMBO,
120                            CB_ADDSTRING,
121                            0,
122                            (LPARAM) name);
123         SendDlgItemMessage(hWnd,
124                            IDC_MEMO7_COMBO,
125                            CB_SETCURSEL,
126                            entry_max,
127                            0);
128         entry_max++;
129     }
130     else
131     {
132         SendDlgItemMessage(hWnd, IDC_MEMO7_COMBO, CB_SETCURSEL, path_index, 0);
133         if (path_index != combo_index)
134         {
135             (void)load_memo (hWnd, std_StringListText (path_list, path_index));
136         }
137     }
138 }
139 
140 
141 
142 /* init_combo:
143  *  Remplit un combo vide.
144  */
init_combo(HWND hWnd)145 static void init_combo (HWND hWnd)
146 {
147     add_combo_entry (hWnd, is_fr?"(Aucun)":"(None)", "");
148 }
149 
150 
151 
152 /* clear_combo:
153  *  Vide un combo.
154  */
clear_combo(HWND hWnd)155 static void clear_combo (HWND hWnd)
156 {
157     wmemo_Free ();
158     memo_Eject ();
159     SendDlgItemMessage(hWnd, IDC_MEMO7_COMBO, CB_RESETCONTENT, 0, 0);
160     init_combo (hWnd);
161     teo.command = TEO_COMMAND_COLD_RESET;
162     update_params(hWnd);
163 }
164 
165 
166 
167 /* combo_changed:
168  *  Changement de s�lection du combobox.
169  */
combo_changed(HWND hWnd)170 static void combo_changed (HWND hWnd)
171 {
172     int index = SendDlgItemMessage(hWnd, IDC_MEMO7_COMBO, CB_GETCURSEL, 0, 0);
173 
174     if (index != combo_index)
175     {
176         if (index == 0)
177         {
178             memo_Eject ();
179         }
180         else
181         {
182             (void)load_memo (hWnd, std_StringListText (path_list, index));
183         }
184         teo.command = TEO_COMMAND_COLD_RESET;
185         update_params(hWnd);
186     }
187 }
188 
189 
190 
191 /* open_file:
192  *  Chargement d'un fichier cartouche.
193  */
open_file(HWND hWnd)194 static void open_file (HWND hWnd)
195 {
196    char current_file[BUFFER_SIZE]="";
197    char def_folder[] = ".\\memo";
198    OPENFILENAME ofn;
199 
200    memset(&ofn, 0, sizeof(OPENFILENAME));
201    ofn.lStructSize = sizeof(OPENFILENAME);
202    ofn.hwndOwner = hWnd;
203    ofn.lpstrFilter = is_fr?"Fichiers M7\0*.m7\0":"M7 files\0*.m7\0";
204    ofn.nFilterIndex = 1;
205    ofn.lpstrFile = current_file;
206    ofn.nMaxFile = BUFFER_SIZE;
207    ofn.lpstrTitle = is_fr?"Choisissez votre cartouche:":"Choose your cartridge:";
208    ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
209    ofn.lpstrDefExt ="m7";
210 
211     if (teo.memo.file != NULL)
212         ofn.lpstrInitialDir = teo.memo.file;
213     else
214     if (teo.default_folder != NULL)
215         ofn.lpstrInitialDir = teo.default_folder;
216     else
217     if (access(def_folder, F_OK) == 0)
218         ofn.lpstrInitialDir = def_folder;
219 
220    if (GetOpenFileName(&ofn))
221    {
222       if (load_memo (hWnd, ofn.lpstrFile) >= 0)
223       {
224           add_combo_entry (hWnd, teo.memo.label, teo.memo.file);
225           teo.default_folder = std_free (teo.default_folder);
226           teo.default_folder = std_strdup_printf ("%s", teo.memo.file);
227           (void)snprintf (current_file, MAX_PATH, "%s", teo.memo.file);
228           teo.command = TEO_COMMAND_COLD_RESET;
229           update_params(hWnd);
230       }
231    }
232 }
233 
234 
235 /* ------------------------------------------------------------------------- */
236 
237 
238 /* wmemo_Free:
239  *  Lib�re la m�moire utilis�e par la liste des cartouches.
240  */
wmemo_Free(void)241 void wmemo_Free (void)
242 {
243     std_StringListFree (name_list);
244     name_list=NULL;
245     std_StringListFree (path_list);
246     path_list=NULL;
247     entry_max=0;
248     combo_index=0;
249 }
250 
251 
252 
253 /* wmemo_TabProc:
254  *  Proc�dure pour l'onglet des cartouches
255  */
wmemo_TabProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)256 int CALLBACK wmemo_TabProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
257 {
258    static int first=1;
259    HANDLE himg;
260    struct STRING_LIST *slist;
261 
262    switch(uMsg)
263    {
264       case WM_INITDIALOG:
265          /* initialisation du s�lecteur de cartouches */
266          if (first)
267          {
268              init_combo(hWnd);
269              if (teo.memo.file != NULL)
270                  add_combo_entry (hWnd, teo.memo.label, teo.memo.file);
271              update_params (hWnd);
272              first=0;
273          }
274          /* initialisation du combo */
275          SendDlgItemMessage(hWnd, IDC_MEMO7_COMBO, CB_RESETCONTENT, 0, 0);
276          for (slist=name_list; slist!=NULL; slist=slist->next)
277              SendDlgItemMessage(hWnd, IDC_MEMO7_COMBO, CB_ADDSTRING, 0,
278                                 (LPARAM) std_BaseName(slist->str));
279          SendDlgItemMessage(hWnd, IDC_MEMO7_COMBO, CB_SETCURSEL, combo_index, 0);
280 
281          /* initialisation des images pour les boutons */
282          himg = LoadImage (prog_inst,
283                            "clearlst_ico",
284                            IMAGE_ICON,
285                            0,
286                            0,
287                            LR_DEFAULTCOLOR);
288          SendMessage(GetDlgItem(hWnd, IDC_MEMO7_EJECT_BUTTON),
289                      BM_SETIMAGE,
290                      (WPARAM)IMAGE_ICON,
291                      (LPARAM)himg );
292 
293          himg = LoadImage (prog_inst,
294                            "folder_ico",
295                            IMAGE_ICON,
296                            0,
297                            0,
298                            LR_DEFAULTCOLOR);
299          SendMessage(GetDlgItem(hWnd, IDC_MEMO7_MORE_BUTTON),
300                      BM_SETIMAGE,
301                      (WPARAM)IMAGE_ICON,
302                      (LPARAM)himg );
303 
304          /* initialisation de l'info-bulle */
305          wgui_CreateTooltip (hWnd,
306                              IDC_MEMO7_EJECT_BUTTON,
307                              is_fr?"Vider la liste des fichiers"
308                                   :"Empty the file list");
309          wgui_CreateTooltip (hWnd,
310                              IDC_MEMO7_MORE_BUTTON,
311                              is_fr?"Ouvrir un fichier cartouche"
312                                   :"Open a cartridge file");
313 
314          update_params (hWnd);
315          return TRUE;
316 
317       case WM_COMMAND:
318          switch(LOWORD(wParam))
319          {
320             case IDC_MEMO7_EJECT_BUTTON:
321                clear_combo(hWnd);
322                break;
323 
324             case IDC_MEMO7_MORE_BUTTON:
325                open_file(hWnd);
326                break;
327 
328             case IDC_MEMO7_COMBO:
329                if (HIWORD(wParam)==CBN_SELCHANGE)
330                {
331                    combo_changed(hWnd);
332                }
333                break;
334          }
335          return TRUE;
336 
337       default:
338          return FALSE;
339    }
340 }
341