![]() |
Introduction to Development Tools |
cmyApps in cmyOS 1.0 work over the cmyOS Toolkit, which is included in the system through the cmyWidgets library.
Actually, a cmyOS application needs only two files: app.cmycode that will contain the visual part of the application (widgets), and events.cmycode that will contain the functions that will be called (if they exist) when main widgets are clicked, hovered or whatever the app developer wants.
Note that an app with only one file (app.cmycode) may exist, but we always recommend to use at least two files, because even with no events, events.cmycode can be useful to do some actions when the app is closed.
The cmyOS toolkits cosists in a group of Widgets, which are PHP+Javascript/AJAX classes prepared to receive instructions and communicate with the application. Making an application is as simple as choosing your widgets, grouping them through the addFriend method and developing the functions for the main widgets (e.g. what should the app do when a button is clicked).
The system will automatically call that functions, add the app to the Processes table so it can run in multiple instances (e.g. two or more opened documents in a text editor), and protect the app against external attacks.
A widget, as said previously, is a Class that can be called from any part of the application. Every widget have common values, and some specific values for each of them.
All widgets have some common keys, which are necessary for the widget to be created. You can check how does this keys work in the Hello World app example, after the list.
In this first example we’ll create a “Hello World” app, a simple application with a small window, a “Hello World” Label and a Textbox/Button in one side to modify the “Hello World” title.
<?php function HelloWorld_run($params=null) { // We create the window: $myWindow1 = new Window(array( 'name' => 'HelloWorld_wnd', 'father' => 'cmyApps', 'cent' => 1, 'width' => 250, 'height' => 150, 'title' => 'Hello World example' )); $myWindow1->show(); // We add the initial "Hello World!" Label: $myLabel1 = new Label(array( 'name' => 'HelloWorld_lbl', 'father' => 'HelloWorld_wnd_Content', 'x' => 20, 'y' => 20, 'text' => 'Hello World!' )); $myLabel1->show(0); // We create the Textarea where the user // will add the new "Hello World!" message: $myTextbox1 = new Textbox(array( 'name'=>'HelloWorld_txt', 'father'=>'HelloWorld_wnd_Content', 'x' => 20, 'y' => 50, 'width' => 150 )); $myTextbox1->show(); // We focus it, so the user can start typing // from the moment he opens the app without // having to click into the textarea: $myTextbox1->focus(); $myButton1 = new Button(array( 'name'=>'HelloWorld_btn', 'father'=>'HelloWorld_wnd_Content', 'caption'=>'Change Label Text', 'x'=>20, 'y'=>80 )); // We add the Textbox as friend of the button, // so the user when clicks the button will be // sending the textbox input text. A button can // have as many friends as user wants: $myButton1->addFriend($myTextbox1); $myButton1->show(); } function HelloWorld_end($params=null) { reqLib('cmyWidgets','unserialize'); } ?>
<?php // We create the NAMEOFAPP_on_NAMEOFBUTTON function. // In this case, the app is HelloWorld and the button // is HelloWorld_BTN: function HelloWorld_on_HelloWorld_btn($params="") { // We grab the current text inside the textbox: $myCurrentText = $GLOBALS['HelloWorld_txt']->text; // We set the text in the textbox in the Label: $GLOBALS['HelloWorld_lbl']->setText($myCurrentText); // Finally, we clean the textbox and focus it again // so the user can continue writting: $GLOBALS['HelloWorld_txt']->setText(''); $GLOBALS['HelloWorld_txt']->focus(); } // This function is executed in each received message BEFORE // Execute the message. // The widget library need to update the contents of the widgets, // and there is the correct place for do the update. // The updated is performed calling to updateContent. // It's an automatic function, so you can just copy it over // your apps that use messages by simply changing the app's name: function HelloWorld_on_Message($params="") { reqLib('cmyWidgets','updateContent',$params); } // Finally, the NAMEOFAPP_on_Close() function will be executed when // the user closes the application using the "x" button in the window bar. // This code defines what happens when the "x" button is pressed, normally // The app should remove itself from the system table (kill) function HelloWorld_on_Close(){ global $myPid; service('proc','close',array($myPid)); }