summaryrefslogtreecommitdiff
path: root/desktop/browser.c
blob: 89ae80921c6ef7ad23f933bf7a123f412bae4827 (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
/**
 * $Id: browser.c,v 1.7 2002/12/23 20:19:53 bursa Exp $
 */

#include "netsurf/riscos/font.h"
#include "netsurf/render/box.h"
#include "netsurf/render/layout.h"
#include "netsurf/render/css.h"
#include "netsurf/desktop/browser.h"
#include "netsurf/render/utils.h"
#include "netsurf/desktop/cache.h"
#include "netsurf/utils/log.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <time.h>

struct box_selection
{
  struct box* box;
  int actual_x;
  int actual_y;
  int plot_index;
};

void browser_window_text_selection(struct browser_window* bw, int click_x, int click_y, int click_type);
void browser_window_clear_text_selection(struct browser_window* bw);
void browser_window_change_text_selection(struct browser_window* bw, struct box_position* new_start, struct box_position* new_end);
void browser_window_redraw_boxes(struct browser_window* bw, struct box_position* start, struct box_position* end);
void browser_window_follow_link(struct browser_window* bw,
  int click_x, int click_y, int click_type);

void box_under_area(struct box* box, int x, int y, int ox, int oy, struct box_selection** found, int* count, int* plot_index);

void browser_window_start_throbber(struct browser_window* bw)
{
  bw->throbbing = 1;
  gui_window_start_throbber(bw->window);
  return;
}

void browser_window_stop_throbber(struct browser_window* bw)
{
  bw->throbbing = 0;
  gui_window_stop_throbber(bw->window);
}

void content_destroy(struct content* c)
{
  if (c == NULL)
    return;

  switch (c->type)
  {
    case CONTENT_HTML:
      /* free other memory here */
//      xmlFreeParserCtxt(c->data.html.parser);
      LOG(("free parser"));
//      htmlFreeParserCtxt(c->data.html.parser);
      LOG(("free sheet"));
//      xfree(c->data.html.stylesheet);
      LOG(("free style"));
//      xfree(c->data.html.style);
      if (c->data.html.layout != NULL)
      {
        LOG(("box_free box"));
//        box_free(c->data.html.layout);
        LOG(("free box"));
//        xfree(c->data.html.layout);
      }
      LOG(("free font"));
      font_free_set(c->data.html.fonts);
      break;
    default:
      break;
  }

  c->main_fetch = fetch_cancel(c->main_fetch);
  xfree(c);

  return;
}

size_t content_html_receive_data(struct content* c, void* data, size_t size, size_t nmemb)
{
  size_t amount = nmemb;
  int offset = 0;
  size_t numInChunk = 2048 / size;  /* process in 2k chunks */

  if (numInChunk > nmemb)
    numInChunk = nmemb;
  else if (numInChunk <= (size_t)0)
    numInChunk = 1;

  while (amount > 0)
  {
    htmlParseChunk(c->data.html.parser, (char*)data + (offset * size), numInChunk, 0);
    offset += numInChunk;
    amount -= numInChunk;
    if (amount < numInChunk)
      numInChunk = amount;
    gui_multitask();
  }

  return size * nmemb;
}

void set_content_html(struct content* c)
{
  c->type = CONTENT_HTML;
  c->data.html.parser = htmlCreatePushParserCtxt(0, 0, "", 0, 0, XML_CHAR_ENCODING_8859_1);
  c->data.html.document = NULL;
  c->data.html.markup = NULL;
  c->data.html.layout = NULL;
  c->data.html.stylesheet = NULL;
  c->data.html.style = NULL;
  return;
}

void content_html_reformat(struct content* c, int width)
{
  char* file;
  struct css_selector* selector = xcalloc(1, sizeof(struct css_selector));

  LOG(("Starting stuff"));
  if (c->data.html.layout != NULL)
  {
    /* TODO: skip if width is unchanged */
    layout_document(c->data.html.layout->children, (unsigned long)width);
    return;
  }

  LOG(("Setting document to myDoc"));
  c->data.html.document = c->data.html.parser->myDoc;

  /* skip to start of html */
  LOG(("Skipping to html"));
  if (c->data.html.document == NULL)
  {
    LOG(("There is no document!"));
    return;
  }
  for (c->data.html.markup = c->data.html.document->children;
       c->data.html.markup != 0 &&
         c->data.html.markup->type != XML_ELEMENT_NODE;
       c->data.html.markup = c->data.html.markup->next)
    ;

  if (c->data.html.markup == 0)
  {
    LOG(("No markup"));
    return;
  }
  if (strcmp((const char *) c->data.html.markup->name, "html"))
  {
    LOG(("Not html"));
    return;
  }

//  xfree(c->data.html.stylesheet);
//  xfree(c->data.html.style);

  LOG(("Loading CSS"));
  file = load("<NetSurf$Dir>.Resources.CSS");  /*!!! not portable! !!!*/
  c->data.html.stylesheet = css_new_stylesheet();
  LOG(("Parsing stylesheet"));
  css_parse_stylesheet(c->data.html.stylesheet, file);

  LOG(("Copying base style"));
  c->data.html.style = xcalloc(1, sizeof(struct css_style));
  memcpy(c->data.html.style, &css_base_style, sizeof(struct css_style));

  LOG(("Creating box"));
  c->data.html.layout = xcalloc(1, sizeof(struct box));
  c->data.html.layout->type = BOX_BLOCK;
  c->data.html.layout->node = c->data.html.markup;

  c->data.html.fonts = font_new_set();

  LOG(("XML to box"));
  xml_to_box(c->data.html.markup, c->data.html.style, c->data.html.stylesheet, &selector, 0, c->data.html.layout, 0, 0, c->data.html.fonts);
  LOG(("Layout document"));
  layout_document(c->data.html.layout->children, (unsigned long)width);

  /* can tidy up memory here? */

  return;
}

void browser_window_reformat(struct browser_window* bw)
{
  char status[100];
  clock_t time0, time1;

  LOG(("Entering..."));
  if (bw == NULL)
    return;
  if (bw->current_content == NULL)
    return;

  switch (bw->current_content->type)
  {
    case CONTENT_HTML:
      LOG(("HTML content."));
      browser_window_set_status(bw, "Formatting page...");
      time0 = clock();
      content_html_reformat(bw->current_content, gui_window_get_width(bw->window));
      time1 = clock();
      LOG(("Content reformatted"));
      if (bw->current_content->data.html.layout != NULL)
      {
        LOG(("Setting extent"));
        gui_window_set_extent(bw->window, bw->current_content->data.html.layout->children->width, bw->current_content->data.html.layout->children->height);
        LOG(("Setting scroll"));
        gui_window_set_scroll(bw->window, 0, 0);
        LOG(("Redraw window"));
        gui_window_redraw_window(bw->window);
        LOG(("Complete"));
        sprintf(status, "Format complete (%gs).", ((float) time1 - time0) / CLOCKS_PER_SEC);
        browser_window_set_status(bw, status);
      }
      else
      {
        LOG(("This isn't html"));
        browser_window_set_status(bw, "This is not HTML!");
        cache_free(bw->current_content);
        bw->current_content = NULL;
      }
      break;
    default:
        LOG(("Unknown content type"));
      break;
  }
}

/* create a new history item */
struct history* history_create(char* desc, char* url)
{
  struct history* h = xcalloc(1, sizeof(struct history));
  LOG(("desc = %s, url = %s", desc, url));
  h->description = xstrdup(desc);
  h->url = xstrdup(url);
  LOG(("return h = %p", h));
  return h;
}

void browser_window_back(struct browser_window* bw)
{
  if (bw->history != NULL)
  {
    if (bw->history->earlier != NULL)
    {
      bw->history = bw->history->earlier;
      browser_window_open_location_historical(bw, bw->history->url);
    }
  }
}

void browser_window_forward(struct browser_window* bw)
{
  if (bw->history != NULL)
  {
    if (bw->history->later != NULL)
    {
      bw->history = bw->history->later;
      browser_window_open_location_historical(bw, bw->history->url);
    }
  }
}

/* remember a new page after the current one. anything remembered after the
   current page is forgotten. */
void history_remember(struct history* current, char* desc, char* url)
{
  struct history* h;
  LOG(("current = %p, desc = %s, url = %s", current, desc, url));
  assert(current != NULL);

  /* forget later history items */
  h = current->later;
  while (h != NULL)
  {
    struct history* hh;
    hh = h;
    h = h->later;

    if (hh->description != NULL)
      xfree(hh->description);
    if (hh->url != NULL)
      xfree(hh->url);

    xfree(hh);
  }

  current->later = history_create(desc, url);
  current->later->earlier = current;

  LOG(("end"));
}


struct browser_window* create_browser_window(int flags, int width, int height)
{
  struct browser_window* bw;
  bw = (struct browser_window*) xcalloc(1, sizeof(struct browser_window));

  bw->flags = flags;
  bw->throbbing = 0;
  bw->format_width = width;
  bw->format_height = height;

  bw->scale.mult = 1;
  bw->scale.div = 1;

  bw->current_content = NULL;
  bw->future_content = NULL;
  bw->history = NULL;

  bw->url = NULL;
  bw->title = xstrdup("NetSurf");

  bw->window = create_gui_browser_window(bw);

  return bw;
}

void browser_window_set_status(struct browser_window* bw, char* text)
{
  if (bw->window != NULL)
    gui_window_set_status(bw->window, text);
}

void browser_window_destroy(struct browser_window* bw)
{
  LOG(("bw = %p", bw));
  assert(bw != 0);

  if (bw->current_content != NULL)
    cache_free(bw->current_content);
  if (bw->future_content != NULL)
    cache_free(bw->future_content);

  if (bw->history != NULL)
  {
    struct history* current;

    while (current->earlier != NULL)
      current = current->earlier;

    while (current != NULL)
    {
      struct history* hh;
      hh = current;
      current = current->later;

      if (hh->description != NULL)
        xfree(hh->description);
      if (hh->url != NULL)
        xfree(hh->url);

      xfree(hh);
    }
  }

  xfree(bw->url);
  xfree(bw->title);

  gui_window_destroy(bw->window);

  xfree(bw);

  LOG(("end"));
}

void browser_window_open_location_historical(struct browser_window* bw, char* url)
{
  LOG(("bw = %p, url = %s", bw, url));

  assert(bw != 0 && url != 0);

  if (bw->future_content != NULL)
    cache_free(bw->future_content);

  bw->future_content = cache_get(url);
  if (bw->future_content == 0)
  {
    /* not in cache: start fetch */
    struct fetch_request* req;

    LOG(("not in cache: starting fetch"));

    req = xcalloc(1, sizeof(struct fetch_request));
    req->type = REQUEST_FROM_BROWSER;
    req->requestor.browser = bw;

    bw->future_content = (struct content*) xcalloc(1, sizeof(struct content));
    bw->future_content->main_fetch = create_fetch(url, bw->url, 0, req);

    cache_put(url, bw->future_content, 1000);

    browser_window_start_throbber(bw);
  }
  else
  {
    /* in cache: reformat page and display */
    LOG(("in cache: reformatting"));
    browser_window_reformat(bw);
  }

  LOG(("end"));
}

void browser_window_open_location(struct browser_window* bw, char* url)
{
  LOG(("bw = %p, url = %s", bw, url));
  assert(bw != 0 && url != 0);
  browser_window_open_location_historical(bw, url);
  if (bw->history == NULL)
    bw->history = history_create(NULL, bw->future_content->main_fetch->location);
  else
  {
    history_remember(bw->history, NULL, bw->future_content->main_fetch->location);
    bw->history = bw->history->later;
  }
  LOG(("end"));
}

int browser_window_message(struct browser_window* bw, struct browser_message* msg)
{
  gui_safety previous_safety;

  switch (msg->type)
  {
    case msg_FETCH_SENDING:
      browser_window_set_status(bw, "Sending request...");
      break;

    case msg_FETCH_WAITING:
      browser_window_set_status(bw, "Waiting for reply...");
      break;

    case msg_FETCH_FETCH_INFO:
      browser_window_set_status(bw, "Request received...");
      if (msg->f == bw->future_content->main_fetch)
      {
        switch (msg->data.fetch_info.type)
        {
          case type_HTML:
            set_content_html(bw->future_content);
            break;
          default:
            browser_window_stop_throbber(bw);
            return 1;
        }
      }
      break;

    case msg_FETCH_DATA:
      browser_window_set_status(bw, "Data received...");
      if (msg->f == bw->future_content->main_fetch)
        content_html_receive_data(bw->future_content, msg->data.fetch_data.block, sizeof(char), msg->data.fetch_data.block_size);
      break;

    case msg_FETCH_ABORT:
      browser_window_set_status(bw, "Request failed.");
      if (msg->f == bw->future_content->main_fetch)
      {
        browser_window_stop_throbber(bw);
        bw->future_content->main_fetch = NULL;
        cache_free(bw->future_content);
        bw->future_content = NULL;
      }
      break;

    case msg_FETCH_FINISHED:
      browser_window_set_status(bw, "Request complete.");
      if (msg->f == bw->future_content->main_fetch)
      {
        struct gui_message gmsg;
        if (bw->future_content->main_fetch->location != NULL)
          xfree(bw->url);
        bw->url = xstrdup(bw->future_content->main_fetch->location);

        gmsg.type = msg_SET_URL;
        gmsg.data.set_url.url = bw->url;
        gui_window_message(bw->window, &gmsg);

        htmlParseChunk(bw->future_content->data.html.parser, "", 0, 1);
        bw->future_content->main_fetch = NULL;
        previous_safety = gui_window_set_redraw_safety(bw->window, UNSAFE);
        if (bw->current_content != NULL)
          cache_free(bw->current_content);
        bw->current_content = bw->future_content;
        bw->future_content = NULL;
        browser_window_reformat(bw);
        gui_window_set_redraw_safety(bw->window, previous_safety);
        browser_window_stop_throbber(bw);
      }
      break;

    default:
      browser_window_set_status(bw, "???");
      break;
  }

  return 0;
}

int browser_window_action(struct browser_window* bw, struct browser_action* act)
{
  switch (act->type)
  {
    case act_MOUSE_AT:
     browser_window_follow_link(bw, act->data.mouse.x, act->data.mouse.y, 0);
      break;
    case act_MOUSE_CLICK:
      break;
    case act_CLEAR_SELECTION:
     browser_window_text_selection(bw, act->data.mouse.x, act->data.mouse.y, 0);
      break;
    case act_START_NEW_SELECTION:
     browser_window_text_selection(bw, act->data.mouse.x, act->data.mouse.y, 1);
      break;
    case act_ALTER_SELECTION:
     browser_window_text_selection(bw, act->data.mouse.x, act->data.mouse.y, 2);
      break;
    case act_FOLLOW_LINK:
     browser_window_follow_link(bw, act->data.mouse.x, act->data.mouse.y, 1);
      break;
    case act_FOLLOW_LINK_NEW_WINDOW:
     browser_window_follow_link(bw, act->data.mouse.x, act->data.mouse.y, 2);
      break;
    default:
      break;
  }
  return 0;
}

void box_under_area(struct box* box, int x, int y, int ox, int oy,
    struct box_selection** found, int* count, int* plot_index)
{
  struct box* c;

  if (box == NULL)
    return;

  *plot_index = *plot_index + 1;

  if (x >= box->x + ox && x <= box->x + ox + box->width &&
      y >= box->y + oy && y <= box->y + oy + box->height)
  {
    *found = xrealloc(*found, sizeof(struct box_selection) * (*count + 1));
    (*found)[*count].box = box;
    (*found)[*count].actual_x = box->x + ox;
    (*found)[*count].actual_y = box->y + oy;
    (*found)[*count].plot_index = *plot_index;
    *count = *count + 1;
  }

  for (c = box->children; c != 0; c = c->next)
    if (c->type != BOX_FLOAT_LEFT && c->type != BOX_FLOAT_RIGHT)
      box_under_area(c, x, y, box->x + ox, box->y + oy, found, count, plot_index);

  for (c = box->float_children; c != 0; c = c->next_float)
    box_under_area(c, x, y, box->x + ox, box->y + oy, found, count, plot_index);

  return;
}

void browser_window_follow_link(struct browser_window* bw,
  int click_x, int click_y, int click_type)
{
  struct box_selection* click_boxes;
  int found, plot_index;
  int i;
  int done = 0;

  found = 0;
  click_boxes = NULL;
  plot_index = 0;

  box_under_area(bw->current_content->data.html.layout->children,
                 click_x, click_y, 0, 0, &click_boxes, &found, &plot_index);

  if (found == 0)
    return;

  for (i = found - 1; i >= 0; i--)
  {
    if (click_boxes[i].box->href != NULL)
    {
      if (click_type == 1)
        browser_window_open_location(bw, (char*) click_boxes[i].box->href);
      else if (click_type == 2)
      {
        struct browser_window* bw_new;
        bw_new = create_browser_window(browser_TITLE | browser_TOOLBAR
          | browser_SCROLL_X_NONE | browser_SCROLL_Y_ALWAYS, 640, 480);
        gui_window_show(bw_new->window);
        if (bw->url != NULL)
          bw_new->url = xstrdup(bw->url);
        browser_window_open_location(bw_new, (char*) click_boxes[i].box->href);
      }
      else if (click_type == 0)
      {
        browser_window_set_status(bw, (char*) click_boxes[i].box->href);
        done = 1;
      }
      i = -1;
    }
  }

  if (click_type == 0 && done == 0)
    browser_window_set_status(bw, "");

  free(click_boxes);

  return;
}

void browser_window_text_selection(struct browser_window* bw,
  int click_x, int click_y, int click_type)
{
  struct box_selection* click_boxes;
  int found, plot_index;
  int i;

  if (click_type == 0 /* click_CLEAR_SELECTION */ )
  {
    browser_window_clear_text_selection(bw);
    return;
  }

  found = 0;
  click_boxes = NULL;
  plot_index = 0;

  box_under_area(bw->current_content->data.html.layout->children,
                 click_x, click_y, 0, 0, &click_boxes, &found, &plot_index);

  if (found == 0)
    return;

  for (i = found - 1; i >= 0; i--)
  {
    if (click_boxes[i].box->type == BOX_INLINE)
    {
      struct box_position new_pos;
      struct box_position* start;
      struct box_position* end;
      int click_char_offset, click_pixel_offset;

      /* shortcuts */
      start = &(bw->current_content->data.html.text_selection.start);
      end = &(bw->current_content->data.html.text_selection.end);

      font_position_in_string(click_boxes[i].box->text,
          click_boxes[i].box->font, click_boxes[i].box->length,
          click_x - click_boxes[i].actual_x,
          &click_char_offset, &click_pixel_offset);

      new_pos.box = click_boxes[i].box;
      new_pos.actual_box_x = click_boxes[i].actual_x;
      new_pos.actual_box_y = click_boxes[i].actual_y;
      new_pos.plot_index = click_boxes[i].plot_index;
      new_pos.char_offset = click_char_offset;
      new_pos.pixel_offset = click_pixel_offset;

      if (click_type == 1 /* click_START_SELECTION */ )
      {
        /* update both start and end */
        browser_window_clear_text_selection(bw);
        bw->current_content->data.html.text_selection.altering = alter_UNKNOWN;
        bw->current_content->data.html.text_selection.selected = 1;
        memcpy(start, &new_pos, sizeof(struct box_position));
        memcpy(end, &new_pos, sizeof(struct box_position));
        i = -1;
      }
      else if (bw->current_content->data.html.text_selection.selected == 1 &&
               click_type == 2 /* click_ALTER_SELECTION */)
      {
        /* alter selection */

        if (bw->current_content->data.html.text_selection.altering
            != alter_UNKNOWN)
        {
          if (bw->current_content->data.html.text_selection.altering
              == alter_START)
          {
            if (box_position_gt(&new_pos,end))
            {
              bw->current_content->data.html.text_selection.altering
                = alter_END;
              browser_window_change_text_selection(bw, end, &new_pos);
            }
            else
              browser_window_change_text_selection(bw, &new_pos, end);
          }
          else
          {
            if (box_position_lt(&new_pos,start))
            {
              bw->current_content->data.html.text_selection.altering
                = alter_START;
              browser_window_change_text_selection(bw, &new_pos, start);
            }
            else
              browser_window_change_text_selection(bw, start, &new_pos);
          }
          i = -1;
        }
        else
        {
          /* work out whether the start or end is being dragged */

          int click_start_distance = 0;
          int click_end_distance = 0;

          int inside_block = 0;
          int before_start = 0;
          int after_end = 0;

          if (box_position_lt(&new_pos, start))
            before_start = 1;

          if (box_position_gt(&new_pos, end))
            after_end = 1;

          if (!box_position_lt(&new_pos, start)
              && !box_position_gt(&new_pos, end))
            inside_block = 1;

          if (inside_block == 1)
          {
            click_start_distance = box_position_distance(start, &new_pos);
            click_end_distance = box_position_distance(end, &new_pos);
          }

          if (before_start == 1
              || (after_end == 0 && inside_block == 1
                  && click_start_distance < click_end_distance))
          {
            /* alter the start position */
            bw->current_content->data.html.text_selection.altering
              = alter_START;
            browser_window_change_text_selection(bw, &new_pos, end);
            i = -1;
          }
          else if (after_end == 1
                   || (before_start == 0 && inside_block == 1
                       && click_start_distance >= click_end_distance))
          {
            /* alter the end position */
            bw->current_content->data.html.text_selection.altering = alter_END;
            browser_window_change_text_selection(bw, start, &new_pos);
            i = -1;
          }
        }
      }
    }
  }

  free(click_boxes);

  return;
}

void browser_window_clear_text_selection(struct browser_window* bw)
{
  struct box_position* old_start;
  struct box_position* old_end;

  old_start = &(bw->current_content->data.html.text_selection.start);
  old_end = &(bw->current_content->data.html.text_selection.end);

  if (bw->current_content->data.html.text_selection.selected == 1)
  {
    bw->current_content->data.html.text_selection.selected = 0;
    browser_window_redraw_boxes(bw, old_start, old_end);
  }

  bw->current_content->data.html.text_selection.altering = alter_UNKNOWN;
}

void browser_window_change_text_selection(struct browser_window* bw,
  struct box_position* new_start, struct box_position* new_end)
{
  struct box_position start;
  struct box_position end;

  memcpy(&start, &(bw->current_content->data.html.text_selection.start), sizeof(struct box_position));
  memcpy(&end, &(bw->current_content->data.html.text_selection.end), sizeof(struct box_position));

  if (!box_position_eq(new_start, &start))
  {
    if (box_position_lt(new_start, &start))
      browser_window_redraw_boxes(bw, new_start, &start);
    else
      browser_window_redraw_boxes(bw, &start, new_start);
    memcpy(&start, new_start, sizeof(struct box_position));
  }

  if (!box_position_eq(new_end, &end))
  {
    if (box_position_lt(new_end, &end))
      browser_window_redraw_boxes(bw, new_end, &end);
    else
      browser_window_redraw_boxes(bw, &end, new_end);
    memcpy(&end, new_end, sizeof(struct box_position));
  }

  memcpy(&(bw->current_content->data.html.text_selection.start), &start, sizeof(struct box_position));
  memcpy(&(bw->current_content->data.html.text_selection.end), &end, sizeof(struct box_position));

  bw->current_content->data.html.text_selection.selected = 1;
}


int box_position_lt(struct box_position* x, struct box_position* y)
{
  return (x->plot_index < y->plot_index ||
          (x->plot_index == y->plot_index && x->char_offset < y->char_offset));
}

int box_position_gt(struct box_position* x, struct box_position* y)
{
  return (x->plot_index > y->plot_index ||
          (x->plot_index == y->plot_index && x->char_offset > y->char_offset));
}

int box_position_eq(struct box_position* x, struct box_position* y)
{
  return (x->plot_index == y->plot_index && x->char_offset == y->char_offset);
}

int box_position_distance(struct box_position* x, struct box_position* y)
{
  int dx = (y->actual_box_x + y->pixel_offset)
           - (x->actual_box_x + x->pixel_offset);
  int dy = (y->actual_box_y + y->box->height / 2)
           - (x->actual_box_y + x->box->height / 2);
  return dx*dx + dy*dy;
}

int redraw_min_x = INT_MAX;
int redraw_min_y = INT_MAX;
int redraw_max_x = INT_MIN;
int redraw_max_y = INT_MIN;

int redraw_box_list(struct browser_window* bw, struct box* current,
    int x, int y, struct box_position* start, struct box_position* end,
    int* plot)
{

  struct box* c;

  if (current == start->box)
    *plot = 1;

  if (*plot >= 1 && current->type == BOX_INLINE)
  {
    int minx = x + current->x;
    int miny = y + current->y;
    int maxx = x + current->x + current->width;
    int maxy = y + current->y + current->height;

    if (minx < redraw_min_x)
      redraw_min_x = minx;
    if (miny < redraw_min_y)
      redraw_min_y = miny;
    if (maxx > redraw_max_x)
      redraw_max_x = maxx;
    if (maxy > redraw_max_y)
      redraw_max_y = maxy;

    *plot = 2;
  }

  if (current == end->box)
    return 1;

  for (c = current->children; c != 0; c = c->next)
    if (c->type != BOX_FLOAT_LEFT && c->type != BOX_FLOAT_RIGHT)
      if (redraw_box_list(bw, c, x + current->x, y + current->y,
                          start, end, plot) == 1)
        return 1;

  for (c = current->float_children; c != 0; c = c->next_float)
    if (redraw_box_list(bw, c, x + current->x, y + current->y,
                        start, end, plot) == 1)
      return 1;

  return 0;
}

void browser_window_redraw_boxes(struct browser_window* bw, struct box_position* start, struct box_position* end)
{
  int plot = 0;

  if (box_position_eq(start, end))
    return;

  redraw_min_x = INT_MAX;
  redraw_min_y = INT_MAX;
  redraw_max_x = INT_MIN;
  redraw_max_y = INT_MIN;

  redraw_box_list(bw, bw->current_content->data.html.layout,
    0,0, start, end, &plot);

  if (plot == 2)
    gui_window_redraw(bw->window, redraw_min_x, redraw_min_y,
      redraw_max_x, redraw_max_y);
}