PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/common/Lgi/GMru.cpp

#
C++ | 410 lines | 266 code | 49 blank | 95 comment | 40 complexity | 6be428a18159e85954879d3c0f8da09d MD5 | raw file
Possible License(s): LGPL-2.1, Unlicense
  1. #include <stdio.h>
  2. #include "Lgi.h"
  3. #include "GMru.h"
  4. #include "GVariant.h"
  5. ////////////////////////////////////////////////////////////////////
  6. #define M_MRU_BASE (M_USER+0x3500)
  7. class GMruPrivate
  8. {
  9. public:
  10. int _Size;
  11. List<char> _Items;
  12. GSubMenu *_Parent;
  13. GFileType *_SelectedType;
  14. GFileSelect _Select;
  15. GMruPrivate()
  16. {
  17. _Parent = 0;
  18. _Size = 10;
  19. _SelectedType = 0;
  20. }
  21. ~GMruPrivate()
  22. {
  23. _Items.DeleteArrays();
  24. }
  25. };
  26. ////////////////////////////////////////////////////////////////////
  27. GMru::GMru()
  28. {
  29. d = new GMruPrivate;
  30. }
  31. GMru::~GMru()
  32. {
  33. DeleteObj(d);
  34. }
  35. void GMru::GetFileTypes(GFileSelect *Dlg, bool Write)
  36. {
  37. Dlg->Type("All Files", LGI_ALL_FILES);
  38. }
  39. char *GMru::_GetCurFile()
  40. {
  41. return d->_Items.First();
  42. }
  43. GFileType *GMru::GetSelectedType()
  44. {
  45. return d->_SelectedType;
  46. }
  47. bool GMru::_OpenFile(char *File, bool ReadOnly)
  48. {
  49. bool Status = OpenFile(File, ReadOnly);
  50. if (Status)
  51. {
  52. AddFile(File, true);
  53. }
  54. else
  55. {
  56. RemoveFile(File);
  57. }
  58. return Status;
  59. }
  60. bool GMru::_SaveFile(char *FileName)
  61. {
  62. bool Status = false;
  63. if (FileName)
  64. {
  65. char File[MAX_PATH];
  66. strsafecpy(File, FileName, sizeof(File));
  67. if (!FileExists(File) &&
  68. GetSelectedType() &&
  69. GetSelectedType()->Extension())
  70. {
  71. // extract extension
  72. char Ext[64];
  73. char *s = strchr(GetSelectedType()->Extension(), '.'), *d = Ext;
  74. if (ValidStr(s) && stricmp(s, ".*"))
  75. {
  76. while (*s AND *s != ';')
  77. {
  78. *d++ = *s++;
  79. }
  80. *d++ = 0;
  81. strlwr(Ext);
  82. // bung the extension from the file type if not there
  83. char *Dot = strrchr(File, '.');
  84. if (!Dot OR strchr(Dot, DIR_CHAR))
  85. {
  86. strcat(File, Ext);
  87. }
  88. }
  89. }
  90. Status = SaveFile(File);
  91. if (Status)
  92. {
  93. AddFile(File);
  94. }
  95. else
  96. {
  97. RemoveFile(File);
  98. }
  99. }
  100. return Status;
  101. }
  102. void GMru::_Update()
  103. {
  104. while (d->_Items.Length() > d->_Size)
  105. {
  106. char *s = d->_Items.Last();
  107. d->_Items.Delete(s);
  108. DeleteArray(s);
  109. }
  110. if (d->_Parent)
  111. {
  112. // remove existing items.
  113. d->_Parent->Empty();
  114. // add current items
  115. if (d->_Items.Length() > 0)
  116. {
  117. int i = M_MRU_BASE;
  118. for (char *c = d->_Items.First(); c; c = d->_Items.Next())
  119. {
  120. d->_Parent->AppendItem(c, i++, true);
  121. }
  122. }
  123. else
  124. {
  125. d->_Parent->AppendItem("(none)", -1, false);
  126. }
  127. }
  128. }
  129. bool GMru::Set(GSubMenu *parent, int size)
  130. {
  131. d->_Parent = parent;
  132. if (size > 0)
  133. {
  134. d->_Size = size;
  135. }
  136. _Update();
  137. return true;
  138. }
  139. char *GMru::AddFile(char *FileName, bool Update)
  140. {
  141. char *Status = FileName;
  142. char *c = 0;
  143. for (c = d->_Items.First(); c; c = d->_Items.Next())
  144. {
  145. if (stricmp(c, FileName) == 0)
  146. {
  147. // exact string being added.. just move to the top
  148. // no need to reallocate
  149. d->_Items.Delete(c);
  150. d->_Items.Insert(c, 0);
  151. break;
  152. }
  153. }
  154. if (!c)
  155. {
  156. Status = NewStr(FileName);
  157. d->_Items.Insert(Status, 0);
  158. }
  159. if (Update)
  160. {
  161. // update
  162. _Update();
  163. }
  164. return Status;
  165. }
  166. void GMru::RemoveFile(char *FileName, bool Update)
  167. {
  168. // remove from list if there
  169. for (char *c = d->_Items.First(); c; c = d->_Items.Next())
  170. {
  171. if (stricmp(c, FileName) == 0)
  172. {
  173. d->_Items.Delete(c);
  174. DeleteArray(c);
  175. break;
  176. }
  177. }
  178. if (Update)
  179. {
  180. _Update();
  181. }
  182. }
  183. void GMru::DoFileDlg(bool Open)
  184. {
  185. GetFileTypes(&d->_Select, false);
  186. d->_Select.ShowReadOnly(Open);
  187. if (Open ? d->_Select.Open() : d->_Select.Save())
  188. {
  189. d->_SelectedType = d->_Select.TypeAt(d->_Select.SelectedType());
  190. if (Open)
  191. _OpenFile(d->_Select.Name(), d->_Select.ReadOnly());
  192. else
  193. _SaveFile(d->_Select.Name());
  194. }
  195. }
  196. void GMru::OnCommand(int Cmd)
  197. {
  198. GViewI *Wnd = d->_Parent->GetMenu() ? d->_Parent->GetMenu()->WindowHandle() : 0;
  199. if (Wnd)
  200. {
  201. d->_Select.Parent(Wnd);
  202. d->_Select.ClearTypes();
  203. d->_SelectedType = 0;
  204. if (_GetCurFile())
  205. {
  206. d->_Select.Name(_GetCurFile());
  207. char Path[256];
  208. strsafecpy(Path, _GetCurFile(), sizeof(Path));
  209. LgiTrimDir(Path);
  210. d->_Select.InitialDir(Path);
  211. }
  212. if (Cmd == IDM_OPEN)
  213. {
  214. DoFileDlg(true);
  215. }
  216. else if (Cmd == IDM_SAVEAS)
  217. {
  218. DoFileDlg(false);
  219. }
  220. }
  221. if (Cmd >= M_MRU_BASE AND
  222. Cmd < M_MRU_BASE + d->_Items.Length())
  223. {
  224. int Index = Cmd - M_MRU_BASE;
  225. char *c = d->_Items.ItemAt(Index);
  226. if (c)
  227. {
  228. _OpenFile(c, false);
  229. }
  230. }
  231. }
  232. GMessage::Result GMru::OnEvent(GMessage *Msg)
  233. {
  234. /*
  235. if (d->_Parent AND
  236. MsgCode(Msg) == M_COMMAND)
  237. {
  238. #ifdef BEOS
  239. int32 Cmd = 0;
  240. int32 Event = 0;
  241. Msg->FindInt32("Cmd", &Cmd);
  242. Msg->FindInt32("Event", &Event);
  243. #else
  244. int Cmd = MsgA(Msg) & 0xffff;
  245. #endif
  246. OnCommand(Cmd);
  247. }
  248. */
  249. return false;
  250. }
  251. bool GMru::Serialize(GDom *Store, const char *Prefix, bool Write)
  252. {
  253. bool Status = false;
  254. GVariant v;
  255. if (Store AND Prefix)
  256. {
  257. if (Write)
  258. {
  259. // add our keys
  260. char Key[64];
  261. sprintf(Key, "%s.Items", Prefix);
  262. Store->SetValue(Key, v = d->_Items.Length());
  263. int i=0;
  264. for (char *c=d->_Items.First(); c; c=d->_Items.Next())
  265. {
  266. sprintf(Key, "%s.Item%i", Prefix, i++);
  267. Store->SetValue(Key, v = c);
  268. }
  269. }
  270. else
  271. {
  272. // clear ourself
  273. d->_Items.DeleteArrays();
  274. // read our keys in
  275. char Key[64];
  276. sprintf(Key, "%s.Items", Prefix);
  277. GVariant i;
  278. if (Store->GetValue(Key, i))
  279. {
  280. for (int n=0; n<i.CastInt32(); n++)
  281. {
  282. sprintf(Key, "%s.Item%i", Prefix, n);
  283. GVariant File;
  284. if (Store->GetValue(Key, File))
  285. {
  286. d->_Items.Insert(NewStr(File.Str()));
  287. }
  288. }
  289. }
  290. _Update();
  291. }
  292. }
  293. return Status;
  294. }
  295. /*
  296. bool GMru::Serialize(ObjProperties *Props, char *Prefix, bool Write)
  297. {
  298. bool Status = false;
  299. if (Props AND Prefix)
  300. {
  301. if (Write)
  302. {
  303. // clear existing keys
  304. for (bool b=Props->FirstKey(); b; b=Props->NextKey())
  305. {
  306. if (Props->KeyName() AND
  307. strnicmp(Props->KeyName(), "Prefix", strlen(Prefix)) == 0)
  308. {
  309. Props->DeleteKey();
  310. b = Props->KeyType();
  311. }
  312. else
  313. {
  314. b=Props->NextKey();
  315. }
  316. }
  317. // add our keys
  318. char Key[64];
  319. sprintf(Key, "%s.Items", Prefix);
  320. Props->Set(Key, d->_Items.Length());
  321. int i=0;
  322. for (char *c=d->_Items.First(); c; c=d->_Items.Next())
  323. {
  324. sprintf(Key, "%s.%i", Prefix, i++);
  325. Props->Set(Key, c);
  326. }
  327. }
  328. else
  329. {
  330. // clear ourself
  331. d->_Items.DeleteArrays();
  332. // read our keys in
  333. char Key[64];
  334. sprintf(Key, "%s.Items", Prefix);
  335. int i = 0;
  336. if (Props->Get(Key, i))
  337. {
  338. for (int n=0; n<i; n++)
  339. {
  340. sprintf(Key, "%s.%i", Prefix, n);
  341. char *File;
  342. if (Props->Get(Key, File))
  343. {
  344. d->_Items.Insert(NewStr(File));
  345. }
  346. }
  347. }
  348. _Update();
  349. }
  350. }
  351. return Status;
  352. }
  353. */