Hi!

This patch makes theming NSScroller easier. When applied, the button
cells used by NSScroller are not created by the NSScroller class itself
but created by factory methods in GSTheme. The standard button cells can
thus be easily replaced by a theme.

Here's a screenshot of a theme engine taking advantage of this:
http://www.unix-ag.uni-kl.de/~guenther/images/nsscroller-demo.png

Without this patch, changing the scroller button cells involves
copy&pasting several methods from NSScroller. I hope you can apply my
patch.

Best regards,
Guenther

Index: Source/GSTheme.m
===================================================================
--- Source/GSTheme.m	(revision 25503)
+++ Source/GSTheme.m	(working copy)
@@ -38,6 +38,7 @@
 #include "AppKit/NSButton.h"
 #include "AppKit/NSColor.h"
 #include "AppKit/NSColorList.h"
+#include "AppKit/NSEvent.h"
 #include "AppKit/NSGraphics.h"
 #include "AppKit/NSImage.h"
 #include "AppKit/NSImageView.h"
@@ -693,6 +694,67 @@
 @end
 
 
[EMAIL PROTECTED] GSTheme (Factory)
+
+- (NSButtonCell*) makeScrollerUpCell {
+  NSButtonCell* upCell = [NSButtonCell new];
+  [upCell setHighlightsBy: NSChangeBackgroundCellMask | NSContentsCellMask];
+  [upCell setImage: [NSImage imageNamed: @"common_ArrowUp"]];
+  [upCell setAlternateImage: [NSImage imageNamed: @"common_ArrowUpH"]];
+  [upCell setImagePosition: NSImageOnly];
+  [upCell setContinuous: YES];
+  [upCell sendActionOn: (NSLeftMouseDownMask | NSPeriodicMask)];
+  [upCell setPeriodicDelay: 0.3 interval: 0.03];
+  return upCell;
+}
+
+- (NSButtonCell*) makeScrollerDownCell {
+  NSButtonCell* downCell = [NSButtonCell new];
+  [downCell setHighlightsBy: NSChangeBackgroundCellMask | NSContentsCellMask];
+  [downCell setImage: [NSImage imageNamed: @"common_ArrowDown"]];
+  [downCell setAlternateImage: [NSImage imageNamed: @"common_ArrowDownH"]];
+  [downCell setImagePosition: NSImageOnly];
+  [downCell setContinuous: YES];
+  [downCell sendActionOn: (NSLeftMouseDownMask | NSPeriodicMask)];
+  [downCell setPeriodicDelay: 0.3 interval: 0.03];
+  return downCell;
+}
+
+- (NSButtonCell*) makeScrollerLeftCell {
+  NSButtonCell* leftCell = [NSButtonCell new];
+  [leftCell setHighlightsBy: NSChangeBackgroundCellMask | NSContentsCellMask];
+  [leftCell setImage: [NSImage imageNamed: @"common_ArrowLeft"]];
+  [leftCell setAlternateImage: [NSImage imageNamed: @"common_ArrowLeftH"]];
+  [leftCell setImagePosition: NSImageOnly];
+  [leftCell setContinuous: YES];
+  [leftCell sendActionOn: (NSLeftMouseDownMask | NSPeriodicMask)];
+  [leftCell setPeriodicDelay: 0.3 interval: 0.03];
+  return leftCell;
+}
+
+- (NSButtonCell*) makeScrollerRightCell {
+  NSButtonCell* rightCell = [NSButtonCell new];
+  [rightCell setHighlightsBy: NSChangeBackgroundCellMask | NSContentsCellMask];
+  [rightCell setImage: [NSImage imageNamed: @"common_ArrowRight"]];
+  [rightCell setAlternateImage: [NSImage imageNamed: @"common_ArrowRightH"]];
+  [rightCell setImagePosition: NSImageOnly];
+  [rightCell setContinuous: YES];
+  [rightCell sendActionOn: (NSLeftMouseDownMask | NSPeriodicMask)];
+  [rightCell setPeriodicDelay: 0.3 interval: 0.03];
+  return rightCell;
+}
+
+- (NSButtonCell*) makeScrollerKnobCell {
+  NSButtonCell* knobCell = [NSButtonCell new];
+  [knobCell setButtonType: NSMomentaryChangeButton];
+  [knobCell setImage: [NSImage imageNamed: @"common_Dimple"]];
+  [knobCell setImagePosition: NSImageOnly];
+  return knobCell;
+}
+
[EMAIL PROTECTED]
+
+
 @implementation	GSTheme (Drawing)
 
 - (void) drawButton: (NSRect)frame 
Index: Source/NSScroller.m
===================================================================
--- Source/NSScroller.m	(revision 25503)
+++ Source/NSScroller.m	(working copy)
@@ -32,6 +32,8 @@
 #include <Foundation/NSDate.h>
 #include <Foundation/NSRunLoop.h>
 #include <Foundation/NSDebug.h>
+#include <Foundation/NSException.h>
+#include <Foundation/NSNotification.h>
 
 #include "AppKit/NSApplication.h"
 #include "AppKit/NSButtonCell.h"
@@ -42,6 +44,7 @@
 #include "AppKit/NSScroller.h"
 #include "AppKit/NSScrollView.h"
 #include "AppKit/NSWindow.h"
+#include "GNUstepGUI/GSTheme.h"
 
 /**<p>TODO Description</p>
  */
@@ -72,9 +75,29 @@
     {
       [self setVersion: 1];
       ASSIGN (scrollBarColor, [NSColor scrollBarColor]);
+
+      [[NSNotificationCenter defaultCenter]
+        addObserver: self
+        selector: @selector(themeDeactivated:)
+        name: GSThemeDidActivateNotification
+        object: nil];
     }
 }
 
