Igor Delovski Board Forum Index Igor Delovski Board
My Own Personal Slashdot!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Windows & Screens

 
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Mac Cocoa
Mac Cocoa  
Author Message
XNote
Kapetan


Joined: 16 Jun 2006
Posts: 532

PostPosted: Fri Jul 29, 2022 8:53 pm    Post subject: Windows & Screens Reply with quote

so - Display window on all screens

Code:
- (void)displayOnAllScreens
{       
    NSArray *screenArray = [NSScreen screens];

    _tempWindows = [[NSMutableArray alloc] init];

    if ([screenArray count] == 1) {
        [self displayOnScreen:[NSScreen mainScreen]];
    } else {
        for (int i=0; i<[screenArray count]; i++) {
            [self displayOnScreen:[screenArray objectAtIndex:i]];
        }
    }
}
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3036
Location: Europe

PostPosted: Thu Jun 01, 2023 10:03 pm    Post subject: Reply with quote

adc - RoundTransparentWindow - sample app

"This sample demonstrates how to create windows with custom shapes, no
title bar, and transparent content. It also shows how to change the shape of
the window and recalculate the drop shadow around the window border."


https://stackoverflow.com/questions/19940019/nswindow-with-round-corners-and-shadow
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3036
Location: Europe

PostPosted: Mon Aug 07, 2023 7:57 pm    Post subject: Reply with quote

so - Setting NSScrollView Contents to Top Left Instead of Bottom Left When Document Swapping

"Although isFlipped is subclassing the custom view in Interface Builder, the
documentView is getting replaced on the view swap, the first of which gets
used on awakeFromNib."


Code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
   _window = [[[NSApplication sharedApplication] windows] firstObject];
}
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3036
Location: Europe

PostPosted: Thu Aug 10, 2023 2:50 pm    Post subject: Reply with quote

so - How do I create a Cocoa window programmatically?

"A side note, if you want to programatically instantiate the application
without a main nib, in the main.m file / you can instantiate the AppDelegate
as below. Then in your apps Supporting Files / YourApp.plist Main nib base
file / MainWindow.xib delete this entry. Then use Jason Coco's approach to
attach the window in your AppDelegates init method."


Code:
#import "AppDelegate.h":

int main(int argc, char *argv[])
{

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  [NSApplication sharedApplication];

  AppDelegate *appDelegate = [[AppDelegate alloc] init];
  [NSApp setDelegate:appDelegate];
  [NSApp run];
  [pool release];
  return 0;
}


"This code will create your borderless, blue window at the bottom left of the
screen:"


Code:
NSRect frame = NSMakeRect(0, 0, 200, 200);
NSWindow* window  = [[[NSWindow alloc] initWithContentRect:frame
                    styleMask:NSBorderlessWindowMask
                    backing:NSBackingStoreBuffered
                    defer:NO] autorelease];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront:NSApp];
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3036
Location: Europe

PostPosted: Thu Aug 10, 2023 3:51 pm    Post subject: Reply with quote

NWindow, how to create a modal window?

ChatGPT
To create a modal window using NSWindow in macOS, you can follow
these steps:

Create a New Window Controller:

First, create a new subclass of NSWindowController to manage your modal
window. You can do this by creating a new Cocoa Class in Xcode and
choosing "NSWindowController" as the subclass.

Design the Window:

In Interface Builder, design the content of your modal window by dragging
and dropping interface elements onto the window. You can also set the
window's style, size, and other attributes.

Connect Interface Elements:

If you need to interact with interface elements in the modal window, create
outlets and actions in your window controller to connect them to your code.

Show the Modal Window:

In your main view controller or wherever you want to trigger the modal
window, instantiate your window controller and present the window modally.

Here's a simplified example in Objective-C that demonstrates how to create
and display a modal window:


Code:
// ViewController.m

#import "ViewController.h"
#import "ModalWindowController.h" // Your modal window controller's header

@implementation ViewController

