1 /**
2 * @file gui-tabmanager.c
3 * @brief
4 *
5 * Copyright (C) 2009 Gummi Developers
6 * All Rights reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person
9 * obtaining a copy of this software and associated documentation
10 * files (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use,
12 * copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following
15 * conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30 #include "gui-tabmanager.h"
31
32 #include "gui-main.h"
33 #include "environment.h"
34
35 extern Gummi* gummi;
36 extern GummiGui* gui;
37
tabmanagergui_init(GtkBuilder * builder)38 GuTabmanagerGui* tabmanagergui_init (GtkBuilder* builder) {
39 g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
40
41 GuTabmanagerGui* tm = g_new0 (GuTabmanagerGui, 1);
42
43 tm->notebook = GTK_NOTEBOOK (gtk_builder_get_object (builder, "tab_notebook"));
44
45 tm->unsavednr = 0;
46 return tm;
47 }
48
tabmanagergui_create_page(GuTabContext * tc,GuEditor * editor)49 int tabmanagergui_create_page (GuTabContext* tc, GuEditor* editor) {
50 GuTabPage* tp = g_new0(GuTabPage, 1);
51 tc->page = tp;
52 int pos;
53
54 tp->scrollw = gtk_scrolled_window_new (NULL, NULL);
55 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(tp->scrollw),
56 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
57 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (tp->scrollw),
58 GTK_SHADOW_OUT);
59
60 gchar* labeltext = tabmanager_get_tabname (tc);
61 tabmanagergui_create_label (tp, labeltext);
62 g_signal_connect (tp->button, "clicked",
63 G_CALLBACK (on_menu_close_activate), tc);
64 tabmanagergui_create_infobar (tp);
65
66 gtk_container_add (GTK_CONTAINER (tp->scrollw),
67 GTK_WIDGET (editor->view));
68
69 tp->editorbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
70
71 gtk_box_pack_start (GTK_BOX (tp->editorbox), tp->infobar, FALSE, FALSE, 0);
72 gtk_box_pack_end (GTK_BOX (tp->editorbox), tp->scrollw, TRUE, TRUE, 0);
73
74 pos = gtk_notebook_append_page (GTK_NOTEBOOK (g_tabnotebook),
75 tp->editorbox, GTK_WIDGET (tp->labelbox));
76
77 gtk_widget_show_all (tp->editorbox);
78 return pos;
79 }
80
tabmanagergui_create_infobar(GuTabPage * tp)81 void tabmanagergui_create_infobar (GuTabPage* tp) {
82 // we will probably want to make a separate file for infobar
83 // procedures that we can attach to hboxes in both the editor
84 // and the preview window, TODO for 0.7.0 -Alex
85 GtkWidget* infobar = NULL;
86 GtkWidget* message = NULL;
87 GtkWidget* area = NULL;
88
89 infobar = gtk_info_bar_new ();
90 gtk_widget_set_no_show_all (infobar, TRUE);
91 message = gtk_label_new ("");
92 gtk_label_set_line_wrap (GTK_LABEL(message), TRUE);
93
94 gtk_widget_show (message);
95 area = gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar));
96 gtk_container_add (GTK_CONTAINER (area), message);
97
98 gtk_info_bar_add_button (GTK_INFO_BAR (infobar),
99 _("_Yes"), GTK_RESPONSE_YES);
100 gtk_info_bar_add_button (GTK_INFO_BAR (infobar),
101 _("_No"), GTK_RESPONSE_NO);
102
103 gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar),
104 GTK_MESSAGE_WARNING);
105
106 tp->barlabel = message;
107 tp->infobar = infobar;
108 }
109
tabmanagergui_create_label(GuTabPage * tp,gchar * labeltext)110 void tabmanagergui_create_label (GuTabPage* tp, gchar* labeltext) {
111 static unsigned count = 0;
112 GtkWidget* image = NULL;
113 GtkBox* hbox;
114
115 tp->labelbox = gtk_event_box_new ();
116 hbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
117 tp->unsavednr = ++count;
118
119 gtk_event_box_set_visible_window (GTK_EVENT_BOX (tp->labelbox), FALSE);
120 gtk_container_add (GTK_CONTAINER(tp->labelbox), GTK_WIDGET (hbox));
121
122 tp->label = GTK_LABEL (gtk_label_new (labeltext));
123
124 gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (tp->label), TRUE, TRUE, 5);
125
126 tp->button = GTK_BUTTON (gtk_button_new());
127
128 image = gtk_image_new_from_icon_name("window-close", GTK_ICON_SIZE_MENU);
129
130 gtk_button_set_image (tp->button, image);
131 g_object_set (tp->button, "relief", GTK_RELIEF_NONE,
132 "focus-on-click", FALSE, NULL);
133 gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (tp->button), FALSE,FALSE,0);
134
135 /* TODO get into css i guess lmao
136 rcstyle = gtk_rc_style_new ();
137 rcstyle->xthickness = rcstyle->ythickness = 0;
138 gtk_widget_modify_style (GTK_WIDGET (tp->button), rcstyle);
139 g_object_unref (rcstyle);
140 */
141
142 gtk_widget_show_all (GTK_WIDGET (hbox));
143 }
144
tabmanagergui_get_labeltext(GuTabPage * tp)145 gchar* tabmanagergui_get_labeltext (GuTabPage* tp) {
146 const gchar* text = gtk_label_get_text (GTK_LABEL(tp->label));
147 return (gchar*)text;
148 }
149
tabmanagergui_replace_page(GuTabContext * tc,GuEditor * newec)150 gint tabmanagergui_replace_page (GuTabContext* tc, GuEditor* newec) {
151
152 gummi->tabmanager->active_tab->editor = newec;
153
154 gtk_container_remove (GTK_CONTAINER (tc->page->scrollw),
155 GTK_WIDGET (g_active_editor->view));
156 editor_destroy (g_active_editor);
157 gtk_container_add (GTK_CONTAINER (tc->page->scrollw),
158 GTK_WIDGET (newec->view));
159 gtk_widget_show (GTK_WIDGET(newec->view));
160
161 int pos = gtk_notebook_page_num (g_tabnotebook,
162 gummi->tabmanager->active_tab->page->editorbox);
163 return pos;
164 }
165
tabmanagergui_set_current_page(gint position)166 void tabmanagergui_set_current_page (gint position) {
167 gtk_notebook_set_current_page (g_tabnotebook, position);
168 }
169
tabmanagergui_get_current_page(void)170 gint tabmanagergui_get_current_page (void) {
171 return gtk_notebook_get_current_page (g_tabnotebook);
172 }
173
tabmanagergui_get_n_pages(void)174 gint tabmanagergui_get_n_pages (void) {
175 return gtk_notebook_get_n_pages (g_tabnotebook);
176 }
177
tabmanagergui_update_label(GuTabPage * tp,const gchar * text)178 void tabmanagergui_update_label (GuTabPage* tp, const gchar* text) {
179 g_return_if_fail (tp != NULL);
180 gtk_label_set_text (tp->label, text);
181 if (tp->bold) tablabel_set_bold_text (tp);
182 }
183
tablabel_set_bold_text(GuTabPage * tp)184 void tablabel_set_bold_text (GuTabPage* tp) {
185 gchar* markup;
186 const gchar* cur = gtk_label_get_text (tp->label);
187 markup = g_markup_printf_escaped ("<span weight=\"bold\">%s</span>", cur);
188 gtk_label_set_markup (GTK_LABEL (tp->label), markup);
189 g_free (markup);
190 if (!tp->bold) tp->bold = TRUE;
191 }
192