NO

Author Topic: TBBUTTON struct  (Read 8949 times)

Alessio

  • Guest
TBBUTTON struct
« on: May 21, 2009, 09:51:55 AM »
Hi,

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

Code: [Select]
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

  • Guest
Re: TBBUTTON struct
« Reply #1 on: May 21, 2009, 10:23:56 AM »
Like this.

Code: [Select]
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

  • Guest
Re: TBBUTTON struct
« Reply #2 on: May 21, 2009, 11:22:26 AM »
It not works.

Using

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

gives

Quote
error #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

Quote
error #2069: Initializer must be constant.




JohnF

  • Guest
Re: TBBUTTON struct
« Reply #3 on: May 21, 2009, 11:51:24 AM »
Apparently the static keyword was stopping it.

Code: [Select]
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.

Code: [Select]
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
« Last Edit: May 21, 2009, 12:15:15 PM by JohnF »

Alessio

  • Guest
Re: TBBUTTON struct
« Reply #4 on: May 21, 2009, 12:22:20 PM »
Thank you,
but in this case I prefer to use old good TB_ADDSTRING message.

JohnF

  • Guest
Re: TBBUTTON struct
« Reply #5 on: May 21, 2009, 12:37:20 PM »
I missed the fact that you only had 7 elements, there should be 8.

Code: [Select]
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

  • Guest
Re: TBBUTTON struct
« Reply #6 on: May 21, 2009, 12:49:21 PM »
This is TBBUTTON struct from MSDN online, it has 7 members (same as commctrl.h of pellesc)

Quote
typedef 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

  • Guest
Re: TBBUTTON struct
« Reply #7 on: May 21, 2009, 01:33:49 PM »
BYTE     bReserved[2]  is two params.

John

Alessio

  • Guest
Re: TBBUTTON struct
« Reply #8 on: May 21, 2009, 02:08:57 PM »
 ;) Right but...

Code: [Select]
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!



« Last Edit: May 21, 2009, 02:15:42 PM by Alessio »

JohnF

  • Guest
Re: TBBUTTON struct
« Reply #9 on: May 21, 2009, 03:47:36 PM »
In what way doesn't it work?

This code seems to say it's ok.

Code: [Select]
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

  • Guest
Re: TBBUTTON struct
« Reply #10 on: May 21, 2009, 04:19:00 PM »
It works!
Sorry I've forgot to remove "static" keyword.

Thanks again.

JohnF

  • Guest
Re: TBBUTTON struct
« Reply #11 on: May 21, 2009, 09:26:03 PM »
Pelle,

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

Code: [Select]
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

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: TBBUTTON struct
« Reply #12 on: May 22, 2009, 01:02:00 PM »
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