Common Desktop Environment: ToolTalk Messaging Overview

Example Ttdt_contract_cb

Example D-1 is an example of a typical algorithm of a Ttdt_contract_cb callback for an application that handles its own Pause/Resume/Quit requests but allows the toolkit to handle the X11-related requests.


Note -

This example callback deals with the case when the contract parameter has a value other than zero and can, therefore, also be used as the Ttdt_contract_cb callback passed to ttdt_message_accept.



Example D-1 Typical Algorithm of Ttdt_contract_cb

Tt_message myContractCB(
         Tt_message      msg,
         void           *clientdata,
         Tt_message      contract 
) 
{
         char *opString = tt_message_op( msg );
         Tttk_op op = tttk_string_op( opString );
         tt_free( opString );
         int silent = 0;
         int force  = 0;
         Boolean cancel = False;
         Boolean sensitive = True;
         char *status, command;
         switch (op) {
             case TTDT_QUIT:
                 tt_message_arg_ival( msg, 0, &silent );
                 tt_message_arg_ival( msg, 1, &force );
                 if (contract == 0) {
                         /* Quit entire application */
                         cancel = ! myQuitWholeApp( silent, force );
                 } else {
                         /* Quit just the specified request being worked on */
                         cancel = ! myCancelThisRequest(contract, silent, force);
                 }
                 if (cancel) {
                         /* User canceled Quit; fail the Quit request */
                         tttk_message_fail( msg, TT_DESKTOP_ECANCELED, 0, 1 );
                 } else {
                         tt_message_reply( msg );
                         tttk_message_destroy( msg );
                 }
                 return 0;
             case TTDT_PAUSE:
                 sensitive = False;
             case TTDT_RESUME:
                 if (contract == 0) {
                         int already = 1;
                         if (XtIsSensitive( myTopShell ) != sensitive) {
                                 already = 0;
                                 XtSetSensitive( myTopShell, sensitive );
                         }
                         if (already) {
                                 tt_message_status_set(msg,TT_DESKTOP_EALREADY);
                         }
                 } else {
                         if (XtIsSensitive( thisShell ) == sensitive) {
                                 tt_message_status_set(msg,TT_DESKTOP_EALREADY);
                         } else {
                                 XtSetSensitive( thisShell, sensitive );
                        }
                 }
                 tt_message_reply( msg );
                 tttk_message_destroy( msg );
                 return 0;
             case TTDT_GET_STATUS:
                 if (contract == 0) {
                         status = "Message about status of entire app";
                 } else {
                         status = "Message about status of this request";
                 }
                 tt_message_arg_val_set( msg, 0, status );
                 tt_message_reply( msg );
                 tttk_message_destroy( msg );
                 return 0;
             case TTDT_DO_COMMAND:
                 if (! haveExtensionLanguage) {
                         tttk_message_fail( msg, TT_DESKTOP_ENOTSUP, 0, 1 );
                         return 0;
                 }
                 command = tt_message_arg_val( msg, 0 );
                 result = myEval( command );
                 tt_free( command );
                 tt_message_status_set( msg, result );
                 if (tt_is_err( result )) {
                         tttk_message_fail( msg, result, 0, 1 );
                 } else {
                         tt_message_reply( msg );
                        tttk_message_destroy( msg );
                 }
                 return 0;
         }
         /* Unrecognized message; do not consume it */
         return msg; 
}