/* $Id$ */ /* * Cantus Tag Editor * Copyright © 2002-2004 by Samuel Abels * Copyright © 2007 by Tim Huetz * * 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 **/ #ifdef HAVE_CONFIG_H # include #endif #include //#define _DEBUG_ /****************************************************************************** * Constructor/Destructor ******************************************************************************/ Editarea::Editarea(bool allow_multiple_genres) { multiple_genres = allow_multiple_genres; genreselector = NULL; } Editarea::~Editarea(void) { #ifdef _DEBUG_ //printf(EDITAREANAME "(): Editarea::~Editarea(): Called.\n"); printf("SOMEDITAREA(): Editarea::~Editarea(): Called.\n"); #endif } /****************************************************************************** * Public ******************************************************************************/ GtkWidget *Editarea::build_horizontal(void) { Gtk::HPaned *toplevel = new Gtk::HPaned; Gtk::Table *editarea = build_editarea(); Gtk::Table *comment = build_commentbox(); parent = new Gtk::HBox; toplevel->set_position(380); editarea->set_border_width(6); comment->set_border_width(6); toplevel->add1(*Gtk::manage(editarea)); toplevel->add2(*Gtk::manage(comment)); toplevel->set_focus_chain(focuschain); parent->pack_start(*Gtk::manage(toplevel), TRUE, TRUE); parent->pack_start(*Gtk::manage(buttonbox), FALSE, TRUE); parent->show_all(); return (GtkWidget*)Glib::unwrap(parent); } GtkWidget *Editarea::build_vertical(void) { Gtk::Table *editarea = build_editarea(); Gtk::Table *comment = build_commentbox(); parent = new Gtk::VBox; parent->set_border_width(6); parent->pack_start(*Gtk::manage(editarea), FALSE, TRUE); parent->pack_start(*Gtk::manage(comment), TRUE, TRUE); parent->pack_start(*Gtk::manage(buttonbox), FALSE, TRUE); parent->set_focus_chain(focuschain); parent->show_all(); return (GtkWidget*)Glib::unwrap(parent); } GtkWidget *Editarea::build(bool vertical) { build_buttonbox(vertical); if (vertical) return build_vertical(); else return build_horizontal(); } Gtk::Widget *Editarea::get_widget(const gchar *widgetname) { Gtk::Widget *widget = widgets[widgetname]; if (!widget) g_warning("Editarea::get_widget(): Widget not found: %s\n", widgetname); g_assert(widget != NULL); return widget; } void Editarea::set_entry_text(const gchar *widgetname, const gchar *str) { #ifdef _DEBUG_ printf("Editarea::~set_entry_text(): Called. (%s)\n", str); #endif Gtk::Entry *entry = (Gtk::Entry*)get_widget(widgetname); entry->set_text(str ? str : ""); } Glib::ustring Editarea::get_entry_text(const gchar *widgetname) { Gtk::Entry *entry = (Gtk::Entry*)get_widget(widgetname); return entry->get_text(); } void Editarea::set_textview_text(const gchar *widgetname, const gchar *str) { #ifdef _DEBUG_ printf("Editarea::~set_textview_text(): Called. (%s)\n", str); #endif Gtk::TextView *text = (Gtk::TextView*)get_widget(widgetname); text->get_buffer()->set_text(str); } Glib::ustring Editarea::get_textview_text(const gchar *widgetname) { #ifdef _DEBUG_ printf("Editarea::~set_textview_text(): Called. (%s)\n", widgetname); #endif Gtk::TextView *text = (Gtk::TextView*)get_widget(widgetname); Glib::RefPtr buffer = text->get_buffer(); return buffer->get_text(); } void Editarea::set_check_active(const gchar *widgetname, bool active) { Gtk::CheckButton *check = (Gtk::CheckButton*)get_widget(widgetname); check->set_active(active); } bool Editarea::get_check_active(const gchar *widgetname) { Gtk::CheckButton *check = (Gtk::CheckButton*)get_widget(widgetname); return check->get_active(); } void Editarea::set_active(bool active) { buttonbox->set_sensitive(active); } /****************************************************************************** * Private ******************************************************************************/ Gtk::Table* Editarea::build_editarea(void) { Gtk::Table *table = new Gtk::Table(6, 4); Gtk::CheckButton *check = NULL; Gtk::Entry *entry = NULL; Gtk::SpinButton *spin = NULL; Gtk::HBox *hbox = NULL; Gtk::Button *button = NULL; table->set_row_spacings(3); table->set_col_spacings(12); table->set_border_width(0); // Add all checkbuttons first. check = new Gtk::CheckButton(_("Artist:")); widgets["Artist:Check"] = check; table->attach(*check, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL); check = new Gtk::CheckButton(_("Song:")); widgets["Song:Check"] = check; table->attach(*check, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL); check = new Gtk::CheckButton(_("Album:")); widgets["Album:Check"] = check; table->attach(*check, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL); check = new Gtk::CheckButton(_("Track:")); widgets["Track:Check"] = check; table->attach(*check, 0, 1, 3, 4, Gtk::FILL, Gtk::FILL); check = new Gtk::CheckButton(_("Year:")); widgets["Year:Check"] = check; table->attach(*check, 2, 3, 3, 4, Gtk::FILL, Gtk::FILL); check = new Gtk::CheckButton(_("Genre:")); widgets["Genre:Check"] = check; table->attach(*check, 0, 1, 4, 5, Gtk::FILL, Gtk::FILL); // Add all entry fields. entry = new Gtk::Entry; widgets["Artist"] = entry; table->attach(*entry, 1, 5, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL); entry = new Gtk::Entry; widgets["Song"] = entry; table->attach(*entry, 1, 5, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL); entry = new Gtk::Entry; widgets["Album"] = entry; table->attach(*entry, 1, 5, 2, 3, Gtk::FILL|Gtk::EXPAND, Gtk::FILL); spin = new Gtk::SpinButton; spin->set_range(1, 1000); spin->set_increments(1, 10); widgets["Track"] = spin; table->attach(*spin, 1, 2, 3, 4, Gtk::FILL|Gtk::EXPAND, Gtk::FILL); entry = new Gtk::Entry; entry->set_size_request(70, -1); widgets["Year"] = entry; table->attach(*entry, 4, 5, 3, 4, Gtk::FILL|Gtk::EXPAND, Gtk::FILL); // The genre entry and button are encapsulated in a hbox. Add this box. hbox = new Gtk::HBox; hbox->set_spacing(3); hbox->set_size_request(70, -1); table->attach(*hbox, 1, 5, 4, 5, Gtk::FILL|Gtk::EXPAND, Gtk::FILL); entry = new Gtk::Entry; widgets["Genre"] = entry; entry->set_size_request(70, -1); entry->signal_changed().connect( sigc::mem_fun(*this, &Editarea::on_entry_genre_changed)); hbox->pack_start(*entry, TRUE, TRUE); button = new Gtk::Button("..."); hbox->pack_start(*button, FALSE, FALSE); button->signal_clicked().connect( sigc::mem_fun(*this, &Editarea::on_button_genre_clicked)); // Define the focus chain. FIXME: Check according to HIG. focuschain.push_back(widgets["Artist"]); focuschain.push_back(widgets["Artist:Check"]); focuschain.push_back(widgets["Song"]); focuschain.push_back(widgets["Song:Check"]); focuschain.push_back(widgets["Album"]); focuschain.push_back(widgets["Album:Check"]); focuschain.push_back(widgets["Track"]); focuschain.push_back(widgets["Track:Check"]); focuschain.push_back(widgets["Year"]); focuschain.push_back(widgets["Year:Check"]); focuschain.push_back(widgets["Genre"]); focuschain.push_back(widgets["Genre:Check"]); return table; } Gtk::Table* Editarea::build_commentbox(void) { Gtk::Table *table = new Gtk::Table(2, 1); Gtk::CheckButton *check = new Gtk::CheckButton(_("Comment:")); Gtk::ScrolledWindow *scroll = new Gtk::ScrolledWindow; Gtk::TextView *text = new Gtk::TextView; widgets["Comment:Check"] = check; widgets["Comment"] = text; focuschain.push_back(text); focuschain.push_back(check); text->set_wrap_mode(Gtk::WRAP_WORD); scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); scroll->set_shadow_type(Gtk::SHADOW_IN); scroll->add(*text); table->attach(*check, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL); table->attach(*scroll, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND); return table; } void Editarea::build_buttonbox(bool vertical) { Gtk::Button *button = NULL; if (vertical) buttonbox = new Gtk::HBox; else buttonbox = new Gtk::VBox; buttonbox->set_spacing(6); buttonbox->set_border_width(6); Gtk::Fixed *fixed = new Gtk::Fixed; if (vertical) buttonbox->pack_start(*fixed, TRUE, TRUE); else buttonbox->pack_end(*fixed, TRUE, TRUE); // Add the "Save" button. button = new Gtk::Button(Gtk::Stock::SAVE); button->set_size_request(-1, 35); if (vertical) buttonbox->pack_end(*button, FALSE, TRUE); else buttonbox->pack_start(*button, FALSE, TRUE); button->signal_clicked().connect(signal_button_save_clicked); // Add the "Clear" button. button = new Gtk::Button(Gtk::Stock::CLEAR); button->set_size_request(-1, 35); buttonbox->pack_start(*button, FALSE, TRUE); button->signal_clicked().connect( sigc::mem_fun(*this, &Editarea::on_button_clear_clicked)); } void Editarea::get_children_recursive( Gtk::Container *container, std::list *children) { std::list newchildren = container->get_children(); std::list::iterator iter = newchildren.begin(); while (iter != newchildren.end()) { Gtk::Container *container = dynamic_cast(*iter); if (container) get_children_recursive(container, children); iter++; } newchildren.sort(); children->merge(newchildren); } void Editarea::on_button_clear_clicked(void) { std::list children; std::list::iterator iter; get_children_recursive(parent, &children); iter = children.begin(); while (iter != children.end()) { Gtk::Entry *entry = dynamic_cast(*iter); if (entry) // Handle entry boxes only. entry->set_text(""); iter++; } Gtk::TextView *textview = (Gtk::TextView*)get_widget("Comment"); textview->get_buffer()->set_text(""); } void Editarea::on_button_genre_clicked(void) { if (genreselector) return; Gtk::Entry *entry = (Gtk::Entry*)get_widget("Genre"); genreselector = new GenreSelector(multiple_genres); genreselector->set_selected_genres(entry->get_text()); genreselector->signal_button_close_clicked.connect( sigc::mem_fun(*this, &Editarea::on_genreselector_close_clicked)); genreselector->signal_selection_changed.connect( sigc::mem_fun(*this, &Editarea::on_genreselector_selection_changed)); } void Editarea::on_entry_genre_changed(void) { if (!genreselector) return; Gtk::Entry *entry = (Gtk::Entry*)get_widget("Genre"); genreselector->set_selected_genres(entry->get_text()); } void Editarea::on_genreselector_close_clicked(void) { delete genreselector; genreselector = NULL; } void Editarea::on_genreselector_selection_changed(std::list sel) { if (!genreselector) return; Gtk::Entry *entry = (Gtk::Entry*)get_widget("Genre"); entry->set_text(genreselector->get_selected_genres()); }