2004-09-19 14:24:53 -06:00
/* Copyright (C) 2004 Bart
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Library General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*/
# include "../include/Dialog_Partition_New.h"
namespace GParted
{
2004-10-06 09:32:40 -06:00
Dialog_Partition_New : : Dialog_Partition_New ( )
2004-09-19 14:24:53 -06:00
{
/*TO TRANSLATORS: dialogtitle */
this - > set_title ( _ ( " Create new Partition " ) ) ;
Set_Resizer ( false ) ;
Set_Confirm_Button ( NEW ) ;
//set partition color
2004-10-30 16:17:20 -06:00
color_temp . set ( Get_Color ( " ext2 " ) ) ;
2004-09-19 14:24:53 -06:00
frame_resizer_base - > set_rgb_partition_color ( color_temp ) ;
//set the resizer..
frame_resizer_base - > set_x_start ( 0 ) ;
2004-10-06 09:32:40 -06:00
frame_resizer_base - > set_x_end ( 500 ) ;
2004-09-19 14:24:53 -06:00
frame_resizer_base - > set_used ( 0 ) ;
}
2004-10-06 09:32:40 -06:00
void Dialog_Partition_New : : Set_Data ( const Partition & partition , bool any_extended , unsigned short new_count , const std : : vector < FS > & FILESYSTEMS )
2004-09-19 14:24:53 -06:00
{
2004-10-06 09:32:40 -06:00
this - > new_count = new_count ;
this - > selected_partition = partition ;
this - > FILESYSTEMS = FILESYSTEMS ;
2004-11-21 14:49:38 -07:00
this - > FILESYSTEMS . erase ( this - > FILESYSTEMS . end ( ) ) ; //remove dummy "unknown"
2004-10-06 09:32:40 -06:00
FS fs ; fs . filesystem = " extended " ;
this - > FILESYSTEMS . push_back ( fs ) ;
2004-09-19 14:24:53 -06:00
//add table with selection menu;s...
table_create . set_border_width ( 10 ) ;
table_create . set_row_spacings ( 5 ) ;
hbox_main . pack_start ( table_create , Gtk : : PACK_SHRINK ) ;
/*TO TRANSLATORS: used as label for a list of choices. Create as: <optionmenu with choices> */
2004-10-06 09:32:40 -06:00
table_create . attach ( * mk_label ( ( Glib : : ustring ) _ ( " Create as: " ) + " \t " ) , 0 , 1 , 0 , 1 , Gtk : : FILL ) ;
2004-09-19 14:24:53 -06:00
//fill partitiontype menu
2004-10-01 12:36:12 -06:00
menu_type . items ( ) . push_back ( Gtk : : Menu_Helpers : : MenuElem ( _ ( " Primary Partition " ) ) ) ;
menu_type . items ( ) . push_back ( Gtk : : Menu_Helpers : : MenuElem ( _ ( " Logical Partition " ) ) ) ;
menu_type . items ( ) . push_back ( Gtk : : Menu_Helpers : : MenuElem ( _ ( " Extended Partition " ) ) ) ;
2004-09-19 14:24:53 -06:00
//determine which PartitionType is allowed
if ( partition . inside_extended )
{
2004-10-06 09:32:40 -06:00
menu_type . items ( ) [ 0 ] . set_sensitive ( false ) ;
menu_type . items ( ) [ 2 ] . set_sensitive ( false ) ;
2004-09-19 14:24:53 -06:00
menu_type . set_active ( 1 ) ;
}
else
{
2004-10-06 09:32:40 -06:00
menu_type . items ( ) [ 1 ] . set_sensitive ( false ) ;
if ( any_extended )
menu_type . items ( ) [ 2 ] . set_sensitive ( false ) ;
2004-09-19 14:24:53 -06:00
}
optionmenu_type . set_menu ( menu_type ) ;
2004-10-18 04:52:55 -06:00
optionmenu_type . set_size_request ( 160 , - 1 ) ; //160 is the ideal width for this table column, (when one widget is set, the rest wil take this width as well)
2004-09-19 14:24:53 -06:00
optionmenu_type . signal_changed ( ) . connect ( sigc : : bind < bool > ( sigc : : mem_fun ( * this , & Dialog_Partition_New : : optionmenu_changed ) , true ) ) ;
table_create . attach ( optionmenu_type , 1 , 2 , 0 , 1 , Gtk : : FILL ) ;
//filesystems to choose from
2004-10-06 09:32:40 -06:00
table_create . attach ( * mk_label ( ( Glib : : ustring ) _ ( " Filesystem: " ) + " \t " ) , 0 , 1 , 1 , 2 , Gtk : : FILL ) ;
2004-09-19 14:24:53 -06:00
Build_Filesystems_Menu ( ) ;
optionmenu_filesystem . set_menu ( menu_filesystem ) ;
optionmenu_filesystem . signal_changed ( ) . connect ( sigc : : bind < bool > ( sigc : : mem_fun ( * this , & Dialog_Partition_New : : optionmenu_changed ) , false ) ) ;
table_create . attach ( optionmenu_filesystem , 1 , 2 , 1 , 2 , Gtk : : FILL ) ;
//set some widely used values...
START = partition . sector_start ;
total_length = partition . sector_end - partition . sector_start ;
TOTAL_MB = this - > selected_partition . Get_Length_MB ( ) ;
2004-10-06 09:32:40 -06:00
MB_PER_PIXEL = ( double ) TOTAL_MB / 500 ;
2004-09-19 14:24:53 -06:00
//set spinbuttons
GRIP = true ; //prevents on spinbutton_changed from getting activated prematurely
spinbutton_before . set_range ( 0 , TOTAL_MB - 1 ) ; //mind the -1 !!
spinbutton_before . set_value ( 0 ) ;
spinbutton_size . set_range ( 1 , TOTAL_MB ) ;
spinbutton_size . set_value ( TOTAL_MB ) ;
2004-10-06 09:32:40 -06:00
spinbutton_after . set_range ( 0 , TOTAL_MB - 1 ) ; //mind the -1 !!
2004-09-19 14:24:53 -06:00
spinbutton_after . set_value ( 0 ) ;
GRIP = false ;
//set contents of label_minmax
2004-10-02 05:34:18 -06:00
Set_MinMax_Text ( 1 , TOTAL_MB ) ;
2004-09-19 14:24:53 -06:00
this - > show_all_children ( ) ;
}
Partition Dialog_Partition_New : : Get_New_Partition ( )
{
Partition part_temp ;
2004-09-21 09:25:13 -06:00
PartitionType part_type = GParted : : UNALLOCATED ; //paranoia ;P
2004-09-19 14:24:53 -06:00
Sector new_start , new_end ;
2004-11-17 06:00:25 -07:00
switch ( optionmenu_type . get_history ( ) )
2004-09-19 14:24:53 -06:00
{
2004-10-06 09:32:40 -06:00
case 0 : part_type = GParted : : PRIMARY ; break ;
case 1 : part_type = GParted : : LOGICAL ; break ;
case 2 : part_type = GParted : : EXTENDED ; break ;
2004-09-19 14:24:53 -06:00
}
2004-11-17 06:00:25 -07:00
new_start = START + ( Sector ) ( spinbutton_before . get_value ( ) * MEGABYTE ) ;
new_end = new_start + ( Sector ) ( spinbutton_size . get_value ( ) * MEGABYTE ) ;
2004-09-19 14:24:53 -06:00
//due to loss of precision during calcs from Sector -> MB and back, it is possible the new partition thinks it's bigger then it can be. Here we try to solve this.
if ( new_start < selected_partition . sector_start )
new_start = selected_partition . sector_start ;
if ( new_end > selected_partition . sector_end )
new_end = selected_partition . sector_end ;
2004-09-25 08:12:07 -06:00
part_temp . status = GParted : : STAT_NEW ;
2004-11-17 06:00:25 -07:00
part_temp . Set ( String : : ucompose ( _ ( " New Partition #%1 " ) , new_count ) ,
new_count , part_type ,
FILESYSTEMS [ optionmenu_filesystem . get_history ( ) ] . filesystem ,
new_start , new_end ,
selected_partition . inside_extended , false ) ;
2004-09-19 14:24:53 -06:00
2004-09-25 08:12:07 -06:00
//grow new partition a bit if freespaces are < 1 MB
2004-09-19 14:24:53 -06:00
if ( ( part_temp . sector_start - selected_partition . sector_start ) < MEGABYTE )
part_temp . sector_start = selected_partition . sector_start ;
if ( ( selected_partition . sector_end - part_temp . sector_end ) < MEGABYTE )
part_temp . sector_end = selected_partition . sector_end ;
2004-11-06 04:55:03 -07:00
//if new is extended...
if ( part_temp . type = = GParted : : EXTENDED )
{
Partition UNALLOCATED ;
UNALLOCATED . Set_Unallocated ( part_temp . sector_start , part_temp . sector_end , true ) ;
part_temp . logicals . push_back ( UNALLOCATED ) ;
}
2004-09-19 14:24:53 -06:00
return part_temp ;
}
2004-10-06 09:32:40 -06:00
void Dialog_Partition_New : : optionmenu_changed ( bool type )
2004-09-19 14:24:53 -06:00
{
//optionmenu_type
if ( type )
{
if ( optionmenu_type . get_history ( ) = = GParted : : EXTENDED )
{
menu_filesystem . items ( ) . push_back ( Gtk : : Menu_Helpers : : MenuElem ( " extended " ) ) ;
2004-11-21 14:49:38 -07:00
optionmenu_filesystem . set_history ( 6 ) ;
2004-09-19 14:24:53 -06:00
optionmenu_filesystem . set_sensitive ( false ) ;
}
2004-10-06 09:32:40 -06:00
else if ( menu_filesystem . items ( ) . size ( ) > 6 )
2004-09-19 14:24:53 -06:00
{
menu_filesystem . items ( ) . remove ( menu_filesystem . items ( ) . back ( ) ) ;
optionmenu_filesystem . set_sensitive ( true ) ;
2004-11-21 14:49:38 -07:00
optionmenu_filesystem . set_history ( 0 ) ;
2004-09-19 14:24:53 -06:00
}
}
//optionmenu_filesystem
if ( ! type )
{
2004-10-06 09:32:40 -06:00
//needed vor upper limit check (see also Dialog_Base_Partition::on_signal_resize )
//BART: i don't understand previous sentence, but i think this one's needed for correct color..
selected_partition . filesystem = FILESYSTEMS [ optionmenu_filesystem . get_history ( ) ] . filesystem ;
2004-09-19 14:24:53 -06:00
//set new spinbutton ranges
long MIN , MAX ;
switch ( optionmenu_filesystem . get_history ( ) )
{
2004-10-06 09:32:40 -06:00
case 2 : MIN = 32 ;
2004-10-02 03:39:16 -06:00
TOTAL_MB > 1023 ? MAX = 1023 : MAX = TOTAL_MB ;
break ;
2004-10-06 09:32:40 -06:00
case 3 : MIN = 256 ;
MAX = TOTAL_MB ;
break ;
case 5 : MIN = 40 ;
2004-10-02 03:39:16 -06:00
MAX = TOTAL_MB ;
break ;
2004-10-06 09:32:40 -06:00
default : MIN = 1 ;
2004-10-02 03:39:16 -06:00
MAX = TOTAL_MB ;
2004-09-19 14:24:53 -06:00
}
spinbutton_before . set_range ( 0 , TOTAL_MB - MIN ) ;
spinbutton_size . set_range ( MIN , MAX ) ;
spinbutton_after . set_range ( 0 , TOTAL_MB - MIN ) ;
//set contents of label_minmax
2004-10-02 05:34:18 -06:00
Set_MinMax_Text ( MIN , MAX ) ;
2004-09-19 14:24:53 -06:00
}
//set fitting resizer colors
//backgroundcolor..
2004-10-06 09:32:40 -06:00
optionmenu_type . get_history ( ) = = 2 ? color_temp . set ( " darkgrey " ) : color_temp . set ( " white " ) ;
2004-09-19 14:24:53 -06:00
frame_resizer_base - > override_default_rgb_unused_color ( color_temp ) ;
//partitioncolor..
2004-10-30 16:17:20 -06:00
color_temp . set ( Get_Color ( FILESYSTEMS [ optionmenu_filesystem . get_history ( ) ] . filesystem ) ) ;
2004-09-19 14:24:53 -06:00
frame_resizer_base - > set_rgb_partition_color ( color_temp ) ;
frame_resizer_base - > Draw_Partition ( ) ;
}
void Dialog_Partition_New : : Build_Filesystems_Menu ( )
{
2004-10-06 09:32:40 -06:00
//fill the filesystem menu with the filesystems (except for extended)
for ( unsigned int t = 0 ; t < FILESYSTEMS . size ( ) - 1 ; t + + )
{
menu_filesystem . items ( ) . push_back ( Gtk : : Menu_Helpers : : MenuElem ( FILESYSTEMS [ t ] . filesystem ) ) ;
menu_filesystem . items ( ) [ t ] . set_sensitive ( FILESYSTEMS [ t ] . create ) ;
}
2004-09-19 14:24:53 -06:00
2004-10-06 09:32:40 -06:00
//check if selected unallocated is big enough for fs'es with min. size
//fat16
if ( this - > selected_partition . Get_Length_MB ( ) < 32 )
menu_filesystem . items ( ) [ 2 ] . set_sensitive ( false ) ;
//fat32
if ( this - > selected_partition . Get_Length_MB ( ) < 256 )
menu_filesystem . items ( ) [ 3 ] . set_sensitive ( false ) ;
//reiserfs
if ( this - > selected_partition . Get_Length_MB ( ) < 40 )
menu_filesystem . items ( ) [ 5 ] . set_sensitive ( false ) ;
2004-09-19 14:24:53 -06:00
}
} //GParted