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
|
/*
* Tab chrome
*
* Copyright (C) 2017 Patrick McDermott
*
* This file is part of Marquee.
*
* Marquee 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.
*
* Marquee 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 Marquee. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>
#include "tab-chrome.h"
static void
back_clicked_cb(GtkButton __attribute__((unused)) *toolbutton,
MqTabChrome *chrome)
{
webkit_web_view_go_back(chrome->web_view);
}
static void
forward_clicked_cb(GtkButton __attribute__((unused)) *toolbutton,
MqTabChrome *chrome)
{
webkit_web_view_go_forward(chrome->web_view);
}
static GtkWidget *
back_forward_list_item_new(WebKitBackForwardListItem *list_item,
gint type)
{
GtkWidget *label;
GtkWidget *icon_stack;
GtkWidget *icon;
GtkWidget *box;
label = gtk_label_new(webkit_back_forward_list_item_get_title(
list_item));
gtk_widget_set_halign(label, GTK_ALIGN_START);
icon_stack = gtk_stack_new();
switch (type) {
case 0:
icon = gtk_radio_button_new(NULL);
gtk_stack_add_named(GTK_STACK(icon_stack), icon,
"current");
gtk_widget_set_can_focus(icon, FALSE);
gtk_stack_add_named(GTK_STACK(icon_stack),
gtk_image_new_from_icon_name("go-previous",
GTK_ICON_SIZE_BUTTON), "back");
break;
case -1:
gtk_stack_add_named(GTK_STACK(icon_stack),
gtk_image_new_from_icon_name("go-previous",
GTK_ICON_SIZE_BUTTON), "back");
gtk_stack_add_named(GTK_STACK(icon_stack),
gtk_radio_button_new(NULL), "current");
break;
case 1:
gtk_stack_add_named(GTK_STACK(icon_stack),
gtk_image_new_from_icon_name("go-next",
GTK_ICON_SIZE_BUTTON), "forward");
gtk_stack_add_named(GTK_STACK(icon_stack),
gtk_radio_button_new(NULL), "current");
break;
}
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_pack_start(GTK_BOX(box), icon_stack, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
gtk_widget_set_tooltip_text(box,
webkit_back_forward_list_item_get_uri(list_item));
return box;
}
static void
back_forward_list_box_row_activated_cb(GtkListBox __attribute__((unused)) *box,
GtkListBoxRow *row, MqTabChrome *chrome)
{
webkit_web_view_go_to_back_forward_list_item(chrome->web_view,
webkit_back_forward_list_get_nth_item(
webkit_web_view_get_back_forward_list(chrome->web_view),
gtk_list_box_row_get_index(row) - chrome->back_items));
gtk_widget_hide(chrome->back_forward_popover);
}
static void
back_forward_toggle_button_toggled_cb(GtkToggleButton *toggle_button,
GtkStack *stack)
{
/* Use gtk_widget_show() and gtk_widget_hide() instead of
* gtk_stack_set_visible_child() so that the stack fits the size of only
* the visible child. */
gtk_widget_show_all(gtk_stack_get_child_by_name(stack,
gtk_toggle_button_get_active(toggle_button) ?
"text" : "list"));
gtk_widget_hide(gtk_stack_get_child_by_name(stack,
gtk_toggle_button_get_active(toggle_button) ?
"list" : "text"));
}
static gboolean
back_forward_box_button_press_cb(GtkWidget *widget, GdkEvent *event,
MqTabChrome *chrome)
{
WebKitBackForwardList *back_forward_list;
GtkWidget *list_box;
GtkWidget *text_view;
GtkTextBuffer *text_buffer;
GList *list_item;
GtkTextIter text_iter;
gchar *str;
GtkTextTag *text_tag;
GtkWidget *list_scrolled_window;
GtkWidget *text_scrolled_window;
GtkWidget *stack;
GtkWidget *toggle_button;
GtkWidget *box;
/* Make sure this is a mouse button press event. Either the middle or
* right button is OK. */
if (event->type != GDK_BUTTON_PRESS) {
return FALSE;
}
/* Get the back/forward list for the Web view. */
back_forward_list = webkit_web_view_get_back_forward_list(
chrome->web_view);
/* Set up the list box. */
list_box = gtk_list_box_new();
gtk_list_box_set_selection_mode(GTK_LIST_BOX(list_box),
GTK_SELECTION_BROWSE);
gtk_list_box_set_activate_on_single_click(GTK_LIST_BOX(list_box), TRUE);
g_signal_connect(list_box, "row-activated",
G_CALLBACK(back_forward_list_box_row_activated_cb), chrome);
/* Set up the text view. */
text_view = gtk_text_view_new();
text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view), FALSE);
/* Insert forward list items. */
/* The forward list is backwards, so we need to prepend each item to our
* list box and text buffer. */
list_item = webkit_back_forward_list_get_forward_list(
back_forward_list);
for (; list_item; list_item = list_item->next) {
gtk_list_box_prepend(GTK_LIST_BOX(list_box),
back_forward_list_item_new(list_item->data, 1));
gtk_text_buffer_get_start_iter(text_buffer, &text_iter);
str = g_strdup_printf("\n%s",
webkit_back_forward_list_item_get_uri(list_item->data));
gtk_text_buffer_insert(text_buffer, &text_iter, str, -1);
g_free(str);
}
/* Insert the current item. */
gtk_list_box_prepend(GTK_LIST_BOX(list_box), back_forward_list_item_new(
webkit_back_forward_list_get_current_item(
back_forward_list), 0));
gtk_list_box_select_row(GTK_LIST_BOX(list_box),
gtk_list_box_get_row_at_index(GTK_LIST_BOX(list_box), 0));
gtk_text_buffer_get_start_iter(text_buffer, &text_iter);
text_tag = gtk_text_buffer_create_tag(text_buffer, NULL, "weight",
PANGO_WEIGHT_BOLD, NULL);
gtk_text_buffer_insert_with_tags(text_buffer, &text_iter,
webkit_back_forward_list_item_get_uri(
webkit_back_forward_list_get_current_item(
back_forward_list)), -1, text_tag, NULL);
/* Insert back list items. */
list_item = webkit_back_forward_list_get_back_list(
back_forward_list);
chrome->back_items = 0;
for (; list_item; list_item = list_item->next) {
gtk_list_box_prepend(GTK_LIST_BOX(list_box),
back_forward_list_item_new(list_item->data, -1));
gtk_text_buffer_get_start_iter(text_buffer, &text_iter);
str = g_strdup_printf("%s\n",
webkit_back_forward_list_item_get_uri(list_item->data));
gtk_text_buffer_insert(text_buffer, &text_iter, str, -1);
g_free(str);
++chrome->back_items;
}
/*
* The following GtkScrolledWindow widgets have hardcoded minimum sizes,
* because there seems to be (in GTK+ versions before 3.22) no way to
* set the natural size of GtkScrolledWindow and its GtkViewport.
*
* I tried:
*
* - Setting size requests of the sizes of child widgets plus
* scrollbars (commit 2205669)
* - Putting GtkScrolledWindow widgets in stacks with non-scrolled
* versions of children, to allocate more area for the
* GtkScrolledWindow widgets to fill (commits 922cdef and 90686fa)
* - Setting policies to disable scrollbars upon GtkScrolledWindow
* instantiation, then enabling scrollbars on the GtkScrolledWindow
* "size-allocate" signal (commits c534c7e and 42ca783)
*
* Attempts to match the size of a GtkScrolledWindow's GtkViewport to
* its child's size don't allow the GtkScrolledWindow and GtkViewport to
* shrink when necessary to fit within the window. Therefore, such
* methods are equivalent to simply not using GtkScrolledWindow at all.
* And there appears to be no easy way to calculate the available size
* (the size of the GtkWindow minus all of the surrounding widgets) to
* manually manage a GtkViewport's size.
*
* GtkScrolledWindow or GtkViewport ignores the child's preferred
* (minimum and natural) sizes and apparently just sets a hardcoded size
* of about 64x64 px. I considered subclassing GtkScrolledWindow or
* GtkViewport, but I'm not sure where the hardcoded size is set.
*
* The functions gtk_scrolled_window_set_propagate_natural_width() and
* gtk_scrolled_window_set_propagate_natural_height() were introduced in
* GTK+ 3.22 and probably do what I want. However, I want to maintain
* compatibility with at least GTK+ 3.12 or 3.14.
*/
/* Set up the list scrolled window. */
list_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_min_content_width(
GTK_SCROLLED_WINDOW(list_scrolled_window), 400);
gtk_scrolled_window_set_min_content_height(
GTK_SCROLLED_WINDOW(list_scrolled_window), 200);
gtk_container_add(GTK_CONTAINER(list_scrolled_window), list_box);
/* Set up the text scrolled window. */
text_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_min_content_width(
GTK_SCROLLED_WINDOW(text_scrolled_window), 400);
gtk_scrolled_window_set_min_content_height(
GTK_SCROLLED_WINDOW(text_scrolled_window), 200);
gtk_container_add(GTK_CONTAINER(text_scrolled_window), text_view);
/* Set up the stack. */
stack = gtk_stack_new();
gtk_stack_add_named(GTK_STACK(stack), list_scrolled_window, "list");
gtk_stack_add_named(GTK_STACK(stack), text_scrolled_window, "text");
/* Set up the toggle button. */
toggle_button = gtk_toggle_button_new();
gtk_button_set_image(GTK_BUTTON(toggle_button),
gtk_image_new_from_icon_name("edit-select-all",
GTK_ICON_SIZE_SMALL_TOOLBAR));
gtk_widget_set_halign(toggle_button, GTK_ALIGN_START);
g_signal_connect(toggle_button, "toggled",
G_CALLBACK(back_forward_toggle_button_toggled_cb),
GTK_STACK(stack));
/* Set up the containing box. */
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start(GTK_BOX(box), toggle_button, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), stack, TRUE, FALSE, 0);
/* Set up the popover. */
chrome->back_forward_popover = gtk_popover_new(widget);
gtk_container_add(GTK_CONTAINER(chrome->back_forward_popover), box);
/* NB: gtk_popover_popup() is new in GTK+ 3.22. */
gtk_widget_show_all(chrome->back_forward_popover);
gtk_widget_hide(text_view);
return FALSE;
}
static void
stop_reload_clicked_cb(GtkToolButton __attribute__((unused)) *toolbutton,
MqTabChrome *chrome)
{
if (webkit_web_view_is_loading(chrome->web_view)) {
webkit_web_view_stop_loading(chrome->web_view);
} else {
webkit_web_view_reload(chrome->web_view);
}
}
static void
uri_activate_cb(GtkEntry *entry, MqTabChrome *chrome)
{
webkit_web_view_load_uri(chrome->web_view,
gtk_entry_get_text(GTK_ENTRY(entry)));
}
#define BUTTON_ROWS 5
#define BUTTON_COLS 3
#define NEW_BUTTON(Y, X, ICON, TOOLTIP) \
do { \
buttons[Y * BUTTON_COLS + X] = gtk_button_new_from_icon_name(\
ICON, GTK_ICON_SIZE_BUTTON); \
gtk_widget_set_tooltip_text(buttons[Y * BUTTON_COLS + X], \
TOOLTIP); \
gtk_grid_attach(GTK_GRID(grid), buttons[Y * BUTTON_COLS + X], \
X, Y, 1, 1); \
} while (0)
static void
menu_button_clicked_cb(GtkToolButton *tool_button,
MqTabChrome __attribute__((unused)) *chrome)
{
GtkWidget *grid;
GtkWidget *buttons[BUTTON_ROWS * BUTTON_COLS];
GtkWidget *popover;
/* Set up the grid. */
grid = gtk_grid_new();
NEW_BUTTON(0, 0, "document-open", "Open file");
NEW_BUTTON(0, 1, "document-save-as", "Save page");
NEW_BUTTON(0, 2, "mail-message-new", "E-mail link");
NEW_BUTTON(1, 0, "edit-find", "Find");
NEW_BUTTON(1, 1, "document-print-preview", "Print preview");
NEW_BUTTON(1, 2, "document-print", "Print");
NEW_BUTTON(2, 0, "bookmark-new", "Bookmarks");
NEW_BUTTON(2, 1, "document-open-recent", "History");
NEW_BUTTON(2, 2, "document-save", "Downloads");
NEW_BUTTON(3, 0, "view-fullscreen", "Full screen");
NEW_BUTTON(3, 1, "document-properties", "Developer tools");
NEW_BUTTON(3, 2, "system-run", "Preferences");
NEW_BUTTON(4, 0, "help-about", "About Marquee");
NEW_BUTTON(4, 2, "application-exit", "Quit");
/* Set up the popover. */
popover = gtk_popover_new(GTK_WIDGET(tool_button));
gtk_container_add(GTK_CONTAINER(popover), grid);
/* NB: gtk_popover_popup() is new in GTK+ 3.22. */
gtk_widget_show_all(popover);
}
#undef BUTTON_ROWS
#undef BUTTON_COLS
#undef NEW_BUTTON
static GtkWidget *
navigation_toolbar_new(MqTabChrome *chrome, const gchar *uri)
{
GtkToolbar *navigation_toolbar;
GtkToolItem *back_forward_tool_item;
GtkWidget *back_forward_event_box;
GtkToolItem *uri_tool_item;
navigation_toolbar = GTK_TOOLBAR(gtk_toolbar_new());
back_forward_tool_item = gtk_tool_item_new();
chrome->back_forward_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
/* Back button */
chrome->back_button = gtk_button_new_from_icon_name("go-previous",
GTK_ICON_SIZE_SMALL_TOOLBAR);
gtk_widget_set_tooltip_text(GTK_WIDGET(chrome->back_button),
"Go back one page");
g_signal_connect(chrome->back_button, "clicked",
G_CALLBACK(back_clicked_cb), chrome);
gtk_box_pack_start(GTK_BOX(chrome->back_forward_box),
chrome->back_button, FALSE, FALSE, 0);
/* Forward button */
chrome->forward_button = gtk_button_new_from_icon_name("go-next",
GTK_ICON_SIZE_SMALL_TOOLBAR);
gtk_widget_set_tooltip_text(GTK_WIDGET(chrome->forward_button),
"Go forward one page");
g_signal_connect(chrome->forward_button, "clicked",
G_CALLBACK(forward_clicked_cb), chrome);
gtk_box_pack_start(GTK_BOX(chrome->back_forward_box),
chrome->forward_button, FALSE, FALSE, 0);
gtk_style_context_add_class(
gtk_widget_get_style_context(chrome->back_forward_box),
"linked");
back_forward_event_box = gtk_event_box_new();
g_signal_connect(back_forward_event_box, "button-press-event",
G_CALLBACK(back_forward_box_button_press_cb), chrome);
gtk_container_add(GTK_CONTAINER(back_forward_event_box),
chrome->back_forward_box);
gtk_container_add(GTK_CONTAINER(back_forward_tool_item),
back_forward_event_box);
gtk_toolbar_insert(navigation_toolbar, back_forward_tool_item, -1);
/* Stop/reload button */
chrome->stop_icon = gtk_image_new_from_icon_name("process-stop",
GTK_ICON_SIZE_SMALL_TOOLBAR);
g_object_ref_sink(chrome->stop_icon);
chrome->reload_icon = gtk_image_new_from_icon_name("view-refresh",
GTK_ICON_SIZE_SMALL_TOOLBAR);
g_object_ref_sink(chrome->reload_icon);
chrome->stop_reload_button = gtk_tool_button_new(chrome->stop_icon,
"Stop");
gtk_widget_set_tooltip_text(GTK_WIDGET(chrome->stop_reload_button),
"Stop loading the current page");
g_signal_connect(chrome->stop_reload_button, "clicked",
G_CALLBACK(stop_reload_clicked_cb), chrome);
gtk_toolbar_insert(navigation_toolbar, chrome->stop_reload_button, -1);
/* URI bar */
uri_tool_item = gtk_tool_item_new();
chrome->uri_entry = gtk_entry_new();
if (uri) {
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), uri);
}
gtk_entry_set_placeholder_text(GTK_ENTRY(chrome->uri_entry),
"URI...");
gtk_entry_set_icon_from_icon_name(GTK_ENTRY(chrome->uri_entry),
GTK_ENTRY_ICON_PRIMARY, "text-x-generic");
gtk_entry_set_progress_fraction(GTK_ENTRY(chrome->uri_entry), 0.0);
g_signal_connect(chrome->uri_entry, "activate",
G_CALLBACK(uri_activate_cb), chrome);
gtk_container_add(GTK_CONTAINER(uri_tool_item),
chrome->uri_entry);
gtk_tool_item_set_expand(uri_tool_item, TRUE);
gtk_toolbar_insert(navigation_toolbar, uri_tool_item, -1);
/* URI bar hovered link style */
chrome->hovered_link_style = pango_attr_list_new();
pango_attr_list_insert(chrome->hovered_link_style,
pango_attr_style_new(PANGO_STYLE_ITALIC));
/* Menu button */
chrome->menu_button = gtk_tool_button_new(
gtk_image_new_from_icon_name("open-menu-symbolic",
GTK_ICON_SIZE_SMALL_TOOLBAR), "Menu");
gtk_widget_set_tooltip_text(GTK_WIDGET(chrome->menu_button),
"Open menu");
g_signal_connect(chrome->menu_button, "clicked",
G_CALLBACK(menu_button_clicked_cb), chrome);
gtk_toolbar_insert(navigation_toolbar, chrome->menu_button, -1);
gtk_widget_set_hexpand(GTK_WIDGET(navigation_toolbar), TRUE);
chrome->load_failed = FALSE;
return GTK_WIDGET(navigation_toolbar);
}
MqTabChrome *
mq_tab_chrome_new(const gchar *uri)
{
MqTabChrome *chrome;
chrome = malloc(sizeof(*chrome));
chrome->container = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start(GTK_BOX(chrome->container),
navigation_toolbar_new(chrome, uri), FALSE, FALSE, 0);
return chrome;
}
GtkWidget *
mq_tab_chrome_get_container(MqTabChrome *chrome)
{
return chrome->container;
}
static void
load_changed_cb(WebKitWebView *web_view, WebKitLoadEvent load_event,
MqTabChrome *chrome)
{
switch (load_event) {
case WEBKIT_LOAD_STARTED:
case WEBKIT_LOAD_REDIRECTED:
case WEBKIT_LOAD_COMMITTED:
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry),
webkit_web_view_get_uri(web_view));
gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry),
NULL);
break;
case WEBKIT_LOAD_FINISHED:
gtk_entry_set_progress_fraction(
GTK_ENTRY(chrome->uri_entry), 0.0);
break;
}
gtk_widget_set_sensitive(GTK_WIDGET(chrome->back_button),
webkit_web_view_can_go_back(web_view));
gtk_widget_set_sensitive(GTK_WIDGET(chrome->forward_button),
webkit_web_view_can_go_forward(web_view));
}
static gboolean
load_failed_cb(WebKitWebView __attribute__((unused)) *web_view,
WebKitLoadEvent __attribute__((unused)) load_event,
gchar __attribute__((unused)) *failing_uri,
GError __attribute__((unused)) *error, MqTabChrome *chrome)
{
chrome->load_failed = TRUE;
return FALSE;
}
/* TODO: <Esc> key in URI bar should reset to URI of current hovered link, if
* any, even if different from chrome->hovered_link_uri. */
static void
mouse_target_changed_cb(WebKitWebView __attribute__((unused)) *web_view,
WebKitHitTestResult *hit_test_result,
guint __attribute__((unused)) modifiers,
MqTabChrome *chrome)
{
if (webkit_hit_test_result_context_is_link(hit_test_result)) {
if (chrome->hovered_link_uri && g_strcmp0(gtk_entry_get_text(
GTK_ENTRY(chrome->uri_entry)),
chrome->hovered_link_uri) != 0) {
/* The user has edited the hovered link URI in the URI
* bar. */
} else if (g_strcmp0(gtk_entry_get_text(
GTK_ENTRY(chrome->uri_entry)),
webkit_web_view_get_uri(web_view)) == 0) {
g_free(chrome->hovered_link_uri);
chrome->hovered_link_uri = g_strdup(
webkit_hit_test_result_get_link_uri(
hit_test_result));
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry),
chrome->hovered_link_uri);
gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry),
chrome->hovered_link_style);
} else if (gtk_entry_get_attributes(
GTK_ENTRY(chrome->uri_entry)) ==
chrome->hovered_link_style) {
/* The URI bar's text differs from the Web view's URI
* because the mouse was already targeting a different
* link. */
g_free(chrome->hovered_link_uri);
chrome->hovered_link_uri = g_strdup(
webkit_hit_test_result_get_link_uri(
hit_test_result));
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry),
chrome->hovered_link_uri);
}
} else if (gtk_entry_get_attributes(GTK_ENTRY(chrome->uri_entry)) ==
chrome->hovered_link_style) {
if (g_strcmp0(gtk_entry_get_text(GTK_ENTRY(chrome->uri_entry)),
chrome->hovered_link_uri) == 0) {
/* The user hasn't edited the hovered link URI in the
* URI bar. */
g_free(chrome->hovered_link_uri);
chrome->hovered_link_uri = NULL;
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry),
webkit_web_view_get_uri(web_view));
gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry),
NULL);
}
}
}
static void
load_progress_cb(WebKitWebView *web_view,
GParamSpec __attribute__((unused)) *paramspec, MqTabChrome *chrome)
{
/*
* If loading fails, the WebKitWebView's "estimated-load-progress" is
* set to 1.0 after signals like "load-changed" and "load-failed" are
* emitted. So the only way to avoid leaving behind a full progress bar
* after, for example, canceling a page load is to save a flag on a
* failed load and only update the progress bar if the flag is unset.
*/
if (chrome->load_failed) {
chrome->load_failed = FALSE;
return;
}
gtk_entry_set_progress_fraction(GTK_ENTRY(chrome->uri_entry),
webkit_web_view_get_estimated_load_progress(web_view));
}
static void
uri_cb(WebKitWebView *web_view, GParamSpec __attribute__((unused)) *paramspec,
MqTabChrome *chrome)
{
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry),
webkit_web_view_get_uri(web_view));
gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry), NULL);
}
static void
loading_cb(WebKitWebView *web_view,
GParamSpec __attribute__((unused)) *paramspec, MqTabChrome *chrome)
{
if (webkit_web_view_is_loading(web_view)) {
gtk_tool_button_set_icon_widget(
GTK_TOOL_BUTTON(chrome->stop_reload_button),
chrome->stop_icon);
gtk_tool_button_set_label(
GTK_TOOL_BUTTON(chrome->stop_reload_button), "Stop");
gtk_widget_set_tooltip_text(
GTK_WIDGET(chrome->stop_reload_button),
"Stop loading the current page");
} else {
gtk_tool_button_set_icon_widget(
GTK_TOOL_BUTTON(chrome->stop_reload_button),
chrome->reload_icon);
gtk_tool_button_set_label(
GTK_TOOL_BUTTON(chrome->stop_reload_button), "Reload");
gtk_widget_set_tooltip_text(
GTK_WIDGET(chrome->stop_reload_button),
"Reload the current page");
}
gtk_widget_show_all(GTK_WIDGET(chrome->stop_reload_button));
}
static void
connect_web_view(MqTabChrome *chrome)
{
chrome->hovered_link_uri = NULL;
g_signal_connect(chrome->web_view, "load-changed",
G_CALLBACK(load_changed_cb), chrome);
g_signal_connect(chrome->web_view, "load-failed",
G_CALLBACK(load_failed_cb), chrome);
g_signal_connect(chrome->web_view, "mouse-target-changed",
G_CALLBACK(mouse_target_changed_cb), chrome);
g_signal_connect(chrome->web_view, "notify::estimated-load-progress",
G_CALLBACK(load_progress_cb), chrome);
g_signal_connect(chrome->web_view, "notify::uri",
G_CALLBACK(uri_cb), chrome);
g_signal_connect(chrome->web_view, "notify::is-loading",
G_CALLBACK(loading_cb), chrome);
}
void
mq_tab_chrome_set_web_view(MqTabChrome *chrome, WebKitWebView *web_view)
{
chrome->web_view = web_view;
connect_web_view(chrome);
}
|