# Return Integer from DialogWindow

**URL:** <https://forum.juce.com/t/return-integer-from-dialogwindow/59905>\
**Category:** General JUCE discussion\
**Created:** [February 9, 2024, 6:54pm UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905 "2024-02-09T18:54:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![spiffo](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@spiffo](https://forum.juce.com/u/spiffo)\
**Post date:** [February 9, 2024, 6:54pm UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905/1 "2024-02-09T18:54:54Z")

</div>

OK there will be a simple answer to this but I cannot find it!

I have a DialogWindow that has a Component on it. The Component only has 3 Buttons and depending on which one is clicked we exitModalState(0), exitModalState(1) or exitModalState(2)

So how do I get that integer 0, 1 or 2 back to the DialogWindow and also returned from the openSwapDialog function?

Current code below, missing an integer to return:

```auto
	int MemoryBankComponent::openSwapDialog(int startSlot, int endSlot)
	{
		juce::DialogWindow::LaunchOptions dialog;
		dialog.content.setNonOwned(new DialogComponent());
		dialog.dialogTitle = "Swap Memory Slots!";
		dialog.componentToCentreAround = this;
		dialog.dialogBackgroundColour = getLookAndFeel().findColour(juce::ResizableWindow::backgroundColourId);
		dialog.escapeKeyTriggersCloseButton = true;
		dialog.useNativeTitleBar = false;
		dialog.resizable = false;
		dialog.useBottomRightCornerResizer = false;

		dialog.launchAsync();
	}

	class DialogComponent : public juce::Component
	{
	public:
		DialogComponent()
		{
			addAndMakeVisible(cancelButton);
			cancelButton.onClick = [this] { getParentComponent()->exitModalState(0); };
			addAndMakeVisible(swapInLibrarianButton);
			swapInLibrarianButton.onClick = [this] { getParentComponent()->exitModalState(1); };
			addAndMakeVisible(swapBothButton);
			swapBothButton.onClick = [this] { getParentComponent()->exitModalState(2); };

			setSize(400, 300);
		}
		~DialogComponent() override
		{
		}
		void resized()
		{
			swapInLibrarianButton.setBounds(getWidth() * 0.25, getHeight() * 0.2, getWidth()*0.5, getHeight()*0.1);
			swapBothButton.setBounds(getWidth() * 0.25, getHeight() * 0.5, getWidth() * 0.5, getHeight() * 0.1);
			cancelButton.setBounds(getWidth() * 0.25, getHeight() * 0.8, getWidth() * 0.5, getHeight() * 0.1);
		}
	private:
		// Buttons
		// -------
		juce::TextButton swapInLibrarianButton{ "Swap only in Librarian?" };
		juce::TextButton swapBothButton{ "Also swap in Hardware" };
		juce::TextButton cancelButton{ "Cancel" };
	};

```

Most of the solutions I found on the Forum refer to runModalLoop with an equal number of other articles saying DO NOT run ModalLoops! So I’m using launchAsync instead but I can’t find much reference to returning integers for that. I also looked at the DialogsDemo but tbh I cannot make head nor tail of it.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [February 9, 2024, 8:21pm UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905/2 "2024-02-09T20:21:06Z")

</div>

One method I use is to pass in callbacks that handle the button presses externally. In looking at my code I forget why I didn’t called exitModalState within the dialog. I will have to give that another look.

Contained component:

```auto
    LocateFileComponent (std::vector<juce::File> theMissingFiles, juce::File startingFolder,
                         std::function<void (std::vector<std::tuple <juce::File, juce::File>>)> theLocatedFilesCallback, std::function<void ()> theCancelCallback);

```

Creating dialog:

```auto
    juce::DialogWindow::LaunchOptions options;
    auto locateComponent { std::make_unique<LocateFileComponent> (filesToLocate, locateFilesInitialDirectory, [this] (std::vector<std::tuple <juce::File, juce::File>> locatedFiles)
    {
        // do things with the files
        locateDialog->exitModalState (0);
    },
    [this] ()
    {
        // handle the cancel (usually just doing nothing)
        locateDialog->exitModalState (0);
    }) };
    options.content.setOwned (locateComponent.release ());
    // etc
    locateDialog = options.launchAsync ();

```

---

<div class="post-metadata">

**Author:** ![spiffo](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@spiffo](https://forum.juce.com/u/spiffo)\
**Post date:** [February 10, 2024, 9:21am UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905/3 "2024-02-10T09:21:30Z")

</div>

My first approach was to use the buttons to call functions externally like this:

```auto
findParentComponentOfClass<MemoryBankComponent>()->swapLibrarianMemorySlots();
getParentComponent()->exitModalState(1);

```

But the problem I ran into there was that although the swapLibrarianMemorySlots() function is called from the MemoryBankComponent Class, it does NOT call the object / instance of that Class!

So the variables that I was using inside that function are not available and I end up with:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/a/1/a1ac1d655e5f867128fcc1dfb8a9081bc38764ad.png)

So if someone could show me how to call the Object rather than just the Class, that would be another solution.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [February 10, 2024, 12:40pm UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905/4 "2024-02-10T12:40:55Z")

</div>

My approach should not have this issue, as it captures the `this` pointer from the calling context.

---

<div class="post-metadata">

**Author:** ![spiffo](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@spiffo](https://forum.juce.com/u/spiffo)\
**Post date:** [February 11, 2024, 7:01pm UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905/5 "2024-02-11T19:01:50Z")

</div>

@cpr2323 you put me on the right track and I have a solution of sorts:

When you call the DialogWindow, pass in a pointer to the current Object, like:

```auto
openSwapDialog(this);

```

In the DialogWindow you call this pointer to the original Class & Object **‘m’** , and pass it into the Component you place on the DialogWindow, like:

```auto
	void MemoryBankComponent::openSwapDialog(MemoryBankComponent* m)
	{
		juce::DialogWindow::LaunchOptions dialog;
		dialog.content.setOwned(new DialogComponent(m));
		dialog.dialogTitle = "Swap Memory Slots!";
		dialog.componentToCentreAround = this;
		dialog.dialogBackgroundColour = getLookAndFeel().findColour(juce::ResizableWindow::backgroundColourId);
		dialog.escapeKeyTriggersCloseButton = true;
		dialog.useNativeTitleBar = false;
		dialog.resizable = false;
		dialog.useBottomRightCornerResizer = false;

		dialog.launchAsync();
	}

```

Now in the DialogComponent that is placed on the DialogWindow, below:

```auto
	class DialogComponent : public juce::Component
	{
	public:
		DialogComponent(MemoryBankComponent* m)
		{
			addAndMakeVisible(cancelButton);
			cancelButton.onClick = [this, m] { getParentComponent()->exitModalState(0); m->cancelPressed(); };
			addAndMakeVisible(swapInLibrarianButton);
			swapInLibrarianButton.onClick = [this, m] { getParentComponent()->exitModalState(1); m->swapInLibrarianPressed(); };
			addAndMakeVisible(swapBothButton);
			swapBothButton.onClick = [this, m] { getParentComponent()->exitModalState(2); m->swapBothPressed(); };

			setSize(400, 300);
		}
		~DialogComponent() override
		{
		}
		void resized()
		{
			swapInLibrarianButton.setBounds(getWidth() * 0.25, getHeight() * 0.2, getWidth()*0.5, getHeight()*0.1);
			swapBothButton.setBounds(getWidth() * 0.25, getHeight() * 0.5, getWidth() * 0.5, getHeight() * 0.1);
			cancelButton.setBounds(getWidth() * 0.25, getHeight() * 0.8, getWidth() * 0.5, getHeight() * 0.1);
		}

	private:
		// Buttons
		// -------
		juce::TextButton swapInLibrarianButton{ "Swap only in Librarian?" };
		juce::TextButton swapBothButton{ "Also swap in Hardware" };
		juce::TextButton cancelButton{ "Cancel" };
	};

```

In the **onClick** Lambdas you not only have **this** , referring to the DialogComponent, you also have **m** , which is a pointer to the original Class & Object that called all this in the first place!

Now you can use that pointer **m** to call the functions you want on the original Object, hooray!

I currently have a love / hate relationship with C++, mostly the latter at present 🙂

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [February 11, 2024, 8:46pm UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905/6 "2024-02-11T20:46:27Z")

</div>

Awesome that you have a solution! 🙂

Either I don’t understand this part, or it is redundant:

`void MemoryBankComponent::openSwapDialog(MemoryBankComponent* m)`

Is the caller of `openSwapDialog` a different instantiation of the class? ie. The `this` pointer inside of `openSwapDialog` is the pointer to the current instantiation of the class. Is it possible you don’t need it as a parameter to `openSwapDialog`, and could just do `dialog.content.setOwned(new DialogComponent(this));`

---

<div class="post-metadata">

**Author:** ![spiffo](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@spiffo](https://forum.juce.com/u/spiffo)\
**Post date:** [February 12, 2024, 3:27pm UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905/7 "2024-02-12T15:27:32Z")

</div>

Nope, you understand perfectly, you’re right, it isn’t necessary, the following works just fine:

```auto
	void MemoryBankComponent::openSwapDialog()
	{
		juce::DialogWindow::LaunchOptions dialog;
		dialog.content.setOwned(new DialogComponent(this));
		dialog.dialogTitle = "Swap Memory Slots!";
		dialog.componentToCentreAround = this;
		dialog.dialogBackgroundColour = getLookAndFeel().findColour(juce::ResizableWindow::backgroundColourId);
		dialog.escapeKeyTriggersCloseButton = true;
		dialog.useNativeTitleBar = false;
		dialog.resizable = false;
		dialog.useBottomRightCornerResizer = false;

		dialog.launchAsync();
	}

	class DialogComponent : public juce::Component
	{
	public:
		DialogComponent(MemoryBankComponent* m)
		{
			addAndMakeVisible(cancelButton);
			cancelButton.onClick = [this, m] { getParentComponent()->exitModalState(0); m->cancelPressed(); };
			addAndMakeVisible(swapInLibrarianButton);
			swapInLibrarianButton.onClick = [this, m] { getParentComponent()->exitModalState(1); m->swapInLibrarianPressed(); };
			addAndMakeVisible(swapBothButton);
			swapBothButton.onClick = [this, m] { getParentComponent()->exitModalState(2); m->swapBothPressed(); };

			setSize(400, 300);
		}
		~DialogComponent() override
		{
		}
		void resized()
		{
			swapInLibrarianButton.setBounds(getWidth() * 0.25, getHeight() * 0.2, getWidth()*0.5, getHeight()*0.1);
			swapBothButton.setBounds(getWidth() * 0.25, getHeight() * 0.5, getWidth() * 0.5, getHeight() * 0.1);
			cancelButton.setBounds(getWidth() * 0.25, getHeight() * 0.8, getWidth() * 0.5, getHeight() * 0.1);
		}

	private:
		// Buttons
		// -------
		juce::TextButton swapInLibrarianButton{ "Swap only in Librarian?" };
		juce::TextButton swapBothButton{ "Also swap in Hardware" };
		juce::TextButton cancelButton{ "Cancel" };
	};

```

I’m still not sure how you would use the exitModalState integers though? But at this stage I don’t need to as long as I can call different functions from the Object / Instance of MemoryBankComponent.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [February 12, 2024, 4:35pm UTC](https://forum.juce.com/t/return-integer-from-dialogwindow/59905/8 "2024-02-12T16:35:59Z")

</div>

re “_I’m still not sure how you would use the exitModalState integers though_”, there is no need to. If you really wanted to, you can get an instance of the `ModalComponentManager` and register a callback with it, but, that is a rather round-about way of doing what you are already doing.

Not that it is an issue here, but if you ever want to have a Dialog that is reusable, ie. the interface is the same, but the functions executed when the buttons are pressed, or just to reduce coupling, your solution won’t allow for that. Using the callback solution I suggested is how you would go. And, if you wanted to use a ‘return value’ type approach, the callback could be generic like `std::function<void onDialogButtonPress (int buttonIndex)>`, which the caller would pass in their lambda and handle the `buttonIndex` value there. I find the callback per button approach more explicit when using dialogs, so that is how I go. As well, as in my example, different buttons could produce different data.
