How can I change page orientation within a multi page NSPrintOperation? - objective-c

Let's say that I have a three page report that I want to print out, first page is portrait orientation, second page is landscape, and third page is again portrait.
I've subclassed a special NSView to be my printing operation, but what I get is three pages, all oriented portrait, cutting off the landscape orientation. I have been able to work around it, by separating each of the pages into a separate print job, but that's not acceptable, because it ignores the settings from the
NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView: printView printInfo: printInfo];
[printView setCurrentForm: [formSet objectAtIndex: 0]];
[printOperation setShowsPrintPanel:YES];
[printOperation runOperationModalForWindow: [self window] delegate: nil didRunSelector: nil contextInfo: nil];
[printOperation cleanUpOperation];
[NSPrintOperation setCurrentOperation: nil];
call from the first page (if, for example, they selected "PDF Open in Preview" from that dialog, I want the three pages to be joined as one job, which opens as a PDF in Preview.)
What do I need to do in the code that has the printer version of the NSView rendered, to force the NSPrintOperation to change the orientation?

Set the orientation in rectForPage:
NSPrintOperation.currentOperation.printInfo.orientation = NSPaperOrientationLandscape;
or
NSPrintOperation.currentOperation.printInfo.orientation = NSPaperOrientationPortrait;

Related

UITableViewCells scroll horizontally using auto layout when changing to portrait mode

I am creating a screen that is primarily a UITableView with cells that have differing controls and layouts. I had the tableview working great until I switched to portrait mode where it allowed the tableview cells to scroll horizontally. The cells should never scroll horizontally; only vertically. When I navigate to the screen and I'm already in portrait mode it works fine, even going to landscape and back to portrait.
Some things to note are that I'm using and iPad, storyboards, auto layout, and the UITableViewCell is custom and does not have a nib (though it does the same thing when I tried using a nib). All controls and layout constraints are added programmatically. I am using a custom view controller with a view inside of it. Inside of the view I have the tableview and a toolbar. If I remove the view so that the tableview is directly under the custom view controller it works fine, but then I can't have the toolbar.
Here is the test code I am using to troubleshoot the problem:
// Remove all existing subviews
for(UIView *subView in self.contentView.subviews)
{
[subView removeFromSuperview];
}
UILabel *testLabel = [[UILabel alloc] init];
testLabel.text = #"Test";
[testLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
self.contentView addSubview:testLabel];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(testLabel);
NSString *format = [NSString stringWithFormat:#"V:|-[testLabel]-|"];
NSArray *constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:nil metrics:nil views:viewsDictionary];
//[self.contentView addConstraints:constraintsArray];
The scrolling works as intended until I uncomment the line to add the constraints. This code is in a method of my custom cell and gets called every time a cell is dequeued. I've tried adding the contraints individually, without using the visual format, with no luck. I have also done some research and found the code that removes the contraints from the cell and adds them to the contentView, but that hasn't helped either.
What do I need to do to keep the cells from scrolling horizontally when changing from landscape to portrait mode?
MORE INFO:
After some more debugging I found that the table view's content size is not shrinking horizontally. The width is staying at 1024.

Manipulating an NSSavePanel's accessoryView?

