1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
* Copyright (C) 1994 by Dave Glowacki
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* to rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* DAVE GLOWACKI BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Header: /usr/sww/share/src/X11R6/local/applications/xless-1.7/RCS/window.c,v 1.24 1994/07/29 02:34:16 dglo Exp $
*/
#include <stdio.h>
#include <fcntl.h>
#include <X11/X.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xmu/Converters.h>
#include "xless.h"
#include "XLessWin.icn"
/* list of all xless windows */
WindowInfo *windowlist = 0;
int windowcount = 0;
/* flag for "too many windows" popup */
static int tooManyFlag = 0;
static void popdownTooMany __P((Widget, XtPointer, XtPointer));
WindowInfo *
createWindowInfo()
{
WindowInfo *wi;
wi = (WindowInfo *)XtMalloc((Cardinal )sizeof(WindowInfo));
wi->base = wi->text = wi->toolbox = NULL;
wi->searchPopup = wi->newWindowPopup = wi->changeFilePopup = NULL;
wi->editorButton = wi->reloadButton = NULL;
wi->memory = NULL;
wi->allocated = 0;
wi->used = 0;
wi->file = NULL;
wi->flag = XLessClearFlag;
wi->dataHeight = wi->dataWidth = 0;
wi->searchBuf = wi->newWindowBuf = wi->changeFileBuf = NULL;
wi->inputInfo = NULL;
return(wi);
}
WindowInfo *
findWindowInfo(w)
Widget w;
{
WindowInfo *wi = windowlist;
while (w) {
/* if this is the shell widget... */
if (XtClass(w) == applicationShellWidgetClass) {
/* find this window in the list */
while (wi && (wi->base != w))
wi = wi->next;
return(wi);
}
/* see if parent is the shell widget */
w = XtParent(w);
}
/* couldn't find a window associated with this widget */
return(0);
}
static void
popdownTooMany(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
XtPopdown((Widget)closure);
tooManyFlag = 0;
}
int
CreateWindow(top, filename)
Widget top;
const char *filename;
{
int fd;
const char *shellName;
const char *geom;
Widget base, mainFrame;
WindowInfo *wi;
XLessFlag flag = XLessClearFlag;
const char *fixedName;
static Widget badFileMsg = 0;
/* make sure we haven't created *too* many windows */
if (resources.maxWindows && (windowcount >= resources.maxWindows)) {
static Widget tooManyMsg = 0;
char message[64];
if (!tooManyFlag) {
tooManyFlag = 1;
sprintf(message, "Can't have more than %d windows!",
resources.maxWindows);
if (!tooManyMsg)
tooManyMsg = MessageBox(top, message, "OK", popdownTooMany, 0);
if (tooManyMsg)
SetPopup(top, tooManyMsg);
}
return 1;
}
/* get a filehandle (or exit) */
if (filename) {
#ifdef TILDE_EXPANSION
/* see if we need to do tilde expansion */
if (filename && *filename == '~') {
filename = TildeExpand(filename);
if (*filename != '~')
flag |= XLessFreeFilename;
}
#endif /* TILDE_EXPANSION */
/* try to open the file */
fd = open(filename, O_RDONLY);
if (fd == -1) {
CouldntOpen(top, filename);
return 2;
}
fixedName = filename;
} else if (windowcount == 0) {
/* first file can come from stdin */
fixedName = "stdin";
fd = 0;
} else {
/* don't let 'em get away with this!!! */
if (!badFileMsg)
badFileMsg = MessageBox(top, "Please specify a file name!",
"OK", 0, 0);
if (badFileMsg)
SetPopup(top, badFileMsg);
return 3;
}
/* keep track of the new window */
wi = createWindowInfo();
wi->file = fixedName;
wi->flag = flag;
/* read the file into memory */
InitData(fd, wi);
/* figure out what to call the shell */
if (resources.name != NULL)
shellName = resources.name;
else
shellName = progname;
/* create a new application shell */
geom = GetGeometryPosition();
base = XtVaAppCreateShell(shellName, className, applicationShellWidgetClass,
disp,
XtNallowShellResize, TRUE,
XtNgeometry, geom,
XtNiconPixmap,
XCreateBitmapFromData(disp, XRootWindow(disp,0),
XLessWin_bits,
XLessWin_width,
XLessWin_height),
NULL);
/* set icon & title name for new window */
SetXNames(base, fixedName);
/* create the container for the subwindows */
#ifdef FRAME_IS_FORM
mainFrame = XtVaCreateManagedWidget("frame", formWidgetClass, base, NULL);
#else
mainFrame = XtVaCreateManagedWidget("frame", panedWidgetClass, base,
XtNorientation, XtorientHorizontal,
NULL);
#endif
/* build widgets for new window */
wi->base = base;
wi->text = MakeText(mainFrame, wi, wi->memory, 0);
wi->toolbox = MakeToolbox(mainFrame, wi, filename);
/* make sure text window gets all keystrokes */
XtSetKeyboardFocus(mainFrame, wi->text);
/* no dialog boxes yet */
wi->searchPopup = 0;
wi->newWindowPopup = 0;
wi->changeFilePopup = 0;
/* display the window */
XtPopup(base, XtGrabNone);
/* add this window to the list */
wi->next = windowlist;
windowlist = wi;
windowcount++;
SetWMHints(wi);
return 0;
}
void
DestroyWindowInfo(wi)
WindowInfo *wi;
{
WindowInfo *prev;
/* free all window-related widgets */
XtDestroyWidget(wi->base);
/* free all existing dialog boxes */
if (wi->searchPopup)
XtDestroyWidget(wi->searchPopup);
if (wi->newWindowPopup)
XtDestroyWidget(wi->newWindowPopup);
if (wi->changeFilePopup)
XtDestroyWidget(wi->changeFilePopup);
/* free all existing dialog box buffers */
if (wi->searchBuf)
XtFree(wi->searchBuf);
if (wi->newWindowBuf)
XtFree(wi->newWindowBuf);
if (wi->changeFileBuf)
XtFree(wi->changeFileBuf);
/* free text memory */
XtFree((char *)wi->memory);
/* free filename string if it was malloc'd */
if (wi->flag & XLessFreeFilename)
XtFree((char *)wi->file);
/* free any input info */
if (wi->inputInfo)
XtFree((char *)wi->inputInfo);
/* remove from windowlist chain */
if (windowlist == wi)
windowlist = wi->next;
else {
prev = windowlist;
while (prev && (prev->next != wi))
prev = prev->next;
if (prev)
prev->next = wi->next;
}
/* one less window to worry about... */
XtFree((char *)wi);
windowcount--;
/* nothing else to do if we've closed all the windows */
if (windowcount == 0) {
exit(0);
}
}
void
DestroyAllWindows()
{
while (windowlist)
DestroyWindowInfo(windowlist);
}
|