On Thursday, August 29, 2024 12:11 CEST, "H. Nikolaus Schaller" 
<[email protected]> wrote:

> Hi, 
> applicationDidFinishLaunching is called after loading the app (but not after 
> loading documents).
> To do the same after loading a document you must use an awakeFromNib method 
> in your Document class.
> 
> The reason is as you an see from the log that your characterWindow isn't 
> connected yet to the window.

indeed, moving that into the awakeFromNib:

      [characterWindow makeKeyAndOrderFront: self];      
      NSLog(@"awakeFromNib: the key Window: %@", [[NSApplication 
sharedApplication] keyWindow]);

make the window pop up. Unfortunately also on startup of the application, 
whereas I only want it to be 
around once I either load or create a new document. Something to figure out.

When I edit the document to edit the name of the character, I have this:

- (IBAction)nameChanged:(id)sender {
    NSString *newName = [self.name stringValue];
    [self.character setValue:newName forKey:@"name"];
    
    NSLog(@"calling updateChangeCount!!!");
    [self updateChangeCount:NSChangeDone];
    NSLog(@"isDocumentEdited? %i", [super isDocumentEdited]);
    [self checkIfWindowIsKey];  // Check if the window is key
}

- (void)checkIfWindowIsKey {
// commented out approach doesn't work only calling my window directly as below
//    if ([[self windowForSheet] isKeyWindow]) {
    if ([characterWindow isKeyWindow]) {
        NSLog(@"The document window is the key window.");
    } else {
        NSLog(@"The document window is not the key window.");
    }
}

creates output:

Character's name changed to: Bula
calling updateChangeCount!!!
isDocumentEdited? 1
The document window is the key window.

However, the save, save as, .... menu items, they all stay grayed out.

when I override the 
- (BOOL)isDocumentEdited {
    return YES;
}

then the "save all" menu item is active, and I can save the file. But "save", 
save as... stay gray.
Somehow, I'm not properly marking the document as "dirty" ?

I guess probably something similarly simple and stupid ;)

thanks,
Sebastian

