CCUIPasswordTextField.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 zilongshanren
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #import "ui/UIEditBox/Mac/CCUIPasswordTextField.h"
  22. #include "ui/UIEditBox/Mac/CCUITextFieldFormatter.h"
  23. @interface RSVerticallyCenteredSecureTextFieldCell : NSSecureTextFieldCell
  24. {
  25. BOOL mIsEditingOrSelecting;
  26. }
  27. @end
  28. @implementation RSVerticallyCenteredSecureTextFieldCell
  29. - (NSRect)drawingRectForBounds:(NSRect)theRect
  30. {
  31. // Get the parent's idea of where we should draw
  32. NSRect newRect = [super drawingRectForBounds:theRect];
  33. // When the text field is being
  34. // edited or selected, we have to turn off the magic because it screws up
  35. // the configuration of the field editor. We sneak around this by
  36. // intercepting selectWithFrame and editWithFrame and sneaking a
  37. // reduced, centered rect in at the last minute.
  38. if (mIsEditingOrSelecting == NO)
  39. {
  40. // Get our ideal size for current text
  41. NSSize textSize = [self cellSizeForBounds:theRect];
  42. // Center that in the proposed rect
  43. float heightDelta = newRect.size.height - textSize.height;
  44. if (heightDelta > 0)
  45. {
  46. newRect.size.height -= heightDelta;
  47. newRect.origin.y += (heightDelta / 2);
  48. }
  49. }
  50. return newRect;
  51. }
  52. - (void)selectWithFrame:(NSRect)aRect
  53. inView:(NSView *)controlView
  54. editor:(NSText *)textObj
  55. delegate:(id)anObject
  56. start:(long)selStart
  57. length:(long)selLength
  58. {
  59. aRect = [self drawingRectForBounds:aRect];
  60. mIsEditingOrSelecting = YES;
  61. [super selectWithFrame:aRect
  62. inView:controlView
  63. editor:textObj
  64. delegate:anObject
  65. start:selStart
  66. length:selLength];
  67. mIsEditingOrSelecting = NO;
  68. }
  69. - (void)editWithFrame:(NSRect)aRect
  70. inView:(NSView *)controlView
  71. editor:(NSText *)textObj
  72. delegate:(id)anObject
  73. event:(NSEvent *)theEvent
  74. {
  75. aRect = [self drawingRectForBounds:aRect];
  76. mIsEditingOrSelecting = YES;
  77. [super editWithFrame:aRect
  78. inView:controlView
  79. editor:textObj
  80. delegate:anObject
  81. event:theEvent];
  82. mIsEditingOrSelecting = NO;
  83. }
  84. @end
  85. @interface CCUIPasswordTextField()
  86. {
  87. }
  88. @end
  89. @implementation CCUIPasswordTextField
  90. -(id) initWithFrame:(NSRect)frameRect
  91. {
  92. if (self = [super initWithFrame:frameRect]) {
  93. [self setLineBreakMode:NSLineBreakByTruncatingTail];
  94. }
  95. return self;
  96. }
  97. -(void)dealloc
  98. {
  99. [super dealloc];
  100. }
  101. +(void)load
  102. {
  103. [self setCellClass:[RSVerticallyCenteredSecureTextFieldCell class]];
  104. }
  105. -(void)ccui_setPlaceholderFont:(NSFont *)font
  106. {
  107. //TODO:
  108. }
  109. -(NSString*)ccui_placeholder
  110. {
  111. return self.placeholderString;
  112. }
  113. -(NSFont*)ccui_placeholderFont
  114. {
  115. return [NSFont systemFontOfSize:self.bounds.size.height * 3.0 / 2.0];
  116. }
  117. -(NSColor*)ccui_placeholderColor
  118. {
  119. return [NSColor whiteColor];
  120. }
  121. -(void)ccui_setPlaceholder:(NSString *)text
  122. {
  123. //TODO:
  124. }
  125. -(void)ccui_setPlaceholderColor:(NSColor *)color
  126. {
  127. //TODO;
  128. }
  129. #pragma mark - CCUITextInput
  130. - (NSString *)ccui_text
  131. {
  132. return self.stringValue;
  133. }
  134. - (void)ccui_setText:(NSString *)ccui_text
  135. {
  136. self.stringValue = ccui_text;
  137. }
  138. - (NSColor *)ccui_textColor
  139. {
  140. return self.textColor;
  141. }
  142. - (void)ccui_setTextColor:(NSColor *)ccui_textColor
  143. {
  144. self.textColor = ccui_textColor;
  145. }
  146. - (NSFont *)ccui_font
  147. {
  148. return self.font;
  149. }
  150. - (void)ccui_setFont:(NSFont *)ccui_font
  151. {
  152. self.font = ccui_font;
  153. }
  154. - (NSTextAlignment)ccui_alignment
  155. {
  156. return self.alignment;
  157. }
  158. - (void)ccui_setTextHorizontalAlignment:(NSTextAlignment)ccui_alignment
  159. {
  160. self.alignment = ccui_alignment;
  161. }
  162. - (void)ccui_setDelegate:(id<NSTextFieldDelegate,NSTextViewDelegate>)delegate
  163. {
  164. self.delegate = delegate;
  165. }
  166. - (void)ccui_setMaxLength:(int)length
  167. {
  168. id formater = [[[CCUITextFieldFormatter alloc]init] autorelease];
  169. [formater setMaximumLength:length];
  170. [self setFormatter:formater];
  171. }
  172. - (int)ccui_maxLength
  173. {
  174. return [self.formatter maximumLength];
  175. }
  176. @end