tom_usenet <to********@hot mail.com> writes:
On Thu, 18 Dec 2003 16:51:34 +0000, Roger Leigh
<${******@inval id.whinlatter.u klinux.net.inva lid> wrote:
Jeffrey Schwab <je******@comca st.net> writes:
typename menu_list::cons t_iterator cur;
Thanks, that works just fine!
Is it possible to declare this as a type, rather than using typename
each time?
You only have to use typename when using a member of a dependent type
that is also a type. So it only applies to code inside a template
definition. e.g. this is fine
ObjectOptionMen u<int>::menu_li st::const_itera tor i;
I see.
I tried putting
typename menu_list::cons t_iterator;
just after the typedefs, but that doesn't work.
I'd rather the users of the class didn't need to use typename--is
there any way to achieve this?
The users of which class? How do they use it?
I wasn't aware of exactly how typename worked. This question is
wrong--I meant code using the ObjectOptionMen u<> class, but since they
don't need to use typename anyway, it's a non-issue.
For anyone else having problems with templates (and default
parameters), my working code follows:
// object-based option menu -*- C++ -*-
// $Id: objectoptionmen u.h,v 1.2 2003/12/14 16:14:54 roger Exp $
//
// Copyright (C) 2003 Roger Leigh.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
////////////////////////////////////////////////////////////////////////////
#ifndef GTKMM_RLEXTRA_O BJECTOPTIONMENU _H
#define GTKMM_RLEXTRA_O BJECTOPTIONMENU _H
#include <cassert>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <gtkmm/optionmenu.h>
#include <gtkmm/menu.h>
#include <libglademm/xml.h>
namespace Gtkmm
{
template<typena me T>
class ObjectOptionMen uDescribeObject
{
public:
typedef T object_type;
std::string operator () (const object_type& object) const
{
std::ostringstr eam desc;
desc << object;
return desc.str();
}
};
template<typena me T>
class ObjectOptionMen uItem : public Gtk::MenuItem
{
public:
typedef T object_type;
ObjectOptionMen uItem(const object_type& object,
const Glib::ustring& label):
Gtk::MenuItem(l abel),
m_object(object )
{}
virtual ~ObjectOptionMe nuItem()
{}
const object_type& get_object() const
{
return m_object;
}
private:
object_type m_object;
};
template<typena me T, typename D = ObjectOptionMen uDescribeObject <T> >
class ObjectOptionMen u : public Gtk::OptionMenu
{
public:
typedef T object_type;
typedef D description_fun c;
typedef std::vector<obj ect_type> menu_list;
ObjectOptionMen u():
describe_object (),
m_options(),
m_menu()
{
}
ObjectOptionMen u(const menu_list& list):
describe_object (),
m_options(),
m_menu()
{
add_menu(list);
}
/**
* Constructor for initialisation from a Glade interface description.
* @param cobject the GTK+ C object.
* @param xml_interface the Glade XML interface.
*/
explicit ObjectOptionMen u(BaseObjectTyp e* cobject,
const Glib::RefPtr<Gn ome::Glade::Xml >& xml_interface):
describe_object (),
m_options(),
m_menu()
{}
virtual ~ObjectOptionMe nu()
{}
void set_menu(const menu_list& list)
{
std::cerr << "ObjectOptionMe nu::add_menu()" << std::endl;
remove_menu();
for (typename menu_list::cons t_iterator cur = list.begin();
cur != list.end();
++cur)
{
m_options.push_ back(*cur);
Gtk::MenuItem *menu_item =
manage(new ObjectOptionMen uItem<object_ty pe>(*cur, describe_object (*cur)));
m_menu.append(* menu_item);
}
Gtk::OptionMenu ::set_menu(m_me nu);
Gtk::OptionMenu ::set_history(0 );
}
void remove_menu()
{
m_options.clear ();
Gtk::OptionMenu ::remove_menu() ;
m_menu.items(). clear();
}
const object_type* get_history() const
{
int selected = Gtk::OptionMenu ::get_history() ;
const Gtk::MenuItem& menu_item = get_menu()->items()[selected];
const Gtk::MenuItem* menu_item_ptr = &menu_item;
const ObjectOptionMen uItem<object_ty pe> *object_menu_it em =
dynamic_cast<co nst ObjectOptionMen uItem<object_ty pe>* >(menu_item_ptr );
if (object_menu_it em != NULL)
return &object_menu_it em->get_object() ;
return NULL;
}
void set_history(con st object_type& object)
{
Gtk::MenuShell: :MenuList& menu_list = get_menu()->items();
Gtk::MenuShell: :MenuList::size _type size = menu_list.size( );
for (Gtk::MenuShell ::MenuList::siz e_type cur = 0;
cur < size;
++cur)
{
const Gtk::MenuItem& menu_item = menu_list[cur];
const Gtk::MenuItem* menu_item_ptr = &menu_item;
const ObjectOptionMen uItem<object_ty pe> *object_menu_it em =
dynamic_cast<co nst ObjectOptionMen uItem<object_ty pe>* >(menu_item_ptr );
if (object_menu_it em != NULL)
{
if (object == object_menu_ite m->get_object() )
{
Gtk::OptionMenu ::set_history(c ur);
return;
}
}
}
Gtk::OptionMenu ::set_history(0 );
}
void on_changed()
{
std::cerr << "OptionMenu::on _changed()" << std::endl;
std::cerr << " There are " << m_menu.items(). size() << " items" << std::endl;
if (get_history() != NULL)
std::cerr << " Currently selected: " << describe_object (*get_history() ) << std::endl;
else
std::cerr << " Nothing currently selected" << std::endl;
}
protected:
description_fun c describe_object ;
menu_list m_options;
Gtk::Menu m_menu;
}; // class OptionMenu
}; // namespace Gtkmm
#endif // GTKMM_RLEXTRA_O BJECTOPTIONMENU _H
--
Roger Leigh
Printing on GNU/Linux?
http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.