Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

GtkUtils.cc

Go to the documentation of this file.
00001 /*
00002 
00003     Gtk+ utility class
00004     Copyright (C) 1999-2002 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <X11/Xlib.h>
00027 #include <gtk/gtk.h>
00028 #include <gdk/gdkprivate.h>
00029 
00030 #include "GtkUtils.hh"
00031 
00032 
00033 void GListFreeItem (gpointer gpData, gpointer gpUserData)
00034 {
00035     free(gpData);
00036 }
00037 
00038 
00039 inline void clGtkUtils::RemoveNewLine(char *cpLineStr)
00040 {
00041     int iCharCntr;
00042     int iCntrMax = strlen(cpLineStr);
00043 
00044     for (iCharCntr = 0; iCharCntr < iCntrMax; iCharCntr++)
00045     {
00046         if (cpLineStr[iCharCntr] == '\n' || cpLineStr[iCharCntr] == '\r')
00047         {
00048             cpLineStr[iCharCntr] = 0x00;
00049             return;
00050         }
00051     }
00052 }
00053 
00054 
00055 void clGtkUtils::BuildOptionMenu(const GtkWidget *gwOptionMenu,
00056     GtkWidget **gwpMenu, GtkWidget *gwaMenuItem[], const char *cpaMenuTxts[],
00057     int iNumItems)
00058 {
00059     int iItemCntr;
00060     GtkWidget *gwLocalMenu;
00061     GtkWidget *gwLocalMenuItem;
00062 
00063     gwLocalMenu = gtk_menu_new();
00064     *gwpMenu = gwLocalMenu;
00065     gtk_widget_show(gwLocalMenu);
00066     for (iItemCntr = 0; iItemCntr < iNumItems; iItemCntr++)
00067     {
00068         gwLocalMenuItem = gtk_menu_item_new_with_label(cpaMenuTxts[iItemCntr]);
00069         gwaMenuItem[iItemCntr] = gwLocalMenuItem;
00070         gtk_menu_append(GTK_MENU(gwLocalMenu), gwLocalMenuItem);
00071         gtk_widget_show(gwLocalMenuItem);
00072     }
00073     gtk_menu_set_active(GTK_MENU(gwLocalMenu), 0);
00074     gtk_option_menu_set_menu(GTK_OPTION_MENU(gwOptionMenu), gwLocalMenu);
00075 }
00076 
00077 
00078 int clGtkUtils::OptionMenuGetActive(const GtkWidget *gwOptionMenu,
00079     GtkWidget *gwaMenuItems[], int iMenuItemCount)
00080 {
00081     int iItemCntr;
00082     GtkWidget *gwMenuWidget;
00083     GtkWidget *gwActiveItem;
00084 
00085     gwMenuWidget = gtk_option_menu_get_menu(GTK_OPTION_MENU(gwOptionMenu));
00086     gwActiveItem = gtk_menu_get_active(GTK_MENU(gwMenuWidget));
00087     for (iItemCntr = 0; iItemCntr < iMenuItemCount; iItemCntr++)
00088     {
00089         if (gwActiveItem == gwaMenuItems[iItemCntr])
00090             return iItemCntr;
00091     }
00092     return -1;
00093 }
00094 
00095 
00096 void clGtkUtils::ConnectMotionEvent(const GtkWidget *gwDest, 
00097     const GtkWidget *gwSrc)
00098 {
00099     gtk_signal_connect_object(GTK_OBJECT(gwSrc),
00100         "motion_notify_event",
00101         (GtkSignalFunc) EVENT_METHOD(gwDest, motion_notify_event),
00102         GTK_OBJECT(gwDest));
00103 }
00104 
00105 
00106 void clGtkUtils::EnableBackingStore(const GtkWidget *gwWidget)
00107 {
00108     #if (GTK_MAJOR_VERSION == 1)
00109     GdkWindowPrivate *gwpWindow;
00110     XSetWindowAttributes xswaAttrib;
00111     
00112     gwpWindow = (GdkWindowPrivate *) gwWidget->window;
00113     xswaAttrib.backing_store = Always;
00114     XChangeWindowAttributes(gwpWindow->xdisplay, gwpWindow->xwindow,
00115         CWBackingStore, &xswaAttrib);
00116     #endif
00117 }
00118 
00119 
00120 void clGtkUtils::ComboListFromFile(const GtkWidget *gwWidget,
00121     GList **pListPtr, const char *cpFileName)
00122 {
00123     char cpLineBuf[GU_TXTBUFSIZE];
00124     char *cpNewItem;
00125     FILE *fileLoad;
00126     GList *glList = *pListPtr;
00127 
00128     if (glList != NULL) 
00129     {
00130         g_list_foreach(glList, GListFreeItem, NULL);
00131         g_list_free(glList);
00132         glList = NULL;
00133     }
00134     fileLoad = fopen(cpFileName, "rt");
00135     if (fileLoad != NULL)
00136     {
00137         while (fgets(cpLineBuf, GU_TXTBUFSIZE, fileLoad) != NULL)
00138         {
00139             RemoveNewLine(cpLineBuf);
00140             if (strlen(cpLineBuf) > 0)
00141             {
00142                 cpNewItem = (char *) g_malloc(strlen(cpLineBuf) + 1);
00143                 if (cpNewItem == NULL) return;
00144                 strcpy(cpNewItem, cpLineBuf);
00145                 glList = g_list_append(glList, cpNewItem);
00146             }
00147         }
00148         gtk_combo_set_popdown_strings(GTK_COMBO(gwWidget), glList);
00149         fclose(fileLoad);
00150     }
00151     *pListPtr = glList;
00152 }
00153 

Generated on Sun Oct 26 19:11:20 2003 for HASAS by doxygen 1.3.3