pcb 4.1.1
An interactive printed circuit board layout editor.

library.c

Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include <stdio.h>
00006 #include <stdarg.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00010 #include "xincludes.h"
00011 
00012 #include "compat.h"
00013 #include "global.h"
00014 #include "data.h"
00015 #include "misc.h"
00016 #include "set.h"
00017 #include "buffer.h"
00018 
00019 #include "hid.h"
00020 #include "../hidint.h"
00021 #include "lesstif.h"
00022 
00023 #ifdef HAVE_LIBDMALLOC
00024 #include <dmalloc.h>
00025 #endif
00026 
00027 static Arg args[30];
00028 static int n;
00029 #define stdarg(t,v) XtSetArg(args[n], t, v); n++
00030 
00031 static Widget library_dialog = 0;
00032 static Widget library_list, libnode_list;
00033 
00034 static XmString *library_strings = 0;
00035 static XmString *libnode_strings = 0;
00036 static int last_pick = -1;
00037 
00038 static void
00039 pick_net (int pick)
00040 {
00041   LibraryMenuType *menu = Library.Menu + pick;
00042   int i;
00043 
00044   if (pick == last_pick)
00045     return;
00046   last_pick = pick;
00047 
00048   if (libnode_strings)
00049     free (libnode_strings);
00050   libnode_strings = (XmString *) malloc (menu->EntryN * sizeof (XmString));
00051   for (i = 0; i < menu->EntryN; i++)
00052     libnode_strings[i] = XmStringCreatePCB (menu->Entry[i].ListEntry);
00053   n = 0;
00054   stdarg (XmNitems, libnode_strings);
00055   stdarg (XmNitemCount, menu->EntryN);
00056   XtSetValues (libnode_list, args, n);
00057 }
00058 
00059 static void
00060 library_browse (Widget w, void *v, XmListCallbackStruct * cbs)
00061 {
00062   pick_net (cbs->item_position - 1);
00063 }
00064 
00065 static void
00066 libnode_select (Widget w, void *v, XmListCallbackStruct * cbs)
00067 {
00068   char *args;
00069   LibraryEntryType *e =
00070     Library.Menu[last_pick].Entry + cbs->item_position - 1;
00071 
00072   if (e->Template == (char *) -1)
00073     {
00074       if (LoadElementToBuffer (PASTEBUFFER, e->AllocatedMemory, true))
00075         SetMode (PASTEBUFFER_MODE);
00076       return;
00077     }
00078   args = Concat("'", EMPTY (e->Template), "' '",
00079                 EMPTY (e->Value), "' '", EMPTY (e->Package), "'", NULL);
00080   if (LoadElementToBuffer (PASTEBUFFER, args, false))
00081     SetMode (PASTEBUFFER_MODE);
00082 }
00083 
00084 static int
00085 build_library_dialog ()
00086 {
00087   if (!mainwind)
00088     return 1;
00089   if (library_dialog)
00090     return 0;
00091 
00092   n = 0;
00093   stdarg (XmNresizePolicy, XmRESIZE_GROW);
00094   stdarg (XmNtitle, "Element Library");
00095   library_dialog = XmCreateFormDialog (mainwind, "library", args, n);
00096 
00097   n = 0;
00098   stdarg (XmNtopAttachment, XmATTACH_FORM);
00099   stdarg (XmNbottomAttachment, XmATTACH_FORM);
00100   stdarg (XmNleftAttachment, XmATTACH_FORM);
00101   stdarg (XmNvisibleItemCount, 10);
00102   library_list = XmCreateScrolledList (library_dialog, "nets", args, n);
00103   XtManageChild (library_list);
00104   XtAddCallback (library_list, XmNbrowseSelectionCallback,
00105                  (XtCallbackProc) library_browse, 0);
00106 
00107   n = 0;
00108   stdarg (XmNtopAttachment, XmATTACH_FORM);
00109   stdarg (XmNbottomAttachment, XmATTACH_FORM);
00110   stdarg (XmNrightAttachment, XmATTACH_FORM);
00111   stdarg (XmNleftAttachment, XmATTACH_WIDGET);
00112   stdarg (XmNleftWidget, library_list);
00113   libnode_list = XmCreateScrolledList (library_dialog, "nodes", args, n);
00114   XtManageChild (libnode_list);
00115   XtAddCallback (libnode_list, XmNbrowseSelectionCallback,
00116                  (XtCallbackProc) libnode_select, 0);
00117 
00118   return 0;
00119 }
00120 
00121 static char *
00122 pcb_basename (char *p)
00123 {
00124   char *rv = strrchr (p, '/');
00125   if (rv)
00126     return rv + 1;
00127   return p;
00128 }
00129 
00130 static int
00131 LibraryChanged (int argc, char **argv, Coord x, Coord y)
00132 {
00133   int i;
00134   if (!Library.MenuN)
00135     return 0;
00136   if (build_library_dialog ())
00137     return 0;
00138   last_pick = -1;
00139   if (library_strings)
00140     free (library_strings);
00141   library_strings = (XmString *) malloc (Library.MenuN * sizeof (XmString));
00142   for (i = 0; i < Library.MenuN; i++)
00143     library_strings[i] = XmStringCreatePCB (
00144                            pcb_basename (Library.Menu[i].Name));
00145   n = 0;
00146   stdarg (XmNitems, library_strings);
00147   stdarg (XmNitemCount, Library.MenuN);
00148   XtSetValues (library_list, args, n);
00149   pick_net (0);
00150   return 0;
00151 }
00152 
00153 static const char libraryshow_syntax[] =
00154 "LibraryShow()";
00155 
00156 static const char libraryshow_help[] =
00157 "Displays the library window.";
00158 
00159 /* %start-doc actions LibraryShow
00160 
00161 %end-doc */
00162 
00163 static int
00164 LibraryShow (int argc, char **argv, Coord x, Coord y)
00165 {
00166   if (build_library_dialog ())
00167     return 0;
00168   return 0;
00169 }
00170 
00171 void
00172 lesstif_show_library ()
00173 {
00174   if (mainwind)
00175     {
00176       if (!library_dialog)
00177         LibraryChanged (0, 0, 0, 0);
00178       XtManageChild (library_dialog);
00179     }
00180 }
00181 
00182 HID_Action lesstif_library_action_list[] = {
00183   {"LibraryChanged", 0, LibraryChanged,
00184    librarychanged_help, librarychanged_syntax},
00185   {"LibraryShow", 0, LibraryShow,
00186    libraryshow_help, libraryshow_syntax},
00187 };
00188 
00189 REGISTER_ACTIONS (lesstif_library_action_list)