comparison src/Input/amidi-plug/i_fileinfo.c @ 0:13389e613d67 trunk

[svn] - initial import of audacious-plugins tree (lots to do)
author nenolod
date Mon, 18 Sep 2006 01:11:49 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13389e613d67
1 /*
2 *
3 * Author: Giacomo Lozito <james@develia.org>, (C) 2005-2006
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21
22 #include <gtk/gtk.h>
23 #include "i_fileinfo.h"
24 #include "i_configure.h"
25 /* this is needed to retrieve information */
26 #include "i_midi.h"
27 /* icon from gnome-mime-audio-midi.png of the GNOME ICON SET */
28 #include "amidi-plug.midiicon.xpm"
29
30
31 void i_fileinfo_ev_destroy( GtkWidget * win , gpointer mf )
32 {
33 i_midi_free( (midifile_t *)mf );
34 g_free( mf );
35 }
36
37
38 void i_fileinfo_ev_close( GtkWidget * button , gpointer fileinfowin )
39 {
40 gtk_widget_destroy( GTK_WIDGET(fileinfowin) );
41 }
42
43
44 void i_fileinfo_table_add_entry( gchar * field_text , gchar * value_text ,
45 GtkWidget * table , guint line , PangoAttrList * attrlist )
46 {
47 GtkWidget *field, *value;
48 field = gtk_label_new( field_text );
49 gtk_label_set_attributes( GTK_LABEL(field) , attrlist );
50 gtk_misc_set_alignment( GTK_MISC(field) , 0 , 0 );
51 gtk_label_set_justify( GTK_LABEL(field) , GTK_JUSTIFY_LEFT );
52 gtk_table_attach( GTK_TABLE(table) , field , 0 , 1 , line , (line + 1) ,
53 GTK_FILL , GTK_FILL , 5 , 2 );
54 value = gtk_label_new( value_text );
55 gtk_misc_set_alignment( GTK_MISC(value) , 0 , 0 );
56 gtk_label_set_justify( GTK_LABEL(value) , GTK_JUSTIFY_LEFT );
57 gtk_table_attach( GTK_TABLE(table) , value , 1 , 2 , line , (line + 1) ,
58 GTK_FILL , GTK_FILL , 5 , 2 );
59 return;
60 }
61
62
63 /* COMMENT: this will also reset current position in each track! */
64 void i_fileinfo_text_fill( midifile_t * mf , GtkTextBuffer * text_tb, GtkTextBuffer * lyrics_tb )
65 {
66 gint l = 0;
67 /* initialize current position in each track */
68 for (l = 0; l < mf->num_tracks; ++l)
69 mf->tracks[l].current_event = mf->tracks[l].first_event;
70
71 for (;;)
72 {
73 midievent_t * event = NULL;
74 midifile_track_t * event_track = NULL;
75 gint i, min_tick = mf->max_tick + 1;
76
77 /* search next event */
78 for ( i = 0 ; i < mf->num_tracks ; ++i )
79 {
80 midifile_track_t * track = &mf->tracks[i];
81 midievent_t * e2 = track->current_event;
82 if (( e2 ) && ( e2->tick < min_tick ))
83 {
84 min_tick = e2->tick;
85 event = e2;
86 event_track = track;
87 }
88 }
89
90 if (!event)
91 break; /* end of song reached */
92
93 /* advance pointer to next event */
94 event_track->current_event = event->next;
95
96 switch ( event->type )
97 {
98 case SND_SEQ_EVENT_META_TEXT:
99 gtk_text_buffer_insert_at_cursor( text_tb , event->data.metat , strlen(event->data.metat) );
100 break;
101 case SND_SEQ_EVENT_META_LYRIC:
102 gtk_text_buffer_insert_at_cursor( lyrics_tb , event->data.metat , strlen(event->data.metat) );
103 break;
104 }
105 }
106 }
107
108
109 void i_fileinfo_gui( gchar * filename )
110 {
111 static GtkWidget *fileinfowin = NULL;
112 GtkWidget *fileinfowin_vbox, *fileinfowin_columns_hbox;
113 GtkWidget *midiinfoboxes_vbox, *miditextboxes_vbox, *miditextboxes_paned;
114 GtkWidget *title_hbox, *title_icon_image, *title_name_f_label, *title_name_v_entry;
115 GtkWidget *info_frame, *info_frame_tl, *info_table;
116 GtkWidget *text_frame, *text_frame_tl, *text_tv, *text_tv_sw;
117 GtkWidget *lyrics_frame, *lyrics_tv, *lyrics_tv_sw;
118 GtkTextBuffer *text_tb, *lyrics_tb;
119 GtkWidget *footer_hbbox, *footer_bclose;
120 GdkPixbuf *title_icon_pixbuf;
121 PangoAttrList *pangoattrlist;
122 PangoAttribute *pangoattr;
123 GString *value_gstring;
124 gchar *title , *filename_utf8;
125 gint bpm = 0, wavg_bpm = 0;
126 midifile_t *mf;
127
128 if ( fileinfowin )
129 return;
130
131 mf = g_malloc(sizeof(midifile_t));
132
133 /****************** midifile parser ******************/
134 if ( !i_midi_parse_from_filename( filename , mf ) )
135 return;
136 /* midifile is filled with information at this point,
137 bpm information is needed too */
138 i_midi_get_bpm( mf , &bpm , &wavg_bpm );
139 /*****************************************************/
140
141 fileinfowin = gtk_window_new( GTK_WINDOW_TOPLEVEL );
142 gtk_window_set_type_hint( GTK_WINDOW(fileinfowin), GDK_WINDOW_TYPE_HINT_DIALOG );
143 gtk_window_set_position( GTK_WINDOW(fileinfowin) , GTK_WIN_POS_CENTER );
144 g_signal_connect( G_OBJECT(fileinfowin) , "destroy" , G_CALLBACK(i_fileinfo_ev_destroy) , mf );
145 g_signal_connect( G_OBJECT(fileinfowin) , "destroy" , G_CALLBACK(gtk_widget_destroyed) , &fileinfowin );
146 gtk_container_set_border_width( GTK_CONTAINER(fileinfowin), 10 );
147
148 fileinfowin_vbox = gtk_vbox_new( FALSE , 10 );
149 gtk_container_add( GTK_CONTAINER(fileinfowin) , fileinfowin_vbox );
150
151 /* pango attributes */
152 pangoattrlist = pango_attr_list_new();
153 pangoattr = pango_attr_weight_new( PANGO_WEIGHT_BOLD );
154 pangoattr->start_index = 0;
155 pangoattr->end_index = G_MAXINT;
156 pango_attr_list_insert( pangoattrlist , pangoattr );
157
158 /******************
159 *** TITLE LINE ***/
160 title_hbox = gtk_hbox_new( FALSE , 5 );
161 gtk_box_pack_start( GTK_BOX(fileinfowin_vbox) , title_hbox , FALSE , FALSE , 0 );
162
163 title_icon_pixbuf = gdk_pixbuf_new_from_xpm_data( (const gchar **)amidiplug_xpm_midiicon );
164 title_icon_image = gtk_image_new_from_pixbuf( title_icon_pixbuf );
165 g_object_unref( title_icon_pixbuf );
166 gtk_misc_set_alignment( GTK_MISC(title_icon_image) , 0 , 0 );
167 gtk_box_pack_start( GTK_BOX(title_hbox) , title_icon_image , FALSE , FALSE , 0 );
168
169 title_name_f_label = gtk_label_new( _("Name:") );
170 gtk_label_set_attributes( GTK_LABEL(title_name_f_label) , pangoattrlist );
171 gtk_box_pack_start( GTK_BOX(title_hbox) , title_name_f_label , FALSE , FALSE , 0 );
172
173 title_name_v_entry = gtk_entry_new();
174 gtk_editable_set_editable( GTK_EDITABLE(title_name_v_entry) , FALSE );
175 gtk_widget_set_size_request( GTK_WIDGET(title_name_v_entry) , 200 , -1 );
176 gtk_box_pack_start(GTK_BOX(title_hbox) , title_name_v_entry , TRUE , TRUE , 0 );
177
178 fileinfowin_columns_hbox = gtk_hbox_new( FALSE , 2 );
179 gtk_box_pack_start( GTK_BOX(fileinfowin_vbox) , fileinfowin_columns_hbox , TRUE , TRUE , 0 );
180
181 /*********************
182 *** MIDI INFO BOX ***/
183 midiinfoboxes_vbox = gtk_vbox_new( FALSE , 2 );
184 /* pick the entire space if both comments and lyrics boxes are not displayed,
185 pick only required space if at least one of them is displayed */
186 if (( amidiplug_cfg_ap.ap_opts_comments_extract == 0 ) &&
187 ( amidiplug_cfg_ap.ap_opts_lyrics_extract == 0 ))
188 gtk_box_pack_start( GTK_BOX(fileinfowin_columns_hbox) , midiinfoboxes_vbox , TRUE , TRUE , 0 );
189 else
190 gtk_box_pack_start( GTK_BOX(fileinfowin_columns_hbox) , midiinfoboxes_vbox , FALSE , FALSE , 0 );
191
192 info_frame_tl = gtk_label_new( "" );
193 gtk_label_set_markup( GTK_LABEL(info_frame_tl) , _("<span size=\"smaller\"> MIDI Info </span>") );
194 gtk_box_pack_start( GTK_BOX(midiinfoboxes_vbox) , info_frame_tl , FALSE , FALSE , 0 );
195
196 info_frame = gtk_frame_new( NULL );
197 gtk_box_pack_start( GTK_BOX(midiinfoboxes_vbox) , info_frame , TRUE , TRUE , 0 );
198 info_table = gtk_table_new( 6 , 2 , FALSE );
199 gtk_container_set_border_width( GTK_CONTAINER(info_table) , 5 );
200 gtk_container_add( GTK_CONTAINER(info_frame) , info_table );
201 value_gstring = g_string_new( "" );
202
203 /* midi format */
204 G_STRING_PRINTF( value_gstring , "type %i" , mf->format );
205 i_fileinfo_table_add_entry( _("Format:") , value_gstring->str , info_table , 0 , pangoattrlist );
206 /* midi length */
207 G_STRING_PRINTF( value_gstring , "%i" , (gint)(mf->length / 1000) );
208 i_fileinfo_table_add_entry( _("Length (msec):") , value_gstring->str , info_table , 1 , pangoattrlist );
209 /* midi num of tracks */
210 G_STRING_PRINTF( value_gstring , "%i" , mf->num_tracks );
211 i_fileinfo_table_add_entry( _("Num of Tracks:") , value_gstring->str , info_table , 2 , pangoattrlist );
212 /* midi bpm */
213 if ( bpm > 0 )
214 G_STRING_PRINTF( value_gstring , "%i" , bpm ); /* fixed bpm */
215 else
216 G_STRING_PRINTF( value_gstring , _("variable") ); /* variable bpm */
217 i_fileinfo_table_add_entry( _("BPM:") , value_gstring->str , info_table , 3 , pangoattrlist );
218 /* midi weighted average bpm */
219 if ( bpm > 0 )
220 G_STRING_PRINTF( value_gstring , "/" ); /* fixed bpm, don't care about wavg_bpm */
221 else
222 G_STRING_PRINTF( value_gstring , "%i" , wavg_bpm ); /* variable bpm, display wavg_bpm */
223 i_fileinfo_table_add_entry( _("BPM (wavg):") , value_gstring->str , info_table , 4 , pangoattrlist );
224 /* midi time division */
225 G_STRING_PRINTF( value_gstring , "%i" , mf->time_division );
226 i_fileinfo_table_add_entry( _("Time Div:") , value_gstring->str , info_table , 5 , pangoattrlist );
227
228 g_string_free( value_gstring , TRUE );
229
230 /**********************************
231 *** MIDI COMMENTS/LYRICS BOXES ***/
232 miditextboxes_vbox = gtk_vbox_new( FALSE , 2 );
233 gtk_box_pack_start( GTK_BOX(fileinfowin_columns_hbox) , miditextboxes_vbox , TRUE , TRUE , 0 );
234
235 text_frame_tl = gtk_label_new( "" );
236 gtk_label_set_markup( GTK_LABEL(text_frame_tl) ,
237 _("<span size=\"smaller\"> MIDI Comments and Lyrics </span>") );
238 gtk_box_pack_start( GTK_BOX(miditextboxes_vbox) , text_frame_tl , FALSE , FALSE , 0 );
239
240 miditextboxes_paned = gtk_vpaned_new();
241 gtk_box_pack_start( GTK_BOX(miditextboxes_vbox) , miditextboxes_paned , TRUE , TRUE , 0 );
242
243 text_frame = gtk_frame_new( NULL );
244 gtk_paned_pack1( GTK_PANED(miditextboxes_paned) , text_frame , TRUE , TRUE );
245 text_tv = gtk_text_view_new();
246 gtk_text_view_set_editable( GTK_TEXT_VIEW(text_tv) , FALSE );
247 gtk_text_view_set_cursor_visible( GTK_TEXT_VIEW(text_tv) , FALSE );
248 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW(text_tv) , GTK_WRAP_WORD );
249 gtk_text_view_set_right_margin( GTK_TEXT_VIEW(text_tv) , 4 );
250 gtk_text_view_set_left_margin( GTK_TEXT_VIEW(text_tv) , 4 );
251 gtk_widget_set_size_request( text_tv , 300 , 113 );
252 text_tv_sw = gtk_scrolled_window_new( NULL , NULL );
253 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(text_tv_sw) ,
254 GTK_POLICY_AUTOMATIC , GTK_POLICY_ALWAYS );
255 gtk_container_add( GTK_CONTAINER(text_frame) , text_tv_sw );
256 gtk_container_add( GTK_CONTAINER(text_tv_sw) , text_tv );
257
258 lyrics_frame = gtk_frame_new( NULL );
259 gtk_paned_pack2( GTK_PANED(miditextboxes_paned) , lyrics_frame , TRUE , TRUE );
260 lyrics_tv = gtk_text_view_new();
261 gtk_text_view_set_editable( GTK_TEXT_VIEW(lyrics_tv) , FALSE );
262 gtk_text_view_set_cursor_visible( GTK_TEXT_VIEW(lyrics_tv) , FALSE );
263 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW(lyrics_tv) , GTK_WRAP_WORD );
264 gtk_text_view_set_right_margin( GTK_TEXT_VIEW(lyrics_tv) , 4 );
265 gtk_text_view_set_left_margin( GTK_TEXT_VIEW(lyrics_tv) , 4 );
266 gtk_widget_set_size_request( lyrics_tv , 300 , 113 );
267 lyrics_tv_sw = gtk_scrolled_window_new( NULL , NULL );
268 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(lyrics_tv_sw) ,
269 GTK_POLICY_AUTOMATIC , GTK_POLICY_ALWAYS );
270 gtk_container_add( GTK_CONTAINER(lyrics_frame) , lyrics_tv_sw );
271 gtk_container_add( GTK_CONTAINER(lyrics_tv_sw) , lyrics_tv );
272
273 text_tb = gtk_text_view_get_buffer( GTK_TEXT_VIEW(text_tv) );
274 lyrics_tb = gtk_text_view_get_buffer( GTK_TEXT_VIEW(lyrics_tv) );
275
276 /* call the buffer fill routine if at least one between comments and lyrics is enabled */
277 if (( amidiplug_cfg_ap.ap_opts_comments_extract > 0 ) ||
278 ( amidiplug_cfg_ap.ap_opts_lyrics_extract > 0 ))
279 i_fileinfo_text_fill( mf , text_tb , lyrics_tb );
280
281 if (( amidiplug_cfg_ap.ap_opts_comments_extract > 0 ) &&
282 ( gtk_text_buffer_get_char_count( text_tb ) == 0 ))
283 {
284 GtkTextIter start, end;
285 GtkTextTag *tag = gtk_text_buffer_create_tag( text_tb , "italicstyle" ,
286 "style" , PANGO_STYLE_ITALIC , NULL );
287 /*gtk_text_view_set_justification( GTK_TEXT_VIEW(text_tv) , GTK_JUSTIFY_CENTER );*/
288 gtk_text_buffer_set_text( text_tb , _("* no comments available in this MIDI file *") , -1 );
289 gtk_text_buffer_get_iter_at_offset( text_tb , &start , 0 );
290 gtk_text_buffer_get_iter_at_offset( text_tb , &end , -1 );
291 gtk_text_buffer_apply_tag( text_tb , tag , &start , &end );
292 }
293
294 if (( amidiplug_cfg_ap.ap_opts_lyrics_extract > 0 ) &&
295 ( gtk_text_buffer_get_char_count( lyrics_tb ) == 0 ))
296 {
297 GtkTextIter start, end;
298 GtkTextTag *tag = gtk_text_buffer_create_tag( lyrics_tb , "italicstyle" ,
299 "style" , PANGO_STYLE_ITALIC , NULL );
300 /*gtk_text_view_set_justification( GTK_TEXT_VIEW(lyrics_tv) , GTK_JUSTIFY_CENTER );*/
301 gtk_text_buffer_set_text( lyrics_tb , _("* no lyrics available in this MIDI file *") , -1 );
302 gtk_text_buffer_get_iter_at_offset( lyrics_tb , &start , 0 );
303 gtk_text_buffer_get_iter_at_offset( lyrics_tb , &end , -1 );
304 gtk_text_buffer_apply_tag( lyrics_tb , tag , &start , &end );
305 }
306
307 /* hide boxes for disabled options (comments and/or lyrics) */
308 if (( amidiplug_cfg_ap.ap_opts_comments_extract == 0 ) &&
309 ( amidiplug_cfg_ap.ap_opts_lyrics_extract == 0 ))
310 {
311 gtk_widget_set_no_show_all( miditextboxes_vbox , TRUE );
312 gtk_widget_hide( miditextboxes_vbox );
313 }
314 else if ( amidiplug_cfg_ap.ap_opts_comments_extract == 0 )
315 {
316 gtk_widget_set_no_show_all( text_frame , TRUE );
317 gtk_widget_hide( text_frame );
318 }
319 else if ( amidiplug_cfg_ap.ap_opts_lyrics_extract == 0 )
320 {
321 gtk_widget_set_no_show_all( lyrics_frame , TRUE );
322 gtk_widget_hide( lyrics_frame );
323 }
324
325 /**************
326 *** FOOTER ***/
327 footer_hbbox = gtk_hbutton_box_new();
328 gtk_button_box_set_layout( GTK_BUTTON_BOX(footer_hbbox) , GTK_BUTTONBOX_END );
329 footer_bclose = gtk_button_new_from_stock( GTK_STOCK_CLOSE );
330 g_signal_connect( G_OBJECT(footer_bclose) , "clicked" , G_CALLBACK(i_fileinfo_ev_close) , fileinfowin );
331 gtk_container_add( GTK_CONTAINER(footer_hbbox) , footer_bclose );
332 gtk_box_pack_start( GTK_BOX(fileinfowin_vbox) , footer_hbbox , FALSE , FALSE , 0 );
333
334
335 /* utf8-ize filename and set window title */
336 filename_utf8 = g_strdup(g_filename_to_utf8( filename , -1 , NULL , NULL , NULL ));
337 if ( !filename_utf8 )
338 {
339 /* utf8 fallback */
340 gchar *chr , *convert_str = g_strdup( filename );
341 for ( chr = convert_str ; *chr ; chr++ )
342 {
343 if ( *chr & 0x80 )
344 *chr = '?';
345 }
346 filename_utf8 = g_strconcat( convert_str , _(" (invalid UTF-8)") , NULL );
347 g_free(convert_str);
348 }
349 title = g_strdup_printf( "%s - " PLAYER_NAME , g_basename(filename_utf8));
350 gtk_window_set_title( GTK_WINDOW(fileinfowin) , title);
351 g_free(title);
352 /* set the text for the filename header too */
353 gtk_entry_set_text( GTK_ENTRY(title_name_v_entry) , filename_utf8 );
354 gtk_editable_set_position( GTK_EDITABLE(title_name_v_entry) , -1 );
355 g_free(filename_utf8);
356
357 gtk_widget_grab_focus( GTK_WIDGET(footer_bclose) );
358 gtk_widget_show_all( fileinfowin );
359 }