[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19281] branches/blender2.5/blender/source /blender/editors/space_file/file_draw.c: * File browser

Matt Ebb matt at mke3.net
Sat Mar 14 06:21:58 CET 2009


Revision: 19281
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19281
Author:   broken
Date:     2009-03-14 06:21:57 +0100 (Sat, 14 Mar 2009)

Log Message:
-----------
* File browser
Columns view now displays nice human-readable file sizes

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/editors/space_file/file_draw.c

Modified: branches/blender2.5/blender/source/blender/editors/space_file/file_draw.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/space_file/file_draw.c	2009-03-14 03:24:23 UTC (rev 19280)
+++ branches/blender2.5/blender/source/blender/editors/space_file/file_draw.c	2009-03-14 05:21:57 UTC (rev 19281)
@@ -29,6 +29,7 @@
 #include <string.h>
 
 #include "BLI_blenlib.h"
+#include "BLI_dynstr.h"
 #include "BLI_storage_types.h"
 #ifdef WIN32
 #include "BLI_winstuff.h"
@@ -187,10 +188,11 @@
 	uiRoundBox(sx, sy - height, sx + width, sy, 6);
 }
 
-#define FILE_SHORTEN_END 0
-#define FILE_SHORTEN_FRONT 1
+#define FILE_SHORTEN_END				0
+#define FILE_SHORTEN_FRONT				1
+#define FILE_SHORTEN_FSIZE				2
+#define FILE_SHORTEN_FSIZE_WIDTHONLY	3
 
-
 static float shorten_string(char* string, float w, int flag)
 {	
 	char temp[FILE_MAX];
@@ -233,6 +235,50 @@
 	return sw;
 }
 
+static float shorten_filesize(char* string, int flag)
+{	
+	float sw = 0;
+	char tmp[FILE_MAX];
+	int slen = strlen(string);
+	char *s = string;
+	int i;
+	float size;
+
+	/* note:
+	 * input file size is stored as a string, 15 chars long including 
+	 * whitespace at the start, and spaces in between sections.
+	 * i.e. the maximum size is 999 999 999 999 bytes.
+	 */
+	
+	/* get rid of whitespace and convert to a float representing size in bytes */
+	for (i=0; i < slen; i++) {
+		s++;
+		if (s[0] != ' ')
+			strncat(tmp, s, 1);
+	}
+	size = atof(tmp);
+	
+	if (size > 1024*1024*1024) {
+		sprintf(tmp, "%.2f GB", size/(1024*1024*1024));	
+	}
+	else if (size > 1024*1024) {
+		sprintf(tmp, "%.1f MB", size/(1024*1024));
+	}
+	else if (size > 1024) {
+		sprintf(tmp, "%d KB", (int)(size/1024));
+	}
+	else {
+		sprintf(tmp, "%d B", (int)size);
+	}
+	
+	sw = UI_GetStringWidth(G.font, tmp, 0);
+	
+	if (flag != FILE_SHORTEN_FSIZE_WIDTHONLY)
+		strcpy(string, tmp);
+	
+	return sw;
+}
+
 static int get_file_icon(struct direntry *file)
 {
 	if (file->type & S_IFDIR)
@@ -278,7 +324,11 @@
 	float x,y;
 
 	BLI_strncpy(fname,string, FILE_MAXFILE);
-	sw = shorten_string(fname, width, flag );
+	if (ELEM(flag, FILE_SHORTEN_END, FILE_SHORTEN_FRONT))
+		sw = shorten_string(fname, width, flag );
+	else if (flag == FILE_SHORTEN_FSIZE)
+		sw = shorten_filesize(fname, flag);
+	
 	soffs = (width - sw) / 2;
 	x = (float)(sx);
 	y = (float)(sy-height);
@@ -502,11 +552,14 @@
 		
 		UI_ThemeColor4(TH_TEXT);
 		
-		sw = UI_GetStringWidth(G.font, file->size, 0);
+		sw = shorten_filesize(file->size, FILE_SHORTEN_FSIZE_WIDTHONLY);
 		file_draw_string(spos, sy, file->relname, layout->tile_w - sw - 5, layout->tile_h, FILE_SHORTEN_END);
 		
 		spos += filelist_maxnamelen(sfile->files);
-		if (params->display != FILE_SHOWSHORT) {
+		if (params->display == FILE_SHOWSHORT) {
+			if (!(file->type & S_IFDIR))
+				file_draw_string(sx + layout->tile_w - layout->tile_border_x - sw, sy, file->size, sw, layout->tile_h, FILE_SHORTEN_FSIZE);
+		} else {
 #if 0 // XXX TODO: add this for non-windows systems
 			/* rwx rwx rwx */
 			spos += 20;
@@ -534,11 +587,11 @@
 			sw = UI_GetStringWidth(G.font, file->time, 0);
 			file_draw_string(spos, sy, file->time, sw, layout->tile_h, FILE_SHORTEN_END); 
 			
-			sw = UI_GetStringWidth(G.font, file->size, 0);
-			spos += 200-sw;
-			file_draw_string(spos, sy, file->size, sw, layout->tile_h, FILE_SHORTEN_END);
-		} else {
-			file_draw_string(sx + layout->tile_w - 2*layout->tile_border_x - sw - 4, sy, file->size, layout->tile_w - layout->tile_border_x - sw - 5, layout->tile_h, FILE_SHORTEN_END);
+			if (!(file->type & S_IFDIR)) {
+				sw = UI_GetStringWidth(G.font, file->size, 0);
+				spos += 200-sw;
+				file_draw_string(spos, sy, file->size, sw, layout->tile_h, FILE_SHORTEN_END);
+			}
 		}
 	}
 }





More information about the Bf-blender-cvs mailing list