~ubuntu-branches/ubuntu/quantal/openmotif/quantal

« back to all changes in this revision

Viewing changes to demos/programs/todo/todo.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Bauer
  • Date: 2010-06-23 12:12:31 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100623121231-u89gxdp51sg9wjj2
Tags: 2.3.0-1
* New Maintainer (Closes: #379258) 
* Acknowledge NMU changes
* New upstream release (Closes: #494375)
* Get rid of security patches as they are already part of new upstream
  release (00-xpmvuln.openmotif.patch, 342092-CVE-2005-3964.patch)
* Bump Standards to 3.8.4
* Added {misc:Depends} to make the package lintian cleaner
* Fix weak-library-dev-dependency by adding ${binary:Version}) for the
  -dev Package of openmotif
* Let package depend on autotools-dev to use newer autotools-helper-files
* Work around an autoconf-bug (Gentoo-Bug #1475)
* Added Client-side anti-aliased fonts support via XFT
* Added UTF-8 and UTF8_STRING atom support
* Ability to show text and pixmaps in Label, LabelGadget and all
  derived widgets
* Support of PNG/JPEG image formats in the same way as XPM is supported
* Increase FILE_OFFSET_BITS to 64 to show files >2GB in file-selector
  Idea taken from Magne Oestlyngen (Closes: #288537)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 *  @OPENGROUP_COPYRIGHT@
 
3
 *  COPYRIGHT NOTICE
 
4
 *  Copyright (c) 1990, 1991, 1992, 1993 Open Software Foundation, Inc.
 
5
 *  Copyright (c) 1996, 1997, 1998, 1999, 2000 The Open Group
 
6
 *  ALL RIGHTS RESERVED (MOTIF). See the file named COPYRIGHT.MOTIF for
 
7
 *  the full copyright text.
 
8
 *  
 
9
 *  This software is subject to an open license. It may only be
 
10
 *  used on, with or for operating systems which are themselves open
 
11
 *  source systems. You must contact The Open Group for a license
 
12
 *  allowing distribution and sublicensing of this software on, with,
 
13
 *  or for operating systems which are not Open Source programs.
 
14
 *  
 
15
 *  See http://www.opengroup.org/openmotif/license for full
 
16
 *  details of the license agreement. Any use, reproduction, or
 
17
 *  distribution of the program constitutes recipient's acceptance of
 
18
 *  this agreement.
 
19
 *  
 
20
 *  EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS
 
21
 *  PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
22
 *  KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
 
23
 *  WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY
 
24
 *  OR FITNESS FOR A PARTICULAR PURPOSE
 
25
 *  
 
26
 *  EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT
 
27
 *  NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT,
 
28
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
29
 *  DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED
 
30
 *  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
31
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 
32
 *  ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE
 
33
 *  EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
 
34
 *  POSSIBILITY OF SUCH DAMAGES.
 
35
 */
 
36
/* 
 
37
 * HISTORY
 
38
 */
 
39
 
 
40
#ifdef REV_INFO
 
41
#ifndef lint
 
42
static char *rcsid = "$XConsortium: todo.c /main/6 1995/07/14 09:46:43 drk $";
 
43
#endif
 
44
#endif
 
45
 
 
46
#include <pwd.h>
 
47
#include <unistd.h>
 
48
#include <stdlib.h>
 
49
/* X include files */
 
50
#include <X11/Intrinsic.h>
 
51
#include <X11/StringDefs.h>
 
52
#include <X11/Xatom.h>
 
53
/* Motif include files */
 
54
#include <Xm/Xm.h>
 
55
#include <Xm/CascadeBG.h>
 
56
#include <Xm/FileSB.h>
 
57
#include <Xm/Label.h>
 
58
#include <Xm/MainW.h>
 
59
#include <Xm/Notebook.h>
 
60
#include <Xm/PushBG.h>
 
61
#include <Xm/RowColumn.h>
 
62
#include <Xm/ScrolledW.h>
 
63
#include <Xm/Separator.h>
 
64
#include <Xm/Text.h>
 
65
/* Demo include files */
 
66
#include <Xmd/Help.h>
 
67
#include <Xmd/Menus.h>
 
68
#include <Xmd/Print.h>
 
69
 
 
70
#include "page.h"
 
71
 
 
72
#define APP_CLASS "XmdTodo"
 
73
 
 
74
#define MAX(x,y) ((x) > (y) ? (x) : (y))
 
75
#define MIN(x,y) ((x) > (y) ? (y) : (x))
 
76
 
 
77
char * fallback_resources[] = {
 
78
"*text.rows: 24",
 
79
"*text.columns: 80",
 
80
"*print_manager.printerList: lp,./todo.txt",
 
81
"*help_manager.helpFile: todo",
 
82
"*notebook.frameShadowThickness: 2",
 
83
"*notebook.bindingType: XmSPIRAL",
 
84
NULL
 
85
};
 
86
 
 
87
/* Options */
 
88
OptionsRec options;
 
89
 
 
90
#define Offset(field) XtOffsetOf(OptionsRec, field)
 
91
 
 
92
XtResource resources[] = {
 
93
  {"todoFile", "TodoFile", XtRString, sizeof(String), 
 
94
    Offset(todoFile), XtRImmediate, NULL}
 
95
};
 
96
 
 
97
#undef Offset
 
98
 
 
99
XrmOptionDescRec optionDesc[] = {
 
100
  {"-todoFile", "*todoFile", XrmoptionSepArg, (XtPointer) NULL}
 
101
};
 
102
 
 
103
static void QuitAppl(Widget, char *, XmPushButtonCallbackStruct *);
 
104
static void TextChanged(Widget, XtPointer, XtPointer);
 
105
 
 
106
void manageCB(Widget, Widget, XtPointer);
 
107
void PresentFDialog(Widget, XtPointer, XmPushButtonCallbackStruct*);
 
108
void New(Widget, char*, XmPushButtonCallbackStruct *);
 
109
void Save(Widget, char*, XmFileSelectionBoxCallbackStruct *);
 
110
void Open(Widget, char*, XmFileSelectionBoxCallbackStruct *);
 
111
void Print(Widget, char*, XmdPrintCallbackStruct *);
 
112
void SaveIt(Widget, char*, XmPushButtonCallbackStruct *);
 
113
void PageChange(Widget, XtPointer, XmNotebookCallbackStruct *);
 
114
void NewPage(Widget, XtPointer, XmPushButtonCallbackStruct *);
 
115
void DeletePage(Widget, XtPointer, XmPushButtonCallbackStruct *);
 
116
void EditPage(Widget, XtPointer, XmPushButtonCallbackStruct *);
 
117
void SetPage(int);
 
118
void help_cb(Widget, XtPointer, XmAnyCallbackStruct *);
 
119
 
 
120
extern void ReadDB(char*);
 
121
extern void SaveDB(char*);
 
122
 
 
123
extern Page pages[];
 
124
 
 
125
Widget shell, notebook, textw, labelw;
 
126
Widget help_widget, file_dialog;
 
127
int currentPage = 1;
 
128
int modified;
 
129
extern int maxpages;
 
130
struct passwd *user;
 
131
 
 
132
int 
 
133
main(int argc, char* argv[]) 
 
134
{
 
135
  Widget mainw, menubar;
 
136
  Widget *file_menu, *edit_menu, *selected_menu, *help_menu, temp;
 
137
  Widget print_widget;
 
138
  Cardinal size;
 
139
  XtAppContext context;
 
140
  Arg args[10];
 
141
  int n, i;
 
142
  XmString tmp;
 
143
  char temppath[256];
 
144
 
 
145
  if (argc == 2 && strcmp(argv[1], "-help") == 0) {
 
146
    printf("Usage: todo [-todoFile pathname]\n");
 
147
    return(0);
 
148
  }
 
149
 
 
150
  shell         = XtVaAppInitialize(&context, APP_CLASS,
 
151
                                    optionDesc, XtNumber(optionDesc), 
 
152
                                    &argc, argv,
 
153
                                    fallback_resources, 0);
 
154
 
 
155
  XtGetApplicationResources(shell, (XtPointer) &options,
 
156
                            resources, XtNumber(resources), (Arg *) NULL, 0);
 
157
 
 
158
  mainw         = XmCreateMainWindow(shell, "mainw", NULL, 0);
 
159
  XtManageChild(mainw);
 
160
 
 
161
  menubar       = XmCreateMenuBar(mainw, "menub", NULL, 0);
 
162
  XtManageChild(menubar);
 
163
 
 
164
  n = 0;
 
165
  XtSetArg(args[n], XmNcurrentPageNumber, 1); n++;
 
166
  XtSetArg(args[n], XmNlastPageNumber, 100); n++;
 
167
  notebook      = XmCreateNotebook(mainw, "notebook", args, n);
 
168
  XtManageChild(notebook);
 
169
  XtAddCallback(notebook, XmNpageChangedCallback, 
 
170
                (XtCallbackProc) PageChange, NULL);
 
171
 
 
172
  n = 0;
 
173
  XtSetArg(args[n], XmNmenuBar, menubar); n++;
 
174
  XtSetValues(mainw, args, n);
 
175
 
 
176
  n = 0;
 
177
  XtSetArg(args[n], XmNpageNumber, 1); n++;
 
178
  XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
 
179
  textw         = XmCreateScrolledText(notebook, "text", args, n);
 
180
  XtManageChild(textw);
 
181
  XtAddCallback(textw, XmNvalueChangedCallback, TextChanged, NULL);
 
182
 
 
183
  n = 0;
 
184
  XtSetArg(args[n], XmNnotebookChildType, XmSTATUS_AREA); n++;
 
185
  XtSetArg(args[n], XmNpageNumber, 1); n++;
 
186
  labelw        = XmCreateLabel(notebook, "label", args, n);
 
187
  XtManageChild(labelw);
 
188
 
 
189
  XmdCreateMenu(FILE_MENU, menubar, &file_menu, &size);
 
190
  XtAddCallback(file_menu[FILE_NEW], XmNactivateCallback, 
 
191
                (XtCallbackProc) New, NULL);
 
192
  XtAddCallback(file_menu[FILE_OPEN], XmNactivateCallback,
 
193
                (XtCallbackProc) PresentFDialog, (XtPointer) Open);
 
194
  XtAddCallback(file_menu[FILE_EXIT], XmNactivateCallback, 
 
195
                (XtCallbackProc) QuitAppl, NULL);
 
196
  XtAddCallback(file_menu[FILE_SAVE_AS], XmNactivateCallback,
 
197
                (XtCallbackProc) PresentFDialog, (XtPointer) Save);
 
198
  XtAddCallback(file_menu[FILE_SAVE], XmNactivateCallback,
 
199
                (XtCallbackProc) SaveIt, NULL);
 
200
 
 
201
  XmdCreateMenu(SELECTED_MENU, menubar, &selected_menu, &size);
 
202
  XtUnmanageChildren(selected_menu, size);
 
203
  XtManageChild(selected_menu[SELECTED_PROPERTIES]);
 
204
  XtAddCallback(selected_menu[SELECTED_PROPERTIES], 
 
205
                XmNactivateCallback, (XtCallbackProc) EditPage, NULL);
 
206
  XtManageChild(selected_menu[SELECTED_NEW]);
 
207
  XtAddCallback(selected_menu[SELECTED_NEW], 
 
208
                XmNactivateCallback, (XtCallbackProc) NewPage, NULL);
 
209
  XtManageChild(selected_menu[SELECTED_DELETE]);
 
210
  XtAddCallback(selected_menu[SELECTED_DELETE],
 
211
                XmNactivateCallback, (XtCallbackProc) DeletePage, NULL);
 
212
 
 
213
  XmdCreateMenu(HELP_MENU, menubar, &help_menu, &size);
 
214
  XtUnmanageChildren(help_menu, size);
 
215
  XtManageChild(help_menu[HELP_OVERVIEW]);
 
216
 
 
217
  print_widget = XmdCreatePrintDialog(shell, "print_manager", NULL, 0);
 
218
  XtAddCallback(print_widget, XmdNprintCallback, (XtCallbackProc) Print, NULL);
 
219
  tmp = XmStringCreateLocalized("About Printing");
 
220
 
 
221
  XtAddCallback(file_menu[FILE_PRINT], XmNactivateCallback, 
 
222
                (XtCallbackProc) manageCB, (XtPointer) print_widget);
 
223
 
 
224
  help_widget = XmdCreateHelpDialog(shell, "help_manager", NULL, 0);
 
225
  XtAddCallback(help_menu[HELP_OVERVIEW], XmNactivateCallback,
 
226
                (XtCallbackProc) help_cb, (XtPointer) 0);
 
227
  XtAddCallback(print_widget, XmNhelpCallback,
 
228
                (XtCallbackProc) help_cb, (XtPointer) 1);
 
229
 
 
230
  n = 0;
 
231
  XtSetArg(args[n], XmNautoUnmanage, True); n++;
 
232
  file_dialog = XmCreateFileSelectionDialog(shell, "file_dialog", args, n);
 
233
 
 
234
  user = getpwuid(getuid());
 
235
  for (i = 0; i < MAXPAGES; i++) {
 
236
    pages[i] = NULL;
 
237
  }
 
238
 
 
239
  if (options.todoFile == NULL) {
 
240
    strcpy(temppath, user -> pw_dir);
 
241
    strcat(temppath, "/.todo");
 
242
    options.todoFile = XtNewString(temppath);
 
243
  } else {
 
244
    /* Copy the string for consistency */
 
245
    options.todoFile = XtNewString(options.todoFile);
 
246
  }
 
247
 
 
248
  XtVaSetValues(shell, XmNtitle, options.todoFile,
 
249
                XmNtitleEncoding, XA_STRING, NULL, NULL);  
 
250
  ReadDB(options.todoFile);
 
251
  SetPage(0);
 
252
 
 
253
  XtRealizeWidget(shell);
 
254
 
 
255
  XtAppMainLoop(context);
 
256
 
 
257
  return 0;    /* make compiler happy */
 
258
}
 
259
 
 
260
static void 
 
261
QuitAppl(Widget w, char *i, XmPushButtonCallbackStruct *e) 
 
262
{
 
263
  exit(0);
 
264
}
 
265
 
 
266
static void 
 
267
TextChanged(Widget w, XtPointer i, XtPointer i2) 
 
268
{
 
269
  modified = 1;
 
270
}
 
271
 
 
272
void manageCB( widget, w_to_manage, callback_data)
 
273
     Widget widget;
 
274
     Widget w_to_manage;
 
275
     XtPointer callback_data;
 
276
{
 
277
  if (w_to_manage != (Widget) NULL)
 
278
    XtManageChild(w_to_manage);
 
279
}
 
280
 
 
281
void
 
282
PresentFDialog(Widget w, XtPointer cb, XmPushButtonCallbackStruct *cs)
 
283
{
 
284
  XtRemoveAllCallbacks(file_dialog, XmNokCallback);
 
285
  XtAddCallback(file_dialog, XmNokCallback, (XtCallbackProc) cb, NULL);
 
286
  XtManageChild(file_dialog);
 
287
}
 
288
 
 
289
void
 
290
New(Widget w, char* ignore, XmPushButtonCallbackStruct *cs)
 
291
{
 
292
  char buf[128];
 
293
  char *str;
 
294
  Boolean found = False;
 
295
  int i = 0;
 
296
  
 
297
  while(! found) {
 
298
    sprintf(buf, "untitled%d.todo", i++);
 
299
    found = access(buf, F_OK) != 0;
 
300
  }
 
301
 
 
302
  str = XtNewString(buf);
 
303
  ReadDB(str);
 
304
  XtFree(options.todoFile);
 
305
  options.todoFile = str;
 
306
  XtVaSetValues(shell, XmNtitle, str, XmNtitleEncoding, 
 
307
                XA_STRING, NULL, NULL);
 
308
  SetPage(0);
 
309
}
 
310
 
 
311
void 
 
312
Save(Widget w, char *i, XmFileSelectionBoxCallbackStruct *i2) 
 
313
{
 
314
  char *str;
 
315
  if ((str = XmStringUnparse(i2->value, NULL, XmCHARSET_TEXT, 
 
316
                            XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL))) {
 
317
    SaveDB(str);
 
318
    XtFree(options.todoFile);
 
319
    options.todoFile = str;
 
320
    XtVaSetValues(shell, XmNtitle, str, XmNtitleEncoding, 
 
321
                  XA_STRING, NULL, NULL);
 
322
  }
 
323
}
 
324
 
 
325
void 
 
326
Open(Widget w, char *i, XmFileSelectionBoxCallbackStruct *i2) 
 
327
{
 
328
  char *str;
 
329
  if ((str = XmStringUnparse(i2->value, NULL, XmCHARSET_TEXT, 
 
330
                            XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL))) {
 
331
    ReadDB(str);
 
332
    XtFree(options.todoFile);
 
333
    options.todoFile = str;
 
334
    XtVaSetValues(shell, XmNtitle, str, XmNtitleEncoding, 
 
335
                  XA_STRING, NULL, NULL);
 
336
  }
 
337
}
 
338
 
 
339
void
 
340
help_cb(Widget w, XtPointer item, XmAnyCallbackStruct *cb)
 
341
{
 
342
  XtManageChild(help_widget);
 
343
  XmdGotoHelpItem(w, (int) item, help_widget);
 
344
}
 
345
 
 
346
void 
 
347
Print(Widget w, char *ignore, XmdPrintCallbackStruct *cb)
 
348
{
 
349
  int i;
 
350
  FILE *temp;
 
351
  int from, to;
 
352
 
 
353
  temp = fopen("/tmp/.todoout", "w");
 
354
 
 
355
  if (cb -> first == cb -> last &&
 
356
      cb -> first == 0) {
 
357
    from = 0; 
 
358
    to = maxpages - 1;
 
359
  } else {
 
360
    from = MAX(0, cb -> first - 1);
 
361
    to = MIN(maxpages, cb -> last - 1);
 
362
  }
 
363
 
 
364
  for (i = from; i <= to; i++) {
 
365
    if (pages[i] -> label != NULL) {
 
366
      fprintf(temp, "Subject: %s\n", pages[i] -> label);
 
367
      fprintf(temp, "---------------------------\n\n\n");
 
368
    }
 
369
    fprintf(temp, "%s", pages[i] -> page);
 
370
    if (i != (maxpages - 1)) fprintf(temp, "\f");
 
371
  }
 
372
 
 
373
  fclose(temp);
 
374
  XmdPrintDocument("/tmp/.todoout", cb);
 
375
}