1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| <?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* GtkEntry with mask validation
*
* @category Gtk2
* @package Gtk2_EntryMask
* @author Pablo Dall'Oglio <pablo@php.net>
* @author Leonardo Castilhos Thibes <leonardothibes@yahoo.com.br>
* @license LGPL
* @version $Id: EntryMask.class.php,v 1.0 2007/01/30 11:37:43 leonardo Exp $
*/
class EntryMask {
/**
* Characters used to format mask
* @var array
*/
private $chars;
/**
* The Mask applied to the GtkEntry
* @var string
*/
private $mask;
/**
* The GtkEntry object
* @var $object
*/
private $object;
/**
* The Signal handler that is fired when the user types something
* @var integer
*/
private $handler;
/**
* constructor.
*/
public function __construct() {}
/**
* Setando máscara.
*
* @param string $mask the allowed mask
* @param object $object GtkEntry Object
*/
public function setMask($object, $mask) {
/** Obtebdo objeto GtkEntry **/
$this->obj = $object;
/** Obtendo máscara **/
$this->mask = $mask;
/** Setando tamanho máximo do campo **/
$this->obj->set_max_length(strlen(trim($mask)));
/** Array de separadores **/
$this->chars = array('-', '_', '.', ',', '/', '\\', ':', '|', '(', ')', '[', ']', '{', '}');
/** Conectando o sinal "changed" ao metodo onChange **/
$this->handler = $this->obj->connect('changed', array($this, 'onChange'));
}
/**
* whenever the user types something the content is validated according to
* the mask.
*/
public function onChange() {
/** Setando o texto **/
$text = $this->obj->get_text();
/** Removendo separadores **/
$text = $this->unMask($text);
/** Obtendo o tamanho do texto **/
$len = strlen(trim($text));
/** Aplicando a máscara **/
$new = $this->Mask($this->mask, $text);
/** Agendando novo conteúdo **/
Gtk::timeout_add(1, array($this, 'Set'), $new);
Gtk::timeout_add(1, array($this, 'Validate'));
}
/**
* changes the Entry contents without fire "changed" signal
* @param string $text the new text
*/
public function Set($text) {
/** Desconectando o sinal "changed" do metodo "onChange" **/
$this->obj->disconnect($this->handler);
/** Setando o texto do entry **/
$this->obj->set_text($text);
/** Posicionando o cursor no final do texto **/
$this->obj->select_region(-1,-1);
/** Conectando novamente o sinal "changed" no metodo "onChange" **/
$this->handler = $this->obj->connect_after('changed', array($this, 'onChange'));
}
/**
* Validate the content of GtkEntry
*/
public function Validate() {
/** Obtendo texto do objeto **/
$text = $this->obj->get_text();
/** Obtendo máscara **/
$mask = $this->mask;
/** Obtendo tamanho do texto **/
$len = strlen($text);
$text_char = substr($text, $len-1, 1);
$mask_char = substr($mask, $len-1, 1);
/** Comparando os caracteres digitados com a máscara **/
if ($mask_char == '9')
$valid = ereg('([0-9])', $text_char);
elseif ($mask_char == 'a')
$valid = ereg('([a-z])', $text_char);
elseif ($mask_char == 'A')
$valid = ereg('([A-Z])', $text_char);
elseif ($mask_char == 'X')
$valid = (ereg('([a-z])', $text_char) or
ereg('([A-Z])', $text_char) or
ereg('([0-9])', $text_char));
/** Comparando os caracteres digitados com a máscara **/
/** Caracteres que não se aplicam no escopo da máscara são removidos **/
if (!$valid) {
$this->Set(substr($text, 0, -1));
}
}
/**
* put the typed content in the mask format
* @param string $mask the mask
* @param string $text the content
*/
public function Mask($mask, $text) {
/** Run through the mask chars **/
$z = 0;
for ($n=0; $n < strlen($mask); $n++) {
$mask_char = substr($mask, $n, 1);
$text_char = substr($text, $z, 1);
/** Check when has to concatenate with the separator **/
if(in_array($mask_char, $this->chars)) {
if($z < strlen($text)) {
$result .= $mask_char;
}
} else {
$result .= $text_char;
$z++;
}
}
return $result;
}
/**
* removes the mask from text
* @param string $text the content
*/
public function unMask($text) {
/** Run through the content **/
for ($n=0; $n <= strlen($text); $n++) {
$char = substr($text, $n, 1);
/** Check if it's a separator **/
if (!in_array($char, $this->chars)) {
$result .= $char;
}
}
return $result;
}
}
?> |
Partager