News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

TBBUTTON struct

Started by Alessio, May 21, 2009, 09:51:55 AM

Previous topic - Next topic

Alessio

Hi,

there's a way to declare following TBBUTTON struct to prevent error #2069: Initializer must be constant. ?


static CONST CHAR *szToolbar[] =
{ "New", "Select", "Previous", "Next", "Save", "Delete", "Close", "Print" };

static CONST TBBUTTON tbButton[TOOLBAR_BUTTON] =
{
{ NEW, IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT)szToolbar[NEW] },
{ SELECT, IDM_SELECT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT)szToolbar[SELECT] },
{ PREVIOUS, IDM_PREVIOUS, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT)szToolbar[PREVIOUS] },
{ NEXT, IDM_NEXT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT)szToolbar[NEXT] },
{ SAVES, IDM_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT)szToolbar[SAVES] },
{ DELETES, IDM_DELETE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT)szToolbar[DELETES] },
{ CLOSE, IDM_CLOSE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT)szToolbar[CLOSE] },
{ PRINT, IDM_PRINT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT)szToolbar[PRINT] },

};


Thanks.

JohnF

Like this.


static CONST TBBUTTON tbButton[TOOLBAR_BUTTON] =
{
{ NEW, IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, "New" },
{ SELECT, IDM_SELECT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, "Previous" },


John

Alessio

It not works.

Using

Quote{ NEW,      IDM_NEW,      TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, "New" },

gives

Quoteerror #2082: Invalid initialization type: found 'char *', expected 'unsigned long int'

...casting it...

Quote{ NEW,      IDM_NEW,      TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (ULONG)"New" },

returns to

Quoteerror #2069: Initializer must be constant.




JohnF

#3
Apparently the static keyword was stopping it.


CONST TBBUTTON tbButton[TOOLBAR_BUTTON] =
{
{ NEW,   IDM_NEW,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT_PTR)"New" },
{ SELECT, IDM_SELECT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, (INT_PTR)"Previous" },
};


EDIT: You may have to do it like this.


void TBButton(TBBUTTON *tbb, int iBitmap, int idCommand, int fsState, int fsStyle, DWORD_PTR dwData, char * str)
{
// Button
tbb->iBitmap = iBitmap;
tbb->idCommand = idCommand;
tbb->fsState = fsState;
tbb->fsStyle = fsStyle;
tbb->iString = (INT_PTR)str;
}

#define NUM_TOOLBAR_BUTTONS 4
TBBUTTON tbButton[NUM_TOOLBAR_BUTTONS];
// Clear
memset(&tbButton[0], 0, sizeof(TBBUTTON) * NUM_TOOLBAR_BUTTONS);

// Set buttons
TBButton(&tbButton[0], 0, 0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, "Title");



John



John

Alessio

Thank you,
but in this case I prefer to use old good TB_ADDSTRING message.

JohnF

I missed the fact that you only had 7 elements, there should be 8.


CONST TBBUTTON tbButton[TOOLBAR_BUTTON] =
{
{ NEW,    IDM_NEW,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0L, (INT_PTR)"New" },
{ SELECT, IDM_SELECT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0L, (INT_PTR)"Previous" },
};


Now it should work.

John

Alessio

This is TBBUTTON struct from MSDN online, it has 7 members (same as commctrl.h of pellesc)

Quotetypedef struct _TBBUTTON {
    int         iBitmap;
    int         idCommand;
    BYTE     fsState;
    BYTE     fsStyle;
#ifdef _WIN64
    BYTE     bReserved[6]     // padding for alignment
#elif defined(_WIN32)
    BYTE     bReserved[2]     // padding for alignment
#endif
    DWORD_PTR   dwData;
    INT_PTR          iString;
} TBBUTTON, NEAR *PTBBUTTON *LPTBBUTTON;

JohnF

BYTE     bReserved[2]  is two params.

John

Alessio

#8
 ;) Right but...

CONST TBBUTTON tbButton[TOOLBAR_BUTTON] =
{
{ NEW,    IDM_NEW,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0L, (INT_PTR)"New" },
{ SELECT, IDM_SELECT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0L, (INT_PTR)"Previous" },
};


Doesn't work!




JohnF

In what way doesn't it work?

This code seems to say it's ok.


int main(void)
{

CONST TBBUTTON tbButton[TOOLBAR_BUTTON] =
{
//{ NEW,    IDM_NEW,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0L, (INT_PTR)"New" },
{ 1, 2, 3, 4, 5, 6, 7L, (INT_PTR)"New" },
{ SELECT, IDM_SELECT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0L, (INT_PTR)"Previous" },
};

printf("%d\n", tbButton[0].iBitmap);
printf("%d\n", tbButton[0].idCommand);
printf("%d\n", tbButton[0].fsState);
printf("%d\n", tbButton[0].fsStyle);
printf("%d\n", tbButton[0].bReserved[0]);
printf("%d\n", tbButton[0].bReserved[1]);
printf("%d\n", tbButton[0].dwData);
printf("%s\n", (char*)tbButton[0].iString);

    return 0;
}


John

Alessio

It works!
Sorry I've forgot to remove "static" keyword.

Thanks again.

JohnF

Pelle,

Is there a reason that the following can't be declared static?


CONST TBBUTTON tbButton[TOOLBAR_BUTTON] =
{
{ NEW,    IDM_NEW,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0L, (INT_PTR)"New" },
{ SELECT, IDM_SELECT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0L, (INT_PTR)"Previous" },
};


John

Pelle

Using a cast on a static initializer address has always ben a bit of a problem; I added support for cast to *unsigned* integer type of same size as an address in 6.0, but not for a signed integer type. I'm not sure what the correct behavior should be here. I will look at it...
/Pelle