- (IBAction)openModalWindow:(id)sender {
    ModalWindowController *modalController = [[ModalWindowController alloc] init];
    [NSApp runModalForWindow:modalController.window]; // Run the modal loop
}

@end


Code:
// ModalWindowController.m

#import "ModalWindowController.h"

@implementation ModalWindowController

- (instancetype)init {
    self = [super initWithWindowNibName:@"ModalWindow"]; // NIB name of your modal window
    if (self) {
        // Additional initialization if needed
    }
    return self;
}

- (IBAction)closeModalWindow:(id)sender {
    [NSApp stopModal]; // Terminate the modal loop
    [self.window orderOut:self];
}

@end


In this example, the ViewController class triggers the opening of the modal window. When you call runModalForWindow: on your modal window controller, it enters a modal event loop where interactions are limited to the modal window until it's closed. The closeModalWindow: action in the modal window controller stops the modal event loop and closes the modal window.

Remember to replace "ModalWindow" with the actual name of your modal window's NIB file and adjust the code to match your project's structure and requirements.

Modal windows can be useful for capturing user input or displaying temporary interfaces without allowing interaction with the main window until the modal window is dismissed.
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3036
Location: Europe

PostPosted: Thu Aug 10, 2023 3:53 pm    Post subject: Reply with quote

But is there example of runModalForWindow:from inside or how would I
write that method myself?

ChatGPT

Certainly! Here's an example of how you might write your own custom
method that mimics the behavior of runModalForWindow: from within
another method. This example demonstrates the basic structure of a modal
loop using a custom loop method.

Please note that this is a simplified example and doesn't handle all the
intricacies and edge cases that the built-in runModalForWindow: method
manages. For real-world applications, it's recommended to use the provided
API.



Code:
#import "ModalWindowController.h"

@implementation ModalWindowController

- (void)customRunModal {
    [self.window makeKeyAndOrderFront:nil]; // Show the window
   
    // Enter custom modal loop
    NSModalSession modalSession = [NSApp beginModalSessionForWindow:self.window];
   
    while (1) {
        // Process events for the modal window
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
                                             untilDate:[NSDate distantPast]
                                                inMode:NSDefaultRunLoopMode
                                               dequeue:YES];
       
        if (event) {
            [NSApp sendEvent:event];
        } else {
            // Exit the modal loop when no more events are available
            [NSApp endModalSession:modalSession];
            break;
        }
    }
   
    [self.window orderOut:nil]; // Hide the window after exiting the loop
}

@end

In this example:

The customRunModal method is similar to the runModalForWindow: method
but provides a custom event processing loop.

The window is made key and ordered front using makeKeyAndOrderFront: to
ensure it's visible.

A custom modal loop is started using beginModalSessionForWindow:.

The loop processes events using a while loop and the
nextEventMatchingMask: method.

When no more events are available, the modal session is ended, and the
loop is exited using endModalSession:.

The window is ordered out (hidden) after the loop is finished using orderOut:.

This example illustrates the concept of a modal loop but may not cover all
the scenarios and edge cases that runModalForWindow: handles. It's
important to be cautious and thoroughly test any custom modal loop
implementation in real-world applications.


Last edited by Ike on Thu Aug 10, 2023 3:55 pm; edited 1 time in total
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3036
Location: Europe

PostPosted: Thu Aug 10, 2023 3:55 pm    Post subject: Reply with quote

This looks interesting but somehow it makes no sense that the window
should be closed if there are no more events. Like, can a modal window sit
idle?

ChatGPT

You're correct, and I apologize for any confusion. In a typical modal window
scenario, the window should not be automatically closed when there are no
more events to process. The purpose of a modal window is to ensure that
the user interacts with the modal interface before returning control to the
main application.

In the provided example, the modal loop is exited when no more events are
available, which is not the behavior you would want in most cases. A modal
window is supposed to wait for user input and keep the window open until
the user explicitly interacts with it.

