Source

dwb / src / view.c

Full commit
  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
/*
 * Copyright (c) 2010-2011 Stefan Bolte <portix@gmx.net>
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "view.h"

static void dwb_parse_setting(const char *);
static void dwb_parse_key_setting(const char *);
static void dwb_apply_settings(WebSettings *);

static char *lasturi;
static GList *allowed_plugins;

// CALLBACKS
static gboolean dwb_web_view_button_press_cb(WebKitWebView *, GdkEventButton *, GList *);
static gboolean dwb_web_view_close_web_view_cb(WebKitWebView *, GList *);
static gboolean dwb_web_view_console_message_cb(WebKitWebView *, char *, int , char *, GList *);
static WebKitWebView * dwb_web_view_create_web_view_cb(WebKitWebView *, WebKitWebFrame *, GList *);
static gboolean dwb_web_view_plugin_blocker_button_cb(GtkWidget *, GdkEventButton *, char *);
static gboolean dwb_web_view_download_requested_cb(WebKitWebView *, WebKitDownload *, GList *);
static WebKitWebView * dwb_web_view_inspect_web_view_cb(WebKitWebInspector *, WebKitWebView *, GList *);
static void dwb_web_view_hovering_over_link_cb(WebKitWebView *, char *, char *, GList *);
static gboolean dwb_web_view_mime_type_policy_cb(WebKitWebView *, WebKitWebFrame *, WebKitNetworkRequest *, char *, WebKitWebPolicyDecision *, GList *);
static gboolean dwb_web_view_enter_notify_cb(GtkWidget *, GdkEventCrossing *, GList *);
static gboolean dwb_web_view_navigation_policy_cb(WebKitWebView *, WebKitWebFrame *, WebKitNetworkRequest *, WebKitWebNavigationAction *, WebKitWebPolicyDecision *, GList *);
static gboolean dwb_web_view_new_window_policy_cb(WebKitWebView *, WebKitWebFrame *, WebKitNetworkRequest *, WebKitWebNavigationAction *, WebKitWebPolicyDecision *, GList *);
static void dwb_web_view_resource_request_cb(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *, WebKitNetworkRequest *, WebKitNetworkResponse *, GList *);
static void dwb_web_view_window_object_cleared_cb(WebKitWebView *, WebKitWebFrame *, JSGlobalContextRef *, JSObjectRef *, GList *);
static gboolean dwb_web_view_scroll_cb(GtkWidget *, GdkEventScroll *, GList *);
static gboolean dwb_web_view_value_changed_cb(GtkAdjustment *, GList *);
static void dwb_web_view_title_cb(WebKitWebView *, GParamSpec *, GList *);
static void dwb_web_view_load_status_cb(WebKitWebView *, GParamSpec *, GList *);
static void dwb_web_view_realize_cb(GtkWidget *, GList *);
static gboolean dwb_view_entry_keyrelease_cb(GtkWidget *, GdkEventKey *);
static gboolean dwb_view_entry_keypress_cb(GtkWidget *, GdkEventKey *);
static gboolean dwb_view_entry_activate_cb(GtkEntry *);
static gboolean dwb_view_tab_button_press_cb(GtkWidget *, GdkEventButton *, GList *);

/* WEB_VIEW_CALL_BACKS {{{*/

/* dwb_web_view_button_press_cb(WebKitWebView *web, GdkEventButton *button, GList *gl) {{{*/
static gboolean
dwb_web_view_button_press_cb(WebKitWebView *web, GdkEventButton *e, GList *gl) {
  WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web, e);
  WebKitHitTestResultContext context;
  g_object_get(result, "context", &context, NULL);
  gboolean ret = false;

  if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE) {
    dwb_insert_mode(NULL);
  }
  else if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION && e->type == GDK_BUTTON_PRESS && e->state & GDK_BUTTON1_MASK) {
    char *clipboard = gtk_clipboard_wait_for_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY));
    g_strstrip(clipboard);
    Arg a = { .p = clipboard };
    if (e->button == 3) {
      dwb_load_uri(&a);
      ret = true;
    }
    else if (e->button == 2) {
      dwb_add_view(&a);
      ret = true;
    }
    FREE(clipboard);
  }
  else if (e->button == 1 && e->type == GDK_BUTTON_PRESS) {
    dwb_focus(gl);
  }
  else if (e->button == 8) {
    webkit_web_view_go_back(web);
  }
  else if (e->button == 9) {
    webkit_web_view_go_forward(web);
  }
  return ret;
}/*}}}*/

/* dwb_web_view_close_web_view_cb(WebKitWebView *web, GList *gl) {{{*/
static gboolean 
dwb_web_view_close_web_view_cb(WebKitWebView *web, GList *gl) {
  Arg a = { .p = gl };
  dwb_com_remove_view(NULL, &a);
  return true;
}/*}}}*/

/* dwb_web_view_console_message_cb(WebKitWebView *web, char *message, int line, char *sourceid, GList *gl) {{{*/
static gboolean 
dwb_web_view_console_message_cb(WebKitWebView *web, char *message, int line, char *sourceid, GList *gl) {
  if (!strcmp(sourceid, KEY_SETTINGS)) {
    dwb_parse_key_setting(message);
  }
  else if (!(strcmp(sourceid, SETTINGS))) {
    dwb_parse_setting(message);
  }
  else if (gl == dwb.state.fview && !(strcmp(message, "_dwb_input_mode_"))) {
    dwb_insert_mode(NULL);
  }
  else if (gl == dwb.state.fview && !(strcmp(message, "_dwb_normal_mode_"))) {
    dwb_normal_mode(false);
  }
  if (!strcmp(message, "_dwb_no_input_")) {
    dwb_set_error_message(gl, "No input found in current context");
  }
  return true;
}/*}}}*/

