Skip to content

Commit

Permalink
Merge PR #2497: Porting to Gtk::Application - part 1
Browse files Browse the repository at this point in the history
Porting to Gtk::Application - part 1
  • Loading branch information
ice0 authored Jan 30, 2022
2 parents ad03efe + 0db1342 commit 61eba07
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 589 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ compile

# ------------------------------------------------------------------------

cmake-build

ETL/ETL.pc.in
ETL/ETL/etl_profile.h
ETL/ETL/etl_profile_.h
Expand Down
4 changes: 2 additions & 2 deletions synfig-core/src/synfig/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ bool retrieve_modules_to_load(String filename,std::list<String> &modules_to_load
return true;
}

synfig::Main::Main(const synfig::String& basepath,ProgressCallback *cb):
synfig::Main::Main(const synfig::String& rootpath,ProgressCallback *cb):
ref_count_(synfig_ref_count_)
{
if(ref_count_.count())
Expand All @@ -202,7 +202,7 @@ synfig::Main::Main(const synfig::String& basepath,ProgressCallback *cb):

// Paths

root_path = etl::dirname(basepath);
root_path = rootpath;
bin_path = root_path + ETL_DIRECTORY_SEPARATOR + "bin";
share_path = root_path + ETL_DIRECTORY_SEPARATOR + "share";
locale_path = share_path + ETL_DIRECTORY_SEPARATOR + "locale";
Expand Down
2 changes: 1 addition & 1 deletion synfig-core/src/synfig/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Main
synfig::String lib_path;
synfig::String lib_synfig_path;

Main(const synfig::String& basepath,ProgressCallback *cb=0);
Main(const synfig::String& rootpath,ProgressCallback *cb=nullptr);
~Main();

const etl::reference_counter& ref_count()const { return ref_count_; }
Expand Down
10 changes: 3 additions & 7 deletions synfig-core/src/tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,10 @@ int main(int argc, char* argv[])

const std::string binary_path =
SynfigToolGeneralOptions::instance()->get_binary_path();
const std::string root_path = get_absolute_path(binary_path + "/../../");

#ifdef ENABLE_NLS
/*bst::filesystem::path locale_path =
binary_path.parent_path().parent_path();*/
std::string locale_path = get_absolute_path(binary_path + "../../share/locale");
//locale_path = locale_path/"share"/"locale";
//bindtextdomain("synfig", Glib::locale_from_utf8(locale_path.string()).c_str() );
std::string locale_path = root_path + "/share/locale";
bindtextdomain("synfig", Glib::locale_from_utf8(locale_path).c_str() );
bind_textdomain_codeset("synfig", "UTF-8");
textdomain("synfig");
Expand Down Expand Up @@ -272,8 +269,7 @@ int main(int argc, char* argv[])
// Synfig Main initialization needs to be after verbose and
// before any other where it's used
Progress p(binary_path.c_str());
//synfig::Main synfig_main(binary_path.parent_path().string(), &p);
synfig::Main synfig_main(get_absolute_path(binary_path + "/.."), &p);
synfig::Main synfig_main(root_path, &p);

// Info options -----------------------------------------------
parser.process_info_options();
Expand Down
2 changes: 0 additions & 2 deletions synfig-studio/po/POTFILES.in
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,6 @@ src/gui/iconcontroller.cpp
src/gui/iconcontroller.h
src/gui/instance.cpp
src/gui/instance.h
src/gui/ipc.cpp
src/gui/ipc.h
src/gui/keymapsettings.cpp
src/gui/keymapsettings.h
src/gui/main.cpp
Expand Down
1 change: 0 additions & 1 deletion synfig-studio/src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ target_sources(synfigstudio
"${CMAKE_CURRENT_LIST_DIR}/duckmatic.cpp"
"${CMAKE_CURRENT_LIST_DIR}/iconcontroller.cpp"
"${CMAKE_CURRENT_LIST_DIR}/instance.cpp"
"${CMAKE_CURRENT_LIST_DIR}/ipc.cpp"
"${CMAKE_CURRENT_LIST_DIR}/keymapsettings.cpp"
"${CMAKE_CURRENT_LIST_DIR}/onemoment.cpp"
"${CMAKE_CURRENT_LIST_DIR}/pluginmanager.cpp"
Expand Down
2 changes: 0 additions & 2 deletions synfig-studio/src/gui/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ OTHER_HH = \
localization.h \
iconcontroller.h \
instance.h \
ipc.h \
keymapsettings.h \
onemoment.h \
pluginmanager.h \
Expand Down Expand Up @@ -82,7 +81,6 @@ OTHER_CC = \
duckmatic.cpp \
iconcontroller.cpp \
instance.cpp \
ipc.cpp \
keymapsettings.cpp \
onemoment.cpp \
pluginmanager.cpp \
Expand Down
104 changes: 61 additions & 43 deletions synfig-studio/src/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <giomm/file.h>
#include <glibmm/convert.h>
#include <glibmm/init.h>
#include <glibmm/main.h>
#include <glibmm/miscutils.h>
#include <glibmm/timer.h>
#include <glibmm/spawn.h>
Expand Down Expand Up @@ -100,7 +101,6 @@
#include <gui/docks/dock_toolbox.h>

#include <gui/instance.h>
#include <gui/ipc.h>
#include <gui/localization.h>
#include <gui/modules/mod_palette/mod_palette.h>
#include <gui/onemoment.h>
Expand Down Expand Up @@ -152,13 +152,15 @@
#include <thread>

#ifdef _WIN32

#include <gui/main_win32.h>

#define WINVER 0x0500
#include <windows.h>
#include <gdk/gdkwin32.h>
#define mkdir(x,y) mkdir(x)

//#define USE_WIN32_FILE_DIALOGS 1
#ifdef USE_WIN32_FILE_DIALOGS
#include <gdk/gdkwin32.h>
#include <cstring>
static OPENFILENAME ofn={};
#endif // USE_WIN32_FILE_DIALOGS
Expand Down Expand Up @@ -241,7 +243,6 @@ etl::handle<CanvasView> App::selected_canvas_view;
studio::About *studio::App::about = nullptr;
studio::AutoRecover *studio::App::auto_recover = nullptr;
studio::DeviceTracker *studio::App::device_tracker = nullptr;
static studio::IPC *ipc = nullptr;
studio::MainWindow *studio::App::main_window = nullptr;

studio::Dialog_Color *studio::App::dialog_color;
Expand Down Expand Up @@ -1362,15 +1363,61 @@ App::get_default_accel_map()

return default_accel_map;
}
Glib::RefPtr<App> App::instance() {
static Glib::RefPtr<studio::App> app_reference = Glib::RefPtr<App>(new App());
return app_reference;
}

/* === M E T H O D S ======================================================= */
App::App()
: Gtk::Application("org.synfig.SynfigStudio", Gio::APPLICATION_HANDLES_OPEN)
{
add_main_option_entry(Gio::Application::OPTION_TYPE_BOOL, "console", 'c', N_("Opens a console that shows some Synfig Studio output"));
signal_handle_local_options().connect(sigc::mem_fun(*this, &App::on_handle_local_options));
}

int App::on_handle_local_options(const Glib::RefPtr<Glib::VariantDict> &options)
{
// To continue, return -1 to let the default option processing continue.
// https://developer-old.gnome.org/glibmm/unstable/classGio_1_1Application.html#a46b21ab9629b123b7524fe906290d32b
if (!options)
return -1;
if (options->contains("console")) {
#ifdef _WIN32
redirectIOToConsole();
#else
warning(_("Option --console is valid on MS Windows only"));
#endif
}
return -1;
}

App::App(const synfig::String& basepath, int *argc, char ***argv):
Gtk::Main(argc,argv)
void App::on_activate()
{
if (!getenv("SYNFIG_DISABLE_AUTOMATIC_DOCUMENT_CREATION") && !get_selected_instance())
new_instance();

Glib::init(); // need to use Gio functions before app is started
app_base_path_=etl::dirname(basepath);
if (get_windows().size() == 0) {
add_window(*main_window);
} else {
main_window->present();
}
}

void App::on_open(const type_vec_files &files, const Glib::ustring &hint)
{
if (get_windows().size() == 0)
add_window(*main_window);

OneMoment one_moment;
for (const auto& file : files) {
open(file->get_path());
}
}

void App::init(const synfig::String& rootpath)
{
app_base_path_=rootpath;

// Set ui language
load_language_settings();
Expand Down Expand Up @@ -1413,8 +1460,6 @@ App::App(const synfig::String& basepath, int *argc, char ***argv):
}


ipc=new IPC();

if(!SYNFIG_CHECK_VERSION())
{
std::cerr << "FATAL: Synfig Version Mismatch" << std::endl;
Expand All @@ -1440,7 +1485,7 @@ App::App(const synfig::String& basepath, int *argc, char ***argv):
SuperCallback studio_init_cb(splash_screen.get_callback(),9000,10000,10000);

// Initialize the Synfig library
try { synfigapp_main=etl::smart_ptr<synfigapp::Main>(new synfigapp::Main(basepath,&synfig_init_cb)); }
try { synfigapp_main=etl::smart_ptr<synfigapp::Main>(new synfigapp::Main(rootpath,&synfig_init_cb)); }
catch(std::runtime_error &x)
{
get_ui_interface()->error(strprintf("%s\n\n%s", _("Failed to initialize synfig!"), x.what()));
Expand Down Expand Up @@ -1654,7 +1699,6 @@ App::App(const synfig::String& basepath, int *argc, char ***argv):

studio_init_cb.amount_complete(9900,10000);

bool opened_any = false;
if (!getenv("SYNFIG_DISABLE_AUTO_RECOVERY") && auto_recover->recovery_needed())
{
splash_screen.hide();
Expand All @@ -1678,9 +1722,6 @@ App::App(const synfig::String& basepath, int *argc, char ***argv):
_("Synfig Studio has attempted to recover from a previous crash. "
"The files just recovered are NOT YET SAVED."),
_("Thanks"));

if (number_recovered)
opened_any = true;
}
else
{
Expand All @@ -1689,22 +1730,6 @@ App::App(const synfig::String& basepath, int *argc, char ***argv):
splash_screen.show();
}

// Look for any files given on the command line,
// and load them if found.
for(;*argc>=1;(*argc)--)
if((*argv)[*argc] && (*argv)[*argc][0]!='-')
{
studio_init_cb.task(_("Loading files..."));
splash_screen.hide();
open((*argv)[*argc]);
opened_any = true;
splash_screen.show();
}

// if no file was specified to be opened, create a new document to help new users get started more easily
if (!opened_any && !getenv("SYNFIG_DISABLE_AUTOMATIC_DOCUMENT_CREATION"))
new_instance();

studio_init_cb.task(_("Done."));
studio_init_cb.amount_complete(10000,10000);

Expand Down Expand Up @@ -1769,8 +1794,6 @@ App::~App()

delete state_manager;

delete ipc;

delete auto_recover;

delete about;
Expand Down Expand Up @@ -2298,7 +2321,7 @@ App::quit()
return;
process_all_events();

Gtk::Main::quit();
App::instance()->remove_window(*main_window);

get_ui_interface()->task(_("Quit Request sent"));
}
Expand All @@ -2310,9 +2333,6 @@ App::show_setup()
dialog_setup->show();
}

