|
ILOG Views Controls
ILOG Studio generates very basic code for your application. You have to modify it by adding your own code to define the application's behavior. Step 4 shows how to use the code generated by ILOG Studio.
In normal, iterative development, you design the graphical interface, generate and modify the code, come back to the interface design, regenerate the code, modify it again, and so on. This process affects the way you use ILOG Studio.
In this part of the tutorial, you will add some lines of code to make your application run properly.
- When the Open button is pressed, it opens the second panel (making it visible).
- When the Close button is pressed, the second panel disappears. It is not erased -- it is just invisible.
Main Panel
In Step 1, we specified a callback function named OpenPress for the Open button. A method named OpenPress has been generated in the panel source file (MainPanel.cpp). It is called automatically when the button is pressed.
void
MainPanel::OpenPress(IlvGraphic* g)
{
const char* className = g->className();
IlvPrint(" %s : OpenPress method ...",className);
} |
This method prints a small message using the IlvPrint function (on standard output or in a popup window).
You will find the same text for the ClosePress method, called when the Close button is pressed.
Your application should have a different behavior. You can change the generated OpenPress method (in MainPanel.cpp), but this isn't the way it's supposed to be used. If you go back to ILOG Studio later to change the user interface, you may have to regenerate the code because of these modifications. The main panel source and header files will be regenerated, erasing the existing MainPanel.cpp -- and the changes you made in the OpenPress method.
There is a way to modify and enrich generated code without erasing it. The key is changing code-generation options in ILOG Studio. Simply ask Studio to generate callback declarations in the panel header file (MainPanel.h), but ask it NOT to generate callback definitions. The callback methods will be defined in a separate source file, so they will not be erased when new code is generated. To do this, select the MainPanel icon in the Classes subwindow and click the Inspect Panel button
In the Panel Class Inspector, go to the Options page and uncheck the Callback Definitions box. You can now regenerate the code. If you look, you will see that the OpenPress and ClosePress methods are gone. If you try to compile code, you will get an error message because these methods are declared, but not defined.
All you need to do is create a new C++ source file (called MainPanelCallbacks.cpp, for example), and write your code for these methods. This is a simpler approach than creating a new class, but it is not as powerful, because you cannot change or add code in the panel constructor or destructor -- you can only change the callback code.
In MainPanelCallbacks, you can change callback code. For the OpenPress callback, make the second panel appear, and update the toggle to indicate that the second panel is visible.
| IlvApplication* appli = IlvApplication::GetApplication(this); |
First, you need to get a pointer on the Application instance. The GetApplication static function of the IlvApplication class provides it.
SecondPanel* panel = (SecondPanel*)appli->getPanel("SecondPanel");
if (!panel) return; |
The application knows all its panels, so you can get a pointer on the second panel with the getPanel function, providing the name of the panel. If the panel is not found with its name, a null pointer is returned. You can test it.
Now you just have to make it visible.
| IlvToggle* toggle = getToggleIndicator(); |
Now update the toggle. It was named ToggleIndicator when you designed the interface with ILOG Studio. An access function (getToggleIndicator) has been generated in the header panel file (MainPanel.h). You can use it here.
toggle->setState(IlvTrue);
reDrawObj(toggle); |
Change the state of the toggle to illustrate the state of the second panel, and redraw the toggle object to reflect the change.
void MainPanel::ClosePress(IlvGraphic* g)
{
IlvApplication* appli = IlvApplication::GetApplication(this);
SecondPanel* panel = (SecondPanel*)appli->getPanel("SecondPanel");
if (!panel) return;
panel->hide();
IlvToggle* toggle = getToggleIndicator();
toggle->setState(IlvFalse);
reDrawObj(toggle);
} |
Do the same for the ClosePress method. Instead of showing the second panel, hide it with the hide function, and instead of setting the toggle state to True, set it to False (unchecked).
See the entire code (MainPanelCallbacks.cpp).
Download Step 4 Executable.
Conclusion
This tutorial illustrates a complete procedure for interface design and code generation. The process is very efficient, even when adding your own code, taking the average ILOG Views developer only 5 to 10 minutes. The example is purposely simple, in order to help you understand every step of the process. For a more complete discussion, please refer to the ILOG Views Studio user's manual.
|