@ -13,22 +13,22 @@ static struct {
GtkHeaderBar * widget ;
GtkEntry * line ;
GtkEntryBuffer * line_text ;
enum { _SEARCH ,
_FIND ,
_HIDDEN } entry_mode ;
enum { _SEARCH , _FIND , _HIDDEN } entry_mode ;
} bar ;
static int num_tabs = 0 ;
// Forward declarations
void toggle_bar ( GtkNotebook * notebook ) ;
void notebook_create_new_tab ( GtkNotebook * notebook , const char * uri ) ;
/* Utils */
WebKitWebView * notebook_get_webview ( GtkNotebook * notebook )
{
return WEBKIT_WEB_VIEW ( gtk_notebook_get_nth_page (
notebook , gtk_notebook_get_current_page ( notebook ) ) ) ;
return WEBKIT_WEB_VIEW ( gtk_notebook_get_nth_page ( notebook , gtk_notebook_get_current_page ( notebook ) ) ) ;
}
/* Load content*/
void toggle_bar ( GtkNotebook * notebook ) ;
void load_uri ( WebKitWebView * view , const char * uri )
{
if ( strlen ( uri ) = = 0 ) {
@ -41,7 +41,6 @@ void load_uri(WebKitWebView* view, const char* uri)
char tmp [ strlen ( " https:// " ) + strlen ( uri ) ] ;
snprintf ( tmp , sizeof ( tmp ) , " https://%s " , uri ) ;
webkit_web_view_load_uri ( view , tmp ) ;
} else {
// Check for shortcuts
int l = SHORTCUT_N + strlen ( uri ) + 1 ;
@ -68,10 +67,7 @@ void redirect_if_annoying(WebKitWebView* view, const char* uri)
str_init ( uri_filtered , l ) ;
int check = libre_redirect ( uri , uri_filtered ) ;
if ( check = = 2 ) {
webkit_web_view_load_uri ( view , uri_filtered ) ;
}
if ( check = = 2 ) webkit_web_view_load_uri ( view , uri_filtered ) ;
}
}
void set_custom_style ( WebKitWebView * view )
@ -87,19 +83,13 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
GtkNotebook * notebook )
{
switch ( load_event ) {
/* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html>
*/
// see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html>
case WEBKIT_LOAD_STARTED :
case WEBKIT_LOAD_COMMITTED :
set_custom_style ( self ) ;
redirect_if_annoying ( self , webkit_web_view_get_uri ( self ) ) ;
break ;
case WEBKIT_LOAD_REDIRECTED :
redirect_if_annoying ( self , webkit_web_view_get_uri ( self ) ) ;
break ;
case WEBKIT_LOAD_COMMITTED :
redirect_if_annoying ( self , webkit_web_view_get_uri ( self ) ) ;
set_custom_style ( self ) ;
break ;
case WEBKIT_LOAD_FINISHED : {
/* Add gtk tab title */
const char * webpage_title = webkit_web_view_get_title ( self ) ;
@ -114,7 +104,6 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
}
tab_title [ max_length ] = ' \0 ' ;
}
gtk_notebook_set_tab_label_text ( notebook , GTK_WIDGET ( self ) ,
webpage_title = = NULL ? " — " : tab_title ) ;
// gtk_widget_hide(GTK_WIDGET(bar));
@ -123,10 +112,6 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
}
/* Create new tabs */
void notebook_create_new_tab ( GtkNotebook * notebook , const char * uri ) ;
// handle_signal_create_new_tab is bound to a signal inside notebook_create_new_tab
// and itself calls notebook_create_new_tab
// therefore we need to do a forward declaration for one of them.
GtkWidget * handle_signal_create_new_tab ( WebKitWebView * self ,
WebKitNavigationAction * navigation_action ,
GtkNotebook * notebook )
@ -137,11 +122,10 @@ GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
printf ( " Creating new window: %s \n " , uri ) ;
notebook_create_new_tab ( notebook , uri ) ;
gtk_notebook_set_show_tabs ( notebook , true ) ;
return NULL ;
} else {
webkit_web_view_evaluate_javascript ( self , " alert('Too many tabs, not opening a new one') " , - 1 , NULL , " rosenrot-alert-numtabs " , NULL , NULL , NULL ) ;
return NULL ;
}
return NULL ;
/*
WebKitGTK documentation recommends returning the new webview .
I imagine that this might allow e . g . , to go back in a new tab
@ -166,30 +150,24 @@ WebKitWebView* create_new_webview()
" like Gecko) Chrome/110.0.0.0 Safari/537.36 " ) ;
// See: <https://www.useragents.me/> for some common user agents
}
web_context = webkit_web_context_new_with_website_data_manager (
webkit_website_data_manager_new ( DATA_MANAGER_OPTS , NULL ) ) ;
web_context = webkit_web_context_new_with_website_data_manager ( webkit_website_data_manager_new ( DATA_MANAGER_OPTS , NULL ) ) ;
contentmanager = webkit_user_content_manager_new ( ) ;
cookiemanager = webkit_web_context_get_cookie_manager ( web_context ) ;
webkit_cookie_manager_set_persistent_storage (
cookiemanager , DATA_DIR " /cookies.sqlite " ,
WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE ) ;
webkit_cookie_manager_set_persistent_storage ( cookiemanager , DATA_DIR " /cookies.sqlite " , WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE ) ;
webkit_cookie_manager_set_accept_policy ( cookiemanager ,
WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS ) ;
webkit_cookie_manager_set_accept_policy ( cookiemanager , WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS ) ;
if ( g_file_get_contents ( " ~/.config/rose/style.css " , & style , NULL , NULL ) )
if ( g_file_get_contents ( " ~/.config/rose/style.css " , & style , NULL , NULL ) ) {
webkit_user_content_manager_add_style_sheet (
contentmanager , webkit_user_style_sheet_new ( style , WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES , WEBKIT_USER_STYLE_LEVEL_USER , NULL , NULL ) ) ;
}
return g_object_new ( WEBKIT_TYPE_WEB_VIEW , " settings " , settings , " web-context " ,
web_context , " user-content-manager " , contentmanager ,
NULL ) ;
return g_object_new ( WEBKIT_TYPE_WEB_VIEW , " settings " , settings , " web-context " , web_context , " user-content-manager " , contentmanager , NULL ) ;
}
void notebook_create_new_tab ( GtkNotebook * notebook , const char * uri )
{
if ( num_tabs < MAX_NUM_TABS | | MAX_NUM_TABS = = 0 ) {
WebKitWebView * view = create_new_webview ( ) ;
g_signal_connect ( view , " load_changed " , G_CALLBACK ( handle_signal_load_changed ) , notebook ) ;
@ -208,8 +186,8 @@ void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
webkit_web_view_set_zoom_level ( view , ZOOM ) ;
num_tabs + = 1 ;
} else {
webkit_web_view_evaluate_javascript ( notebook_get_webview ( notebook ) , " alert('Too many tabs, not opening a new one')" , - 1 , NULL , " rosenrot-alert-numtabs " , NULL , NULL , NULL ) ;
webkit_web_view_evaluate_javascript ( notebook_get_webview ( notebook ) , " alert('Too many tabs, not opening a new one') " ,
- 1 , NULL , " rosenrot-alert-numtabs " , NULL , NULL , NULL ) ;
}
}
@ -237,19 +215,15 @@ void toggle_bar(GtkNotebook* notebook)
gtk_window_set_focus ( window , GTK_WIDGET ( bar . line ) ) ;
break ;
}
default :
// fallthrough
case _HIDDEN :
gtk_widget_hide ( GTK_WIDGET ( bar . widget ) ) ;
break ; // not needed now, but might reorder
}
}
// Handle what happens when the user is on the bar and presses enter
void handle_signal_bar_press_enter ( GtkEntry * self , GtkNotebook * notebook )
{
if ( bar . entry_mode = = _SEARCH )
load_uri ( notebook_get_webview ( notebook ) ,
gtk_entry_buffer_get_text ( bar . line_text ) ) ;
load_uri ( notebook_get_webview ( notebook ) , gtk_entry_buffer_get_text ( bar . line_text ) ) ;
else if ( bar . entry_mode = = _FIND )
webkit_find_controller_search (
webkit_web_view_get_find_controller ( notebook_get_webview ( notebook ) ) ,
@ -292,12 +266,10 @@ int handle_shortcut(func id, GtkNotebook* notebook)
webkit_web_view_set_zoom_level ( view ,
( zoom + = ZOOM_VAL ) ) ;
break ;
case zoomout :
webkit_web_view_set_zoom_level ( view ,
( zoom - = ZOOM_VAL ) ) ;
break ;
case zoom_reset :
webkit_web_view_set_zoom_level ( view ,
( zoom = ZOOM ) ) ;
@ -310,14 +282,12 @@ int handle_shortcut(func id, GtkNotebook* notebook)
int l = ( n + k - 1 ) % n ;
gtk_notebook_set_current_page ( notebook , l ) ;
break ;
case next_tab : ;
int m = gtk_notebook_get_n_pages ( notebook ) ;
int i = gtk_notebook_get_current_page ( notebook ) ;
int j = ( i + 1 ) % m ;
gtk_notebook_set_current_page ( notebook , j ) ;
break ;
case close_tab :
gtk_notebook_remove_page ( notebook , gtk_notebook_get_current_page ( notebook ) ) ;
num_tabs - = 1 ;
@ -338,7 +308,6 @@ int handle_shortcut(func id, GtkNotebook* notebook)
gtk_window_unfullscreen ( window ) ;
else
gtk_window_fullscreen ( window ) ;
is_fullscreen = ! is_fullscreen ;
break ;
@ -346,20 +315,16 @@ int handle_shortcut(func id, GtkNotebook* notebook)
bar . entry_mode = _SEARCH ;
toggle_bar ( notebook ) ;
break ;
case show_finder :
bar . entry_mode = _FIND ;
toggle_bar ( notebook ) ;
break ;
case finder_next :
webkit_find_controller_search_next (
webkit_web_view_get_find_controller ( view ) ) ;
webkit_find_controller_search_next ( webkit_web_view_get_find_controller ( view ) ) ;
break ;
case finder_prev :
webkit_find_controller_search_previous (
webkit_web_view_get_find_controller ( view ) ) ;
webkit_find_controller_search_previous ( webkit_web_view_get_find_controller ( view ) ) ;
break ;
case new_tab :
@ -462,9 +427,7 @@ int main(int argc, char** argv)
/* Show to user */
gtk_widget_show_all ( GTK_WIDGET ( window ) ) ;
if ( argc ! = 0 ) {
gtk_widget_hide ( GTK_WIDGET ( bar . widget ) ) ;
}
if ( argc ! = 0 ) gtk_widget_hide ( GTK_WIDGET ( bar . widget ) ) ;
/* Deal with more tabs */
if ( argc > 2 ) {