/* dwb_web_view_create_web_view_cb(WebKitWebView *, WebKitWebFrame *, GList *) {{{*/
static WebKitWebView * 
dwb_web_view_create_web_view_cb(WebKitWebView *web, WebKitWebFrame *frame, GList *gl) {
  if (dwb.misc.tabbed_browsing) {
    dwb_add_view(NULL); 
    return CURRENT_WEBVIEW();
  }
  else {
    dwb.state.nv = OPEN_NEW_WINDOW;
    return web;
  }
  // return ((View*)dwb.state.fview->data)->web;
}/*}}}*/

/* dwb_web_view_plugin_blocker_button_cb (GtkWidget *, GdkEventButton, char *uri) {{{*/
static gboolean 
dwb_web_view_plugin_blocker_button_cb(GtkWidget *widget, GdkEventButton *e, char *uri) {
  allowed_plugins = g_list_prepend(allowed_plugins, g_strdup(uri));
  GtkWidget *parent = gtk_widget_get_parent(widget);
  gtk_container_remove(GTK_CONTAINER(parent), widget);
  gtk_widget_destroy(widget);
  webkit_web_view_reload(CURRENT_WEBVIEW());
  return true;
}/*}}}*/

/* dwb_web_view_create_plugin_widget_cb(WebKitWebView *, WebKitWebFrame *, GList *) {{{*/
GtkWidget * 
dwb_web_view_create_plugin_widget_cb(WebKitWebView *web, char *mime_type, char *uri, GHashTable *t, GList *gl) {
  GtkWidget *event_box = NULL;
  View *v = gl->data;
  const char *host = CURRENT_HOST();

  for (GList *l = allowed_plugins; l; l=l->next) {
    if (!strcmp(uri, l->data)) {
      return NULL;
    }
  }
  if (v->status->plugin_blocker && host && !g_list_find_custom(dwb.fc.plugins_allow, host, (GCompareFunc)strcmp) ) {
    lasturi = g_strdup(uri);

    // save all pluginuris so they can be freed since an allocated string has to be passed to g_signal_connect
    Plugin *p = malloc(sizeof(Plugin));
    p->uri = lasturi;
    p->next = v->status->plugins;
    v->status->plugins = p;

    event_box = gtk_event_box_new();
    GtkWidget *label = gtk_label_new("Plugin blocked");
    gtk_container_add(GTK_CONTAINER(event_box), label);
    g_signal_connect(G_OBJECT(event_box), "button-press-event", G_CALLBACK(dwb_web_view_plugin_blocker_button_cb), lasturi);

    gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &dwb.color.active_fg);
    gtk_widget_modify_bg(event_box, GTK_STATE_NORMAL, &dwb.color.active_bg);
    PangoFontDescription *fd = dwb.font.fd_bold;
    pango_font_description_set_absolute_size(fd, dwb.font.active_size * PANGO_SCALE);
    gtk_widget_modify_font(label, fd);
    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);

    gtk_widget_show_all(event_box);
  }
  return event_box;
}/*}}}*/

/* dwb_web_view_download_requested_cb(WebKitWebView *, WebKitDownload *, GList *) {{{*/
static gboolean 
dwb_web_view_download_requested_cb(WebKitWebView *web, WebKitDownload *download, GList *gl) {
  dwb_dl_get_path(gl, download);
  return true;
}/*}}}*/

/* dwb_web_view_inspect_web_view_cb(WebKitWebInspector *, WebKitWebView *, GList * *){{{*/
static WebKitWebView * 
dwb_web_view_inspect_web_view_cb(WebKitWebInspector *inspector, WebKitWebView *wv, GList *gl) {
  GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  GtkWidget *webview = webkit_web_view_new();
  
  gtk_container_add(GTK_CONTAINER(window), webview);
  gtk_widget_show_all(window);

  return WEBKIT_WEB_VIEW(webview);
}/*}}}*/

/* dwb_web_view_hovering_over_link_cb(WebKitWebView *, char *title, char *uri, GList *) {{{*/
static void 
dwb_web_view_hovering_over_link_cb(WebKitWebView *web, char *title, char *uri, GList *gl) {
  View *v = VIEW(gl);
  if (uri) {
    dwb_set_status_bar_text(v->rstatus, uri, NULL, NULL);
  }
  else {
    dwb_update_status_text(gl, NULL);
  }
}/*}}}*/

/* dwb_web_view_mime_type_policy_cb {{{*/
static gboolean 
dwb_web_view_mime_type_policy_cb(WebKitWebView *web, WebKitWebFrame *frame, WebKitNetworkRequest *request, char *mimetype, WebKitWebPolicyDecision *policy, GList *gl) {
  View *v = gl->data;

  v->status->mimetype = g_strdup(mimetype);

  if (!webkit_web_view_can_show_mime_type(web, mimetype) ||  dwb.state.nv == OPEN_DOWNLOAD) {
    dwb.state.mimetype_request = g_strdup(mimetype);
    webkit_web_policy_decision_download(policy);
    return true;
  }
  return  false;
}/*}}}*/

