Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add simple GIO demo

  • Loading branch information...
commit 44371c6201ac393ae5a9ef1d369bf0fd539c3c1c 1 parent f0742a1
@jtappin jtappin authored
View
1  CMakeLists.txt
@@ -85,6 +85,7 @@ set(GTK_LIBRARIES
${GTK2_GDK_LIBRARY}
${GTK2_GDK_PIXBUF_LIBRARY}
${GTK2_GLIB_LIBRARY}
+ ${GTK2_GIO_LIBRARY}
${GTK2_GOBJECT_LIBRARY}
${GTK2_GTK_LIBRARY}
${GTK2_PANGO_LIBRARY} CACHE STRING "" FORCE)
View
5 examples/CMakeLists.txt
@@ -192,6 +192,10 @@ if (NOT NO_BUILD_EXAMPLES)
target_link_libraries(notebooks gtk-fortran_static ${GTK_LIBRARIES})
add_test(notebooks ./notebooks)
+ add_executable(gio_demo "gio_demo.f90")
+ target_link_libraries(gio_demo gtk-fortran_static ${GTK_LIBRARIES})
+ add_test(gio_demo ./gio_demo)
+
add_executable(tests "tests.f90")
target_link_libraries(tests gtk-fortran_static ${GTK_LIBRARIES})
add_test(tests ./tests)
@@ -236,6 +240,7 @@ if (INSTALL_EXAMPLES)
"${CMAKE_CURRENT_SOURCE_DIR}/mandelbrot.f90"
"${CMAKE_CURRENT_SOURCE_DIR}/menu.f90"
"${CMAKE_CURRENT_SOURCE_DIR}/notebooks.f90"
+ "${CMAKE_CURRENT_SOURCE_DIR}/gio_demo.f90"
"${CMAKE_CURRENT_SOURCE_DIR}/tests.f90"
"${CMAKE_CURRENT_SOURCE_DIR}/README"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/gtk-fortran/examples2)
View
93 examples/gio_demo.f90
@@ -0,0 +1,93 @@
+! Copyright (C) 2013
+! Free Software Foundation, Inc.
+
+! This file is part of the gtk-fortran gtk+ Fortran Interface library.
+
+! This is free software; you can redistribute it and/or modify
+! it under the terms of the GNU General Public License as published by
+! the Free Software Foundation; either version 3, or (at your option)
+! any later version.
+
+! This software is distributed in the hope that it will be useful,
+! but WITHOUT ANY WARRANTY; without even the implied warranty of
+! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+! GNU General Public License for more details.
+
+! Under Section 7 of GPL version 3, you are granted additional
+! permissions described in the GCC Runtime Library Exception, version
+! 3.1, as published by the Free Software Foundation.
+
+! You should have received a copy of the GNU General Public License along with
+! this program; see the files COPYING3 and COPYING.RUNTIME respectively.
+! If not, see <http://www.gnu.org/licenses/>.
+
+program g_io_demo
+
+ ! This is a very basic demo to get started with GIO. It uses Fortran I/O to
+ ! read text from the keyboard & writes it to the file gio_demo.dat in the
+ ! current directory.
+
+ use iso_c_binding
+ use gtk_sup
+
+ !********************************
+ ! Gtk modules for gio_demo.f90
+ use g, only: g_file_new_for_path, g_file_replace, g_object_unref, &
+ & g_output_stream_close, g_output_stream_write
+
+ use gtk, only: FALSE, G_FILE_CREATE_NONE
+
+
+ character(len=80) :: str
+ integer(kind=c_int8_t), dimension(80), target :: istr
+
+ type(c_ptr) :: file, stream
+ type(gerror), target :: errmsg
+ character(len=120) :: errtxt
+ integer :: ios, i
+ integer(kind=c_long) :: ncput, nchars
+ integer(kind=c_int) :: iok
+
+ file = g_file_new_for_path('gio_demo.dat'//c_null_char)
+
+ ! Use g_file_replace here so that it won't crash if the file exists.
+ stream = g_file_replace(file, c_null_char, FALSE, G_FILE_CREATE_NONE, &
+ & c_null_ptr, c_loc(errmsg))
+
+ if (.not. c_associated(stream)) then
+ call c_f_string(errmsg%message, errtxt)
+ print *, errtxt
+ stop
+ end if
+
+ do
+ write(*, "(a)", advance='no') "Text> "
+ read(*, "(a)", iostat=ios) str
+ if (ios /= 0) exit
+
+ nchars = len_trim(str)
+ do i = 1, nchars
+ istr(i) = ichar(str(i:i))
+ end do
+ istr(nchars+1) = ichar(c_new_line)
+
+ ncput = g_output_stream_write(stream, c_loc(istr), &
+ & nchars+1, c_null_ptr, c_loc(errmsg))
+ if (ncput < 0) then
+ call c_f_string(errmsg%message, errtxt)
+ print *, errtxt
+ stop
+ end if
+ end do
+
+ iok = g_output_stream_close(stream, c_null_ptr, c_loc(errmsg))
+ call g_object_unref(stream)
+ if (.not. c_f_logical(iok)) then
+ call c_f_string(errmsg%message, errtxt)
+ print *, errtxt
+ stop
+ end if
+
+ call g_object_unref(file)
+ print *
+end program g_io_demo
Please sign in to comment.
Something went wrong with that request. Please try again.