gint Signal_Open_Ok (GtkWidget */*widget*/, int *val){*val=1; return 0;}
gint Signal_Open_Cancel(GtkWidget */*widget*/, int *val){*val=2; return 0;}

bool
App::dialog_open_file_ext(const std::string &title, std::vector<std::string> &filenames, std::string preference, bool allow_multiple_selection)
{
Expand Down Expand Up @@ -4352,13 +4372,11 @@ studio::App::setup_changed()
}

void
studio::App::process_all_events(long unsigned int us)
studio::App::process_all_events()
{
Glib::usleep(us);
while(studio::App::events_pending()) {
while(studio::App::events_pending())
studio::App::iteration(false);
Glib::usleep(us);
const auto& ctx = Glib::MainContext::get_default();
while(ctx->pending()) {
ctx->iteration(false);
}
}

Expand Down
19 changes: 11 additions & 8 deletions synfig-studio/src/gui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <ETL/smart_ptr>

#include <gtkmm/box.h>
#include <gtkmm/main.h>
#include <gtkmm/uimanager.h>

#include <gui/iconcontroller.h>
Expand Down Expand Up @@ -115,15 +114,13 @@ class Dock_Navigator;
class Dock_LayerGroups;
class Dock_SoundWave;

class IPC;

class Module;

class StateManager;

class WorkspaceHandler;

class App : public Gtk::Main, private IconController
class App : public Gtk::Application, private IconController
{
friend class Preferences;
friend class Dialog_Setup;
Expand All @@ -133,6 +130,8 @@ class App : public Gtk::Main, private IconController
*/

public:
static Glib::RefPtr<App> instance();
void init(const synfig::String& basepath);

struct Busy
{
Expand Down Expand Up @@ -166,8 +165,6 @@ class App : public Gtk::Main, private IconController
static Dock_History *dock_history;
static Dock_Canvases *dock_canvases;
static Dock_LayerGroups *dock_layer_groups;
static IPC *ipc;
*/

etl::smart_ptr<synfigapp::Main> synfigapp_main;
Expand Down Expand Up @@ -309,13 +306,19 @@ class App : public Gtk::Main, private IconController
static void add_recent_file(const std::string &filename, bool emit_signal);
static bool dialog_open_file_ext(const std::string &title, std::vector<std::string> &filenames, std::string preference, bool allow_multiple_selection);

App();

protected:
int on_handle_local_options(const Glib::RefPtr<Glib::VariantDict>& options);
void on_activate() override;
void on_open(const type_vec_files& files, const Glib::ustring& hint) override;

/*
-- ** -- P U B L I C M E T H O D S -----------------------------------------
*/

public:

App(const synfig::String& basepath, int *argc, char ***argv);
virtual ~App();

/*
Expand Down Expand Up @@ -479,7 +482,7 @@ class App : public Gtk::Main, private IconController
// This fixes bug 1890020
static void setup_changed();

static void process_all_events(long unsigned int us = 1);
static void process_all_events();
static bool check_python_version( std::string path);
}; // END of class App

Expand Down
Loading

0 comments on commit 61eba07

Please sign in to comment.