/
hl_combo.f90
113 lines (82 loc) · 3.24 KB
/
hl_combo.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
! Copyright (C) 2011
! 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/>.
!
! gfortran -g gtk.f90 gtk-sup.f90 gtk-hl.f90 hl_combo.f90 `pkg-config --cflags --libs gtk+-2.0`
! Contributed by James Tappin.
module handlers
use gtk_hl
use gtk, only: gtk_button_new, gtk_combo_box_get_active, gtk_combo_box_new, gtk&
&_container_add, gtk_main, gtk_main_quit, gtk_widget_destroy, gtk_widget_show, &
>k_widget_show_all, gtk_window_new,gtk_init,TRUE,FALSE
implicit none
type(c_ptr) :: win, box, c1, c2, qbut
contains
subroutine my_destroy(widget, gdata) bind(c)
type(c_ptr), value :: widget, gdata
print *, "Exit called"
call gtk_widget_destroy(win)
call gtk_main_quit ()
end subroutine my_destroy
subroutine c_change(widget, gdata) bind(c)
type(c_ptr), value :: widget, gdata
integer, pointer :: index
integer(kind=c_int) :: isel, nrow
character(len=40) :: value
if (c_associated(gdata)) then
call c_f_pointer(gdata, index)
print "('Box:',I2)", index
end if
isel = hl_gtk_combo_box_get_active(widget, ftext=value)
nrow = hl_gtk_combo_box_n_entries(widget)
print "('Choice:',I2,' of ',i2,' Text:',a)", isel, nrow, trim(value)
end subroutine c_change
end module handlers
program combo_demo
use handlers
implicit none
character(len=20), dimension(8) :: list1
character(len=20) :: item2
integer :: i
integer, target :: d1=1, d2=2
call gtk_init()
! Top level window, and column box
win = hl_gtk_window_new("Combo box demo"//c_null_char, &
& destroy=c_funloc(my_destroy))
box = hl_gtk_box_new()
call gtk_container_add(win, box)
! A list with an entry box
do i = 1, 8
write(list1(i), "('Item # ',I0)") i-1
end do
c1 = hl_gtk_combo_box_new(has_entry=TRUE, changed=c_funloc(c_change), &
& initial_choices=list1, data=c_loc(d1))
call hl_gtk_box_pack(box, c1)
! One without
c2 = hl_gtk_combo_box_new(changed=c_funloc(c_change), data=c_loc(d2))
call hl_gtk_box_pack(box, c2)
do i = 1, 5
write(item2, "('Choice Number',I2)") i
call hl_gtk_combo_box_add_text(c2, trim(item2)//c_null_char, at_start=TRUE)
end do
! Quit button
qbut = hl_gtk_button_new("Quit"//c_null_char, clicked=c_funloc(my_destroy))
call hl_gtk_box_pack(box, qbut)
! Realize & enter event loop
call gtk_widget_show_all(win)
call gtk_main
end program combo_demo