World: r3wp
[View] discuss view related issues
older newer | first last |
Robert 15-Oct-2006 [5789] | I have a question WRT changing the text of a shown label. I use a way where the layout of my GUI is done at startup-time once. Now I need to change the text of some labels at run-time and I don't want to do the following: - name each label that needs to be updated. - write a function that changes the TEXT word and shows the widget I would like to use a word in the initial context that keeps a reference to the text (or function returning the text) which gets reevaluated if a SHOW is used. Do I have to write an own style that supports this or is there a simpler way? |
Henrik 15-Oct-2006 [5790x2] | an easy way is to keep all labels in a panel and then traverse the pane and set the label texts on a label. |
if your panel consists of multiple different types of faces, you can filter on 'style | |
Anton 15-Oct-2006 [5792x4] | How about this: |
data: ["hello" "there" "robert"] spec: copy [] foreach string data [ append spec compose [label 100 (string)] ] view window: layout append spec [ btn "change original strings" [ insert clear data/1 "happy" insert clear data/2 "to be" insert clear data/3 "changed" show window ] ] | |
You have to make sure the initial labels are large enough for your longest strings. Of course we can stylize label so that it resizes automatically, but maybe you don't need that. | |
Henrik's way can be helped using SET-FACE, I think. (I haven't used this feature yet.) Have a look in svv/vid-styles/panel/access 'set-face* | |
Robert 15-Oct-2006 [5796x3] | Anton, ah the insert clear method... I missed this one. I think that's the way to go. I'll check this out. Thanks. |
Hmm... one more. Let's I have: label-text: "The current value is ?" and I know want to change the ? dynamically. After first replacement the ? is gone. So I need some way to keep the source version. Or first insert the source version, than replace and show. | |
Oh man... to many typos (I cut my right finger): - Let's say I have - know = now | |
Gregg 15-Oct-2006 [5799] | ; Something like this maybe? REBOL [] texts: [ EN [ lbl-1 "Hello" lbl-2 "Goodbye" ] DE [ lbl-1 "Hallo" lbl-2 "Aufwiedersehen" ] ] lang: 'EN res: func [id] [texts/:lang/:id] good-ID?: func [id] [find texts/:lang id] has-res-ID?: func [face] [ all [ find first face 'res-ID word? face/res-ID good-ID? face/res-ID ] ] re-show: func [face] [ foreach f face/pane [ if has-res-ID? f [ set-face f res f/res-ID ] ] show face ] lay: layout [ text 100 with [res-ID: 'lbl-1] text 100 with [res-ID: 'lbl-2] btn "English" [lang: 'EN re-show lay] btn "Deutsch" [lang: 'DE re-show lay] ] re-show lay view lay |
Louis 15-Oct-2006 [5800] | tr "Rp/US$:" exchange-rate: field 270x24 feel [ exchange-rate/text: exchange-rat show exchange-rate ] The above code loads exchange-rat into exchange-rate/text when the script loads. But after saving data it appears to be empty. How can exchange-rate/text be forced to persistently show its contents? |
Louis 16-Oct-2006 [5801x3] | I changed it to this: tr "Rp/US$:" exchange-rate: field 270x24 feel [exchange-rate/text: exchange-rat: (read %exchange-rate.txt) show exchange-rate (write %exchange-rate.txt exchange-rat) ] But it still doesn't work. |
What I want is: 1. The exchange rate to be saved in a file, so that it will always be loaded upon start the script. 2. The exchange rate to remain in the field through each loop of view. 3. And, of course, I want to be able to change the exchange rate if needed. Seems like it should be a common easy thing to do, but it eludes me. | |
Perhaps it would be clearer to say that I don't want to have to manually enter the exchange rate for each record. | |
MikeL 16-Oct-2006 [5804x2] | Louis, I haven't been following this problem but if you want to just load a rate from a text file you could do something like this which handles loading before showing the face and allows saving an updated rate REBOL [] these-styles: stylize [ lab: label 60x20 right bold middle font-size 11 fld: field 60x20 font-size 11 middle edge [size: 1x1] ] rate-file: %/c/rate-file.txt if not exists? rate-file [write rate-file 1.000] rate: load rate-file view layout [ across styles these-styles lab "Rate" rate-field: fld bold (form rate) return btn "Save" #"^s" [save rate-file form rate-field/text] ] |
If you want to keep the update function out of this script, it is simple to just use the REBOL editor to update the rate via btn "Editor" #"^e" [editor rate-file] But make the rate field read only. | |
Louis 16-Oct-2006 [5806] | MikeL, thanks! I'm testing to see if this does what I need. I'll report back. |
Anton 16-Oct-2006 [5807x2] | Robert, try this: |
data: [ "name: ?" "name: Robert" "age: ?" "age: unknown" "occupation: ?" "occupation: programmer" ] spec: copy [] foreach [format string] data [ append spec compose [label 100 (string)] ] view window: layout append spec [ btn "change original strings" [ insert clear data/2 replace copy data/1 "?" "Anton" insert clear data/4 replace copy data/3 "?" "31" insert clear data/6 replace copy data/5 "?" "rebol surgeon" show window ] ] | |
Louis 16-Oct-2006 [5809x8] | rebol [] either exists? %exchange-rate.txt [ exchange-rat: load %exchange-rate.txt ][ save %exchange-rate.txt 1 exchange-rat: load %exchange-rate.txt ] exchange-rate-style: stylize [ exchange-rate-label: label 60x20 right bold middle exchange-rate-field: field 270x24 middle ] view layout [ styles exchange-rate-style exchange-rate-label "Rp/US$:" exchange-rate: exchange-rate-field bold (form exchange-rat) button "Save" [ save %exchange-rate.txt exchange-rate/text ] ] The above code works in the above test script (mimics MikeL's script); but in my accounting script it yields and error message: ** Script Error: val needs a value ** Where: forskip ** Near: forall args [ val: first args switch/default type?/word val [ pair! [append pairs val] integer... >> |
and = an | |
I just cut and pasted the code from my accounting script into the test script above. | |
In my accounting script, however, some of the code is separated by other code. Could that be the problem? | |
By the way, thanks MikeL. This is what I am wanting. | |
Ok, I've traced the problem to 'clear-fields. How can I protect this particular field so that it is not cleared by 'clear-fields with all the rest of the fields upon save? | |
rebol [] either all [exists? %exchange-rate.txt "" <> read %exchange-rate.txt][ exchange-rat: read %exchange-rate.txt ][ write %exchange-rate.txt 1 exchange-rat: read %exchange-rate.txt ] exchange-rate-style: stylize [ exchange-rate-label: label 60x20 exchange-rate-field: field 50x24 note-label: label 55x20 Note-field: field 55x55 ] test: layout [ styles exchange-rate-style note-label "Notes:" notes: note-field bold exchange-rate-label "Rp/US$:" exchange-rate: exchange-rate-field bold (form exchange-rat) button "Save" [ exchange-rat: exchange-rate/text write %exchange-rate.txt exchange-rate/text ;clear-fields test show test ;<=========<< UNCOMMENT THIS LINE TO SEE THE PROBLEM. ;Needed: for the Notes field to clear on each save, but the exchange-rate field to persist. ] ] view/options test [resize] | |
Please View experts come to my rescue! | |
Sunanda 16-Oct-2006 [5817] | You could replace the clear-fields line with this line: Notes/text: copy "" show notes Thus just clearing the notes field rather than all fields. |
Graham 16-Oct-2006 [5818x4] | this includes a lot of redundant code |
either all [exists? %exchange-rate.txt "" <> read %exchange-rate.txt][ exchange-rat: read %exchange-rate.txt ][ write %exchange-rate.txt 1 exchange-rat: read %exchange-rate.txt ] | |
if not all [exists? %exchange-rate.txt "" <> exchange-rat: read %exchange-rate.txt][ write %exchange-rate.txt "1" exchange-rat: "1" ] | |
if not all [exists? %exchange-rate.txt "" <> exchange-rat: read %exchange-rate.txt][ write %exchange-rate.txt exchange-rat: "1" ] is a little shorter again | |
Louis 16-Oct-2006 [5822x7] | Sunanda and Graham, many thanks. Slowly but surely my script is better and better. The REBOL solution is almost always much easier than I thought. Thanks again! |
Ok, I have 61 out of 63 fields to clear, so it would be a lot easier to deal with those two that don't have to be cleared than to deal with the 61 that must be. So I propose a modification to the clear-fields function: Here is the source: clear-fields: func [ "Clear all text fields faces of a layout." panel [object!] ][ if not all [in panel 'type panel/type = 'face] [exit] unfocus foreach face panel/pane [ if all [series? face/text flag-face? face field] [ clear face/text face/line-list: none ] ] ] | |
I would just like to put the panes I don't want to be cleared into a block; all the rest would be cleared. | |
I think I can do this, but it would probably be better for you experts to come up with something really simple and elegant that can be submitted to RT for Rebol 3. | |
It is just logical that this function has such a refinement. | |
Oh, the source above is what needs to be modified, not my modification. The only reason I posted it is to show how simple it is, and therefore how easy it should be to add a refinement to it. | |
It would be logical to call the refinement /except: /except "Block of fields not to clear." | |
Louis 17-Oct-2006 [5829] | I''m working on this myself. My first question: How can evaluation be based on the name of a field, and thus not on the contents of the field? If the name of the field is this then do that. |
Sunanda 17-Oct-2006 [5830] | Does this do what you want? clear-fields: func [ "Clear all text fields faces of a layout." panel [object!] /except exceptions [block!] ][ if not all [in panel 'type panel/type = 'face] [exit] unfocus if not block? exceptions [exceptions: copy []] exceptions: reduce exceptions foreach face panel/pane [ if all [series? face/text flag-face? face field not find exceptions face] [ clear face/text face/line-list: none show face ] ] ] ;; test/ example l-o: layout [ a: field "A" b: field "B" c: field "C" button "clear c" [ clear-fields/except l-o [a b] ] button "clear a" [ clear-fields/except l-o [b c] ] ] unview/all view l-o |
Louis 17-Oct-2006 [5831x3] | Sunanda, thanks! That works perfectly. Your refinement made what would have been a big job into a minor task that took only a few seconds. In my opinion, this refinement definately needs to be in Rebol 3. I will make newbies much happier. |
What is the best way to submit this to RT? | |
By the way, Allen Kamp sent me the same refinement code as Sunanda. Thanks to you also, Allen! | |
Anton 18-Oct-2006 [5834] | Louis, just catching up with your post back on Monday, I wanted to say that it's a bit odd to use the FEEL to do init code. It's not really intended for that. A way to do custom init code is like this: layout [ field with [ append init [ ; custom init code here text: exchange-rat ] ] ] and in the above case, just setting the text facet, it simplifies to: layout [ field (exchange-rat) ] |
Louis 18-Oct-2006 [5835] | Thanks. I like things to be simple. By the way, I just looked at your styles gallery. Very nice, and gives me examples to do things I need to do. |
Henrik 18-Oct-2006 [5836x3] | anton, the style gallery still doesn't work. I get the same include errors as yesterday. |
http://www.lexicon.net/antonr/rebol/patch/ctx-text-next-field-patch.r <--- link is dead | |
which is one of the first includes | |
older newer | first last |