No announcement yet.

gfx_Button is not accepting dynamic strings

  • Filter
  • Time
  • Show
Clear All
new posts

  • gfx_Button is not accepting dynamic strings

    #platform "uLCD-32PT_GFX2"

    #inherit "4DGL_16bitColours.fnc"

    //#inherit "FONT4.FNT"

    var myarray[10];

    func main()
    var x1,y1,x2,y2;
    var xx1,yy1,xx2,yy2;

    to(myarray); putstr("HELLO\nTHERE");

    //gfx_Selection(1, GRAY, YELLOW); // pre-emptive selection of hilite line

    gfx_Button( 1, 50, 50, GRAY, BLACK, FONT3, 3, 3, myarray );

    // save the extents of the button

    //gfx_MoveTo(40,40); // this would otherwise modify the extents

    x1 := peekW(GFX_LEFT);
    y1 := peekW(GFX_TOP);
    x2 := peekW(GFX_RIGHT);
    y2 := peekW(GFX_BOTTOM);

    // these will receive the extent of the second part of the string
    xx1 := peekW(GFX_X1);
    yy1 := peekW(GFX_Y1);
    xx2 := peekW(GFX_X2);
    yy2 := peekW(GFX_Y2);





    var list1, sel, linecount, loop;

    list1 := "Line1\nLine2\nLine3\nLine4\nLine5"; // list 1 points to string constant

    sel := 1; // preset selection to line 1
    linecount := 5; // max lines

    print("Wait for exit....");


    gfx_Selection(sel, RED, YELLOW); // pre-emptive selection of hilite line

    gfx_Button( 0 , 70, 100, GRAY, BLACK, 2, 1, 1, list1); // use multiline button as list

    if(sel++ == linecount) sel := 1; // demo, scan through the selections

    pause (250);


    gfx_Selection(0, RED, YELLOW); // de-select (colour = dont care)


    repeat forever

    If you try to move the button it formats strangely as well

    also note: This is a SAMPLE. Not my code.

  • #2

    If you pass myarray as str_Ptr(myarray) does that fix it?


    • #3

      By random flailing (Which I'm to understand is basically how everyone works with strings in 4DGL) I have discovered that I can use dynamic strings in buttons by using the ever mysterious str_Ptr function.

      However I have an array of dynamic strings. and I can only get it to work with the first one

      str_Ptr(array[0]); works
      str_Ptr(array[1]); does not

      This function is voodoo.


      • #4

        Variables are word aligned, str_Ptr raises a byte aligned pointer so that the variable can be accessed as a string. So accessing the second element of an array as a str_Ptr, would, at best, give you a pointer to the third byte onwards. Is that what you are seeing, or is something else happening.

        There are many examples of the use of str_Ptr.

        Also, the R28 release notes documents some new string functions that you may find easier to understand/use.


        • #5

          That isn't what should be happening though right? This is an array of strings. The first element contains a pointer to a string in dynamic memory, the second element is the same.
          If it worked for the first pointer, why not the second? hmmm...


          • #6

            A Little more information:
            In a function called state, I declared a private array called attTitles[15]
            I loop through all the elements and run this:
            attTitle[index] := mem_AllocZ(20);
            and I can get at it with state.attTitle[n]
            The only way I could think to have a little encapsulation.
            This works! I can get at the strings and print them and manipulate them and everything is wonderful.

            I just can't get the gfx_Button to use them as strings. I am stumped.


            • #7

              Sorry, I'm not following this well.

              Do you mean to be taking the address contained in array elemnt n, eg str_Ptr(&myarray[2])


              • #8

                Welcome to the club sir!

                my understanding of this is that I have to pass in the location in memory where the string is stored to gfx_Button

                The array should be an array of pointers right? is it that 4dgl infers that something is a pointer datatype? Like in c? Or are all vars simply a 16 bit variable?

                Anyway. this array I have contains pointers to blocks of memory with strings in them

                I figured, if I passed in that number to gfx_Button, then it would read the string in the memory location it points to. I'm not sure if the strings are null terminated, it's really hard to tell WHAT'S going on in the display. However I'm not even getting strings then garbage. The reference operator, it returns the variables address yes? And the pointer operator goes TO the address stored in the variable yes?

                This is why I expected *state.attTitle[1] to point to the string, because attTitle[1] holds the address of that block of memory.

                then I find str_Ptr somehow does some kind of byte alignment allowing you to read stings? So I thought maybe str_Ptr(state.attTitle[1]) would take the pointer stored in attTitle[1] and convert it or... well something

                Yah I'm lost. Help.

                So to be clear. print([STR]state.atTitle[1]); does exactly what you would expect
                gfx_Button(yada,yada,yada,state.atTitle[1]); does not. What's the difference in these situations?


                • #9

                  The club is I need to understand what you are talking about, you need to supply a short sample of code that demonstrates the error.

                  If I 'simply' take the two lines you have given in your last example and try to turn them into code I get this.

                  #platform "uLCD-32PT_GFX2"

                  func main()
                  state.atTitle[0] := "This is a test" ;
                  state.atTitle[1] := "This is a another test" ;
                  print([STR]state.atTitle[0], "\n"); // does exactly what you would expect
                  print([STR]state.atTitle[1], "\n"); // does exactly what you would expect
                  gfx_Button(UP,0,100, 0xf800, 0xffff, FONT1, 1,1, state.atTitle[0]); // does not. What's the difference in these situations?
                  gfx_Button(UP,0,140, 0xf800, 0xffff, FONT1, 1,1, state.atTitle[1]); // does not. What's the difference in these situations?
                  repeat forever

                  func state()
                  var private
                  var private atTitle[15];

                  ..and, needless to say, it works as expected.

                  But I don't think this is how you are trying to make it work.