xmore.c revision 9c125d91
1/* 2 * $Xorg: xmore.c,v 1.1 2004/04/30 02:05:54 gisburn Exp $ 3 * 4Copyright 2004 Roland Mainz <roland.mainz@nrubsig.org> 5 6Permission to use, copy, modify, distribute, and sell this software and its 7documentation for any purpose is hereby granted without fee, provided that 8the above copyright notice appear in all copies and that both that 9copyright notice and this permission notice appear in supporting 10documentation. 11 12The above copyright notice and this permission notice shall be included in 13all copies or substantial portions of the Software. 14 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22Except as contained in this notice, the name of The Open Group shall not be 23used in advertising or otherwise to promote the sale, use or other dealings 24in this Software without prior written authorization from The Open Group. 25 * 26 */ 27 28/* Force ANSI C prototypes from X11 headers */ 29#ifndef FUNCPROTO 30#define FUNCPROTO 15 31#endif /* !FUNCPROTO */ 32 33#include <X11/StringDefs.h> 34#include <X11/Intrinsic.h> 35#include <X11/Shell.h> 36#include <X11/Xaw/Form.h> 37#include <X11/Xaw/List.h> 38#include <X11/Xaw/Command.h> 39#include <X11/Xaw/AsciiText.h> 40#include <X11/Xaw/Cardinals.h> 41 42#include "xmore.h" 43 44#include <stdlib.h> 45#include <stdio.h> 46#include <limits.h> 47 48/* Turn a NULL pointer string into an empty string */ 49#define NULLSTR(x) (((x)!=NULL)?(x):("")) 50 51#define Error(x) { printf x ; exit(EXIT_FAILURE); } 52#define Assertion(expr, msg) { if (!(expr)) { Error msg } } 53#define Log(x) { if (userOptions.verbose) printf x; } 54 55/* Global vars */ 56static Widget toplevel = NULL; 57static Widget text = NULL; 58static const char *ProgramName; /* program name (from argv[0]) */ 59static const char *viewFileName; /* file to browse (from argv[1]) */ 60 61/* prototypes */ 62static void quitAction(Widget w, XEvent *event, String *params, Cardinal *num_params); 63static void quitXtProc(Widget w, XtPointer client_data, XtPointer callData); 64 65static XrmOptionDescRec options[] = { 66{ 67 "-v", "*verbose", XrmoptionNoArg, (XPointer) "on" }, 68}; 69 70 71static XtActionsRec actions[] = { 72 { "quit", quitAction }, 73}; 74 75/* See xmore.h */ 76static XMoreResourceData userOptions; 77 78#define Offset(field) XtOffsetOf(XMoreResourceData, field) 79 80static XtResource resources[] = { 81 {"verbose", "Verbose", XtRBoolean, sizeof(Boolean), Offset(verbose), XtRImmediate, (XtPointer)False}, 82 {"textfont", XtCFont, XtRFontSet, sizeof(XFontSet), Offset(textfont), XtRString, STANDARDFONT}, 83}; 84 85 86static String fallback_resources[] = { 87#ifdef NOTYET 88 "*iconPixmap: xmore32", 89 "*iconMask: xmore32", 90#endif /* NOTYET */ 91 "*textfont: " STANDARDFONT, 92 "*international: True", /* set this globally for ALL widgets to avoid wiered crashes */ 93 "*text.Translations: #override \\n\\" 94 "\tCtrl<Key>S: no-op(RingBell)\\n\\" 95 "\tCtrl<Key>R: no-op(RingBell)\\n\\" 96 "\t<Key>space: next-page()\\n\\" 97 "\t<Key>F: next-page()\\n\\" 98 "\tCtrl<Key>B: previous-page()\\n\\" 99 "\t<Key>B: previous-page()\\n\\" 100 "\t<Key>K: scroll-one-line-down()\\n\\" 101 "\t<Key>Y: scroll-one-line-down()\\n\\" 102 "\t<Key>Return: scroll-one-line-up()\\n\\" 103 "\t<Key>J: scroll-one-line-up()\\n\\" 104 "\t<Key>E: scroll-one-line-up()\\n\\" 105 "\t<Key>q: quit()\\n", 106 "*text.baseTranslations: #override \\n\\" 107 "\t<Key>space: next-page()\\n\\" 108 "\t<Key>F: next-page()\\n\\" 109 "\tCtrl<Key>B: previous-page()\\n\\" 110 "\t<Key>K: scroll-one-line-down()\\n\\" 111 "\t<Key>Y: scroll-one-line-down()\\n\\" 112 "\t<Key>Return: scroll-one-line-up()\\n\\" 113 "\t<Key>J: scroll-one-line-up()\\n\\" 114 "\t<Key>E: scroll-one-line-up()\\n\\" 115 "\t<Key>q: quit()\\n", 116 NULL, 117}; 118 119static void 120quitAction(Widget w, XEvent *event, String *params, Cardinal *num_params) 121{ 122 XtAppSetExitFlag(XtWidgetToApplicationContext(w)); 123} 124 125static void 126quitXtProc(Widget w, XtPointer client_data, XtPointer callData) 127{ 128 XtCallActionProc(w, "quit", NULL, NULL, 0); 129} 130 131int main( int argc, char *argv[] ) 132{ 133 XtAppContext app; 134 Widget form; 135 Widget quitbutton; 136 int n; 137 Arg args[8]; 138 139 ProgramName = argv[0]; 140 141 XtSetLanguageProc(NULL, NULL, NULL); 142 toplevel = XtOpenApplication(&app, "XMore", 143 options, XtNumber(options), 144 &argc, argv, fallback_resources, 145 sessionShellWidgetClass, NULL, ZERO); 146 147 if (argc != 2) 148 { 149 printf("usage: %s [ x options ] filename\n", argv[0]); 150 exit(EXIT_FAILURE); 151 } 152 153 XtGetApplicationResources(toplevel, (XtPointer)&userOptions, resources, 154 XtNumber(resources), NULL, 0); 155 156 XtAppAddActions(app, actions, XtNumber(actions)); 157 158 viewFileName = argv[1]; 159 160 form = XtCreateManagedWidget("form", formWidgetClass, toplevel, NULL, 0); 161 162 n = 0; 163 XtSetArg(args[n], XtNtype, XawAsciiFile); n++; 164 XtSetArg(args[n], XtNstring, viewFileName); n++; 165 XtSetArg(args[n], XtNwidth, 700); n++; 166 XtSetArg(args[n], XtNheight, 300); n++; 167 XtSetArg(args[n], XtNscrollHorizontal, XawtextScrollAlways); n++; 168 XtSetArg(args[n], XtNscrollVertical, XawtextScrollAlways); n++; 169 XtSetArg(args[n], XtNfontSet, userOptions.textfont); n++; 170 text = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, n); 171 172 n = 0; 173 XtSetArg(args[n], XtNfromHoriz, NULL); n++; 174 XtSetArg(args[n], XtNfromVert, text); n++; 175 XtSetArg(args[n], XtNlabel, "Quit"); n++; 176 quitbutton = XtCreateManagedWidget("quit", commandWidgetClass, form, args, n); 177 XtAddCallback(quitbutton, XtNcallback, quitXtProc, NULL); 178 179 XtRealizeWidget(toplevel); 180 181 XtAppMainLoop(app); 182 183 return EXIT_SUCCESS; 184} 185 186