/Tools/nib2cib/NSCustomResource.j

http://github.com/cacaodev/cappuccino · Unknown · 137 lines · 113 code · 24 blank · 0 comment · 0 complexity · 28f6c8e5575ccf072c82408f9e9d762f MD5 · raw file

  1. /*
  2. * NSCustomResource.j
  3. * nib2cib
  4. *
  5. * Portions based on NSCustomResource.m (01/08/2009) in Cocotron (http://www.cocotron.org/)
  6. * Copyright (c) 2006-2007 Christopher J. W. Lloyd
  7. *
  8. * Created by Francisco Tolmasky.
  9. * Copyright 2008, 280 North, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. @import <AppKit/_CPCibCustomResource.j>
  26. @import "Nib2CibException.j"
  27. @global CP_NSMapClassName
  28. var FILE = require("file"),
  29. imageSize = require("cappuccino/imagesize").imagesize,
  30. supportedTemplateImages = {
  31. "NSAddTemplate": "CPAddTemplate",
  32. "NSRemoveTemplate": "CPRemoveTemplate",
  33. "NSToolbarShowColors": "CPImageNameColorPanel"
  34. };
  35. @implementation _CPCibCustomResource (NSCoding)
  36. - (id)NS_initWithCoder:(CPCoder)aCoder
  37. {
  38. self = [super init];
  39. if (self)
  40. {
  41. _className = CP_NSMapClassName([aCoder decodeObjectForKey:@"NSClassName"]);
  42. _resourceName = [aCoder decodeObjectForKey:@"NSResourceName"];
  43. var size = CGSizeMakeZero(),
  44. framework = @"",
  45. bundleIdentifier = @"";
  46. if (_resourceName == "NSSwitch" || _resourceName == "NSRadioButton")
  47. return nil;
  48. else if (/^NS[A-Z][A-Za-z]+$/.test(_resourceName))
  49. {
  50. if (supportedTemplateImages[_resourceName])
  51. {
  52. // Defer resolving this path until runtime.
  53. _resourceName = supportedTemplateImages[_resourceName];
  54. }
  55. else
  56. [CPException raise:Nib2CibException format:@"The built in image “%@” is not supported.", _resourceName];
  57. }
  58. else
  59. {
  60. var match = /^(.+)@(.+)$/.exec(_resourceName);
  61. if (match)
  62. {
  63. _resourceName = match[1];
  64. framework = match[2];
  65. }
  66. var resourceInfo = [aCoder resourceInfoForName:_resourceName inFramework:framework];
  67. if (!resourceInfo)
  68. CPLog.warn("Resource \"" + _resourceName + "\" not found in the Resources directories");
  69. else
  70. {
  71. size = imageSize(FILE.canonical(resourceInfo.path)) || CGSizeMakeZero();
  72. framework = resourceInfo.framework;
  73. }
  74. // Account for the fact that an extension may have been inferred.
  75. if (resourceInfo && resourceInfo.path)
  76. {
  77. // Include subdirectories in the name
  78. match = /^.+\/Resources\/(.+)$/.exec(resourceInfo.path)
  79. _resourceName = match[1];
  80. }
  81. }
  82. if (resourceInfo && resourceInfo.path && resourceInfo.framework)
  83. {
  84. var frameworkPath = FILE.dirname(FILE.dirname(resourceInfo.path)),
  85. bundle = [CPBundle bundleWithPath:frameworkPath];
  86. [bundle loadWithDelegate:nil];
  87. bundleIdentifier = [bundle bundleIdentifier] || @"";
  88. }
  89. _properties = @{ @"size":size, @"bundleIdentifier":bundleIdentifier, @"framework":framework };
  90. CPLog.debug(" Resource: %s\n Framework: %s%s\n Path: %s\n Size: %d x %d",
  91. _resourceName,
  92. framework ? framework : "<none>",
  93. bundleIdentifier ? " (" + bundleIdentifier + ")" :
  94. framework ? " (<no bundle identifier>)" : "",
  95. resourceInfo ? FILE.canonical(resourceInfo.path) : "",
  96. size.width,
  97. size.height);
  98. }
  99. return self;
  100. }
  101. @end
  102. @implementation NSCustomResource : _CPCibCustomResource
  103. {
  104. }
  105. - (id)initWithCoder:(CPCoder)aCoder
  106. {
  107. return [self NS_initWithCoder:aCoder];
  108. }
  109. - (Class)classForKeyedArchiver
  110. {
  111. return [_CPCibCustomResource class];
  112. }
  113. @end