File: l_agreement_dlg.cpp

package info (click to toggle)
xevil 2.02r2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,964 kB
  • ctags: 8,002
  • sloc: cpp: 47,780; makefile: 220; csh: 4
file content (629 lines) | stat: -rw-r--r-- 16,773 bytes parent folder | download | duplicates (8)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/* 
 * XEvil(TM) Copyright (C) 1994,2000 Steve Hardt and Michael Judge
 * http://www.xevil.com
 * satan@xevil.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, the file "gpl.txt"; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA, or visit http://www.gnu.org.
 */

// "l_agreement_dlg.cpp"

// No corresponding header file, the only entry point is through
// LAgreement::check_accpted().


#include "utils.h"
extern "C" {
#include <X11/Xutil.h>
#include <X11/Xatom.h>
}


#include "xdata.h"
#include "panel.h"
#include "l_agreement.h"


#define PREV_LABEL "Prev"
#define NEXT_LABEL "Next"
#define ACCEPT_LABEL "Accept"
#define REJECT_LABEL "Reject"



class LAgreementDlg {
public:
  enum {
    ACCEPTED,REJECTED,UNDECIDED,
  };

  LAgreementDlg(Xvars &,int dpyNum,int argc,char** argv,
                Boolean lgViewportVal,Boolean smoothScrollVal,
                Boolean reduceDrawVal);

  ~LAgreementDlg();

  void process_event(int dpyNum,XEvent *event);

  int get_status() {return status;}
  /* EFFECTS: Return ACCEPTED, REJECTED or ,UNDECIDED. */

  Boolean get_large_viewport() 
    {return optionToggles[LARGE_VIEWPORT]->get_value();}
  /* EFFECTS: Return whether we should use large viewports for XEvil. */

  Boolean get_smooth_scroll() 
    {return optionToggles[SMOOTH_SCROLL]->get_value();}

  Boolean get_reduce_draw() 
    {return !optionToggles[DRAW_BACKGROUNDS]->get_value();}
  /* NOTE: The opposite of DRAW_BACKGROUNDS. */


private:
  enum {SHAREWARE_1,PREV,NEXT,ACCEPT,REJECT,SHAREWARE_2,BUTTONS_MAX};
  enum {LARGE_VIEWPORT,SMOOTH_SCROLL,DRAW_BACKGROUNDS,OPTIONS_MAX};
  static const char* buttonLabels[BUTTONS_MAX];
  static const char* optionToggleLabels[OPTIONS_MAX];
  static const char* optionTextText[OPTIONS_MAX];

  enum {
    TEXT_PADDING = 3,
    BUTTON_VERT_PADDING = 6,
    OPTION_HORIZ_PADDING = 8,
    BUTTONS_EDGE_PADDING = 10, // On outside edges.
  };

  void check_viewed_before();
  /* EFFECTS: Check for an .xevilrc  Sets viewedBefore and rcFilename. */

  void mark_as_viewed();
  /* EFFECTS: Create a .xevilrc to mark that the user has viewed the
     dialog at least once. */

  void redraw();

  void parse(const char*);

  void compute_button_pos(Pos buttonPos[BUTTONS_MAX],
                          Size buttonSizes[BUTTONS_MAX],
                          int widthMax,
                          int top);
  /* MODIFIES: buttonPos */
  /* EFFECTS: Compute the location of all the "buttons" including the 
     shareware labels.  widthMax is the available space.
     top is the very top of the buttons. */

  static void button_callback(Panel*,void*,void*);
  void _button_callback(Panel*);
  Boolean get_button_sensitive(int);


  Xvars* xvars;
  int separatorY_1;
  int separatorY_2;
  int dpyNum;
  int status;

  // Little hack here, not all LAgreementDlg::buttons are really buttons,
  // also used for the obnoxious "SHAREWARE" labels.
  TextPanel* buttons[BUTTONS_MAX];

  PtrList pages;
  int currentPage;

  Size windowSize;
  Size fontSize;
  Window window;

  TogglePanel* optionToggles[OPTIONS_MAX];
  TextPanel* optionText[OPTIONS_MAX];

  // Has the User already seen the license agreement dialog in some
  // previous use of XEvil.
  Boolean viewedBefore;
  // The absolute filename of the .xevilrc file.
  char* rcFilename;
};



