aboutsummaryrefslogtreecommitdiff
path: root/ag_render.f90
blob: b91a41601128382f1c8a6356da5aed93df3b4ee7 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
! Copyright (c) 2020 Jeffrey Armstrong <jeff@rainbow-100.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

module ag_render
use render
use appgraphics
implicit none

    integer, parameter::default_width = 640
    integer, parameter::default_height = 700

    integer, parameter::default_font_size = 18

    integer, parameter::max_links_displayed = 64

    integer, volatile::ag_render_event
    integer, volatile::scroll_position
    integer, volatile::wheel_movement

    integer, parameter::ag_render_event_none       = 0
    integer, parameter::ag_render_event_closed     = 1
    integer, parameter::ag_render_event_mouseclick = 2
    integer, parameter::ag_render_event_back       = 3
    integer, parameter::ag_render_event_go         = 4
    integer, parameter::ag_render_event_scroll     = 5
    integer, parameter::ag_render_event_resize     = 6
    integer, parameter::ag_render_event_mousemove  = 7
    integer, parameter::ag_render_event_wheel      = 8
    integer, parameter::ag_render_event_favorite   = 9
    integer, parameter::ag_render_event_internal   = 10

    type :: link
        integer, dimension(4)::location
        character(1024)::url
    end type

    type, extends(renderer) :: appgraphics_renderer

        integer::window_id
        integer::address_id
        integer::go_button_id
        integer::back_button_id
        integer::fave_button_id
        integer::internal_button_id
        integer::scroll_id

        integer::font_size
        integer::default_font
        integer::line_spacing

        ! Current window width
        integer::width

        integer::left_border, right_border
        integer::bullet_margin

        integer::text_color
        integer::link_color
        integer::background_color

        integer::address_bar_height
        integer::scroll_bar_width
        integer::status_bar_height

        integer::link_count
        type(link), dimension(max_links_displayed)::links

        ! Needed to compute document height
        integer::initial_y
        
        ! Page height
        integer::page_height

        ! An error has occurred, so render blank
        logical::render_blank

        ! A title was guessed and set
        logical::title_guessed
        
        ! Text of the title
        character(len=256)::title_text

        contains

        procedure :: initialize => ag_initialize
        procedure :: new_page => ag_new_page
        procedure :: prepare_for_layout => ag_prepare_for_layout
        procedure :: layout_complete => ag_layout_complete

        procedure :: text_width => ag_text_width
        procedure :: text_height => ag_text_height
        procedure :: is_text_visible => ag_text_visible

        procedure :: draw_proportional => ag_text_draw

        procedure :: preformatted_width => ag_preformatted_width
        procedure :: preformatted_height => ag_preformatted_height
        procedure :: is_preformatted_visible => ag_text_visible

        procedure :: draw_preformatted => ag_preformatted_draw

        procedure :: link_width => ag_link_width
        procedure :: link_height => ag_link_height
        procedure :: is_link_visible => ag_text_visible

        procedure :: draw_link => ag_link_draw

        procedure :: request_input => ag_request_input

        procedure :: draw_error => ag_draw_error

        procedure :: report_status => ag_draw_status

        procedure :: request_action => ag_action

        procedure :: request_save_filename => ag_request_save_filename

        procedure :: report_displayed_page => ag_report_page

        procedure :: report_unsupported_protocol => ag_report_unsupported_protocol

        procedure :: set_favicon => ag_set_favicon
        
        procedure :: rendering_height_available => ag_rendering_height_available
        procedure :: scroll_max_value => ag_scroll_max_value

    end type appgraphics_renderer