+/*
+ * Called when the current theme is activated.
+ */
++ (void) themeActivated: (NSNotification*)notif
+{
+  // release button cells so that the new theme can
+  // provide them later.
+  DESTROY(upCell);
+  DESTROY(downCell);
+  DESTROY(leftCell);
+  DESTROY(rightCell);
+  DESTROY(knobCell);
+}
+
 /**<p>Returns the NSScroller's width. By default 18.</p>
    <p>Subclasses can override this to provide different scrollbar width.  But
    you may need to also override -drawParts .</p>
@@ -326,47 +349,16 @@
    */
   if (knobCell)
     return;
-
-  upCell = [NSButtonCell new];
-  [upCell setHighlightsBy: NSChangeBackgroundCellMask | NSContentsCellMask];
-  [upCell setImage: [NSImage imageNamed: @"common_ArrowUp"]];
-  [upCell setAlternateImage: [NSImage imageNamed: @"common_ArrowUpH"]];
-  [upCell setImagePosition: NSImageOnly];
-  [upCell setContinuous: YES];
-  [upCell sendActionOn: (NSLeftMouseDownMask | NSPeriodicMask)];
-  [upCell setPeriodicDelay: 0.3 interval: 0.03];
-
-  downCell = [NSButtonCell new];
-  [downCell setHighlightsBy: NSChangeBackgroundCellMask | NSContentsCellMask];
-  [downCell setImage: [NSImage imageNamed: @"common_ArrowDown"]];
-  [downCell setAlternateImage: [NSImage imageNamed: @"common_ArrowDownH"]];
-  [downCell setImagePosition: NSImageOnly];
-  [downCell setContinuous: YES];
-  [downCell sendActionOn: (NSLeftMouseDownMask | NSPeriodicMask)];
-  [downCell setPeriodicDelay: 0.3 interval: 0.03];
-
-  leftCell = [NSButtonCell new];
-  [leftCell setHighlightsBy: NSChangeBackgroundCellMask | NSContentsCellMask];
-  [leftCell setImage: [NSImage imageNamed: @"common_ArrowLeft"]];
-  [leftCell setAlternateImage: [NSImage imageNamed: @"common_ArrowLeftH"]];
-  [leftCell setImagePosition: NSImageOnly];
-  [leftCell setContinuous: YES];
-  [leftCell sendActionOn: (NSLeftMouseDownMask | NSPeriodicMask)];
-  [leftCell setPeriodicDelay: 0.3 interval: 0.03];
-
-  rightCell = [NSButtonCell new];
-  [rightCell setHighlightsBy: NSChangeBackgroundCellMask | NSContentsCellMask];
-  [rightCell setImage: [NSImage imageNamed: @"common_ArrowRight"]];
-  [rightCell setAlternateImage: [NSImage imageNamed: @"common_ArrowRightH"]];
-  [rightCell setImagePosition: NSImageOnly];
-  [rightCell setContinuous: YES];
-  [rightCell sendActionOn: (NSLeftMouseDownMask | NSPeriodicMask)];
-  [rightCell setPeriodicDelay: 0.3 interval: 0.03];
-
-  knobCell = [NSButtonCell new];
-  [knobCell setButtonType: NSMomentaryChangeButton];
-  [knobCell setImage: [NSImage imageNamed: @"common_Dimple"]];
-  [knobCell setImagePosition: NSImageOnly];
+  
+  GSTheme* theme = [GSTheme theme];
+  
+  upCell = [theme makeScrollerUpCell];
+  downCell = [theme makeScrollerDownCell];
+  leftCell = [theme makeScrollerLeftCell];
+  rightCell = [theme makeScrollerRightCell];
+  knobCell = [theme makeScrollerKnobCell];
+  
+  NSAssert(upCell && downCell && leftCell && rightCell && knobCell, @"Scroller cell factory methods broken.");
 }
 
 - (void) _setTargetAndActionToCells
Index: Headers/Additions/GNUstepGUI/GSTheme.h
===================================================================
--- Headers/Additions/GNUstepGUI/GSTheme.h	(revision 25503)
+++ Headers/Additions/GNUstepGUI/GSTheme.h	(working copy)
@@ -372,6 +372,45 @@
 @end
 
 /**
+ * Theme factory methods
+ */
[EMAIL PROTECTED]  GSTheme (Factory)
+
+/**
+ * Creates and returns a new button cell to be used for a scroller's
+ * "up" button.
+ */
+- (NSButtonCell*) makeScrollerUpCell;
+
+/**
+ * Creates and returns a new button cell to be used for a scroller's
+ * "down" button.
+ */
+- (NSButtonCell*) makeScrollerDownCell;
+
+
+/**
+ * Creates and returns a new button cell to be used for a scroller's
+ * "right" button.
+ */
+- (NSButtonCell*) makeScrollerRightCell;
+
+/**
+ * Creates and returns a new button cell to be used for a scroller's
+ * "left" button.
+ */
+- (NSButtonCell*) makeScrollerLeftCell;
+
+/**
+ * Creates and returns a new button cell to be used for a scroller's
+ * "knob".
+ */
+- (NSButtonCell*) makeScrollerKnobCell;
+
[EMAIL PROTECTED]
+
+
+/**
  * Theme drawing methods
  */
 @interface	GSTheme (Drawing)
_______________________________________________
Gnustep-dev mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/gnustep-dev

Reply via email to