keybindings.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
  2. // Copyright © 2019-2020 Badwolf Authors <https://hacktivis.me/projects/badwolf>
  3. // SPDX-License-Identifier: BSD-3-Clause
  4. #include "keybindings.h"
  5. #include "badwolf.h"
  6. #include <glib/gi18n.h> /* _() */
  7. static gboolean
  8. about_dialogCb_activate_link(GtkAboutDialog *about_dialog, gchar *uri, gpointer user_data)
  9. {
  10. (void)about_dialog;
  11. struct Window *window = (struct Window *)user_data;
  12. badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(window, uri, NULL));
  13. gtk_widget_destroy(GTK_WIDGET(about_dialog));
  14. return TRUE;
  15. }
  16. static void
  17. badwolf_about_dialog(GtkWindow *main_window, gpointer user_data)
  18. {
  19. struct Window *window = (struct Window *)user_data;
  20. GtkWidget *about_dialog = gtk_about_dialog_new();
  21. gtk_window_set_transient_for(GTK_WINDOW(about_dialog), main_window);
  22. gtk_window_set_destroy_with_parent(GTK_WINDOW(about_dialog), TRUE);
  23. gtk_about_dialog_set_license(GTK_ABOUT_DIALOG(about_dialog),
  24. "SPDX-License-Identifier: BSD-3-Clause");
  25. gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(about_dialog),
  26. "2019 Haelwenn (lanodan) Monnier <contact+badwolf@hacktivis.me>");
  27. gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about_dialog), homepage);
  28. gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(about_dialog),
  29. _("Minimalist and privacy-oriented WebKitGTK+ browser"));
  30. gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about_dialog), version);
  31. g_signal_connect(about_dialog, "activate-link", G_CALLBACK(about_dialogCb_activate_link), window);
  32. (void)gtk_dialog_run(GTK_DIALOG(about_dialog));
  33. gtk_widget_destroy(about_dialog);
  34. }
  35. static void
  36. toggle_caret_browsing(WebKitWebView *webView)
  37. {
  38. WebKitSettings *settings = webkit_web_view_get_settings(webView);
  39. webkit_settings_set_enable_caret_browsing(settings,
  40. !webkit_settings_get_enable_caret_browsing(settings));
  41. webkit_web_view_set_settings(webView, settings);
  42. }
  43. /* commonCb_key_press_event: Global callback for keybindings
  44. *
  45. * Theses shortcuts should be avoided as much as possible:
  46. * - Single key shortcuts (ie. backspace and space)
  47. * - Triple key shortcuts (except for Ctrl+Shift)
  48. * - Unix Terminal shortcuts (specially Ctrl-W)
  49. *
  50. * loosely follows https://developer.gnome.org/hig/stable/keyboard-input.html
  51. */
  52. gboolean
  53. commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *browser)
  54. {
  55. GtkNotebook *notebook = GTK_NOTEBOOK(window->notebook);
  56. if(((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
  57. {
  58. if(browser != NULL)
  59. {
  60. switch(((GdkEventKey *)event)->keyval)
  61. {
  62. case GDK_KEY_F4: webkit_web_view_try_close(browser->webView); return TRUE;
  63. case GDK_KEY_r:
  64. if(((GdkEventKey *)event)->state & GDK_SHIFT_MASK)
  65. webkit_web_view_reload_bypass_cache(browser->webView);
  66. else
  67. webkit_web_view_reload(browser->webView);
  68. return TRUE;
  69. case GDK_KEY_f: gtk_widget_grab_focus(browser->search); return TRUE;
  70. case GDK_KEY_l: gtk_widget_grab_focus(browser->location); return TRUE;
  71. case GDK_KEY_bracketleft: webkit_web_view_go_back(browser->webView); return TRUE;
  72. case GDK_KEY_bracketright: webkit_web_view_go_forward(browser->webView); return TRUE;
  73. case GDK_KEY_0:
  74. webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(browser->webView), 1);
  75. return TRUE;
  76. }
  77. }
  78. else
  79. {
  80. switch(((GdkEventKey *)event)->keyval)
  81. {
  82. case GDK_KEY_Page_Down: gtk_notebook_next_page(notebook); return TRUE;
  83. case GDK_KEY_Page_Up: gtk_notebook_prev_page(notebook); return TRUE;
  84. case GDK_KEY_t: badwolf_new_tab(notebook, new_browser(window, NULL, NULL)); return TRUE;
  85. }
  86. }
  87. }
  88. if((((GdkEventKey *)event)->state & GDK_MOD1_MASK))
  89. {
  90. if((browser != NULL) && (((GdkEventKey *)event)->keyval == GDK_KEY_d))
  91. {
  92. webkit_web_view_try_close(browser->webView);
  93. return TRUE;
  94. }
  95. switch(((GdkEventKey *)event)->keyval)
  96. {
  97. case GDK_KEY_Left: gtk_notebook_prev_page(notebook); return TRUE;
  98. case GDK_KEY_Right: gtk_notebook_next_page(notebook); return TRUE;
  99. }
  100. if((((GdkEventKey *)event)->keyval >= GDK_KEY_0) &&
  101. (((GdkEventKey *)event)->keyval <= GDK_KEY_9))
  102. gtk_notebook_set_current_page(notebook, (gint)(((GdkEventKey *)event)->keyval - GDK_KEY_1));
  103. }
  104. if(browser != NULL)
  105. {
  106. switch(((GdkEventKey *)event)->keyval)
  107. {
  108. case GDK_KEY_F5: webkit_web_view_reload(browser->webView); return TRUE;
  109. case GDK_KEY_F7: toggle_caret_browsing(browser->webView); return TRUE;
  110. case GDK_KEY_F12:
  111. webkit_web_inspector_show(webkit_web_view_get_inspector(browser->webView));
  112. return TRUE;
  113. }
  114. }
  115. else
  116. {
  117. switch(((GdkEventKey *)event)->keyval)
  118. {
  119. case GDK_KEY_F1: badwolf_about_dialog(GTK_WINDOW(window->main_window), window); return TRUE;
  120. }
  121. }
  122. return FALSE;
  123. }
  124. gboolean
  125. WebViewCb_key_press_event(WebKitWebView *webView, GdkEvent *event, gpointer user_data)
  126. {
  127. (void)webView;
  128. struct Client *browser = (struct Client *)user_data;
  129. if(commonCb_key_press_event(browser->window, event, browser)) return TRUE;
  130. return FALSE;
  131. }
  132. gboolean
  133. boxCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
  134. {
  135. (void)widget;
  136. struct Client *browser = (struct Client *)user_data;
  137. if(commonCb_key_press_event(browser->window, event, browser)) return TRUE;
  138. return FALSE;
  139. }
  140. gboolean
  141. main_windowCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
  142. {
  143. (void)widget;
  144. struct Window *window = (struct Window *)user_data;
  145. if(commonCb_key_press_event(window, event, NULL)) return TRUE;
  146. return FALSE;
  147. }