// This method is way too big, split it up a bit.
LAgreementDlg::LAgreementDlg(Xvars &x_vars,int dpy_num,int argc,char** argv,
                             Boolean lgViewportVal,Boolean smoothScrollVal,
                             Boolean reduceDrawVal) {
  xvars = &x_vars;
  dpyNum = dpy_num;
  status = UNDECIDED; // User hasn't said yes or no yet.

  // Initialize this up at the top, so we get the right button states.
  currentPage = 0;
  
  // Create content model, the list of pages.
  parse(LAgreement::get_text());


  // We should really just put this in Xvars.
  fontSize = xvars->fontSize[dpyNum];

  // Compute sizes for the buttons.
  Size buttonSizes[BUTTONS_MAX];
  int n;
  for (n = 0; n < BUTTONS_MAX; n++) {
    buttonSizes[n] = 
      TextPanel::get_unit(xvars->font[dpyNum],
                          Utils::strlen(buttonLabels[n]));
  }
  // Just used for the height, for two rows.
  Size optionTextUnit = 
    TextPanel::get_unit(xvars->font[dpyNum],1,2);


  // Create window
  windowSize.width = 
    fontSize.width * Line::get_text_columns() 
    + 2 * TEXT_PADDING;
  windowSize.height = 
    fontSize.height * Page::get_text_rows()
    + 2 * TEXT_PADDING // around the text in the main area
    + 1 // separator
    + (OPTIONS_MAX + 1) * TEXT_PADDING
    + OPTIONS_MAX * optionTextUnit.height
    + 1 // separator
    + 2 * BUTTON_VERT_PADDING
    + buttonSizes[0].height;

  window = 
    xvars->create_toplevel_window(argc,argv,dpyNum,windowSize,
                                  "XEvil License Agreement",ExposureMask);


  // Compute location of the top separator line.
  separatorY_1 = 
    fontSize.height * Page::get_text_rows()
    + 2 * TEXT_PADDING;


  // Compute max width of the option buttons.
  int optionsMaxWidth = 0;
  for (n = 0; n < OPTIONS_MAX; n++) {
    Size opSize = 
      TogglePanel::get_unit(xvars->font[dpyNum],
                            Utils::strlen(optionToggleLabels[n]),1);
    optionsMaxWidth = Utils::maximum(optionsMaxWidth,opSize.width);
  }
    

  // Now create the toggle buttons and corresponding text.
  Pos optionPos(OPTION_HORIZ_PADDING,separatorY_1 + 1 + TEXT_PADDING);
  for (n = 0; n < OPTIONS_MAX; n++) {
    // Create toggle button.
    Size optionToggleUnit = 
      TogglePanel::get_unit(xvars->font[dpyNum],
                            strlen(optionToggleLabels[n]),1);
    Pos togglePos(OPTION_HORIZ_PADDING,
                  optionPos.y + optionTextUnit.height / 2 
                  - optionToggleUnit.height / 2);
    optionToggles[n] = 
      new TogglePanel(dpyNum,*xvars,window,
                      togglePos,optionToggleUnit,
                      // Don't care about callback.  We will poll result when 
                      // dialog is popped down.
                      NULL,NULL,
                      optionToggleLabels[n]);
    assert(optionToggles[n]);

    // Create text for toggle button.
    Pos textPos(2 * OPTION_HORIZ_PADDING + optionsMaxWidth,
                optionPos.y);
    Size textUnit = 
      TextPanel::get_unit(xvars->font[dpyNum],strlen(optionTextText[n]),2);
    optionText[n] = 
      new TextPanel(dpyNum,*xvars,window,
                    textPos,textUnit,
                    NULL,NULL,
                    optionTextText[n],
                    // Don't want a border.
                    TEXT_PANEL_EVENT_MASK,False);
    assert(optionText[n]);

    optionPos.y += optionTextUnit.height;
  }

  // Set initial values
  optionToggles[LARGE_VIEWPORT]->set_value(lgViewportVal);
  optionToggles[SMOOTH_SCROLL]->set_value(smoothScrollVal);
  optionToggles[DRAW_BACKGROUNDS]->set_value(!reduceDrawVal);
  

  // Location of the bottom separator line.
  separatorY_2 =
    separatorY_1 + 1 
    + (OPTIONS_MAX + 1) * TEXT_PADDING
    + OPTIONS_MAX * optionTextUnit.height;


  // Before creating the buttons.
  // Sets initial value of viewedBefore.
  check_viewed_before();


  // Figure out where to place the buttons.
  Pos buttonPos[BUTTONS_MAX];
  compute_button_pos(buttonPos,buttonSizes,
                     windowSize.width,
                     separatorY_2 + 1 + BUTTON_VERT_PADDING);

  // Create the buttons
  for (n = 0; n < BUTTONS_MAX; n++) {

    // Shareware labels.
    if (n == SHAREWARE_1 || n == SHAREWARE_2) {
      buttons[n] = 
        new TextPanel(dpyNum,*xvars,window,
                      buttonPos[n],buttonSizes[n],
                      NULL,NULL,buttonLabels[n],
                      TEXT_PANEL_EVENT_MASK,
                      False);
    }
    // Normal buttons.
    else {
      buttons[n] = 
        new ButtonPanel(dpyNum,*xvars,window,
                        buttonPos[n],buttonSizes[n],
                        button_callback,(void*)this,
                        buttonLabels[n]);
    }
    assert(buttons[n]);
    buttons[n]->set_sensitive(get_button_sensitive(n));
  }
  
  // Pop up the window.
  XMapWindow(xvars->dpy[dpyNum],window);
}



