How to make main document window full size from within main component

Finishing up a stand-alone game, I started an “Audio Application” project from Projucer so it has Main.cpp, MainComponent.cpp and MainComponent.h

In Main.cpp class MainWindow I got this;

			setResizable (false, false);
			setResizeLimits(640, 360, 4096, 2080);
			centreWithSize (getWidth(), getHeight());

setResizable was set to false, false because I absolutely do not want the user to resize while the game is running.

However when game is not running, I have a “Start” and a “Resize Window” button, which for now simply toggles window size from small up to biggest possible screen pixel resolution. That works like a charm, but could be perfected.

  1. Whenever I change the size I would love for it to recenter to the middle of screen as when app first starts.

  2. When reaching max screen pixel resolution, I would love for the Main Document window to go full size, that is without the menu bar, but I can only get setFullScreen (true) to work from Main.cpp.

Below is my MainComponent “resize” code;

void MainComponent::buttonClicked(Button* button) {
	if (button == &appSizeButton) {
		appSize -= 0.5;
		if (appSize <= 0.5) appSize = 4;

		WindowWidth = getParentMonitorArea().getWidth() / appSize;
		WindowHeight = getParentMonitorArea().getHeight() / appSize;

		appSizeButton.setVisible(false);
		startButton.setVisible(false);
		setVisible(false);

		setSize(WindowWidth, WindowHeight);
		
		nextLevel();
		setVisible(true);
		addAndMakeVisible(appSizeButton);
		addAndMakeVisible(startButton);
	}
...
}

Full screen is a property of the Main Window, not of the Main content component. If you need to trigger the full screen from the child content component, you have to do some trickery, like get the top level parent component of the content component, dynamic_cast that to a ResizableWindow and then call setFullScreen.

auto window = dynamic_cast<ResizableWindow*>(getTopLevelComponent());
window->setFullScreen(true);

However, when testing that, I could get the window only to maximize, not go to the real full screen mode. Maybe a bug or limitation in the Juce code…

1 Like

Thanks. I have also been wondering if I could restart the app with a parameter which Main.cpp could use to decide to go full screen,

or (this may sound crazy as my knowledge on C++ leaves some to be desired),

but if there was a way to “exit” out of MainComponent.cpp “function”, again perhaps with a parameter telling Main.cpp either that it is a shutdown, or a resize.

I can’t get the fullscreen working even when calling it from the main.cpp code…How did you manage to do that?

Hold on trying to remember. I know I had that working in an earlier version.

Desktop:: setKioskModeComponent?
https://docs.juce.com/master/classDesktop.html#a8fa4033714b5a9cd0714c9b9633b3366

1 Like

That must have been what I did. So the below is true full screen, no title or menu bar, called from Main.cpp

			Desktop::getInstance().setKioskModeComponent(this);
			setFullScreen(true);

Now my question was actually how about setting that from within a component, and when I resize component how to automatically recenter related to screen resolution.

Oh well answering one of my own questions as, the below actually works called from my MainComponent.;

Desktop::getInstance().setKioskModeComponent(this);

Just one last thing, how to recenter, because after going true full screen I can no longer drag the window to the right position as there are no titlebar.

Getting real close. With below when cycling through app sizes, from small to full screen, appSize = 1 being full screen, and then starting over with small size, it now automatically centers non-full screen, and goes true full screen first time around, except it is not centered. However next time I change to full screen it suddenly has a title bar. Also note that below window->centreWithSize(getWidth(), getHeight()); did not work for full screen.

		setSize(WindowWidth, WindowHeight);

		if (appSize == 1) {
			Desktop::getInstance().setKioskModeComponent(this);
		}
		else {
			auto window = dynamic_cast<ResizableWindow*>(getTopLevelComponent());
			window->centreWithSize(getWidth(), getHeight());
		}

Please don’t take this the wrong way, but… Did you read the docs for setKioskModeComponent?

To exit kiosk mode, just call setKioskModeComponent (nullptr). When this is called, the component that’s currently being used will be resized back to the size and position it was in before being put into this mode.

So, you should probably do that when exiting kiosk mode.

As for resizing in kiosk mode, I’m not sure what the problem is. Although since it looks like the window you are resizing may be taking up the whole screen, a simple window->setBounds (0, 0, getWidth(), getHeight()); may do the trick.

1 Like

Oops I forgot to read into that, thanks!

The Desktop::getInstance().setKioskModeComponent(nullptr); did fix half the remaining problem.

However window->setBounds(0, 0, getWidth(), getHeight()); puts back the titlebar. So my only problem now is to move the “true” full screen window back to top left corner.

Ok fixed that with; window->setTopLeftPosition(0, 0);

Awesome!

So for anyone wanting to get true full screen, with no title or menu bar, and/or wanting to change app size, from non-full size with menu bar, to true full screen kiosk mode and back, all within MainComponent here is how to do it. Note Main.cpp is still the same as in original post.

if (button == &appSizeButton) {
		appSize -= 0.5;
		if (appSize < 1) appSize = 4;

		// Exit kiosk mode?
		if (appSize == 4) Desktop::getInstance().setKioskModeComponent(nullptr);
		
		WindowWidth = getParentMonitorArea().getWidth() / appSize;
		WindowHeight = getParentMonitorArea().getHeight() / appSize;

		// Remove component
		setVisible(false);

		// Set new size
		setSize(WindowWidth, WindowHeight);

		// Either go full screen kiosk mode
		auto window = dynamic_cast<ResizableWindow*>(getTopLevelComponent());
		if (appSize == 1) {
			Desktop::getInstance().setKioskModeComponent(this);
			window->setTopLeftPosition(0, 0);
		}
		// Or a smaller size that is centered horizontal and vertical
		else {
			window->setTopLeftPosition((getParentMonitorArea().getWidth() - WindowWidth) / 2, 
				(getParentMonitorArea().getHeight() - WindowHeight) / 2);
		}}

		// Update needed variables to new app size and re-add screen components
		ballRestart();
		newBricks(false);
		setVisible(true);

Finally for true kiosk mode to work, without minimize, maximize, and close buttons, in Main.cpp when creating the Document Window, you need to set requiredButtons to zero like this;

MainWindow (String name)  : DocumentWindow (name, 
                                                    Desktop::getInstance().getDefaultLookAndFeel()
                                                                          .findColour (ResizableWindow::backgroundColourId), 0)

Thank you very much for your help!

And thank you very much for your help!

Slight correction to my above working example, as during testing I failed to notice that window->centreWithSize(getWidth(), getHeight()); actually cuts of a vertical area in the bottom equal to the height of the menu bar. So after having resized to app sizes less than full screen kiosk mode, do this;

window->setTopLeftPosition((getParentMonitorArea().getWidth() - WindowWidth) / 2, (getParentMonitorArea().getHeight() - WindowHeight) / 2);

And I edited above to reflect this to set your app to the middle of the screen or use any position you need, but don’t use centreWithSize in this context.