00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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