📄 dialogs.cpp
字号:
/* Copyright (C) 2001 2002 Chris VineThis program is distributed under the General Public Licence, version 2.For particulars of this and relevant disclaimers see the fileCOPYING distributed with the source files.*/#include <sys/types.h>#include <unistd.h>#include <algorithm>#include <cstdlib>#include <cstring>#include <gtkmm/main.h>#include <gdk/gdkkeysyms.h> // the key codes are here#include <gdkmm/pixbuf.h>#include <gtkmm/stock.h>#include <glibmm/timer.h>#include <glibmm/convert.h>#if GTKMM_VERSION >= 24#include <gtk/gtkfilechooser.h>#include <glibmm/slisthandle.h>#endif#include "dialogs.h"#include "gpl.h"#ifdef ENABLE_NLS#include <libintl.h>#endifFileReadSelectDialog::FileReadSelectDialog(int size, bool multi_files, Gtk::Window& window): FileDialog(gettext("File to fax")), in_exec_loop(false), standard_size(size), view_button(gettext("View")), parent(window) { if (multi_files) set_select_multiple(); if (!prog_config.homedir.empty()) { std::string temp(prog_config.homedir);#if GTKMM_VERSION >= 24 temp += "/faxout"; // gtkmm-2.4 incorrectly uses Glib::ustring for filename parameters // where the contents will not necessarily comprise valid UTF-8, so // use the raw C function gtk_file_chooser_set_current_folder(static_cast<Gtk::FileChooser*>(this)->gobj(), (const gchar*)temp.c_str());#else temp += "/faxout/"; set_filename(temp);#endif }#if GTKMM_VERSION >= 24 get_action_area()->set_homogeneous(true); get_action_area()->pack_start(view_button, false, false, 0); view_button.signal_clicked().connect(sigc::mem_fun(*this, &FileReadSelectDialog::view_file)); Gtk::Button* button_p = manage(new Gtk::Button(Gtk::Stock::CANCEL)); get_action_area()->pack_start(*button_p, false, false, 0); button_p->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &FileReadSelectDialog::selected), false)); button_p->set_flags(Gtk::CAN_DEFAULT); button_p = manage(new Gtk::Button(Gtk::Stock::OK)); get_action_area()->pack_start(*button_p, false, false, 0); button_p->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &FileReadSelectDialog::selected), true)); button_p->set_flags(Gtk::CAN_DEFAULT); view_button.set_flags(Gtk::CAN_DEFAULT);#else hide_fileop_buttons(); get_action_area()->set_homogeneous(false); Gtk::Label* label_p = manage (new Gtk::Label); get_action_area()->pack_start(*label_p, true, false, 0); get_action_area()->pack_start(view_button, false, false, 0); view_button.signal_clicked().connect(sigc::mem_fun(*this, &FileReadSelectDialog::view_file)); get_ok_button()->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &FileReadSelectDialog::selected), true)); get_cancel_button()->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &FileReadSelectDialog::selected), false)); view_button.set_flags(Gtk::CAN_DEFAULT);#endif set_transient_for(parent); set_type_hint(Gdk::WINDOW_TYPE_HINT_DIALOG); parent.set_sensitive(false); set_modal(true); set_position(Gtk::WIN_POS_CENTER_ON_PARENT); set_icon(prog_config.window_icon_r); show_all();#if GTKMM_VERSION < 24 // now get and set the button size for view_button view_button.set_size_request(get_cancel_button()->get_width(), get_cancel_button()->get_height());#endif}std::pair<const char*, char* const*> FileReadSelectDialog::get_view_file_parms(void) { std::vector<std::string> view_parms; std::string view_cmd; std::string view_name; std::string::size_type end_pos; try { // lock the Prog_config object to stop it being accessed in // FaxListDialog::get_ps_viewer_parms() while we are accessing it here // (this is ultra cautious as it is only copied/checked for emptiness // there, and the GUI interface is insensitive if we are here) Glib::Mutex::Lock lock(*prog_config.mutex_p); view_cmd = Glib::filename_from_utf8(prog_config.ps_view_cmd); } catch (Glib::ConvertError&) { write_error("UTF-8 conversion error in FileReadSelectDialog::get_view_file_parms()\n"); return std::pair<const char*, char* const*>(0,0); } if ((end_pos = view_cmd.find_first_of(' ')) != std::string::npos) { // we have parms view_name.assign(view_cmd, 0, end_pos); view_parms.push_back(view_name); // find start of next parm std::string::size_type start_pos = view_cmd.find_first_not_of(' ', end_pos); while (start_pos != std::string::npos) { end_pos = view_cmd.find_first_of(' ', start_pos); if (end_pos != std::string::npos) { view_parms.push_back(view_cmd.substr(start_pos, end_pos - start_pos)); start_pos = view_cmd.find_first_not_of(' ', end_pos); // prepare for next interation } else { view_parms.push_back(view_cmd.substr(start_pos, view_cmd.size() - start_pos)); start_pos = end_pos; } } } else { // just a view command without parameters to be passed view_name = view_cmd; view_parms.push_back(view_name); } view_parms.push_back(get_filename_string()); char** exec_parms = new char*[view_parms.size() + 1]; char** temp_pp = exec_parms; std::vector<std::string>::const_iterator iter; for (iter = view_parms.begin(); iter != view_parms.end(); ++iter, ++temp_pp) { *temp_pp = new char[iter->size() + 1]; std::strcpy(*temp_pp, iter->c_str()); } *temp_pp = 0; char* prog_name = new char[view_name.size() + 1]; std::strcpy(prog_name, view_name.c_str()); return std::pair<const char*, char* const*>(prog_name, exec_parms);}void FileReadSelectDialog::view_file(void) { // check pre-conditions std::string filename(get_filename_string()); if (filename.empty() || filename[filename.size() - 1] == '/' || access(filename.c_str(), R_OK)) { beep(); return; } // get the arguments for the exec() call below (because this is a // multi-threaded program, we must do this before fork()ing because // we use functions to get the arguments which are not async-signal-safe) std::pair<const char*, char* const*> view_file_parms(get_view_file_parms()); if (view_file_parms.first) { // this will be 0 if get_view_file_parms() // threw a Glib::ConvertError) pid_t pid = fork(); if (pid == -1) { write_error("Fork error - exiting\n"); std::exit(FORK_ERROR); } if (!pid) { // child process - as soon as everything is set up we are going to do an exec() connect_to_stderr(); execvp(view_file_parms.first, view_file_parms.second); // if we reached this point, then the execvp() call must have failed // report error and end process - use _exit() and not exit() write_error("Can't find the postscript viewer program - please check your installation\n" "and the PATH environmental variable\n"); _exit(0); } // end of view program process // release the memory allocated on the heap for // the redundant view_file_parms // we are in the main parent process here - no worries about // only being able to use async-signal-safe functions delete_parms(view_file_parms); }}void FileReadSelectDialog::delete_parms(std::pair<const char*, char* const*> parms_pair) { delete[] parms_pair.first; char* const* temp_pp = parms_pair.second; for(; *temp_pp != 0; ++temp_pp) { delete[] *temp_pp; } delete[] parms_pair.second;}std::vector<Glib::ustring> FileReadSelectDialog::exec(void) { in_exec_loop = true; Gtk::Main::run(); return result;}std::string FileReadSelectDialog::get_filename_string(void) {#if GTKMM_VERSION >= 24 // in gtkmm-2.4, Gtk::FileChooser::get_filename() erroneously // returns a Glib::ustring object without converting // to UTF-8 - we therefore need to get the gchar* string to handle // correctly whatever the filesystem codeset is char* file_name = (char*)gtk_file_chooser_get_filename(static_cast<Gtk::FileChooser*>(this)->gobj()); if (file_name) { std::string return_val(file_name); g_free((gchar*)file_name); return return_val; } // no filename - return empty string return std::string();#else return get_filename();#endif}std::vector<std::string> FileReadSelectDialog::get_filenames_vec(void) {#if GTKMM_VERSION >= 24 // in gtkmm-2.4, Gtk::FileChooser::get_filenames() erroneously // returns a Glib::SListHandle<Glib::ustring> without converting // to UTF-8 - we therefore need to get a Glib::SListHandle<std::string> // object to handle correctly whatever the filesystem codeset is return Glib::SListHandle<std::string>( gtk_file_chooser_get_filenames(static_cast<Gtk::FileChooser*>(this)->gobj()), Glib::OWNERSHIP_DEEP);#else return get_selections();#endif}void FileReadSelectDialog::selected(bool accept) { if (accept) { std::vector<std::string> vec(get_filenames_vec()); try { std::transform(vec.begin(), vec.end(), back_inserter(result), Glib::filename_to_utf8); } catch (Glib::ConvertError&) { write_error("UTF-8 conversion error in FileReadSelectDialog::selected()\n"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -