1 /*
2  * Motif
3  *
4  * Copyright (c) 1987-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * HISTORY
25  */
26 
27 #ifdef REV_INFO
28 #ifndef lint
29 static char *rcsid = "$XConsortium: todo.c /main/6 1995/07/14 09:46:43 drk $";
30 #endif
31 #endif
32 
33 #include <pwd.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 /* X include files */
37 #include <X11/Intrinsic.h>
38 #include <X11/StringDefs.h>
39 #include <X11/Xatom.h>
40 /* Motif include files */
41 #include <Xm/Xm.h>
42 #include <Xm/CascadeBG.h>
43 #include <Xm/FileSB.h>
44 #include <Xm/Label.h>
45 #include <Xm/MainW.h>
46 #include <Xm/Notebook.h>
47 #include <Xm/PushBG.h>
48 #include <Xm/RowColumn.h>
49 #include <Xm/ScrolledW.h>
50 #include <Xm/Separator.h>
51 #include <Xm/Text.h>
52 /* Demo include files */
53 #include <Xmd/Help.h>
54 #include <Xmd/Menus.h>
55 #include <Xmd/Print.h>
56 
57 #include "page.h"
58 
59 #define APP_CLASS "XmdTodo"
60 
61 #define MAX(x,y) ((x) > (y) ? (x) : (y))
62 #define MIN(x,y) ((x) > (y) ? (y) : (x))
63 
64 char * fallback_resources[] = {
65 "*text.rows: 24",
66 "*text.columns: 80",
67 "*print_manager.printerList: lp,./todo.txt",
68 "*help_manager.helpFile: todo",
69 "*notebook.frameShadowThickness: 2",
70 "*notebook.bindingType:	XmSPIRAL",
71 NULL
72 };
73 
74 /* Options */
75 OptionsRec options;
76 
77 #define Offset(field) XtOffsetOf(OptionsRec, field)
78 
79 XtResource resources[] = {
80   {"todoFile", "TodoFile", XtRString, sizeof(String),
81     Offset(todoFile), XtRImmediate, NULL}
82 };
83 
84 #undef Offset
85 
86 XrmOptionDescRec optionDesc[] = {
87   {"-todoFile", "*todoFile", XrmoptionSepArg, (XtPointer) NULL}
88 };
89 
90 static void QuitAppl(Widget, char *, XmPushButtonCallbackStruct *);
91 static void TextChanged(Widget, XtPointer, XtPointer);
92 
93 void manageCB(Widget, Widget, XtPointer);
94 void PresentFDialog(Widget, XtPointer, XmPushButtonCallbackStruct*);
95 void New(Widget, char*, XmPushButtonCallbackStruct *);
96 void Save(Widget, char*, XmFileSelectionBoxCallbackStruct *);
97 void Open(Widget, char*, XmFileSelectionBoxCallbackStruct *);
98 void Print(Widget, char*, XmdPrintCallbackStruct *);
99 void SaveIt(Widget, char*, XmPushButtonCallbackStruct *);
100 void PageChange(Widget, XtPointer, XmNotebookCallbackStruct *);
101 void NewPage(Widget, XtPointer, XmPushButtonCallbackStruct *);
102 void DeletePage(Widget, XtPointer, XmPushButtonCallbackStruct *);
103 void EditPage(Widget, XtPointer, XmPushButtonCallbackStruct *);
104 void SetPage(int);
105 void help_cb(Widget, XtPointer, XmAnyCallbackStruct *);
106 
107 extern void ReadDB(char*);
108 extern void SaveDB(char*);
109 
110 extern Page pages[];
111 
112 Widget shell, notebook, textw, labelw;
113 Widget help_widget, file_dialog;
114 int currentPage = 1;
115 int modified;
116 extern int maxpages;
117 struct passwd *user;
118 
119 int
main(int argc,char * argv[])120 main(int argc, char* argv[])
121 {
122   Widget mainw, menubar;
123   Widget *file_menu, *edit_menu, *selected_menu, *help_menu, temp;
124   Widget print_widget;
125   Cardinal size;
126   XtAppContext context;
127   Arg args[10];
128   int n, i;
129   XmString tmp;
130   char temppath[256];
131 
132   if (argc == 2 && strcmp(argv[1], "-help") == 0) {
133     printf("Usage: todo [-todoFile pathname]\n");
134     return(0);
135   }
136 
137   shell		= XtVaAppInitialize(&context, APP_CLASS,
138 				    optionDesc, XtNumber(optionDesc),
139 				    &argc, argv,
140 				    fallback_resources, NULL);
141 
142   XtGetApplicationResources(shell, (XtPointer) &options,
143 			    resources, XtNumber(resources), (Arg *) NULL, 0);
144 
145   mainw		= XmCreateMainWindow(shell, "mainw", NULL, 0);
146   XtManageChild(mainw);
147 
148   menubar	= XmCreateMenuBar(mainw, "menub", NULL, 0);
149   XtManageChild(menubar);
150 
151   n = 0;
152   XtSetArg(args[n], XmNcurrentPageNumber, 1); n++;
153   XtSetArg(args[n], XmNlastPageNumber, 100); n++;
154   notebook	= XmCreateNotebook(mainw, "notebook", args, n);
155   XtManageChild(notebook);
156   XtAddCallback(notebook, XmNpageChangedCallback,
157 		(XtCallbackProc) PageChange, NULL);
158 
159   n = 0;
160   XtSetArg(args[n], XmNmenuBar, menubar); n++;
161   XtSetValues(mainw, args, n);
162 
163   n = 0;
164   XtSetArg(args[n], XmNpageNumber, 1); n++;
165   XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
166   textw		= XmCreateScrolledText(notebook, "text", args, n);
167   XtManageChild(textw);
168   XtAddCallback(textw, XmNvalueChangedCallback, TextChanged, NULL);
169 
170   n = 0;
171   XtSetArg(args[n], XmNnotebookChildType, XmSTATUS_AREA); n++;
172   XtSetArg(args[n], XmNpageNumber, 1); n++;
173   labelw	= XmCreateLabel(notebook, "label", args, n);
174   XtManageChild(labelw);
175 
176   XmdCreateMenu(FILE_MENU, menubar, &file_menu, &size);
177   XtAddCallback(file_menu[FILE_NEW], XmNactivateCallback,
178 		(XtCallbackProc) New, NULL);
179   XtAddCallback(file_menu[FILE_OPEN], XmNactivateCallback,
180 		(XtCallbackProc) PresentFDialog, (XtPointer) Open);
181   XtAddCallback(file_menu[FILE_EXIT], XmNactivateCallback,
182 		(XtCallbackProc) QuitAppl, NULL);
183   XtAddCallback(file_menu[FILE_SAVE_AS], XmNactivateCallback,
184 		(XtCallbackProc) PresentFDialog, (XtPointer) Save);
185   XtAddCallback(file_menu[FILE_SAVE], XmNactivateCallback,
186 		(XtCallbackProc) SaveIt, NULL);
187 
188   XmdCreateMenu(SELECTED_MENU, menubar, &selected_menu, &size);
189   XtUnmanageChildren(selected_menu, size);
190   XtManageChild(selected_menu[SELECTED_PROPERTIES]);
191   XtAddCallback(selected_menu[SELECTED_PROPERTIES],
192 		XmNactivateCallback, (XtCallbackProc) EditPage, NULL);
193   XtManageChild(selected_menu[SELECTED_NEW]);
194   XtAddCallback(selected_menu[SELECTED_NEW],
195 		XmNactivateCallback, (XtCallbackProc) NewPage, NULL);
196   XtManageChild(selected_menu[SELECTED_DELETE]);
197   XtAddCallback(selected_menu[SELECTED_DELETE],
198   		XmNactivateCallback, (XtCallbackProc) DeletePage, NULL);
199 
200   XmdCreateMenu(HELP_MENU, menubar, &help_menu, &size);
201   XtUnmanageChildren(help_menu, size);
202   XtManageChild(help_menu[HELP_OVERVIEW]);
203 
204   print_widget = XmdCreatePrintDialog(shell, "print_manager", NULL, 0);
205   XtAddCallback(print_widget, XmdNprintCallback, (XtCallbackProc) Print, NULL);
206   tmp = XmStringCreateLocalized("About Printing");
207 
208   XtAddCallback(file_menu[FILE_PRINT], XmNactivateCallback,
209 		(XtCallbackProc) manageCB, (XtPointer) print_widget);
210 
211   help_widget = XmdCreateHelpDialog(shell, "help_manager", NULL, 0);
212   XtAddCallback(help_menu[HELP_OVERVIEW], XmNactivateCallback,
213 		(XtCallbackProc) help_cb, (XtPointer) 0);
214   XtAddCallback(print_widget, XmNhelpCallback,
215 		(XtCallbackProc) help_cb, (XtPointer) 1);
216 
217   n = 0;
218   XtSetArg(args[n], XmNautoUnmanage, True); n++;
219   file_dialog = XmCreateFileSelectionDialog(shell, "file_dialog", args, n);
220 
221   user = getpwuid(getuid());
222   for (i = 0; i < MAXPAGES; i++) {
223     pages[i] = NULL;
224   }
225 
226   if (options.todoFile == NULL) {
227     strcpy(temppath, user -> pw_dir);
228     strcat(temppath, "/.todo");
229     options.todoFile = XtNewString(temppath);
230   } else {
231     /* Copy the string for consistency */
232     options.todoFile = XtNewString(options.todoFile);
233   }
234 
235   XtVaSetValues(shell, XmNtitle, options.todoFile,
236 		XmNtitleEncoding, XA_STRING, NULL, NULL);
237   ReadDB(options.todoFile);
238   SetPage(0);
239 
240   XtRealizeWidget(shell);
241 
242   XtAppMainLoop(context);
243 
244   return 0;    /* make compiler happy */
245 }
246 
247 static void
QuitAppl(Widget w,char * i,XmPushButtonCallbackStruct * e)248 QuitAppl(Widget w, char *i, XmPushButtonCallbackStruct *e)
249 {
250   exit(0);
251 }
252 
253 static void
TextChanged(Widget w,XtPointer i,XtPointer i2)254 TextChanged(Widget w, XtPointer i, XtPointer i2)
255 {
256   modified = 1;
257 }
258 
manageCB(widget,w_to_manage,callback_data)259 void manageCB( widget, w_to_manage, callback_data)
260      Widget widget;
261      Widget w_to_manage;
262      XtPointer callback_data;
263 {
264   if (w_to_manage != (Widget) NULL)
265     XtManageChild(w_to_manage);
266 }
267 
268 void
PresentFDialog(Widget w,XtPointer cb,XmPushButtonCallbackStruct * cs)269 PresentFDialog(Widget w, XtPointer cb, XmPushButtonCallbackStruct *cs)
270 {
271   XtRemoveAllCallbacks(file_dialog, XmNokCallback);
272   XtAddCallback(file_dialog, XmNokCallback, (XtCallbackProc) cb, NULL);
273   XtManageChild(file_dialog);
274 }
275 
276 void
New(Widget w,char * ignore,XmPushButtonCallbackStruct * cs)277 New(Widget w, char* ignore, XmPushButtonCallbackStruct *cs)
278 {
279   char buf[128];
280   char *str;
281   Boolean found = False;
282   int i = 0;
283 
284   while(! found) {
285     sprintf(buf, "untitled%d.todo", i++);
286     found = access(buf, F_OK) != 0;
287   }
288 
289   str = XtNewString(buf);
290   ReadDB(str);
291   XtFree(options.todoFile);
292   options.todoFile = str;
293   XtVaSetValues(shell, XmNtitle, str, XmNtitleEncoding,
294 		XA_STRING, NULL, NULL);
295   SetPage(0);
296 }
297 
298 void
Save(Widget w,char * i,XmFileSelectionBoxCallbackStruct * i2)299 Save(Widget w, char *i, XmFileSelectionBoxCallbackStruct *i2)
300 {
301   char *str;
302   if ((str = XmStringUnparse(i2->value, NULL, XmCHARSET_TEXT,
303 			    XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL))) {
304     SaveDB(str);
305     XtFree(options.todoFile);
306     options.todoFile = str;
307     XtVaSetValues(shell, XmNtitle, str, XmNtitleEncoding,
308 		  XA_STRING, NULL, NULL);
309   }
310 }
311 
312 void
Open(Widget w,char * i,XmFileSelectionBoxCallbackStruct * i2)313 Open(Widget w, char *i, XmFileSelectionBoxCallbackStruct *i2)
314 {
315   char *str;
316   if ((str = XmStringUnparse(i2->value, NULL, XmCHARSET_TEXT,
317 			    XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL))) {
318     ReadDB(str);
319     XtFree(options.todoFile);
320     options.todoFile = str;
321     XtVaSetValues(shell, XmNtitle, str, XmNtitleEncoding,
322 		  XA_STRING, NULL, NULL);
323   }
324 }
325 
326 void
help_cb(Widget w,XtPointer item,XmAnyCallbackStruct * cb)327 help_cb(Widget w, XtPointer item, XmAnyCallbackStruct *cb)
328 {
329   XtManageChild(help_widget);
330   XmdGotoHelpItem(w, (int) item, help_widget);
331 }
332 
333 void
Print(Widget w,char * ignore,XmdPrintCallbackStruct * cb)334 Print(Widget w, char *ignore, XmdPrintCallbackStruct *cb)
335 {
336   int i;
337   FILE *temp;
338   int from, to;
339 
340   temp = fopen("/tmp/.todoout", "w");
341 
342   if (cb -> first == cb -> last &&
343       cb -> first == 0) {
344     from = 0;
345     to = maxpages - 1;
346   } else {
347     from = MAX(0, cb -> first - 1);
348     to = MIN(maxpages, cb -> last - 1);
349   }
350 
351   for (i = from; i <= to; i++) {
352     if (pages[i] -> label != NULL) {
353       fprintf(temp, "Subject: %s\n", pages[i] -> label);
354       fprintf(temp, "---------------------------\n\n\n");
355     }
356     fprintf(temp, "%s", pages[i] -> page);
357     if (i != (maxpages - 1)) fprintf(temp, "\f");
358   }
359 
360   fclose(temp);
361   XmdPrintDocument("/tmp/.todoout", cb);
362 }
363