Display the current directory in unicode

rename function Print_in_window_utf16() to Print_in_window_unicode()
This commit is contained in:
Thomas Bernard 2018-02-12 14:16:10 +01:00
parent b882eb9fe4
commit 9a03a54fef
8 changed files with 102 additions and 47 deletions

View File

@ -2009,7 +2009,7 @@ int L_Run(lua_State* L)
return luaL_error(L, "run: too many nested calls (100)"); return luaL_error(L, "run: too many nested calls (100)");
// store the current directory (on the stack) // store the current directory (on the stack)
Get_current_directory(saved_directory,MAX_PATH_CHARACTERS); Get_current_directory(saved_directory, NULL, MAX_PATH_CHARACTERS);
#if defined (__AROS__) #if defined (__AROS__)
// Convert path written on Linux/Windows norms to AROS norms : // Convert path written on Linux/Windows norms to AROS norms :

View File

@ -2,6 +2,7 @@
*/ */
/* Grafx2 - The Ultimate 256-color bitmap paint program /* Grafx2 - The Ultimate 256-color bitmap paint program
Copyright 2018 Thomas Bernard
Copyright 2014 Sergii Pylypenko Copyright 2014 Sergii Pylypenko
Copyright 2011 Pawel Góralski Copyright 2011 Pawel Góralski
Copyright 2009 Franck Charlet Copyright 2009 Franck Charlet
@ -134,7 +135,7 @@ byte Native_filesel(byte load)
// -- Fileselector data // -- Fileselector data
T_Fileselector Filelist; static T_Fileselector Filelist;
/// Selector settings to use, for all functions called by Load_or_save /// Selector settings to use, for all functions called by Load_or_save
T_Selector_settings * Selector; T_Selector_settings * Selector;
@ -540,7 +541,7 @@ void Read_list_of_files(T_Fileselector *list, byte selected_format)
// Après effacement, il ne reste ni fichier ni répertoire dans la liste // Après effacement, il ne reste ni fichier ni répertoire dans la liste
// On lit tous les répertoires: // On lit tous les répertoires:
current_path = Get_current_directory(curdir, MAX_PATH_CHARACTERS); current_path = Get_current_directory(curdir, NULL, MAX_PATH_CHARACTERS);
For_each_directory_entry(current_path, &callback_data, Read_dir_callback); For_each_directory_entry(current_path, &callback_data, Read_dir_callback);
@ -965,7 +966,7 @@ void Display_file_list(T_Fileselector *list, short offset_first,short selector_o
{ {
// Name without icon // Name without icon
if (current_item->Unicode_short_name) if (current_item->Unicode_short_name)
Print_in_window_utf16(8,95+index*8,current_item->Unicode_short_name,text_color,background_color); Print_in_window_unicode(8,95+index*8,current_item->Unicode_short_name,text_color,background_color);
else else
Print_in_window(8,95+index*8,current_item->Short_name,text_color,background_color); Print_in_window(8,95+index*8,current_item->Short_name,text_color,background_color);
} }
@ -1166,11 +1167,10 @@ void Display_bookmark(T_Dropdown_button * Button, int bookmark_number)
void Print_current_directory(void) void Print_current_directory(void)
// //
// Affiche Selector->Directory sur 37 caractères // Shows Selector->Directory on 37 chars
// //
{ {
char converted_name[MAX_PATH_CHARACTERS]; char converted_name[MAX_PATH_CHARACTERS];
char temp_name[MAX_DISPLAYABLE_PATH+1]; // Nom tronqué
int length; // length du répertoire courant int length; // length du répertoire courant
int index; // index de parcours de la chaine complète int index; // index de parcours de la chaine complète
@ -1190,34 +1190,60 @@ void Print_current_directory(void)
Window_rectangle(10,84,37*8,8,MC_Light); Window_rectangle(10,84,37*8,8,MC_Light);
length=strlen(converted_name); if (Selector->Directory_unicode[0] != 0)
if (length>MAX_DISPLAYABLE_PATH) {
{ // Doh! il va falloir tronquer le répertoire (bouh !) length=Unicode_strlen(Selector->Directory_unicode);
if (length>MAX_DISPLAYABLE_PATH)
{ // We need to truncate the directory
word temp_name[MAX_DISPLAYABLE_PATH+1]; // truncated name
// On commence par copier bêtement les 3 premiers caractères (e.g. "C:\") memcpy(temp_name, Selector->Directory_unicode, 3*2); // first 3 chars "C:\"
for (index=0;index<3;index++) Unicode_char_strlcpy(temp_name+3, "...", MAX_DISPLAYABLE_PATH+1-3);
temp_name[index]=converted_name[index];
// On y rajoute 3 petits points: // next we look for a place to fit everything ;)
strcpy(temp_name+3,"..."); for (index=3;index<length;index++)
if ( (Selector->Directory_unicode[index]==PATH_SEPARATOR[0]) &&
(length-index<=MAX_DISPLAYABLE_PATH-6) )
{
// we found the place !
Unicode_strlcpy(temp_name+6,Selector->Directory_unicode+index, MAX_DISPLAYABLE_PATH+1-6);
break;
}
// Ensuite, on cherche un endroit à partir duquel on pourrait loger tout Print_in_window_unicode(10,84,temp_name,MC_Black,MC_Light);
// le reste de la chaine (Ouaaaaaah!!! Vachement fort le mec!!) }
for (index++;index<length;index++) else // The string is short enough
if ( (converted_name[index]==PATH_SEPARATOR[0]) && Print_in_window_unicode(10,84,Selector->Directory_unicode,MC_Black,MC_Light);
(length-index<=MAX_DISPLAYABLE_PATH-6) ) }
{ else
// Ouf: on vient de trouver un endroit dans la chaîne à partir duquel {
// on peut faire la copie: length=strlen(converted_name);
strcpy(temp_name+6,converted_name+index); if (length>MAX_DISPLAYABLE_PATH)
break; { // We need to truncate the directory
} char temp_name[MAX_DISPLAYABLE_PATH+1]; // truncated name
// Enfin, on peut afficher la chaîne tronquée for (index=0;index<3;index++) // copy the first 3 chars "C:\"
Print_in_window(10,84,temp_name,MC_Black,MC_Light); temp_name[index]=converted_name[index];
// Add ...
strcpy(temp_name+3,"...");
// next we look for a place to fit everything ;)
for (index++;index<length;index++)
if ( (converted_name[index]==PATH_SEPARATOR[0]) &&
(length-index<=MAX_DISPLAYABLE_PATH-6) )
{
// we found the place !
strcpy(temp_name+6,converted_name+index);
break;
}
// display truncated string
Print_in_window(10,84,temp_name,MC_Black,MC_Light);
}
else // The string is short enough
Print_in_window(10,84,converted_name,MC_Black,MC_Light);
} }
else // Ahhh! La chaîne peut loger tranquillement dans la fenêtre
Print_in_window(10,84,converted_name,MC_Black,MC_Light);
Update_window_area(10,84,37*8,8); Update_window_area(10,84,37*8,8);
} }
@ -1244,7 +1270,7 @@ void Print_filename_in_fileselector(void)
{ {
word filename_unicode[32]; word filename_unicode[32];
Unicode_strlcpy(filename_unicode, Selector_filename_unicode, 28); // 28 including the terminating 0 Unicode_strlcpy(filename_unicode, Selector_filename_unicode, 28); // 28 including the terminating 0
Print_in_window_utf16(82,48,filename_unicode,MC_Black,MC_Light); Print_in_window_unicode(82,48,filename_unicode,MC_Black,MC_Light);
} }
else else
Print_in_window_limited(82,48,filename,27,MC_Black,MC_Light); Print_in_window_limited(82,48,filename,27,MC_Black,MC_Light);
@ -1628,7 +1654,7 @@ byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context
} }
Change_directory(context->File_directory); Change_directory(context->File_directory);
Get_current_directory(Selector->Directory,MAX_PATH_CHARACTERS); Get_current_directory(Selector->Directory, Selector->Directory_unicode, MAX_PATH_CHARACTERS);
// Affichage des premiers fichiers visibles: // Affichage des premiers fichiers visibles:
Reload_list_of_files(Selector->Format_filter,file_scroller); Reload_list_of_files(Selector->Format_filter,file_scroller);
@ -2176,7 +2202,7 @@ byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context
Extract_filename(previous_directory, Selector->Directory); Extract_filename(previous_directory, Selector->Directory);
} }
Get_current_directory(Selector->Directory,MAX_PATH_CHARACTERS); Get_current_directory(Selector->Directory, Selector->Directory_unicode, MAX_PATH_CHARACTERS);
// read the new directory // read the new directory
Read_list_of_files(&Filelist, Selector->Format_filter); Read_list_of_files(&Filelist, Selector->Format_filter);
Sort_list_of_files(&Filelist); Sort_list_of_files(&Filelist);

View File

@ -545,7 +545,7 @@ void Release_lock_file(const char *file_directory)
remove(lock_filename); remove(lock_filename);
} }
const char * Get_current_directory(char * buf, size_t size) const char * Get_current_directory(char * buf, word * buf_unicode, size_t size)
{ {
#if defined(__MINT__) #if defined(__MINT__)
buf[0] = 'A'+Dgetdrv(); buf[0] = 'A'+Dgetdrv();
@ -554,9 +554,34 @@ const char * Get_current_directory(char * buf, size_t size)
Dgetpath(buf+3,0); Dgetpath(buf+3,0);
strcat(buf,PATH_SEPARATOR); strcat(buf,PATH_SEPARATOR);
if (buf_unicode != NULL)
buf_unicode[0] = 0; // no unicode support
return buf; return buf;
#else #else
return getcwd(buf, size); char * ret = getcwd(buf, size);
#ifdef ENABLE_FILENAMES_ICONV
if (ret != NULL && buf_unicode != NULL)
{
char * input = buf;
size_t inbytesleft = strlen(buf);
char * output = (char *)buf_unicode;
size_t outbytesleft = 2 * (size - 1);
if (cd_utf16 != (iconv_t)-1)
{
size_t r = iconv(cd_utf16, &input, &inbytesleft, &output, &outbytesleft);
if (r != (size_t)-1)
{
output[0] = '\0';
output[1] = '\0';
}
}
}
#else
if (buf_unicode != NULL)
buf_unicode[0] = 0; // no unicode support
#endif
return ret;
#endif #endif
} }

View File

@ -130,7 +130,7 @@ void Release_lock_file(const char *file_directory);
/// ///
/// Return the current directory, equivalent to getcwd() /// Return the current directory, equivalent to getcwd()
const char * Get_current_directory(char * buf, size_t size); const char * Get_current_directory(char * buf, word * buf_unicode, size_t size);
/// ///
/// Change current directory. return 0 for success, -1 in case of error /// Change current directory. return 0 for success, -1 in case of error

View File

@ -473,6 +473,17 @@ int Init_program(int argc,char * argv[])
printf("===============================\n"); printf("===============================\n");
#endif #endif
#ifdef ENABLE_FILENAMES_ICONV
// iconv is used to convert filenames
cd = iconv_open(TOCODE, FROMCODE); // From UTF8 to ANSI
cd_inv = iconv_open(FROMCODE, TOCODE); // From ANSI to UTF8
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
cd_utf16 = iconv_open("UTF-16BE", FROMCODE); // From UTF8 to UTF16
#else
cd_utf16 = iconv_open("UTF-16LE", FROMCODE); // From UTF8 to UTF16
#endif
#endif /* ENABLE_FILENAMES_ICONV */
// On crée dès maintenant les descripteurs des listes de pages pour la page // On crée dès maintenant les descripteurs des listes de pages pour la page
// principale et la page de brouillon afin que leurs champs ne soient pas // principale et la page de brouillon afin que leurs champs ne soient pas
// invalide lors des appels aux multiples fonctions manipulées à // invalide lors des appels aux multiples fonctions manipulées à
@ -489,15 +500,7 @@ int Init_program(int argc,char * argv[])
// Choose directory for settings (read/write) // Choose directory for settings (read/write)
Set_config_directory(program_directory,Config_directory); Set_config_directory(program_directory,Config_directory);
// On détermine le répertoire courant: // On détermine le répertoire courant:
Get_current_directory(Main.selector.Directory,MAX_PATH_CHARACTERS); Get_current_directory(Main.selector.Directory,Main.selector.Directory_unicode,MAX_PATH_CHARACTERS);
#ifdef ENABLE_FILENAMES_ICONV
// Initialisation de iconv
// utilisé pour convertir les noms de fichiers
cd = iconv_open(TOCODE, FROMCODE); // From UTF8 to ANSI
cd_inv = iconv_open(FROMCODE, TOCODE); // From ANSI to UTF8
cd_utf16 = iconv_open("UTF-16LE", FROMCODE); // From UTF8 to UTF16
#endif /* ENABLE_FILENAMES_ICONV */
// On en profite pour le mémoriser dans le répertoire principal: // On en profite pour le mémoriser dans le répertoire principal:
strcpy(Initial_directory,Main.selector.Directory); strcpy(Initial_directory,Main.selector.Directory);

