Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristopherson committed Aug 18, 2020
1 parent 0455d1b commit 050be51
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 0 deletions.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,80 @@ Drive: C:
Directory: \~\Documents\github\fcore\
Filename: CMakeLists
Extension: .txt
```

## Dialog Example
The following example illustrates how to interact with the user with OS-specific dialog boxes.
```fortran
program main
use ui_dialogs
implicit none
! Local Variables
logical :: check
type(dialog_result) :: mbox
type(text_output_dialog_result) :: openfile, savefile, folder
type(file_filter) :: filters(2)
! Initialize the UI environment
check = initialize_ui_environment()
if (.not.check) then
print '(A)', "Could not initialize the UI environement. Good bye."
return
end if
! Show the user a message box
mbox = show_message_box("Testing...", "Example", MSGBX_BTN_OK_CANCEL, &
MSGBX_ICON_INFORMATION)
if (mbox%result == DIALOG_RESULT_OK) then
print '(A)', "The user pressed OK."
else if (mbox%result == DIALOG_RESULT_CANCEL) then
print '(A)', "The user pressed Cancel."
else
print '(AI0A)', "The user pressed an unknown button with result: ", &
mbox%result, "."
end if
! Set up file filters
filters(1)%name = "Text Files (*.txt)"
filters(1)%pattern = "*.txt"
filters(2)%name = "All Files (*.*)"
filters(2)%pattern = "*.*"
! Open an file
openfile = show_open_file_dialog(filters)
if (openfile%result == DIALOG_RESULT_OK) then
print '(A)', "The user selected file: " // openfile%string_list(1)%str
end if
! Save a file
savefile = show_save_file_dialog("txt", filters)
if (savefile%result == DIALOG_RESULT_OK) then
print '(A)', "The user wishes to save file: " // &
savefile%string_list(1)%str
end if
! Locate a directory
folder = show_browse_folder_dialog()
if (folder%result == DIALOG_RESULT_OK) then
print '(A)', "Selected Folder: " // folder%string_list(1)%str
end if
! Be sure to clean up
call clean_up_ui_environment()
end program
```
The dialogs shown appear as follows on a Windows machine.
![](images/message_box.png?raw=true)
![](images/open_file_dialog.png?raw=true)
![](images/save_file_dialog.png?raw=true)
![](images/folder_dialog.png?raw=true)

The command-line output of the program is as follows.
```text
The user pressed OK.
The user selected file: C:\~\Documents\github\fcore\CMakeLists.txt
The user wishes to save file: C:\~\Documents\github\fcore\test.txt
Selected Folder: C:\~\Documents\github\fcore\include
```
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ target_link_libraries(text_io_example fcore)
# Folder Operations Example
add_executable(folder_ops_example folder_ops_example.f90)
target_link_libraries(folder_ops_example fcore)

# UI Dialog Example
add_executable(ui_dialog_example ui_dialog_example.f90)
target_link_libraries(ui_dialog_example fcore)
60 changes: 60 additions & 0 deletions examples/ui_dialog_example.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
! ui_dialog_example.f90

program main
use ui_dialogs
implicit none

! Local Variables
logical :: check
type(dialog_result) :: mbox
type(text_output_dialog_result) :: openfile, savefile, folder
type(file_filter) :: filters(2)

! Initialize the UI environment
check = initialize_ui_environment()
if (.not.check) then
print '(A)', "Could not initialize the UI environement. Good bye."
return
end if

! Show the user a message box
mbox = show_message_box("Testing...", "Example", MSGBX_BTN_OK_CANCEL, &
MSGBX_ICON_INFORMATION)
if (mbox%result == DIALOG_RESULT_OK) then
print '(A)', "The user pressed OK."
else if (mbox%result == DIALOG_RESULT_CANCEL) then
print '(A)', "The user pressed Cancel."
else
print '(AI0A)', "The user pressed an unknown button with result: ", &
mbox%result, "."
end if

! Set up file filters
filters(1)%name = "Text Files (*.txt)"
filters(1)%pattern = "*.txt"

filters(2)%name = "All Files (*.*)"
filters(2)%pattern = "*.*"

! Open an file
openfile = show_open_file_dialog(filters)
if (openfile%result == DIALOG_RESULT_OK) then
print '(A)', "The user selected file: " // openfile%string_list(1)%str
end if

! Save a file
savefile = show_save_file_dialog("txt", filters)
if (savefile%result == DIALOG_RESULT_OK) then
print '(A)', "The user wishes to save file: " // &
savefile%string_list(1)%str
end if

! Locate a directory
folder = show_browse_folder_dialog()
if (folder%result == DIALOG_RESULT_OK) then
print '(A)', "Selected Folder: " // folder%string_list(1)%str
end if

! Be sure to clean up
call clean_up_ui_environment()
end program
Binary file added images/folder_dialog.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/message_box.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/open_file_dialog.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/save_file_dialog.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 050be51

Please sign in to comment.