To clarify, a modal window can indeed sit idle until the user interacts with it
or until some specific action is performed within the modal window. Here's a
corrected version of the example to demonstrate how a modal window
should behave:


Code:
#import "ModalWindowController.h"

@implementation ModalWindowController

- (void)customRunModal {
    [self.window makeKeyAndOrderFront:nil]; // Show the window
   
    // Enter custom modal loop
    NSModalSession modalSession = [NSApp beginModalSessionForWindow:self.window];
   
    while (1) {
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
                                             untilDate:[NSDate distantFuture] // Wait indefinitely
                                                inMode:NSDefaultRunLoopMode
                                               dequeue:YES];
       
        if (event) {
            [NSApp sendEvent:event];
        }
       
        // Handle specific conditions that should exit the modal loop
        if (someCondition) {
            [NSApp endModalSession:modalSession];
            break;
        }
    }
   
    [self.window orderOut:nil]; // Hide the window after exiting the loop
}

@end

In this corrected example:

The modal loop is set to wait indefinitely for events using [NSDate
distantFuture].

The loop continues processing events until a specific condition is met. This
condition could be a button click, user input, or any event that should signal
the end of the modal interaction.

Once the condition is met, the modal loop is exited using endModalSession:,
and the window is hidden using orderOut:.

Again, please note that the custom modal loop in this example is a simplified
demonstration. In real-world applications, you should carefully design and
test the modal interaction to ensure that it meets your application's
requirements and provides a smooth user experience.
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3036
Location: Europe

PostPosted: Thu Aug 10, 2023 4:02 pm    Post subject: Reply with quote

ha, thanks. So maybe NSWindowDelegate should catch -windowShouldClose: method or catch a ESC key and then close the window?

ChatGPT

Exactly! The NSWindowDelegate protocol and its methods are commonly
used to control the behavior of windows, including modal windows. You can
use the windowShouldClose: method to intercept the user's intent to close
the window (for example, by clicking the close button or pressing the ESC
key) and handle it according to your requirements.

Here's an example of how you might use the windowShouldClose: method to
prevent a modal window from closing immediately when the user clicks the
close button or presses ESC:


Code:
#import "ModalWindowController.h"

@implementation ModalWindowController

- (void)customRunModal {
    [self.window makeKeyAndOrderFront:nil]; // Show the window
    self.window.delegate = self; // Set the window delegate
   
    // Enter custom modal loop
    NSModalSession modalSession = [NSApp beginModalSessionForWindow:self.window];
   
    while (1) {
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
                                             untilDate:[NSDate distantFuture] // Wait indefinitely
                                                inMode:NSDefaultRunLoopMode
                                               dequeue:YES];
       
        if (event) {
            [NSApp sendEvent:event];
        }
       
        // Handle specific conditions that should exit the modal loop
        if (someCondition) {
            [NSApp endModalSession:modalSession];
            break;
        }
    }
   
    [self.window orderOut:nil]; // Hide the window after exiting the loop
    self.window.delegate = nil; // Reset the window delegate
}

#pragma mark - NSWindowDelegate

- (BOOL)windowShouldClose:(NSWindow *)sender {
    // Prevent the modal window from closing immediately
    return NO;
}

@end

In this example, the windowShouldClose: method returns NO to prevent the
modal window from closing when the user clicks the close button or presses
the ESC key. This gives you the opportunity to handle the window closure
within your modal loop, ensuring that the user interaction is completed
before the window is closed.

Remember to set the window delegate back to nil after the modal interaction
is complete to avoid unexpected behavior in other parts of your application.
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3036
Location: Europe

PostPosted: Thu Aug 17, 2023 10:24 pm    Post subject: Reply with quote

so - Is there a way to make a custom NSWindow work with Spaces

"After a long time I found a solution to this annoying problem. Indeed
[window setMovableByWindowBackground:YES]; conflicts with my own
resizing methods, the window trembles, it looks awful!"
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Mac Cocoa All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Delovski.hr
Powered by php-B.B. © 2001, 2005 php-B.B. Group