/* dwb_web_view_enter_notify_cb(GtkWidget *, GdkEventCrossing *, GList *){{{*/
static gboolean 
dwb_web_view_enter_notify_cb(GtkWidget *web, GdkEventCrossing *e, GList *gl) {
  dwb_focus(gl);
  return false;
}/*}}}*/

/* dwb_web_view_navigation_policy_cb {{{*/
static gboolean 
dwb_web_view_navigation_policy_cb(WebKitWebView *web, WebKitWebFrame *frame, WebKitNetworkRequest *request, WebKitWebNavigationAction *action,
    WebKitWebPolicyDecision *policy, GList *gl) {

  char *uri = (char *) webkit_network_request_get_uri(request);
  int button = webkit_web_navigation_action_get_button(action);

  Arg a = { .p = uri };

  if (button != -1) {
    if (button == 2) {
        dwb_add_view(&a);
        webkit_web_policy_decision_ignore(policy);
        return true;
    }
  }

  if (dwb.state.nv == OPEN_NEW_VIEW || dwb.state.nv == OPEN_NEW_WINDOW) {
    if (dwb.state.nv == OPEN_NEW_VIEW) {
      dwb.state.nv = OPEN_NORMAL;
      dwb_add_view(&a); 
    }
    else {
      dwb_new_window(&a);
    }
    dwb.state.nv = OPEN_NORMAL;
    return true;
  }
  const char *request_uri = webkit_network_request_get_uri(request);
  WebKitWebNavigationReason reason = webkit_web_navigation_action_get_reason(action);

  if (reason == WEBKIT_WEB_NAVIGATION_REASON_FORM_SUBMITTED) {
    if (dwb.state.mode == INSERT_MODE) {
      dwb_normal_mode(true);
    }
    if (dwb.state.mode == SEARCH_FIELD_MODE) {
      webkit_web_policy_decision_ignore(policy);
      dwb.state.search_engine = dwb.state.form_name && !g_strrstr(request_uri, HINT_SEARCH_SUBMIT) 
        ? g_strdup_printf("%s?%s=%s", request_uri, dwb.state.form_name, HINT_SEARCH_SUBMIT) 
        : g_strdup(request_uri);
      dwb_save_searchengine();
      return true;
    }
  }

  if (dwb_handle_mail(uri)) 
    return true;

  return false;
}/*}}}*/

/* dwb_web_view_new_window_policy_cb {{{*/
static gboolean 
dwb_web_view_new_window_policy_cb(WebKitWebView *web, WebKitWebFrame *frame, WebKitNetworkRequest *request, WebKitWebNavigationAction *action, WebKitWebPolicyDecision *policy, GList *gl) {
  return false;
}/*}}}*/

/* dwb_web_view_resource_request_cb{{{*/
static void 
dwb_web_view_resource_request_cb(WebKitWebView *web, WebKitWebFrame *frame,
    WebKitWebResource *resource, WebKitNetworkRequest *request,
    WebKitNetworkResponse *response, GList *gl) {
  SoupMessage *msg = webkit_network_request_get_message(request);

  if (dwb_block_ad(gl, webkit_network_request_get_uri(request))) {
    webkit_network_request_set_uri(request, "about:blank");
    return;
  }
  
  if (!msg) 
    return;

  static SoupBuffer buffer;
  SoupContentSniffer *sniffer = soup_content_sniffer_new();
  View *v = gl->data;

  if (v && v->status) {
    const char *content_type = soup_content_sniffer_sniff(sniffer, msg, &buffer, NULL);
    if (!v->status->current_host) {
      SoupURI *uri = soup_message_get_uri(msg);
      v->status->current_host = g_strdup(uri->host);
      v->status->block_current = 
        !dwb_get_host_blocked(dwb.fc.content_block_allow, v->status->current_host) && !dwb_get_host_blocked(dwb.fc.content_allow, v->status->current_host) 
        ? true : false;
    }
    if (v->status->block && v->status->block_current && g_regex_match_simple(dwb.misc.content_block_regex, content_type, 0, 0)) {
      webkit_network_request_set_uri(request, "about:blank");
      v->status->items_blocked++;
    }
  }
}/*}}}*/

/* dwb_web_view_window_object_cleared_cb {{{*/
static void 
dwb_web_view_window_object_cleared_cb(WebKitWebView *web, WebKitWebFrame *frame, 
    JSGlobalContextRef *context, JSObjectRef *object, GList *gl) {
    webkit_web_view_execute_script(web, dwb.misc.systemscripts);
}/*}}}*/

/* dwb_web_view_scroll_cb(GtkWidget *w, GdkEventScroll * GList *) {{{*/
static gboolean
dwb_web_view_scroll_cb(GtkWidget *w, GdkEventScroll *e, GList *gl) {
  dwb_update_status_text(gl, NULL);
  return  false;
}/*}}}*/

/* dwb_web_view_value_changed_cb(GtkAdjustment *a, GList *gl) {{{ */
static gboolean
dwb_web_view_value_changed_cb(GtkAdjustment *a, GList *gl) {
  dwb_update_status_text(gl, a);
  return false;
}/* }}} */

/* dwb_web_view_title_cb {{{*/
static void 
dwb_web_view_title_cb(WebKitWebView *web, GParamSpec *pspec, GList *gl) {
  dwb_update_status(gl);
}/*}}}*/
static void 
dwb_web_view_progress_cb(WebKitWebView *web, GParamSpec *pspec, GList *gl) {
  View *v = gl->data;
  v->status->progress = webkit_web_view_get_progress(web) * 100;
  if (v->status->progress == 100) {
    v->status->progress = 0;
  }
  dwb_update_status(gl);
}

