1 /***************************************************************************
2 * Copyright (C) 2015 by Pere Ràfols Soler *
3 * sapista2@gmail.com *
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 2 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, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "sidechainbox.h"
22 #include "colors.h"
23
24 #define MARGIN 6
25 #define RADIUS 4
26
SideChainBox(std::string sTitle,int top_padding)27 SideChainBox::SideChainBox( std::string sTitle, int top_padding): m_title(sTitle), m_top_padding(top_padding)
28 {
29
30 }
31
~SideChainBox()32 SideChainBox::~SideChainBox()
33 {
34
35 }
36
set_label(const Glib::ustring & label)37 void SideChainBox::set_label(const Glib::ustring& label)
38 {
39 m_title = label;
40 Glib::RefPtr<Gdk::Window> win = get_window();
41 if(win)
42 {
43 Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
44 win->invalidate_rect(r, false);
45 }
46 }
47
on_expose_event(GdkEventExpose * event)48 bool SideChainBox::on_expose_event(GdkEventExpose* event)
49 {
50 bool ret = Gtk::EventBox::on_expose_event(event); //Call parent redraw()
51
52 Glib::RefPtr<Gdk::Window> window = get_window();
53 if(window)
54 {
55 Gtk::Allocation allocation = get_allocation();
56 const int width = allocation.get_width();
57 const int height = allocation.get_height();
58
59 Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
60
61 //Paint backgroud
62 cr->save();
63 cr->set_source_rgb(BACKGROUND_R, BACKGROUND_G, BACKGROUND_B);
64 cr->paint(); //Fill all with background color
65 cr->restore();
66
67 //Draw a box
68 cr->save();
69 cr->arc( MARGIN + 0.5, MARGIN + m_top_padding + 0.5, RADIUS, M_PI, -0.5*M_PI);
70 cr->line_to(width/6 , MARGIN + m_top_padding + 0.5 - RADIUS);
71 cr->move_to(5*width/6 , MARGIN + m_top_padding + 0.5 - RADIUS);
72 cr->line_to(width - 1 - MARGIN - 0.5, MARGIN + m_top_padding + 0.5 - RADIUS);
73 cr->arc( width - 1 - MARGIN - 0.5, MARGIN + m_top_padding + 0.5, RADIUS, -0.5*M_PI, 0);
74 cr->line_to(width - 1 - MARGIN - 0.5 + RADIUS, height - 1 - MARGIN - 0.5);
75 cr->arc( width - 1 - MARGIN - 0.5, height - 1 - MARGIN - 0.5, RADIUS, 0.0, 0.5*M_PI);
76 cr->line_to( MARGIN + 0.5, height - 1 - MARGIN - 0.5 + RADIUS);
77 cr->arc( MARGIN + 0.5, height - 1 - MARGIN - 0.5, RADIUS, 0.5*M_PI, M_PI);
78 cr->line_to( MARGIN + 0.5 - RADIUS, MARGIN + m_top_padding + 0.5 );
79
80 cr->set_line_width(1);
81 cr->set_source_rgba(1,1,1, 0.3);
82 cr->stroke();
83 cr->restore();
84
85 //Draw Text
86 cr->save();
87 Glib::RefPtr<Pango::Layout> pangoLayout = Pango::Layout::create(cr);
88 Pango::FontDescription font_desc("sans 12px");
89 pangoLayout->set_font_description(font_desc);
90 pangoLayout->set_text(m_title);
91
92 int stringWidth, stringHeight;
93 pangoLayout->get_pixel_size(stringWidth, stringHeight);
94
95 //and text
96 cr->move_to(0.5*(width - stringWidth), m_top_padding - 0.5*stringHeight );
97 cr->set_source_rgba(0.9, 0.9, 0.9, 0.7);
98 pangoLayout->show_in_cairo_context(cr);
99 cr->stroke();
100 cr->restore();
101 }
102
103 return ret;
104 }
105
106
107