I have an NSSavePanel set up with an accessory view that contains a format popup menu and a tabless tabview with various options regarding the selected format. It shows up on the panel correctly, but I cannot seem to adjust its size.
Calling -setFrameSize on the accessoryView results in odd and incorrect resizing once, and then no response for any subsequent calls. Other controls in the view flat out refuse to respond to any calls at all.
Does NSSavePanel work like NSSearchField, which only uses its assigned menu as a template? If so, how can I manipulate the specific instance of the accessory view in the current active save panel?
My experience has been (especially with code that has to run on 10.5) that in order to handle permuting sizes in the accessoryView for NSSavePanel we had to remove (set it to nil) and re-add it. Under 10.7 (and, I believe, 10.6), it appears to be sufficient to call [savePanel layoutIfNecessary] after changing the frameSize.
In this case, since you mention that you are using invisible tab views. Usually a tab view has a consistent size. If you're looking to resize the NSSavePanel based on the contents of the various subviews, you may want to keep them as separate views (in the same or other NIB files) and load them as child views in the NSSavePanel.
I've successfully done this in a situation where the NIBs were dynamically loaded from an on-disk list of plug-in modules, where I used a single overall view that contained the popup menu and then I resized that view using -setFrameSize: in order to change it prior to adding the child view to it. Then I used addSubview: to add the subview to my existing view and called [savePanel layoutIfNeeded] after changing the size and adding the subview.
Here is the snippet of four adding the exportAccessoryViewController (this for us is what changes based on the selection of the popup menu) to our existing exportFormatOptionsView view (which contains the popup menu):
NSSize currentSize = [exportFormatOptionsView bounds].size;
NSView *newView = [exportAccessoryViewController view];
NSSize newSize = NSMakeSize( currentSize.width, currentSize.height+[newView bounds].size.height);
// resize the parent view
[exportFormatOptionsView setFrameSize: newSize];
// move the child view
[exportFormatOptionsView addSubview: newView];
of course, when you switch these out dynamically, you need to make sure you change the view size of the intermediate view back to the original size, so you can add the supplementary view in later, which I did like this:
NSSize currentSize = [exportFormatOptionsView bounds].size;
NSView *oldView = [exportAccessoryViewController view];
// pull it out
[oldView removeFromSuperview];
NSSize newSize = NSMakeSize( currentSize.width, currentSize.height-[oldView bounds].size.height);
[exportFormatOptionsView setFrameSize: newSize];
exportAccessoryViewController = nil;
Hope this helps.

My UITabBar doesn't show up