View File

@ -549,6 +549,7 @@ typedef struct T_Selector_settings
short Position; ///< Index of the first file/entry to display in list short Position; ///< Index of the first file/entry to display in list
short Offset; ///< Position of the "highlight" bar in the file list short Offset; ///< Position of the "highlight" bar in the file list
char Directory[MAX_PATH_CHARACTERS]; ///< Directory currently browsed char Directory[MAX_PATH_CHARACTERS]; ///< Directory currently browsed
word Directory_unicode[MAX_PATH_CHARACTERS]; ///< Directory currently browsed
} T_Selector_settings; } T_Selector_settings;
/// structure for Main or Spare page global data /// structure for Main or Spare page global data

View File

@ -762,7 +762,7 @@ void Print_in_window(short x,short y,const char * str,byte text_color,byte backg
} }
/// Draws a string in a window /// Draws a string in a window
void Print_in_window_utf16(short x,short y,const word * str,byte text_color,byte background_color) void Print_in_window_unicode(short x,short y,const word * str,byte text_color,byte background_color)
{ {
short x_pos = x; short x_pos = x;
const word * p = str; const word * p = str;

View File

@ -67,7 +67,7 @@ word Palette_cells_Y(void);
void Print_general(short x,short y,const char * str,byte text_color,byte background_color); void Print_general(short x,short y,const char * str,byte text_color,byte background_color);
void Print_in_window(short x,short y,const char * str,byte text_color,byte background_color); void Print_in_window(short x,short y,const char * str,byte text_color,byte background_color);
void Print_in_window_utf16(short x,short y,const word * str,byte text_color,byte background_color); void Print_in_window_unicode(short x,short y,const word * str,byte text_color,byte background_color);
void Print_in_window_limited(short x,short y,const char * str,byte size,byte text_color,byte background_color); void Print_in_window_limited(short x,short y,const char * str,byte size,byte text_color,byte background_color);
void Print_char_in_window(short x_pos,short y_pos, unsigned int c,byte text_color,byte background_color); void Print_char_in_window(short x_pos,short y_pos, unsigned int c,byte text_color,byte background_color);
void Print_in_menu(const char * str, short position); void Print_in_menu(const char * str, short position);