fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_arg.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Optional argument initialization code for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 // OPTIONAL initialization code for a program using fltk.
20 // You do not need to call this! Feel free to make up your own switches.
21 
22 #include <FL/Fl.H>
23 #include <FL/x.H>
24 #include <FL/Fl_Window.H>
25 #include <FL/Fl_Tooltip.H>
26 #include <FL/filename.H>
27 #include <FL/fl_draw.H>
28 #include <ctype.h>
29 #include "flstring.h"
30 
31 #if defined(WIN32) || defined(__APPLE__)
32 int XParseGeometry(const char*, int*, int*, unsigned int*, unsigned int*);
33 # define NoValue 0x0000
34 # define XValue 0x0001
35 # define YValue 0x0002
36 # define WidthValue 0x0004
37 # define HeightValue 0x0008
38 # define AllValues 0x000F
39 # define XNegative 0x0010
40 # define YNegative 0x0020
41 #endif
42 
43 static int fl_match(const char *a, const char *s, int atleast = 1) {
44  const char *b = s;
45  while (*a && (*a == *b || tolower(*a) == *b)) {a++; b++;}
46  return !*a && b >= s+atleast;
47 }
48 
49 // flags set by previously parsed arguments:
50 extern char fl_show_iconic; // in Fl_x.cxx
51 static char arg_called;
52 static char return_i;
53 static const char *name;
54 static const char *geometry;
55 static const char *title;
56 // these are in Fl_get_system_colors and are set by the switches:
57 extern const char *fl_fg;
58 extern const char *fl_bg;
59 extern const char *fl_bg2;
60 
130 int Fl::arg(int argc, char **argv, int &i) {
131  arg_called = 1;
132  const char *s = argv[i];
133 
134  if (!s) {i++; return 1;} // something removed by calling program?
135 
136  // a word that does not start with '-', or a word after a '--', or
137  // the word '-' by itself all start the "non-switch arguments" to
138  // a program. Return 0 to indicate that we don't understand the
139  // word, but set a flag (return_i) so that args() will return at
140  // that point:
141  if (s[0] != '-' || s[1] == '-' || !s[1]) {return_i = 1; return 0;}
142  s++; // point after the dash
143 
144  if (fl_match(s, "iconic")) {
145  fl_show_iconic = 1;
146  i++;
147  return 1;
148  } else if (fl_match(s, "kbd")) {
150  i++;
151  return 1;
152  } else if (fl_match(s, "nokbd", 3)) {
154  i++;
155  return 1;
156  } else if (fl_match(s, "dnd", 2)) {
157  Fl::dnd_text_ops(1);
158  i++;
159  return 1;
160  } else if (fl_match(s, "nodnd", 3)) {
161  Fl::dnd_text_ops(0);
162  i++;
163  return 1;
164  } else if (fl_match(s, "tooltips", 2)) {
166  i++;
167  return 1;
168  } else if (fl_match(s, "notooltips", 3)) {
170  i++;
171  return 1;
172  }
173 #ifdef __APPLE__
174  // The Finder application in MacOS X passes the "-psn_N_NNNNN" option
175  // to all apps...
176  else if (strcmp(s, "NSDocumentRevisionsDebugMode") == 0) {
177  i++;
178  if (argv[i]) i++;
179  return 1;
180  } else if (strncmp(s, "psn_", 4) == 0) {
181  i++;
182  return 1;
183  }
184 #endif // __APPLE__
185 
186  const char *v = argv[i+1];
187  if (i >= argc-1 || !v)
188  return 0; // all the rest need an argument, so if missing it is an error
189 
190  if (fl_match(s, "geometry")) {
191 
192  int flags, gx, gy; unsigned int gw, gh;
193  flags = XParseGeometry(v, &gx, &gy, &gw, &gh);
194  if (!flags) return 0;
195  geometry = v;
196 
197 #if !defined(WIN32) && !defined(__APPLE__)
198  } else if (fl_match(s, "display", 2)) {
199  Fl::display(v);
200 #endif
201 
202  } else if (fl_match(s, "title", 2)) {
203  title = v;
204 
205  } else if (fl_match(s, "name", 2)) {
206  name = v;
207 
208  } else if (fl_match(s, "bg2", 3) || fl_match(s, "background2", 11)) {
209  fl_bg2 = v;
210 
211  } else if (fl_match(s, "bg", 2) || fl_match(s, "background", 10)) {
212  fl_bg = v;
213 
214  } else if (fl_match(s, "fg", 2) || fl_match(s, "foreground", 10)) {
215  fl_fg = v;
216 
217  } else if (fl_match(s, "scheme", 1)) {
218  Fl::scheme(v);
219 
220  } else return 0; // unrecognized
221 
222  i += 2;
223  return 2;
224 }
225 
226 
282 int Fl::args(int argc, char** argv, int& i, Fl_Args_Handler cb) {
283  arg_called = 1;
284  i = 1; // skip argv[0]
285  while (i < argc) {
286  if (cb && cb(argc,argv,i)) continue;
287  if (!arg(argc,argv,i)) return return_i ? i : 0;
288  }
289  return i;
290 }
291 
292 // show a main window, use any parsed arguments
293 void Fl_Window::show(int argc, char **argv) {
294  if (argc && !arg_called) Fl::args(argc,argv);
295 
297 
298 #if !defined(WIN32) && !defined(__APPLE__)
299  // Get defaults for drag-n-drop and focus...
300  const char *key = 0, *val;
301 
303  if (!key) key = "fltk";
304 
305  val = XGetDefault(fl_display, key, "dndTextOps");
306  if (val) Fl::dnd_text_ops(strcasecmp(val, "true") == 0 ||
307  strcasecmp(val, "on") == 0 ||
308  strcasecmp(val, "yes") == 0);
309 
310  val = XGetDefault(fl_display, key, "tooltips");
311  if (val) Fl_Tooltip::enable(strcasecmp(val, "true") == 0 ||
312  strcasecmp(val, "on") == 0 ||
313  strcasecmp(val, "yes") == 0);
314 
315  val = XGetDefault(fl_display, key, "visibleFocus");
316  if (val) Fl::visible_focus(strcasecmp(val, "true") == 0 ||
317  strcasecmp(val, "on") == 0 ||
318  strcasecmp(val, "yes") == 0);
319 #endif // !WIN32 && !__APPLE__
320 
321  // set colors first, so background_pixel is correct:
322  static char beenhere;
323  if (!beenhere) {
324  if (geometry) {
325  int fl = 0, gx = x(), gy = y(); unsigned int gw = w(), gh = h();
326  fl = XParseGeometry(geometry, &gx, &gy, &gw, &gh);
327  if (fl & XNegative) gx = Fl::w()-w()+gx;
328  if (fl & YNegative) gy = Fl::h()-h()+gy;
329  // int mw,mh; minsize(mw,mh);
330  // if (mw > gw) gw = mw;
331  // if (mh > gh) gh = mh;
332  Fl_Widget *r = resizable();
333  if (!r) resizable(this);
334  // for WIN32 we assume window is not mapped yet:
335  if (fl & (XValue | YValue))
336  x(-1), resize(gx,gy,gw,gh);
337  else
338  size(gw,gh);
339  resizable(r);
340  }
341  }
342 
343  // set the class, which is used by X version of get_system_colors:
344  if (name) {xclass(name); name = 0;}
345  else if (!xclass()) xclass(fl_filename_name(argv[0]));
346 
347  if (title) {label(title); title = 0;}
348  else if (!label()) label(xclass());
349 
350  if (!beenhere) {
351  beenhere = 1;
352  Fl::scheme(Fl::scheme()); // opens display! May call Fl::fatal()
353  }
354 
355  // Show the window AFTER we have set the colors and scheme.
356  show();
357 
358 #if !defined(WIN32) && !defined(__APPLE__)
359  // set the command string, used by state-saving window managers:
360  int j;
361  int n=0; for (j=0; j<argc; j++) n += strlen(argv[j])+1;
362  char *buffer = new char[n];
363  char *p = buffer;
364  for (j=0; j<argc; j++) for (const char *q = argv[j]; (*p++ = *q++););
365  XChangeProperty(fl_display, fl_xid(this), XA_WM_COMMAND, XA_STRING, 8, 0,
366  (unsigned char *)buffer, p-buffer-1);
367  delete[] buffer;
368 #endif // !WIN32 && !__APPLE__
369 }
370 
371 // Calls useful for simple demo programs, with automatic help message:
372 
373 static const char * const helpmsg =
374 "options are:\n"
375 " -bg2 color\n"
376 " -bg color\n"
377 " -di[splay] host:n.n\n"
378 " -dn[d]\n"
379 " -fg color\n"
380 " -g[eometry] WxH+X+Y\n"
381 " -i[conic]\n"
382 " -k[bd]\n"
383 " -na[me] classname\n"
384 " -nod[nd]\n"
385 " -nok[bd]\n"
386 " -not[ooltips]\n"
387 " -s[cheme] scheme\n"
388 " -ti[tle] windowtitle\n"
389 " -to[oltips]";
390 
391 const char * const Fl::help = helpmsg+13;
392 
401 void Fl::args(int argc, char **argv) {
402  int i; if (Fl::args(argc,argv,i) < argc) Fl::error(helpmsg);
403 }
404 
405 #if defined(WIN32) || defined(__APPLE__)
406 
407 /* the following function was stolen from the X sources as indicated. */
408 
409 /* Copyright Massachusetts Institute of Technology 1985, 1986, 1987 */
410 /* $XConsortium: XParseGeom.c,v 11.18 91/02/21 17:23:05 rws Exp $ */
411 
412 /*
413 Permission to use, copy, modify, distribute, and sell this software and its
414 documentation for any purpose is hereby granted without fee, provided that
415 the above copyright notice appear in all copies and that both that
416 copyright notice and this permission notice appear in supporting
417 documentation, and that the name of M.I.T. not be used in advertising or
418 publicity pertaining to distribution of the software without specific,
419 written prior permission. M.I.T. makes no representations about the
420 suitability of this software for any purpose. It is provided "as is"
421 without express or implied warranty.
422 */
423 
424 /*
425  * XParseGeometry parses strings of the form
426  * "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
427  * width, height, xoffset, and yoffset are unsigned integers.
428  * Example: "=80x24+300-49"
429  * The equal sign is optional.
430  * It returns a bitmask that indicates which of the four values
431  * were actually found in the string. For each value found,
432  * the corresponding argument is updated; for each value
433  * not found, the corresponding argument is left unchanged.
434  */
435 
436 static int ReadInteger(char* string, char** NextString)
437 {
438  int Result = 0;
439  int Sign = 1;
440 
441  if (*string == '+')
442  string++;
443  else if (*string == '-') {
444  string++;
445  Sign = -1;
446  }
447  for (; (*string >= '0') && (*string <= '9'); string++) {
448  Result = (Result * 10) + (*string - '0');
449  }
450  *NextString = string;
451  if (Sign >= 0)
452  return (Result);
453  else
454  return (-Result);
455 }
456 
457 int XParseGeometry(const char* string, int* x, int* y,
458  unsigned int* width, unsigned int* height)
459 {
460  int mask = NoValue;
461  char *strind;
462  unsigned int tempWidth = 0, tempHeight = 0;
463  int tempX = 0, tempY = 0;
464  char *nextCharacter;
465 
466  if ( (string == NULL) || (*string == '\0')) return(mask);
467  if (*string == '=')
468  string++; /* ignore possible '=' at beg of geometry spec */
469 
470  strind = (char *)string;
471  if (*strind != '+' && *strind != '-' && *strind != 'x') {
472  tempWidth = ReadInteger(strind, &nextCharacter);
473  if (strind == nextCharacter)
474  return (0);
475  strind = nextCharacter;
476  mask |= WidthValue;
477  }
478 
479  if (*strind == 'x' || *strind == 'X') {
480  strind++;
481  tempHeight = ReadInteger(strind, &nextCharacter);
482  if (strind == nextCharacter)
483  return (0);
484  strind = nextCharacter;
485  mask |= HeightValue;
486  }
487 
488  if ((*strind == '+') || (*strind == '-')) {
489  if (*strind == '-') {
490  strind++;
491  tempX = -ReadInteger(strind, &nextCharacter);
492  if (strind == nextCharacter)
493  return (0);
494  strind = nextCharacter;
495  mask |= XNegative;
496 
497  } else {
498  strind++;
499  tempX = ReadInteger(strind, &nextCharacter);
500  if (strind == nextCharacter)
501  return(0);
502  strind = nextCharacter;
503  }
504  mask |= XValue;
505  if ((*strind == '+') || (*strind == '-')) {
506  if (*strind == '-') {
507  strind++;
508  tempY = -ReadInteger(strind, &nextCharacter);
509  if (strind == nextCharacter)
510  return(0);
511  strind = nextCharacter;
512  mask |= YNegative;
513 
514  } else {
515  strind++;
516  tempY = ReadInteger(strind, &nextCharacter);
517  if (strind == nextCharacter)
518  return(0);
519  strind = nextCharacter;
520  }
521  mask |= YValue;
522  }
523  }
524 
525  /* If strind isn't at the end of the string the it's an invalid
526  geometry specification. */
527 
528  if (*strind != '\0') return (0);
529 
530  if (mask & XValue)
531  *x = tempX;
532  if (mask & YValue)
533  *y = tempY;
534  if (mask & WidthValue)
535  *width = tempWidth;
536  if (mask & HeightValue)
537  *height = tempHeight;
538  return (mask);
539 }
540 
541 #endif // ifdef WIN32
542 
543 //
544 // End of "$Id$".
545 //
Fl_Widget::y
int y() const
Definition: Fl_Widget.H:289
Fl_Group::resizable
Fl_Widget * resizable() const
Definition: Fl_Group.H:152
Fl::get_system_colors
static void get_system_colors()
Definition: Fl_get_system_colors.cxx:228
Fl.H
Fl_Window::show
virtual void show()
Definition: Fl_win32.cxx:2542
fl_fg
const char * fl_fg
Definition: Fl_get_system_colors.cxx:87
Fl::args
static int args(int argc, char **argv, int &i, Fl_Args_Handler cb=0)
Definition: Fl_arg.cxx:282
geometry
static const char * geometry
Definition: Fl_arg.cxx:54
x.H
filename.H
Fl_Widget::x
int x() const
Definition: Fl_Widget.H:284
NULL
#define NULL
Definition: forms.H:34
helpmsg
static const char *const helpmsg
Definition: Fl_arg.cxx:373
Fl::arg
static int arg(int argc, char **argv, int &i)
Definition: Fl_arg.cxx:130
b
long b
Definition: jpegint.h:397
key
int key
Definition: Fl_Text_Editor.cxx:91
Fl::h
static int h()
Definition: Fl_win32.cxx:571
Fl::display
static void display(const char *)
Definition: Fl_display.cxx:32
Fl_Widget::w
int w() const
Definition: Fl_Widget.H:294
buffer
static char * buffer
Definition: file.cxx:215
Fl::first_window
static Fl_Window * first_window()
Definition: Fl.cxx:751
Fl::dnd_text_ops
static int dnd_text_ops()
Definition: Fl.H:1213
Fl_Window.H
Fl_Window::xclass
const char * xclass() const
Definition: Fl_Window.cxx:287
p
static menustate * p
Definition: Fl_Menu.cxx:606
fl_draw.H
utility header to pull drawing functions together
fl_filename_name
const char * fl_filename_name(const char *filename)
Definition: Fl_win32.cxx:2179
fl_bg2
const char * fl_bg2
Definition: Fl_get_system_colors.cxx:89
Fl_Window::resize
virtual void resize(int X, int Y, int W, int H)
Definition: Fl_win32.cxx:1643
Fl_Tooltip::enable
static void enable(int b=1)
Definition: Fl_Tooltip.H:55
cb
static void cb(Fl_Widget *, void *v)
Definition: factory.cxx:937
fl_display
FL_EXPORT HINSTANCE fl_display
END TIMERS.
Definition: Fl_win32.cxx:2147
Fl_Widget
Definition: Fl_Widget.H:101
Fl_Widget::h
int h() const
Definition: Fl_Widget.H:299
Fl_Tooltip::disable
static void disable()
Definition: Fl_Tooltip.H:57
Fl_Tooltip.H
Fl_Window::label
const char * label() const
Definition: Fl_Window.H:450
Fl_Widget::size
void size(int W, int H)
Definition: Fl_Widget.H:341
return_i
static char return_i
Definition: Fl_arg.cxx:52
x
int x
Definition: test.c:73
Fl::visible_focus
static int visible_focus()
Definition: Fl.H:1197
y
int y
Definition: test.c:74
fl_match
static int fl_match(const char *a, const char *s, int atleast=1)
Definition: Fl_arg.cxx:43
flstring.h
title
static const char * title
Definition: Fl_arg.cxx:55
arg_called
static char arg_called
Definition: Fl_arg.cxx:51
Fl::scheme
static const char * scheme()
Definition: Fl.H:339
Fl::w
static int w()
Definition: Fl_win32.cxx:579
Fl::error
static void(* error)(const char *,...)
Definition: Fl.H:513
name
static const char * name
Definition: Fl_arg.cxx:53
fl_bg
const char * fl_bg
Definition: Fl_get_system_colors.cxx:88
fl_show_iconic
char fl_show_iconic
Definition: Fl_win32.cxx:1799
Fl::help
static const char *const help
Definition: Fl.H:315
beenhere
static uchar beenhere
Definition: fl_color.cxx:57
fl_xid
Window fl_xid(const Fl_Window *)
Fl_Args_Handler
int(* Fl_Args_Handler)(int argc, char **argv, int &i)
Definition: Fl.H:119