[gparted] modern-gtk2: Use Cairo for drawing the partition info (!17)



commit 7fd39932a35918b83ebbeefe2b1bd27c80821fae
Author: Luca Bacci <luca bacci982 gmail com>
Date:   Thu Aug 2 18:41:40 2018 +0200

    modern-gtk2: Use Cairo for drawing the partition info (!17)
    
    Second commit in a series to convert Gdk::GC based drawing to Cairo
    based drawing.  This specific commit makes the transition for the
    graphical partition info widget that is used in the "Information about"
    dialog.
    
    Closes !17 - Gtk2 modernisation

 include/Dialog_Partition_Info.h |  4 +-
 src/Dialog_Partition_Info.cc    | 93 ++++++++++++++++++++++-------------------
 2 files changed, 52 insertions(+), 45 deletions(-)
---
diff --git a/include/Dialog_Partition_Info.h b/include/Dialog_Partition_Info.h
index cf520769..91c731b7 100644
--- a/include/Dialog_Partition_Info.h
+++ b/include/Dialog_Partition_Info.h
@@ -45,8 +45,7 @@ private:
        void init_drawingarea() ;
        void Display_Info();
 
-       //signalhandlers
-       void drawingarea_on_realize();
+       // Signal handler
        bool drawingarea_on_expose( GdkEventExpose *ev );
 
        const Partition & partition;  // (Alias to element in Win_GParted::display_partitions[] vector).
@@ -57,7 +56,6 @@ private:
        Gtk::VBox info_msg_vbox ;
        Gtk::ScrolledWindow info_scrolled ;
 
-       Glib::RefPtr<Gdk::GC> gc;
        Glib::RefPtr<Pango::Layout> pango_layout;
 
        Gdk::Color color_partition, color_used, color_unused, color_unallocated, color_text ;
diff --git a/src/Dialog_Partition_Info.cc b/src/Dialog_Partition_Info.cc
index 0d814473..62cec8ee 100644
--- a/src/Dialog_Partition_Info.cc
+++ b/src/Dialog_Partition_Info.cc
@@ -24,6 +24,7 @@
 
 #include <gtkmm/alignment.h>
 #include <gtkmm/viewport.h>
+#include <gdkmm/general.h>
 
 namespace GParted
 {
@@ -116,41 +117,62 @@ Dialog_Partition_Info::Dialog_Partition_Info( const Partition & partition ) : pa
        this ->show_all_children() ;
 }
 
-void Dialog_Partition_Info::drawingarea_on_realize()
-{
-       gc = Gdk::GC::create( drawingarea .get_window() ) ;
-       
-       drawingarea .get_window() ->set_background( color_partition ) ;
-}
 
 bool Dialog_Partition_Info::drawingarea_on_expose( GdkEventExpose *ev )
 {
+       Glib::RefPtr<Gdk::Window> window = drawingarea.get_window();
+       if (!window)
+               return true;
+
+       Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
+
+       // Clip to the area indicated by the expose event so that we only redraw
+       // the portion of the window that needs to be redrawn.
+       cr->rectangle(ev->area.x, ev->area.y, ev->area.width, ev->area.height);
+       cr->clip();
+
+       Gdk::Cairo::set_source_color(cr, color_partition);
+       cr->rectangle(0, 0, 400, 60);
+       cr->fill();
+
        if ( partition.filesystem != FS_UNALLOCATED )
        {
-               //used
-               gc ->set_foreground( color_used );
-               drawingarea .get_window() ->draw_rectangle( gc, true, BORDER, BORDER, used, 44 ) ;
-               
-               //unused
-               gc ->set_foreground( color_unused );
-               drawingarea .get_window() ->draw_rectangle( gc, true, BORDER + used, BORDER, unused, 44 ) ;
-
-               //unallocated
-               gc ->set_foreground( color_unallocated );
-               drawingarea .get_window() ->draw_rectangle( gc, true, BORDER + used + unused, BORDER, 
unallocated, 44 ) ;
+               // Used
+               Gdk::Cairo::set_source_color(cr, color_used);
+               cr->rectangle(BORDER,
+                             BORDER,
+                             used,
+                             60 - 2 * BORDER);
+               cr->fill();
+
+               // Unused
+               Gdk::Cairo::set_source_color(cr, color_unused);
+               cr->rectangle(BORDER + used,
+                             BORDER,
+                             unused,
+                             60 - 2 * BORDER);
+               cr->fill();
+
+               // Unallocated
+               Gdk::Cairo::set_source_color(cr, color_unallocated);
+               cr->rectangle(BORDER + used + unused,
+                             BORDER,
+                             unallocated,
+                             60 - 2 * BORDER);
+               cr->fill();
        }
-       
-       //text
-       gc ->set_foreground( color_text );
-       drawingarea .get_window() ->draw_layout( gc, 180, BORDER + 6, pango_layout ) ;
-       
+
+       // Text
+       Gdk::Cairo::set_source_color(cr, color_text);
+       cr->move_to(180, BORDER + 6);
+       pango_layout->show_in_cairo_context(cr);
+
        return true;
 }
 
 void Dialog_Partition_Info::init_drawingarea() 
 {
        drawingarea .set_size_request( 400, 60 ) ;
-       drawingarea .signal_realize().connect( sigc::mem_fun(*this, 
&Dialog_Partition_Info::drawingarea_on_realize) ) ;
        drawingarea .signal_expose_event().connect( sigc::mem_fun(*this, 
&Dialog_Partition_Info::drawingarea_on_expose) ) ;
        
        frame = manage( new Gtk::Frame() ) ;
@@ -183,22 +205,13 @@ void Dialog_Partition_Info::init_drawingarea()
                unallocated = 0 ;
        }
        
-       //allocate some colors
-       color_used.set( "#F8F8BA" );
-       this ->get_colormap() ->alloc_color( color_used ) ;
-       
-       color_unused .set( "white" ) ;
-       this ->get_colormap() ->alloc_color( color_unused ) ;
-
-       color_unallocated .set( "darkgrey" ) ;
-       this ->get_colormap() ->alloc_color( color_unallocated ) ;
-
-       color_text .set( "black" );
-       this ->get_colormap() ->alloc_color( color_text ) ;
-
+       // Colors
+       color_used.set("#F8F8BA");
+       color_unused.set("white");
+       color_unallocated.set("darkgrey");
+       color_text.set("black");
        color_partition.set( Utils::get_color( partition.get_filesystem_partition().filesystem ) );
-       this ->get_colormap() ->alloc_color( color_partition ) ;         
-       
+
        //set text of pangolayout
        pango_layout = drawingarea .create_pango_layout( 
                partition .get_path() + "\n" + Utils::format_size( partition .get_sector_length(), partition 
.sector_size ) ) ;
@@ -654,10 +667,6 @@ void Dialog_Partition_Info::Display_Info()
 
 Dialog_Partition_Info::~Dialog_Partition_Info()
 {
-       this ->get_colormap() ->free_color( color_used ) ;
-       this ->get_colormap() ->free_color( color_unused ) ;
-       this ->get_colormap() ->free_color( color_text ) ;
-       this ->get_colormap() ->free_color( color_partition ) ;
 }
 
 } //GParted


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]