bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSPopUpButton icon buttons patch...


From: Matt Rice
Subject: NSPopUpButton icon buttons patch...
Date: Mon, 25 Jul 2005 08:37:16 -0700 (PDT)

heres a patch and a little test program.. which tries
to get small pull-down buttons

as explained here.. 
http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuidelines/XHIGControls/chapter_18_section_3.html#//apple_ref/doc/uid/20000957-TP30000359-BAADHEBI

where i'd like to have a pop-up button in the table
view's corner view, for adding/removing table columns
so its quite limited in space...

so instead of using the cell width for the menu width,

it uses the wider of the two, it also disables the
"state" image in the title, 

also implements the "preferredEdge" stuff..
but i've added an extension to enable by subclassing.

i won't really use it or pout if there are objections
to this part but there was a TODO there...
testing with cocoa seemed to show that setting the
preferred edge didn't do anything, and the default
encoded settings were non-sensical, so if we use them
by default with the key value coding implementation
would produce undesirable results.

on a side note i noticed
NSMenuView and NSMenuItemCell are deprecated
and NSMenu has changed super classes from NSPanel to
NSObject...

it isn't very pretty or flexible but unless there are
any objections, i'd like to commit this stuff

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Attachment: popUpTest-0.0.1.tar.gz
Description: 3952076433-popUpTest-0.0.1.tar.gz

Index: Source/NSMenuView.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Source/NSMenuView.m,v
retrieving revision 1.105
diff -u -r1.105 NSMenuView.m
--- Source/NSMenuView.m 26 May 2005 02:52:44 -0000      1.105
+++ Source/NSMenuView.m 25 Jul 2005 14:09:51 -0000
@@ -39,6 +39,7 @@
 #include "AppKit/NSWindow.h"
 #include "AppKit/PSOperators.h"
 #include "AppKit/NSImage.h"
+#include "AppKit/NSPopUpButtonCell.h"
 
 #include "GNUstepGUI/GSTitleView.h"
 
@@ -640,6 +641,11 @@
     }
   else
     {
+      if (_needsSizing)    
+        {
+         _cellSize.width = accumulatedOffset + 3;
+       }
+
       _keyEqOffset = _cellSize.width - _keyEqWidth - popupImageWidth;
     }
 
@@ -846,6 +852,7 @@
   NSRect cellFrame;
   NSRect screenFrame;
   int items = [_itemCells count];
+  NSPopUpButtonCell *_owner;
   
   // Convert the screen rect to our view
   cellFrame.size = screenRect.size;
@@ -855,7 +862,32 @@
   // Only call update if needed.
   if ((NSEqualSizes(_cellSize, cellFrame.size) == NO) || _needsSizing)
     {
-      _cellSize = cellFrame.size;
+      NSBezelStyle styleMask;
+      
+      if (_needsSizing)
+       [self update];
+      
+      if (cellFrame.size.width > _cellSize.width) 
+       {
+         _needsSizing = NO;
+         _cellSize.width = cellFrame.size.width;
+       }
+      if ((_owner = [_attachedMenu _owningPopUp]))
+       {
+         styleMask = [_owner bezelStyle];
+         switch (styleMask)
+           {
+             case NSRegularSquareBezelStyle:
+             case NSThickSquareBezelStyle:
+             case NSThickerSquareBezelStyle:
+             case NSShadowlessSquareBezelStyle:
+               /* do nothing */
+               break;
+             default:
+               _cellSize.height = cellFrame.size.height;
+               break;
+           }
+       }
       [self update];
     }
   
