Multiple Documents Open in Single App Instance

Hi. My application has one main window into which I load all the user’s project file data and when that window closes the app closes with it.

And I use a mutex to prevent multiple instances of the same app opening on a network and using the same license key.

But really I would like a single user (not on a network) to be able to open multiple project files into different instances of the same main window and then choose which window/project to view via the menubar “Window” menu item. And this has to work on Mac and Windows.

How can I achieve this? And how do other app windows know which main window to interact with if there are multiple main windows/projects open?

Thanks.

In the NewDocument and OpenDocument events you can create copies of your window.
Turn Implicit Instance off on your main window, and then create copies of it when the user opens or creates a new document. If App.AutoQuit = true then the app won’t quit until all the windows are closed.

So something along the lines of

dim w as new winMyMainWindow  // Variable name is not important, it goes out of scope real quick.
w.SetDocument(File)  // SetDocument would be a method you create on your window to take in the document and handle it
w.show

make sure to understand OO (object oriented programming) , which is the answer to your question.

Thanks, Tim. I had never realized these two events existed. But I am still a little confused.

  1. Say my main window is called “MainWindow” and I have hard-coded throughout the app modules and other windows code referring to it, such as:
If MainWindow.customvalue = 1
If MainWindow.Height = 500
If self.name is "MainWindow"
If self = MainWindow
Mainwindow.DoThis()

… and so on, how do I handle directly referring to the properties and methods and events and name of that MainWindow.

  1. If there’s a floating “FindWindow” for example, how can it know on its “Search” button click what MainWindow instance to work with?

  2. If you manually state window.show then do you have to state window.hide to hide the other instances?

  3. What would your advice be as to the best way to convert my existing code that currently uses a single “MainWindow” to multiple instances of it?

[quote=304178:@Denise Adams]Thanks, Tim. I had never realized these two events existed. But I am still a little confused.

  1. Say my main window is called “MainWindow” and I have hard-coded throughout the app modules and other windows code referring to it, such as:

… and so on, how do I handle directly referring to the properties and methods and events and name of that MainWindow.
[/quote]
Unfortunately you won’t be able to make your app handle multiple documents like this. You had said, “window into which I load all the user’s project file data,” which I assumed you meant the window was entirely self contained. If anywhere you have to refer to the window by name you have to refactor your code so that it refers to the window by reference. There are multiple ways to approach it, depending on your code it may be easiest to pass the window reference to the method as a parameter.

The FindWindow should loop through all the open windows, and find the document window that’s frontmost. You’ll see this is the behavior most document based apps use. That way you can bring another document forward, and use the same search window.

I’m not sure why you need to hide the other instances, that’d be like Pages deciding you no longer need to see a document you had open. When the user closes the window it is destroyed, you won’t have to manually manage that.

Edit: If for some reason you must find the other open windows and hide them you can loop through them using Window(i) and WindowCount

Depending on how much you’ve relied on the window always being MainWindow and the design of your code, it may be easier to rewrite from the ground up. The more you try to bend the code you’ve already written, the messier the project becomes. My app Answers is going to need a total rewrite to behave like a multiple document application, so that’s on the low priority to-do list - it happens.

Thanks for your advice. I guess I’ll need to think long and hard about whether or not to do this and if so the best way to approach it based on what you have suggested.

Does anynone have any advice/tips for creating multiple document applications, especially when you have code in modules referring to various main window instances etc? Are there any templates available or Xojo examples?

The XojoText example is a simple multiple document app:

Examples/Sample Applications/XojoText

And its related 2-part webinar:

Thanks, Paul. That’s exactly what I needed.

If anyone else has any tips or advice about things I might need to think about and common pitfalls and cross-platform considerations etc then please let me know.