LinkTextFieldCell.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * $Id$
  3. *
  4. * The iODBC driver manager.
  5. *
  6. * Copyright (C) 1996-2021 OpenLink Software <[email protected]>
  7. * All Rights Reserved.
  8. *
  9. * This software is released under the terms of either of the following
  10. * licenses:
  11. *
  12. * - GNU Library General Public License (see LICENSE.LGPL)
  13. * - The BSD License (see LICENSE.BSD).
  14. *
  15. * Note that the only valid version of the LGPL license as far as this
  16. * project is concerned is the original GNU Library General Public License
  17. * Version 2, dated June 1991.
  18. *
  19. * While not mandated by the BSD license, any patches you make to the
  20. * iODBC source code may be contributed back into the iODBC project
  21. * at your discretion. Contributions will benefit the Open Source and
  22. * Data Access community as a whole. Submissions may be made at:
  23. *
  24. * http://www.iodbc.org
  25. *
  26. *
  27. * GNU Library Generic Public License Version 2
  28. * ============================================
  29. * This library is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU Library General Public
  31. * License as published by the Free Software Foundation; only
  32. * Version 2 of the License dated June 1991.
  33. *
  34. * This library is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  37. * Library General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU Library General Public
  40. * License along with this library; if not, write to the Free
  41. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  42. *
  43. *
  44. * The BSD License
  45. * ===============
  46. * Redistribution and use in source and binary forms, with or without
  47. * modification, are permitted provided that the following conditions
  48. * are met:
  49. *
  50. * 1. Redistributions of source code must retain the above copyright
  51. * notice, this list of conditions and the following disclaimer.
  52. * 2. Redistributions in binary form must reproduce the above copyright
  53. * notice, this list of conditions and the following disclaimer in
  54. * the documentation and/or other materials provided with the
  55. * distribution.
  56. * 3. Neither the name of OpenLink Software Inc. nor the names of its
  57. * contributors may be used to endorse or promote products derived
  58. * from this software without specific prior written permission.
  59. *
  60. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  61. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  62. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  63. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
  64. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  65. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  66. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  67. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  68. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  69. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  70. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  71. */
  72. #import "LinkTextFieldCell.h"
  73. @implementation LinkTextFieldCell
  74. @synthesize clickHandler = _clickHandler;
  75. - (id)copyWithZone:(NSZone *)zone {
  76. LinkTextFieldCell *result = [super copyWithZone:zone];
  77. result->_clickHandler = [_clickHandler copy];
  78. return result;
  79. }
  80. - (void)dealloc
  81. {
  82. [_clickHandler release];
  83. [super dealloc];
  84. }
  85. + (BOOL)prefersTrackingUntilMouseUp
  86. {
  87. return YES;
  88. }
  89. - (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
  90. NSUInteger hitTestResult = [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
  91. if ((hitTestResult & NSCellHitContentArea) != 0) {
  92. hitTestResult |= NSCellHitTrackableArea;
  93. }
  94. return hitTestResult;
  95. }
  96. - (void)_setAttributedStringTextColor:(NSColor *)color {
  97. NSMutableAttributedString *attrValue = [[self attributedStringValue] mutableCopy];
  98. NSRange range = NSMakeRange(0, [attrValue length]);
  99. [attrValue addAttribute:NSForegroundColorAttributeName value:color range:range];
  100. [self setAttributedStringValue:attrValue];
  101. }
  102. - (void)_handleLinkClick {
  103. NSAttributedString *attrValue = [self attributedStringValue];
  104. NSString *link = [attrValue attribute:NSLinkAttributeName atIndex:0 effectiveRange:NULL];
  105. if (link!=nil) {
  106. if (_clickHandler != nil)
  107. _clickHandler(link, self);
  108. else
  109. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:link]];
  110. }
  111. }
  112. - (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)flag {
  113. BOOL result = YES;
  114. NSUInteger hitTestResult = [self hitTestForEvent:theEvent inRect:cellFrame ofView:controlView];
  115. if ((hitTestResult & NSCellHitContentArea) != 0)
  116. {
  117. [self _setAttributedStringTextColor:[NSColor redColor]];
  118. result = [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:flag];
  119. theEvent = [NSApp currentEvent];
  120. hitTestResult = [self hitTestForEvent:theEvent inRect:cellFrame ofView:controlView];
  121. if ((hitTestResult & NSCellHitContentArea) != 0) {
  122. [self _handleLinkClick];
  123. }
  124. }
  125. return result;
  126. }
  127. - (void)setBackgroundStyle:(NSBackgroundStyle)style {
  128. [super setBackgroundStyle:style];
  129. if (style == NSBackgroundStyleDark) {
  130. [self _setAttributedStringTextColor:[NSColor whiteColor]];
  131. }
  132. }
  133. - (NSArray *)accessibilityActionNames
  134. {
  135. return [[super accessibilityActionNames] arrayByAddingObject:NSAccessibilityPressAction];
  136. }
  137. - (void)accessibilityPerformAction:(NSString *)action
  138. {
  139. if ([action isEqualToString:NSAccessibilityPressAction]) {
  140. [self _handleLinkClick];
  141. } else {
  142. [super accessibilityPerformAction:action];
  143. }
  144. }
  145. @end