Scroll Views

  • This photographer is trying to drive me insane.

    Ok... The interface is hopefully going to be an NSScrollView. It will
    have a certain number of buttons from left to right depending on the
    size of the window. Once the width is reached a new line will be
    begun. I have no trouble getting the buttons with images in them to
    appear in an NSClipView defined in the nib. The problem is... I do
    not know how to either resize the clipview or tell the scrollview
    that the clip view has resize because what I get is the lines of
    buttons that appear to go up above the top of the window and no
    scrollbar.
    Now I assume I am suppose to set the content view size first, then
    begin adding the images. So that is what I've done. The code
    currently only adds 1 button per line which is all I care about for
    testing. After setting the content size then I would add it to the
    scrolleview which is what I must be doing wrong... i think.

    Anyway I've made a mess this is the code that doesn't work:

    -(void)awakeFromNib
    {

    [scroller setHasVerticalScroller:YES];
    NSString * imagePath =@"/Users/mine/Pictures/DSCF0019.JPG";


    int n = 0;
    float top = 6.0*140.0;
    NSRect scf = [scrollContent frame];
    [scrollContent setFrame:NSMakeRect(scf.origin.x,scf.origin.y,
    150.0,top)];
    while(n !=6){

      ThumbNail * thisThumb =[[ThumbNail alloc]initWithFile:imagePath
    originx:0.0 originy:top];
      [scrollContent addSubview:[thisThumb thumbNailImage]];
      top -=140;
      n++;
      }

    [scroller setContentView:scrollContent];
    }

    I hope maybe some one can shed some light for me.
    Thanks
    April
  • Sorry if I missed it earlier, but is there a reason you aren't using
    a matrix to do this?

    - Dave

    On Sep 21, 2007, at 7:33 PM, Development wrote:

    > This photographer is trying to drive me insane.
    >
    > Ok... The interface is hopefully going to be an NSScrollView. It
    > will have a certain number of buttons from left to right depending
    > on the size of the window. Once the width is reached a new line
    > will be begun. I have no trouble getting the buttons with images in
    > them to appear in an NSClipView defined in the nib. The problem
    > is... I do not know how to either resize the clipview or tell the
    > scrollview that the clip view has resize because what I get is the
    > lines of buttons that appear to go up above the top of the window
    > and no scrollbar.
    > Now I assume I am suppose to set the content view size first, then
    > begin adding the images. So that is what I've done. The code
    > currently only adds 1 button per line which is all I care about for
    > testing. After setting the content size then I would add it to the
    > scrolleview which is what I must be doing wrong... i think.
    >
    > Anyway I've made a mess this is the code that doesn't work:
    >
    >
    > -(void)awakeFromNib
    > {
    >
    > [scroller setHasVerticalScroller:YES];
    > NSString * imagePath =@"/Users/mine/Pictures/DSCF0019.JPG";
    >
    >
    > int n = 0;
    > float top = 6.0*140.0;
    > NSRect scf = [scrollContent frame];
    > [scrollContent setFrame:NSMakeRect(scf.origin.x,scf.origin.y,
    > 150.0,top)];
    > while(n !=6){
    >
    > ThumbNail * thisThumb =[[ThumbNail alloc]initWithFile:imagePath
    > originx:0.0 originy:top];
    > [scrollContent addSubview:[thisThumb thumbNailImage]];
    > top -=140;
    > n++;
    > }
    >
    > [scroller setContentView:scrollContent];
    > }
    >
    > I hope maybe some one can shed some light for me.
    > Thanks
    > April
  • I don't think a matrix would work would it? It wouldn't allow me to
    scroll through...

    However... It took some time but between my books and the developer
    site i did finally get the scrollview to work the way I wanted. I
    have included the working code on the chance that some one with the
    same problem searches the list archive. This is just code I used to
    figure out the scroll view. The actual program will get images from a
    folder.

    -(void)awakeFromNib
    {

    [scroller setHasVerticalScroller:YES];
    NSString * imagePath =@"/Users/fns/Pictures/DSCF0019.JPG";


    int n = 0;
    float top = (10.0*140.0);
    NSRect scf = [scrollClip frame];
    [scrollClip setFrame:NSMakeRect(scf.origin.x,scf.origin.y,150.0,top)];
    while(n < 10){

      ThumbNail * thisThumb =[[ThumbNail alloc]initWithFile:imagePath
    originx:5.0 originy:(top -140)];
      [scrollClip addSubview:[thisThumb thumbNailImage]];
      top -=140;
      n++;
      }
    [scroller setDocumentView:scrollClip];
    NSPoint newOrigin = NSMakePoint(0.0,NSMaxY([[scroller documentView]
    frame])-NSHeight([[scroller contentView]bounds]));

    float height = NSHeight([[scroller contentView]bounds]);
    float width = NSWidth([[scroller contentView]bounds]);
    [[scroller contentView] scrollRectToVisible:NSMakeRect
    (newOrigin.x,newOrigin.y,width,height)];
    NSPoint csp=[[scroller contentView] bounds].origin;
    }
  • If you use a matrix and make that a subview of a scrollview, the
    scrolling comes for free. You can test this in Interface Builder by
    making a window with a big matrix of buttons and then going up to the
    Layout menu with that selected, and selecting Make subviews of
    > Scroll View. If you shrink the scroll view and then  do a command-
    R to test run the interface, you'll see that the scrolling to your
    buttons is handled for you.

    Now, having said all that, whenever I've done this, for a real
    application, you'll want to create an NSMatrix subclass and make that
    a subview of an NSScrollView. So, same thing, but you'd make the
    matrix programatically. Making a subclass of NSMatrix lets you
    respond to view sizing changes, as well as allowing the user to
    change the size of the thumbnails themselves if you want that. You
    also need to create a subclass to set up your items, unless they're
    going to be exactly the same as some standard button that already
    exists. I've always used an NSImageCell subclass for that so that I
    had more control over the layout.

    You don't need to do any calculations besides how many of your
    buttons fit in the given width and how many rows of them you need to
    display them all. There should be a lot of information in the
    archives about this. This works really well for thumbnail type views,
    which it sounds like you're working on. You really shouldn't have to
    worry about the scrolling at all.

    Basically, the matrix is like one huge sheet of your buttons. You
    make that programattically, and then the scroll view controls how
    much of that is visible. If the user grows the view, then you change
    the number of columns in your matrix to make it fit nicely. If they
    scroll, you do nothing. The scroll view handles all that for you.
    It's easier than the code you're writing below. :)

    - Dave

    On Sep 21, 2007, at 11:46 PM, Development wrote:

    > I don't think a matrix would work would it? It wouldn't allow me to
    > scroll through...
    >
    > However... It took some time but between my books and the developer
    > site i did finally get the scrollview to work the way I wanted. I
    > have included the working code on the chance that some one with the
    > same problem searches the list archive. This is just code I used to
    > figure out the scroll view. The actual program will get images from
    > a folder.
    >
    > -(void)awakeFromNib
    > {
    >
    > [scroller setHasVerticalScroller:YES];
    > NSString * imagePath =@"/Users/fns/Pictures/DSCF0019.JPG";
    >
    >
    > int n = 0;
    > float top = (10.0*140.0);
    > NSRect scf = [scrollClip frame];
    > [scrollClip setFrame:NSMakeRect(scf.origin.x,scf.origin.y,
    > 150.0,top)];
    > while(n < 10){
    >
    > ThumbNail * thisThumb =[[ThumbNail alloc]initWithFile:imagePath
    > originx:5.0 originy:(top -140)];
    > [scrollClip addSubview:[thisThumb thumbNailImage]];
    > top -=140;
    > n++;
    > }
    > [scroller setDocumentView:scrollClip];
    > NSPoint newOrigin = NSMakePoint(0.0,NSMaxY([[scroller
    > documentView] frame])-NSHeight([[scroller contentView]bounds]));
    >
    > float height = NSHeight([[scroller contentView]bounds]);
    > float width = NSWidth([[scroller contentView]bounds]);
    > [[scroller contentView] scrollRectToVisible:NSMakeRect
    > (newOrigin.x,newOrigin.y,width,height)];
    > NSPoint csp=[[scroller contentView] bounds].origin;
    > }
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today