1/* 2 * This file is part of gitg 3 * 4 * Copyright (C) 2013 - Ignacio Casal Quinteiro 5 * 6 * gitg is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * gitg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with gitg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20namespace Gitg 21{ 22 public class ProgressBin : Gtk.Bin 23 { 24 private double d_fraction; 25 26 public double fraction 27 { 28 get { return d_fraction; } 29 set 30 { 31 d_fraction = value; 32 queue_draw(); 33 } 34 } 35 36 construct 37 { 38 set_has_window(true); 39 set_redraw_on_allocate(false); 40 } 41 42 public override void realize() 43 { 44 set_realized(true); 45 46 Gtk.Allocation allocation; 47 get_allocation(out allocation); 48 49 Gdk.WindowAttr attributes = {}; 50 attributes.x = allocation.x; 51 attributes.y = allocation.y; 52 attributes.width = allocation.width; 53 attributes.height = allocation.height; 54 attributes.window_type = Gdk.WindowType.CHILD; 55 attributes.wclass = Gdk.WindowWindowClass.INPUT_OUTPUT; 56 attributes.event_mask = get_events() | 57 Gdk.EventMask.EXPOSURE_MASK | 58 Gdk.EventMask.BUTTON_PRESS_MASK | 59 Gdk.EventMask.BUTTON_RELEASE_MASK; 60 61 var attributes_mask = Gdk.WindowAttributesType.X | Gdk.WindowAttributesType.Y; 62 var window = new Gdk.Window(get_parent_window(), 63 attributes, attributes_mask); 64 65 66 set_window(window); 67 window.set_user_data(this); 68 } 69 70 public override void size_allocate(Gtk.Allocation allocation) 71 { 72 set_allocation(allocation); 73 74 var window = get_window(); 75 if (window != null) 76 { 77 window.move_resize(allocation.x, allocation.y, allocation.width, allocation.height); 78 } 79 80 var child = get_child(); 81 if (child != null && child.get_visible()) 82 { 83 Gtk.Allocation child_allocation = {}; 84 int border_width = (int)get_border_width(); 85 child_allocation.x = border_width; 86 child_allocation.y = border_width; 87 child_allocation.width = allocation.width - 2 * border_width; 88 child_allocation.height = allocation.height - 2 * border_width; 89 90 child.size_allocate(child_allocation); 91 } 92 } 93 94 public override bool draw(Cairo.Context cr) 95 { 96 Gtk.Allocation allocation; 97 get_allocation(out allocation); 98 99 var context = get_style_context(); 100 context.render_background(cr, 0, 0, allocation.width, allocation.height); 101 102 context.save(); 103 context.add_class("progress-bin"); 104 105 context.render_background(cr, 0, 0, allocation.width * d_fraction, allocation.height); 106 context.restore(); 107 108 base.draw(cr); 109 return true; 110 } 111 } 112} 113 114// ex:ts=4 noet 115