kachigusa
kachigusa

Reputation: 209

How to create a dialog box to gather user input in Fortran

I am trying to make a Fortran code showing some dialog box for Windows environments. For example, I have succeeded to make a simple "OK/Cancel" dialog as shown below.

PROGRAM test_dialog
!
    implicit none
!
    write(*,*) OKCancelDialog( "Continue?"//achar(0), "Test dialog"//achar(0) )
!
CONTAINS
!
    FUNCTION OkCancelDialog( message, dlg_title ) RESULT( res )
!
        use ifwin
!
        implicit none
!
        character( len = * ), intent( in ) :: message
        character( len = * ), intent( in ) :: dlg_title
        integer                            :: res
!
        integer( SINT )                    :: ret
!
        ret = MessageBox( GetForegroundWindow(), message, dlg_title, MB_ICONQUESTION + MB_OKCANCEL )
!
        res = 0
        select case( ret )
            case( IDOK )
                res = 1
            case( IDCANCEL )
                res = 0
            case default
                res = 0
        end select
!
    END FUNCTION OkCancelDialog
!
END PROGRAM test_dialog

But, I don't know how to make a dialog box to gather user input (e.g., number or text), like following attached image, by calling Win32 APIs (or other ways) from Fortran.

enter image description here

Does anyone have good idea for this purpose? It will be really appreciated if you share some wisdom (e.g., example code).

Upvotes: 4

Views: 1405

Answers (2)

tinyfiledialogs
tinyfiledialogs

Reputation: 1365

In C, tinyfiledialogs offers many modal cross-platform dialogs and popup notifications (both for graphic and console mode). I am the author. It includes examples of how to call this C code from Fortran.

    aTitle = "a Title" // char(0)
    aMessage = "a Message" // char(0)
    aDefaultInput = "an Input" // char(0)
    cpointer = tinyfd_inputBox(aTitle, aMessage, c_loc(aDefaultInput) )
    if ( c_associated(cpointer) ) then
        call c_f_pointer(cpointer, fpointer) ! Convert C Pointer to Fortran pointer     
        string = fpointer(1:index(fpointer,c_null_char)-1) ! Remove NULL character at the end
        write (*,'(A)') string
    endif

*** v3.10 (2023): NEW FORTRAN interface module fully implemented with examples ***

*** v3.13 (2023): NEW PASCAL interface module fully implemented with examples ***

In Fortran, tinyfiledialogs handles char strings as UTF-8 (on windows, macos and unix).

Upvotes: 1

Steve Lionel
Steve Lionel

Reputation: 7267

It would help if you specified which Fortran compiler you're using, as some include Windows API definition modules and other helpful items. Intel Visual Fortran has modules and also a bunch of examples that can be downloaded as part of the Intel Parallel Studio XE for Windows Samples Bundle - see the compiler_f subfolder of that. For pure API calls, the Win32\Angle sample contains a dialog text box that it reads from, so you can study that. This should translate well (other than module names) to other compilers. Do keep in mind that the WinAPI calls are all STDCALL (on 32-bit), so make sure whichever interfaces you are using specify that.

Intel Fortran also provides a more Fortran-friendly dialog box library which can handle a lot of what you need. The sample Dialog\WHIZZY illustrates many controls using this.

Your sample code shows only a message box, which is NOT a dialog. Dialog boxes require the use of a resource editor to define the dialog, and then code to display the dialog window, wait for input, and then process the user's choices.

Upvotes: 3

Related Questions