/* dwb_web_view_load_status_cb {{{*/
static void 
dwb_web_view_load_status_cb(WebKitWebView *web, GParamSpec *pspec, GList *gl) {
  WebKitLoadStatus status = webkit_web_view_get_load_status(web);

  switch (status) {
    case WEBKIT_LOAD_PROVISIONAL: 
      dwb_view_clean_vars(gl);
      break;
    case WEBKIT_LOAD_COMMITTED: 
      break;
    case WEBKIT_LOAD_FINISHED:
      dwb_update_status(gl);
      dwb_prepend_navigation(gl, &dwb.fc.history);
      dwb_clean_load_end(gl);
      // reload all user scripts, so the active element can automatically be
      // detected. 
      // TODO remove with new webkit version 
      webkit_web_view_execute_script(web, dwb.misc.scripts);
      break;
    case WEBKIT_LOAD_FAILED: 
      dwb_clean_load_end(gl);
      break;
    default:
      break;
  }
}/*}}}*/

/* dwb_web_view_realize_cb {{{*/
static void
dwb_web_view_realize_cb(GtkWidget *widget, GList *gl) {
  GdkWindow *window = gtk_widget_get_window(widget);
  GdkEventMask events = gdk_window_get_events(window);
  gdk_window_set_events(window, events | GDK_ENTER_NOTIFY_MASK);
}/*}}}*/

// Entry
/* dwb_entry_keyrelease_cb {{{*/
static gboolean 
dwb_view_entry_keyrelease_cb(GtkWidget* entry, GdkEventKey *e) { 
  Mode mode = dwb.state.mode;

  if (mode == HINT_MODE) {
    if (DIGIT(e) || DWB_TAB_KEY(e)) {
      return true;
    }
    else {
      return dwb_update_hints(e);
    }
  }
  else if (mode == FIND_MODE) {
    dwb_update_search(dwb.state.forward_search);
  }
  return false;
}/*}}}*/

/* dwb_entry_keypress_cb(GtkWidget* entry, GdkEventKey *e) {{{*/
static gboolean 
dwb_view_entry_keypress_cb(GtkWidget* entry, GdkEventKey *e) {
  Mode mode = dwb.state.mode;
  gboolean ret = false;
  if (e->keyval == GDK_BackSpace) {
    return false;
  }
  if (mode == HINT_MODE) {
    if (DIGIT(e) || DWB_TAB_KEY(e)) {
      return dwb_update_hints(e);
    }
    else if  (e->keyval == GDK_Return) {
      return true;
    }
  }
  else if (mode == SEARCH_FIELD_MODE) {
    if (DWB_TAB_KEY(e)) {
      return dwb_update_hints(e);
    }
    else if (e->keyval == GDK_Return) {
      return false;
    }
  }
  else if (mode == DOWNLOAD_GET_PATH) {
    if (DWB_TAB_KEY(e)) {
      dwb_comp_complete_download(e->state & GDK_SHIFT_MASK);
      return true;
    }
    else {
      dwb_comp_clean_path_completion();
    }
  }
  else if (mode & COMPLETION_MODE && e->keyval != GDK_Tab && e->keyval != GDK_ISO_Left_Tab && !e->is_modifier && !CLEAN_STATE(e)) {
    dwb_comp_clean_completion();
  }
  else if (mode == FIND_MODE) {
    return false;
  }
  else if (DWB_TAB_KEY(e)) {
    dwb_comp_complete(dwb_eval_completion_type(), e->state & GDK_SHIFT_MASK);
    return true;
  }
  if (dwb_eval_editing_key(e)) {
    ret = true;
  }
  return ret;
}/*}}}*/

/* dwb_entry_activate_cb (GtkWidget *entry) {{{*/
static gboolean 
dwb_view_entry_activate_cb(GtkEntry* entry) {
  gboolean ret = false;
  Mode mode = dwb.state.mode;

  if (mode == HINT_MODE) {
    ret = false;
  }
  else if (mode == FIND_MODE) {
    dwb_focus_scroll(dwb.state.fview);
    dwb_search(NULL);
  }
  else if (mode == SEARCH_FIELD_MODE) {
    dwb_submit_searchengine();
  }
  else if (mode == SETTINGS_MODE) {
    dwb_parse_setting(GET_TEXT());
  }
  else if (mode == KEY_MODE) {
    dwb_parse_key_setting(GET_TEXT());
  }
  else if (mode == COMMAND_MODE) {
    dwb_parse_command_line(GET_TEXT());
  }
  else if (mode == DOWNLOAD_GET_PATH) {
    dwb_dl_start();
  }
  else if (mode == SAVE_SESSION) {
    dwb_session_save(GET_TEXT());
    dwb_end();
  }
  else {
    Arg a = { .n = 0, .p = (char*)GET_TEXT() };
    dwb_load_uri(&a);
    dwb_prepend_navigation_with_argument(&dwb.fc.commands, a.p, NULL);
    dwb_normal_mode(true);
  }

  return true;
}/*}}}*/
/*}}}*/

/* dwb_view_tab_button_press_cb(GtkWidget, GdkEventButton* , GList * ){{{*/
static gboolean
dwb_view_tab_button_press_cb(GtkWidget *tabevent, GdkEventButton *e, GList *gl) {
  if (e->button == 1 && e->type == GDK_BUTTON_PRESS) {
    Arg a = { .p = gl };
    dwb_focus(gl);
    dwb_view_push_master(&a);
    return true;
  }
  return false;
}/*}}}*/

/* DWB_WEB_VIEW {{{*/

/* dwb_web_view_add_history_item(GList *gl) {{{*/
void 
dwb_web_view_add_history_item(GList *gl) {
  WebKitWebView *web = WEBVIEW(gl);
  const char *uri = webkit_web_view_get_uri(web);
  const char *title = webkit_web_view_get_title(web);
  WebKitWebBackForwardList *bl = webkit_web_view_get_back_forward_list(web);
  WebKitWebHistoryItem *hitem = webkit_web_history_item_new_with_data(uri,  title);
  webkit_web_back_forward_list_add_item(bl, hitem);
}/*}}}*/

/* dwb_view_modify_style(GList *gl, GdkColor *fg, GdkColor *bg, GdkColor *tabfg, GdkColor *tabbg, PangoFontDescription *fd, int fontsize) {{{*/
void 
dwb_view_modify_style(GList *gl, GdkColor *fg, GdkColor *bg, GdkColor *tabfg, GdkColor *tabbg, PangoFontDescription *fd, int fontsize) {
  View *v = gl->data;
  if (fg) {
    gtk_widget_modify_fg(v->rstatus, GTK_STATE_NORMAL, fg);
    gtk_widget_modify_fg(v->lstatus, GTK_STATE_NORMAL, fg);
    gtk_widget_modify_text(v->entry, GTK_STATE_NORMAL, fg);
  }
  if (bg) {
    gtk_widget_modify_bg(v->statusbox, GTK_STATE_NORMAL, bg);
    gtk_widget_modify_base(v->entry, GTK_STATE_NORMAL, bg);
  }
  if (tabfg) 
    gtk_widget_modify_fg(v->tablabel, GTK_STATE_NORMAL, tabfg);
  if (tabbg)
    gtk_widget_modify_bg(v->tabevent, GTK_STATE_NORMAL, tabbg);
  if (fd) {
    pango_font_description_set_absolute_size(fd, fontsize * PANGO_SCALE);
    gtk_widget_modify_font(v->rstatus, fd);
    gtk_widget_modify_font(v->lstatus, fd);
    gtk_widget_modify_font(v->tablabel, fd);
  }

} /*}}}*/

/* dwb_view_set_active_style (GList *) {{{*/
void 
dwb_view_set_active_style(GList *gl) {
  dwb_view_modify_style(gl, &dwb.color.active_fg, &dwb.color.active_bg, &dwb.color.tab_active_fg, &dwb.color.tab_active_bg, dwb.font.fd_bold, dwb.font.active_size);
}/*}}}*/

/* dwb_view_set_normal_style {{{*/
void 
dwb_view_set_normal_style(GList *gl) {
  dwb_view_modify_style(gl, &dwb.color.normal_fg, &dwb.color.normal_bg, &dwb.color.tab_normal_fg, &dwb.color.tab_normal_bg, dwb.font.fd_bold, dwb.font.normal_size);
}/*}}}*/

/* dwb_web_view_init_signals(View *v) {{{*/
static void
dwb_web_view_init_signals(GList *gl) {
  View *v = gl->data;
  GtkAdjustment *a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(v->scroll));
  v->status->signals[SIG_BUTTON_PRESS]          = g_signal_connect(v->web, "button-press-event",                    G_CALLBACK(dwb_web_view_button_press_cb), gl);
  v->status->signals[SIG_CLOSE_WEB_VIEW]        = g_signal_connect(v->web, "close-web-view",                        G_CALLBACK(dwb_web_view_close_web_view_cb), gl);
  v->status->signals[SIG_CONSOLE_MESSAGE]       = g_signal_connect(v->web, "console-message",                       G_CALLBACK(dwb_web_view_console_message_cb), gl);
  v->status->signals[SIG_CREATE_WEB_VIEW]       = g_signal_connect(v->web, "create-web-view",                       G_CALLBACK(dwb_web_view_create_web_view_cb), gl);
  v->status->signals[SIG_CREATE_PLUGIN_WIDGET]  = g_signal_connect(v->web, "create-plugin-widget",                  G_CALLBACK(dwb_web_view_create_plugin_widget_cb), gl);
  v->status->signals[SIG_DOWNLOAD_REQUESTED]    = g_signal_connect(v->web, "download-requested",                    G_CALLBACK(dwb_web_view_download_requested_cb), gl);
  v->status->signals[SIG_HOVERING_OVER_LINK]    = g_signal_connect(v->web, "hovering-over-link",                    G_CALLBACK(dwb_web_view_hovering_over_link_cb), gl);
  v->status->signals[SIG_MIME_TYPE]             = g_signal_connect(v->web, "mime-type-policy-decision-requested",   G_CALLBACK(dwb_web_view_mime_type_policy_cb), gl);
  v->status->signals[SIG_NAVIGATION]            = g_signal_connect(v->web, "navigation-policy-decision-requested",  G_CALLBACK(dwb_web_view_navigation_policy_cb), gl);
  v->status->signals[SIG_NEW_WINDOW]            = g_signal_connect(v->web, "new-window-policy-decision-requested",  G_CALLBACK(dwb_web_view_new_window_policy_cb), gl);
  v->status->signals[SIG_RESOURCE_REQUEST]      = g_signal_connect(v->web, "resource-request-starting",             G_CALLBACK(dwb_web_view_resource_request_cb), gl);
  v->status->signals[SIG_WINDOW_OBJECT]         = g_signal_connect(v->web, "window-object-cleared",                 G_CALLBACK(dwb_web_view_window_object_cleared_cb), gl);

  v->status->signals[SIG_LOAD_STATUS]           = g_signal_connect(v->web, "notify::load-status",                   G_CALLBACK(dwb_web_view_load_status_cb), gl);
  v->status->signals[SIG_PROGRESS]              = g_signal_connect(v->web, "notify::progress",                   G_CALLBACK(dwb_web_view_progress_cb), gl);
  v->status->signals[SIG_TITLE]                 = g_signal_connect(v->web, "notify::title",                         G_CALLBACK(dwb_web_view_title_cb), gl);
  v->status->signals[SIG_SCROLL]                = g_signal_connect(v->web, "scroll-event",                          G_CALLBACK(dwb_web_view_scroll_cb), gl);
  v->status->signals[SIG_VALUE_CHANGED]         = g_signal_connect(a,      "value-changed",                         G_CALLBACK(dwb_web_view_value_changed_cb), gl);

  v->status->signals[SIG_ENTRY_KEY_PRESS]       = g_signal_connect(v->entry, "key-press-event",                     G_CALLBACK(dwb_view_entry_keypress_cb), NULL);
  v->status->signals[SIG_ENTRY_KEY_RELEASE]     = g_signal_connect(v->entry, "key-release-event",                   G_CALLBACK(dwb_view_entry_keyrelease_cb), NULL);
  v->status->signals[SIG_ENTRY_ACTIVATE]        = g_signal_connect(v->entry, "activate",                            G_CALLBACK(dwb_view_entry_activate_cb), NULL);

  v->status->signals[SIG_TAB_BUTTON_PRESS]      = g_signal_connect(v->tabevent, "button-press-event",               G_CALLBACK(dwb_view_tab_button_press_cb), gl);
  v->status->signals[SIG_ENTER_NOTIFY]          = g_signal_connect(v->web,    "enter-notify-event",                   G_CALLBACK(dwb_web_view_enter_notify_cb), gl);
} /*}}}*/

/* dwb_view_clean_vars(GList *){{{*/
void
dwb_view_clean_vars(GList *gl) {
  View *v = gl->data;

  for (Plugin *p = v->status->plugins; p; p=p->next) {
    g_free(p->uri);
    g_free(p);
  }
  v->status->plugins = NULL;

  v->status->items_blocked = 0;
  if (v->status->current_host) {
    g_free(v->status->current_host); 
    v->status->current_host = NULL;
  }
}/*}}}*/

/* dwb_view_create_web_view(View *v)         return: GList * {{{*/
static GList * 
dwb_view_create_web_view(GList *gl) {
  View *v = g_malloc(sizeof(View));

  ViewStatus *status = g_malloc(sizeof(ViewStatus));
  status->search_string = NULL;
  status->downloads = NULL;
  status->current_host = NULL;
  status->custom_encoding = false;
  status->plugins = NULL;
  status->progress = 0;
  v->status = status;

  v->vbox = gtk_vbox_new(false, 0);
  v->web = webkit_web_view_new();
  g_signal_connect(v->web, "realize", G_CALLBACK(dwb_web_view_realize_cb), gl);

  // Entry
  v->entry = gtk_entry_new();
  gtk_entry_set_inner_border(GTK_ENTRY(v->entry), NULL);

  gtk_widget_modify_font(v->entry, dwb.font.fd_normal);
  gtk_entry_set_has_frame(GTK_ENTRY(v->entry), false);
  gtk_entry_set_inner_border(GTK_ENTRY(v->entry), false);

  // completion
  v->bottombox = gtk_vbox_new(false, 1);

  // Statusbox
  GtkWidget *status_hbox;
  v->statusbox = gtk_event_box_new();
  v->lstatus = gtk_label_new(NULL);
  v->rstatus = gtk_label_new(NULL);

  gtk_misc_set_alignment(GTK_MISC(v->lstatus), 0.0, 0.5);
  gtk_misc_set_alignment(GTK_MISC(v->rstatus), 1.0, 0.5);
  gtk_label_set_use_markup(GTK_LABEL(v->lstatus), true);
  gtk_label_set_use_markup(GTK_LABEL(v->rstatus), true);
  gtk_label_set_ellipsize(GTK_LABEL(v->rstatus), PANGO_ELLIPSIZE_MIDDLE);

  status_hbox = gtk_hbox_new(false, 2);
  gtk_box_pack_start(GTK_BOX(status_hbox), v->lstatus, false, false, 0);
  gtk_box_pack_start(GTK_BOX(status_hbox), v->entry, true, true, 0);
  gtk_box_pack_start(GTK_BOX(status_hbox), v->rstatus, true, true, 0);
  gtk_container_add(GTK_CONTAINER(v->statusbox), status_hbox);

  gtk_box_pack_end(GTK_BOX(v->bottombox), v->statusbox, false, false, 0);

  // Srolling
  v->scroll = gtk_scrolled_window_new(NULL, NULL);
  gtk_container_add(GTK_CONTAINER(v->scroll), v->web);
  WebKitWebFrame *frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(v->web));
  g_signal_connect(frame, "scrollbars-policy-changed", G_CALLBACK(dwb_true), NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(v->scroll), GTK_POLICY_NEVER, GTK_POLICY_NEVER);

  // WebInspector
  WebKitWebInspector *inspector = webkit_web_view_get_inspector(WEBKIT_WEB_VIEW(v->web));
  g_signal_connect(inspector, "inspect-web-view", G_CALLBACK(dwb_web_view_inspect_web_view_cb), gl);

  // Tabbar
  v->tabevent = gtk_event_box_new();
  v->tablabel = gtk_label_new(NULL);

  gtk_label_set_use_markup(GTK_LABEL(v->tablabel), true);
  gtk_label_set_width_chars(GTK_LABEL(v->tablabel), 1);
  gtk_misc_set_alignment(GTK_MISC(v->tablabel), 0.0, 0.5);
  gtk_label_set_ellipsize(GTK_LABEL(v->tablabel), PANGO_ELLIPSIZE_END);

  gtk_container_add(GTK_CONTAINER(v->tabevent), v->tablabel);
  gtk_widget_modify_font(v->tablabel, dwb.font.fd_normal);

  gtk_box_pack_end(GTK_BOX(dwb.gui.topbox), v->tabevent, true, true, 0);

  gtk_box_pack_start(GTK_BOX(v->vbox), v->scroll, true, true, 0);
  gtk_box_pack_start(GTK_BOX(v->vbox), v->bottombox, false, false, 0);

  gtk_box_pack_start(GTK_BOX(dwb.gui.left), v->vbox, true, true, 0);
  
  // Show
  gtk_widget_show(v->vbox);
  gtk_widget_show(v->lstatus);
  gtk_widget_show(v->entry);
  gtk_widget_show(v->rstatus);
  gtk_widget_show(status_hbox);
  gtk_widget_show(v->statusbox);
  gtk_widget_show(v->bottombox);
  gtk_widget_show_all(v->scroll);
  gtk_widget_show_all(v->tabevent);

  gl = g_list_prepend(gl, v);
  dwb_web_view_init_signals(gl);
  webkit_web_view_set_settings(WEBKIT_WEB_VIEW(v->web), webkit_web_settings_copy(dwb.state.web_settings));
  // apply settings
  v->setting = dwb_get_default_settings();
  for (GList *l = g_hash_table_get_values(v->setting); l; l=l->next) {
    WebSettings *s = l->data;
    if (!s->builtin && !s->global) {
      s->func(gl, s);
    }
  }
  dwb_view_clean_vars(gl);
  return gl;
} /*}}}*/