Okay guys, maybe you can help me out with this one. I'm about ready to pull my hair out.
Recently I decided to upgrade my app and make it look better, and with that I wanted to move it into full support for iPad platforms as well. For a while everything worked great. Just press copy MainWindow.xib for iPad, add the views that I used on the iPhone configurations, and everything should be great, but that didn't work too well. Take a look:
Here is the iPhone screenshot:
Here is the iPad screenshot:
Where's the tab bar? I don't understand! I added the initial view when I was first putting it together, but when I linked all of the IBOutlets to the proper pieces, the tab bar no longer shows up.
Screenshot of IB:
Tab Bar properties:
Tint: A bluish color
Image Tint: A goldish color
Mode: Scale to fill
Tag: 0
User Interaction Enabled: (Checked)
Multiple Touch: (Unchecked)
Alpha: 1
Opaque: (Checked)
Hidden (Unchecked)
Clears Graphic Context: (Checked)
Clip Subviews: (Unchecked)
Autoresize Subviews: (Checked)
Stretching: (x,y,w,h):(0,0,1,1)
The viewController.h file is a delegate for UITabBar, UITextField, and UITextView
ViewDidLoad (bar is the IBOutlet for the tab bar):
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMovieIntro];
NSURL *url = [NSURL URLWithString:#"http://www.faithlifefellowship.us/Audio/Sermons/NewSermonBanner.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
if(!image == NULL)
{
NewSermonBanner.image = image;
}
series = [[Series alloc] init];
SeriesName = #"";
NSRange range = [[[UIDevice currentDevice] name] rangeOfString:#"iPad"];
int i = 0;
if(range.location != NSNotFound)
i = 1;
bar.selectedItem = hometab;
//set delegates
[bar setDelegate:self];
[personalName setDelegate:self];
[personalEmail setDelegate:self];
[content setDelegate:self];
[prContent setDelegate:self];
[prName setDelegate:self];
[prEmail setDelegate:self];
}
I'm stumped. If you have any ideas or need any more information, let me know.
Thanks!
I'm going to give you a few things I'm getting in order to fix this. It will be tons easier if you could upload the source code for me/us to download and be able to pinpoint the problem.
Sometimes (I can't remember exactly when) I've had my navigation bar not show up because it was missing a connection.
Make sure you are not hiding the tab bar anywhere in code, though it doesn't seem to be the case since it shows up on iPhone.
Otherwise I'm gonna take a guess and say it's something in the NIB. Here are some things you can try:
Check all your connections to outlets
Make sure your objects in the NIB are of the correct class
Verify that the tab bar's "hidden" property is not check in Interface Builder
Compare and verify all the structure of the NIB file between iPhone and iPad
These are just some ideas :) again if you can post the code it would be fantastic.
Let us know how it goes,
Felipe
I had this problem with a app that support both iPhone and iPad. Make sure 'is initial view controller' is checked for the UITabBarController when you examine the view controller using the object inspector. When you do this, xcode will display a 'inbound' arrow on the left side of the view controller if you're using storyboards.

Is there Any way to imitate Lion's Launchpad?

I have been struggling to imitate Launchpad.
At the beginning I thought about making NSWindow bgcolor transparent:
//make NSWindow's bgcolor transparent
[window setOpaque:NO];
[window setBackgroundColor:[NSColor clearColor]];
But now I realized it's way more ideal to
capture wallpaper
blur it and make it bg-image for NSWindow or a view
Rather than hiding all the opened windows and menubar, which was the first idea I had have come with (Still not sure with better, if you had any better idea...).
Capture & blur wallpaper used by a user
Make it background image for nswindow or a view
Fade-in to fullscreen view
Click somewhere blank or press ESC to fade-out
Are those possible to achieve without using private APIs?
Sorry if it's not clear my poor English.
Simply I'm trying to imitate Launchpad-styled full screen.
Thanks for any advice.
To get an image of the desktop background, use:
NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageuRLForScreen:[NSScreen mainScreen]
NSImage *theDekstopImage = [[NSImage alloc] initWithContentsOfURL:imageURL];
You can blur the image using CIFilter. Here's a Apple doc describing how to apply filters.
And then you can load that image into a color and set that as the background color for the window. Additionally, set the window to have no style mask (no close buttons, title frame, etc.), cover the screen, and be in front of everything except the dock:
[yourWindow setBackgroundColor:[NSColor colorWithPatternImage:theDesktopImage]];
[yourWindow setStyleMask:NSBorderlessWindowMask];
[yourWindow setLevel:kCGDockWindowLevel - 1];
[yourWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
To have the window fade in, you can use NSWindow's animator proxy. (Replace 1.0 with 0.0 to make it fade out.)
[[yourWindow animator] setAlphaValue:1.0];
Of course you could customize that a bit more with things like CoreAnimation, but this should work just fine.
To handle background clicking, I suggest making a subclass of NSView where you -orderOut: your window on -mouseDown:. Then put an instance of that subclass that spans the entire frame of your window.
Also, NSViews sometimes don't respond to key events, so you can add an event listener to detect any time the escape key is pressed while your app is active:
[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:(NSEvent *ev)^ {
if([ev keyCode] == 0x53) {
[yourWindow orderOut:self];
}
return ev;
}

How to make a print dialog with preview for printing an image file

I have a mac cocoa image editing application. I would like to add print image functionality to my application. I basically always have a jpg/png file with me. Whenever the user goes to File -> Print menu I would like to give user a dialog with print options/settings to print this jpg/png file. I would like the print dialog to be customized for image printing such as one we get when printing an image from Preview application.
I don't want to use something like the following because this will give a plain not very customizable print dialog:
[NSPrintOperation printOperationWithView:viewToPrint printInfo:[self.document printInfo]]
Well, you can put everything inside a NSView and then use the print method. This method will print everything that's inside the view (including subviews). Now for the customizable printing you can make something like: User clicks a button labeled "print", it makes your custom made dialog appear where the user can choose options such as, show exemple date (which is in a subview, of the view that's going to be printed), you store those options and them when the user clicks the print at your dialog, you can remove the subview he didn't wanted to show. And after its printed, show it again. There's also other methods for printing views here: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html
Please use this code.
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSImage *pic = [[NSImage alloc] initWithContentsOfFile: #"/Users/Anne/Desktop/Sample.png"];
NSRect picRect = NSRectFromCGRect(CGRectMake(0, 0, pic.size.width, pic.size.height));
NSImageView *imageView = [[NSImageView alloc] initWithFrame:picRect];
[imageView setImage:pic];
NSPrintOperation * picPrint = [NSPrintOperation printOperationWithView:imageView printInfo:printInfo];
[picPrint setCanSpawnSeparateThread:YES];
[picPrint runOperation];