SDL text box highlighting text
<p>I am making a program in C++ that uses SDL to display text boxes. I've made the basic functions of my text boxes like typing text, limitations, copy/paste, <em>etc.</em> but I am stuck at make it able to highlight text.</p>

<p><strong>Here's how I draw textboxes:</strong></p>

<pre><code>void draw_text_box(SDL_Surface *screan, char *message, struct textbox *text_box, int r, int g, int b)
{
    int temp_width;
    int temp_frame;
    SDL_Surface *text;
    SDL_Color textcolor = {0,0,0,0};
    SDL_Rect temp_location, clr_box;

    temp_width = text_box-&gt;location.w; //preserve the correct width

    temp_location = text_box-&gt;location;
    temp_location.y += 4;
    temp_location.h = 12;

    clr_box = text_box-&gt;location;

    //colors!
    textcolor.r = r;
    textcolor.g = g;
    textcolor.b = b;

    //text = TTF_RenderText_Solid(ttf_font, text_box-&gt;prev_message, textcolor);
    //SDL_BlitSurface( text, NULL, screan, &amp;text_box-&gt;location);//&lt;-changes "location" width to width of text

    //clear
    SDL_FillRect(screan, &amp;clr_box, SDL_MapRGB(screen-&gt;format, 0, 0, 0));

    //strcpy(text_box-&gt;prev_message, message);

    //set the text
    text = TTF_RenderText_Solid(ttf_font, message, textcolor);

    if(!text) return;

    //set the offset
    temp_location.x = text_box-&gt;last_shift;
    temp_location.y = 0;
    temp_location.h = text-&gt;h;

    if(text_box-&gt;cursor_loc - text_box-&gt;last_shift &gt; temp_location.w)
    temp_location.x = text_box-&gt;cursor_loc - temp_location.w;

    if(text_box-&gt;cursor_loc - 3 &lt; text_box-&gt;last_shift)
    temp_location.x = text_box-&gt;cursor_loc - 3;

    if(temp_location.x &lt; 0) temp_location.x = 0;
    text_box-&gt;last_shift = temp_location.x;

    //draw
    SDL_BlitSurface( text, &amp;temp_location, screan, &amp;text_box-&gt;location);//&lt;-changes "location" width to width of text
    text_box-&gt;location.w = temp_width;
}
</code></pre>

<p><strong>Setting the text:</strong></p>

<pre><code>void set_text_box_text(SDL_Surface *screan, struct textbox *text_box, char *message)
{
    if(text_box-&gt;message != message)
        strcpy(text_box-&gt;message, message);

    text_box-&gt;char_amt = strlen(message);
    text_box-&gt;cursor = text_box-&gt;char_amt;
    text_box-&gt;last_shift = 0;

    if (screan) //if not NULL
    {
        if (text_box-&gt;selected)
            select_text_box(screan,text_box);
        else
            unselect_text_box(screan,text_box);
    }
    else
    {
        ;//text_box-&gt;prev_message[0] = '\0';
    }
}
</code></pre>

<p><strong>Select and deselect the textbox:</strong></p>

<pre><code>void select_text_box(SDL_Surface *screan, struct textbox *text_box)
{
    char temp_message[405] = "";
    char temp_left[405];
    int i;

    text_box-&gt;selected = 1;

    if (text_box-&gt;pass_char == '\0')
    {
        if (text_box-&gt;cursor != 0)
        {
            left(temp_left, text_box-&gt;message, text_box-&gt;cursor);
            strcat(temp_message, temp_left);
        }

        temp_message[text_box-&gt;cursor] = '|';
        temp_message[text_box-&gt;cursor + 1] = '\0';

        right(temp_left, text_box-&gt;message, text_box-&gt;cursor);
        strcat(temp_message, temp_left);
    }
    else
    {
        for(i=0;i&lt;text_box-&gt;cursor;i++)
            temp_message[i] = text_box-&gt;pass_char;

        temp_message[i] = '|';

        for(i++;i&lt;text_box-&gt;char_amt + 1;i++)
            temp_message[i] = text_box-&gt;pass_char;

        temp_message[i] = '\0';
    }

    draw_text_box(screan, temp_message, text_box, 250, 250, 250);

}

void unselect_text_box(SDL_Surface *screan, struct textbox *text_box)
{
    char temp_message[405];
    int i;

    text_box-&gt;selected = 0;

    if (text_box-&gt;pass_char == '\0')
        draw_text_box(screan, text_box-&gt;message, text_box, 250, 250, 250);
    else
    {
        for(i=0;i&lt;text_box-&gt;char_amt;i++)
            temp_message[i] = text_box-&gt;pass_char;

        temp_message[i] = '\0';
        draw_text_box(screan, temp_message, text_box, 250, 250, 250);
    }
}
</code></pre>