TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

gdisp_mainBoard.c

Go to the documentation of this file.
00001 
00043 /*
00044  * System includes.
00045  */
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <assert.h>
00049 #include <string.h>
00050 #include <time.h>
00051 
00052 
00053 /*
00054  * GDISP+ includes.
00055  */
00056 #include "gdisp_kernel.h"
00057 #include "gdisp_prototypes.h"
00058 
00059 
00060 /*
00061  --------------------------------------------------------------------
00062                              STATIC ROUTINES
00063  --------------------------------------------------------------------
00064 */
00065 
00066 
00067 /*
00068  * The "delete_event" occurs when the window manager sens this event
00069  * to the application, usually by the "close" option, or on the titlebar.
00070  * Returning TRUE means that we do not want to have the "destroy" event 
00071  * emitted, keeping GDISP+ running. Returning FALSE, we ask that "destroy"
00072  * be emitted, which in turn will call the "destroy" signal handler.
00073  */
00074 static gint
00075 gdispManageDeleteEventFromWM (GtkWidget *mainBoardWindow,
00076                               GdkEvent  *event,
00077                               gpointer   data)
00078 {
00079 
00080   return FALSE;
00081 
00082 }
00083 
00084 
00085 /*
00086  * The "destroy" event occurs when we call "gtk_widget_destroy" on
00087  * the top-level window, of if we return FALSE in the "delete_event"
00088  * callback (see above).
00089  */
00090 static void
00091 gdispDestroySignalHandler (GtkWidget *mainBoardWindow,
00092                            gpointer   data)
00093 {
00094 
00095   /*
00096    * Tells GTK+ that it has to exit from the GTK+ main processing loop.
00097    */
00098   gtk_main_quit();
00099 
00100 }
00101 
00102 
00103 /*
00104  * 'Quit' item from 'File' menu of the main menu bar.
00105  * Quit handler : tell GTK+ we have to leave...
00106  */
00107 static void
00108 gdispQuitItemHandler (gpointer factoryData,
00109                       guint    itemData)
00110 {
00111 
00112 #if defined(NOT_TO_FORGET)
00113   Kernel_T *kernel = (Kernel_T*)factoryData;
00114 #endif
00115 
00116   /*
00117    * Tells GTK+ that it has to exit from the GTK+ main processing loop.
00118    */
00119   gtk_main_quit();
00120 
00121 }
00122 
00123 
00124 /*
00125  * Add a line into the main board output list.
00126  * !!! WARNING !!!
00127  * 'string' parameter is released after being used.
00128  */
00129 static void
00130 gdispOutputWrite(Kernel_T  *kernel,
00131                  GString   *string,
00132                  Message_T  messageType)
00133 {
00134 
00135 #define _TIME_STRING_MAX_LEN_ 80
00136 
00137   gchar      timeString[_TIME_STRING_MAX_LEN_];
00138   GtkWidget *listItem = (GtkWidget*)NULL;
00139   GtkWidget *hBox     = (GtkWidget*)NULL;
00140   time_t     nowTime;
00141 
00142   assert(kernel);
00143   assert(kernel->widgets.mainBoardWindow);
00144   assert(kernel->widgets.mainBoardOutputList);
00145 
00146 
00147   /*
00148    * Format time message : hours:minutes.
00149    */
00150   nowTime = time((time_t*)NULL);
00151   strftime(timeString,
00152            _TIME_STRING_MAX_LEN_,
00153            "%H:%M : ",
00154            localtime(&nowTime));
00155   g_string_prepend(string,timeString);
00156 
00157 
00158   /*
00159    * Create a 'list item' containing a 'label' and 'pixmap'.
00160    * Add this 'list item' into the main board output list.
00161    */
00162   hBox = gdisp_getMessagePixmaps (kernel,
00163                                   kernel->widgets.mainBoardWindow,
00164                                   messageType,
00165                                   string->str);
00166 
00167   listItem = gtk_list_item_new();
00168   gtk_container_add(GTK_CONTAINER(listItem),hBox);  
00169 
00170   gtk_container_add(GTK_CONTAINER(kernel->widgets.mainBoardOutputList),
00171                     listItem);
00172 
00173   gtk_widget_show(listItem);
00174 
00175   gtk_list_scroll_vertical(GTK_LIST(kernel->widgets.mainBoardOutputList),
00176                            GTK_SCROLL_PAGE_FORWARD /* GTK_SCROLL_JUMP */,
00177                            (gfloat)1 /* bottom of the list */);
00178 
00179   kernel->widgets.mainBoardOutputListSize++;
00180 
00181   g_string_free(string,TRUE);
00182 
00183 
00184   /*
00185    * Raise main-board window in order to see the incoming message.
00186    */
00187   gdk_window_raise(GTK_WIDGET(kernel->widgets.mainBoardWindow)->window);
00188 
00189 }
00190 
00191 
00192 static void
00193 gdisp_clearOutputList (gpointer data)
00194 {
00195 
00196   Kernel_T *kernel = (Kernel_T*)data;
00197 
00198   assert(kernel);
00199   assert(kernel->widgets.mainBoardOutputList);
00200 
00201   /*
00202    * Use output list size to remove all list items.
00203    */
00204   if (kernel->widgets.mainBoardOutputListSize != 0) {
00205 
00206     gtk_list_clear_items(GTK_LIST(kernel->widgets.mainBoardOutputList),
00207                          0 /* start index */,
00208                          kernel->widgets.mainBoardOutputListSize /* stop */);
00209 
00210     kernel->widgets.mainBoardOutputListSize = 0;
00211 
00212   }
00213 
00214 }
00215 
00216 
00217 /*
00218  * Event handler for the main board output list.
00219  * Show the popup-menu given as an argument when right-clicking
00220  * onto the output list.
00221  */
00222 static gint
00223 gdisp_outputListEvent (GtkWidget *widget,
00224                        GdkEvent  *event )
00225 {
00226 
00227   if (event->type == GDK_BUTTON_PRESS) {
00228 
00229     GdkEventButton *bEvent = (GdkEventButton*)event;
00230 
00231     if (bEvent->button == 3 /* right-click */) {
00232 
00233       gtk_menu_popup(GTK_MENU(widget),
00234                      NULL /* parent menu shell      */,
00235                      NULL /* parent menu item       */,
00236                      NULL /* user position function */,
00237                      NULL /* user private data      */,
00238                      bEvent->button,
00239                      bEvent->time);
00240 
00241       /*
00242        * Tell calling code that we have handled this event.
00243        * The process stops here.
00244        */
00245       return TRUE;
00246 
00247     }
00248 
00249   }
00250 
00251   /*
00252    * Tell calling code that we have not handled this event.
00253    * Pass it on.
00254    */
00255   return FALSE;
00256 
00257 }
00258 
00259 
00260 /*
00261  * This is the 'GtkItemFactoryEntry' structure used to generate new menus.
00262  *
00263  * Item 1 : The menu path.
00264  *          The letter after the underscore indicates an accelerator key 
00265  *          once the menu is open.
00266  * Item 2 : The accelerator key for the entry.
00267  * Item 3 : The callback function.
00268  * Item 4 : The callback parameter.
00269  *          This changes the parameters with which the function is called.
00270  *          The default is NULL.
00271  * Item 5 : The item type, used to define what kind of an item it is.
00272  *          Here are the possible values :
00273  *
00274  *          NULL            -> same as "<Item>".
00275  *          ""              -> same as "<Item>".
00276  *          "<Title>"       -> create a title item.
00277  *          "<Item>"        -> create a simple item.
00278  *          "<CheckItem>"   -> create a check item.
00279  *          "<ToggleItem>"  -> create a toggle item.
00280  *          "<RadioItem>"   -> create a radio item.
00281  *          <path>          -> path of a radio item to link against.
00282  *          "<Separator>"   -> create a separator.
00283  *          "<Branch>"      -> create an item to hold sub items (optional).
00284  *          "<LastBranch>"  -> create a right justified branch.
00285  */
00286 
00287 static GtkItemFactoryEntry
00288 gdispMainBoardMenuDefinitions[] = {
00289 
00290  { "/_File",                    NULL,
00291                                 NULL,                   0, "<Branch>"     },
00292  { "/_File/_New",               "<control>N",
00293                                 NULL,                   0, NULL           },
00294  { "/_File/_Open",              "<control>O",
00295                                 NULL,                   0, NULL           },
00296  { "/_File/_Save",              "<control>S",
00297                                 NULL,                   0, NULL           },
00298  { "/_File/Save _As",           NULL,
00299                                 NULL,                   0, NULL           },
00300  { "/_File/Sep1",               NULL,
00301                                 NULL,                   0, "<Separator>"  },
00302  { "/_File/Quit",               "<control>Q",
00303                                 gdispQuitItemHandler,   0, NULL           },
00304  { "/_Data",                    NULL,
00305                                 NULL,                   0, "<Branch>"     },
00306  { "/_Data/_All Data",          "<control>A",
00307                                 gdisp_showDataBook,     0, NULL           },
00308  { "/_Plots",                   NULL,
00309                                 NULL,                   0, "<Branch>"     },
00310  { "/_Plots/New page",          NULL,
00311                                 NULL,                   0, "<Branch>"     },
00312  { "/_Plots/New page/_Custom",  "<control>C",
00313                                 gdisp_createGraphicPage,0, NULL           },
00314  { "/_Plots/New page/Sep3",     NULL,
00315                                 NULL,                   0, "<Separator>"  },
00316  { "/_Plots/New page/_1 x 1",   "<control>1",
00317                                 gdisp_createGraphicPage,1, NULL           },
00318  { "/_Plots/New page/_2 x 2",   "<control>2",
00319                                 gdisp_createGraphicPage,2, NULL           },
00320  { "/_Plots/New page/_3 x 3",   "<control>3",
00321                                 gdisp_createGraphicPage,3, NULL           },
00322  { "/_Plots/New page/_4 x 4",   "<control>4",
00323                                 gdisp_createGraphicPage,4, NULL           },
00324  { "/_Plots/New page/_5 x 5",   "<control>5",
00325                                 gdisp_createGraphicPage,5, NULL           },
00326  { "/_Help",                    NULL,
00327                                 NULL,                   0, "<LastBranch>" },
00328  { "/_Help/About",              NULL,
00329                                 NULL,                   0, NULL           },
00330 
00331 };
00332 
00333 
00334 /*
00335  --------------------------------------------------------------------
00336                              PUBLIC ROUTINES
00337  --------------------------------------------------------------------
00338 */
00339 
00340 
00341 /*
00342  * Create GDISP+ main board.
00343  */
00344 void
00345 gdisp_createMainBoard (Kernel_T *kernel)
00346 {
00347 
00348   GtkWidget      *mainVBox        =      (GtkWidget*)NULL;
00349   GtkWidget      *mainHBox        =      (GtkWidget*)NULL;
00350   GtkWidget      *pilotBox        =      (GtkWidget*)NULL;
00351   GtkWidget      *menuBar         =      (GtkWidget*)NULL;
00352   GtkWidget      *scrolledWindow  =      (GtkWidget*)NULL;
00353   GtkWidget      *hSeparator      =      (GtkWidget*)NULL;
00354   GtkWidget      *vSeparator      =      (GtkWidget*)NULL;
00355   GtkWidget      *outputMenu      =      (GtkWidget*)NULL;
00356   GtkWidget      *outputClearItem =      (GtkWidget*)NULL;
00357   GtkItemFactory *itemFactory     = (GtkItemFactory*)NULL;
00358   GtkAccelGroup  *accelGroup      =  (GtkAccelGroup*)NULL;
00359   gint            menuItemNb      =                  0;
00360   GtkWidget      *pixmapWidget    =      (GtkWidget*)NULL;
00361   Pixmap_T       *pixmap          =       (Pixmap_T*)NULL;
00362 
00363   assert(kernel);
00364 
00365 
00366   /* ------------------------ MAIN WINDOW ------------------------ */
00367 
00368   /*
00369    * The GTK_WINDOW_TOPLEVEL argument specifies that we want the window
00370    * to undergo window manager decoration and placement.
00371    */
00372   kernel->widgets.mainBoardWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00373   assert(kernel->widgets.mainBoardWindow);
00374 
00375   gtk_signal_connect(GTK_OBJECT(kernel->widgets.mainBoardWindow),
00376                      "delete_event",
00377                      GTK_SIGNAL_FUNC(gdispManageDeleteEventFromWM),
00378                      (gpointer)kernel);
00379 
00380   gtk_signal_connect(GTK_OBJECT(kernel->widgets.mainBoardWindow),
00381                      "destroy",
00382                      GTK_SIGNAL_FUNC(gdispDestroySignalHandler),
00383                      (gpointer)kernel);
00384 
00385   /*
00386    * Set up window title and border width.
00387    */
00388   gtk_widget_set_usize(GTK_WIDGET(kernel->widgets.mainBoardWindow),
00389                        600 /* width  */,
00390                        150 /* height */);
00391 
00392   gtk_window_set_title(GTK_WINDOW(kernel->widgets.mainBoardWindow),
00393                        "GDISP+ Copyright (c) 2004.");
00394 
00395   gtk_container_set_border_width(
00396                            GTK_CONTAINER(kernel->widgets.mainBoardWindow),
00397                            3);
00398 
00399 
00400   /* ------------------------ PACKING BOX ------------------------ */
00401 
00402   /*
00403    * We need a vertical packing box for managing all widgets.
00404    */
00405   mainVBox = gtk_vbox_new(FALSE, /* homogeneous */
00406                           3      /* spacing     */ );
00407   gtk_container_border_width(GTK_CONTAINER(mainVBox),2);
00408   gtk_container_add(GTK_CONTAINER(kernel->widgets.mainBoardWindow),mainVBox);
00409   gtk_widget_show(mainVBox);
00410 
00411 
00412   /* ------------------------ MENU BAR  ------------------------ */
00413 
00414   /*
00415    * Create all popup-menus with 'itemFactory' process.
00416    */
00417   menuItemNb = sizeof(gdispMainBoardMenuDefinitions   ) /
00418                sizeof(gdispMainBoardMenuDefinitions[0]);
00419 
00420   accelGroup = gtk_accel_group_new();
00421 
00422   /*
00423    * This function initializes the item factory.
00424    *  Param 1 : The type of menu.
00425    *   Can be GTK_TYPE_MENU_BAR, GTK_TYPE_MENU or GTK_TYPE_OPTION_MENU.
00426    *  Param 2 : The path of the menu.
00427    *  Param 3 : A pointer to a 'gtk_accel_group'.
00428    *   The item factory sets up the accelerator table while generating menus.
00429    */
00430   itemFactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR,
00431                                      "<main>", 
00432                                      accelGroup);
00433 
00434   /*
00435    * This function generates the menu items.
00436    * Pass the item factory, the number of items in the array,
00437    * the array itself, and any callback data for the the menu items.
00438    */
00439   gtk_item_factory_create_items(itemFactory,
00440                                 menuItemNb,
00441                                 gdispMainBoardMenuDefinitions,
00442                                 (gpointer)kernel);
00443 
00444   /*
00445    * Attach the new accelerator group to the window.
00446    */
00447   gtk_window_add_accel_group(GTK_WINDOW(kernel->widgets.mainBoardWindow),
00448                              accelGroup);
00449 
00450   /*
00451    * Finally, get back item factory widget and pack it.
00452    */ 
00453   menuBar = gtk_item_factory_get_widget(itemFactory,"<main>");
00454   gtk_box_pack_start(GTK_BOX(mainVBox),
00455                      menuBar,
00456                      FALSE /* expand  */,
00457                      TRUE  /* fill    */,
00458                      0     /* padding */);
00459   gtk_widget_show(menuBar);
00460 
00461 
00462   /* ------------------------ MAIN WINDOW ------------------------ */
00463 
00464   /*
00465    * Map top-level window.
00466    */
00467   gtk_widget_show(kernel->widgets.mainBoardWindow);
00468 
00469 
00470   /* ------------------------ SEPARATOR ------------------------ */
00471 
00472   /*
00473    * Separate menu-bar and output window with a horizontal separator.
00474    */
00475   hSeparator = gtk_hseparator_new();
00476   gtk_box_pack_start(GTK_BOX(mainVBox),
00477                      hSeparator,
00478                      FALSE /* expand  */,
00479                      TRUE  /* fill    */,
00480                      0     /* padding */);
00481   gtk_widget_show(hSeparator);
00482 
00483 
00484   /* ------------------------ PACKING BOX ------------------------ */
00485 
00486   /*
00487    * We need a horizontal packing box for managing the logo and
00488    * the output window.
00489    */
00490   mainHBox = gtk_hbox_new(FALSE, /* homogeneous */
00491                           3      /* spacing     */ );
00492   gtk_container_border_width(GTK_CONTAINER(mainHBox),0);
00493   gtk_box_pack_start(GTK_BOX(mainVBox),
00494                      mainHBox,
00495                      TRUE  /* expand  */,
00496                      TRUE  /* fill    */,
00497                      0     /* padding */);
00498   gtk_widget_show(mainHBox);
00499 
00500 
00501   /* ------------ MENU WHEN CLICKING ON THE LIST ------------ */
00502 
00503   /*
00504    * Init the menu-widget, and remember
00505    * -- never 'gtk_show_widget()' the menu widget !! --
00506    * This is the menu that holds the menu items, the one that will pop
00507    * up when you click on the output list.
00508    */
00509   outputMenu = gtk_menu_new ();
00510 
00511   /*
00512    * Create a menu-item with the name 'Clear'...
00513    * ... and add it to the output menu.
00514    * Connect a signal for clearing the output list when "activated".
00515    */
00516   outputClearItem = gtk_menu_item_new_with_label("Clear");
00517   gtk_menu_append(GTK_MENU(outputMenu),outputClearItem);
00518 
00519   gtk_signal_connect_object(GTK_OBJECT(outputClearItem),
00520                             "activate",
00521                             GTK_SIGNAL_FUNC(gdisp_clearOutputList),
00522                             (gpointer)kernel);
00523 
00524   gtk_widget_show(outputClearItem);
00525 
00526 
00527   /* ------------------------ OUTPUT WINDOW  ------------------------ */
00528 
00529   /*
00530    * This is the scrolled window to put the List widget inside.
00531    */
00532   scrolledWindow = gtk_scrolled_window_new(NULL /* H Adjustment */,
00533                                            NULL /* V Adjustment */);
00534   gtk_box_pack_start(GTK_BOX(mainHBox),
00535                      scrolledWindow,
00536                      TRUE  /* expand  */,
00537                      TRUE  /* fill    */,
00538                      0     /* padding */);
00539   gtk_widget_show(scrolledWindow);
00540 
00541   /*
00542    * Create the List widget.
00543    */
00544   kernel->widgets.mainBoardOutputList = gtk_list_new();
00545   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledWindow),
00546                                         kernel->widgets.mainBoardOutputList);
00547   gtk_widget_show(kernel->widgets.mainBoardOutputList);
00548 
00549   /*
00550    * Connect signals for clearing the list.
00551    */
00552   gtk_signal_connect_object(GTK_OBJECT(kernel->widgets.mainBoardOutputList),
00553                             "event",
00554                             GTK_SIGNAL_FUNC(gdisp_outputListEvent),
00555                             GTK_OBJECT(outputMenu));
00556 
00557 
00558   /* ------------------------ SEPARATOR ------------------------ */
00559 
00560   /*
00561    * Separate scrolled-window and play button with a vertical separator.
00562    */
00563   vSeparator = gtk_vseparator_new();
00564   gtk_box_pack_start(GTK_BOX(mainHBox),
00565                      vSeparator,
00566                      FALSE /* expand  */,
00567                      TRUE  /* fill    */,
00568                      0     /* padding */);
00569   gtk_widget_show(vSeparator);
00570 
00571 
00572   /* ----------------------- PILOT BOARD ----------------------- */
00573 
00574   /*
00575    * Create the pilot board (not shown after creation).
00576    */
00577   pilotBox = gdisp_createPilotBoard(kernel);
00578 
00579   gtk_box_pack_start(GTK_BOX(mainHBox),
00580                      pilotBox,
00581                      FALSE /* expand  */,
00582                      TRUE  /* fill    */,
00583                      0     /* padding */);
00584   gtk_widget_show(pilotBox);
00585 
00586 
00587   /* ------------------------ SEPARATOR ------------------------ */
00588 
00589   /*
00590    * Separate play button and logo with a vertical separator.
00591    */
00592   vSeparator = gtk_vseparator_new();
00593   gtk_box_pack_start(GTK_BOX(mainHBox),
00594                      vSeparator,
00595                      FALSE /* expand  */,
00596                      TRUE  /* fill    */,
00597                      0     /* padding */);
00598   gtk_widget_show(vSeparator);
00599 
00600 
00601   /* ------------------------ GDISP+ LOGO  ------------------------ */
00602 
00603   /*
00604    * Use GDK services to create GDISP+ Logo (XPM format).
00605    */
00606   pixmap = gdisp_getPixmapById(kernel,
00607                                GD_PIX_gdispLogo,
00608                                kernel->widgets.mainBoardWindow);
00609 
00610   /*
00611    * Create a pixmap widget to contain the pixmap.
00612    */
00613   pixmapWidget = gtk_pixmap_new(pixmap->pixmap,
00614                                 pixmap->mask);
00615   gtk_box_pack_start(GTK_BOX(mainHBox),
00616                      pixmapWidget,
00617                      FALSE /* expand  */,
00618                      TRUE  /* fill    */,
00619                      0     /* padding */);
00620   gtk_widget_show(pixmapWidget);
00621 
00622 
00623   /* ------------------------ END MAIN BOARD  ------------------------ */
00624 
00625   /*
00626    * Tell the kernel that the output list has been created.
00627    * It is now ready to be used.
00628    */
00629   kernel->outputFunc = gdispOutputWrite;
00630 
00631 }
00632 
00633 
00634 void
00635 gdisp_writeInitialInformation (Kernel_T *kernel)
00636 {
00637 
00638   GString *messageString = (GString*)NULL;
00639 
00640 
00641   /*
00642    * Keep in mind that the GString is released into the
00643    * 'gdispOutputWrite' function.
00644    * Do not 'g_string_free' it.
00645    */
00646 
00647   /*
00648    * Add into the list GDISP+ Version identification.
00649    */
00650   messageString = g_string_new("GDISP+ Graphic Tool, Version 1.0");
00651   kernel->outputFunc(kernel,messageString,GD_MESSAGE);
00652 
00653 
00654   /*
00655    * Report whether we are in a thread-safe environment.
00656    */
00657   messageString = g_string_new((gchar*)NULL);
00658   g_string_sprintf(messageString,
00659                    "Implementation is %sthread-safe.",
00660                    kernel->isThreadSafe == TRUE ? "" : "NOT ");
00661   kernel->outputFunc(kernel,messageString,GD_WARNING);
00662 
00663 }
00664 
Framework Home Page.

Beware !! TSP wave is coming...