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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
////////////////////////////////////////////////////////////////////////////////////////
//
// 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
//
////////////////////////////////////////////////////////////////////////////////////////
#ifndef NST_WINDOW_MENU_H
#define NST_WINDOW_MENU_H
#pragma once
#include "language/resource.h"
#include "NstResourceMenu.hpp"
#include "NstSystemAccelerator.hpp"
#include "NstWindowCustom.hpp"
namespace Nestopia
{
namespace Window
{
class Menu
{
public:
explicit Menu(uint);
~Menu();
void Hook(Custom&);
void Unhook();
uint Height() const;
bool Visible() const;
void Show(bool=true) const;
bool Toggle() const;
void EnableAccelerator(bool);
void SetKeys(const ACCEL*,uint);
void SetColor(COLORREF) const;
void ResetColor() const;
void ToggleModeless(bool) const;
class Item;
void Insert(const Item&,uint,GenericString) const;
private:
static void Clear(HMENU);
static uint NumItems(HMENU);
static void Redraw(const Custom*);
public:
void Redraw() const
{
Redraw( window );
}
class Item
{
friend class Menu;
class Stream
{
friend class Menu;
friend class Item;
const Item& item;
uint GetLength() const;
bool GetFullString(wchar_t*,uint) const;
void SetFullString(wcstring) const;
template<typename T>
void GetFullString(T& string) const
{
string.Resize( GetLength() );
GetFullString( string.Ptr(), string.Length() );
}
explicit Stream(const Item& i)
: item(i) {}
public:
void operator << (const GenericString&) const;
template<typename T>
uint operator >> (T& string) const
{
GetFullString( string );
string.FirstOf( '\t' ).Clear();
return string.Length();
}
};
public:
enum Type
{
NONE,
SUBMENU,
COMMAND,
SEPARATOR
};
Type GetType() const;
bool Enable (bool=true) const;
bool Check (bool=true) const;
void Check (uint,uint,bool) const;
void Check (uint,uint) const;
bool Enabled () const;
bool Checked () const;
bool Disabled () const { return !Enabled(); }
bool Unchecked () const { return !Checked(); }
bool ToggleCheck() const { return !Check( Unchecked() ); }
bool ToggleEnable() const { return !Enable( Disabled() ); }
bool Disable () const { return Enable (false); }
bool Uncheck () const { return Check (false); }
uint GetCommand() const;
void Remove() const;
void Clear() const;
uint NumItems() const;
bool Exists() const;
Stream Text() const
{
return Stream( *this );
}
private:
inline uint Flag() const;
HMENU hMenu;
uint pos;
const Custom* const window;
public:
Item(const Custom* w,HMENU h=NULL,uint p=0)
: hMenu(h), pos(p), window(w) {}
HMENU GetHandle() const
{
return hMenu;
}
Item operator [] (uint subPos) const
{
return Item( window, ::GetSubMenu( hMenu, pos ), subPos );
}
};
typedef Collection::Router<void,uint> CmdHandler;
class PopupHandler
{
friend class Menu;
struct Key
{
HMENU const hKey;
const Item item;
Key(const Custom* w,HMENU h,uint p)
: hKey(::GetSubMenu(h,p)), item(w,h,p) {}
explicit Key(WPARAM wParam)
: hKey(reinterpret_cast<HMENU>(wParam)), item(NULL) {}
operator HMENU() const
{
return hKey;
}
};
Menu& menu;
public:
template<uint A,uint B=UINT_MAX,uint C=UINT_MAX,uint D=UINT_MAX> struct Pos
{
enum { ID = A | (B + 1) << 8 | (C + 2) << 16 | (D + 3) << 24 };
};
template<uint A> struct Pos<A,UINT_MAX,UINT_MAX,UINT_MAX>
{
enum { ID = A };
};
template<uint A,uint B> struct Pos<A,B,UINT_MAX,UINT_MAX>
{
enum { ID = A | (B + 1) << 8 };
};
template<uint A,uint B,uint C> struct Pos<A,B,C,UINT_MAX>
{
enum { ID = A | (B + 1) << 8 | (C + 2) << 16 };
};
struct Param
{
Item menu;
bool show;
Param(const Item& i,bool s)
: menu(i), show(s) {}
};
template<typename Data> struct Entry
{
typedef void (Data::*Function)(const Param&);
uint id;
Function function;
};
private:
typedef Collection::Router<void,const Param&,Key> Handler;
Key GetKey(uint) const;
template<typename Data>
void Add(Data*,const Entry<Data>*,uint);
PopupHandler(Menu& m)
: menu(m) {}
public:
template<typename Data,uint COUNT>
void Add(Data* data,const Entry<Data>(&list)[COUNT])
{
Add( data, list, COUNT );
}
void Remove(const void* data)
{
menu.popupHandler.Hooks().Remove( data );
}
};
private:
ibool OnCommand (Param&);
ibool OnInitMenuPopup (Param&);
ibool OnUninitMenuPopup (Param&);
void OnCreate (Param&);
void OnDestroy (Param&);
enum {IDM_OFFSET = 100};
Resource::Menu handle;
Custom* window;
System::Accelerator accelerator;
CmdHandler cmdHandler;
MsgHandler::Callback cmdCallback;
PopupHandler::Handler popupHandler;
bool acceleratorEnabled;
class Instances
{
public:
static void Update(Menu*);
static void Remove(Menu*);
static void EnableAccelerators(bool);
private:
static void Update();
static bool TranslateNone (MSG&);
static bool TranslateSingle (MSG&);
static bool TranslateMulti (MSG&);
typedef Collection::Vector<const Menu*> Menus;
typedef bool (*Translator)(MSG&);
static Translator translator;
static Menus menus;
static bool acceleratorsEnabled;
public:
static bool TransAccelerator(MSG& msg)
{
return translator( msg );
}
};
public:
HMENU GetHandle() const
{
return handle;
}
uint NumItems() const
{
return NumItems( handle );
}
Item operator [] (uint pos) const
{
return Item( window, handle, pos );
}
void Hide() const
{
Show( false );
}
void Clear() const
{
Clear( handle );
}
CmdHandler& Commands()
{
return cmdHandler;
}
PopupHandler Popups()
{
return *this;
}
static bool TransAccelerator(MSG& msg)
{
return Instances::TransAccelerator( msg );
}
static void EnableAccelerators(bool enable)
{
Instances::EnableAccelerators( enable );
}
};
template<typename Data>
void Menu::PopupHandler::Add(Data* const data,const Entry<Data>* list,const uint count)
{
for (const Entry<Data>* const end = list + count; list != end; ++list)
menu.popupHandler.Hooks().Add( GetKey(list->id), data, list->function );
}
}
}
#endif
|