Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
6.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //
  9. // Half-Life Model Viewer (c) 1999 by Mete Ciragan
  10. //
  11. // file: FileAssociation.cpp
  12. // last modified: May 04 1999, Mete Ciragan
  13. // copyright: The programs and associated files contained in this
  14. // distribution were developed by Mete Ciragan. The programs
  15. // are not in the public domain, but they are freely
  16. // distributable without licensing fees. These programs are
  17. // provided without guarantee or warrantee expressed or
  18. // implied.
  19. //
  20. // version: 1.2
  21. //
  22. // email: [email protected]
  23. // web: http://www.swissquake.ch/chumbalum-soft/
  24. //
  25. #include "FileAssociation.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <mxtk/mx.h>
  30. FileAssociation *g_FileAssociation = 0;
  31. FileAssociation::FileAssociation ()
  32. : mxWindow (0, 100, 100, 400, 210, "File Associations", mxWindow::Dialog)
  33. {
  34. cExtension = new mxChoice (this, 5, 5, 220, 22, IDC_EXTENSION);
  35. //new mxButton (this, 230, 5, 75, 22, "Add", IDC_ADD);
  36. //new mxButton (this, 310, 5, 75, 22, "Remove", IDC_REMOVE);
  37. new mxGroupBox (this, 5, 30, 380, 115, "Assocations");
  38. rbAction[0] = new mxRadioButton (this, 10, 50, 120, 22, "program", IDC_ACTION1, true);
  39. rbAction[1] = new mxRadioButton (this, 10, 72, 120, 22, "associated program", IDC_ACTION2);
  40. rbAction[2] = new mxRadioButton (this, 10, 94, 120, 22, "HLMV default", IDC_ACTION3);
  41. rbAction[3] = new mxRadioButton (this, 10, 116, 120, 22, "none", IDC_ACTION4);
  42. leProgram = new mxLineEdit (this, 130, 50, 220, 22, "", IDC_PROGRAM);
  43. leProgram->setEnabled (false);
  44. bChooseProgram = new mxButton (this, 352, 50, 22, 22, ">>", IDC_CHOOSEPROGRAM);
  45. bChooseProgram->setEnabled (false);
  46. rbAction[0]->setChecked (false);
  47. rbAction[1]->setChecked (true);
  48. new mxButton (this, 110, 155, 75, 22, "Ok", IDC_OK);
  49. new mxButton (this, 215, 155, 75, 22, "Cancel", IDC_CANCEL);
  50. initAssociations ();
  51. }
  52. FileAssociation::~FileAssociation ()
  53. {
  54. }
  55. int
  56. FileAssociation::handleEvent (mxEvent *event)
  57. {
  58. if (event->event != mxEvent::Action)
  59. return 0;
  60. switch (event->action)
  61. {
  62. case IDC_EXTENSION:
  63. {
  64. int index = cExtension->getSelectedIndex ();
  65. if (index >= 0)
  66. setAssociation (index);
  67. }
  68. break;
  69. case IDC_ACTION1:
  70. case IDC_ACTION2:
  71. case IDC_ACTION3:
  72. case IDC_ACTION4:
  73. {
  74. leProgram->setEnabled (rbAction[0]->isChecked ());
  75. bChooseProgram->setEnabled (rbAction[0]->isChecked ());
  76. int index = cExtension->getSelectedIndex ();
  77. if (index >= 0)
  78. d_associations[index].association = event->action - IDC_ACTION1;
  79. }
  80. break;
  81. case IDC_PROGRAM:
  82. {
  83. int index = cExtension->getSelectedIndex ();
  84. if (index >= 0)
  85. strcpy (d_associations[index].program, leProgram->getLabel ());
  86. }
  87. break;
  88. case IDC_CHOOSEPROGRAM:
  89. {
  90. const char *ptr = mxGetOpenFileName (this, 0, "*.exe");
  91. if (ptr)
  92. {
  93. leProgram->setLabel (ptr);
  94. int index = cExtension->getSelectedIndex ();
  95. if (index >= 0)
  96. strcpy (d_associations[index].program, leProgram->getLabel ());
  97. }
  98. }
  99. break;
  100. case IDC_OK:
  101. saveAssociations ();
  102. case IDC_CANCEL:
  103. setVisible (false);
  104. break;
  105. }
  106. return 1;
  107. }
  108. void
  109. FileAssociation::initAssociations ()
  110. {
  111. int i;
  112. cExtension->removeAll ();
  113. for (i = 0; i < 16; i++)
  114. d_associations[i].association = -1;
  115. char path[256];
  116. strcpy (path, mx::getApplicationPath ());
  117. strcat (path, "/hlmv.fa");
  118. FILE *file = fopen (path, "rt");
  119. if (!file)
  120. return;
  121. i = 0;
  122. char line[256];
  123. while (i < 16 && fgets (line, 256, file))
  124. {
  125. int j = 0;
  126. while (line[++j] != '\"');
  127. line[j] = '\0';
  128. strcpy (d_associations[i].extension, &line[1]);
  129. while (line[++j] != '\"');
  130. int k = j + 1;
  131. while (line[++j] != '\"');
  132. line[j] = '\0';
  133. strcpy (d_associations[i].program, &line[k]);
  134. d_associations[i].association = atoi (&line[++j]);
  135. cExtension->add (d_associations[i].extension);
  136. ++i;
  137. }
  138. fclose (file);
  139. setAssociation (0);
  140. }
  141. void
  142. FileAssociation::setAssociation (int index)
  143. {
  144. cExtension->select (index);
  145. leProgram->setLabel (d_associations[index].program);
  146. for (int i = 0; i < 4; i++)
  147. rbAction[i]->setChecked (i == d_associations[index].association);
  148. leProgram->setEnabled (d_associations[index].association == 0);
  149. bChooseProgram->setEnabled (d_associations[index].association == 0);
  150. // TODO: check for valid associtaion
  151. #ifdef WIN32__
  152. char path[256];
  153. strcpy (path, mx_gettemppath ());
  154. strcat (path, "/hlmvtemp.");
  155. strcat (path, d_associations[index].extension);
  156. FILE *file = fopen (path, "wb");
  157. if (file)
  158. fclose (file);
  159. int val = (int) ShellExecute ((HWND) getHandle (), "open", path, 0, 0, SW_HIDE);
  160. char str[32];
  161. sprintf (str, "%d", val);
  162. setLabel (str);
  163. rbAction[1]->setEnabled (val != 31);
  164. /*
  165. WORD dw = 0;
  166. HICON hIcon = ExtractAssociatedIcon ((HINSTANCE) GetWindowLong ((HWND) getHandle (), GWL_HINSTANCE), path, &dw);
  167. SendMessage ((HWND) getHandle (), WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) hIcon);
  168. char str[32];
  169. sprintf (str, "%d", (int) hIcon);
  170. setLabel (str);
  171. */
  172. DeleteFile (path);
  173. //DestroyIcon (hIcon);
  174. #endif
  175. rbAction[2]->setEnabled (
  176. !mx_strcasecmp (d_associations[index].extension, "mdl") ||
  177. !mx_strcasecmp (d_associations[index].extension, "tga") ||
  178. !mx_strcasecmp (d_associations[index].extension, "wav")
  179. );
  180. }
  181. void
  182. FileAssociation::saveAssociations ()
  183. {
  184. char path[256];
  185. strcpy (path, mx::getApplicationPath ());
  186. strcat (path, "/hlmv.fa");
  187. FILE *file = fopen (path, "wt");
  188. if (!file)
  189. return;
  190. for (int i = 0; i < 16; i++)
  191. {
  192. if (d_associations[i].association == -1)
  193. break;
  194. fprintf (file, "\"%s\" \"%s\" %d\n",
  195. d_associations[i].extension,
  196. d_associations[i].program,
  197. d_associations[i].association);
  198. }
  199. fclose (file);
  200. }
  201. int
  202. FileAssociation::getMode (char *extension)
  203. {
  204. for (int i = 0; i < 16; i++)
  205. {
  206. //if (!strcmp (d_associations[i].extension, mx_strlower (extension)))
  207. if (!strcmp (d_associations[i].extension, extension))
  208. return d_associations[i].association;
  209. }
  210. return -1;
  211. }
  212. char *
  213. FileAssociation::getProgram (char *extension)
  214. {
  215. for (int i = 0; i < 16; i++)
  216. {
  217. //if (!strcmp (d_associations[i].extension, mx_strlower (extension)))
  218. if (!strcmp (d_associations[i].extension, extension))
  219. return d_associations[i].program;
  220. }
  221. return 0;
  222. }