contains

    subroutine window_closing_callback()
    use appgraphics, only: stopidle
    implicit none

        ag_render_event = ag_render_event_closed
        call stopidle()

    end subroutine window_closing_callback

    subroutine back_button_callback()
    use appgraphics, only: stopidle
    implicit none

        ag_render_event = ag_render_event_back
        call stopidle()

    end subroutine back_button_callback

    subroutine go_button_callback()
    use appgraphics, only: stopidle
    implicit none

        ag_render_event = ag_render_event_go
        call stopidle()

    end subroutine go_button_callback

    subroutine fave_button_callback()
    use appgraphics, only: stopidle
    implicit none

        ag_render_event = ag_render_event_favorite
        call stopidle()

    end subroutine fave_button_callback

    subroutine internal_button_callback()
    use appgraphics, only: stopidle
    implicit none

        ag_render_event = ag_render_event_internal
        call stopidle()

    end subroutine internal_button_callback


    subroutine mouse_button_callback(x, y)
    use appgraphics, only: stopidle
    implicit none

        integer::x, y

        ag_render_event = ag_render_event_mouseclick
        call stopidle()

    end subroutine mouse_button_callback

    subroutine mouse_move_callback(x, y)
    use appgraphics, only: stopidle
    implicit none

        integer::x, y

        ag_render_event = ag_render_event_mousemove
        call stopidle()

    end subroutine mouse_move_callback

    subroutine mouse_wheel_callback(x, y)
    use appgraphics, only: stopidle
    implicit none

        integer::x, y

        wheel_movement = wheel_movement + x
        ag_render_event = ag_render_event_wheel
        call stopidle()

    end subroutine mouse_wheel_callback

    subroutine scrolled_callback(x)
    use appgraphics, only: stopidle
    implicit none

        integer::x
        
        scroll_position = x
        ag_render_event = ag_render_event_scroll
        call stopidle()

    end subroutine scrolled_callback

    subroutine resize_callback()
    use appgraphics, only: stopidle
    implicit none

        ag_render_event = ag_render_event_resize
        call stopidle()

    end subroutine resize_callback

    subroutine ag_set_favicon(self, f)
    implicit none
    
        class(appgraphics_renderer)::self
        character(*), intent(in)::f
        
        self%favicon = f
        
        call set_window_title(self, self%title_text)
        
    end subroutine ag_set_favicon

    subroutine set_window_title(self, text)
    use appgraphics, only: setwindowtitle
    implicit none

        type(appgraphics_renderer)::self
        character(*), intent(in)::text

        if(len_trim(text) > 256) then 
            self%title_text = text(1:250)//"..."
        else
            self%title_text = text
        end if

        if(len_trim(self%title_text) > 0) then
            if(len_trim(self%favicon) > 0) then
                call setwindowtitle("LR-87 :: "//self%favicon//" "//trim(self%title_text))
            else
                call setwindowtitle("LR-87 :: "//trim(self%title_text))
            end if
        else
            call setwindowtitle("LR-87")
        end if

    end subroutine set_window_title

    subroutine draw_address_bar(self, expanding)
    use appgraphics
    implicit none

        type(appgraphics_renderer)::self
        logical, optional::expanding

        logical::myexpand
        integer::x, address_width, label_x, ignored
        
        ! Double-buffer
        integer::i, active_page

        ! See below where this is used...
        interface
            function switch_to_thread() bind(c, name="SwitchToThread")
            use iso_c_binding
            integer(kind=c_int)::switch_to_thread
            end function switch_to_thread
        end interface

        if(present(expanding)) then
            myexpand = expanding
        else
            myexpand = .false.
        endif

        self%address_bar_height = scaledpi(24)

        ! Set up some address bar colors
        active_page = getactivepage()
        do i = 0,1
            call setactivepage(i)

            call setfillstyle(SOLID_FILL, LIGHTGRAY)
            call setbkcolor(LIGHTGRAY)
            call setcolor(BLACK)
            call settextstyle(SANS_SERIF_FONT, HORIZ_DIR, scaledpi(18))
        end do
        call setactivepage(active_page)

        ! Draw the buttons first
        x = 5
        if(self%back_button_id < 0) then
            self%back_button_id = createbutton(x, 2, scaledpi(40), scaledpi(20), &
                                               ACHAR(240)//ACHAR(159)//ACHAR(161)//ACHAR(184), &
                                               back_button_callback)
        else
            call setbuttonposition(self%back_button_id, x, 2, scaledpi(40), scaledpi(20))
        end if

        x = x + scaledpi(50)
        label_x = x

        call settextstyle(WINDOWS_FONT, HORIZ_DIR, scaledpi(14))
        x = x + textwidth("Address:") + 5
        address_width = getmaxx()/2 - textwidth("Address:") - 5

        if(self%address_id < 0) then
            self%address_id = createtextbox(x, 2, address_width, scaledpi(20))
            call settextboxentercallback(self%address_id, go_button_callback)
        else
            call settextboxposition(self%address_id, x, 2, address_width, scaledpi(20))
        end if

        ! Clears any drawing operations for controls quickly
        ignored = switch_to_thread()

        call settextstyle(SANS_SERIF_FONT, HORIZ_DIR, scaledpi(18))

        x = x + 10 + address_width
        if(self%go_button_id < 0) then
            self%go_button_id = createbutton(x, 2, scaledpi(40), scaledpi(20), &
                                             ACHAR(240)//ACHAR(159)//ACHAR(161)//ACHAR(186), &
                                             go_button_callback)
        else
            call setbuttonposition(self%go_button_id, x, 2, scaledpi(40), scaledpi(20))
        end if

        call settextstyle(SANS_SERIF_FONT, HORIZ_DIR, scaledpi(20))

        ! If we're not expanding, just draw these buttons in order
        if(.not. myexpand) then

            x = getmaxx() - scaledpi(95)
            if(self%fave_button_id < 0) then
                self%fave_button_id = createbutton(x, 2, scaledpi(40), scaledpi(20), &
                                                   ACHAR(226)//ACHAR(152)//ACHAR(133), &
                                                   fave_button_callback)
            else
                call setbuttonposition(self%fave_button_id, x, 2, scaledpi(40), scaledpi(20))
            end if

            x = x + scaledpi(50)
            if(self%internal_button_id < 0) then
                self%internal_button_id = createbutton(x, 2, scaledpi(40), scaledpi(20), &
                                                       ACHAR(226)//ACHAR(156)//ACHAR(135), &
                                                       internal_button_callback)
            else
                call setbuttonposition(self%internal_button_id, x, 2, scaledpi(40), scaledpi(20))
            end if

        ! If expanding due to resize, we need to draw the rightmost first.
        ! Also, the buttons would exist at this point for sure, so no need to
        ! check for that anymore
        else

            x = getmaxx() - scaledpi(45)
            call setbuttonposition(self%internal_button_id, x, 2, scaledpi(40), scaledpi(20))

            x = x - scaledpi(50)
            call setbuttonposition(self%fave_button_id, x, 2, scaledpi(40), scaledpi(20))

        end if

        ! Clears any drawing operations for controls quickly
        ignored = switch_to_thread()

        active_page = getactivepage()
        do i = 0,1
            call setactivepage(i)

            call setviewport(0, 0, getmaxx()+1, self%address_bar_height+1, .true.)
            call clearviewport()

            call settextstyle(WINDOWS_FONT, HORIZ_DIR, scaledpi(14))
            call outtextxy(label_x, 5, "Address:")

            call resetviewport()
        end do
        call setactivepage(active_page)

    end subroutine draw_address_bar

    subroutine draw_status_bar(self, text)
    use appgraphics
    implicit none

        type(appgraphics_renderer)::self
        character(*), intent(in)::text
        
        ! Double-buffer
        integer::i, active_page
        
        call resetviewport()
        call setviewport(0, getmaxy()-self%status_bar_height, getmaxx()+1, getmaxy()+1, .true.)
        active_page = getactivepage()
        self%status_bar_height = scaledpi(16)
        
        do i = 0, 1
            call setactivepage(i)
            
            call setbkcolor(LIGHTGRAY)
            call setcolor(BLACK)
            call settextstyle(WINDOWS_FONT, HORIZ_DIR, scaledpi(14))
            
            
            call clearviewport()
            call outtextxy(5, 2, trim(text))
        end do
        call setactivepage(active_page)
        call resetviewport()

    end subroutine draw_status_bar

    subroutine draw_scroll_bar(self)
    use appgraphics
    implicit none

        type(appgraphics_renderer)::self

        integer::h

        self%scroll_bar_width = 15
        h = getmaxy()-self%address_bar_height-self%status_bar_height - 1
        if(self%scroll_id < 0) then
            self%scroll_id = createscrollbar(getmaxx() - self%scroll_bar_width + 1, &
                                             self%address_bar_height+1, &
                                             self%scroll_bar_width, &
                                             h, &
                                             SCROLL_VERTICAL, &
                                             scrolled_callback)

            call setscrollrange(self%scroll_id, 0, 100)
        else
            call setscrollbarposition(self%scroll_id, &
                                      getmaxx() - self%scroll_bar_width + 1, &
                                      self%address_bar_height+1, &
                                      self%scroll_bar_width, &
                                      h)
        end if

    end subroutine draw_scroll_bar

    function compute_max_width(self)
    implicit none

        type(appgraphics_renderer)::self
        integer::compute_max_width

        compute_max_width = getmaxx() - self%left_border - &
                            self%right_border - self%scroll_bar_width

    end function compute_max_width

    subroutine ag_initialize(self)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self

        call setapplicationdpiaware()

        self%window_id = initwindow(scaledpi(default_width), scaledpi(default_height), "LR-87", &
                                    DEFAULT_POSITION, DEFAULT_POSITION, &
                                    .TRUE., .FALSE.)

        call setwindowsmallicon(1)
        call setwindowlargeicon(1)

        call setwindowclosecallback(window_closing_callback)
        call registermousehandler(MOUSE_LB_UP, mouse_button_callback)
        call registermousehandler(MOUSE_MOVE, mouse_move_callback)
        call registermousehandler(MOUSE_WHEEL, mouse_wheel_callback)
        call enableresize(resize_callback)

        call setbkcolor(LIGHTGRAY)
        call clearviewport()

        self%address_id = -1
        self%go_button_id = -1
        self%back_button_id = -1
        self%fave_button_id = -1
        self%internal_button_id = -1
        self%scroll_id = -1

        call draw_address_bar(self)
        !call draw_status_bar(self, "Welcome to the LR-87 Gemini Client")
        call self%report_status("Welcome to the LR-87 Gemini Client")
        call draw_scroll_bar(self)

        self%background_color = WHITE
        self%text_color = BLACK
        self%link_color = BLUE

        self%max_width = compute_max_width(self)
        self%font_size = scaledpi(default_font_size)
        self%default_font = SERIF_FONT
        self%line_spacing = 2
        self%left_border = 15
        self%right_border = 15
        self%bullet_margin = 5

        self%render_blank = .false.

        self%title_guessed = .false.

        self%width = default_width

        call setdialoghotkeys(.true.)

    end subroutine ag_initialize

    function get_link_at_mouse(self)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        integer::get_link_at_mouse

        integer::i, mx, my

        get_link_at_mouse = -1
        mx = mousex()
        my = mousey()
        my = my - self%address_bar_height
        do i = 1, self%link_count
            if(my >= self%links(i)%location(2) .and. my <= self%links(i)%location(4) .and. &
               mx >= self%links(i)%location(1) .and. mx <= self%links(i)%location(3)) then

                get_link_at_mouse = i
                exit
            end if
        end do

    end function get_link_at_mouse

    subroutine ag_prepare_for_layout(self)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self

        call setbkcolor(self%background_color)
        call resetviewport()

        call setviewport(0, &
                         self%address_bar_height+1, &
                         getmaxx() - self%scroll_bar_width + 1, &
                         getmaxy() - self%status_bar_height, &
                         .true.)

        call clearviewport()

        self%link_count = 0
        self%initial_y = self%y

    end subroutine ag_prepare_for_layout
    
    function ag_rendering_height_available(self)
    use appgraphics, only: getmaxy
    implicit none
    
        class(appgraphics_renderer), intent(in)::self
        integer::ag_rendering_height_available
        
        ag_rendering_height_available = getmaxy() - self%status_bar_height - &
                                        self%address_bar_height - 1
        
    end function ag_rendering_height_available
    
    function ag_scroll_max_value(self)
    implicit none
        
        class(appgraphics_renderer), intent(in)::self
        integer::ag_scroll_max_value
        
        ag_scroll_max_value = max(0, self%page_height - self%rendering_height_available())
    
    end function ag_scroll_max_value
    
    subroutine ag_layout_complete(self)
    use appgraphics, only: swapbuffers
    implicit none
    
        class(appgraphics_renderer)::self
        integer::nPages
        
        call swapbuffers()
        
        if(self%page_height == 0) then
            self%page_height = self%y + self%initial_y
            nPages = self%rendering_height_available()
            call setscrollrange(self%scroll_id, 0, self%scroll_max_value()+nPages)
            call setscrollpagesize(self%scroll_id, nPages)
        end if
        
    end subroutine ag_layout_complete

    subroutine ag_new_page(self)
    implicit none

        class(appgraphics_renderer)::self

        self%y = 0
        scroll_position = 0
        wheel_movement = 0

        call setscrollposition(self%scroll_id, 0)
        ag_render_event = ag_render_event_none

        self%title_guessed = .false.
        self%page_height = 0

    end subroutine ag_new_page

    pure function get_font_size(self, text_type)
    implicit none

        class(appgraphics_renderer), intent(in)::self
        integer, intent(in)::text_type
        integer::get_font_size

        get_font_size = self%font_size
        if(text_type == proportional_type_heading_1) then
            get_font_size = 2*get_font_size
        else if(text_type > proportional_type_heading_1) then
            get_font_size = (3*get_font_size)/2
        end if

    end function get_font_size

    pure function get_indent_margin(self)
    implicit none

        type(appgraphics_renderer), intent(in)::self
        integer::get_indent_margin

        get_indent_margin = self%left_border + self%font_size + 2*self%bullet_margin

    end function get_indent_margin

    function ag_text_width(self, text, text_type)
    use appgraphics
    use render
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        integer, intent(in), optional::text_type
        integer::ag_text_width
        integer::font_size

        font_size = self%font_size
        if(present(text_type)) then
            font_size = get_font_size(self, text_type)
        end if

        call settextstyle(self%default_font, HORIZ_DIR, font_size)
        if(len_trim(text) > 0) then
            ag_text_width = textwidth(trim(text))
        else
            ag_text_width = 0
        end if

        !ag_text_width = ag_text_width + self%left_border + self%right_border

        if(present(text_type)) then
            if(text_type == proportional_type_list_item) then
                ag_text_width = ag_text_width + get_indent_margin(self)
            else if(text_type == proportional_type_quotation) then
                ag_text_width = ag_text_width + 2 * get_indent_margin(self)
            end if
        end if

    end function ag_text_width

    function ag_text_height(self, text, text_type)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        integer, intent(in), optional::text_type
        integer::ag_text_height
        integer::font_size

        font_size = self%font_size
        if(present(text_type)) then
            font_size = get_font_size(self, text_type)
        end if

        call settextstyle(self%default_font, HORIZ_DIR, font_size)
        if(len_trim(text) > 0) then
            ag_text_height = textheight(trim(text)) + self%line_spacing
        else
            ag_text_height = self%line_spacing + self%font_size
        end if

    end function ag_text_height

    function ag_text_visible(self, text)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        logical::ag_text_visible

        ag_text_visible = (self%y > 0 .and. self%y < getmaxy())

    end function ag_text_visible

    subroutine ag_text_draw(self, text, text_type)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        integer, intent(in), optional::text_type
        integer::font_size
        integer::x, bx, by
        integer::font_color

        font_size = self%font_size
        font_color = self%text_color
        x = self%left_border

        if(present(text_type)) then
            font_size = get_font_size(self, text_type)

            select case(text_type)

                case (proportional_type_heading_1)
                    ! The first level 1 heading can be guessed as the page title
                    if(.not. self%title_guessed) then
                        
                        call set_window_title(self, text)
                        self%title_guessed = .true.

                    end if

                case (proportional_type_list_item)
                    ! If a list item, need to draw a box
                    bx = x + self%bullet_margin
                    by = self%y + self%font_size/4

                    call setfillstyle(SOLID_FILL, self%text_color)
                    call bar(bx, by, bx+self%font_size/2, by+self%font_size/2)

                    x = get_indent_margin(self)

                case (proportional_type_quotation)
                    ! Scoot in and draw grey
                    x = x + get_indent_margin(self)
                    font_color = DARKGRAY

            end select
        end if

        call settextstyle(self%default_font, HORIZ_DIR, font_size)
        call setcolor(font_color)
        call outtextxy(x, self%y, trim(text))

    end subroutine ag_text_draw

    function ag_link_width(self, text)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        integer::ag_link_width

        ag_link_width = ag_text_width(self, text)

    end function ag_link_width

    function ag_link_height(self, text)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        integer::ag_link_height

        ag_link_height = ag_text_height(self, text)

    end function ag_link_height

    subroutine ag_link_draw(self, text, url)
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text, url
        integer::text_width

        call settextstyle(self%default_font, HORIZ_DIR, self%font_size)
        call setcolor(self%link_color)
        call outtextxy(self%left_border, self%y, trim(text))

        text_width = ag_link_width(self, trim(text))

        ! Need to remove indents, borders, etc.
        !text_width = text_width - self%left_border - self%right_border

        call line(self%left_border, self%y + 7*self%font_size/8, &
                  self%left_border + text_width, self%y + 7*self%font_size/8)

        ! Store this link
        self%link_count = self%link_count + 1
        if(self%link_count < max_links_displayed) then
            self%links(self%link_count)%location(1) = self%left_border
            self%links(self%link_count)%location(2) = self%y

            self%links(self%link_count)%location(3) = self%left_border + text_width
            self%links(self%link_count)%location(4) = self%y + self%font_size

            self%links(self%link_count)%url = url
        end if

    end subroutine ag_link_draw

    function ag_preformatted_width(self, text)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        integer::ag_preformatted_width

        call settextstyle(MONOSPACE_FONT, HORIZ_DIR, self%font_size)
        if(len_trim(text) > 0) then
            ag_preformatted_width = textwidth(trim(text))
        else
            ag_preformatted_width = 0
        end if

        !ag_preformatted_width = ag_preformatted_width + self%left_border + self%right_border

    end function ag_preformatted_width

    function ag_preformatted_height(self, text)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        integer::ag_preformatted_height


        call settextstyle(MONOSPACE_FONT, HORIZ_DIR, self%font_size)
        if(len_trim(text) > 0) then
            ag_preformatted_height = textheight(trim(text)) + self%line_spacing
        else
            ag_preformatted_height = self%font_size + self%line_spacing
        end if

    end function ag_preformatted_height

    subroutine ag_preformatted_draw(self, text)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text

        call settextstyle(MONOSPACE_FONT, HORIZ_DIR, self%font_size)
        call setcolor(BLACK)
        call outtextxy(self%left_border, self%y, trim(text))

    end subroutine ag_preformatted_draw

    function ag_request_input(self, question, answer)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::question
        character(*), intent(out)::answer
        logical::ag_request_input

        ag_request_input = dlgrequesttext(answer, "Input Requested", question)

    end function ag_request_input

    subroutine ag_draw_error(self, text)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text

        ! Force blank draw
        self%render_blank = .true.
        ag_render_event = ag_render_event_resize

        call dlgmessage(0, text)

    end subroutine ag_draw_error

    subroutine ag_draw_status(self, text)
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text
        character(64), save::last_text
        
        if(trim(last_text) == trim(text)) then
            return
        end if
        
        call draw_status_bar(self, text)

        last_text = text
        
    end subroutine ag_draw_status

    function ag_action(self, text)
    use render, only: render_action_none, render_action_layout, &
                      render_action_goto, render_action_quit, &
                      idle_status
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(out)::text
        integer::ag_action
        integer::link_clicked, link_under
        integer::ignored

        logical::expanding_horizontally

        ! Resize can be laggy, so ensure it actually was finalized
        if(self%max_width /= compute_max_width(self)) then
            ag_render_event = ag_render_event_resize
        end if

        if(ag_render_event == ag_render_event_none) then
            call startidle(10000)
        end if

        ag_action = render_action_none
        select case (ag_render_event)

            case(ag_render_event_closed)
                ag_action = render_action_quit

            case(ag_render_event_back)
                ag_action = render_action_back

            case(ag_render_event_go)
                ignored = gettextboxcontents(self%address_id, text)
                ag_action = render_action_goto

            case(ag_render_event_mouseclick)
                link_clicked = get_link_at_mouse(self)
                if(link_clicked > 0) then
                    text = self%links(link_clicked)%url
                    ag_action = render_action_goto
                end if

                call clearmouseclick(MOUSE_LB_UP)

            case(ag_render_event_mousemove)
                link_under = get_link_at_mouse(self)
                if(link_under > 0) then
                    call self%report_status(self%links(link_under)%url)
                else
                    call self%status_ready()
                end if

                call clearmouseclick(MOUSE_MOVE)

            case(ag_render_event_wheel)
                scroll_position = scroll_position - &
                    (wheel_movement/120)* &
                    self%text_height("Scrolled Example", proportional_type_normal)
            
                ! Limit
                scroll_position = max(0, scroll_position)
                scroll_position = min(self%scroll_max_value(), &
                                      scroll_position)
            
                call setscrollposition(self%scroll_id, scroll_position)

                self%y = -1*scroll_position
                ag_action = render_action_layout
                
                wheel_movement = 0

            case(ag_render_event_scroll)
                
                self%y = -1*scroll_position
                ag_action = render_action_layout

            case(ag_render_event_resize)

                expanding_horizontally = (getmaxx() > self%width)
                self%width = getmaxx()

                ! Effectively have to redraw everything
                call draw_address_bar(self, expanding=expanding_horizontally)
                call self%report_status(idle_status)
                call draw_scroll_bar(self)
                call setscrollposition(self%scroll_id, 0)

                ! Need to recompute the max width for the layout engine
                self%max_width = compute_max_width(self)
                self%y = 0

                ag_action = render_action_rewrap

            case(ag_render_event_favorite)
                ag_action = render_action_favorite

            case(ag_render_event_internal)
                text = "lr87://menu"
                ag_action = render_action_goto

        end select

        ! if a layout was requested, but we're blank...
        if(ag_action == render_action_layout .and. self%render_blank) then
            call ag_prepare_for_layout(self) ! This clears the screen fine
        end if

        ! We've handled the event here, reset to none
        ag_render_event = ag_render_event_none

    end function ag_action

    function ag_request_save_filename(self, url, mimetype, filename)
    use appgraphics
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::url
        character(*), intent(in)::mimetype
        character(*), intent(out)::filename
        logical::ag_request_save_filename

        ag_request_save_filename = dlgsavefile(filename)

    end function ag_request_save_filename

    subroutine ag_report_page(self, text)
    use appgraphics
    use internal_links, only: internal_url
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::text

        call set_window_title(self, text)

        if(index(text, "gemini://") > 0 .or. index(text, internal_url) > 0) then
            call settextboxcontents(self%address_id, trim(text))
        end if

    end subroutine ag_report_page

    function ag_report_unsupported_protocol(self, url)
    use iso_c_binding, only: c_null_char, c_null_ptr
    use appgraphics, only: dlgyesno
    implicit none

        class(appgraphics_renderer)::self
        character(*), intent(in)::url
        logical::ag_report_unsupported_protocol

        type(c_ptr)::ret

        interface
            function ShellExecute(h, verb, filename, params, dir, showcmd) bind(c, name="ShellExecuteA")
            use iso_c_binding
            type(c_ptr)::h
            character(kind=c_char)::verb
            character(kind=c_char)::filename
            type(c_ptr)::params
            type(c_ptr)::dir
            integer(kind=c_int)::showcmd
            type(c_ptr)::ShellExecute
            end function ShellExecute
        end interface

        if(dlgyesno(DIALOG_WARN, "Ask Windows to open "//trim(url)//" in another program?")) then

            ret = ShellExecute(c_null_ptr, &
                               "open"//c_null_char, &
                               trim(url)//c_null_char, &
                               c_null_ptr, &
                               c_null_ptr, &
                               5) ! 5 is SW_SHOW

        end if

        ag_report_unsupported_protocol = .false.

    end function ag_report_unsupported_protocol

end module ag_render