LAgreementDlg::~LAgreementDlg() {
  Utils::freeif(rcFilename);
  
  int n;
  for (n = 0; n < BUTTONS_MAX; n++) {
    delete buttons[n];
  }

  for (n = 0; n < OPTIONS_MAX; n++) {
    delete optionToggles[n];
    delete optionText[n];
  }

  XDestroyWindow(xvars->dpy[dpyNum],window);

  for (n = 0; n < pages.length(); n++) {
    delete (Page*)pages.get(n);
  }
}



void LAgreementDlg::process_event(int dpyNum,XEvent *event) {
  // Received WM_DELETE_WINDOW.
  if (event->type == ClientMessage && event->xclient.format == 32 &&
      event->xclient.data.l[0] == xvars->wmDeleteWindow[dpyNum] &&
      event->xany.window == window) {
    status = REJECTED;
  }

  // Get expose, window close, and delegate to children
  if (event->type == Expose 
      && event->xexpose.window == window) {
    redraw();
    return;
  }

  // See if the toggle buttons or toggle labels want it.
  int n;
  for (n = 0; n < OPTIONS_MAX; n++) {
    if (optionToggles[n]->process_event(dpyNum,event)) {
      return;
    }
    if (optionText[n]->process_event(dpyNum,event)) {
      return;
    }
  }
    
  // Give event to first button that wants it.
  for (n = 0; n < BUTTONS_MAX; n++) {    
    if (buttons[n]->process_event(dpyNum,event)) {
      return;
    }
  }
}



void LAgreementDlg::check_viewed_before() {
  // Now that XEvil is GPL, we don't force the user to page through the
  // license agreement the first time XEvil is run, i.e. no more .xevilrc.
  viewedBefore = True;
  rcFilename = NULL;

#if 0
  const char* home = Utils::getenv("HOME");
  if (!home || !*home) {
    viewedBefore = False;
    rcFilename = NULL;
    return;
  }

  ostrstream str;
  str << home;
  if (home[strlen(home)-1] != '/') {
    str << "/";
  }
  str << ".xevilrc" << ends;

  // Keep rcFilename around even if the file doesn't exist yet.
  rcFilename = str.str();

  FILE* fp = fopen(rcFilename,"r");
  if (!fp) {
    viewedBefore = False;
    return;
  }
  else {
    viewedBefore = True;
    fclose(fp);
  }
#endif
}



void LAgreementDlg::mark_as_viewed() {
  if (rcFilename) {
    FILE* fp = fopen(rcFilename,"w");
    if (fp) {
      fprintf(fp,"XEvil is your friend.  Trust XEvil.\n");
      fclose(fp);
    }
  }
  // else HOME env variable is not available, so give up.
}



void LAgreementDlg::redraw() {
  XClearWindow(xvars->dpy[dpyNum],window);

  // Loop over all lines on current page.
  Page* page = (Page*)pages.get(currentPage);
  const PtrList& lines = page->get_lines();  
  for (int n = 0; n < lines.length(); n++) {
    int length;
    const char* text = ((Line*)lines.get(n))->get_text(length);
    if (length > 0) {
      XDrawString(xvars->dpy[dpyNum],window,xvars->gc[dpyNum],
                  TEXT_PADDING,
                  TEXT_PADDING 
                  + xvars->font[dpyNum]->max_bounds.ascent 
                  + fontSize.height * n,
                  text,length);                           
    }
  }

  // Draw separators.
  XDrawLine(xvars->dpy[dpyNum],window,xvars->gc[dpyNum],
            0,separatorY_1,windowSize.width - 1,separatorY_1);
  XDrawLine(xvars->dpy[dpyNum],window,xvars->gc[dpyNum],
            0,separatorY_2,windowSize.width - 1,separatorY_2);

  // Buttons will redraw themselves.
}
 

 
void LAgreementDlg::parse(const char *text) {
  const char *p = text;

  Line::set_text_columns(70);
  Page::set_text_rows(24);
  
  while (*p) {
    Page* page = new Page(&p,p);
    pages.add(page);
  }
}



