summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 65adadfa3cc12a2e3ca61b56471fd3eb3acd8461 (plain)
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
/* WWWLite, a lightweight web browser.
   Copyright (C) 2019 defanor

   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, see <https://www.gnu.org/licenses/>.
*/

#include <glib.h>
#include <gtk/gtk.h>
#include "browserbox.h"

gchar **start_uri = NULL;

static GOptionEntry entries[] =
{
  { G_OPTION_REMAINING, 0, G_OPTION_FLAG_NONE,
    G_OPTION_ARG_STRING_ARRAY, &start_uri, "URI", NULL },
  { NULL }
};

static gboolean
key_press_event_cb (GtkWidget *widget, GdkEventKey *ev, GtkStack *tabs)
{
  if (ev->state & GDK_CONTROL_MASK) {
    if (ev->keyval == GDK_KEY_t) {
      GtkWidget *browser_box = GTK_WIDGET(browser_box_new(NULL));
      BROWSER_BOX(browser_box)->tabs = tabs;
      gtk_stack_add_titled(tabs, browser_box, "New tab", "New tab");
      gtk_widget_show_all(browser_box);
      gtk_stack_set_visible_child(tabs, browser_box);
      return TRUE;
    } else if (ev->keyval == GDK_KEY_w) {
      GtkWidget *current_tab = gtk_stack_get_visible_child(tabs);
      if (current_tab) {
        gtk_widget_destroy(current_tab);
      }
      return TRUE;
    }
  }
  GtkWidget *current_tab = gtk_stack_get_visible_child(tabs);
  if (current_tab != NULL) {
    BrowserBox *bb = BROWSER_BOX(current_tab);
    if (ev->keyval == GDK_KEY_Back || ev->keyval == GDK_KEY_BackSpace) {
      return history_back(bb);
    } else if (ev->keyval == GDK_KEY_Forward) {
      return history_forward(bb);
    }
  }
  return FALSE;
}

static gboolean
button_press_event_cb (GtkWidget *widget, GdkEventButton *ev, GtkStack *tabs)
{
  GtkWidget *current_tab = gtk_stack_get_visible_child(tabs);
  if (current_tab != NULL) {
    BrowserBox *bb = BROWSER_BOX(current_tab);
    if (ev->button == 8) {
      return history_back(bb);
    } else if (ev->button == 9) {
      return history_forward(bb);
    }
  }
  return FALSE;
}

static void activate (GtkApplication *app, gpointer user_data)
{
  GtkWidget *window;

  window = gtk_application_window_new (app);
  gtk_window_resize(GTK_WINDOW(window), 800, 800);
  gtk_window_set_title (GTK_WINDOW (window), "WWWLite");

  GtkWidget *evbox = gtk_event_box_new();
  gtk_container_add (GTK_CONTAINER (window), evbox);

  GtkWidget *box = block_box_new(0);
  gtk_container_add (GTK_CONTAINER (evbox), box);

  GtkWidget *switcher = gtk_stack_switcher_new();
  gtk_container_add (GTK_CONTAINER (box), switcher);

  GtkWidget *stack = gtk_stack_new();
  gtk_stack_switcher_set_stack(GTK_STACK_SWITCHER(switcher), GTK_STACK(stack));
  gtk_container_add (GTK_CONTAINER (box), stack);
  gtk_box_set_child_packing(GTK_BOX(box), stack, TRUE, TRUE, 0, GTK_PACK_START);

  GtkWidget *browser_box = GTK_WIDGET(browser_box_new(start_uri != NULL ? start_uri[0] : NULL));
  gtk_stack_add_titled(GTK_STACK(stack), browser_box, "Tab 1", "Tab 1");
  BROWSER_BOX(browser_box)->tabs = GTK_STACK(stack);

  g_signal_connect (evbox, "key-press-event",
                    G_CALLBACK (key_press_event_cb), stack);
  g_signal_connect (evbox, "button-release-event",
                    G_CALLBACK (button_press_event_cb), stack);

  gtk_widget_show_all (window);

  word_cache = g_hash_table_new((GHashFunc)wck_hash, (GEqualFunc)wck_equal);
  return;
}

int
main (int argc, char **argv)
{
  GOptionContext *context = g_option_context_new ("[URI]");
  g_option_context_add_main_entries (context, entries, NULL);
  g_option_context_add_group (context, gtk_get_option_group(TRUE));
  GError *error = NULL;
  GtkApplication *app;
  int status;
  if (! g_option_context_parse (context, &argc, &argv, &error)) {
    g_print("Failed to parse arguments: %s\n", error->message);
    exit(1);
  }

  app = gtk_application_new (NULL, G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}