Home Forums General Programming Modal Window Return Values

This topic contains 11 replies, has 2 voices, and was last updated by lfo2vco lfo2vco 3 years, 11 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #14020
    lfo2vco
    lfo2vco
    Participant

    Hi, I am using a Modal Window based of Atom’s demo panel. However the function attached to it runs regardless of whether I select OK or cancel.

    I have tried various lines of code to try to capture the return values but with no success. Here’s the method:

    function renamePatch()
    
    if panel:getRestoreState() == true or panel:getProgramState() == true then
    	return
    end
    
    -- ////////// Get Name For Rename Dialogue //////////
    
    nameData = L(PatchLabel:getProperty("uiLabelText"))
    
    function trim (s)
    	return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
    end
    
    nameTrim = (trim(nameData))
    
    -- ////////////////////////////////////////////
    -- ////////// Create Rename Dialogue //////////
    
    	modalWindow = AlertWindow("\n\nRename the Editor patch,\nup to 16 characters long.", "Please remember to save the\namended patch and name to\nyour Kiwi-3P synthesizer.", AlertWindow.InfoIcon)
    	modalWindow:addButton("Cancel", 0, KeyPress(KeyPress.escapeKey), KeyPress())
    	modalWindow:addButton("    OK    ", 1, KeyPress(KeyPress.returnKey), KeyPress())
    	modalWindow:addTextEditor ("renameTextEditor", (nameTrim), "Rename As:", false)
    	modalWindow:setModalHandler(windowCallback)
    
    	modalWindow:runModalLoop()
    
    	textEditor = modalWindow:getTextEditor("renameTextEditor"):getText()
    	if textEditor ~= nil then
    		PatchLabel:setProperty("uiLabelText", (textEditor), false)
    	end
    		if textEditor == "" then
    			PatchLabel:setProperty("uiLabelText", "Untitled Patch  ", false)
    		end
    
    end
    
    -- ////////// End Rename Dialogue //////////
    -- ////////////////////////////////////////////

    So the question is two part; What code do I need to capture the OK & Cancel values? Should it go in the windowCallback method or in the method above?

    #14021
    atom
    atom
    Moderator

    runModalLoop() returns a number that indicates what button was pressed to dismiss the window.

    #14022
    lfo2vco
    lfo2vco
    Participant

    Cheers Atom, so something like:

    if runModalLoop == 1 then
    textEditor = modalWindow:getTextEditor blah blah blah

    Should do it then.

    #14023
    atom
    atom
    Moderator
    ret = modalWindow:runModalLoop()
    
    if ret == 1 then
    -- i think this is OK button
    end
    if ret == 0 then
    -- this should be cancel
    end
    
    -- but it might be the other way around
    
    #14024
    lfo2vco
    lfo2vco
    Participant

    Excellent, indeed the values are the correct way around. Thanks again : )

    #14025
    lfo2vco
    lfo2vco
    Participant

    OK that works, however it requires each button to be pressed twice before dismissing the window. Same if I use keystrokes.

    #14041
    atom
    atom
    Moderator

    The double stuff might be caused bu the modal callbacks subsystem, If you want just a simple window that displays a messages and has a OK/Cancel button this will work

    ret = utils.questionWindow("title", "message", "OK", "Cancel")
    if ret then console ("true") else console("false") end
    

    Of if you want to get a text response from the user, you can use the other method in the utils

    askForTextInputWindow (const String title, const String message, const String initialInputContent, const String onScreenLabel, const bool isPassword, const String button1Text, const String button2Text);
    
    #14097
    lfo2vco
    lfo2vco
    Participant

    Hi Atom, I have finally found some time to sit down and look at this. I have opted for the ‘askForTextInputWindow’ as a text response is required.

    I have been trying to get the return values from the buttons in this window with no success… also I will need to get the initialInputContent. Any pointers gratefully received.

    I have been trawling the Juce site for info, but to no avail.

    #14098
    atom
    atom
    Moderator

    This method will never return anything except a string that is typed in the window. You won’t know what button was pressed i’m afraid you only get the string inside the window, this is a very simple “get a line of text from user” method with no error checking.

    #14099
    lfo2vco
    lfo2vco
    Participant

    Fair enough, so I can set button2 to false and just have an OK button then.

    #14102
    lfo2vco
    lfo2vco
    Participant

    MMMMmmmm that didn’t work, so I have window two buttons that in effect do the same thing and I cannot remove of one of them.

    I think in this instance it would be better of use the Model Window with only one button and no return callbacks. : )

    #14109
    lfo2vco
    lfo2vco
    Participant

    Hi Atom, it seems I have resolved my issue with the Modal Window…

    A silly misunderstanding on my part, which became obvious when I revisited the method. When you instructed me to add ret = modalWindow:runModalLoop() to get the button return values. I added the whole statement to the method.

    This meant that I ended up with

    modalWindow:runModalLoop()
    ret = modalWindow:runModalLoop()

    hence why the buttons required two presses before dismissing the window.

    Amateurs eh!! I realise now that I only needed to add ret =

    Thanks for your patience as always.

    • This reply was modified 3 years, 11 months ago by lfo2vco lfo2vco.
Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.

Comments are closed.