hello-world/src/menu.c

Go to the documentation of this file.
00001 /*
00002  * File Name: menu.c
00003  */
00004 
00005 /*
00006  * This file is part of hello-world.
00007  *
00008  * hello-world is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * hello-world is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2009 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system include files, between < >
00032 #include <stdio.h>
00033 #include <unistd.h>
00034 
00035 // ereader include files, between < >
00036 
00037 // local include files, between " "
00038 #include "log.h"
00039 #include "i18n.h"
00040 #include "ipc.h"
00041 #include "main.h"
00042 #include "menu.h"
00043 
00044 
00045 //----------------------------------------------------------------------------
00046 // Type Declarations
00047 //----------------------------------------------------------------------------
00048 
00049 
00050 //----------------------------------------------------------------------------
00051 // Constants
00052 //----------------------------------------------------------------------------
00053 
00054 // menus for application, must be unique
00055 // TODO: replace "helloworld" with your application name
00056 static const char *MENU_MAIN                = "helloworld_menu_main";
00057                                             
00058 // menu groups, must be unique
00059 // TODO: replace "helloworld" with your application name
00060 static const char *GROUP_VIEWS              = "helloworld_view";
00061 static const char *GROUP_ACTIONS            = "helloworld_actions";
00062                                             
00063 // menu items
00064 //   group "Views"                          
00065 static const char *ITEM_VIEW_ICON           = "view_small";
00066 static const char *ITEM_VIEW_LIST           = "view_detail";
00067 //   group "Actions"                        
00068 static const char *ITEM_ACTION_OPEN         = "open";
00069 static const char *ITEM_ACTION_COPY         = "copy";
00070 static const char *ITEM_ACTION_CUT          = "cut";
00071 static const char *ITEM_ACTION_PASTE        = "paste";
00072 static const char *ITEM_ACTION_CLOSE        = "close";
00073                                             
00074 // item states                              
00075 static const char *STATE_NORMAL             = "normal";
00076 static const char *STATE_SELECTED           = "selected";
00077 static const char *STATE_DISABLED           = "disabled";
00078 
00079 
00080 //----------------------------------------------------------------------------
00081 // Static Variables
00082 //----------------------------------------------------------------------------
00083 
00084 
00085 
00086 //============================================================================
00087 // Local Function Definitions
00088 //============================================================================
00089 
00090 
00091 
00092 //============================================================================
00093 // Functions Implementation
00094 //============================================================================
00095 
00096 // initialise popup menu
00097 void menu_init ( void ) 
00098 {
00099     static gboolean firsttime = TRUE;
00100     const char      *group;
00101 
00102     LOGPRINTF("entry");
00103 
00104     // execute only once
00105     if ( !firsttime )
00106     {
00107         WARNPRINTF("function called twice");
00108         return;
00109     }
00110     firsttime = FALSE;
00111 
00112     // add groups
00113     group = GROUP_VIEWS;
00114     ipc_menu_add_group( group,                  "",    "folder"          );
00115     ipc_menu_add_item ( ITEM_VIEW_ICON,         group, "view_small"      );
00116     ipc_menu_add_item ( ITEM_VIEW_LIST,         group, "view_detail"     );
00117     //                                                                                                                
00118     group = GROUP_ACTIONS;
00119     ipc_menu_add_group( group,                  "",    "folder"          );
00120     ipc_menu_add_item ( ITEM_ACTION_OPEN,       group, "open"            );
00121     ipc_menu_add_item ( ITEM_ACTION_COPY,       group, "copy"            );
00122     ipc_menu_add_item ( ITEM_ACTION_CUT,        group, "cut"             );
00123     ipc_menu_add_item ( ITEM_ACTION_PASTE,      group, "paste"           );
00124     ipc_menu_add_item ( ITEM_ACTION_CLOSE,      group, "close"           );
00125 
00126     // add menus
00127     ipc_menu_add_menu( MENU_MAIN, GROUP_VIEWS, GROUP_ACTIONS, NULL );
00128 
00129     // set menu texts
00130     menu_set_text();
00131 
00132     // disable what is not yet implemented
00133     // TODO: implement all and remove this list
00134     group = GROUP_ACTIONS;
00135     ipc_menu_set_item_state ( ITEM_ACTION_COPY,  group, STATE_DISABLED );
00136     ipc_menu_set_item_state ( ITEM_ACTION_CUT,   group, STATE_DISABLED );
00137     ipc_menu_set_item_state ( ITEM_ACTION_PASTE, group, STATE_DISABLED );
00138     
00139     // set menu context
00140     menu_show();    
00141 }
00142 
00143 
00144 // remove the proper popup menu
00145 void menu_destroy (void)
00146 {
00147     LOGPRINTF("entry");
00148 
00149     // remove the main menu
00150     ipc_remove_menu( MENU_MAIN );
00151 }
00152 
00153 
00154 // show the proper popup menu
00155 void menu_show (void)
00156 {
00157     LOGPRINTF("entry");
00158 
00159     // show the main menu
00160     ipc_menu_show_menu( MENU_MAIN );
00161 }
00162 
00163 
00164 // set menu texts
00165 void menu_set_text ( void ) 
00166 {
00167     const char      *group;
00168 
00169     LOGPRINTF("entry");
00170 
00171     // set groups + items
00172     group = GROUP_VIEWS;
00173     ipc_menu_set_group_label( group,                    _("View")              );
00174     ipc_menu_set_item_label ( ITEM_VIEW_ICON,    group, _("Thumbnails")        );
00175     ipc_menu_set_item_label ( ITEM_VIEW_LIST,    group, _("Details")           );
00176     //                                                                                                                 
00177     group = GROUP_ACTIONS;                                                     
00178     ipc_menu_set_group_label( group,                    _("Actions")           );
00179     ipc_menu_set_item_label ( ITEM_ACTION_OPEN,  group, _("Open")              );
00180     ipc_menu_set_item_label ( ITEM_ACTION_COPY,  group, _("Copy")              );
00181     ipc_menu_set_item_label ( ITEM_ACTION_CUT,   group, _("Cut")               );
00182     ipc_menu_set_item_label ( ITEM_ACTION_PASTE, group, _("Paste")             );
00183     ipc_menu_set_item_label ( ITEM_ACTION_CLOSE, group, _("Close")             );
00184 
00185     // set menus
00186     ipc_menu_set_menu_label( MENU_MAIN,  _("Hello World Menu") );
00187 }
00188 
00189 
00190 // select one of the view types
00191 void menu_select_view_type ( const viewtypes_t view_type )
00192 {
00193     static const char   *item_old = "";
00194            const char   *item_new = "";
00195 
00196     LOGPRINTF("entry: view_type [%d]", view_type);
00197     g_assert(view_type < N_VIEWTYPES);
00198 
00199     switch (view_type)
00200     {
00201         case ICONVIEW:
00202             item_new = ITEM_VIEW_ICON;
00203             break;
00204         case LISTVIEW:
00205             item_new = ITEM_VIEW_LIST;
00206             break;
00207         default:
00208             ;  // ignore: handled by assert at function entry
00209     }
00210 
00211     if ( strcmp(item_old, item_new) != 0 )
00212     {
00213         if ( item_old[0] != '\0' )
00214         {
00215             ipc_menu_set_item_state ( item_old, GROUP_VIEWS, STATE_NORMAL   );
00216         }
00217         ipc_menu_set_item_state ( item_new, GROUP_VIEWS, STATE_SELECTED );
00218 
00219         item_old = item_new;
00220     }
00221 }
00222 
00223 
00224 //----------------------------------------------------------------------------
00225 // Callbacks from popupmenu
00226 //----------------------------------------------------------------------------
00227 
00228 // user has pressed a menu button
00229 void menu_on_item_activated ( const gchar *item,
00230                               const gchar *group,
00231                               const gchar *menu,
00232                               const gchar *state )
00233 {
00234     gboolean    ok = TRUE;
00235     viewtypes_t view_type  = 0;
00236     gchar       *msg = NULL;
00237 
00238     LOGPRINTF("entry: item [%s] group [%s]", item, group);
00239 
00240     if ( strcmp(group, GROUP_VIEWS) == 0 )
00241     {
00242         if ( strcmp(state, STATE_NORMAL) == 0 )
00243         {
00244             if (      strcmp(item, ITEM_VIEW_ICON) == 0 )
00245             {
00246                 view_type = ICONVIEW;
00247             }
00248             else if ( strcmp(item, ITEM_VIEW_LIST) == 0 )
00249             {
00250                 view_type = LISTVIEW;
00251             }
00252             else
00253             {
00254                 WARNPRINTF("unexpected menu item [%s] in group [%s]", item, group);
00255                 ok = FALSE;
00256             }
00257 
00258             if (ok)
00259             {
00260                 main_set_view_type( view_type );
00261             }
00262         }
00263         else
00264         {
00265             WARNPRINTF("ignore state [%s] item [%s][%s]", state, item, group);
00266         }
00267     }
00268     else if ( strcmp(group, GROUP_ACTIONS) == 0 )
00269     {
00270         if ( strcmp(state, STATE_NORMAL) == 0 )
00271         {
00272             if (      strcmp(item, ITEM_ACTION_OPEN) == 0 )
00273             {
00274                 // open a document in the viewer
00275                 msg = g_strdup_printf("uds %s/%s", g_mountpoint, "test.pdf");
00276                 ipc_sys_start_task(msg, g_mountpoint, "test.pdf", NULL, NULL);
00277                 g_free(msg);
00278             }
00279             else if ( strcmp(item, ITEM_ACTION_CLOSE) == 0 )
00280             {
00281                 // quit and exit application
00282                 main_quit();
00283                 _exit(0);
00284                 return;
00285             }
00286             else
00287             {
00288                 WARNPRINTF("unexpected menu item [%s] in group [%s]", item, group);
00289                 ok = FALSE;
00290             }
00291 
00292             if (ok)
00293             {
00294                 main_set_view_type( view_type );
00295             }
00296         }
00297         else
00298         {
00299             WARNPRINTF("ignore state [%s] item [%s][%s]", state, item, group);
00300         }
00301     }
00302     else
00303     {
00304         WARNPRINTF("unexpected menu group [%s]", group);
00305         ok = FALSE;
00306     }
00307 
00308     if (!ok)
00309     {
00310         msg = g_strdup_printf( _("Unhandled menu item.\n"
00311                                  "menu: %s\n"
00312                                  "group: %s\n"
00313                                  "item: %s\n"
00314                                  "state: %s"),
00315                                menu,
00316                                group,
00317                                item,
00318                                state );
00319         gtk_label_set_text( GTK_LABEL(g_action), msg);
00320         g_free(msg);
00321     }
00322 }
Generated by  doxygen 1.6.2-20100208