Advertisement
grvrulz

connectedbuttons

May 5th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 7.36 KB | None | 0 0
  1. //
  2. //  Copyright (C) 2008 Christian Hergert <chris@dronelabs.com>
  3. //  Copyright (C) 2011 Giulio Collura
  4. //
  5. //  This program is free software: you can redistribute it and/or modify
  6. //  it under the terms of the GNU General Public License as published by
  7. //  the Free Software Foundation, either version 3 of the License, or
  8. //  (at your option) any later version.
  9. //
  10. //  This program is distributed in the hope that it will be useful,
  11. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //  GNU General Public License for more details.
  14. //
  15. //  You should have received a copy of the GNU General Public License
  16. //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. //
  18.  
  19. using Gtk;
  20. using Gdk;
  21.  
  22. namespace Granite.Widgets {
  23.  
  24.     public class ConnectedButton : Gtk.Box {
  25.  
  26.         public signal void mode_added (int index, Gtk.Widget widget);
  27.         public signal void mode_removed (int index, Gtk.Widget widget);
  28.         public signal void mode_changed (Gtk.Widget widget);
  29.  
  30.         // Style properties. Please note that style class names are for internal
  31.         // use only. Theme developers should use GraniteWidgetsConnectedButton instead.
  32.         internal static CssProvider style_provider;
  33.         internal static StyleContext widget_style;
  34.         private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
  35.  
  36.         private const string STYLESHEET = """
  37.            .GraniteConnectedButton .button {
  38.                -GtkToolbar-button-relief: normal;
  39.                border-radius: 0 0 0 0;
  40.                border-style: solid;
  41.                border-width: 1px 0 1px 1px;
  42.  
  43.                -unico-outer-stroke-width: 1px 0 1px 0;
  44.                -unico-outer-stroke-radius: 0 0 0 0;
  45.            }
  46.  
  47.            .GraniteConnectedButton .button:active,
  48.            .GraniteConnectedButton .button:insensitive {
  49.                -unico-outer-stroke-width: 1px 0 1px 0;
  50.            }
  51.  
  52.            .GraniteConnectedButton .button:first-child {
  53.                border-radius: 3px 0 0 3px;
  54.                border-width: 1px 0 1px 1px;
  55.  
  56.                -unico-outer-stroke-width: 1px 0 1px 1px;
  57.            }
  58.  
  59.            .GraniteConnectedButton .button:last-child {
  60.                border-radius: 0 3px 3px 0;
  61.                border-width: 1px;
  62.  
  63.                -unico-outer-stroke-width: 1px 1px 1px 0;
  64.            }
  65.        """;
  66.  
  67.         private int _selected = -1;
  68.  
  69.         public int selected {
  70.             get {
  71.                 return _selected;
  72.             }
  73.             set {
  74.                 set_active(value);
  75.             }
  76.         }
  77.  
  78.         public uint n_items {
  79.             get {
  80.                 return get_children ().length ();
  81.             }
  82.         }
  83.  
  84.         public ConnectedButton () {
  85.  
  86.             if (style_provider == null)
  87.             {
  88.                 style_provider = new CssProvider ();
  89.                 try {
  90.                     style_provider.load_from_data (STYLESHEET, -1);
  91.                 } catch (Error e) {
  92.                     warning ("GraniteConnectedButton: %s. The widget will not look as intended", e.message);
  93.                 }
  94.             }
  95.  
  96.             widget_style = get_style_context ();
  97.             widget_style.add_class ("GraniteConnectedButton");
  98.  
  99.             homogeneous = true;
  100.             spacing = 0;
  101.             app_paintable = true;
  102.             set_visual (get_screen ().get_rgba_visual ());
  103.  
  104.             can_focus = true;
  105.         }
  106.  
  107.         public int append_pixbuf (Gdk.Pixbuf? pixbuf) {
  108.             if (pixbuf == null) {
  109.                 warning ("GraniteWidgetsConnectedButton: Attempt to add null pixbuf failed.");
  110.                 return -1;
  111.             }
  112.  
  113.             var image = new Image.from_pixbuf (pixbuf);
  114.             return append (image);
  115.         }
  116.  
  117.         public int append_text (string? text) {
  118.             if (text == null) {
  119.                 warning ("GraniteWidgetsConnectedButton: Attempt to add null text string failed.");
  120.                 return -1;
  121.             }
  122.  
  123.             return append (new Gtk.Label(text));
  124.         }
  125.  
  126.         /**
  127.          * This is the recommended method for adding icons to the ConnectedButton widget.
  128.          * If the name of a symbolic icon is passed, it will be properly themed for
  129.          * each state of the widget. That is, it will match the foreground color
  130.          * defined by the theme for each state (active, prelight, insensitive, etc.)
  131.          */
  132.         public int append_icon (string icon_name, Gtk.IconSize size) {
  133.             return append (new Image.from_icon_name (icon_name, size));
  134.         }
  135.  
  136.         public int append (Gtk.Widget w) {
  137.             if (w == null) {
  138.                 warning ("GraniteWidgetsConnectedButton: Attempt to add null widget failed.");
  139.                 return -1;
  140.             }
  141.  
  142.             var button = new ConnectedButtonItem ();
  143.  
  144.             button.add (w);
  145.  
  146.             button.button_press_event.connect (() => {
  147.                 int selected = get_children().index (button);
  148.                 //set_active (selected);
  149.                 return true;
  150.             });
  151.  
  152.             add (button);
  153.             button.show_all ();
  154.  
  155.             int item_index = (int)get_children ().length ();
  156.             mode_added (item_index, w); // Emit the added signal
  157.             return item_index;
  158.         }
  159.  
  160.         public void set_active (int new_active_index) {
  161.             //if (new_active_index >= get_children ().length () || _selected == new_active_index)
  162.                 //return;
  163.  
  164.             //if (_selected >= 0)
  165.               //  ((Button) get_children ().nth_data (_selected)).set_active (false);
  166.  
  167.             //_selected = new_active_index;
  168.             //((Button) get_children ().nth_data (_selected)).set_active (true);
  169.  
  170.             //mode_changed (((Button) get_children ().nth_data (_selected)).get_child ());
  171.         }
  172.  
  173.         public void set_item_visible (int index, bool val) {
  174.             var item = get_children ().nth_data (index);
  175.             if (item == null)
  176.                 return;
  177.  
  178.             item.set_no_show_all (!val);
  179.             item.set_visible (val);
  180.         }
  181.  
  182.         public new void remove (int index) {
  183.             mode_removed (index, (get_children ().nth_data (index) as Gtk.Bin).get_child ());
  184.             get_children ().nth_data (index).destroy ();
  185.         }
  186.  
  187.         /*public void clear_children () {
  188.             foreach (weak Widget button in get_children ()) {
  189.                 button.hide ();
  190.                 if (button.get_parent () != null)
  191.                     base.remove (button);
  192.             }
  193.  
  194.             _selected = -1;
  195.         }
  196.  
  197.         protected override bool scroll_event (EventScroll ev) {
  198.             if (ev.direction == Gdk.ScrollDirection.DOWN) {
  199.                 selected ++;
  200.             }
  201.             else if (ev.direction == Gdk.ScrollDirection.UP) {
  202.                 selected --;
  203.             }
  204.  
  205.             return false;
  206.         }*/
  207.     }
  208.  
  209.     private class ConnectedButtonItem : Gtk.Button {
  210.         public ConnectedButtonItem () {
  211.             can_focus = false;
  212.  
  213.             const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
  214.  
  215.             get_style_context ().add_class ("raised");
  216.             get_style_context ().add_provider (ConnectedButton.style_provider, style_priority);
  217.         }
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement