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
|
#ifndef ADD_BUTTONS_H
#define ADD_BUTTONS_H
/*
* Menu buttons to select tags
*
* Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <gtkmm/menu.h>
#include "TagMenu.h"
//#include "DebtagsDocument.h"
#include <gtkmm/eventbox.h>
template<typename Doc>
class AddButton : public Gtk::Button
{
public:
typedef SigC::Signal1<void, typename Doc::Tag> type_signal_selected;
protected:
type_signal_selected _signal_selected;
typedef typename Doc::Facet Facet;
typedef typename Doc::Tag Tag;
typedef typename Doc::Package Package;
protected:
virtual bool invoke(GdkEvent* e) = 0;
Doc& doc;
void do_selected(Tag tag)
{
_signal_selected.emit(tag);
}
public:
AddButton(Doc& doc, const std::string& label) : Gtk::Button(label), doc(doc)
{
add_events(Gdk::BUTTON_PRESS_MASK);
signal_event().connect(sigc::mem_fun(*this, &AddButton<Doc>::invoke));
}
type_signal_selected signal_selected() throw () { return _signal_selected; }
};
template<typename Doc, typename Sel>
class AddAllButton : public AddButton<Doc>
{
protected:
typedef typename Doc::Facet Facet;
typedef typename Doc::Tag Tag;
typedef typename Doc::Package Package;
virtual bool invoke(GdkEvent* e)
{
if (e->type == GDK_BUTTON_PRESS)
{
TagMenu<Doc>* addMenu = manage(new TagMenu<Doc>());
addMenu->populateUnselected(this->doc, this->sel.currentTags());
addMenu->signal_selected().connect(sigc::mem_fun(*this, &AddAllButton<Doc, Sel>::do_selected));
addMenu->popup(e->button.button, e->button.time);
return true;
}
return false;
}
const Sel& sel;
public:
AddAllButton(Doc& doc, const Sel& sel, const std::string& label)
: AddButton<Doc>(doc, label), sel(sel) {}
};
template<typename Doc, typename Sel>
AddAllButton<Doc, Sel>* newAddAllButton(Doc& doc, const Sel& sel, const std::string& label)
{
return new AddAllButton<Doc, Sel>(doc, sel, label);
}
template<typename Doc, typename Sel>
class AddCompanionButton : public AddButton<Doc>
{
protected:
typedef typename Doc::Facet Facet;
typedef typename Doc::Tag Tag;
typedef typename Doc::Package Package;
virtual bool invoke(GdkEvent* e)
{
if (e->type == GDK_BUTTON_PRESS)
{
TagMenu<Doc>* addMenu = manage(new TagMenu<Doc>());
if (this->doc.getComputeIntensive())
{
addMenu->populateAvailableSmart(this->doc, this->sel.currentTags(), this->doc.subCollection);
} else {
addMenu->populateAvailable(this->doc, this->sel.currentTags(), this->doc.subCollection);
}
addMenu->signal_selected().connect(sigc::mem_fun(*this, &AddCompanionButton<Doc, Sel>::do_selected));
addMenu->popup(e->button.button, e->button.time);
return true;
}
return false;
}
const Sel& sel;
public:
AddCompanionButton(Doc& doc, const Sel& sel, const std::string& label)
: AddButton<Doc>(doc, label), sel(sel) {}
};
template<typename Doc, typename Sel>
AddCompanionButton<Doc, Sel>* newAddCompanionButton(Doc& doc, const Sel& sel, const std::string& label)
{
return new AddCompanionButton<Doc, Sel>(doc, sel, label);
}
template<typename Doc, typename Sel>
class AddDiscriminantButton : public AddButton<Doc>
{
protected:
typedef typename Doc::Facet Facet;
typedef typename Doc::Tag Tag;
typedef typename Doc::Package Package;
virtual bool invoke(GdkEvent* e)
{
if (e->type == GDK_BUTTON_PRESS)
{
std::vector<Tag> tags = this->doc.subCollection.tagsInDiscriminanceOrder();
std::set<Tag> current = this->sel.currentTags();
Gtk::Menu* addMenu = manage(new Gtk::Menu());
int count = 0;
for (typename std::vector<Tag>::const_reverse_iterator i = tags.rbegin();
i != tags.rend(); ++i)
{
// Only show tags that are not in the package yet
if (current.find(*i) != current.end())
continue;
if (count >= 21)
break;
addMenu->items().push_back(Gtk::Menu_Helpers::MenuElem(
"[" + i->fullname() + "] " + i->shortDescription(),
sigc::bind<Tag>(
sigc::mem_fun(*this, &AddDiscriminantButton<Doc, Sel>::do_selected), *i)));
++count;
}
addMenu->popup(e->button.button, e->button.time);
return true;
}
return false;
}
const Sel& sel;
public:
AddDiscriminantButton(Doc& doc, const Sel& sel, const std::string& label)
: AddButton<Doc>(doc, label), sel(sel) {}
};
template<typename Doc, typename Sel>
AddDiscriminantButton<Doc, Sel>* newAddDiscriminantButton(Doc& doc, const Sel& sel, const std::string& label)
{
return new AddDiscriminantButton<Doc, Sel>(doc, sel, label);
}
template<typename Doc, typename Sel>
class AddRelevantButton : public AddButton<Doc>
{
protected:
typedef typename Doc::Facet Facet;
typedef typename Doc::Tag Tag;
typedef typename Doc::Package Package;
virtual bool invoke(GdkEvent* e)
{
if (e->type == GDK_BUTTON_PRESS)
{
std::vector<Tag> tags = this->doc.subCollection.tagsInRelevanceOrder(this->doc.debtags());
std::set<Tag> current = sel.currentTags();
Gtk::Menu* addMenu = manage(new Gtk::Menu());
int count = 0;
for (typename std::vector<Tag>::const_reverse_iterator i = tags.rbegin();
i != tags.rend(); ++i)
{
// Only show tags that are not in the package yet
if (current.find(*i) != current.end())
continue;
if (count >= 21)
break;
addMenu->items().push_back(Gtk::Menu_Helpers::MenuElem(
"[" + i->fullname() + "] " + i->shortDescription(),
sigc::bind<Tag>(
sigc::mem_fun(*this, &AddRelevantButton<Doc, Sel>::do_selected), *i)));
++count;
}
addMenu->popup(e->button.button, e->button.time);
return true;
}
return false;
}
const Sel& sel;
public:
AddRelevantButton(Doc& doc, const Sel& sel, const std::string& label)
: AddButton<Doc>(doc, label), sel(sel) {}
};
template<typename Doc, typename Sel>
AddRelevantButton<Doc, Sel>* newAddRelevantButton(Doc& doc, const Sel& sel, const std::string& label)
{
return new AddRelevantButton<Doc, Sel>(doc, sel, label);
}
// vim:set ts=4 sw=4:
#endif
|