DialogBox.st
changeset 118 3ee5ea99d0e2
parent 112 81633ba1bf40
child 120 710d41f17b68
equal deleted inserted replaced
117:53cbfeaa9c9a 118:3ee5ea99d0e2
    13 
    13 
    14 'From Smalltalk/X, Version:2.10.4 on 28-dec-1994 at 1:38:02 pm'!
    14 'From Smalltalk/X, Version:2.10.4 on 28-dec-1994 at 1:38:02 pm'!
    15 
    15 
    16 ModalBox subclass:#DialogBox
    16 ModalBox subclass:#DialogBox
    17 	 instanceVariableNames:'buttonPanel okButton okAction abortButton abortAction
    17 	 instanceVariableNames:'buttonPanel okButton okAction abortButton abortAction
    18 		acceptReturnAsOK'
    18 		acceptReturnAsOK yPosition leftIndent addedComponents'
    19 	 classVariableNames:''
    19 	 classVariableNames:''
    20 	 poolDictionaries:''
    20 	 poolDictionaries:''
    21 	 category:'Views-DialogBoxes'
    21 	 category:'Views-DialogBoxes'
    22 !
    22 !
    23 
    23 
    24 !DialogBox class methodsFor:'documentation'!
    24 !DialogBox class methodsFor:'documentation'!
    25 
    25 
    26 version
    26 version
    27 "
    27 "
    28 $Header: /cvs/stx/stx/libwidg/DialogBox.st,v 1.7 1995-03-26 20:15:10 claus Exp $
    28 $Header: /cvs/stx/stx/libwidg/DialogBox.st,v 1.8 1995-05-03 00:28:59 claus Exp $
    29 "
    29 "
    30 !
    30 !
    31 
    31 
    32 documentation
    32 documentation
    33 "
    33 "
    59 "
    59 "
    60 !
    60 !
    61 
    61 
    62 examples
    62 examples
    63 "
    63 "
    64     since DialogBox is abstract and meant as a base for InfoBox, YesNoBox etc,
    64     mostly, DialogBox is used as an abstract class as a base for InfoBox, 
    65     the following examples are somewhat artificial ...
    65     YesNoBox etc.
       
    66     However, you can construct dialogs programmatically, as shown in
       
    67     the following examples:
    66 
    68 
    67 
    69 
    68     DialogBox new open
    70     DialogBox new open
    69 
    71 
    70     DialogBox new addOkButton; open
    72     DialogBox new addOkButton; open
    71 
    73 
    72     DialogBox new addAbortButton; addOkButton; open
    74     DialogBox new addAbortButton; addOkButton; open
       
    75 
       
    76     DialogBox new
       
    77 	addTextLabel:'hello';
       
    78 	addAbortButton; 
       
    79 	addOkButton; 
       
    80 	open
       
    81 
       
    82     DialogBox new
       
    83 	label:'a simple dialog';
       
    84 	addTextLabel:'hello';
       
    85 	addAbortButton; 
       
    86 	addOkButton; 
       
    87 	extent:200@200;
       
    88 	sizeFixed:true;
       
    89 	open
       
    90 
       
    91     DialogBox new
       
    92 	addTextLabel:(Image fromFile:'bitmaps/garfield.gif');
       
    93 	addAbortButton; 
       
    94 	addOkButton; 
       
    95 	open
       
    96 
       
    97     DialogBox new
       
    98 	addTextLabel:'hello';
       
    99 	addTextLabel:(Image fromFile:'bitmaps/garfield.gif');
       
   100 	addTextLabel:'world';
       
   101 	addAbortButton; 
       
   102 	addOkButton; 
       
   103 	open
       
   104 
       
   105   constructing a dialog from elements:
       
   106 
       
   107    adding a fileSelectionList:
       
   108 
       
   109      |top panel l scr fileName ok|
       
   110 
       
   111      fileName := '' asValue.
       
   112 
       
   113      top := DialogBox new.
       
   114      top extent:200@300; sizeFixed:true.
       
   115 
       
   116      panel := VerticalPanelView new.
       
   117 
       
   118      l := FileSelectionList on:fileName.
       
   119      l useIndex:false.
       
   120      scr := ScrollableView forView:l.
       
   121      panel addSubView:scr.
       
   122      scr left:0.0; width:1.0.
       
   123 
       
   124      top addComponent:panel.
       
   125      top addAbortButton; addOkButton.
       
   126      top okAction:[ok := true].
       
   127      ok := false.
       
   128      top openModal.
       
   129 
       
   130      ok ifTrue:[
       
   131 	 Transcript show:'fileName: '; showCr:fileName value storeString.
       
   132      ]
       
   133 
       
   134 
       
   135    adding a panel with checkBoxes:
       
   136 
       
   137      |top panel b model value1 value2 value3 value4 ok|
       
   138 
       
   139      value1 := true asValue.
       
   140      value2 := false asValue.
       
   141      value3 := false asValue.
       
   142      value4 := true asValue.
       
   143 
       
   144      top := DialogBox new.
       
   145      top extent:200@300.
       
   146 
       
   147      panel := VerticalPanelView new.
       
   148 
       
   149      b := CheckBox on:value1.
       
   150      b label:'check1'.
       
   151      panel addSubView:b.
       
   152 
       
   153      b := CheckBox on:value2.
       
   154      b label:'check2'.
       
   155      panel addSubView:b.
       
   156 
       
   157      b := CheckBox on:value3.
       
   158      b label:'check3'.
       
   159      panel addSubView:b.
       
   160 
       
   161      b := CheckBox on:value4.
       
   162      b label:'check4'.
       
   163      panel addSubView:b.
       
   164 
       
   165      top addComponent:panel.
       
   166      top addAbortButton; addOkButton.
       
   167      top okAction:[ok := true].
       
   168      ok := false.
       
   169      top openModal.
       
   170 
       
   171      ok ifTrue:[
       
   172 	 Transcript show:'value1: '; showCr:value1 value.
       
   173 	 Transcript show:'value2: '; showCr:value2 value.
       
   174 	 Transcript show:'value3: '; showCr:value3 value.
       
   175 	 Transcript show:'value4: '; showCr:value4 value.
       
   176      ]
       
   177 
       
   178 
    73 "
   179 "
    74 !
   180 !
    75 
   181 
    76 copyright
   182 copyright
    77 "
   183 "
   120 
   226 
   121     "
   227     "
   122      Dialog 
   228      Dialog 
   123 	 request:'enter a string:' 
   229 	 request:'enter a string:' 
   124 	 initialAnswer:'the default'  
   230 	 initialAnswer:'the default'  
       
   231     "
       
   232 !
       
   233 
       
   234 request:aString initialAnswer:initial onCancel:cancelAction
       
   235     "launch a Dialog, which allows user to enter something.
       
   236      Return the entered string (may be empty string) 
       
   237      or cancelValue (if cancel was pressed)"
       
   238 
       
   239     |val|
       
   240 
       
   241     val :=self 
       
   242 	request:aString 
       
   243 	displayAt:nil 
       
   244 	centered:true 
       
   245 	action:[:result | ^ result] 
       
   246 	initialAnswer:initial.
       
   247 
       
   248     ^ cancelAction value
       
   249 
       
   250     "
       
   251      Dialog 
       
   252 	 request:'enter a string:' 
       
   253 	 initialAnswer:'the default'  
       
   254 	 onCancel:['foooo']   
   125     "
   255     "
   126 !
   256 !
   127 
   257 
   128 request:aString displayAt:aPoint initialAnswer:initial
   258 request:aString displayAt:aPoint initialAnswer:initial
   129     "launch a Dialog, which allows user to enter something.
   259     "launch a Dialog, which allows user to enter something.
   366 preferedExtent 
   496 preferedExtent 
   367     "return my prefered extent"
   497     "return my prefered extent"
   368 
   498 
   369     |w h p|
   499     |w h p|
   370 
   500 
   371     okButton isNil ifTrue:[
   501     addedComponents notNil ifTrue:[
   372 	^ super preferedExtent
   502 	w := addedComponents 
       
   503 		inject:0 
       
   504 		into:[:max :element | 
       
   505 			max max:(element preferedExtent x + element leftInset)].
       
   506     ] ifFalse:[
       
   507 	w := super preferedExtent x.
   373     ].
   508     ].
   374     p := buttonPanel preferedExtent.
       
   375     w := p x.
       
   376     h := ViewSpacing
   509     h := ViewSpacing
   377 	 + "okButton preferedExtent" p y
   510 	 + yPosition
   378 	 + ViewSpacing.
   511 	 + ViewSpacing.
   379 
   512 
       
   513     okButton notNil ifTrue:[
       
   514 	p := buttonPanel preferedExtent.
       
   515 	w := w max:p x.
       
   516 	h := h
       
   517 	     + p y
       
   518 	     + ViewSpacing.
       
   519     ].
       
   520 
       
   521 "/    okButton isNil ifTrue:[
       
   522 "/        ^ super preferedExtent
       
   523 "/    ].
       
   524 "/    p := buttonPanel preferedExtent.
       
   525 "/    w := p x.
       
   526 "/    h := ViewSpacing
       
   527 "/         + p y
       
   528 "/         + ViewSpacing.
       
   529 "/
   380     ^ w @ h
   530     ^ w @ h
   381 ! !
   531 ! !
   382 
   532 
   383 !DialogBox methodsFor:'initialization'!
   533 !DialogBox methodsFor:'initialization'!
   384 
   534 
   392     mm := ViewSpacing.
   542     mm := ViewSpacing.
   393 
   543 
   394     acceptReturnAsOK := true.
   544     acceptReturnAsOK := true.
   395 
   545 
   396     buttonPanel := HorizontalPanelView in:self.
   546     buttonPanel := HorizontalPanelView in:self.
   397     buttonPanel origin:(0.0 @ 1.0) corner:(1.0 @ 1.0).
   547     buttonPanel 
   398     buttonPanel bottomInset:mm; 
   548 	origin:(0.0 @ 1.0) corner:(1.0 @ 1.0);
   399 		topInset:(font height + mm * 2) negated.
   549 	bottomInset:mm; 
   400     buttonPanel borderWidth:0.
   550 	topInset:(font height + mm * 2) negated;
   401     buttonPanel layout:#spread.
   551 	borderWidth:0;
       
   552 	layout:#spread.
       
   553 
       
   554     yPosition := 0.
       
   555     leftIndent := 0.
   402 
   556 
   403     "
   557     "
   404      |b|
   558      |b|
   405      b := DialogBox new.
   559      b := DialogBox new.
   406      b addAbortButton; addOkButton; showAtPointer
   560      b addAbortButton; 
       
   561        addOkButton; 
       
   562        showAtPointer
   407     "
   563     "
   408     "
   564     "
   409      |b|
   565      |b|
   410      b := DialogBox new.
   566      b := DialogBox new.
   411      b addOkButton; showAtPointer
   567      b addOkButton; 
   412     "
   568        showAtPointer
   413 !
   569     "
   414 
   570     "
   415 addOkButton
   571      |b|
   416     "create an okButton - to be sent from redefined initialize
   572      b := DialogBox new.
   417      methods in subclasses."
   573      b addTextLabel:'hello world';
   418 
   574        addOkButton; 
   419     okButton := Button okButtonIn:buttonPanel.
   575        showAtPointer
   420     okButton model:self; change:#okPressed.
   576     "
   421     okButton isReturnButton:acceptReturnAsOK.
   577     "
   422     buttonPanel subViews size > 1 ifTrue:[
   578      |b|
   423 	buttonPanel layout:#fitSpace.
   579      b := DialogBox new.
   424     ].
   580      b addTextLabel:'hello world';
   425 !
   581        addVerticalSpace:50; 
   426 
   582        addOkButton; 
   427 addAbortButton
   583        showAtPointer
   428     "create an abortButton - to be sent from redefined initialize
   584     "
   429      methods in subclasses."
       
   430 
       
   431     abortButton := Button abortButtonIn:buttonPanel.
       
   432     abortButton model:self; change:#abortPressed.
       
   433     buttonPanel subViews size > 1 ifTrue:[
       
   434 	buttonPanel layout:#fitSpace.
       
   435     ].
       
   436 !
   585 !
   437 
   586 
   438 reAdjustGeometry
   587 reAdjustGeometry
   439     "sent late in snapin processing - gives me a chance
   588     "sent late in snapin processing - gives me a chance
   440      to resize for changed font dimensions."
   589      to resize for changed font dimensions."
   445     self resize
   594     self resize
   446 !
   595 !
   447 
   596 
   448 focusSequence
   597 focusSequence
   449     ^ buttonPanel subViews
   598     ^ buttonPanel subViews
       
   599 ! !
       
   600 
       
   601 !DialogBox methodsFor:'construction-adding'!
       
   602 
       
   603 addButton:aButton after:someOtherButtonOrNil
       
   604     "add a button in the buttonPanel.
       
   605      If the argument someOtherButtonOrNil is nil, the button is
       
   606      added at the end."
       
   607 
       
   608     buttonPanel addSubView:aButton after:someOtherButtonOrNil.
       
   609     buttonPanel subViews size > 1 ifTrue:[
       
   610 	buttonPanel layout:#fitSpace.
       
   611     ].
       
   612 !
       
   613 
       
   614 addButton:aButton
       
   615     "add a button in the buttonPanel"
       
   616 
       
   617     self addButton:aButton after:nil
       
   618 !
       
   619 
       
   620 addOkButton:action
       
   621     "create an okButton - to be sent from redefined initialize
       
   622      methods in subclasses."
       
   623 
       
   624     okButton := Button okButton.
       
   625     action notNil ifTrue:[okButton action:action].
       
   626     okButton model:self; change:#okPressed.
       
   627     okButton isReturnButton:acceptReturnAsOK.
       
   628     self addButton:okButton.
       
   629 !
       
   630 
       
   631 addOkButton
       
   632     "create an okButton - to be sent from redefined initialize
       
   633      methods in subclasses."
       
   634 
       
   635     self addOkButton:nil
       
   636 !
       
   637 
       
   638 addAbortButton
       
   639     "create an abortButton - to be sent from redefined initialize
       
   640      methods in subclasses."
       
   641 
       
   642     abortButton := Button abortButton.
       
   643     abortButton model:self; change:#abortPressed.
       
   644     self addButton:abortButton.
       
   645 !
       
   646 
       
   647 addVerticalSpace:nPixel
       
   648     yPosition := yPosition + nPixel.
       
   649 !
       
   650 
       
   651 addComponent:aComponent
       
   652     "add a component with its prefered height and full width"
       
   653 
       
   654     addedComponents isNil ifTrue:[
       
   655 	addedComponents := OrderedCollection new.
       
   656     ].
       
   657     addedComponents add:aComponent.
       
   658     self addSubView:aComponent.
       
   659     aComponent height:(aComponent preferedExtent y).
       
   660     aComponent origin:0.0@yPosition; width:1.0; leftInset:leftIndent.
       
   661     yPosition := yPosition + aComponent height.
       
   662 !
       
   663 
       
   664 addTextLabel:aString
       
   665     "create a text label - the name has been choosen for ST-80 compatibility;
       
   666      however, ST/X labels allow image labels too."
       
   667 
       
   668     |l|
       
   669 
       
   670     l := Label new label:aString.
       
   671     l 
       
   672 	origin:(0.0 @ 0.0);
       
   673 	topInset:yPosition; 
       
   674 	bottomInset:yPosition negated;
       
   675 	leftInset:leftIndent;
       
   676 	rightInset:leftIndent negated.
       
   677 
       
   678     self addComponent:l.
       
   679 
       
   680     "
       
   681      |b|
       
   682 
       
   683      b := DialogBox new.
       
   684      b addTextLabel:'hello'.
       
   685      b showAtPointer
       
   686     "
       
   687     "
       
   688      |b|
       
   689 
       
   690      b := DialogBox new.
       
   691      b leftIndent:100.
       
   692      b addTextLabel:'hello'.
       
   693      b leftIndent:0.
       
   694      b addTextLabel:'world'.
       
   695      b showAtPointer
       
   696     "
       
   697     "
       
   698      |b|
       
   699 
       
   700      b := DialogBox new.
       
   701      b addTextLabel:'hello'.
       
   702      b addTextLabel:'world'.
       
   703      b addOkButton.
       
   704      b showAtPointer
       
   705     "
       
   706     "
       
   707      |b|
       
   708 
       
   709      b := DialogBox new.
       
   710      b addTextLabel:'hello world\\How about this ?' withCRs.
       
   711      b addOkButton.
       
   712      b showAtPointer
       
   713     "
       
   714     "
       
   715      |b|
       
   716 
       
   717      b := DialogBox new.
       
   718      b addTextLabel:'hello world\\How about this ?' withCRs.
       
   719      b addTextLabel:'not bad'.
       
   720      b addAbortButton.
       
   721      b addOkButton.
       
   722      b showAtPointer
       
   723     "
       
   724 !
       
   725 
       
   726 addCheckBox:label on:aModel
       
   727     |b|
       
   728 
       
   729     b := CheckBox on:aModel.
       
   730     b label:label.
       
   731     self addComponent:b.
       
   732     ^ b
       
   733 !
       
   734 
       
   735 yPosition 
       
   736     ^ yPosition 
       
   737 !
       
   738 
       
   739 yPosition:aNumber 
       
   740     yPosition := aNumber.
       
   741 !
       
   742 
       
   743 leftIndent:aNumber 
       
   744     leftIndent := aNumber.
   450 ! !
   745 ! !
   451 
   746 
   452 !DialogBox methodsFor:'private'!
   747 !DialogBox methodsFor:'private'!
   453 
   748 
   454 hideAndEvaluate:aBlock
   749 hideAndEvaluate:aBlock