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
|
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
*
* This program 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; version 2 of the
* License.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "stdafx.h"
#ifndef __APPLE__
#include <algorithm>
#endif
using namespace mforms;
MenuBase::MenuBase()
: _parent(0)
{
_impl = &mforms::ControlFactory::get_instance()->_menu_item_impl;
}
MenuBase::~MenuBase()
{
for (std::vector<MenuItem*>::iterator iter = _items.begin(); iter != _items.end(); ++iter)
(*iter)->release();
_items.clear();
}
void MenuBase::add_item(MenuItem *item)
{
insert_item(-1, item);
}
void MenuBase::insert_item(int index, MenuItem *item)
{
if (index < 0 || index > (int) _items.size())
index = _items.size();
item->_parent = this;
_impl->insert_item(this, index, item);
_items.insert(_items.begin()+index, item);
// item->retain();
}
void MenuBase::remove_all()
{
_impl->remove_item(this, NULL); // null means remove all
std::vector<MenuItem*>::iterator iter;
for (iter = _items.begin(); iter != _items.end(); ++iter)
{
(*iter)->_parent = 0;
(*iter)->release();
}
_items.clear();
}
void MenuBase::remove_item(MenuItem *item)
{
std::vector<MenuItem*>::iterator iter = std::find(_items.begin(), _items.end(), item);
if (iter != _items.end())
{
(*iter)->_parent = 0;
_impl->remove_item(this, item);
item->release();
_items.erase(iter);
}
}
void MenuBase::set_enabled(bool flag)
{
_impl->set_enabled(this, flag);
}
bool MenuBase::get_enabled()
{
return _impl->get_enabled(this);
}
MenuItem *MenuBase::find_item(const std::string &name)
{
for (std::vector<MenuItem*>::const_iterator iter = _items.begin(); iter != _items.end(); ++iter)
{
if ((*iter)->get_name() == name)
return *iter;
MenuItem *item;
if ((item = (*iter)->find_item(name)))
return item;
}
return 0;
}
void MenuBase::validate()
{
for (std::vector<MenuItem*>::const_iterator iter = _items.begin(); iter != _items.end(); ++iter)
(*iter)->validate();
}
MenuBar *MenuBase::get_menubar()
{
if (dynamic_cast<MenuBar*>(this) != NULL)
return dynamic_cast<MenuBar*>(this);
MenuBase *p = _parent;
while (p && p->get_parent())
p = p->get_parent();
if (p)
return dynamic_cast<MenuBar*>(p);
return 0;
}
MenuItem::MenuItem(const std::string &title, const MenuItemType type)
: MenuBase()
{
_impl->create_menu_item(this, title, type); // ! Warning there will be no checked menu with this!
}
void MenuItem::set_title(const std::string &title)
{
_impl->set_title(this, title);
}
std::string MenuItem::get_title()
{
return _impl->get_title(this);
}
void MenuItem::set_shortcut(const std::string &shortcut)
{
_impl->set_shortcut(this, shortcut);
}
void MenuItem::set_checked(bool flag)
{
_impl->set_checked(this, flag);
}
bool MenuItem::get_checked()
{
return _impl->get_checked(this);
}
void MenuItem::callback()
{
_clicked_signal();
}
void MenuItem::validate()
{
if (_validate)
set_enabled(_validate());
if (!_items.empty())
MenuBase::validate();
}
void MenuItem::set_validator(const boost::function<bool ()> &slot)
{
_validate = slot;
}
void MenuItem::set_name(const std::string &name)
{
_name= name;
}
MenuBar::MenuBar()
: MenuBase()
{
_impl->create_menu_bar(this);
}
void MenuBar::will_show_submenu_from(MenuItem *item)
{
_signal_will_show(item);
}
void MenuBar::set_item_enabled(const std::string &item_name, bool flag)
{
MenuItem *item = find_item(item_name);
if (item)
item->set_enabled(flag);
}
void MenuBar::set_item_checked(const std::string &item_name, bool flag)
{
MenuItem *item = find_item(item_name);
if (item)
item->set_checked(flag);
}
|