gui_utils.c
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:8k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is MPEG4IP.
  13.  * 
  14.  * The Initial Developer of the Original Code is Cisco Systems Inc.
  15.  * Portions created by Cisco Systems Inc. are
  16.  * Copyright (C) Cisco Systems Inc. 2000, 2001.  All Rights Reserved.
  17.  * 
  18.  * Contributor(s): 
  19.  *              Bill May        wmay@cisco.com
  20.  */
  21. /*
  22.  * gui_utils.c - various gui utilities taken from Developing Linux
  23.  * Applications and from xmps
  24.  */
  25. /*
  26.  * Sample GUI application front end.
  27.  *
  28.  * Auth: Eric Harlow
  29.  *
  30.  */
  31. #include <gtk/gtk.h>
  32. #include <gdk/gdkkeysyms.h>
  33. #include <string.h>
  34. #include "gui_utils.h"
  35. #include <stdio.h>
  36. #include <syslog.h>
  37. /*
  38.  * CreateWidgetFromXpm
  39.  *
  40.  * Using the window information and the string with the icon color/data, 
  41.  * create a widget that represents the data.  Once done, this widget can
  42.  * be added to buttons or other container widgets.
  43.  */
  44. GtkWidget *CreateWidgetFromXpm (GtkWidget *window, gchar **xpm_data)
  45. {
  46.   // got this code from Xmps
  47.     GdkColormap *colormap;
  48.     GdkPixmap *gdkpixmap;
  49.     GdkBitmap *mask;
  50.     GtkWidget *pixmap;
  51.     colormap = gtk_widget_get_colormap(window);
  52.     gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
  53.        NULL, xpm_data);
  54.     if (gdkpixmap == NULL) {
  55.       printf("Errorn");
  56.       return (NULL);
  57.     }
  58.     pixmap = gtk_pixmap_new (gdkpixmap, mask);
  59.     gdk_pixmap_unref (gdkpixmap);
  60.     gdk_bitmap_unref (mask);
  61.     gtk_widget_show(pixmap);
  62.     return pixmap;
  63. }
  64. /*
  65.  * CreateMenuItem
  66.  *
  67.  * Creates an item and puts it in the menu and returns the item.
  68.  *
  69.  * menu - container menu
  70.  * szName - Name of the menu - NULL for a separator
  71.  * szAccel - Acceleration string - "^C" for Control-C
  72.  * szTip - Tool tips
  73.  * func - Call back function
  74.  * data - call back function data
  75.  *
  76.  * returns new menuitem
  77.  */
  78. GtkWidget *CreateMenuItem (GtkWidget *menu, 
  79.    GtkAccelGroup *accel_group,
  80.    GtkTooltips   *tooltips,
  81.                            char *szName, 
  82.                            char *szAccel,
  83.                            char *szTip, 
  84.                            GtkSignalFunc func,
  85.                            gpointer data)
  86. {
  87.     GtkWidget *menuitem;
  88.     /* --- If there's a name, create the item and put a
  89.      *     Signal handler on it.
  90.      */
  91.     if (szName && strlen (szName)) {
  92.         menuitem = gtk_menu_item_new_with_label (szName);
  93.         gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
  94.                     GTK_SIGNAL_FUNC(func), data);
  95.     } else {
  96.         /* --- Create a separator --- */
  97.         menuitem = gtk_menu_item_new ();
  98.     }
  99.     /* --- Add menu item to the menu and show it. --- */
  100.     gtk_menu_append (GTK_MENU (menu), menuitem);
  101.     gtk_widget_show (menuitem);
  102.     /* --- If there was an accelerator --- */
  103.     if (szAccel && accel_group != NULL) {
  104.       guint modifier = 0;
  105.       guint key;
  106.       if (szAccel[0] == '^') {
  107. szAccel++;
  108. modifier |= GDK_CONTROL_MASK;
  109.       }
  110.       if (strncmp(szAccel, "M-", 2) == 0) {
  111. szAccel += 2;
  112. modifier |= GDK_MOD1_MASK;
  113.       }
  114.       key = szAccel[0];
  115.       if (strncmp(szAccel,"<enter>", sizeof("<enter>")) == 0) {
  116. key = GDK_Return;
  117.       }
  118.       gtk_widget_add_accelerator (menuitem, 
  119.   "activate", 
  120.   accel_group,
  121.   key, 
  122.   modifier,
  123.   GTK_ACCEL_VISIBLE);
  124.     }
  125.     /* --- If there was a tool tip --- */
  126.     if (szTip && strlen (szTip) && tooltips != NULL) {
  127.         /* --- If tooltips not created yet --- */
  128.         gtk_tooltips_set_tip (tooltips, menuitem, szTip, NULL);
  129.     }
  130.     return (menuitem);
  131. }
  132. /*
  133.  * CreateMenuCheck
  134.  *
  135.  * Create a menu checkbox
  136.  *
  137.  * menu - container menu
  138.  * szName - name of the menu
  139.  * func - Call back function.
  140.  * data - call back function data
  141.  *
  142.  * returns new menuitem
  143.  */ 
  144. GtkWidget *CreateMenuCheck (GtkWidget *menu, 
  145.                             char *szName, 
  146.                             GtkSignalFunc func, 
  147.                             gpointer data,
  148.     gboolean initial_data)
  149. {
  150.     GtkWidget *menuitem;
  151.     /* --- Create menu item --- */
  152.     menuitem = gtk_check_menu_item_new_with_label (szName);
  153.     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
  154.    initial_data);
  155.     /* --- Add it to the menu 2--- */
  156.     gtk_menu_append (GTK_MENU (menu), menuitem);
  157.     gtk_widget_show (menuitem);
  158.     /* --- Listen for "toggled" messages --- */
  159.     gtk_signal_connect (GTK_OBJECT (menuitem), "toggled",
  160.                         GTK_SIGNAL_FUNC(func), data);
  161.     return (menuitem);
  162. }
  163. /*
  164.  * CreateMenuRadio
  165.  *
  166.  * Create a menu radio
  167.  *
  168.  * menu - container menu
  169.  * szName - name of the menu
  170.  * func - Call back function.
  171.  * data - call back function data
  172.  *
  173.  * returns new menuitem
  174.  */ 
  175. GtkWidget *CreateMenuRadio (GtkWidget *menu, 
  176.                             char *szName, 
  177.                             GSList **group,
  178.                             GtkSignalFunc func, 
  179.                             gpointer data)
  180. {
  181.     GtkWidget *menuitem;
  182.     /* --- Create menu item --- */
  183.     menuitem = gtk_radio_menu_item_new_with_label (*group, szName);
  184.     *group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (menuitem));
  185.     /* --- Add it to the menu --- */
  186.     gtk_menu_append (GTK_MENU (menu), menuitem);
  187.     gtk_widget_show (menuitem);
  188.     /* --- Listen for "toggled" messages --- */
  189.     gtk_signal_connect (GTK_OBJECT (menuitem), "toggled",
  190.                         GTK_SIGNAL_FUNC(func), data);
  191.     return (menuitem);
  192. }
  193. /*
  194.  * CreateSubMenu
  195.  *
  196.  * Create a submenu off the menubar.
  197.  *
  198.  * menubar - obviously, the menu bar.
  199.  * szName - Label given to the new submenu
  200.  *
  201.  * returns new menu widget
  202.  */
  203. GtkWidget *CreateSubMenu (GtkWidget *menubar, char *szName)
  204. {
  205.     GtkWidget *menuitem;
  206.     GtkWidget *menu;
  207.  
  208.     /* --- Create menu --- */
  209.     menuitem = gtk_menu_item_new_with_label (szName);
  210.     /* --- Add it to the menubar --- */
  211.     gtk_widget_show (menuitem);
  212.     gtk_menu_append (GTK_MENU (menubar), menuitem);
  213.     /* --- Get a menu and attach to the menuitem --- */
  214.     menu = gtk_menu_new ();
  215.     gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
  216.     /* --- Viola! --- */
  217.     return (menu);
  218. }
  219. /*
  220.  * CreateBarSubMenu
  221.  *
  222.  * Create a submenu within an existing submenu.  (In other words, it's not
  223.  * the menubar.)
  224.  *
  225.  * menu - existing submenu
  226.  * szName - label to be given to the new submenu off of this submenu
  227.  *
  228.  * returns new menu widget 
  229.  */ 
  230. GtkWidget *CreateBarSubMenu (GtkWidget *menu, char *szName)
  231. {
  232.     GtkWidget *menuitem;
  233.     GtkWidget *submenu;
  234.  
  235.     /* --- Create menu --- */
  236.     menuitem = gtk_menu_item_new_with_label (szName);
  237.     /* --- Add it to the menubar --- */
  238.     gtk_menu_bar_append (GTK_MENU_BAR (menu), menuitem);
  239.     gtk_widget_show (menuitem);
  240.     /* --- Get a menu and attach to the menuitem --- */
  241.     submenu = gtk_menu_new ();
  242.     gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
  243.     /* --- Viola! --- */
  244.     return (submenu);
  245. }
  246. /*
  247.  * FreeChild
  248.  *
  249.  * Free all the children of the widget
  250.  * This is called when the button has to display a new image.
  251.  * The old image is destroyed here.
  252.  */
  253. void FreeChild (GtkWidget *widget)
  254. {
  255.     /* --- Free button children --- */
  256.     gtk_container_foreach (
  257.                GTK_CONTAINER (widget),
  258.                (GtkCallback) gtk_widget_destroy,
  259.                NULL);
  260. }
  261. static char *logs[] = {
  262.   "Emerg",
  263.   "Alert",
  264.   "Critical",
  265.   "Error",
  266.   "Warning",
  267.   "Notice",
  268.   "Info",
  269.   "Debug"
  270. };
  271. void CreateLogLevelSubmenu (GtkWidget *menu,
  272.     char *szName,
  273.     int active,
  274.     GtkSignalFunc func)
  275. {
  276.   int ix;
  277.   GtkWidget *submenu, *this;
  278.   GSList *radiolist = NULL;
  279.   submenu = CreateSubMenu(menu, szName);
  280.   for (ix = LOG_EMERG; ix <= LOG_DEBUG; ix++) {
  281.     this = CreateMenuRadio(submenu,
  282.    logs[ix],
  283.    &radiolist,
  284.    func,
  285.    GINT_TO_POINTER(ix));
  286.     if (active == ix) {
  287.       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(this), TRUE);
  288.     }
  289.   }
  290. }