void LAgreementDlg::compute_button_pos(Pos buttonPos[BUTTONS_MAX],
                                       Size buttonSizes[BUTTONS_MAX],
                                       int widthMax,
                                       int top) {
  buttonPos[SHAREWARE_1].x = BUTTONS_EDGE_PADDING;
  buttonPos[SHAREWARE_1].y = top;
  buttonPos[SHAREWARE_2].x = 
    widthMax - BUTTONS_EDGE_PADDING - buttonSizes[SHAREWARE_2].width;
  buttonPos[SHAREWARE_2].y = top;

  int bWidth = widthMax - 2 * BUTTONS_EDGE_PADDING - 
    buttonSizes[SHAREWARE_1].width - buttonSizes[SHAREWARE_2].width;

  for (int n = SHAREWARE_1 + 1; n < SHAREWARE_2; n++) {
    buttonPos[n].x = 
      buttonPos[SHAREWARE_1].x + buttonSizes[SHAREWARE_1].width +
      (int)((float)n / (float)(BUTTONS_MAX - 1) * (float)bWidth)
      - (buttonSizes[n].width / 2);
    buttonPos[n].y = top;
  }
#if 0
    Pos pos((int)((n + 1.0f) / (float)(BUTTONS_MAX + 1) 
                  * windowSize.width) 
            - (buttonSizes[n].width / 2),
            );
#endif
}



void LAgreementDlg::button_callback(Panel* panel,void*,void* closure) {
  LAgreementDlg* dlg = (LAgreementDlg*)closure;
  dlg->_button_callback(panel);
}

 

void LAgreementDlg::_button_callback(Panel* panel) {
  // Which button was pressed.  Search through four buttons, big deal.
  int button;
  for (button = 0; button < BUTTONS_MAX; button++) {
    if (buttons[button] == panel) {
      break;
    }
  }
  assert(button < BUTTONS_MAX);


  switch (button) {
  case PREV:
    assert(currentPage > 0);
    currentPage--;
    break;
  case NEXT:
    assert(currentPage < pages.length() - 1);
    currentPage++;
    break;
  case ACCEPT:
    // So user won't have to click through it again.
    mark_as_viewed(); 
    status = ACCEPTED;
    // Don't bother with a redraw.
    return;
  case REJECT:
    status = REJECTED;
    // Don't bother with a redraw.
    return;
  default:
    assert(0);
  }

  // Update whether buttons are sensitive or not.
  for (int n = 0; n < BUTTONS_MAX; n++) {
    buttons[n]->set_sensitive(get_button_sensitive(n));
  }
  redraw();
}




Boolean LAgreementDlg::get_button_sensitive(int button) {
  switch (button) {
  case PREV:
    // If not the first page.
    return currentPage > 0;
  case NEXT:
    // If not the last page.
    return currentPage < pages.length() - 1;
  case ACCEPT:
  case REJECT:
    // If the last page or if user has already seen the dialog at least
    // once.
    return viewedBefore || (currentPage == pages.length() - 1);
  case SHAREWARE_1:
  case SHAREWARE_2:
    return True;
  default:
    assert(0);
    return False;
  }
}

   

// Don't write "SHAREWARE".  XEvil is now GPL.
const char* LAgreementDlg::buttonLabels[BUTTONS_MAX] = {
  "_",    //  "SHAREWARE",
  "Prev",
  "Next",
  "Accept",
  "Reject",
  "_",    //  "SHAREWARE",
};



const char* LAgreementDlg::optionToggleLabels[OPTIONS_MAX] = {
  "Large Viewport",
  "Smooth Scroll",
  "Draw Background",
};



const char* LAgreementDlg::optionTextText[OPTIONS_MAX] = {
  "Unselect [Large Viewport] to speed up drawing,\n"
  "or to play two-player on one machine.",
  
  "Use [Smooth Scroll] if you have a machine\n"
  "with fast graphics.",

  "Only meaningful for smooth scroll.\n"
  "Unselect [Draw Background] to speed up drawing."
};



Boolean LAgreement::check_accepted(Boolean& largeViewport,
                                   Boolean& smoothScroll,
                                   Boolean& reduceDraw,
                                   Xvars &xvars,
                                   int dpyNum,
                                   int argc,char** argv) {
  // User ran "xevil -accept_agreement".
  if (commLineAccepted) {
    return True;
  }

  LAgreementDlg dlg(xvars,dpyNum,argc,argv,
                    largeViewport,smoothScroll,reduceDraw);

  // Run the dialog modally until the user chooses one way or the other.
  while (1) {
    // Grab X events.
    XEvent event;
    XNextEvent(xvars.dpy[dpyNum],&event);

    // Let the dialog handle it, return if a decision has been made.
    dlg.process_event(dpyNum,&event);
    int val = dlg.get_status();

    // Poll value of the option toggle buttons.
    largeViewport = dlg.get_large_viewport();
    smoothScroll = dlg.get_smooth_scroll();
    reduceDraw = dlg.get_reduce_draw();

    if (val == LAgreementDlg::REJECTED) {
      return False;
    }
    else if (val == LAgreementDlg::ACCEPTED) {
      return True;
    }
  }

  // LAgreementDlg destructor will pop down window.
}