Sign in to follow this  
szecs

[C] Same functions with different function pointers inside

Recommended Posts

szecs    2990
I try to explain my problem:
void MenuLeftClick()
{
	for( int i = MenuNumWindows-1; i >= 0; i-- )
	{
		for( int k = 0; k < MAX_MENU_ELEMENTS; k++ )
		{
			if( GUI_Elements[k].LeftClickFunc 
			   && GUI_Elements[k].parent_window == MenuWindowBuffer[i]
			   && ItemSelect(&GUI_Elements[k]) )
			{	
				GUI_Elements[k].LeftClickFunc();
				return;
			}
		}

		if( WindowSelect( &GUI_Windows[MenuWindowBuffer[i]]) )
			return;

		if( GUI_Windows[MenuWindowBuffer[i]].WindowUnClickFunc )
			GUI_Windows[MenuWindowBuffer[i]].WindowUnClickFunc();

		if( GUI_Windows[MenuWindowBuffer[i]].type == MENU_WINDOW_DIALOG )
			return;
		
	}
}
void MenuLeftUp()
{	
	for( int i = MenuNumWindows-1; i >= 0; i-- )
	{
		for( int k = 0; k < MAX_MENU_ELEMENTS; k++ )
		{
			if( GUI_Elements[k].LeftUpFunc 
			   && GUI_Elements[k].parent_window == MenuWindowBuffer[i]
			   && ItemSelect(&GUI_Elements[k]) )
			{	
				GUI_Elements[k].LeftUpFunc();
				return;
			}
		}

		if( WindowSelect( &GUI_Windows[MenuWindowBuffer[i]]) )
			return;

		if( GUI_Windows[MenuWindowBuffer[i]].WindowUnUpFunc )
			GUI_Windows[MenuWindowBuffer[i]].WindowUnUpFunc();

		if( GUI_Windows[MenuWindowBuffer[i]].type == MENU_WINDOW_DIALOG )
			return;
	}
}
As you can see the only difference is GUI_Elements[k].LeftClickFunc(); vs GUI_Elements[k].LeftUpFunc(); and GUI_Windows[MenuWindowBuffer[ i ]].WindowUnClickFunc() vs GUI_Windows[MenuWindowBuffer[ i ]].WindowUnUpFunc() And I will need the same for right mouse button click too. Is there a way around this, or I can only copy the code? The code (and the GUI) is just a day old, but you can critique it if you like, especially the names. Thanks in advance!

Share this post


Link to post
Share on other sites
fastcall22    10845
If it were C++, I'd refactor the code with a template function type! Alas, since C shouldn't sport such spiffy stuff, a macro might surely suffice! (Or a function pointer?)

Or maybe something like this:

int f()
{
for( int i = MenuNumWindows-1; i >= 0; i-- )
{
for( int k = 0; k < MAX_MENU_ELEMENTS; k++ )
if( GUI_Elements[k].LeftClickFunc
&& GUI_Elements[k].parent_window == MenuWindowBuffer[i]
&& ItemSelect(&GUI_Elements[k]) )
return k;

/* ... */
}
return -1;
}

void MenuLeftClick() {
int k = f();
if ( k >= 0 )
GUI_Elements[k].LeftClickFunc();
}

void MenuLeftUp() {
int k = f();
if ( k >= 0 )
GUI_Elements[k].LeftUpFunc();
}


Share this post


Link to post
Share on other sites
Zipster    2365
How about:
void MenuMouseClick(int code)
{
/* ... */
GUI_Elements[k].ClickFuncs[code]();
/* ... */
}

void MenuMouseUp(int code)
{
/* ... */
GUI_Elements[k].UpFuncs[code]();
/* ... */
}

Where all you do is replace individually named function pointers with an array of function pointers, and pass in a code indicating which mouse button it is?

Share this post


Link to post
Share on other sites
szecs    2990
Thanks for the replies, I think I will go Zipster's way, because I realized that the mouse up and down 'engine' aren't similar, since mouse up has no effect outside the topmost window, while mouse down will make the newly clicked window active (if the topmost is not a dialog).
So I click functions can be in one array, and release functions in the other (if right button release has any effect in Windows at all).

I could have solved it, but my question was a bit theoretical.
So I can't really do it in C (only with some horrible MACRO), only in C++.

Share this post


Link to post
Share on other sites
BeerNutts    4401
Even Easier would be:

void MouseClickHandle(int iMouseButton, BOOL bDownClick)
{
.
.
.
GUI_Elements[k].ClickFuncs[iMouseButton][bDownClick]();
.
.
.
}



No need for multiple functions, just multiple parameters

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this