summaryrefslogtreecommitdiffstats
path: root/src/window.c
blob: 0b1b634e0f99ec6f099f978336b96064160aaf47 (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
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
/*
 * Main window
 *
 * 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 "window.h"
#include "tab.h"

static void
tab_list_button_clicked_cb(GtkButton __attribute__((unused)) *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(gtk_stack_get_child_by_name(stack, "tab_list"));
	gtk_widget_hide(gtk_stack_get_child_by_name(stack, "buttons"));
}

#define NEW_BUTTON(BOX, I, NAME, TOOLTIP) \
	do { \
		buttons[I] = gtk_button_new_from_icon_name(NAME, \
			GTK_ICON_SIZE_BUTTON); \
		gtk_widget_set_tooltip_text(buttons[I], TOOLTIP); \
		gtk_widget_set_can_focus(buttons[I], FALSE); \
		gtk_box_pack_start(GTK_BOX(BOX ? all_tabs_button_box : \
					current_tab_button_box), buttons[I], \
			TRUE, FALSE, 0); \
	} while (0)

static gboolean
tab_label_button_press_cb(GtkWidget *widget, GdkEvent *event, MqTab *tab)
{
	GtkWidget *current_tab_button_box;
	GtkWidget *all_tabs_button_box;
	GtkWidget *buttons[7];
	GtkWidget *button_rows_box;
	GtkWidget *stack;
	GtkWidget *popover;  /* TODO: Add to MqWindow */

	/* Make sure this is a right mouse button press event. */
	if (event->type != GDK_BUTTON_PRESS || event->button.button != 3) {
		return FALSE;
	}

	/* Set up button boxes. */
	current_tab_button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
	all_tabs_button_box    = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
	gtk_widget_set_halign(current_tab_button_box, GTK_ALIGN_CENTER);
	gtk_widget_set_halign(all_tabs_button_box,    GTK_ALIGN_CENTER);

	/* Set up buttons. */
	NEW_BUTTON(0, 0, "view-refresh",       "Reload Tab");
	NEW_BUTTON(0, 1, "edit-copy",          "Duplicate Tab");
	NEW_BUTTON(0, 2, "window-new",         "Open Tab in New Window");
	NEW_BUTTON(0, 3, "window-close",       "Close Tab");
	NEW_BUTTON(1, 4, "tab-new-symbolic",   "New Tab");
	NEW_BUTTON(1, 5, "edit-undo",          "Undo Close Tab");
	NEW_BUTTON(1, 6, "view-list-symbolic", "Tab List...");

	/* Set up the button rows box. */
	button_rows_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
	gtk_box_pack_start(GTK_BOX(button_rows_box), current_tab_button_box,
		TRUE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(button_rows_box), all_tabs_button_box,
		TRUE, FALSE, 0);

	/* Set up the stack. */
	stack = gtk_stack_new();
	gtk_stack_add_named(GTK_STACK(stack), button_rows_box, "buttons");
	gtk_stack_add_named(GTK_STACK(stack), gtk_label_new("Tab list"),
		"tab_list");

	g_signal_connect(buttons[6], "clicked",
		G_CALLBACK(tab_list_button_clicked_cb), GTK_STACK(stack));

	/* Set up the popover. */
	popover = gtk_popover_new(widget);
	gtk_container_add(GTK_CONTAINER(popover), stack);

	/* NB: gtk_popover_popup() is new in GTK+ 3.22. */
	gtk_widget_show_all(popover);
	/* TODO: gtk_widget_hide() tab list */

	return FALSE;
}

#undef NEW_BUTTON

static void
add_tab(MqWindow *window, gchar *uri, gint position)
{
	GtkWidget *tab_label_image;
	GtkWidget *tab_label_label;
	GtkWidget *tab_label;
	MqTab     *tab;
	GtkWidget *tab_label_event_box;
	GtkWidget *tab_widget;

	tab_label_image = gtk_image_new_from_icon_name("text-x-generic",
		GTK_ICON_SIZE_BUTTON);

	tab_label_label = gtk_label_new("New tab");
	gtk_label_set_ellipsize(GTK_LABEL(tab_label_label),
		PANGO_ELLIPSIZE_END);
	gtk_widget_set_hexpand(tab_label_label, TRUE);
	gtk_widget_set_size_request(tab_label_label, 50, 1);

	tab_label = gtk_grid_new();
	gtk_grid_attach(GTK_GRID(tab_label), tab_label_image,
		0, 0, 1, 1);
	gtk_grid_attach(GTK_GRID(tab_label), tab_label_label,
		1, 0, 1, 1);
	gtk_grid_attach(GTK_GRID(tab_label), gtk_button_new_from_icon_name(
			"window-close", GTK_ICON_SIZE_BUTTON), 2, 0, 1, 1);
	gtk_widget_show_all(tab_label);

	tab = mq_tab_new(uri, tab_label_image, tab_label_label);

	tab_label_event_box = gtk_event_box_new();
	g_signal_connect(tab_label_event_box, "button-press-event",
		G_CALLBACK(tab_label_button_press_cb), tab);
	gtk_event_box_set_visible_window(GTK_EVENT_BOX(tab_label_event_box),
		FALSE);
	gtk_container_add(GTK_CONTAINER(tab_label_event_box), tab_label);

	tab_widget = mq_tab_get_container(tab);
	position = gtk_notebook_insert_page(GTK_NOTEBOOK(window->notebook),
		tab_widget, tab_label_event_box, position);
	gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(window->notebook),
		tab_widget, TRUE);
	gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(window->notebook),
		tab_widget, TRUE);

	mq_tab_update_position(tab, position);
}

static void
update_positions(GtkNotebook __attribute__((unused)) *notebook,
	GtkWidget __attribute__((unused)) *child,
	guint __attribute__((unused)) page_num, MqWindow *window)
{
	/* TODO: Once MqWindow has a data structure for tabs, loop through them
	 * all and call mq_tab_update_position(). */
	/* TODO: Should this also update the tabs data structure?  Probably. */

	/* Temporarily "use" window. */
	window = window;
}

MqWindow *
mq_window_new(gchar **uris)
{
	MqWindow *window;
	guint i;

	window = malloc(sizeof(*window));

	window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size(GTK_WINDOW(window->window), 1024, 768);
	/* TODO: Don't quit on window close */
	g_signal_connect(window->window, "destroy",
		G_CALLBACK(gtk_main_quit), NULL);

	window->notebook = gtk_notebook_new();
	gtk_notebook_set_scrollable(GTK_NOTEBOOK(window->notebook), TRUE);
	gtk_notebook_set_group_name(GTK_NOTEBOOK(window->notebook), "mq-tabs");
	gtk_container_add(GTK_CONTAINER(window->window),
		window->notebook);
	g_signal_connect(window->notebook, "page-reordered",
		G_CALLBACK(update_positions), window);

	if (uris && uris[0]) {
		for (i = 0; uris && uris[i]; ++i) {
			add_tab(window, uris[i], -1);
		}
	} else {
		add_tab(window, "about:new", -1);
	}

	gtk_widget_show_all(window->window);

	return window;
}