PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/extThree20CSSStyle/Sources/TTDefaultCSSStyleSheet.m

https://github.com/GetMoPix/three20
Objective C | 399 lines | 208 code | 111 blank | 80 comment | 7 complexity | 477c5b1e2546a07a575bfd485f1b1475 MD5 | raw file
  1. //
  2. // Copyright 2009-2011 Facebook
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #import "extThree20CSSStyle/TTDefaultCSSStyleSheet.h"
  17. #import "extThree20CSSStyle/TTCSSRuleSet.h"
  18. // extThree20CSSStyle
  19. #import "extThree20CSSStyle/TTCSSStyleSheet.h"
  20. #import "extThree20CSSStyle/TTCSSApplyProtocol.h"
  21. // Core
  22. #import "Three20Core/TTCorePreprocessorMacros.h"
  23. #import "Three20Core/TTDebug.h"
  24. #import "Three20Core/TTGlobalCorePaths.h"
  25. NSString* kDefaultCSSPath = @"extThree20CSSStyle.bundle/stylesheets/default.css";
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. @implementation TTDefaultCSSStyleSheet
  30. @synthesize styleSheet = _styleSheet;
  31. ///////////////////////////////////////////////////////////////////////////////////////////////////
  32. - (id)init {
  33. self = [super init];
  34. if (self) {
  35. [[NSNotificationCenter defaultCenter]
  36. addObserver: self
  37. selector: @selector(didReceiveMemoryWarning:)
  38. name: UIApplicationDidReceiveMemoryWarningNotification
  39. object: nil];
  40. _styleSheet = [[TTCSSStyleSheet alloc] init];
  41. BOOL loadedSuccessfully = [_styleSheet
  42. loadFromFilename:TTPathForBundleResource(kDefaultCSSPath)];
  43. // Test if load succesfully.
  44. if (!loadedSuccessfully) {
  45. [NSException raise:NSInternalInconsistencyException
  46. format:@"%@ fail to load the Default CSS file. "
  47. @"It's very likely that you forgot to add the extThree20CSSStyle.bundle "
  48. @"to your project. If you didn't, ensure that it's being copied in "
  49. @"the 'Copy Bundle Resources' phase.", NSStringFromClass([self class])];
  50. return nil;
  51. }
  52. }
  53. return self;
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. - (void)dealloc {
  57. [[NSNotificationCenter defaultCenter]
  58. removeObserver: self
  59. name: UIApplicationDidReceiveMemoryWarningNotification
  60. object: nil];
  61. TT_RELEASE_SAFELY(_styleSheet);
  62. TT_RELEASE_SAFELY(_cachedCssFiles);
  63. [super dealloc];
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////
  66. //////////////////////////////////////////////////////////////////////////////////////////////////
  67. #pragma mark -
  68. #pragma mark NSNotifications
  69. ///////////////////////////////////////////////////////////////////////////////////////////////////
  70. - (void)didReceiveMemoryWarning:(void*)object {
  71. [self freeMemory];
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////
  74. - (void)freeMemory {
  75. [_styleSheet freeMemory];
  76. [super freeMemory];
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////////////////////////
  79. ///////////////////////////////////////////////////////////////////////////////////////////////////
  80. #pragma mark -
  81. #pragma mark Public
  82. ///////////////////////////////////////////////////////////////////////////////////////////////////
  83. - (BOOL)addStyleSheetFromDisk:(NSString*)filename ignoreCache:(BOOL)cache {
  84. // Check if this file is already cached, also respect if should ignore the cache.
  85. if (!cache && [_cachedCssFiles containsObject:filename]) {
  86. TTDWARNING( @"'%@' is already loaded and cached. Ignoring...", filename );
  87. return NO;
  88. }
  89. TTCSSStyleSheet* styleSheet = [[TTCSSStyleSheet alloc] init];
  90. BOOL loadedSuccessfully = [styleSheet loadFromFilename:filename];
  91. [_styleSheet addStyleSheet:styleSheet];
  92. TT_RELEASE_SAFELY(styleSheet);
  93. ///////////// //////// //////// //////// ////////
  94. // Init cache, if needed.
  95. if ( !_cachedCssFiles )
  96. _cachedCssFiles = [NSMutableSet new];
  97. // Cache if Loaded Successfully.
  98. if ( loadedSuccessfully )
  99. [_cachedCssFiles addObject:filename];
  100. return loadedSuccessfully;
  101. }
  102. ///////////////////////////////////////////////////////////////////////////////////////////////////
  103. - (BOOL)addStyleSheetFromDisk:(NSString*)filename {
  104. return [self addStyleSheetFromDisk:filename ignoreCache:NO];
  105. }
  106. ///////////////////////////////////////////////////////////////////////////////////////////////////
  107. + (TTDefaultCSSStyleSheet*)globalCSSStyleSheet {
  108. TTDASSERT([[TTStyleSheet globalStyleSheet] isKindOfClass:[TTDefaultStyleSheet class]]);
  109. return (TTDefaultCSSStyleSheet*)[TTStyleSheet globalStyleSheet];
  110. }
  111. ///////////////////////////////////////////////////////////////////////////////////////////////////
  112. -(void)applyCssFromSelector:(NSString*)selectorName toObject:(id<TTCSSApplyProtocol>)anObject {
  113. // Assert that the conforms with the protocol.
  114. if ( ![(id)anObject conformsToProtocol:@protocol(TTCSSApplyProtocol)] )
  115. [NSException raise:NSInternalInconsistencyException
  116. format:@"'%@' must conform with the 'TTCSSApplyProtocol' protocol",
  117. NSStringFromClass([(id)anObject class]) ];
  118. // Apply retrieved rules to the object.
  119. [anObject applyCssRules:[self css:selectorName]];
  120. }
  121. ///////////////////////////////////////////////////////////////////////////////////////////////////
  122. ///////////////////////////////////////////////////////////////////////////////////////////////////
  123. #pragma mark -
  124. #pragma mark Common styles
  125. ///////////////////////////////////////////////////////////////////////////////////////////////////
  126. - (UIColor*)textColor {
  127. return [_styleSheet colorWithCssSelector: @"body"
  128. forState: UIControlStateNormal];
  129. }
  130. ///////////////////////////////////////////////////////////////////////////////////////////////////
  131. - (UIColor*)highlightedTextColor {
  132. return [_styleSheet colorWithCssSelector: @"body"
  133. forState: UIControlStateHighlighted];
  134. }
  135. ///////////////////////////////////////////////////////////////////////////////////////////////////
  136. - (UIFont*)font {
  137. return [_styleSheet fontWithCssSelector: @"body"
  138. forState: UIControlStateNormal];
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////////////////////////
  141. - (UIColor*)backgroundColor {
  142. return [_styleSheet backgroundColorWithCssSelector: @"body"
  143. forState: UIControlStateNormal];
  144. }
  145. ///////////////////////////////////////////////////////////////////////////////////////////////////
  146. - (UIColor*)navigationBarTintColor {
  147. return [_styleSheet backgroundColorWithCssSelector: @"navigationBar"
  148. forState: UIControlStateNormal];
  149. }
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////
  151. - (UIColor*)toolbarTintColor {
  152. return [_styleSheet backgroundColorWithCssSelector: @"toolbar"
  153. forState: UIControlStateNormal];
  154. }
  155. ///////////////////////////////////////////////////////////////////////////////////////////////////
  156. - (UIColor*)searchBarTintColor {
  157. return [_styleSheet backgroundColorWithCssSelector: @"searchbar"
  158. forState: UIControlStateNormal];
  159. }
  160. ///////////////////////////////////////////////////////////////////////////////////////////////////
  161. ///////////////////////////////////////////////////////////////////////////////////////////////////
  162. #pragma mark -
  163. #pragma mark Tables
  164. ///////////////////////////////////////////////////////////////////////////////////////////////////
  165. - (UIColor*)tablePlainBackgroundColor {
  166. return [_styleSheet backgroundColorWithCssSelector: @"table"
  167. forState: UIControlStateNormal];
  168. }
  169. ///////////////////////////////////////////////////////////////////////////////////////////////////
  170. - (UIColor*)tableGroupedBackgroundColor {
  171. return [_styleSheet backgroundColorWithCssSelector: @"groupedTable"
  172. forState: UIControlStateNormal];
  173. }
  174. ///////////////////////////////////////////////////////////////////////////////////////////////////
  175. - (UIColor*)searchTableBackgroundColor {
  176. return [_styleSheet backgroundColorWithCssSelector: @"searchTable"
  177. forState: UIControlStateNormal];
  178. }
  179. ///////////////////////////////////////////////////////////////////////////////////////////////////
  180. - (UIColor*)searchTableSeparatorColor {
  181. return [_styleSheet backgroundColorWithCssSelector: @"searchTableSeparator"
  182. forState: UIControlStateNormal];
  183. }
  184. ///////////////////////////////////////////////////////////////////////////////////////////////////
  185. ///////////////////////////////////////////////////////////////////////////////////////////////////
  186. #pragma mark -
  187. #pragma mark Table Items
  188. ///////////////////////////////////////////////////////////////////////////////////////////////////
  189. - (UIColor*)linkTextColor {
  190. return [_styleSheet colorWithCssSelector: @".tableItemLink"
  191. forState: UIControlStateNormal];
  192. }
  193. ///////////////////////////////////////////////////////////////////////////////////////////////////
  194. - (UIColor*)timestampTextColor {
  195. return [_styleSheet colorWithCssSelector: @".tableItemTimestamp"
  196. forState: UIControlStateNormal];
  197. }
  198. ///////////////////////////////////////////////////////////////////////////////////////////////////
  199. - (UIColor*)moreLinkTextColor {
  200. return [_styleSheet colorWithCssSelector: @".moreButton"
  201. forState: UIControlStateNormal];
  202. }
  203. ///////////////////////////////////////////////////////////////////////////////////////////////////
  204. ///////////////////////////////////////////////////////////////////////////////////////////////////
  205. #pragma mark -
  206. #pragma mark Table Headers
  207. ///////////////////////////////////////////////////////////////////////////////////////////////////
  208. - (UIColor*)tableHeaderTextColor {
  209. return [_styleSheet colorWithCssSelector: @".tableHeader"
  210. forState: UIControlStateNormal];
  211. }
  212. ///////////////////////////////////////////////////////////////////////////////////////////////////
  213. - (UIColor*)tableHeaderShadowColor {
  214. return [_styleSheet textShadowColorWithCssSelector: @".tableHeader"
  215. forState: UIControlStateNormal];
  216. }
  217. ///////////////////////////////////////////////////////////////////////////////////////////////////
  218. - (CGSize)tableHeaderShadowOffset {
  219. return [_styleSheet textShadowOffsetWithCssSelector: @".tableHeader"
  220. forState: UIControlStateNormal];
  221. }
  222. ///////////////////////////////////////////////////////////////////////////////////////////////////
  223. - (UIColor*)tableHeaderTintColor {
  224. return [_styleSheet backgroundColorWithCssSelector: @".tableHeader"
  225. forState: UIControlStateNormal];
  226. }
  227. ///////////////////////////////////////////////////////////////////////////////////////////////////
  228. ///////////////////////////////////////////////////////////////////////////////////////////////////
  229. #pragma mark -
  230. #pragma mark Photo Captions
  231. ///////////////////////////////////////////////////////////////////////////////////////////////////
  232. - (UIColor*)photoCaptionTextColor {
  233. return [_styleSheet colorWithCssSelector: @".photoCaption"
  234. forState: UIControlStateNormal];
  235. }
  236. ///////////////////////////////////////////////////////////////////////////////////////////////////
  237. - (UIColor*)photoCaptionTextShadowColor {
  238. return [_styleSheet textShadowColorWithCssSelector: @".photoCaption"
  239. forState: UIControlStateNormal];
  240. }
  241. ///////////////////////////////////////////////////////////////////////////////////////////////////
  242. - (CGSize)photoCaptionTextShadowOffset {
  243. return [_styleSheet textShadowOffsetWithCssSelector: @".photoCaption"
  244. forState: UIControlStateNormal];
  245. }
  246. ///////////////////////////////////////////////////////////////////////////////////////////////////
  247. ///////////////////////////////////////////////////////////////////////////////////////////////////
  248. #pragma mark -
  249. #pragma mark DragRefreshHeader
  250. ///////////////////////////////////////////////////////////////////////////////////////////////////
  251. - (UIFont*)tableRefreshHeaderLastUpdatedFont {
  252. return [_styleSheet fontWithCssSelector: @".dragRefreshHeaderLastUpdated"
  253. forState: UIControlStateNormal];
  254. }
  255. ///////////////////////////////////////////////////////////////////////////////////////////////////
  256. - (UIFont*)tableRefreshHeaderStatusFont {
  257. return [_styleSheet fontWithCssSelector: @".dragRefreshHeaderStatusFont"
  258. forState: UIControlStateNormal];
  259. }
  260. ///////////////////////////////////////////////////////////////////////////////////////////////////
  261. - (UIColor*)tableRefreshHeaderBackgroundColor {
  262. return [_styleSheet backgroundColorWithCssSelector: @".dragRefreshHeader"
  263. forState: UIControlStateNormal];
  264. }
  265. ///////////////////////////////////////////////////////////////////////////////////////////////////
  266. - (UIColor*)tableRefreshHeaderTextColor {
  267. return [_styleSheet colorWithCssSelector: @".dragRefreshHeader"
  268. forState: UIControlStateNormal];
  269. }
  270. ///////////////////////////////////////////////////////////////////////////////////////////////////
  271. - (UIColor*)tableRefreshHeaderTextShadowColor {
  272. return [_styleSheet textShadowColorWithCssSelector: @".dragRefreshHeader"
  273. forState: UIControlStateNormal];
  274. }
  275. ///////////////////////////////////////////////////////////////////////////////////////////////////
  276. - (CGSize)tableRefreshHeaderTextShadowOffset {
  277. return [_styleSheet textShadowOffsetWithCssSelector: @".dragRefreshHeader"
  278. forState: UIControlStateNormal];
  279. }
  280. ///////////////////////////////////////////////////////////////////////////////////////////////////
  281. -(TTCSSRuleSet*)css:(NSString*)selector {
  282. return [_styleSheet css:selector];
  283. }
  284. ///////////////////////////////////////////////////////////////////////////////////////////////////
  285. -(TTCSSRuleSet*)css:(NSString*)selectorName forState:(UIControlState)state {
  286. return [_styleSheet css:selectorName forState:state];
  287. }
  288. @end