Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Howto] Portable GTK GUI on Windows
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
canduc17
l33t
l33t


Joined: 11 Oct 2005
Posts: 795
Location: Ferrara, Italy

PostPosted: Sun Nov 14, 2010 2:33 pm    Post subject: [Howto] Portable GTK GUI on Windows Reply with quote

(Italian version!!!)

I had to implement a little GUI program on Windows, and I wondered how to do it using open source software only...
I would like to have something portable, compilable on both Linux and Windows: so I started some experiment.
Here's few notes about what I discovered...I hope this could be helpful to someone out there.

I developed everything with GTK+, the Gnome libraries written in C which are also portable on Windows.
But I've worked with them indirectly using gtkmm, a wrapper that permits you to use GTK+ libraries programming in C++ rather than in C using a cleaner syntax and the potential of this object oriented language. This interface is used by very important open source projects such as Inkscape and Ardour.

I must use GCC as compiler of course: it exists a Windows porting inside the MinGW package.
This "Minimalistic GNU for Windows" offers also a Unix shell, really useful expecially to compile your software.
And finally the IDE: I've chosen Code::Blocks, pretty configurable and with a Windows version too.

Summarizing, I've downloaded and used the following packages:
  1. mingw-get-inst-20100909.exe (gcc, shell unix, ssh, vim editor, make...)
  2. codeblocks-10.05-setup.exe (version without MinGW inside it)
  3. gtkmm-win32-devel-2.22.0-1.exe (which also include porting of GTK+ libraries)

  • MinGW
    Launch the installer you can find at the url mentioned above and choose an installation path without blanks (the default C:\MinGW it's ok), otherwise you can have troubles.
    Choose C, C++ compilers and MSYS (the Unix shell): the installer will download and install what you've chosen.
    Finally add the string C:\MinGW\bin; to the environment variables Path (on Windows 7 it is in Control Panel --> System and Security --> System -->
    Advanced System Settings --> Environment Variables --> System Variables
    ).
    Open MSYS and type
    Code:
    gcc --version
    If this command is recognized and a version number is returned, the installation went fine and now you can compile as with your Gentoo Box: but the final product will be of course an .exe executable for Windows.

  • gtkmm
    With the installer described above, install every component in C:\gtkmm and check if in the variables environment Path it has been added automatically the string C:\gtkmm\bin.

    Now we can try our first little experiment.
    Let's take as example the files you can find here, inside gtkmm documentation: it uses Glade and GTK::Builder.
    Basically, instead "drawing" the GUI with C++, it is possible to create it in an easier way with Glade which produces an xml file that describes the graphical interface; this GUI is then loaded at runtime and "interpretated" by GTK::Builder. Using these tools, writing and debugging the GUI it is faster and requires less C++ code.
    So we have 2 files, main.cc (the real program) and basic.ui (the xml which describes the GUI). Let's write a simple Makefile:
    Code:
    CC=g++
    FLAGS=-Wall -g -O3 -DDEBUG
    GTKMMFLAGS=`pkg-config gtkmm-2.4 --cflags --libs`

    #Executable name:
    TARGET=gtkmmExample

    all: main.cc
       $(CC) $(FLAGS) *.cc -o $(TARGET) $(GTKMMFLAGS)
    clean:
       rm -f gtkmmExample
    and save these three files in a directory reachable by MSYS, for example C:\MinGW\msys\1.0\home\user\example.
    Open from Start menu the MinGW Shell (MSYS), enter the "example" directory and launch make: if you don't get errors, the executable gtkmmExample.exe has been created and it should be launchable with a simple double click from Windows File Manager.

  • Code::Blocks
    For very simple programs you can use Vim installed with MinGW, or another open source editor for Windows like SciTE. But if your projects are quite big, an IDE like Code::Blocks is more suitable.
    Use the installer described above: it will find GCC already installed with MinGW and it will set it as the default compiler.
    But to compile correctly you have to set the gtkmm library inclusion. Let's see how to do it, using the same example above.

    Open Code::Blocks, create an empty console project selecting Console Application --> Empty Project and save it in a directory such as C:\Users\user\Desktop\example.
    Copy main.cc and basic.ui in this directory: to add them, click with the right button on project's name and from the contextual menu select "Add Files...".

    Open Project --> Build options --> Compiler settings --> Other options and paste the string
    Code:
    `pkg-config --cflags gtkmm-2.4`
    After that open Project --> Build options --> Linker settings --> Other linker options and paste
    Code:
    `pkg-config --libs gtkmm-2.4`
    These operations must to be done twice, both for "Debug" and "Release" build targets (you can choose them at the up-right of the Build Options window).
    Now with the button with a gear and a green play on it you can compile and execute your little example.


That's all folks: now you can obtain the same version of a GUI program both on Linux and Windows!
Enjoy!!!

:::::::::: EDIT ::::::::::
Other useful notes, after a little bit more experience:
  1. Glade file inclusion
    If you do not want to carry your glade file with your executable to get it work, you have to include it inside the C++ code, defining it as a long char string.
    For example, at the beginning of the file we can make this global declaration:
    Code:
    char glade_xml[]="<?xml version=\"1.0\"?>\
    <interface>\
      <requires lib=\"gtk+\" version=\"2.16\"/>\
    ...
    <signal name=\"insert_text\" handler=\"on_textbuffer1_insert_text\" object=\"casa\" after=\"yes\"/>\
      </object>\
    </interface>";
    and call this array with
    Code:
    // Inside main() function:
    Gtk::Main kit(argc, argv);
    Glib::RefPtr<Gtk::Builder> refBuilder = Gtk::Builder::create();
    refBuilder->add_from_string(glade_xml);
    The only trouble is that before this inclusion, you have to escape every character like " or ??? and you have to put a \ at the end of every line.
    To solve this, let's use sed:
    Code:
    sed -e 's/"/\\"/g' basic.ui | sed -e 's/???/?\\?\\?/g' | sed -e 's/$/\\/g'
  2. Libraries static linking
    If you try to move your .exe compiled with Code::Blocks on another Windows machine that doesn't have the development environment described above, your program will not start, complaining about the missing of 2 dynamic libraries: libgcc_s_dw2-1.dll and libstdc++-6.dll.
    To avoid this you have to statically include these dlls in the executable: open your Code::Blocks project, go into Project --> Build options --> Linker settings --> Other linker options and add at the Debug and Release versions the following lines
    Code:
    -static-libgcc
    -static-libstdc++
  3. Code::Blocks development: execution without console
    In Code::Blocks, while with Debug version of your executable it is useful to have a console appearing behind your GUI, in the Release version this is really annoying.
    To eliminate it go into Project --> Properties --> Build Targets and for the Release version, in the Type menu choose GUI application.

_________________
A cow enters a supermarket, does an handstand and dies. (Public toilet graffito)
Back to top
View user's profile Send private message
tomk
Administrator
Administrator


Joined: 23 Sep 2003
Posts: 7219
Location: Sat in front of my computer

PostPosted: Mon Nov 15, 2010 5:11 pm    Post subject: Reply with quote

Moved from Documentation, Tips & Tricks to Off the Wall as it's not about Gentoo.
_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
doubleagent
Guru
Guru


Joined: 15 Apr 2005
Posts: 444
Location: 127.0.0.1

PostPosted: Mon Nov 15, 2010 5:36 pm    Post subject: Reply with quote

That seems...wrong, somehow.
_________________
shickapooka wrote:
i think they programmed [otw] based on a right-wing jewish-nigger-nazi, his gay, retarded, left-wing love slave with webbed feet, and their three headed cat that poops uncontrollably. the cat is also an apple fanboy
Back to top
View user's profile Send private message
shickapooka800
Guru
Guru


Joined: 05 Dec 2004
Posts: 304
Location: no

PostPosted: Mon Nov 15, 2010 5:41 pm    Post subject: Reply with quote

i've been too lazy to figure any of this shit out for development of quick and dirty windows apps at work. this is a nice little tip. i've just been making quick and dirty python scripts but my coworkers aren't exactly keen on executing a python script or editing it for hardcoded options. i might sit down and write a proper work utility now!
Back to top
View user's profile Send private message
canduc17
l33t
l33t


Joined: 11 Oct 2005
Posts: 795
Location: Ferrara, Italy

PostPosted: Mon Nov 15, 2010 8:08 pm    Post subject: Reply with quote

I understand you, shickapooka800: I've decided to experiment this after a little bit of python scripting, indeed.
But the majority of Windows users are not so used in scripts and command lines...They HAVE TO click on some graphical widget... :-D
And trying to please them, I've learnt a lot of interesting stuff...
_________________
A cow enters a supermarket, does an handstand and dies. (Public toilet graffito)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum