File: NstManagerRecentFiles.cpp

package info (click to toggle)
nestopia 1.52.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,140 kB
  • sloc: cpp: 127,444; xml: 27,234; ansic: 3,635; makefile: 949; sh: 19
file content (172 lines) | stat: -rw-r--r-- 4,964 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#include "NstManager.hpp"
#include "NstManagerRecentFiles.hpp"

namespace Nestopia
{
	namespace Managers
	{
		NST_COMPILE_ASSERT
		(
			IDM_FILE_RECENT_2 == IDM_FILE_RECENT_1 + 1 &&
			IDM_FILE_RECENT_3 == IDM_FILE_RECENT_1 + 2 &&
			IDM_FILE_RECENT_4 == IDM_FILE_RECENT_1 + 3 &&
			IDM_FILE_RECENT_5 == IDM_FILE_RECENT_1 + 4 &&
			IDM_FILE_RECENT_6 == IDM_FILE_RECENT_1 + 5 &&
			IDM_FILE_RECENT_7 == IDM_FILE_RECENT_1 + 6 &&
			IDM_FILE_RECENT_8 == IDM_FILE_RECENT_1 + 7 &&
			IDM_FILE_RECENT_9 == IDM_FILE_RECENT_1 + 8
		);

		RecentFiles::RecentFiles(Emulator& e,const Configuration& cfg,Window::Menu& m)
		: Manager(e,m,this,&RecentFiles::OnEmuEvent)
		{
			static const Window::Menu::CmdHandler::Entry<RecentFiles> commands[] =
			{
				{ IDM_FILE_RECENT_1,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_2,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_3,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_4,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_5,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_6,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_7,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_8,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_9,     &RecentFiles::OnMenu  },
				{ IDM_FILE_RECENT_LOCK,  &RecentFiles::OnLock  },
				{ IDM_FILE_RECENT_CLEAR, &RecentFiles::OnClear }
			};

			menu.Commands().Add( this, commands );

			Configuration::ConstSection files( cfg["paths"]["recent"]["files"] );

			menu[IDM_FILE_RECENT_LOCK].Check( files["locked"].Yes() );

			uint count = 0;

			Path path;

			for (uint i=0; i < MAX_FILES; ++i)
			{
				path = files["file"][i].Str();

				if (path.Length())
				{
					path.MakePretty();
					path.Insert( 0, "&x ", 3 );
					path[1] = '1' + count;

					menu[IDM_FILE_RECENT_1 + count++].Text() << path;
				}
			}

			menu[IDM_FILE_RECENT_CLEAR].Enable( count );

			for (count += IDM_FILE_RECENT_1; count <= IDM_FILE_RECENT_9; ++count)
				menu[count].Remove();
		}

		void RecentFiles::Save(Configuration& cfg) const
		{
			Configuration::Section files( cfg["paths"]["recent"]["files"] );

			files["locked"].YesNo() = menu[IDM_FILE_RECENT_LOCK].Checked();

			HeapString path;

			for (uint i=0; i < MAX_FILES && menu[IDM_FILE_RECENT_1 + i].Text() >> path; ++i)
				files["file"][i].Str() = path(3);
		}

		void RecentFiles::OnMenu(uint cmd)
		{
			HeapString path;

			if ((menu[cmd].Text() >> path) > 3)
				Application::Instance::GetMainWindow().Send( Application::Instance::WM_NST_LAUNCH, 0, path(3).Ptr() );
		}

		void RecentFiles::OnLock(uint)
		{
			menu[IDM_FILE_RECENT_LOCK].ToggleCheck();
		}

		void RecentFiles::OnClear(uint)
		{
			menu[IDM_FILE_RECENT_CLEAR].Disable();

			for (uint i=IDM_FILE_RECENT_1; i <= IDM_FILE_RECENT_9; ++i)
				menu[i].Remove();
		}

		void RecentFiles::Add(const uint idm,const HeapString& name) const
		{
			if (menu[idm].Exists())
			{
				menu[idm].Text() << name;
			}
			else
			{
				Window::Menu::Item item( menu[IDM_POS_FILE][IDM_POS_FILE_RECENTFILES] );
				menu.Insert( item[item.NumItems() - 3], idm, name );
			}
		}

		void RecentFiles::OnEmuEvent(const Emulator::Event event,const Emulator::Data data)
		{
			switch (event)
			{
				case Emulator::EVENT_LOAD:

					if (menu[IDM_FILE_RECENT_LOCK].Unchecked())
					{
						HeapString items[MAX_FILES];
						HeapString curPath( emulator.GetStartPath() );

						for (uint i=IDM_FILE_RECENT_1, j=0; i <= IDM_FILE_RECENT_9 && menu[i].Text() >> items[j]; ++i)
						{
							if (items[j](3) != curPath)
								items[j][1] = ('2' + j), ++j;
						}

						curPath.Insert( 0, "&1 ", 3 );
						Add( IDM_FILE_RECENT_1, curPath );

						for (uint i=0; i < MAX_FILES-1 && items[i].Length(); ++i)
							Add( IDM_FILE_RECENT_2 + i, items[i] );

						menu[IDM_FILE_RECENT_CLEAR].Enable();
					}
					break;

				case Emulator::EVENT_NETPLAY_MODE:

					menu[IDM_POS_FILE][IDM_POS_FILE_RECENTFILES].Enable( !data );
					break;
			}
		}
	}
}