~elementary-apps/pantheon-files/trunk

« back to all changes in this revision

Viewing changes to filechooser-module/BreadcrumbsElements.vala

  • Committer: donadigo
  • Date: 2015-04-30 22:07:48 UTC
  • mto: This revision was merged to the branch mainline in revision 1842.
  • Revision ID: donadigos159@gmail.com-20150430220748-kay2fc7qf1sn30bf
Initial commit; filechooser gtk module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2011 Lucas Baudin <xapantu@gmail.com>
 
3
 *
 
4
 * Marlin is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; either version 2 of the
 
7
 * License, or (at your option) any later version.
 
8
 *
 
9
 * Marlin is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public
 
15
 * License along with this program; see the file COPYING.  If not,
 
16
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
public class Marlin.View.Chrome.BreadcrumbsElement : Object {
 
22
 
 
23
    private static const int ICON_MARGIN = 3;
 
24
    public string? text;
 
25
    public double offset = 0;
 
26
    public double last_height = 0;
 
27
    public double text_width = -1;
 
28
    public double text_height = -1;
 
29
    public double max_width = -1;
 
30
    public double x = 0;
 
31
    public double width {
 
32
        get {
 
33
            return text_width + padding.left + padding.right + last_height / 2;
 
34
        }
 
35
    }
 
36
    public double real_width {
 
37
        get {
 
38
            return (max_width > 0 ? max_width : text_width) +
 
39
                    padding.left + padding.right + last_height / 2;
 
40
        }
 
41
    }
 
42
    public bool hidden = false;
 
43
    public bool display = true;
 
44
    public bool display_text = true;
 
45
    public string? text_displayed = null;
 
46
 
 
47
    public bool pressed = false;
 
48
    private Gtk.Border padding = Gtk.Border ();
 
49
    private Gdk.Pixbuf icon = null;
 
50
 
 
51
    public BreadcrumbsElement (string text_) {
 
52
        text = text_;
 
53
        text_displayed = Uri.unescape_string (text);
 
54
    }
 
55
 
 
56
    public void set_icon (Gdk.Pixbuf icon_) {
 
57
        icon = icon_;
 
58
    }
 
59
 
 
60
    public double draw (Cairo.Context cr, double x, double y, double height, Gtk.StyleContext button_context, Gtk.Widget widget) {
 
61
        var state = button_context.get_state ();
 
62
        if (pressed)
 
63
            state |= Gtk.StateFlags.ACTIVE;
 
64
 
 
65
        padding = button_context.get_padding (state);
 
66
        double line_width = cr.get_line_width ();
 
67
 
 
68
        cr.restore ();
 
69
        cr.save ();
 
70
        last_height = height;
 
71
        cr.set_source_rgb (0,0,0);
 
72
        string? text = text_displayed ?? this.text;
 
73
        Pango.Layout layout = widget.create_pango_layout (text ?? "");
 
74
 
 
75
        if (icon == null) {
 
76
            computetext_width (layout);
 
77
        } else if (!display_text) {
 
78
            text_width = icon.get_width () + ICON_MARGIN;
 
79
        } else {
 
80
            computetext_width (layout);
 
81
            text_width += icon.get_width () + 2 * ICON_MARGIN;
 
82
        }
 
83
 
 
84
        if (max_width > 0) {
 
85
            layout.set_width (Pango.units_from_double (max_width));
 
86
            layout.set_ellipsize (Pango.EllipsizeMode.MIDDLE);
 
87
        }
 
88
 
 
89
        if (offset > 0.0) {
 
90
            cr.move_to (x - height/2, y);
 
91
            cr.line_to (x, y + height/2);
 
92
            cr.line_to (x - height/2, y + height);
 
93
            cr.line_to (x + text_width + padding.left, y + height);
 
94
            cr.line_to (x + text_width + height/2 + padding.left, y + height/2);
 
95
            cr.line_to (x + text_width + padding.left, y);
 
96
            cr.close_path ();
 
97
            cr.clip ();
 
98
        }
 
99
 
 
100
        if (pressed) {
 
101
            cr.save ();
 
102
            double text_width = max_width > 0 ? max_width : this.text_width;
 
103
            var base_x = x;
 
104
            var left_x = base_x - height / 2 + line_width;
 
105
            var right_x = base_x + text_width + padding.left + padding.right;
 
106
            var arrow_right_x = right_x + height / 2;
 
107
            var top_y = y + padding.top - line_width;
 
108
            var bottom_y = y + height - padding.bottom + line_width;
 
109
            var arrow_y = y + height / 2;
 
110
            cr.move_to (left_x, top_y);
 
111
            cr.line_to (base_x, arrow_y);
 
112
            cr.line_to (left_x, bottom_y);
 
113
            cr.line_to (right_x, bottom_y);
 
114
            cr.line_to (arrow_right_x, arrow_y);
 
115
            cr.line_to (right_x, top_y);
 
116
            cr.close_path ();
 
117
 
 
118
            cr.clip ();
 
119
            button_context.save ();
 
120
            button_context.set_state (Gtk.StateFlags.ACTIVE);
 
121
            button_context.render_background (cr, left_x, y, text_width + height + padding.left + padding.right + 2 * line_width, height);
 
122
            button_context.render_frame (cr, 0, padding.top - line_width, widget.get_allocated_width (), height - line_width);
 
123
            button_context.restore ();
 
124
            cr.restore ();
 
125
        }
 
126
 
 
127
        x += padding.left;
 
128
        x -= Math.sin (offset*Math.PI_2) * width;
 
129
        if (icon == null) {
 
130
            button_context.render_layout (cr, x,
 
131
                                          y + height/2 - text_height/2, layout);
 
132
        } else if (!display_text) {
 
133
            button_context.render_icon (cr, icon, x + ICON_MARGIN,
 
134
                                         y + height/2 - icon.get_height ()/2);
 
135
        } else {
 
136
            button_context.render_icon (cr, icon, x + ICON_MARGIN,
 
137
                                         y + height/2 - icon.get_height ()/2);
 
138
            button_context.render_layout (cr, x + icon.get_width () + 2 * ICON_MARGIN,
 
139
                                          y + height/2 - text_height/2, layout);
 
140
        }
 
141
 
 
142
        x += padding.right + (max_width > 0 ? max_width : text_width);
 
143
 
 
144
        /* Draw the separator */
 
145
        cr.save ();
 
146
        cr.translate (x - height / 4, y + height / 2);
 
147
        cr.rectangle (0, -height / 2 + line_width, height, height - 2 * line_width);
 
148
        cr.clip ();
 
149
        cr.rotate (Math.PI_4);
 
150
        button_context.save ();
 
151
        button_context.add_class ("noradius-button");
 
152
        if (pressed)
 
153
            button_context.set_state (Gtk.StateFlags.ACTIVE);
 
154
 
 
155
        button_context.render_frame (cr, -height / 2, -height / 2, height, height);
 
156
        button_context.restore ();
 
157
        cr.restore ();
 
158
 
 
159
        x += height / 2;
 
160
 
 
161
        return x;
 
162
    }
 
163
 
 
164
    private void computetext_width (Pango.Layout pango) {
 
165
        int text_width, text_height;
 
166
        pango.get_size (out text_width, out text_height);
 
167
        this.text_width = Pango.units_to_double (text_width);
 
168
        this.text_height = Pango.units_to_double (text_height);
 
169
    }
 
170
}