Saturday, December 29, 2007

Adding a menu to KMainWindow programmatically

First of all, let me stress that I'm not an experienced KDE programmer: my only KDE application, for now, is SourceHighlightIDE, a sort of IDE for developing GNU Source-highlight language definition files. While I was adding new features to this program, for the next release, I wanted to add the typical "Window" menu that permits tile all the windows in the workspace or cascade them, or selecting a specific window (SourceHighlightIDE is a MDI application). I then simply added this code to the KMainWindow subclass:

windowsMenu = new KPopupMenu( this );
connect( windowsMenu, SIGNAL( aboutToShow() ),
this, SLOT( windowsMenuAboutToShow() ) );
menuBar()->activateItemAt(-1); //needed as insertItem might crash if a menu is activated

// position: 2, means the third menu (the fourth one is Help)
menuBar()->insertItem( "&Window", windowsMenu, -1, 2);

However, the "Window" menu was never added to the application.

Actually, I know that one should add additional menus to the .rc file, e.g.,
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<kpartgui name="sourcehighlightide" version="1">
<Menu name="custom"><text>C&amp;ustom</text>
<Action name="custom_action" />

but I wanted a way to add the menu programmaticaly, so that I would not depend on the .rc file that must be installed.

Actually, the code added by me above works, but you must make sure to execute it after the call to setupGUI().

Hope this saves some headache to others wanting the same thing :-)

Saturday, December 15, 2007

doublecpp (dynamic overloading in C++)

Doublecpp is a preprocessor for C++ that handles a new linguistic construct for defining branches of a multi-method. The "right'' branch of such a method will be selected dynamically at run-time according to the actual type of the object on which the method is invoked and to the actual type of the first argument: double dispatch.

This way, you will have dynamic overloading (but only on the first argument, for the other arguments standard static overloading will be adopted).

The home page of the project is And the documentation can be found on-line at this link.

Doublecpp is based on our research on formalization and implementation of dynamic overloading in languages providing only static overloading. In particular, doublecpp will take as input a program written in C++ extended with the linguistic construct for dynamic overloading and will produce in output a standard C++ program with the same semantics.

The idea of the translation is based on the Visitor pattern, but it does not require the programmer to manually implement such pattern; furthermore, it performs all the type checks to assure type safety; thus, it is not merely an automatic implementation of this pattern.

Such implementation avoids down-casts and it is efficient since it uses dynamic binding twice, i.e., dynamic overloading takes place in constant time (other implementations rely on RTTI checks and require to inspect the class hierarchy, thus their complexity depends on the depth and width of the class hierarchy).

More importantly, our implementation of dynamic overloading is type safe: once the program is successfully compiled, no run-time errors (exceptions) will be raised during the execution of the program. Thus, all possible (static and dynamic) ambiguities are ruled out during the type checking.

The papers concerning this research are the following:

Translating Double-Dispatch into Single-Dispatch
Lorenzo Bettini, Sara Capecchi, Betti Venneri.
Proceedings of the Second Workshop on Object Oriented Developments (WOOD 2004). pp. 59-78. ENTCS vol. 138 no. 2. Elsevier. 2005. abstract doubledisp.pdf bibtex
Double Dispatch in C++
Lorenzo Bettini, Sara Capecchi, Betti Venneri.
Software - Practice and Experience. pp. 581 - 613. vol. 36 no. 6. 2006. abstract bibtex