/* dwb_view_push_master (Arg *) {{{*/
gboolean 
dwb_view_push_master(Arg *arg) {
  GList *gl = NULL, *l = NULL;
  View *old = NULL, *new;
  if (!dwb.state.views->next) {
    return false;
  }
  if (arg && arg->p) {
    gl = arg->p;
  }
  else if (dwb.state.nummod) {
    gl = g_list_nth(dwb.state.views, dwb.state.nummod);
    if (!gl) {
      return false;
    }
    CLEAR_COMMAND_TEXT(dwb.state.views);
    dwb_view_set_normal_style(dwb.state.fview);
  }
  else {
    gl = dwb.state.fview;
  }
  if (gl == dwb.state.views) {
    old = gl->data;
    l = dwb.state.views->next;
    new = l->data;
    gtk_widget_reparent(old->vbox, dwb.gui.right);
    gtk_box_reorder_child(GTK_BOX(dwb.gui.right), old->vbox, 0);
    gtk_widget_reparent(new->vbox, dwb.gui.left);
    dwb.state.views = g_list_remove_link(dwb.state.views, l);
    dwb.state.views = g_list_concat(l, dwb.state.views);
    dwb_focus(l);
  }
  else {
    old = dwb.state.views->data;
    new = gl->data;
    gtk_widget_reparent(new->vbox, dwb.gui.left);
    gtk_widget_reparent(old->vbox, dwb.gui.right);
    gtk_box_reorder_child(GTK_BOX(dwb.gui.right), old->vbox, 0);
    dwb.state.views = g_list_remove_link(dwb.state.views, gl);
    dwb.state.views = g_list_concat(gl, dwb.state.views);
    dwb_grab_focus(dwb.state.views);
  }
  if (dwb.state.layout & MAXIMIZED) {
    gtk_widget_show(dwb.gui.left);
    gtk_widget_hide(dwb.gui.right);
    gtk_widget_show(new->vbox);
    gtk_widget_hide(old->vbox);
  }
  gtk_box_reorder_child(GTK_BOX(dwb.gui.topbox), new->tabevent, -1);
  dwb_update_layout();
  return true;
}/*}}}*/

/* dwb_view_remove (void) {{{*/
void
dwb_view_remove() {
  GList *gl = NULL;
  if (!dwb.state.views->next) {
    dwb_end();
    exit(EXIT_SUCCESS);
  }
  if (dwb.state.nummod) {
    gl = g_list_nth(dwb.state.views, dwb.state.nummod);
  }
  else {
    gl = dwb.state.fview;
    if ( !(dwb.state.fview = dwb.state.fview->next) ) {
      dwb.state.fview = dwb.state.views;
      gtk_widget_show_all(dwb.gui.topbox);
    }
  }
  View *v = gl->data;
  if (gl == dwb.state.views) {
    if (dwb.state.views->next) {
      gtk_widget_reparent(VIEW(dwb.state.views->next)->vbox, dwb.gui.left);
    }
  }

  /* Get History for the undo list */
  WebKitWebBackForwardList *bflist = webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(v->web));
  GList *store = NULL;

  for (int i = -webkit_web_back_forward_list_get_back_length(bflist); i<=0; i++) {
    WebKitWebHistoryItem *item = webkit_web_back_forward_list_get_nth_item(bflist, i);
    Navigation *n = dwb_navigation_from_webkit_history_item(item);
    if (n) 
      store = g_list_append(store, n);
  }
  dwb.state.undo_list = g_list_prepend(dwb.state.undo_list, store);

  /* Destroy widget */
  gtk_widget_destroy(v->scroll);
  gtk_widget_destroy(v->vbox);
  dwb.gui.entry = NULL;
  dwb_grab_focus(dwb.state.fview);
  gtk_container_remove(GTK_CONTAINER(dwb.gui.topbox), v->tabevent);

  /*  clean up */ 
  dwb_source_remove(gl);
  FREE(v->status);
  FREE(v);

  dwb.state.views = g_list_delete_link(dwb.state.views, gl);

  /* Update MAXIMIZED layout */ 
  if (dwb.state.layout & MAXIMIZED) {
    gtk_widget_show(CURRENT_VIEW()->vbox);
    if (dwb.state.fview == dwb.state.views) {
      gtk_widget_hide(dwb.gui.right);
      gtk_widget_show(dwb.gui.left);
    }
    else {
      gtk_widget_show(dwb.gui.right);
      gtk_widget_hide(dwb.gui.left);
    }
  }
  dwb_update_layout();
}/*}}}*/

void 
dwb_view_new_reorder() {
  if (dwb.state.views) {
    View *views = dwb.state.views->data;
    CLEAR_COMMAND_TEXT(dwb.state.views);
    gtk_widget_reparent(views->vbox, dwb.gui.right);
    gtk_box_reorder_child(GTK_BOX(dwb.gui.right), views->vbox, 0);
    if (dwb.state.layout & MAXIMIZED) {
      gtk_widget_hide(((View *)dwb.state.fview->data)->vbox);
      if (dwb.state.fview != dwb.state.views) {
        gtk_widget_hide(dwb.gui.right);
        gtk_widget_show(dwb.gui.left);
      }
    }
  }
}

/* dwb_add_view(Arg *arg)               return: View *{{{*/
GList *  
dwb_add_view(Arg *arg) {
  dwb_view_new_reorder();
  dwb.state.views = dwb_view_create_web_view(dwb.state.views);
  dwb_focus(dwb.state.views);

  dwb_update_layout();
  if (arg && arg->p) {
    dwb_load_uri(arg);
  }
  else if (strcmp("about:blank", dwb.misc.startpage)) {
    Arg a = { .p = dwb.misc.startpage }; 
    dwb_load_uri(&a);
  }
  return dwb.state.views;
} /*}}}*/

GList *
dwb_add_view_new_with_webview(void) {
  dwb_view_new_reorder();
  return NULL;
}

/* dwb_parse_setting(const char *){{{*/
void
dwb_parse_setting(const char *text) {
  WebSettings *s;
  Arg *a = NULL;
  char **token = g_strsplit(text, " ", 2);

  GHashTable *t = dwb.state.setting_apply == APPLY_GLOBAL ? dwb.settings : ((View*)dwb.state.fview->data)->setting;
  if (token[0]) {
    if  ( (s = g_hash_table_lookup(t, token[0])) ) {
      if ( (a = dwb_util_char_to_arg(token[1], s->type)) || (s->type == CHAR && a->p == NULL)) {
        s->arg = *a;
        dwb_apply_settings(s);
        dwb_set_normal_message(dwb.state.fview, true, "Saved setting %s: %s", s->n.first, s->type == BOOLEAN ? ( s->arg.b ? "true" : "false") : token[1]);
        dwb_save_settings();
      }
      else {
        dwb_set_error_message(dwb.state.fview, "No valid value.");
      }
    }
    else {
      dwb_set_error_message(dwb.state.fview, "No such setting: %s", token[0]);
    }
  }
  dwb_normal_mode(false);

  g_strfreev(token);

}/*}}}*/

/* dwb_parse_key_setting(const char  *text) {{{*/
void
dwb_parse_key_setting(const char *text) {
  KeyValue value;
  char **token = g_strsplit(text, " ", 2);

  value.id = g_strdup(token[0]);

  if (token[1]) {
    value.key = dwb_str_to_key(token[1]); 
  }
  else {
    Key key = { NULL, 0 };
    value.key = key;
  }

  dwb_set_normal_message(dwb.state.fview, true, "Saved key for command %s: %s", token[0], token[1] ? token[1] : "");

  dwb.keymap = dwb_keymap_add(dwb.keymap, value);
  dwb.keymap = g_list_sort(dwb.keymap, (GCompareFunc)dwb_util_keymap_sort_second);

  g_strfreev(token);
  dwb_normal_mode(false);
}/*}}}*/

/* dwb_apply_settings(WebSettings *s) {{{*/
static void
dwb_apply_settings(WebSettings *s) {
  WebSettings *new;
  if (dwb.state.setting_apply == APPLY_GLOBAL) {
    new = g_hash_table_lookup(dwb.settings, s->n.first);
    new->arg = s->arg;
    for (GList *l = dwb.state.views; l; l=l->next)  {
      WebSettings *new =  g_hash_table_lookup((((View*)l->data)->setting), s->n.first);
      new->arg = s->arg;
      if (s->func) {
        s->func(l, s);
      }
    }
  }
  else {
    s->func(dwb.state.fview, s);
  }
  dwb_normal_mode(false);

}/*}}}*/

/*}}}*/