From: <ale...@us...> - 2003-12-02 04:15:25
|
Update of /cvsroot/morphix/mcp2/src In directory sc8-pr-cvs1:/tmp/cvs-serv29106 Modified Files: Makefile.am callbacks.c interface.c main.c Added Files: data.c data.h Log Message: added more robustness in imagehandling, needs conf/xml file as first argument, fixed resizing (disabled), fixed sizechanging (sortof) --- NEW FILE: data.c --- #include <gtk/gtk.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <libxml/xmlmemory.h> #include <libxml/parser.h> #include "data.h" extern gchar *window_name; void parseItem(xmlDocPtr doc, xmlNodePtr cur) { xmlChar *key; cur = cur->xmlChildrenNode; item_struct *item; if (item_count+1 >= MAX_ITEMS) { fprintf(stderr,"Reached maximum of items, bailing out..."); return; } item = (item_struct *) malloc(sizeof(item_struct)); item->selected = FALSE; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *) "command"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); item->command = (gchar *)malloc(strlen(key) + 1); strncpy(item->command,key,strlen(key)+1); xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *) "name"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); item->name = (gchar *)malloc(strlen(key) + 1); strncpy(item->name,key,strlen(key)+1); xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *) "tip"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); item->tip = (gchar *)malloc(strlen(key)+1); strncpy(item->tip,key,strlen(key) + 1); xmlFree(key); } if ((!xmlStrcmp(cur->name, (const xmlChar *) "icon"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); item->iconpath = (gchar *)malloc(strlen(key)+ 1); strncpy(item->iconpath,key,strlen(key)+1); xmlFree(key); } cur = cur->next; } if (item->command != NULL && item->iconpath != NULL) { item_count++; items[item_count-1] = item; } else { fprintf(stderr,"Found incomplete item!\n"); } return; } void parseGlobal(xmlDocPtr doc, xmlNodePtr cur) { xmlChar *key; cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *) "name"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); window_name = (gchar *)malloc(strlen(key)+ 1); strncpy(window_name,key,strlen(key)+1); xmlFree(key); } cur = cur->next; } if (window_name == NULL) { window_name = g_strdup("Morphix Control Panel"); } return; } void parseDoc(char *docname) { xmlDocPtr doc; xmlNodePtr cur; doc = xmlParseFile(docname); if (doc == NULL) { fprintf(stderr,"Document not parsed succesfully! \n"); return; } cur = xmlDocGetRootElement(doc); if (cur == NULL) { fprintf(stderr,"empty document\n"); return; } if (xmlStrcmp(cur->name, (const xmlChar *) "mcp")) { fprintf(stderr,"document of the wrong type, root node != mcp"); xmlFreeDoc(doc); return; } cur = cur->xmlChildrenNode; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *) "item"))) { parseItem(doc,cur); } if ((!xmlStrcmp(cur->name, (const xmlChar *) "global"))) { parseGlobal(doc,cur); } cur = cur->next; } xmlFreeDoc(doc); return; } --- NEW FILE: data.h --- #ifndef MCP_DATA_H #define MCP_DATA_H #define MAX_ITEMS 40 typedef struct { GdkPixbuf *pixbuf; gboolean selected; gchar *command; gchar *name; gchar *tip; gchar *iconpath; } item_struct; item_struct *items[MAX_ITEMS]; gint item_count; void parseDoc(char *docname); #endif Index: Makefile.am =================================================================== RCS file: /cvsroot/morphix/mcp2/src/Makefile.am,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** Makefile.am 10 Nov 2003 02:04:00 -0000 1.1.1.1 --- Makefile.am 1 Dec 2003 21:19:44 -0000 1.2 *************** *** 12,16 **** support.c support.h \ interface.c interface.h \ ! callbacks.c callbacks.h mcp_LDADD = @PACKAGE_LIBS@ $(INTLLIBS) -L/usr/lib -lxml2 -lz -lpthread -lm --- 12,17 ---- support.c support.h \ interface.c interface.h \ ! callbacks.c callbacks.h \ ! data.c data.h mcp_LDADD = @PACKAGE_LIBS@ $(INTLLIBS) -L/usr/lib -lxml2 -lz -lpthread -lm Index: callbacks.c =================================================================== RCS file: /cvsroot/morphix/mcp2/src/callbacks.c,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** callbacks.c 10 Nov 2003 02:04:00 -0000 1.1.1.1 --- callbacks.c 1 Dec 2003 21:19:44 -0000 1.2 *************** *** 11,34 **** #include "interface.h" #include "support.h" ! #define MCP_CONF "../mcp.conf" ! #define MAX_ITEMS 40 ! typedef struct { ! GdkPixbuf *pixbuf; ! gboolean selected; ! gchar *command; ! gchar *name; ! gchar *tip; ! gchar *iconpath; ! } item_struct; GdkPixbuf *image; ! gint item_count; ! gint minimumx; ! gint minimumy; gint debug_switch = 1; - item_struct *items[MAX_ITEMS]; extern GtkWidget *MainWindow; GtkWidget *da; --- 11,28 ---- #include "interface.h" #include "support.h" + #include "data.h" ! #define MINIMUM_WIDTH 400 ! #define MAX_ICON_WIDTH 48 ! #define MAX_ICON_HEIGHT 48 ! extern gchar *config_file; GdkPixbuf *image; ! gint minimum_width = MINIMUM_WIDTH; ! gint minimum_height = -1; gint debug_switch = 1; extern GtkWidget *MainWindow; + extern gchar *window_name; GtkWidget *da; *************** *** 38,43 **** int box_width = 100; int box_height = 100; ! int icon_width = 48; ! int icon_height = 48; int icon_from_top = 5; --- 32,37 ---- int box_width = 100; int box_height = 100; ! int icon_width = MAX_ICON_WIDTH; ! int icon_height = MAX_ICON_HEIGHT; int icon_from_top = 5; *************** *** 78,191 **** } - void parseItem(xmlDocPtr doc, xmlNodePtr cur) { - xmlChar *key; - cur = cur->xmlChildrenNode; - item_struct *item; - - if (item_count+1 >= MAX_ITEMS) { - fprintf(stderr,"Reached maximum of items, bailing out..."); - return; - } - - item = (item_struct *) malloc(sizeof(item_struct)); - item->selected = FALSE; - while (cur != NULL) { - if ((!xmlStrcmp(cur->name, (const xmlChar *) "command"))) { - key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); - item->command = (gchar *)malloc(strlen(key) + 1); - strncpy(item->command,key,strlen(key)+1); - xmlFree(key); - } - if ((!xmlStrcmp(cur->name, (const xmlChar *) "name"))) { - key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); - item->name = (gchar *)malloc(strlen(key) + 1); - strncpy(item->name,key,strlen(key)+1); - xmlFree(key); - } - if ((!xmlStrcmp(cur->name, (const xmlChar *) "tip"))) { - key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); - item->tip = (gchar *)malloc(strlen(key)+1); - strncpy(item->tip,key,strlen(key) + 1); - xmlFree(key); - } - if ((!xmlStrcmp(cur->name, (const xmlChar *) "icon"))) { - key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); - item->iconpath = (gchar *)malloc(strlen(key)+ 1); - strncpy(item->iconpath,key,strlen(key)+1); - xmlFree(key); - } - cur = cur->next; - } - - if (item->command != NULL && item->iconpath != NULL) { - items[item_count++] = item; - } - else { - fprintf(stderr,"Found incomplete item!\n"); - } - return; - } - - void parseGlobal(xmlDocPtr doc, xmlNodePtr cur) { - xmlChar *key; - cur = cur->xmlChildrenNode; - - minimumx = -1; - minimumy = -1; - - while (cur != NULL) { - - if ((!xmlStrcmp(cur->name, (const xmlChar *) "minimumx"))) { - key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); - minimumx = atoi(key); - xmlFree(key); - } - if ((!xmlStrcmp(cur->name, (const xmlChar *) "minimumy"))) { - key = xmlNodeListGetString(doc, cur->xmlChildrenNode,1); - minimumy = atoi(key); - xmlFree(key); - } - cur = cur->next; - } - return; - } - - void parseDoc(char *docname) { - xmlDocPtr doc; - xmlNodePtr cur; - - doc = xmlParseFile(docname); - - if (doc == NULL) { - fprintf(stderr,"Document not parsed succesfully! \n"); - return; - } - cur = xmlDocGetRootElement(doc); - - if (cur == NULL) { - fprintf(stderr,"empty document\n"); - return; - } - - if (xmlStrcmp(cur->name, (const xmlChar *) "mcp")) { - fprintf(stderr,"document of the wrong type, root node != mcp"); - xmlFreeDoc(doc); - return; - } - cur = cur->xmlChildrenNode; - while (cur != NULL) { - if ((!xmlStrcmp(cur->name, (const xmlChar *) "item"))) { - parseItem(doc,cur); - } - if ((!xmlStrcmp(cur->name, (const xmlChar *) "global"))) { - parseGlobal(doc,cur); - } - cur = cur->next; - } - xmlFreeDoc(doc); - return; - } - - /* this could use a diagram to clear it up :) --- 72,75 ---- *************** *** 227,231 **** int y_coord = top_border + (nr_y * box_height + nr_y * top_border); ! if (x_coord + side_border * 2 > minimumx) { nr_x = 0; nr_y++; --- 111,115 ---- int y_coord = top_border + (nr_y * box_height + nr_y * top_border); ! if (x_coord + side_border * 2 > minimum_width) { nr_x = 0; nr_y++; *************** *** 240,243 **** --- 124,128 ---- color.green = 0; gtk_widget_modify_fg(da,GTK_STATE_NORMAL,&color); + items[i]->selected = FALSE; } else { *************** *** 263,266 **** --- 148,186 ---- } + GdkPixbuf *assign_fallback(gchar *reassign) { + GdkPixbuf *pix; + + reassign = g_strdup_printf("/usr/share/pixmaps/no.xpm"); + pix = gdk_pixbuf_new_from_file(reassign,NULL); + if (pix == NULL) { + fprintf(stderr,"Couldn't use fallback icon %s\nAborting",reassign); + exit(1); + } + return pix; + } + + void set_minimum_size() { + gint i; + int nr_x = 0; + int nr_y = 0; + int x_coord = -1; + int y_coord = -1; + for (i = 0; i < item_count; i++) { + items[i]->selected = FALSE; + x_coord = side_border + (nr_x * box_width + nr_x * side_border); + y_coord = top_border + (nr_y * box_height + nr_y * top_border); + + if (x_coord + side_border * 2 > minimum_width) { + nr_x = 0; + nr_y++; + } + x_coord = side_border + (nr_x * box_width + nr_x * side_border); + y_coord = top_border + (nr_y * box_height + nr_y * top_border); + nr_x++; + } + minimum_width = MINIMUM_WIDTH; + minimum_height = y_coord + box_height; + } + void on_window1_show (GtkWidget *widget, *************** *** 268,279 **** { GtkWidget *drawingarea = lookup_widget(MainWindow,"MainDrawingArea"); - item_count = 0; - parseDoc(MCP_CONF); gint i; ! if (minimumx > 0 && minimumy > 0) { ! // printf("Size request: %d %d\n",minimumx, minimumy); ! // gtk_widget_set_size_request (MainWindow, minimumx, minimumy); } GError *error = NULL; for (i = 0; i < item_count; i++) { --- 188,207 ---- { GtkWidget *drawingarea = lookup_widget(MainWindow,"MainDrawingArea"); gint i; ! gtk_window_set_resizable(GTK_WINDOW(MainWindow),FALSE); ! ! item_count = 0; ! gtk_window_set_title(GTK_WINDOW(MainWindow),window_name); ! set_minimum_size(); ! if (minimum_width > 0 && minimum_height > 0) { ! printf("Size request: %d %d\n",minimum_width, minimum_height); ! gtk_widget_set_size_request (drawingarea,minimum_width, minimum_height); ! gtk_widget_set_size_request (MainWindow,minimum_width, minimum_height+20); ! gtk_window_set_default_size (GTK_WINDOW(MainWindow), minimum_width, minimum_height); } + + fprintf(stderr,"Number of items: %d\n",item_count); + GError *error = NULL; for (i = 0; i < item_count; i++) { *************** *** 285,292 **** if (pix == NULL) { fprintf(stderr,"Couldn't find icon %s\n",items[i]->iconpath); } else { ! items[i]->pixbuf = pix; } } da = drawingarea; --- 213,227 ---- if (pix == NULL) { fprintf(stderr,"Couldn't find icon %s\n",items[i]->iconpath); + free(items[i]->iconpath); + pix = assign_fallback(items[i]->iconpath); } else { ! if (gdk_pixbuf_get_width(pix) > MAX_ICON_WIDTH || gdk_pixbuf_get_height(pix) > MAX_ICON_HEIGHT) { ! fprintf(stderr,"Icon too large: %s\n",items[i]->iconpath); ! free(items[i]->iconpath); ! pix = assign_fallback(items[i]->iconpath); ! } } + items[i]->pixbuf = pix; } da = drawingarea; *************** *** 295,299 **** gint width, height; - gtk_widget_add_events(da,GDK_BUTTON_PRESS_MASK); --- 230,233 ---- *************** *** 303,310 **** RedrawIconviewer(pixmap,da); g_object_unref(pixmap); - } - gboolean on_drawingarea1_button_press_event (GtkWidget *widget, --- 237,242 ---- *************** *** 328,332 **** int y_coord = top_border + (nr_y * box_height + nr_y * top_border); ! if (x_coord + side_border * 2 > minimumx) { nr_x = 0; nr_y++; --- 260,264 ---- int y_coord = top_border + (nr_y * box_height + nr_y * top_border); ! if (x_coord + side_border * 2 > minimum_width) { nr_x = 0; nr_y++; *************** *** 354,365 **** items[i]->selected = TRUE; } - GdkPixmap *pixmap = gdk_pixmap_new(da->window,width,height,-1); - RedrawIconviewer(pixmap,da); - g_object_unref(pixmap); break; } - nr_x++; } } --- 286,296 ---- items[i]->selected = TRUE; } break; } nr_x++; } + pixmap = gdk_pixmap_new(da->window,width,height,-1); + RedrawIconviewer(pixmap,da); + g_object_unref(pixmap); } Index: interface.c =================================================================== RCS file: /cvsroot/morphix/mcp2/src/interface.c,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** interface.c 10 Nov 2003 02:04:00 -0000 1.1.1.1 --- interface.c 1 Dec 2003 21:19:44 -0000 1.2 *************** *** 32,42 **** GtkWidget *MainWindow; GtkWidget *vbox1; GtkWidget *MainDrawingArea; GtkWidget *statusbar1; MainWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL); ! gtk_widget_set_size_request (MainWindow, 400, 250); ! gtk_window_set_title (GTK_WINDOW (MainWindow), _("Morphix Configuration Panel")); ! gtk_window_set_default_size (GTK_WINDOW (MainWindow), 400, 250); gtk_window_set_destroy_with_parent (GTK_WINDOW (MainWindow), TRUE); --- 32,41 ---- GtkWidget *MainWindow; GtkWidget *vbox1; + GtkWidget *scrolledwindow1; GtkWidget *MainDrawingArea; GtkWidget *statusbar1; MainWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL); ! gtk_window_set_title (GTK_WINDOW (MainWindow), _("Morphix Control Panel")); gtk_window_set_destroy_with_parent (GTK_WINDOW (MainWindow), TRUE); *************** *** 45,51 **** gtk_container_add (GTK_CONTAINER (MainWindow), vbox1); MainDrawingArea = gtk_drawing_area_new (); gtk_widget_show (MainDrawingArea); ! gtk_box_pack_start (GTK_BOX (vbox1), MainDrawingArea, TRUE, TRUE, 0); statusbar1 = gtk_statusbar_new (); --- 44,55 ---- gtk_container_add (GTK_CONTAINER (MainWindow), vbox1); + scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL); + gtk_widget_show (scrolledwindow1); + gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 0); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); + MainDrawingArea = gtk_drawing_area_new (); gtk_widget_show (MainDrawingArea); ! gtk_container_add (GTK_CONTAINER (scrolledwindow1), MainDrawingArea); statusbar1 = gtk_statusbar_new (); *************** *** 96,99 **** --- 100,104 ---- GLADE_HOOKUP_OBJECT_NO_REF (MainWindow, MainWindow, "MainWindow"); GLADE_HOOKUP_OBJECT (MainWindow, vbox1, "vbox1"); + GLADE_HOOKUP_OBJECT (MainWindow, scrolledwindow1, "scrolledwindow1"); GLADE_HOOKUP_OBJECT (MainWindow, MainDrawingArea, "MainDrawingArea"); GLADE_HOOKUP_OBJECT (MainWindow, statusbar1, "statusbar1"); Index: main.c =================================================================== RCS file: /cvsroot/morphix/mcp2/src/main.c,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** main.c 10 Nov 2003 02:04:00 -0000 1.1.1.1 --- main.c 1 Dec 2003 21:19:44 -0000 1.2 *************** *** 1,7 **** - /* - * Initial main.c file generated by Glade. Edit as required. - * Glade will not overwrite this file. - */ - #ifdef HAVE_CONFIG_H # include <config.h> --- 1,2 ---- *************** *** 9,17 **** --- 4,16 ---- #include <gtk/gtk.h> + #include <stdio.h> #include "interface.h" #include "support.h" + #include "data.h" GtkWidget *MainWindow; + gchar *window_name = NULL; + gchar *config_file; int *************** *** 19,37 **** { #ifdef ENABLE_NLS ! bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR); ! bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); ! textdomain (GETTEXT_PACKAGE); #endif ! gtk_set_locale (); ! gtk_init (&argc, &argv); - add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps"); - /* - * The following code was added by Glade to create one of each component - * (except popup menus), just so that you see something after building - * the project. Delete any components that you don't want shown initially. - */ MainWindow = create_MainWindow (); gtk_widget_show (MainWindow); --- 18,38 ---- { #ifdef ENABLE_NLS ! bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR); ! bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); ! textdomain (GETTEXT_PACKAGE); #endif ! gtk_set_locale (); ! if (argc != 2) { ! fprintf(stderr,"Usage: mcp configfile\n"); ! exit(1); ! } ! config_file = g_strdup(argv[1]); ! parseDoc(config_file); ! gtk_init (&argc, &argv); ! ! add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps"); MainWindow = create_MainWindow (); gtk_widget_show (MainWindow); |