@@ -869,11 +901,11 @@
 
       if (_horizontal == NO)
        {
-         f = screenRect.size.height * (items - 1);
-         screenFrame.size.height += f + _leftBorderOffset;
-         screenFrame.origin.y -= f;
-         screenFrame.size.width += _leftBorderOffset;
+         f = _cellSize.height * (items - ([[_attachedMenu _owningPopUp] 
usesItemFromMenu] == YES && [[_attachedMenu _owningPopUp] pullsDown]));
+         screenFrame.size.height = f + _leftBorderOffset; 
+         screenFrame.origin.y -= f - cellFrame.size.height;
          screenFrame.origin.x -= _leftBorderOffset;
+         screenFrame.size.width += _leftBorderOffset;
          // Compute position for popups, if needed
          if (selectedItemIndex != -1) 
            {
@@ -896,8 +928,33 @@
   r = [NSWindow frameRectForContentRect: screenFrame
                styleMask: [_window styleMask]];
   
-  // Update position,if needed, using the preferredEdge;
-  // TODO
+  if (r.size.width < _cellSize.width) 
+    r.size.width = _cellSize.width;
+
+  if ((_owner = [_attachedMenu _owningPopUp]))
+    {
+      if ([_owner usesPreferredEdge])
+        {
+         // Update position,if needed, using the preferredEdge;
+         if (edge == NSMinYEdge)
+           {
+             r.origin.y -= cellFrame.size.height;  
+           }
+         else if (edge == NSMaxXEdge)
+           {
+             r.origin.x += cellFrame.size.width;
+           }
+         else if (edge == NSMinXEdge)
+           {
+             r.origin.x -= r.size.width;
+           }
+       }
+      else if ([_owner pullsDown] == YES)
+       {
+         r.origin.y -= cellFrame.size.height;
+       }
+    }
+  
   
   // Set the window frame
   [_window setFrame: r display: NO]; 
@@ -922,7 +979,8 @@
   NSDrawTiledRects(rect, rect, sides, grays, 2);
   
   // Draw the menu cells.
-  for (i = 0; i < howMany; i++)
+  for (i = ([[_attachedMenu _owningPopUp] usesItemFromMenu] == YES
+           && [[_attachedMenu _owningPopUp] pullsDown]); i < howMany; i++)
     {
       NSRect           aRect;
       NSMenuItemCell   *aCell;
Index: Source/NSPopUpButtonCell.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Source/NSPopUpButtonCell.m,v
retrieving revision 1.70
diff -u -r1.70 NSPopUpButtonCell.m
--- Source/NSPopUpButtonCell.m  17 Jun 2005 14:55:48 -0000      1.70
+++ Source/NSPopUpButtonCell.m  25 Jul 2005 14:09:52 -0000
@@ -150,13 +150,13 @@
     {
       // pop up
       [self setArrowPosition: NSPopUpArrowAtCenter];
-      [self setPreferredEdge: NSMinYEdge];
+      [self setPreferredEdge: NSMaxYEdge];
     }
   else
     {
       // pull down
       [self setArrowPosition: NSPopUpArrowAtBottom];
-      [self setPreferredEdge: NSMaxYEdge];
+      [self setPreferredEdge: NSMinYEdge];
     }
 
   [self setMenuItem: item];
@@ -190,6 +190,16 @@
   return _pbcFlags.preferredEdge;
 }
 
+/** GNUstep extension the default is <code>NO</code>
+ *  By default pull down menus will pop-up directly below the button and
+ *  pop up menus will apprear on top of the button.
+ *  Subclasses can use this method to return YES and the pop up preferred edge.
+ */
+- (BOOL) usesPreferredEdge
+{
+  return NO; 
+}
+
 // If YES (the default) the popup button will display an item from the
 // menu.  This will be the selected item for a popup or the first item for
 // a pull-down.  If this is NO, then the menu item set with -setMenuItem: 
@@ -693,8 +703,16 @@
               eventNumber: [theEvent eventNumber]
               clickCount: [theEvent clickCount] 
               pressure: [theEvent pressure]];
-  [NSApp sendEvent: e];
-
+  /* since the menu's window can be outside the event location,
+   * because of the preferredEdge NSApp will not send it to the menu window.
+   * so call mouseDown: directly. */
+  [mr mouseDown:e];
+
+  /* if we use a preferredEdge which is not directly over the control 
+   * we can mouse up without causing an action which won't cause a so we
+   * dismiss the popUp even though most cases it will already be dismissed */
+  [self dismissPopUp];
+        
   return NO;
 }
 
@@ -734,6 +752,7 @@
                        inView: (NSView*)controlView
 {
   BOOL new = NO;
+  float tmpStateImageWidth;
 
   if ([self menuItem] == nil)
     {
@@ -754,7 +773,11 @@
 
   /* We need to calc our size to get images placed correctly */
   [self calcSize];
+  /* we shouldn't show state in the title. */
+  tmpStateImageWidth = _stateImageWidth;
+  _stateImageWidth = 0;
   [super drawInteriorWithFrame: cellFrame inView: controlView];
+  _stateImageWidth = tmpStateImageWidth;
 
   if (_cell.shows_first_responder)
     {
@@ -767,6 +790,48 @@
     {
       [self setMenuItem: nil];
     }
+}
+
+- (NSRect) stateImageRectForBounds:(NSRect)cellFrame
+{
+  return NSZeroRect;
+}
+
+- (NSRect) titleRectForBounds:(NSRect)cellFrame
+{
+  /* overrides so we don't calculate state offset */
+  cellFrame.origin.x  += 4; /* left offset */
+
+  switch (_cell.image_position)
+    {
+      case NSNoImage:
+      case NSImageOverlaps:
+        break;
+
+      case NSImageOnly:
+        cellFrame = NSZeroRect;
+        break;
+
+      case NSImageLeft:
+        cellFrame.origin.x  += _imageWidth + GSCellTextImageXDist;
+        cellFrame.size.width = _titleWidth;
+        break;
+
+      case NSImageRight:
+        cellFrame.size.width = _titleWidth;
+        break;
+
+      case NSImageBelow:
+        cellFrame.size.height /= 2;
+        cellFrame.origin.y += cellFrame.size.height;
+        break;
+
+      case NSImageAbove:
+        cellFrame.size.height /= 2;
+        break;
+    }
+
+  return cellFrame;
 }
 
 /* FIXME: this method needs to be rewritten to be something like 
Index: Source/NSMenu.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Source/NSMenu.m,v
retrieving revision 1.129
diff -u -r1.129 NSMenu.m
--- Source/NSMenu.m     21 Jun 2005 22:48:21 -0000      1.129
+++ Source/NSMenu.m     25 Jul 2005 14:09:55 -0000
@@ -1534,6 +1534,11 @@
   return _popUpButtonCell != nil;
 }
 
+- (NSPopUpButtonCell *)_owningPopUp
+{
+  return _popUpButtonCell;
+}
+
 - (void)_setOwnedByPopUp: (NSPopUpButtonCell*)popUp
 {
   if (_popUpButtonCell != popUp)
Index: Headers/AppKit/NSMenu.h
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Headers/AppKit/NSMenu.h,v
retrieving revision 1.4
diff -u -r1.4 NSMenu.h
--- Headers/AppKit/NSMenu.h     26 May 2005 02:52:41 -0000      1.4
+++ Headers/AppKit/NSMenu.h     25 Jul 2005 14:09:56 -0000
@@ -647,6 +647,7 @@
 - (void) shiftOnScreen;
 
 /* Popup behaviour */
+- (NSPopUpButtonCell *)_owningPopUp;
 - (BOOL) _ownedByPopUp;
 - (void) _setOwnedByPopUp: (NSPopUpButtonCell*)popUp;
 @end

reply via email to

[Prev in Thread] Current Thread [Next in Thread]