[Bf-codereview] Blender password text input (issue 6713044)

jiri.hnidek at gmail.com jiri.hnidek at gmail.com
Tue Oct 16 16:07:44 CEST 2012


Reviewers: bf-codereview_blender.org,

Description:
Blender password text input

Please review this at http://codereview.appspot.com/6713044/

Affected files:
   M     source/blender/editors/interface/interface_widgets.c
   M     source/blender/makesrna/RNA_types.h
   M     source/blender/python/intern/bpy_props.c


Index: source/blender/editors/interface/interface_widgets.c
===================================================================
--- source/blender/editors/interface/interface_widgets.c	(revision 51366)
+++ source/blender/editors/interface/interface_widgets.c	(working copy)
@@ -949,6 +949,55 @@
  	glDisable(GL_BLEND);
  }

+/**
+ * \brief This function convert password string that should not be  
displayed
+ * to asterisk representation (e.g. mysecretpasswd -> *************)
+ *
+ * This function convert every UTF-8 character to asterisk character.
+ *
+ * \param drawstr	The pointer at password string, that should not be
+ * displayed.
+ * \param hidstr	The pointer at password string, that could be displayed.
+ * This string has to be allocated and has to be at least UI_MAX_DRAW_STR
+ * length.
+ *
+ */
+static void ui_text_password_from_text(char *drawstr, char *hidstr)
+{
+	int i = 0, j = 0;
+
+	while ((i < UI_MAX_DRAW_STR) && (drawstr[i] != '\0')) {
+		/* UI elements does not display all UTF-8 characters including
+		 * black circle, despite this character is in font file */
+#if 0
+		/* j += BLI_str_utf8_from_unicode(black_circle, &hidstr[j]); */
+
+		/* UTF-8 representation of black circle: 0x25cf, E2 97 8F */
+		hidstr[j] = '\xE2';
+		hidstr[j+1] = '\x97';
+		hidstr[j+2] = '\x8F';
+		j += 3;
+#else
+		/* Use simple asterisk character for this purpose now */
+		hidstr[j] = '*';
+		j++;
+#endif
+
+		/* Skip one UTF-8 character (could be converted to macro in
+		 * string_utf8.c) */
+		if ((drawstr[i] >> 7) == 0) {
+			i++;
+		} else if ((drawstr[i] >> 5) == 6) {
+			i += 2;
+		} else if ((drawstr[i] >> 4) == 14) {
+			i += 3;
+		} else if ((drawstr[i] >> 3) == 30) {
+			i += 4;
+		}
+	}
+	hidstr[j]='\0';
+}
+
  static void ui_text_clip_give_prev_off(uiBut *but)
  {
  	char *prev_utf8 = BLI_str_find_prev_char_utf8(but->drawstr, but->drawstr  
+ but->ofs);
@@ -1280,6 +1329,13 @@
  		return;
  	}

+	/* Is this string password which should not be displayed in UI */
+	if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_PASSWORD) {
+		char hidstr[UI_MAX_DRAW_STR];
+		ui_text_password_from_text(but->drawstr, hidstr);
+		BLI_strncpy(but->drawstr, hidstr, sizeof(hidstr));
+	}
+
  	/* clip but->drawstr to fit in available space */
  	if (but->editstr && but->pos >= 0) {
  		ui_text_clip_cursor(fstyle, but, rect);
Index: source/blender/makesrna/RNA_types.h
===================================================================
--- source/blender/makesrna/RNA_types.h	(revision 51366)
+++ source/blender/makesrna/RNA_types.h	(working copy)
@@ -113,6 +113,7 @@
  	PROP_BYTESTRING = 4, /* a string which should be represented as bytes
  	                      * in python, still NULL terminated though. */
  	PROP_TRANSLATE = 5, /* a string which should be translated */
+	PROP_PASSWORD = 6,	/* a string which should not be displayed in UI */

  	/* numbers */
  	PROP_UNSIGNED = 13,
Index: source/blender/python/intern/bpy_props.c
===================================================================
--- source/blender/python/intern/bpy_props.c	(revision 51366)
+++ source/blender/python/intern/bpy_props.c	(working copy)
@@ -78,6 +78,7 @@
  	{PROP_FILENAME, "FILENAME", 0, "Filename", ""},
  	{PROP_BYTESTRING, "BYTE_STRING", 0, "Byte String", ""},
  	{PROP_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
+	{PROP_PASSWORD, "PASSWORD", 0, "Password", 0},

  	{PROP_NONE, "NONE", 0, "None", ""},
  	{0, NULL, 0, NULL, NULL}};




More information about the Bf-codereview mailing list