keybindings.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
  2. // SPDX-FileCopyrightText: 2019-2023 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 UNUSED(user_data))
  9. {
  10. badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(uri, NULL), TRUE);
  11. gtk_window_close(GTK_WINDOW(about_dialog));
  12. return TRUE;
  13. }
  14. static void
  15. badwolf_about_dialog(GtkWindow *main_window, gpointer UNUSED(user_data))
  16. {
  17. GtkWidget *about_dialog = gtk_about_dialog_new();
  18. char *comments = NULL;
  19. comments = g_strdup_printf(_("Minimalist and privacy-oriented web browser based on WebKitGTK\n"
  20. "Runtime WebKit version: %d.%d.%d"),
  21. webkit_get_major_version(),
  22. webkit_get_minor_version(),
  23. webkit_get_micro_version());
  24. gtk_window_set_transient_for(GTK_WINDOW(about_dialog), main_window);
  25. gtk_window_set_destroy_with_parent(GTK_WINDOW(about_dialog), TRUE);
  26. gtk_about_dialog_set_license(
  27. GTK_ABOUT_DIALOG(about_dialog),
  28. "SPDX License Identifiers: BSD-3-Clause AND CC-BY-SA-4.0 AND MPL-2.0");
  29. gtk_about_dialog_set_copyright(
  30. GTK_ABOUT_DIALOG(about_dialog),
  31. "2019-2021 Badwolf Authors <https://hacktivis.me/projects/badwolf>");
  32. gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about_dialog), homepage);
  33. gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(about_dialog), comments);
  34. gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about_dialog), version);
  35. gtk_about_dialog_set_logo_icon_name(GTK_ABOUT_DIALOG(about_dialog), "badwolf");
  36. g_signal_connect(about_dialog, "activate-link", G_CALLBACK(about_dialogCb_activate_link), window);
  37. gtk_dialog_run(GTK_DIALOG(about_dialog));
  38. gtk_widget_destroy(about_dialog);
  39. }
  40. static void
  41. toggle_caret_browsing(WebKitWebView *webView)
  42. {
  43. WebKitSettings *settings = webkit_web_view_get_settings(webView);
  44. webkit_settings_set_enable_caret_browsing(settings,
  45. !webkit_settings_get_enable_caret_browsing(settings));
  46. webkit_web_view_set_settings(webView, settings);
  47. }
  48. /* commonCb_key_press_event: Global callback for keybindings
  49. *
  50. * Theses shortcuts should be avoided as much as possible:
  51. * - Single key shortcuts (ie. backspace and space)
  52. * - Triple key shortcuts (except for Ctrl+Shift)
  53. * - Unix Terminal shortcuts (specially Ctrl-W)
  54. *
  55. * loosely follows https://developer.gnome.org/hig/guidelines/keyboard.html
  56. */
  57. gboolean
  58. commonCb_key_press_event(GdkEvent *event, struct Client *browser)
  59. {
  60. GtkNotebook *notebook = GTK_NOTEBOOK(window->notebook);
  61. if(((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
  62. {
  63. if(browser != NULL)
  64. {
  65. switch(((GdkEventKey *)event)->keyval)
  66. {
  67. case GDK_KEY_Escape:
  68. webkit_web_view_terminate_web_process(browser->webView);
  69. return TRUE;
  70. case GDK_KEY_F4:
  71. webkit_web_view_try_close(browser->webView);
  72. return TRUE;
  73. case GDK_KEY_r:
  74. if(((GdkEventKey *)event)->state & GDK_SHIFT_MASK)
  75. webkit_web_view_reload_bypass_cache(browser->webView);
  76. else
  77. webkit_web_view_reload(browser->webView);
  78. return TRUE;
  79. case GDK_KEY_f:
  80. gtk_widget_grab_focus(browser->search);
  81. return TRUE;
  82. case GDK_KEY_l:
  83. gtk_widget_grab_focus(browser->location);
  84. return TRUE;
  85. case GDK_KEY_bracketleft:
  86. webkit_web_view_go_back(browser->webView);
  87. return TRUE;
  88. case GDK_KEY_bracketright:
  89. webkit_web_view_go_forward(browser->webView);
  90. return TRUE;
  91. case GDK_KEY_0:
  92. webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(browser->webView), 1);
  93. return TRUE;
  94. case GDK_KEY_p:
  95. webkit_print_operation_run_dialog(webkit_print_operation_new(browser->webView),
  96. GTK_WINDOW(window->main_window));
  97. return TRUE;
  98. }
  99. }
  100. else
  101. {
  102. switch(((GdkEventKey *)event)->keyval)
  103. {
  104. case GDK_KEY_Page_Down:
  105. gtk_notebook_next_page(notebook);
  106. return TRUE;
  107. case GDK_KEY_Page_Up:
  108. gtk_notebook_prev_page(notebook);
  109. return TRUE;
  110. case GDK_KEY_t:
  111. badwolf_new_tab(notebook, new_browser(NULL, NULL), TRUE);
  112. return TRUE;
  113. }
  114. }
  115. }
  116. if((((GdkEventKey *)event)->state & GDK_MOD1_MASK))
  117. {
  118. if((browser != NULL) && (((GdkEventKey *)event)->keyval == GDK_KEY_d))
  119. {
  120. webkit_web_view_try_close(browser->webView);
  121. return TRUE;
  122. }
  123. switch(((GdkEventKey *)event)->keyval)
  124. {
  125. case GDK_KEY_Left:
  126. gtk_notebook_prev_page(notebook);
  127. return TRUE;
  128. case GDK_KEY_Right:
  129. gtk_notebook_next_page(notebook);
  130. return TRUE;
  131. }
  132. if((((GdkEventKey *)event)->keyval >= GDK_KEY_0) &&
  133. (((GdkEventKey *)event)->keyval <= GDK_KEY_9))
  134. gtk_notebook_set_current_page(notebook, (gint)(((GdkEventKey *)event)->keyval - GDK_KEY_1));
  135. }
  136. if(browser != NULL)
  137. {
  138. switch(((GdkEventKey *)event)->keyval)
  139. {
  140. case GDK_KEY_F5:
  141. webkit_web_view_reload(browser->webView);
  142. return TRUE;
  143. case GDK_KEY_Escape:
  144. webkit_web_view_stop_loading(browser->webView);
  145. return TRUE;
  146. case GDK_KEY_F7:
  147. toggle_caret_browsing(browser->webView);
  148. return TRUE;
  149. case GDK_KEY_F12:
  150. webkit_web_inspector_show(webkit_web_view_get_inspector(browser->webView));
  151. return TRUE;
  152. }
  153. }
  154. else
  155. {
  156. switch(((GdkEventKey *)event)->keyval)
  157. {
  158. case GDK_KEY_F1:
  159. badwolf_about_dialog(GTK_WINDOW(window->main_window), window);
  160. return TRUE;
  161. }
  162. }
  163. return FALSE;
  164. }
  165. gboolean
  166. WebViewCb_key_press_event(WebKitWebView *UNUSED(webView), GdkEvent *event, gpointer user_data)
  167. {
  168. struct Client *browser = (struct Client *)user_data;
  169. if(commonCb_key_press_event(event, browser)) return TRUE;
  170. return FALSE;
  171. }
  172. gboolean
  173. boxCb_key_press_event(GtkWidget *UNUSED(widget), GdkEvent *event, gpointer user_data)
  174. {
  175. struct Client *browser = (struct Client *)user_data;
  176. if(commonCb_key_press_event(event, browser)) return TRUE;
  177. return FALSE;
  178. }
  179. gboolean
  180. main_windowCb_key_press_event(GtkWidget *UNUSED(widget),
  181. GdkEvent *event,
  182. gpointer UNUSED(user_data))
  183. {
  184. if(commonCb_key_press_event(event, NULL)) return TRUE;
  185. return FALSE;
  186. }
  187. gboolean
  188. tab_boxCb_button_release_event(GtkWidget *UNUSED(widget), GdkEvent *event, gpointer user_data)
  189. {
  190. struct Client *browser = (struct Client *)user_data;
  191. if(((GdkEventButton *)event)->button == GDK_BUTTON_MIDDLE)
  192. {
  193. webkit_web_view_try_close(browser->webView);
  194. return TRUE;
  195. }
  196. return FALSE;
  197. }