/core/externals/google-toolbox-for-mac/XcodePlugin/GTMXcodePreferences.m

http://macfuse.googlecode.com/ · Objective C · 108 lines · 58 code · 15 blank · 35 comment · 7 complexity · 91815bd9a087118a46dec000b83186a3 MD5 · raw file

  1. //
  2. // GTMXcodePreferences.m
  3. //
  4. // Copyright 2007-2009 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMXcodePreferences.h"
  19. #import "GTMXcodePlugin.h"
  20. #import "GTMDefines.h"
  21. NSString *GTMXcodePreferencesMenuItemPrefChanged
  22. = @"GTMXcodePreferencesMenuItemPrefChanged";
  23. NSString *GTMXcodeCorrectWhiteSpaceOnSave
  24. = @"GTMXcodeCorrectWhiteSpaceOnSave";
  25. NSString *GTMXCodeSuppressMenuItemIcon
  26. = @"GTMXCodeSuppressMenuItemIcon";
  27. @implementation GTMXcodePreferences
  28. // Set our minimum size for the pane
  29. - (NSSize)minModuleSize {
  30. return NSMakeSize(268, 80);
  31. }
  32. // Return our nice little icon
  33. - (id)imageForPreferenceNamed:(id)parameter1 {
  34. NSBundle *bundle = [GTMXcodePlugin pluginBundle];
  35. NSString *path = [bundle pathForImageResource:@"GTM"];
  36. NSImage *image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
  37. [image setScalesWhenResized:YES];
  38. [image setSize:NSMakeSize(32, 32)];
  39. return image;
  40. }
  41. // This gets called everytime preferences are pulled up so that we can
  42. // set up our state.
  43. - (void)initializeFromDefaults {
  44. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  45. // Handle menuitem icon (pref is suppress, button is show)
  46. // NOTE: this preference is negative, but the UI works as the positive, this
  47. // is done so the lack of the preference (ie-the default) will turn on the
  48. // icons.
  49. NSInteger state = NSOffState;
  50. if (![defaults boolForKey:GTMXCodeSuppressMenuItemIcon]) {
  51. state = NSOnState;
  52. }
  53. [showImageOnMenuItems_ setState:state];
  54. state = [defaults boolForKey:GTMXcodeCorrectWhiteSpaceOnSave] ? NSOnState
  55. : NSOffState;
  56. [correctWhiteSpace_ setState:state];
  57. }
  58. // This gets called on Apply or OK is "hasChangesPending" returns YES.
  59. - (void)saveChanges {
  60. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  61. // Handle menuitem icon (pref is suppress, button is show)
  62. // NOTE: this preference is negative, but the UI works as the positive, this
  63. // is done so the lack of the preference (ie-the default) will turn on the
  64. // icons.
  65. BOOL newSetting = ([showImageOnMenuItems_ state] == NSOnState);
  66. BOOL oldSetting = ![defaults boolForKey:GTMXCodeSuppressMenuItemIcon];
  67. if (newSetting != oldSetting) {
  68. if (newSetting) {
  69. [defaults removeObjectForKey:GTMXCodeSuppressMenuItemIcon];
  70. } else {
  71. [defaults setBool:YES forKey:GTMXCodeSuppressMenuItemIcon];
  72. }
  73. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  74. [nc postNotificationName:GTMXcodePreferencesMenuItemPrefChanged
  75. object:self];
  76. }
  77. BOOL setting = ([correctWhiteSpace_ state] == NSOnState);
  78. [defaults setBool:setting forKey:GTMXcodeCorrectWhiteSpaceOnSave];
  79. // save out our settings
  80. [defaults synchronize];
  81. }
  82. // Currently we'll always return YES as it doesn't hurt at all.
  83. - (BOOL)hasChangesPending {
  84. return YES;
  85. }
  86. + (BOOL)showImageOnMenuItems {
  87. // NOTE: this preference is negative, but the UI works as the positive, this
  88. // is done so the lack of the preference (ie-the default) will turn on the
  89. // icons.
  90. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  91. return ![defaults boolForKey:GTMXCodeSuppressMenuItemIcon];
  92. }
  93. @end