> 
> BR,
> Nikolaus
> 
> > Am 29.08.2024 um 14:34 schrieb Sebastian Reitenbach 
> > <[email protected]>:
> > 
> > Hi Nikolaus,
> > 
> > On Thursday, August 29, 2024 09:54 CEST, "H. Nikolaus Schaller" 
> > <[email protected]> wrote:
> > 
> >> Hi,
> >> there may be a completely different approach:
> >> 
> >> - add an IBOutlet (e.g. called mainWindow) to your NSDocument for your 
> >> NSWindow and connect it.
> >> - then you get a pointer mainWindow that can be accessed in -awakeFromNib; 
> >> for your makeKeyAndOrderFront:
> >> 
> >> This avoids scanning the top level objects.
> > 
> > thank you. That seems to do the trick for the simple test App I created. 
> > After creating IBOutlet and wiring it up in Gorm, I added the following at 
> > the end of applicationDidFinishLaunching:
> > 
> >      [mainWindow makeKeyAndOrderFront: self];      
> >      NSLog(@"the key Window: %@", [[NSApplication sharedApplication] 
> > keyWindow]);
> > 
> > which spits out:
> > 2024-08-29 11:49:14.793 Test5[72122:2615662661832] the key Window: 
> > <NSWindow: 0x2609f549008>Number: 4 Title: Test5 Window
> > 
> > However, that same approach doesn't work in my Document based app. I read 
> > through David's "Cocoa Programming, Developers Handbook", and now I'm 
> > working with ChatGPT as my mentor here, since never done that before, and 
> > don't have any Mac programming experience ;)
> > What I told ChatGPT I want to do: MVC Document based app, that should have 
> > a central .gorm file for menu and other central stuff, and then .gorm files 
> > for each of different types of documents to be handled.
> > 
> > Now I ended up with:
> > * central .gorm file containing my menu, connected to an AppDelegate, that 
> > loads the Gorm file.
> > * a DocumentController class (NSDocumentController subclass), which based 
> > on what types of documents I want to create new, calls out one of my two 
> > Document classes (NSDocument subclasses). To each of these NSDocument 
> > subclasses, I've separate .gorm files.
> > 
> > Opening the Window with your approach works so far that the window opens, 
> > but it doesn't become the key window:
> > 
> > With this in the makeWindowControllers method:
> > 
> >  [characterWindow makeKeyAndOrderFront:self];
> >  NSLog(@"THE KEY WINDOW: %@", [[NSApplication sharedApplication] 
> > keyWindow]);
> > 
> > it opens the Window, but logs:
> > THE KEY WINDOW: (null)
> > 
> > With above output, it's the only window (besides the menu), that the 
> > application has, and it takes 
> > input, but I wonder why the application doesn't even have a keyWindow at 
> > all?
> > I thought at least one should always be Key, and with that firstResponder, 
> > but may have misunderstood? 
> > 
> > ChatGPT insists on going through the topLevelObjects. But now, I'm not 
> > sure, even if that works,
> > if it then would make the window key?
> > 
> > Sebastian
> >> 
> >> Hope this helps,
> >> Nikolaus
> >> 
> >> 
> >>> Am 29.08.2024 um 12:06 schrieb Sebastian Reitenbach 
> >>> <[email protected]>:
> >>> 
> >>> Hi,
> >>> 
> >>> I'm trying to create a document based app with ProjectCenter and Gorm, 
> >>> following different advices,
> >>> it boils down to use NSBundles loadNibNamed: owner: topLevelObjects:
> >>> Then afterward, find the window in the topLevelObjects, that I want to 
> >>> have my key window, and make it makeKeyAndOrderFront:
> >>> 
> >>> Whatever I do, the topLevelObjects I get returned are empty.
> >>> 
> >>> To demonstrate, I just created a New Application in ProjectCenter, opened 
> >>> the Interface in Gorm, and 
> >>> dragged a NSWindow into the Objects. Example code here: 
> >>> https://github.com/buzzdeee/testTopLevelObjects
> >>> 
> >>> Then added this to AppController.m applicationDidFinishLaunching:
> >>> 
> >>>  NSLog(@"here in AppController  applicationDidFinishLaunching...");
> >>> 
> >>> NSBundle *mainBundle = [NSBundle mainBundle];
> >>> NSArray *topLevelObjects = nil;
> >>> NSLog(@"going to load .gorm file");
> >>> BOOL success = [mainBundle loadNibNamed:@"Test5" owner:self 
> >>> topLevelObjects:&topLevelObjects];  
> >>> NSLog(@"topLevelObjects: %@", topLevelObjects);
> >>>   if (success) {
> >>>       NSLog(@"Successfully loaded Test5.gorm");
> >>> 
> >>>       if (topLevelObjects == nil || [topLevelObjects count] == 0) {
> >>>           NSLog(@"No top-level objects were loaded.");
> >>>       } else {
> >>>           NSLog(@"Top-level objects: %@", topLevelObjects);
> >>>           for (id obj in topLevelObjects) {
> >>>               NSLog(@"Loaded object: %@", NSStringFromClass([obj class]));
> >>>           }
> >>>       }
> >>>   } else {
> >>>       NSLog(@"Failed to load Test5.gorm");
> >>>   } 
> >>> }
> >>> 
> >>> The output is:
> >>> 2024-08-29 09:20:30.103 Test5[81465:10651290713480] here in AppController 
> >>>  applicationDidFinishLaunching...
> >>> 2024-08-29 09:20:30.104 Test5[81465:10651290713480] going to load .gorm 
> >>> file
> >>> 2024-08-29 09:20:30.113 Test5[81465:10651290713480] topLevelObjects: 
> >>> (null)
> >>> 2024-08-29 09:20:30.113 Test5[81465:10651290713480] Successfully loaded 
> >>> Test5.gorm
> >>> 2024-08-29 09:20:30.113 Test5[81465:10651290713480] No top-level objects 
> >>> were loaded.
> >>> 
> >>> When in Gorm, I enable "Visible at launch" for the NSWindow object, 
> >>> output changes to:
> >>> 
> >>> 2024-08-29 09:13:57.990 Test5[1172:4346915151048] here in AppController  
> >>> applicationDidFinishLaunching...
> >>> 2024-08-29 09:13:57.990 Test5[1172:4346915151048] going to load .gorm file
> >>> 2024-08-29 09:13:57.998 Test5[1172:4346915151048] Exception occurred 
> >>> while loading model: Index 3 is out of range 3 (in 'objectAtIndex:')
> >>> 2024-08-29 09:13:57.998 Test5[1172:4346915151048] Failed to load Gorm
> >>> 2024-08-29 09:13:57.998 Test5[1172:4346915151048] topLevelObjects: (null)
> >>> 2024-08-29 09:13:57.998 Test5[1172:4346915151048] Failed to load 
> >>> Test5.gorm
> >>> 
> >>> However, the .gorm file still loads, and I get to see the menu and 
> >>> window. But I guess that is because, NSMainNibFile = "Test5.gorm"; in my 
> >>> Info.plist.
> >>> 
> >>> In any case, shouldn't there be some topLevelObjects in the .gorm file? 
> >>> 
> >>> latest gnustep-make/base/gui/back/objc2/ProjectCenter releases, Gorm-1.3.1
> >>> 
> >>> thanks,
> >>> Sebastian
> >>> 
> >>> 
> >> 
> > 
>


Reply via email to