home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18920 < prev    next >
Encoding:
Text File  |  1993-01-11  |  4.8 KB  |  123 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!think.com!ames!purdue!mentor.cc.purdue.edu!biomac1s.bio.purdue.edu!user
  3. From: pf@bilbo.bio.purdue.edu (Paul Furbacher)
  4. Subject: Re: modifying TInputLine to handle integers/ reals etc..need help
  5. Message-ID: <pf-100193222604@biomac1s.bio.purdue.edu>
  6. Followup-To: comp.lang.c++
  7. Sender: news@mentor.cc.purdue.edu (USENET News)
  8. Organization: Purdue University
  9. References: <C0ntu3.22I@athena.cs.uga.edu>
  10. Date: Mon, 11 Jan 1993 04:00:08 GMT
  11. Lines: 110
  12.  
  13. In article <C0ntu3.22I@athena.cs.uga.edu>, 
  14.   ravi@athena.cs.uga.edu (Ravi Vemuri) wrote:
  15. > I am having prbs creating a TInputLine object to handle integers. I overrode
  16. > the setdata and getdata functions by incorporating function calls for
  17. > string to integer conversion and viceversa. However, there are a few prbs
  18. > I could'nt overcome(and hence, works only partly!):
  19. > 1. my object does not respond to <Enter> key like the parent object does.
  20.  
  21. This means that you overrode the HandleEvent() method too.  If your
  22. descendent has lost the ability to trap the kbEnter event, then
  23. look to see that you are first trapping the special keycodes,
  24. e.g., non-numeric for integer, and then pass on the event to 
  25. the ancestor's HandleEvent() method (member function in c++).
  26.  
  27. You can also add validation upon loss of focus caused by 
  28. kbTab and kbShiftTab by adding a trap in the HandleEvent for 
  29. those two events --
  30.    if (Event.what = evKeydown) and 
  31.       ((Event.KeyCode = kbTab) or (Event.KeyCode = kbShiftTab)) then
  32.    begin
  33.      if not Descendent.Valid(command) then 
  34.      begin
  35.        Select;
  36.        ClearEvent(Event);
  37.      end;
  38.    end;  
  39.  
  40. (Sorry, I'm up to my ears these days in Pascal, but it's very 
  41. much like pseudo-code.)
  42.  
  43. Then you could trap for the "." and clear it, if you wished.
  44. Finally, you should call the ancestor's HandleEvent() and 
  45. let it handle all the other more mundane stuff.
  46.  
  47. > 2. I didnot override datasize(), but i think i should. however, the manual
  48. > does state that the simple str to int and int to str conversions should
  49. > suffice.
  50.  
  51. What function does the DataSize function perform?  It is called
  52. by a TDialog instance when the dialog box is performing its 
  53. SetData/GetData operation on all the subviews.  As it tracks
  54. through the subview list in z-order, it asks "what's your datasize?",
  55. the subview says "string10" or "longint" or "char", etc., and
  56. the the owning group (the dialog box) either takes or gives that
  57. many bytes to the subview.  If you've ever gotten the number of
  58. bytes wrong in the structure you passed into the Set/GetData()
  59. call, you know you got something weird.  The reason is that
  60. as the owning group parcels out the bytes, it isn't smart enough
  61. that you got things in the wrong order or gave it too many or
  62. too few bytes to parcel out. So if you're going to alter what
  63. you want the owning dialog box to pass to or take from your 
  64. TIntegerLine instance, you'll have to alter the value 
  65. which TIntegerLine.DataSize returns.
  66.  
  67. DataSize for the ancestor TInputLine reflects the length of 
  68. the string passed in, at least in the Pascal version of TV.
  69. With an integer (longint) inputline you would change this
  70. to longint *if* you create descendent SetData() and GetData()
  71. methods which convert the integer into and back from the 
  72. string which is handled by the HandleEvent() (which is essentially
  73. a string handling and building routine).  So, in this scheme,
  74. GetData would take the string in the *Data field, convert it
  75. to a longint (because DataSize = sizeof(longint)), and SetData
  76. would take the integer (or longint) given to it and convert
  77. it into a string to assign to *Data. 
  78.  
  79. The Pascal code looks like this.
  80.  
  81. function TIntegerLine.DataSize: word;
  82. begin
  83.   DataSize := sizeof(longint);
  84. end;
  85.  
  86. procedure TIntegerLine.GetData(var Rec);
  87. var
  88.   Code : integer;
  89. begin
  90.   Val(Data^, longint(Rec), Code); 
  91. end;
  92.  
  93. procedure TIntegerLine.SetData(var Rec);
  94. begin
  95.   Str(longint(Rec),Data^);
  96.   SelectAll(True); // highlights the whole input line 
  97. end; 
  98.  
  99. Make sense? You'll have to make some language and version 
  100. translations no doubt, but if you look it over, it should become
  101. clear.
  102.  
  103. Lastly, and not least importantly, you will have to create
  104. a descendent Valid() function which will validate whether the
  105. user has entered a correct number. You don't want "4pq8" to pass
  106. as a valid integer!  And you'll want to perhaps limit the 
  107. range of the value in many cases, so include range-checking in the
  108. Valid() method as well.  If the string doesn't convert
  109. to a valid number or exceeds the range, let the user know 
  110. with a message dialog.  And return the focus to the input 
  111. line with SelectAll(true).
  112.  
  113. Have fun.  Go lightly on this project -- do just enough to 
  114. get by, because the next version of TV will most likely have 
  115. a whole bunch of inputline validation built into it, if 
  116. the version 2 release of TV/Pascal is an indication.  
  117.  
  118. The documentation is much better in version 2, as well. You 
  119. can't wait can you.
  120.  
  121. PF
  122.