MAAttachedWindow.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. //
  2. // MAAttachedWindow.m
  3. //
  4. // Created by Matt Gemmell on 27/09/2007.
  5. // Copyright 2007 Magic Aubergine.
  6. //
  7. #import "MAAttachedWindow.h"
  8. #define MAATTACHEDWINDOW_DEFAULT_BACKGROUND_COLOR [NSColor colorWithCalibratedWhite:0.1 alpha:0.75]
  9. #define MAATTACHEDWINDOW_DEFAULT_BORDER_COLOR [NSColor whiteColor]
  10. #define MAATTACHEDWINDOW_SCALE_FACTOR [[NSScreen mainScreen] userSpaceScaleFactor]
  11. @interface MAAttachedWindow (MAPrivateMethods)
  12. // Geometry
  13. - (void)_updateGeometry;
  14. - (MAWindowPosition)_bestSideForAutomaticPosition;
  15. - (float)_arrowInset;
  16. // Drawing
  17. - (void)_updateBackground;
  18. - (NSColor *)_backgroundColorPatternImage;
  19. - (NSBezierPath *)_backgroundPath;
  20. - (void)_appendArrowToPath:(NSBezierPath *)path;
  21. - (void)_redisplay;
  22. @end
  23. @implementation MAAttachedWindow
  24. #pragma mark Initializers
  25. - (MAAttachedWindow *)initWithView:(NSView *)view
  26. attachedToPoint:(NSPoint)point
  27. inWindow:(NSWindow *)window
  28. onSide:(MAWindowPosition)side
  29. atDistance:(float)distance
  30. {
  31. // Insist on having a valid view.
  32. if (!view) {
  33. return nil;
  34. }
  35. // Create dummy initial contentRect for window.
  36. NSRect contentRect = NSZeroRect;
  37. contentRect.size = [view frame].size;
  38. if ((self = [super initWithContentRect:contentRect
  39. styleMask:NSBorderlessWindowMask
  40. backing:NSBackingStoreBuffered
  41. defer:NO])) {
  42. _view = view;
  43. _window = window;
  44. _point = point;
  45. _side = side;
  46. _distance = distance;
  47. // Configure window characteristics.
  48. [super setBackgroundColor:[NSColor clearColor]];
  49. [self setMovableByWindowBackground:NO];
  50. [self setExcludedFromWindowsMenu:YES];
  51. [self setAlphaValue:1.0];
  52. [self setOpaque:NO];
  53. [self setHasShadow:YES];
  54. [self useOptimizedDrawing:YES];
  55. // Set up some sensible defaults for display.
  56. _MABackgroundColor = [MAATTACHEDWINDOW_DEFAULT_BACKGROUND_COLOR copy];
  57. borderColor = [MAATTACHEDWINDOW_DEFAULT_BORDER_COLOR copy];
  58. borderWidth = 2.0;
  59. viewMargin = 2.0;
  60. arrowBaseWidth = 20.0;
  61. arrowHeight = 16.0;
  62. hasArrow = YES;
  63. cornerRadius = 8.0;
  64. drawsRoundCornerBesideArrow = YES;
  65. _resizing = NO;
  66. // Work out what side to put the window on if it's "automatic".
  67. if (_side == MAPositionAutomatic) {
  68. _side = [self _bestSideForAutomaticPosition];
  69. }
  70. // Configure our initial geometry.
  71. [self _updateGeometry];
  72. // Update the background.
  73. [self _updateBackground];
  74. // Add view as subview of our contentView.
  75. [[self contentView] addSubview:_view];
  76. // Subscribe to notifications for when we change size.
  77. [[NSNotificationCenter defaultCenter] addObserver:self
  78. selector:@selector(windowDidResize:)
  79. name:NSWindowDidResizeNotification
  80. object:self];
  81. }
  82. return self;
  83. }
  84. - (MAAttachedWindow *)initWithView:(NSView *)view
  85. attachedToPoint:(NSPoint)point
  86. inWindow:(NSWindow *)window
  87. atDistance:(float)distance
  88. {
  89. return [self initWithView:view attachedToPoint:point
  90. inWindow:window onSide:MAPositionAutomatic
  91. atDistance:distance];
  92. }
  93. - (MAAttachedWindow *)initWithView:(NSView *)view
  94. attachedToPoint:(NSPoint)point
  95. onSide:(MAWindowPosition)side
  96. atDistance:(float)distance
  97. {
  98. return [self initWithView:view attachedToPoint:point
  99. inWindow:nil onSide:side
  100. atDistance:distance];
  101. }
  102. - (MAAttachedWindow *)initWithView:(NSView *)view
  103. attachedToPoint:(NSPoint)point
  104. atDistance:(float)distance
  105. {
  106. return [self initWithView:view attachedToPoint:point
  107. inWindow:nil onSide:MAPositionAutomatic
  108. atDistance:distance];
  109. }
  110. - (MAAttachedWindow *)initWithView:(NSView *)view
  111. attachedToPoint:(NSPoint)point
  112. inWindow:(NSWindow *)window
  113. {
  114. return [self initWithView:view attachedToPoint:point
  115. inWindow:window onSide:MAPositionAutomatic
  116. atDistance:0];
  117. }
  118. - (MAAttachedWindow *)initWithView:(NSView *)view
  119. attachedToPoint:(NSPoint)point
  120. onSide:(MAWindowPosition)side
  121. {
  122. return [self initWithView:view attachedToPoint:point
  123. inWindow:nil onSide:side
  124. atDistance:0];
  125. }
  126. - (MAAttachedWindow *)initWithView:(NSView *)view
  127. attachedToPoint:(NSPoint)point
  128. {
  129. return [self initWithView:view attachedToPoint:point
  130. inWindow:nil onSide:MAPositionAutomatic
  131. atDistance:0];
  132. }
  133. - (void)dealloc
  134. {
  135. [[NSNotificationCenter defaultCenter] removeObserver:self];
  136. [borderColor release];
  137. [_MABackgroundColor release];
  138. [super dealloc];
  139. }
  140. #pragma mark Geometry
  141. - (void)_updateGeometry
  142. {
  143. NSRect contentRect = NSZeroRect;
  144. contentRect.size = [_view frame].size;
  145. // Account for viewMargin.
  146. _viewFrame = NSMakeRect(viewMargin * MAATTACHEDWINDOW_SCALE_FACTOR,
  147. viewMargin * MAATTACHEDWINDOW_SCALE_FACTOR,
  148. [_view frame].size.width, [_view frame].size.height);
  149. contentRect = NSInsetRect(contentRect,
  150. -viewMargin * MAATTACHEDWINDOW_SCALE_FACTOR,
  151. -viewMargin * MAATTACHEDWINDOW_SCALE_FACTOR);
  152. // Account for arrowHeight in new window frame.
  153. // Note: we always leave room for the arrow, even if it currently set to
  154. // not be shown. This is so it can easily be toggled whilst the window
  155. // is visible, without altering the window's frame origin point.
  156. float scaledArrowHeight = arrowHeight * MAATTACHEDWINDOW_SCALE_FACTOR;
  157. switch (_side) {
  158. case MAPositionLeft:
  159. case MAPositionLeftTop:
  160. case MAPositionLeftBottom:
  161. contentRect.size.width += scaledArrowHeight;
  162. break;
  163. case MAPositionRight:
  164. case MAPositionRightTop:
  165. case MAPositionRightBottom:
  166. _viewFrame.origin.x += scaledArrowHeight;
  167. contentRect.size.width += scaledArrowHeight;
  168. break;
  169. case MAPositionTop:
  170. case MAPositionTopLeft:
  171. case MAPositionTopRight:
  172. _viewFrame.origin.y += scaledArrowHeight;
  173. contentRect.size.height += scaledArrowHeight;
  174. break;
  175. case MAPositionBottom:
  176. case MAPositionBottomLeft:
  177. case MAPositionBottomRight:
  178. contentRect.size.height += scaledArrowHeight;
  179. break;
  180. default:
  181. break; // won't happen, but this satisfies gcc with -Wall
  182. }
  183. // Position frame origin appropriately for _side, accounting for arrow-inset.
  184. contentRect.origin = (_window) ? [_window convertBaseToScreen:_point] : _point;
  185. float arrowInset = [self _arrowInset];
  186. float halfWidth = contentRect.size.width / 2.0;
  187. float halfHeight = contentRect.size.height / 2.0;
  188. switch (_side) {
  189. case MAPositionTopLeft:
  190. contentRect.origin.x -= contentRect.size.width - arrowInset;
  191. break;
  192. case MAPositionTop:
  193. contentRect.origin.x -= halfWidth;
  194. break;
  195. case MAPositionTopRight:
  196. contentRect.origin.x -= arrowInset;
  197. break;
  198. case MAPositionBottomLeft:
  199. contentRect.origin.y -= contentRect.size.height;
  200. contentRect.origin.x -= contentRect.size.width - arrowInset;
  201. break;
  202. case MAPositionBottom:
  203. contentRect.origin.y -= contentRect.size.height;
  204. contentRect.origin.x -= halfWidth;
  205. break;
  206. case MAPositionBottomRight:
  207. contentRect.origin.x -= arrowInset;
  208. contentRect.origin.y -= contentRect.size.height;
  209. break;
  210. case MAPositionLeftTop:
  211. contentRect.origin.x -= contentRect.size.width;
  212. contentRect.origin.y -= arrowInset;
  213. break;
  214. case MAPositionLeft:
  215. contentRect.origin.x -= contentRect.size.width;
  216. contentRect.origin.y -= halfHeight;
  217. break;
  218. case MAPositionLeftBottom:
  219. contentRect.origin.x -= contentRect.size.width;
  220. contentRect.origin.y -= contentRect.size.height - arrowInset;
  221. break;
  222. case MAPositionRightTop:
  223. contentRect.origin.y -= arrowInset;
  224. break;
  225. case MAPositionRight:
  226. contentRect.origin.y -= halfHeight;
  227. break;
  228. case MAPositionRightBottom:
  229. contentRect.origin.y -= contentRect.size.height - arrowInset;
  230. break;
  231. default:
  232. break; // won't happen, but this satisfies gcc with -Wall
  233. }
  234. // Account for _distance in new window frame.
  235. switch (_side) {
  236. case MAPositionLeft:
  237. case MAPositionLeftTop:
  238. case MAPositionLeftBottom:
  239. contentRect.origin.x -= _distance;
  240. break;
  241. case MAPositionRight:
  242. case MAPositionRightTop:
  243. case MAPositionRightBottom:
  244. contentRect.origin.x += _distance;
  245. break;
  246. case MAPositionTop:
  247. case MAPositionTopLeft:
  248. case MAPositionTopRight:
  249. contentRect.origin.y += _distance;
  250. break;
  251. case MAPositionBottom:
  252. case MAPositionBottomLeft:
  253. case MAPositionBottomRight:
  254. contentRect.origin.y -= _distance;
  255. break;
  256. default:
  257. break; // won't happen, but this satisfies gcc with -Wall
  258. }
  259. // Reconfigure window and view frames appropriately.
  260. [self setFrame:contentRect display:NO];
  261. [_view setFrame:_viewFrame];
  262. }
  263. - (MAWindowPosition)_bestSideForAutomaticPosition
  264. {
  265. // Get all relevant geometry in screen coordinates.
  266. NSRect screenFrame;
  267. if (_window && [_window screen]) {
  268. screenFrame = [[_window screen] visibleFrame];
  269. } else {
  270. screenFrame = [[NSScreen mainScreen] visibleFrame];
  271. }
  272. NSPoint pointOnScreen = (_window) ? [_window convertBaseToScreen:_point] : _point;
  273. NSSize viewSize = [_view frame].size;
  274. viewSize.width += (viewMargin * MAATTACHEDWINDOW_SCALE_FACTOR) * 2.0;
  275. viewSize.height += (viewMargin * MAATTACHEDWINDOW_SCALE_FACTOR) * 2.0;
  276. MAWindowPosition side = MAPositionBottom; // By default, position us centered below.
  277. float scaledArrowHeight = (arrowHeight * MAATTACHEDWINDOW_SCALE_FACTOR) + _distance;
  278. // We'd like to display directly below the specified point, since this gives a
  279. // sense of a relationship between the point and this window. Check there's room.
  280. if (pointOnScreen.y - viewSize.height - scaledArrowHeight < NSMinY(screenFrame)) {
  281. // We'd go off the bottom of the screen. Try the right.
  282. if (pointOnScreen.x + viewSize.width + scaledArrowHeight >= NSMaxX(screenFrame)) {
  283. // We'd go off the right of the screen. Try the left.
  284. if (pointOnScreen.x - viewSize.width - scaledArrowHeight < NSMinX(screenFrame)) {
  285. // We'd go off the left of the screen. Try the top.
  286. if (pointOnScreen.y + viewSize.height + scaledArrowHeight < NSMaxY(screenFrame)) {
  287. side = MAPositionTop;
  288. }
  289. } else {
  290. side = MAPositionLeft;
  291. }
  292. } else {
  293. side = MAPositionRight;
  294. }
  295. }
  296. float halfWidth = viewSize.width / 2.0;
  297. float halfHeight = viewSize.height / 2.0;
  298. NSRect parentFrame = (_window) ? [_window frame] : screenFrame;
  299. float arrowInset = [self _arrowInset];
  300. // We're currently at a primary side.
  301. // Try to avoid going outwith the parent area in the secondary dimension,
  302. // by checking to see if an appropriate corner side would be better.
  303. switch (side) {
  304. case MAPositionBottom:
  305. case MAPositionTop:
  306. // Check to see if we go beyond the left edge of the parent area.
  307. if (pointOnScreen.x - halfWidth < NSMinX(parentFrame)) {
  308. // We go beyond the left edge. Try using right position.
  309. if (pointOnScreen.x + viewSize.width - arrowInset < NSMaxX(screenFrame)) {
  310. // We'd still be on-screen using right, so use it.
  311. if (side == MAPositionBottom) {
  312. side = MAPositionBottomRight;
  313. } else {
  314. side = MAPositionTopRight;
  315. }
  316. }
  317. } else if (pointOnScreen.x + halfWidth >= NSMaxX(parentFrame)) {
  318. // We go beyond the right edge. Try using left position.
  319. if (pointOnScreen.x - viewSize.width + arrowInset >= NSMinX(screenFrame)) {
  320. // We'd still be on-screen using left, so use it.
  321. if (side == MAPositionBottom) {
  322. side = MAPositionBottomLeft;
  323. } else {
  324. side = MAPositionTopLeft;
  325. }
  326. }
  327. }
  328. break;
  329. case MAPositionRight:
  330. case MAPositionLeft:
  331. // Check to see if we go beyond the bottom edge of the parent area.
  332. if (pointOnScreen.y - halfHeight < NSMinY(parentFrame)) {
  333. // We go beyond the bottom edge. Try using top position.
  334. if (pointOnScreen.y + viewSize.height - arrowInset < NSMaxY(screenFrame)) {
  335. // We'd still be on-screen using top, so use it.
  336. if (side == MAPositionRight) {
  337. side = MAPositionRightTop;
  338. } else {
  339. side = MAPositionLeftTop;
  340. }
  341. }
  342. } else if (pointOnScreen.y + halfHeight >= NSMaxY(parentFrame)) {
  343. // We go beyond the top edge. Try using bottom position.
  344. if (pointOnScreen.y - viewSize.height + arrowInset >= NSMinY(screenFrame)) {
  345. // We'd still be on-screen using bottom, so use it.
  346. if (side == MAPositionRight) {
  347. side = MAPositionRightBottom;
  348. } else {
  349. side = MAPositionLeftBottom;
  350. }
  351. }
  352. }
  353. break;
  354. default:
  355. break; // won't happen, but this satisfies gcc with -Wall
  356. }
  357. return side;
  358. }
  359. - (float)_arrowInset
  360. {
  361. float cornerInset = (drawsRoundCornerBesideArrow) ? cornerRadius : 0;
  362. return (cornerInset + (arrowBaseWidth / 2.0)) * MAATTACHEDWINDOW_SCALE_FACTOR;
  363. }
  364. #pragma mark Drawing
  365. - (void)_updateBackground
  366. {
  367. // Call NSWindow's implementation of -setBackgroundColor: because we override
  368. // it in this class to let us set the entire background image of the window
  369. // as an NSColor patternImage.
  370. NSDisableScreenUpdates();
  371. [super setBackgroundColor:[self _backgroundColorPatternImage]];
  372. if ([self isVisible]) {
  373. [self display];
  374. [self invalidateShadow];
  375. }
  376. NSEnableScreenUpdates();
  377. }
  378. - (NSColor *)_backgroundColorPatternImage
  379. {
  380. NSImage *bg = [[NSImage alloc] initWithSize:[self frame].size];
  381. NSRect bgRect = NSZeroRect;
  382. bgRect.size = [bg size];
  383. [bg lockFocus];
  384. NSBezierPath *bgPath = [self _backgroundPath];
  385. [NSGraphicsContext saveGraphicsState];
  386. [bgPath addClip];
  387. // Draw background.
  388. [_MABackgroundColor set];
  389. [bgPath fill];
  390. // Draw border if appropriate.
  391. if (borderWidth > 0) {
  392. // Double the borderWidth since we're drawing inside the path.
  393. [bgPath setLineWidth:(borderWidth * 2.0) * MAATTACHEDWINDOW_SCALE_FACTOR];
  394. [borderColor set];
  395. [bgPath stroke];
  396. }
  397. [NSGraphicsContext restoreGraphicsState];
  398. [bg unlockFocus];
  399. return [NSColor colorWithPatternImage:[bg autorelease]];
  400. }
  401. - (NSBezierPath *)_backgroundPath
  402. {
  403. /*
  404. Construct path for window background, taking account of:
  405. 1. hasArrow
  406. 2. _side
  407. 3. drawsRoundCornerBesideArrow
  408. 4. arrowBaseWidth
  409. 5. arrowHeight
  410. 6. cornerRadius
  411. */
  412. float scaleFactor = MAATTACHEDWINDOW_SCALE_FACTOR;
  413. float scaledRadius = cornerRadius * scaleFactor;
  414. float scaledArrowWidth = arrowBaseWidth * scaleFactor;
  415. float halfArrowWidth = scaledArrowWidth / 2.0;
  416. NSRect contentArea = NSInsetRect(_viewFrame,
  417. -viewMargin * scaleFactor,
  418. -viewMargin * scaleFactor);
  419. float minX = ceilf(NSMinX(contentArea) * scaleFactor + 0.5f);
  420. float midX = NSMidX(contentArea) * scaleFactor;
  421. float maxX = floorf(NSMaxX(contentArea) * scaleFactor - 0.5f);
  422. float minY = ceilf(NSMinY(contentArea) * scaleFactor + 0.5f);
  423. float midY = NSMidY(contentArea) * scaleFactor;
  424. float maxY = floorf(NSMaxY(contentArea) * scaleFactor - 0.5f);
  425. NSBezierPath *path = [NSBezierPath bezierPath];
  426. [path setLineJoinStyle:NSRoundLineJoinStyle];
  427. // Begin at top-left. This will be either after the rounded corner, or
  428. // at the top-left point if cornerRadius is zero and/or we're drawing
  429. // the arrow at the top-left or left-top without a rounded corner.
  430. NSPoint currPt = NSMakePoint(minX, maxY);
  431. if (scaledRadius > 0 &&
  432. (drawsRoundCornerBesideArrow ||
  433. (_side != MAPositionBottomRight && _side != MAPositionRightBottom))
  434. ) {
  435. currPt.x += scaledRadius;
  436. }
  437. NSPoint endOfLine = NSMakePoint(maxX, maxY);
  438. BOOL shouldDrawNextCorner = NO;
  439. if (scaledRadius > 0 &&
  440. (drawsRoundCornerBesideArrow ||
  441. (_side != MAPositionBottomLeft && _side != MAPositionLeftBottom))
  442. ) {
  443. endOfLine.x -= scaledRadius;
  444. shouldDrawNextCorner = YES;
  445. }
  446. [path moveToPoint:currPt];
  447. // If arrow should be drawn at top-left point, draw it.
  448. if (_side == MAPositionBottomRight) {
  449. [self _appendArrowToPath:path];
  450. } else if (_side == MAPositionBottom) {
  451. // Line to relevant point before arrow.
  452. [path lineToPoint:NSMakePoint(midX - halfArrowWidth, maxY)];
  453. // Draw arrow.
  454. [self _appendArrowToPath:path];
  455. } else if (_side == MAPositionBottomLeft) {
  456. // Line to relevant point before arrow.
  457. [path lineToPoint:NSMakePoint(endOfLine.x - scaledArrowWidth, maxY)];
  458. // Draw arrow.
  459. [self _appendArrowToPath:path];
  460. }
  461. // Line to end of this side.
  462. [path lineToPoint:endOfLine];
  463. // Rounded corner on top-right.
  464. if (shouldDrawNextCorner) {
  465. [path appendBezierPathWithArcFromPoint:NSMakePoint(maxX, maxY)
  466. toPoint:NSMakePoint(maxX, maxY - scaledRadius)
  467. radius:scaledRadius];
  468. }
  469. // Draw the right side, beginning at the top-right.
  470. endOfLine = NSMakePoint(maxX, minY);
  471. shouldDrawNextCorner = NO;
  472. if (scaledRadius > 0 &&
  473. (drawsRoundCornerBesideArrow ||
  474. (_side != MAPositionTopLeft && _side != MAPositionLeftTop))
  475. ) {
  476. endOfLine.y += scaledRadius;
  477. shouldDrawNextCorner = YES;
  478. }
  479. // If arrow should be drawn at right-top point, draw it.
  480. if (_side == MAPositionLeftBottom) {
  481. [self _appendArrowToPath:path];
  482. } else if (_side == MAPositionLeft) {
  483. // Line to relevant point before arrow.
  484. [path lineToPoint:NSMakePoint(maxX, midY + halfArrowWidth)];
  485. // Draw arrow.
  486. [self _appendArrowToPath:path];
  487. } else if (_side == MAPositionLeftTop) {
  488. // Line to relevant point before arrow.
  489. [path lineToPoint:NSMakePoint(maxX, endOfLine.y + scaledArrowWidth)];
  490. // Draw arrow.
  491. [self _appendArrowToPath:path];
  492. }
  493. // Line to end of this side.
  494. [path lineToPoint:endOfLine];
  495. // Rounded corner on bottom-right.
  496. if (shouldDrawNextCorner) {
  497. [path appendBezierPathWithArcFromPoint:NSMakePoint(maxX, minY)
  498. toPoint:NSMakePoint(maxX - scaledRadius, minY)
  499. radius:scaledRadius];
  500. }
  501. // Draw the bottom side, beginning at the bottom-right.
  502. endOfLine = NSMakePoint(minX, minY);
  503. shouldDrawNextCorner = NO;
  504. if (scaledRadius > 0 &&
  505. (drawsRoundCornerBesideArrow ||
  506. (_side != MAPositionTopRight && _side != MAPositionRightTop))
  507. ) {
  508. endOfLine.x += scaledRadius;
  509. shouldDrawNextCorner = YES;
  510. }
  511. // If arrow should be drawn at bottom-right point, draw it.
  512. if (_side == MAPositionTopLeft) {
  513. [self _appendArrowToPath:path];
  514. } else if (_side == MAPositionTop) {
  515. // Line to relevant point before arrow.
  516. [path lineToPoint:NSMakePoint(midX + halfArrowWidth, minY)];
  517. // Draw arrow.
  518. [self _appendArrowToPath:path];
  519. } else if (_side == MAPositionTopRight) {
  520. // Line to relevant point before arrow.
  521. [path lineToPoint:NSMakePoint(endOfLine.x + scaledArrowWidth, minY)];
  522. // Draw arrow.
  523. [self _appendArrowToPath:path];
  524. }
  525. // Line to end of this side.
  526. [path lineToPoint:endOfLine];
  527. // Rounded corner on bottom-left.
  528. if (shouldDrawNextCorner) {
  529. [path appendBezierPathWithArcFromPoint:NSMakePoint(minX, minY)
  530. toPoint:NSMakePoint(minX, minY + scaledRadius)
  531. radius:scaledRadius];
  532. }
  533. // Draw the left side, beginning at the bottom-left.
  534. endOfLine = NSMakePoint(minX, maxY);
  535. shouldDrawNextCorner = NO;
  536. if (scaledRadius > 0 &&
  537. (drawsRoundCornerBesideArrow ||
  538. (_side != MAPositionRightBottom && _side != MAPositionBottomRight))
  539. ) {
  540. endOfLine.y -= scaledRadius;
  541. shouldDrawNextCorner = YES;
  542. }
  543. // If arrow should be drawn at left-bottom point, draw it.
  544. if (_side == MAPositionRightTop) {
  545. [self _appendArrowToPath:path];
  546. } else if (_side == MAPositionRight) {
  547. // Line to relevant point before arrow.
  548. [path lineToPoint:NSMakePoint(minX, midY - halfArrowWidth)];
  549. // Draw arrow.
  550. [self _appendArrowToPath:path];
  551. } else if (_side == MAPositionRightBottom) {
  552. // Line to relevant point before arrow.
  553. [path lineToPoint:NSMakePoint(minX, endOfLine.y - scaledArrowWidth)];
  554. // Draw arrow.
  555. [self _appendArrowToPath:path];
  556. }
  557. // Line to end of this side.
  558. [path lineToPoint:endOfLine];
  559. // Rounded corner on top-left.
  560. if (shouldDrawNextCorner) {
  561. [path appendBezierPathWithArcFromPoint:NSMakePoint(minX, maxY)
  562. toPoint:NSMakePoint(minX + scaledRadius, maxY)
  563. radius:scaledRadius];
  564. }
  565. [path closePath];
  566. return path;
  567. }
  568. - (void)_appendArrowToPath:(NSBezierPath *)path
  569. {
  570. if (!hasArrow) {
  571. return;
  572. }
  573. float scaleFactor = MAATTACHEDWINDOW_SCALE_FACTOR;
  574. float scaledArrowWidth = arrowBaseWidth * scaleFactor;
  575. float halfArrowWidth = scaledArrowWidth / 2.0;
  576. float scaledArrowHeight = arrowHeight * scaleFactor;
  577. NSPoint currPt = [path currentPoint];
  578. NSPoint tipPt = currPt;
  579. NSPoint endPt = currPt;
  580. // Note: we always build the arrow path in a clockwise direction.
  581. switch (_side) {
  582. case MAPositionLeft:
  583. case MAPositionLeftTop:
  584. case MAPositionLeftBottom:
  585. // Arrow points towards right. We're starting from the top.
  586. tipPt.x += scaledArrowHeight;
  587. tipPt.y -= halfArrowWidth;
  588. endPt.y -= scaledArrowWidth;
  589. break;
  590. case MAPositionRight:
  591. case MAPositionRightTop:
  592. case MAPositionRightBottom:
  593. // Arrow points towards left. We're starting from the bottom.
  594. tipPt.x -= scaledArrowHeight;
  595. tipPt.y += halfArrowWidth;
  596. endPt.y += scaledArrowWidth;
  597. break;
  598. case MAPositionTop:
  599. case MAPositionTopLeft:
  600. case MAPositionTopRight:
  601. // Arrow points towards bottom. We're starting from the right.
  602. tipPt.y -= scaledArrowHeight;
  603. tipPt.x -= halfArrowWidth;
  604. endPt.x -= scaledArrowWidth;
  605. break;
  606. case MAPositionBottom:
  607. case MAPositionBottomLeft:
  608. case MAPositionBottomRight:
  609. // Arrow points towards top. We're starting from the left.
  610. tipPt.y += scaledArrowHeight;
  611. tipPt.x += halfArrowWidth;
  612. endPt.x += scaledArrowWidth;
  613. break;
  614. default:
  615. break; // won't happen, but this satisfies gcc with -Wall
  616. }
  617. [path lineToPoint:tipPt];
  618. [path lineToPoint:endPt];
  619. }
  620. - (void)_redisplay
  621. {
  622. if (_resizing) {
  623. return;
  624. }
  625. _resizing = YES;
  626. NSDisableScreenUpdates();
  627. [self _updateGeometry];
  628. [self _updateBackground];
  629. NSEnableScreenUpdates();
  630. _resizing = NO;
  631. }
  632. # pragma mark Window Behaviour
  633. - (BOOL)canBecomeMainWindow
  634. {
  635. return NO;
  636. }
  637. - (BOOL)canBecomeKeyWindow
  638. {
  639. return YES;
  640. }
  641. - (BOOL)isExcludedFromWindowsMenu
  642. {
  643. return YES;
  644. }
  645. - (BOOL)validateMenuItem:(NSMenuItem *)item
  646. {
  647. if (_window) {
  648. return [_window validateMenuItem:item];
  649. }
  650. return [super validateMenuItem:item];
  651. }
  652. - (IBAction)performClose:(id)sender
  653. {
  654. if (_window) {
  655. [_window performClose:sender];
  656. } else {
  657. [super performClose:sender];
  658. }
  659. }
  660. # pragma mark Notification handlers
  661. - (void)windowDidResize:(NSNotification *)note
  662. {
  663. [self _redisplay];
  664. }
  665. #pragma mark Accessors
  666. - (void)setPoint:(NSPoint)point side:(MAWindowPosition)side
  667. {
  668. // Thanks to Martin Redington.
  669. _point = point;
  670. _side = side;
  671. NSDisableScreenUpdates();
  672. [self _updateGeometry];
  673. [self _updateBackground];
  674. NSEnableScreenUpdates();
  675. }
  676. - (NSColor *)windowBackgroundColor {
  677. return [[_MABackgroundColor retain] autorelease];
  678. }
  679. - (void)setBackgroundColor:(NSColor *)value {
  680. if (_MABackgroundColor != value) {
  681. [_MABackgroundColor release];
  682. _MABackgroundColor = [value copy];
  683. [self _updateBackground];
  684. }
  685. }
  686. - (NSColor *)borderColor {
  687. return [[borderColor retain] autorelease];
  688. }
  689. - (void)setBorderColor:(NSColor *)value {
  690. if (borderColor != value) {
  691. [borderColor release];
  692. borderColor = [value copy];
  693. [self _updateBackground];
  694. }
  695. }
  696. - (float)borderWidth {
  697. return borderWidth;
  698. }
  699. - (void)setBorderWidth:(float)value {
  700. if (borderWidth != value) {
  701. float maxBorderWidth = viewMargin;
  702. if (value <= maxBorderWidth) {
  703. borderWidth = value;
  704. } else {
  705. borderWidth = maxBorderWidth;
  706. }
  707. [self _updateBackground];
  708. }
  709. }
  710. - (float)viewMargin {
  711. return viewMargin;
  712. }
  713. - (void)setViewMargin:(float)value {
  714. if (viewMargin != value) {
  715. viewMargin = MAX(value, 0.0);
  716. // Adjust cornerRadius appropriately (which will also adjust arrowBaseWidth).
  717. [self setCornerRadius:cornerRadius];
  718. }
  719. }
  720. - (float)arrowBaseWidth {
  721. return arrowBaseWidth;
  722. }
  723. - (void)setArrowBaseWidth:(float)value {
  724. float maxWidth = (MIN(_viewFrame.size.width, _viewFrame.size.height) +
  725. (viewMargin * 2.0)) - cornerRadius;
  726. if (drawsRoundCornerBesideArrow) {
  727. maxWidth -= cornerRadius;
  728. }
  729. if (value <= maxWidth) {
  730. arrowBaseWidth = value;
  731. } else {
  732. arrowBaseWidth = maxWidth;
  733. }
  734. [self _redisplay];
  735. }
  736. - (float)arrowHeight {
  737. return arrowHeight;
  738. }
  739. - (void)setArrowHeight:(float)value {
  740. if (arrowHeight != value) {
  741. arrowHeight = value;
  742. [self _redisplay];
  743. }
  744. }
  745. - (float)hasArrow {
  746. return hasArrow;
  747. }
  748. - (void)setHasArrow:(float)value {
  749. if (hasArrow != value) {
  750. hasArrow = value;
  751. [self _updateBackground];
  752. }
  753. }
  754. - (float)cornerRadius {
  755. return cornerRadius;
  756. }
  757. - (void)setCornerRadius:(float)value {
  758. float maxRadius = ((MIN(_viewFrame.size.width, _viewFrame.size.height) +
  759. (viewMargin * 2.0)) - arrowBaseWidth) / 2.0;
  760. if (value <= maxRadius) {
  761. cornerRadius = value;
  762. } else {
  763. cornerRadius = maxRadius;
  764. }
  765. cornerRadius = MAX(cornerRadius, 0.0);
  766. // Adjust arrowBaseWidth appropriately.
  767. [self setArrowBaseWidth:arrowBaseWidth];
  768. }
  769. - (float)drawsRoundCornerBesideArrow {
  770. return drawsRoundCornerBesideArrow;
  771. }
  772. - (void)setDrawsRoundCornerBesideArrow:(float)value {
  773. if (drawsRoundCornerBesideArrow != value) {
  774. drawsRoundCornerBesideArrow = value;
  775. [self _redisplay];
  776. }
  777. }
  778. - (void)setBackgroundImage:(NSImage *)value
  779. {
  780. if (value) {
  781. [self setBackgroundColor:[NSColor colorWithPatternImage:value]];
  782. }
  783. }
  784. @end