home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Secrets / Secrets2.iso / Audio / WAV / MaplayP / _SETUP.1 / rfu_list.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-24  |  4.4 KB  |  210 lines

  1. #ifdef __WIN32__
  2.  
  3. #define STRICT
  4. #include <windows.h>
  5. #include "mp2win.h"
  6.  
  7. #include "rfu_list.h"
  8. #include "str_lib.h"
  9.  
  10. RecentFileList::RecentFileList(int num_entries0, int type0)
  11. {
  12.     num_entries = num_entries0;
  13.    type = type0;
  14.    valid_entries = 0;
  15.  
  16.    int i;
  17.  
  18.    name_array = new char *[num_entries];
  19.  
  20.    for(i=0; i<num_entries; i++) {
  21.       name_array[i] = new char[MAX_PATH];
  22.         lstrcpy(name_array[i], "");
  23.    }
  24.  
  25.    menu_init = new bool [num_entries];
  26.  
  27.     menu_init[0] = TRUE;
  28.    for (i=1; i<num_entries; i++)
  29.        menu_init[i] = FALSE;
  30. }
  31.  
  32. RecentFileList::~RecentFileList()
  33. {
  34.     int i;
  35.  
  36.    for (i=0;i<num_entries;i++) {
  37.        delete [] name_array[i];
  38.    }
  39.  
  40.    delete [] name_array;
  41.    delete [] menu_init;
  42. }
  43.  
  44. bool RecentFileList::registry_io(bool write_mode)
  45. {
  46.     char ret_type[256];
  47.    char temp_str1[32];
  48.    char temp_str2[32];
  49.  
  50.     HKEY ret_key1;
  51.    HKEY ret_key2;
  52.  
  53.    int i;
  54.    DWORD disp;
  55.    DWORD size;
  56.  
  57.    RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_CREATE_SUB_KEY,
  58.                   &ret_key1);
  59.  
  60.    RegCreateKeyEx(ret_key1, "Jeff's Tacos", 0, ret_type,
  61.                   REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY,
  62.                   NULL, &ret_key2, &disp);
  63.  
  64.     RegCloseKey(ret_key1);
  65.  
  66.    RegCreateKeyEx(ret_key2, "Maplay 1.2+ for Win32", 0, ret_type,
  67.                      REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY,
  68.                   NULL, &ret_key1, &disp);
  69.  
  70.     if (type == RFL_FILE_TYPE) {
  71.         RegCreateKeyEx(ret_key1, "Recent Files", 0, ret_type,
  72.                          REG_OPTION_NON_VOLATILE, KEY_WRITE,
  73.                       NULL, &ret_key2, &disp);
  74.    } else {
  75.         RegCreateKeyEx(ret_key1, "Recent Lists", 0, ret_type,
  76.                          REG_OPTION_NON_VOLATILE, KEY_WRITE,
  77.                       NULL, &ret_key2, &disp);
  78.     }
  79.  
  80.    RegCloseKey(ret_key1);
  81.  
  82.    if (write_mode || (disp == REG_CREATED_NEW_KEY)) {
  83.  
  84.        if (disp == REG_CREATED_NEW_KEY)
  85.             valid_entries = 0;
  86.  
  87.         for (i=0; i<num_entries; i++) {
  88.  
  89.             my_itoa(i, temp_str2, 10);
  90.           lstrcpy(temp_str1, "File ");
  91.           lstrcat(temp_str1, temp_str2);
  92.  
  93.            RegSetValueEx(ret_key2, temp_str1, 0, REG_SZ,
  94.                         (CONST BYTE *) name_array[i],
  95.                        MAX_PATH * sizeof(char));
  96.       }
  97.  
  98.    } else {
  99.  
  100.        valid_entries = 0;
  101.       i = 0;
  102.       bool bad_entry = FALSE;
  103.  
  104.         while ((i<num_entries) && !bad_entry)
  105.       {
  106.  
  107.          my_itoa(i, temp_str2, 10);
  108.          lstrcpy(temp_str1, "File ");
  109.          lstrcat(temp_str1, temp_str2);
  110.  
  111.            size = MAX_PATH;
  112.            RegQueryValueEx(ret_key2, temp_str1, 0, NULL,
  113.                              (LPBYTE) name_array[i], &size);
  114.  
  115.             bad_entry = (name_array[i][0] == '\0');
  116.  
  117.          if (!bad_entry)
  118.              valid_entries++;
  119.  
  120.          i++;
  121.       }
  122.    }
  123.  
  124.    RegCloseKey(ret_key2);
  125.    return(TRUE);
  126. }
  127.  
  128. bool RecentFileList::read_from_registry()
  129. {
  130.     return(registry_io(FALSE));
  131. }
  132.  
  133. bool RecentFileList::write_to_registry()
  134. {
  135.     return(registry_io(TRUE));
  136. }
  137.  
  138. char *RecentFileList::get_name_at_pos(char *name, int pos)
  139. {
  140.     char *ret_val = name;
  141.  
  142.    lstrcpy(ret_val, name_array[pos]);
  143.  
  144.    return(ret_val);
  145. }
  146.  
  147. void RecentFileList::push(char *filename)
  148. {
  149.    int i, j;
  150.  
  151.    // Check if the filename is already in the list
  152.     for (i=0; i<valid_entries; i++) {
  153.        if (b_strcmpi(filename, name_array[i])) {
  154.             for (j=i; j>0; j--) {
  155.               lstrcpy(name_array[j], name_array[j-1]);
  156.          }
  157.          lstrcpy(name_array[0], filename);
  158.           return;
  159.       }
  160.    }
  161.  
  162.    // Push the stack down
  163.    if (valid_entries < num_entries)
  164.        valid_entries++;
  165.  
  166.     for (i=valid_entries-1; i>0; i--) {
  167.        lstrcpy(name_array[i], name_array[i-1]);
  168.    }
  169.  
  170.    lstrcpy(name_array[0], filename);
  171.    return;
  172. }
  173.  
  174. HMENU RecentFileList::build_menu(HMENU menu, HWND hWnd)
  175. {
  176.  
  177.    char menu_entry[MAX_PATH + 8];
  178.    char num_str[8];
  179.    int  message_base;
  180.    int i;
  181.  
  182.    message_base = (type == RFL_FILE_TYPE) ? CM_FILERECENTF1 :
  183.                        CM_FILERECENTL1;
  184.  
  185.    for (i=0; i<num_entries; i++) {
  186.        if (menu_init[i]) {
  187.            RemoveMenu(menu, message_base + i, MF_BYCOMMAND);
  188.       }
  189.    }
  190.  
  191.       for (i=0; i<valid_entries; i++) {
  192.  
  193.          lstrcpy(menu_entry, "&");
  194.       my_itoa(i+1, num_str, 10);
  195.       lstrcat(menu_entry, num_str);
  196.       lstrcat(menu_entry, " ");
  197.       lstrcat(menu_entry, name_array[i]);
  198.  
  199.       AppendMenu(menu, MF_ENABLED | MF_STRING,
  200.                  message_base + i, menu_entry);
  201.  
  202.       menu_init[i] = TRUE;
  203.    }
  204.  
  205.    DrawMenuBar(hWnd);
  206.  
  207.     return(menu);
  208. }
  209. #endif // __WIN32__
  210.