View.st
changeset 140 0db355079dc4
parent 135 cf8e46015072
child 143 b237b9013f51
equal deleted inserted replaced
139:2e075def9aee 140:0db355079dc4
    21 
    21 
    22 View comment:'
    22 View comment:'
    23 COPYRIGHT (c) 1995 by Claus Gittinger
    23 COPYRIGHT (c) 1995 by Claus Gittinger
    24 	      All Rights Reserved
    24 	      All Rights Reserved
    25 
    25 
    26 $Header: /cvs/stx/stx/libview/View.st,v 1.41 1995-05-03 00:26:19 claus Exp $
    26 $Header: /cvs/stx/stx/libview/View.st,v 1.42 1995-05-09 00:23:01 claus Exp $
    27 '!
    27 '!
    28 
    28 
    29 !View class methodsFor:'documentation'!
    29 !View class methodsFor:'documentation'!
    30 
    30 
    31 copyright
    31 copyright
    42 "
    42 "
    43 !
    43 !
    44 
    44 
    45 version
    45 version
    46 "
    46 "
    47 $Header: /cvs/stx/stx/libview/View.st,v 1.41 1995-05-03 00:26:19 claus Exp $
    47 $Header: /cvs/stx/stx/libview/View.st,v 1.42 1995-05-09 00:23:01 claus Exp $
    48 "
    48 "
    49 !
    49 !
    50 
    50 
    51 documentation
    51 documentation
    52 "
    52 "
    58     from this class. 
    58     from this class. 
    59 
    59 
    60     Instance variables:
    60     Instance variables:
    61 
    61 
    62 	model           <nil | any>         the model (if any)
    62 	model           <nil | any>         the model (if any)
       
    63 
    63 	aspectMsg       <nil | Symbol>      the aspect; typically
    64 	aspectMsg       <nil | Symbol>      the aspect; typically
    64 					    dependentViews react on changes
    65 					    dependentViews react on changes
    65 					    of this aspect and update their contents.
    66 					    of this aspect and update their contents.
       
    67 
    66 	changeMsg       <nil | Symbol>      the changeMessage; typically
    68 	changeMsg       <nil | Symbol>      the changeMessage; typically
    67 					    dependentViews send this message to
    69 					    dependentViews send this message to
    68 					    the model to tell it about changes.
    70 					    the model to tell it about changes.
       
    71 
    69 	menuMsg         <nil | Symbol>      the menuMessage; typically
    72 	menuMsg         <nil | Symbol>      the menuMessage; typically
    70 					    dependentViews send this message to
    73 					    dependentViews send this message to
    71 					    the model to ask for a popup menu.
    74 					    the model to ask for a popup menu.
       
    75 					    Some classes allow setting an explicit
       
    76 					    menuHolder and menuPerformer.
    72 "
    77 "
    73 ! !
    78 ! !
    74 
    79 
    75 !View class methodsFor:'instance creation'!
    80 !View class methodsFor:'instance creation'!
       
    81 
       
    82 on:aModel aspect:aspectMsg
       
    83     "st-80 style view creation: create a new view, set its model
       
    84      and selectors for aspect"
       
    85 
       
    86     ^ self new 
       
    87 	on:aModel
       
    88 	aspect:aspectMsg
       
    89 !
       
    90 
       
    91 on:aModel aspect:aspectMsg change:changeMsg
       
    92     "st-80 style view creation: create a new view, set its model
       
    93      and selectors for aspect and change"
       
    94 
       
    95     ^ self new 
       
    96 	on:aModel
       
    97 	aspect:aspectMsg
       
    98 	change:changeMsg
       
    99 !
       
   100 
       
   101 on:aModel aspect:aspectMsg menu:menuMsg
       
   102     "st-80 style view creation: create a new view, set its model
       
   103      and selectors for aspect and menu"
       
   104 
       
   105     ^ self new 
       
   106 	on:aModel
       
   107 	aspect:aspectMsg
       
   108 	menu:menuMsg
       
   109 !
    76 
   110 
    77 on:aModel aspect:aspectMsg change:changeMsg menu:menuMsg
   111 on:aModel aspect:aspectMsg change:changeMsg menu:menuMsg
    78     "st-80 style view creation: create a new view, set its model
   112     "st-80 style view creation: create a new view, set its model
    79      and access selectors for aspect, change and menu"
   113      and selectors for aspect, change and menu"
    80 
   114 
    81     ^ self new 
   115     ^ self new 
    82 	on:aModel
   116 	on:aModel
    83 	aspect:aspectMsg
   117 	aspect:aspectMsg
    84 	change:changeMsg
   118 	change:changeMsg
    91     ^ self new model:aModel
   125     ^ self new model:aModel
    92 ! !
   126 ! !
    93 
   127 
    94 !View class methodsFor:'defaults'!
   128 !View class methodsFor:'defaults'!
    95 
   129 
    96 defaultAspectMsg   
   130 defaultAspectMessage   
    97     "subclasses which by default do NOT want to be informed about changed
   131     "subclasses which by default do NOT want to be informed about changed
    98      models should redefine this to return nil"
   132      models should redefine this to return nil"
    99 
   133 
   100     ^ #value
   134     ^ #value
   101 !
   135 !
   102 
   136 
   103 defaultChangeMsg   
   137 defaultChangeMessage   
   104     "subclasses which by default do NOT want to inform the model
   138     "subclasses which by default do NOT want to inform the model
   105      should redefine this to return nil"
   139      should redefine this to return nil"
   106 
   140 
   107     ^ #value:
   141     ^ #value:
   108 ! !
   142 ! !
   122 
   156 
   123     "/
   157     "/
   124     "/ MVC way of doing it:
   158     "/ MVC way of doing it:
   125     "/ if the model is a block, evaluate it, optionally
   159     "/ if the model is a block, evaluate it, optionally
   126     "/ passing the arg and the receiver as arguments.
   160     "/ passing the arg and the receiver as arguments.
       
   161     "/
   127     "/ otherwise (the common case) send it a changeMsg message
   162     "/ otherwise (the common case) send it a changeMsg message
   128     "/ also with arg and the receiver (depending on the number of arguments
   163     "/ also with arg and the receiver (depending on the number of arguments
   129     "/ as defined by the selector.
   164     "/ as defined by the selector).
   130     "/
   165     "/
   131     (model notNil and:[aSelector notNil]) ifTrue:[
   166     (model notNil and:[aSelector notNil]) ifTrue:[
   132 	n := aSelector numArgs.
   167 	n := aSelector numArgs.
   133 	model isBlock ifTrue:[
   168 	model isBlock ifTrue:[
   134 	    n := model numArgs.
   169 	    n := model numArgs.
   169     model notNil ifTrue:[
   204     model notNil ifTrue:[
   170 	model removeDependent:self
   205 	model removeDependent:self
   171     ].
   206     ].
   172     model := aModel.
   207     model := aModel.
   173 
   208 
   174     "/
       
   175     "/ set default change- and aspectMsgs
       
   176     "/ (for ST-80 compatibility)
       
   177     "/
       
   178     changeMsg isNil ifTrue:[
       
   179 	changeMsg := self class defaultChangeMsg
       
   180     ].
       
   181     aspectMsg isNil ifTrue:[
       
   182 	aspectMsg := self class defaultAspectMsg
       
   183     ].
       
   184 
       
   185     model notNil ifTrue:[
   209     model notNil ifTrue:[
   186 	aModel addDependent:self
   210 	aModel addDependent:self
   187     ].
   211     ].
   188     controller notNil ifTrue:[
   212     controller notNil ifTrue:[
   189 	controller model:aModel
   213 	controller model:aModel
   214     aspectMsg := aspectSymbol.
   238     aspectMsg := aspectSymbol.
   215     menuMsg := menuSymbol.
   239     menuMsg := menuSymbol.
   216     self model:aModel.
   240     self model:aModel.
   217 !
   241 !
   218 
   242 
       
   243 on:aModel aspect:aspectSymbol change:changeSymbol
       
   244     "ST-80 compatibility: set model, aspect and change
       
   245      messages - needs a view which uses these"
       
   246 
       
   247     aspectMsg := aspectSymbol.
       
   248     changeMsg := changeSymbol.
       
   249     self model:aModel.
       
   250 !
       
   251 
   219 on:aModel aspect:aspectSymbol change:changeSymbol menu:menuSymbol
   252 on:aModel aspect:aspectSymbol change:changeSymbol menu:menuSymbol
   220     "ST-80 compatibility: set model, aspect, change and menu
   253     "ST-80 compatibility: set model, aspect, change and menu
   221      messages - needs a view which uses these"
   254      messages - needs a view which uses these"
   222 
   255 
   223     aspectMsg := aspectSymbol.
   256     aspectMsg := aspectSymbol.
   224     changeMsg := changeSymbol.
   257     changeMsg := changeSymbol.
   225     menuMsg := menuSymbol.
   258     menuMsg := menuSymbol.
   226     self model:aModel.
   259     self model:aModel.
       
   260 !
       
   261 
       
   262 menuMessage:aSymbol
       
   263     "ST-80 style menus: If a views menuSymbol is nonNil, it
       
   264      will send it to its model when the middleButton is pressed.
       
   265      That method should return nil or the menu to be shown.
       
   266      This is useful for very dynamic menus, where it does not
       
   267      make sense to define an initial menu.
       
   268      This is the same as #menu: which was added for ST-80 compatibility."
       
   269 
       
   270     menuMsg := aSymbol
       
   271 !
       
   272 
       
   273 menuMessage
       
   274     "Return the symbol sent to the model to aquire the menu"
       
   275 
       
   276     ^ menuMsg
   227 !
   277 !
   228 
   278 
   229 menu:menuSymbol
   279 menu:menuSymbol
   230     "ST-80 style menus: If a views menuSymbol is nonNil, it
   280     "ST-80 style menus: If a views menuSymbol is nonNil, it
   231      will send it to its model when the middleButton is pressed.
   281      will send it to its model when the middleButton is pressed.
   232      That method should return nil or the menu to be shown.
   282      That method should return nil or the menu to be shown.
   233      This is useful for very dynamic menus, where it does not
   283      This is useful for very dynamic menus, where it does not
   234      make sense to define an initial menu."
   284      make sense to define an initial menu.
       
   285      Alias for #menuMessage:, for ST-80 compatibility."
   235 
   286 
   236     menuMsg := menuSymbol
   287     menuMsg := menuSymbol
       
   288 !
       
   289 
       
   290 changeMessage:aSymbol
       
   291     "ST-80 style change notification: If a views changeSymbol is nonNil, 
       
   292      it will send it to its model when something changes.
       
   293      This is the same as change: which was added for ST-80 compatibility."
       
   294 
       
   295     changeMsg := aSymbol
       
   296 !
       
   297 
       
   298 changeMessage
       
   299     "Return the symbol sent to the model if nonNil when something changes."
       
   300 
       
   301     ^ changeMsg
   237 !
   302 !
   238 
   303 
   239 change:changeSymbol
   304 change:changeSymbol
   240     "ST-80 style change notification: If a views changeSymbol is nonNil, 
   305     "ST-80 style change notification: If a views changeSymbol is nonNil, 
   241      it will send it to its model when something changes."
   306      it will send it to its model when something changes.
       
   307      Alias for changeMessage: for ST-80 compatibility."
   242 
   308 
   243     changeMsg := changeSymbol
   309     changeMsg := changeSymbol
   244 !
   310 !
   245 
   311 
   246 aspect:aspectSymbol
   312 aspect:aspectSymbol
   248      it will respond to changes of this aspect from the model."
   314      it will respond to changes of this aspect from the model."
   249 
   315 
   250     aspectMsg := aspectSymbol
   316     aspectMsg := aspectSymbol
   251 !
   317 !
   252 
   318 
   253 changeMessage
   319 aspect
   254     "Return the symbol sent to the model if nonNil when something changes."
   320     "Return the aspect used with changes from/to the model"
   255 
   321 
   256     ^ changeMsg
   322     ^ aspectMsg
   257 !
       
   258 
       
   259 menuMessage
       
   260     "Return the symbol sent to the model to aquire the menu"
       
   261 
       
   262     ^ menuMsg
       
   263 !
   323 !
   264 
   324 
   265 aspectMessage
   325 aspectMessage
   266     "Return the aspect used with changes from/to the model"
   326     "Return the aspect used with changes from/to the model"
   267 
   327 
   268     ^ aspectMsg
   328     ^ aspectMsg
       
   329 !
       
   330 
       
   331 addModelInterfaceTo:aDictionary
       
   332     "this adds entries for all messages sent to my model
       
   333      to aDictionary"
       
   334 
       
   335     aDictionary at:#aspectMessage put:aspectMsg.
       
   336     aDictionary at:#changeMessage put:changeMsg.
       
   337     aDictionary at:#menuMessage put:menuMsg.
       
   338 
       
   339     "
       
   340      Button new modelInterface 
       
   341     "
       
   342 !
       
   343 
       
   344 modelInterface
       
   345     "this returns a dictionary of messages sent to my model.
       
   346      It can be used for builders and wrappers to get information
       
   347      about the messages sent to my model.
       
   348      The returned dictionary contains one entry for each get-Msg,
       
   349      and the recevier will implement corresponding messages (with a colon)
       
   350      to set the message symbol."
       
   351 
       
   352     |d|
       
   353 
       
   354     d := IdentityDictionary new.
       
   355     self addModelInterfaceTo:d.
       
   356     ^ d
       
   357 
       
   358     "
       
   359      Button new modelInterface
       
   360      Label new modelInterface 
       
   361     "
       
   362 
       
   363     "
       
   364      does the view support setting the menuMessage ?
       
   365 
       
   366      SelectionInListView new modelInterface includesKey:#menuMessage 
       
   367     "
       
   368 
       
   369     "
       
   370      turn off all interaction to the model:
       
   371 
       
   372      |m v if|
       
   373 
       
   374      m := SelectionInList new.
       
   375      m list:#('one' 'two' 'tree' 'four') asValue.
       
   376      m selection:1 asValue.
       
   377      v := SelectionInListView on:m.
       
   378      v open.
       
   379      v inspect.
       
   380 
       
   381      if := v modelInterface.
       
   382      if inspect.
       
   383 
       
   384      if keysAndValuesDo:[:what :msg |
       
   385 	 v perform:(what , ':') asSymbol with:nil.
       
   386      ].
       
   387     "
   269 ! !
   388 ! !
   270 
   389 
   271 !View methodsFor:'accessing-menus'!
   390 !View methodsFor:'accessing-menus'!
   272 
   391 
   273 menuHolder
   392 menuHolder
   288 
   407 
   289 !View methodsFor:'initialization'!
   408 !View methodsFor:'initialization'!
   290 
   409 
   291 initialize
   410 initialize
   292     super initialize.
   411     super initialize.
       
   412 
       
   413     aspectMsg := self class defaultAspectMessage.
       
   414     changeMsg := self class defaultChangeMessage.
       
   415 
   293     model notNil ifTrue:[
   416     model notNil ifTrue:[
   294 	controller notNil ifTrue:[
   417 	controller notNil ifTrue:[
   295 	    controller model:model
   418 	    controller model:model
   296 	]
   419 	]
   297     ].
   420     ].