NSGraphics_SKExtensions.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // NSGraphics_SKExtensions.m
  3. // Skim
  4. //
  5. // Created by Christiaan Hofman on 10/20/11.
  6. /*
  7. This software is Copyright (c) 2011-2018
  8. Christiaan Hofman. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the
  17. distribution.
  18. - Neither the name of Christiaan Hofman nor the names of any
  19. contributors may be used to endorse or promote products derived
  20. from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #import "NSGraphics_SKExtensions.h"
  34. //#import "NSGeometry_SKExtensions.h"
  35. //#import "NSColor_SKExtensions.h"
  36. void SKDrawResizeHandle(CGContextRef context, NSPoint point, CGFloat radius, BOOL active)
  37. {
  38. CGRect rect = CGRectMake(point.x - radius, point.y - radius, 2 * radius, 2 * radius);
  39. NSColor *color = [[(active ? [NSColor colorWithRed:37/255.0 green:139/255.0 blue:251/255.0 alpha:1] : [NSColor secondarySelectedControlColor]) colorUsingColorSpaceName:NSCalibratedRGBColorSpace] colorWithAlphaComponent:1];
  40. CGContextSetFillColorWithColor(context, [color CGColor]);
  41. CGContextSetStrokeColorWithColor(context, [[NSColor whiteColor] CGColor]);
  42. CGContextSetLineWidth(context, 0.25 * radius);
  43. CGContextFillEllipseInRect(context, rect);
  44. CGContextStrokeEllipseInRect(context, CGRectInset(rect, -0.25 *radius, -0.25 * radius));
  45. }
  46. void SKDrawResizeHandles(CGContextRef context, NSRect rect, CGFloat radius, BOOL active)
  47. {
  48. SKDrawResizeHandle(context, NSMakePoint(NSMinX(rect), NSMidY(rect)), radius, active);
  49. SKDrawResizeHandle(context, NSMakePoint(NSMidX(rect), NSMaxY(rect)), radius, active);
  50. SKDrawResizeHandle(context, NSMakePoint(NSMidX(rect), NSMinY(rect)), radius, active);
  51. SKDrawResizeHandle(context, NSMakePoint(NSMaxX(rect), NSMidY(rect)), radius, active);
  52. SKDrawResizeHandle(context, NSMakePoint(NSMinX(rect), NSMaxY(rect)), radius, active);
  53. SKDrawResizeHandle(context, NSMakePoint(NSMinX(rect), NSMinY(rect)), radius, active);
  54. SKDrawResizeHandle(context, NSMakePoint(NSMaxX(rect), NSMaxY(rect)), radius, active);
  55. SKDrawResizeHandle(context, NSMakePoint(NSMaxX(rect), NSMinY(rect)), radius, active);
  56. }
  57. #pragma mark -
  58. void SKDrawTextFieldBezel(NSRect rect, NSView *controlView) {
  59. static NSTextFieldCell *cell = nil;
  60. if (cell == nil) {
  61. cell = [[NSTextFieldCell alloc] initTextCell:@""];
  62. [cell setBezeled:YES];
  63. }
  64. [[NSColor controlColor] setFill];
  65. NSRectFill(rect);
  66. [cell drawWithFrame:rect inView:controlView];
  67. [cell setControlView:nil];
  68. }
  69. #pragma mark -
  70. #define MIN_BUTTON_WIDTH 82.0
  71. #define MAX_BUTTON_WIDTH 100.0
  72. #define EXTRA_BUTTON_WIDTH 12.0
  73. void SKShiftAndResizeViews(NSArray *views, CGFloat dx, CGFloat dw) {
  74. for (NSView *view in views)
  75. SKShiftAndResizeView(view, dx, dw);
  76. }
  77. void SKShiftAndResizeView(NSView *view, CGFloat dx, CGFloat dw) {
  78. NSRect frame = [view frame];
  79. frame.origin.x += dx;
  80. frame.size.width += dw;
  81. [view setFrame:frame];
  82. }
  83. void SKResizeWindow(NSWindow *window, CGFloat dw) {
  84. NSRect frame = [window frame];
  85. frame.size.width += dw;
  86. [window setFrame:frame display:NO];
  87. }
  88. void SKAutoSizeButtons(NSArray *buttons, BOOL rightAlign) {
  89. if ([buttons count] == 0)
  90. return;
  91. NSButton *button = [buttons objectAtIndex:0];
  92. CGFloat x = rightAlign ? NSMaxX([button frame]) : NSMinX([button frame]);
  93. CGFloat width = 0.0;
  94. for (button in buttons) {
  95. [button sizeToFit];
  96. width = fmax(width, NSWidth([button frame]) + EXTRA_BUTTON_WIDTH);
  97. }
  98. width = fmin(MAX_BUTTON_WIDTH, fmax(MIN_BUTTON_WIDTH, width));
  99. for (button in buttons) {
  100. NSRect frame = [button frame];
  101. frame.size.width = fmax(width, NSWidth(frame) + EXTRA_BUTTON_WIDTH);
  102. if (rightAlign) {
  103. x -= NSWidth(frame);
  104. frame.origin.x = x;
  105. } else {
  106. frame.origin.x = x;
  107. x += NSWidth(frame);
  108. }
  109. [button setFrame:frame];
  110. }
  111. }
  112. CGFloat SKAutoSizeLabelFields(NSArray *labelFields, NSArray *controls, BOOL resizeControls) {
  113. if ([labelFields count] == 0)
  114. return 0.0;
  115. NSControl *control;
  116. NSRect frame;
  117. CGFloat left = CGFLOAT_MAX, width = 0.0, right, dw = -NSMaxX([[labelFields lastObject] frame]);
  118. for (control in labelFields) {
  119. [control sizeToFit];
  120. frame = [control frame];
  121. left = fmin(left, NSMinX(frame));
  122. width = fmax(width, NSWidth(frame));
  123. }
  124. right = left + width;
  125. for (control in labelFields) {
  126. frame = [control frame];
  127. frame.origin.x = right - NSWidth(frame);
  128. [control setFrame:frame];
  129. }
  130. dw += right;
  131. SKShiftAndResizeViews(controls, dw, resizeControls ? -dw : 0.0);
  132. return dw;
  133. }
  134. extern CGFloat SKAutoSizeLabelField(NSControl *labelField, NSControl *control, BOOL resizeControls) {
  135. return SKAutoSizeLabelFields([NSArray arrayWithObjects:labelField, nil], [NSArray arrayWithObjects:control, nil], resizeControls);
  136. }