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
|
/*
* Copyright 2019 elementary, Inc. (https://elementary.io)
* Copyright 2008–2013 Christian Hergert <chris@dronelabs.com>,
* Copyright 2008–2013 Giulio Collura <random.cpp@gmail.com>,
* Copyright 2008–2013 Victor Eduardo <victoreduardm@gmail.com>,
* Copyright 2008–2013 ammonkey <am.monkeyd@gmail.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
namespace Granite.Widgets {
/**
* This widget is a multiple option modal switch
*
* {{../doc/images/ModeButton.png}}
*/
public class ModeButton : Gtk.Box {
private class Item : Gtk.ToggleButton {
public int index { get; construct; }
public Item (int index) {
Object (index: index);
add_events (Gdk.EventMask.SCROLL_MASK);
}
}
public signal void mode_added (int index, Gtk.Widget widget);
public signal void mode_removed (int index, Gtk.Widget widget);
public signal void mode_changed (Gtk.Widget widget);
/**
* Index of currently selected item.
*/
public int selected {
get { return _selected; }
set { set_active (value); }
}
/**
* Read-only length of current ModeButton
*/
public uint n_items {
get { return item_map.size; }
}
private int _selected = -1;
private Gee.HashMap<int, Item> item_map;
/**
* Makes new ModeButton
*/
public ModeButton () {
}
construct {
homogeneous = true;
spacing = 0;
item_map = new Gee.HashMap<int, Item> ();
var style = get_style_context ();
style.add_class (Gtk.STYLE_CLASS_LINKED);
style.add_class ("raised"); // needed for toolbars
}
/**
* Appends Pixbuf to ModeButton
*
* @param pixbuf Gdk.Pixbuf to append to ModeButton
*/
public int append_pixbuf (Gdk.Pixbuf pixbuf) {
return append (new Gtk.Image.from_pixbuf (pixbuf));
}
/**
* Appends text to ModeButton
*
* @param text text to append to ModeButton
* @return index of new item
*/
public int append_text (string text) {
return append (new Gtk.Label (text));
}
/**
* Appends icon to ModeButton
*
* @param icon_name name of icon to append
* @param size desired size of icon
* @return index of appended item
*/
public int append_icon (string icon_name, Gtk.IconSize size) {
return append (new Gtk.Image.from_icon_name (icon_name, size));
}
/**
* Appends given widget to ModeButton
*
* @param w widget to add to ModeButton
* @return index of new item
*/
public int append (Gtk.Widget w) {
int index;
for (index = item_map.size; item_map.has_key (index); index++);
assert (item_map[index] == null);
var item = new Item (index);
item.scroll_event.connect (on_scroll_event);
item.add (w);
item.toggled.connect (() => {
if (item.active) {
selected = item.index;
} else if (selected == item.index) {
// If the selected index still references this item, then it
// was toggled by the user, not programmatically.
// -> Reactivate the item to prevent an empty selection.
item.active = true;
}
});
item_map[index] = item;
add (item);
item.show_all ();
mode_added (index, w);
return index;
}
/**
* Clear selected items
*/
private void clear_selected () {
// Update _selected before deactivating the selected item to let it
// know that it is being deactivated programmatically, not by the
// user.
_selected = -1;
foreach (var item in item_map.values) {
if (item != null && item.active) {
item.set_active (false);
}
}
}
/**
* Sets item of given index's activity
*
* @param new_active_index index of changed item
*/
public void set_active (int new_active_index) {
if (new_active_index <= -1) {
clear_selected ();
return;
}
return_if_fail (item_map.has_key (new_active_index));
var new_item = item_map[new_active_index] as Item;
if (new_item != null) {
assert (new_item.index == new_active_index);
new_item.set_active (true);
if (_selected == new_active_index) {
return;
}
// Unselect the previous item
var old_item = item_map[_selected] as Item;
// Update _selected before deactivating the selected item to let
// it know that it is being deactivated programmatically, not by
// the user.
_selected = new_active_index;
if (old_item != null) {
old_item.set_active (false);
}
mode_changed (new_item.get_child ());
}
}
/**
* Changes visibility of item of given index
*
* @param index index of item to be modified
* @param val value to change the visiblity to
*/
public void set_item_visible (int index, bool val) {
return_if_fail (item_map.has_key (index));
var item = item_map[index] as Item;
if (item != null) {
assert (item.index == index);
item.no_show_all = !val;
item.visible = val;
}
}
/**
* Removes item at given index
*
* @param index index of item to remove
*/
public new void remove (int index) {
return_if_fail (item_map.has_key (index));
var item = item_map[index] as Item;
if (item != null) {
assert (item.index == index);
item_map.unset (index);
mode_removed (index, item.get_child ());
item.destroy ();
}
}
/**
* Clears all children
*/
public void clear_children () {
foreach (weak Gtk.Widget button in get_children ()) {
button.hide ();
if (button.get_parent () != null) {
base.remove (button);
}
}
item_map.clear ();
_selected = -1;
}
private bool on_scroll_event (Gtk.Widget widget, Gdk.EventScroll ev) {
int offset;
switch (ev.direction) {
case Gdk.ScrollDirection.DOWN:
case Gdk.ScrollDirection.RIGHT:
offset = 1;
break;
case Gdk.ScrollDirection.UP:
case Gdk.ScrollDirection.LEFT:
offset = -1;
break;
default:
return false;
}
// Try to find a valid item, since there could be invisible items in
// the middle and those shouldn't be selected. We use the children list
// instead of item_map because order matters here.
var children = get_children ();
uint n_children = children.length ();
var selected_item = item_map[selected];
if (selected_item == null) {
return false;
}
int new_item = children.index (selected_item);
if (new_item < 0) {
return false;
}
do {
new_item += offset;
var item = children.nth_data (new_item) as Item;
if (item != null && item.visible && item.sensitive) {
selected = item.index;
break;
}
} while (new_item >= 0 && new_item < n_children);
return false;
}
}
}
|