[SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Do you like to promote your wxWidgets based application or component!? Post it here and let's see what the critics have to say. Also, if you found that ONE wx component the world needs to know about, put it here for future reference.
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

[SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by T-Rex »

Hi.

I can see that there is still a lot of topics related to usage of shared libs or plugins with wxWidgets apps on different platform.
For Windows it's not hard to implement such app but on Linux and OS X this may be quite tricky (especially when you are not planning to use installer for your app and create the app which will work on different machines without need to recompile it).

So, specially for those, who still has problems with implementation of plugins for wxWidgets apps, I created the sample which compiles and runs on all 3 platforms, has 2 types of plugins (with and without GUI), creates the relocatable application where all plugins and other libs search for dependencies using relative paths which means that you don't need to rebuild the app on different machines to make it work.

You can find the complete source code at GitHub: Modular wxWidgets Application with Plugins

The project is under development since I'm writing an article about this in parallel, so you are welcome to follow the project at GitHub and try the new versions.

---
Features which I plan to add in nearest updates:
  • Embedding of wxWidgets libs into OS X Bundle
  • Embedding of wxWidgets libs into application's distro for Linux
  • Document/View: Support of different file formats using plugins
  • Communication between plugins
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by evstevemd »

That will be great! I am planning the same on my wx series. So I will copy anything I miss in my design.

May I ask a favour, that you update the code in your tutorial for getting Mac Adresses?
http://wxwidgets.info/cross-platform-wa ... r-machine/
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by evstevemd »

Hi T-Rex, can you briefly explain how your app works?
Not everything but just a flow of things.
I'm too lazy to figure it myself thru codes ;)
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by T-Rex »

There are 2 types of plugins: GUI and non-GUI. The interface to interact with plugins is declared in 2 separate DLLs (wxGUIPluginBase, wxNonGUIPluginBase). All specific implementations of plugins reference these DLLs. Also a host app references it and uses the base class for accessing plugins' functionality.
There is a static library (wxModulerCore) which contains the class which has general implementation of plugin loader. It scans folder for DLLs and tries to load plugins (plugin objects) from them.
The host app has a SampleModularCore class derived from wxModularCore which loads GUI plugins from `plugins\gui` subfolder and non-GUI plugins from `plugins\nongui` subfolder.
Main window of wxModularHost app loads notebook tabs from GUI plugins.
Also there is a sample code which shows the data exchange between plugins which don't know about each other.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by evstevemd »

Thanks. I understand =D>
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by xaviou »

Hi

I tried to build this project with MinGW MakeFiles (obtained with cmake).
I had to comment the line #75 in the main CMakeList.txt file :

Code: Select all

#	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /wd4996 /Ym0x20000000")
After that, there are still errors at compil time :

Code: Select all

Scanning dependencies of target wxModularCore
[  3%] Building CXX object C:/Dev/wxModularApp/wxModularCore/Win/CMakeFiles/wxModularCore.dir/wxModularCore.cpp.obj
[  7%] Building CXX object C:/Dev/wxModularApp/wxModularCore/Win/CMakeFiles/wxModularCore.dir/wxModularCoreSettings.cpp.obj
[ 11%] Building CXX object C:/Dev/wxModularApp/wxModularCore/Win/CMakeFiles/wxModularCore.dir/C_/Dev/wxModularApp/include/stdwx.cpp.obj
g++.exe: error: /YcC:/Dev/wxModularApp/build/../include/stdwx.h: Invalid argument
C:\Dev\wxModularApp\wxModularCore\Win\CMakeFiles\wxModularCore.dir\build.make:105: recipe for target 'C:/Dev/wxModularApp/wxModularCore/Win/CMakeFiles/wxModularCore.dir/C_/Dev/wxModularApp/include/stdwx.cpp.obj' failed
mingw32-make[2]: *** [C:/Dev/wxModularApp/wxModularCore/Win/CMakeFiles/wxModularCore.dir/C_/Dev/wxModularApp/include/stdwx.cpp.obj] Error 1
CMakeFiles\Makefile2:127: recipe for target 'C:/Dev/wxModularApp/wxModularCore/Win/CMakeFiles/wxModularCore.dir/all' failed
mingw32-make[1]: *** [C:/Dev/wxModularApp/wxModularCore/Win/CMakeFiles/wxModularCore.dir/all] Error 2
Makefile:74: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Then I tried to manually re-create Code::Blocks projects and workspace files to build it, and I had to make some changes to the "include/stdwx.h" file to use extern "C" instead of WXEXPORT (line 4).

Code: Select all

#if !defined(__GNUWIN32__) && (defined(WIN32) || defined(WINDOWS))
With these modifications, all build fine.

Regards
Xav'
My wxWidgets stuff web page : X@v's wxStuff
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by xaviou »

Hi again....

Having a look at the code, I discovered the wxModularCore::GetPluginExt().
Why didn't you used the wxWidgets's build-in function from wxDynamicLibrary wxDynamicLibrary::GetDllExt() ?

Regards
Xav'
My wxWidgets stuff web page : X@v's wxStuff
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by T-Rex »

Regarding plugin extension, we are using the custom extension for plugins on Windows, but yes, I will replace the implementation with the call of wxWidgets built-in function in base class. Thanks for pointing.
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by T-Rex »

As for modifications for MinGW, you can create a pull request at GitHub if you want, this will simplify the integration of these changes.
iwbnwif
Super wx Problem Solver
Super wx Problem Solver
Posts: 282
Joined: Tue Mar 19, 2013 8:52 pm

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by iwbnwif »

T-Rex, many thanks for this example and xaviou for the hints on building the sample.

I have created a workspace in CodeLite and successfully built the 7 projects as follows:
  • SampleGUIPlugin1 = DLL
    SampleGUIPlugin2 = DLL
    SampleNonGuiPlugin = DLL
    wxGuiPluginBase = DLL
    wxModularCore = Static Library (.a)
    wxModularHost = EXE
    wxNonGuiPluginBase = DLL
But I have a few questions:

1. Why are the two 'base' projects organized as DLLs and what would I need to do to convert them to static libraries?

2. [Sorry for the basic event question :oops:] In SampleGuiPluginWindow1::OnSENDEVENTBUTTONClick an event is created and sent. This event is picked up by SampleGuiPluginWindow2 by being bound to a method in that class. Would any class that binds to this event also receive the event? (i.e. is the event 'broadcast' to whoever is listening?)

3. Is there a reason to use DECLARE_EXPORTED_EVENT_TYPE and DEFINE_EVENT_TYPE instead of wxDECLARE_EXPORTED_EVENT and wxDEFINE_EVENT?

My interest is in sending events from a DLL to a 'host' application in a thread safe manner. The DLL is not a plugin but is 'normally' linked (i.e. the exports are declared with WXEXPORT.

Thank you for any explanations!
wxWidgets 3.1.2, MinGW64 8.1.0, g++ 8.1.0, Ubuntu 19.04, Windows 10, CodeLite + wxCrafter
Some people, when confronted with a GUI problem, think "I know, I'll use Eclipse RCP". Now they have two problems.
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by T-Rex »

1. The code from `base` DLLs are used by plugins and by main app. It's better to have only one `instance` of compiled implementation of these classes than compile it into every plugin and into the host app. Also if you declare the `base` class as DECLARE_DYNAMIC_CLASS() and want to use the RTTI, then, if 2modules have its declaration and implementation, as far as I remember, you will get the error which states that class is already defined. Also there can be problems with static methods which w.g. should work as factories and generate the singleinstance objects. In fact, using DLLs for common code is the main idea of creating plugin system. We use shared build of wxWidgets with shared runtime because of this. If you use the static runtime, you will get multiple instances of wxWidgets - in app and in each plugin.

2. We pass the event handler of main window to plugin and event will be propagated to all event handlers in event handler chain. This includes the child windows (also the child window from second plugin). Not sure about `any` class, but for child windows should work. Or you can use PushEventHandler/PopEventHandler if you create the stand-alone event handler object.

3. No specific reason of this. Also take a look at include/wx/event.h http://screencast.com/t/IpAVB0xxMJI
iwbnwif
Super wx Problem Solver
Super wx Problem Solver
Posts: 282
Joined: Tue Mar 19, 2013 8:52 pm

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by iwbnwif »

Thank you for the fast response.

Ah, yes of course - I have suffered from the class already defined problem before!

I created a SampleGuiPlugin3 and it also receives the event, providing I put event.Skip() in the SampleGuiPlugin2 event handling method.

All is clear now and all is working :) thanks again!
wxWidgets 3.1.2, MinGW64 8.1.0, g++ 8.1.0, Ubuntu 19.04, Windows 10, CodeLite + wxCrafter
Some people, when confronted with a GUI problem, think "I know, I'll use Eclipse RCP". Now they have two problems.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by evstevemd »

Care to attach CodeLite project and files to a Lazy guy :p
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by T-Rex »

No CodeLite, sorry. Only generators supported by CMake.
iwbnwif
Super wx Problem Solver
Super wx Problem Solver
Posts: 282
Joined: Tue Mar 19, 2013 8:52 pm

Re: [SAMPLE] wxWidgets app With Plugins (Windows/Linux/Mac)

Post by iwbnwif »

Attached is a zip file of the codelite workspace. I hope it contains everything you need.
Attachments
modular_app.zip
Codelite workspace containing projects
(43.3 KiB) Downloaded 304 times
wxWidgets 3.1.2, MinGW64 8.1.0, g++ 8.1.0, Ubuntu 19.04, Windows 10, CodeLite + wxCrafter
Some people, when confronted with a GUI problem, think "I know, I'll use Eclipse RCP". Now they have two problems.
Post Reply