mp3splt-gtk

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

messages.c
Go to the documentation of this file.
1 /**********************************************************
2 *
3 * mp3splt-gtk -- utility based on mp3splt,
4 * for mp3/ogg splitting without decoding
5 *
6 * Copyright: (C) 2005-2012 Alexandru Munteanu
7 * Contact: io_fx@yahoo.fr
8 *
9 * http://mp3splt.sourceforge.net/
10 *
11 *********************************************************/
12
13 /**********************************************************
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 * USA.
29 *
30 *********************************************************/
31
32 /*!********************************************************
33 * \file
34 * The messages history dialog
35 *********************************************************/
36
37 #include <gtk/gtk.h>
38 #include <glib/gi18n.h>
39
40 #include <libmp3splt/mp3splt.h>
41
42 #include "main_win.h"
43
44 GtkWidget *mess_history_dialog = NULL;
45 GtkTextBuffer *mess_hist_buffer = NULL;
46 GtkTextTagTable *mess_hist_tag_table = NULL;
47 GtkWidget *mess_hist_view = NULL;
48 gint debug_is_active = FALSE;
49
50 void create_mess_history_dialog();
51
52 extern splt_state *the_state;
53
55 const char *get_current_time()
56 {
57 time_t cur_time = { 0 };
58 static char time_str[128] = { '\0' };
59 cur_time = time(NULL);
60 const struct tm *tm = localtime(&cur_time);
61 strftime(time_str, sizeof(time_str), "(%H:%M:%S) ", tm);
62
63 return time_str;
64 }
65
67 void put_message_in_history(const gchar *message, splt_message_type mess_type)
68 {
69 if (mess_type == SPLT_MESSAGE_INFO ||
70 (mess_type == SPLT_MESSAGE_DEBUG && debug_is_active))
71 {
72 GtkTextTag *gray_tag =
73 gtk_text_tag_table_lookup(mess_hist_tag_table, "gray_bold");
74
75 GtkTextIter iter;
76 const char *current_time = get_current_time();
77 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(mess_hist_buffer), &iter);
78 gtk_text_buffer_insert_with_tags(GTK_TEXT_BUFFER(mess_hist_buffer),
79 &iter, current_time, -1, gray_tag, NULL);
80
81 gtk_text_buffer_insert(GTK_TEXT_BUFFER(mess_hist_buffer), &iter,
82 message, -1);
83 gtk_text_buffer_insert(GTK_TEXT_BUFFER(mess_hist_buffer), &iter,
84 "\n", -1);
85
86 gtk_text_iter_set_line_offset(&iter, 0);
87 GtkTextMark *mark = gtk_text_buffer_get_mark(mess_hist_buffer, "end");
88 gtk_text_buffer_move_mark(mess_hist_buffer, mark, &iter);
89 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(mess_hist_view), mark);
90 }
91 }
92
94 void mess_history_hide(GtkDialog *dialog, gint response_id, gpointer data)
95 {
96 gtk_widget_hide(mess_history_dialog);
97 }
98
105 void mess_history_hide2(GtkWidget *widget, gpointer data)
106 {
107 mess_history_hide(NULL, 0, NULL);
108 //TODO: ugly HACK!
109 create_mess_history_dialog();
110 }
111
113 void add_mess_hist_tags()
114 {
115 GtkTextTag *tag = gtk_text_tag_new("gray_bold");
116
117 GValue fg_val = { 0 };
118 g_value_init(&fg_val, G_TYPE_STRING);
119 g_value_set_static_string(&fg_val, "gray");
120 g_object_set_property(G_OBJECT(tag), "foreground", &fg_val);
121
122 gtk_text_tag_table_add(mess_hist_tag_table, tag);
123 }
124
126 void debug_check_event(GtkToggleButton *debug_toggle, gpointer user_data)
127 {
128 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(debug_toggle)))
129 {
130 debug_is_active = TRUE;
131 }
132 else
133 {
134 debug_is_active = FALSE;
135 }
136 }
137
139 void clear_messages_event(GtkWidget *widget, gpointer data)
140 {
141 GtkTextIter start_iter;
142 GtkTextIter end_iter;
143 gtk_text_buffer_get_start_iter(mess_hist_buffer, &start_iter);
144 gtk_text_buffer_get_end_iter(mess_hist_buffer, &end_iter);
145 gtk_text_buffer_delete(mess_hist_buffer, &start_iter, &end_iter);
146 }
147
149 GtkWidget *create_text_component()
150 {
151 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
152 gtk_container_set_border_width(GTK_CONTAINER(vbox), 3);
153
154 //text view
155 mess_hist_tag_table = gtk_text_tag_table_new();
156 add_mess_hist_tags();
157
158 mess_hist_buffer = gtk_text_buffer_new(mess_hist_tag_table);
159 GtkTextIter iter;
160 gtk_text_buffer_get_end_iter(mess_hist_buffer, &iter);
161 gtk_text_buffer_create_mark(mess_hist_buffer, "end", &iter, TRUE);
162 mess_hist_view = gtk_text_view_new_with_buffer(mess_hist_buffer);
163
164 gtk_text_view_set_editable(GTK_TEXT_VIEW(mess_hist_view), FALSE);
165 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(mess_hist_view), FALSE);
166 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(mess_hist_view), 5);
167
168 GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
169 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_NONE);
170 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
171 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
172
173 gtk_container_add(GTK_CONTAINER(scrolled_window), mess_hist_view);
174
175 //top
176 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
177
178 //debug option
179 GtkWidget *debug_check_button =
180 gtk_check_button_new_with_mnemonic(_("Enable _debug messages"));
181 g_signal_connect(G_OBJECT(debug_check_button), "toggled",
182 G_CALLBACK(debug_check_event), NULL);
183 gtk_box_pack_start(GTK_BOX(hbox), debug_check_button, FALSE, FALSE, 0);
184
185 //clear button
186 GtkWidget *clear_button =
187 create_cool_button(GTK_STOCK_CLEAR, _("C_lear"), FALSE);
188 g_signal_connect(G_OBJECT(clear_button), "clicked",
189 G_CALLBACK(clear_messages_event), NULL);
190 gtk_box_pack_end(GTK_BOX(hbox), clear_button, FALSE, FALSE, 0);
191
192 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
193 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 3);
194
195 return vbox;
196 }
197
199 void create_mess_history_dialog()
200 {
201 mess_history_dialog = gtk_dialog_new_with_buttons(_("Messages history"), NULL,
202 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL);
203
204 gtk_window_set_default_size(GTK_WINDOW(mess_history_dialog), 550, 300);
205
206 g_signal_connect_swapped(mess_history_dialog, "response",
207 G_CALLBACK(mess_history_hide), NULL);
208 g_signal_connect(G_OBJECT(mess_history_dialog), "delete_event",
209 G_CALLBACK(mess_history_hide2), NULL);
210
211 gtk_window_set_position(GTK_WINDOW(mess_history_dialog), GTK_WIN_POS_CENTER);
212
213 GtkWidget *text_component = create_text_component();
214 GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(mess_history_dialog));
215 gtk_box_pack_start(GTK_BOX(area), text_component, TRUE, TRUE, 0);
216 }
217