Code acts differently in "run"

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Code acts differently in "run"

Post by jassing »

The code below, if while in the pb IDE I hite "f5" (run) the combo box has the 1st element disabled.
When I say "create exe"; the resulting exe does not show the element disabled. Any ideas why?

Code: Select all

Enumeration
  #winTest
EndEnumeration
Enumeration
  #StatusBar_winTest
EndEnumeration
Enumeration
  #cntKeyEntry
  #cboKeyLength1
EndEnumeration
Macro SetStatus(a)
  StatusBarText(#StatusBar_winTest, 0, "-=[ "+a+" ]=-", #PB_StatusBar_Center)
EndMacro

;{ begin code by klaver: http://www.purebasic.fr/english/viewtopic.php?p=352245#p352245
Structure MyCB
  oldProc.i
  prevItm.i
  lastKey.i
EndStructure

Define wc.WNDCLASSEX\cbSize = SizeOf(WNDCLASSEX)
GetClassInfoEx_(GetModuleHandle_(0), @"ComboBox", @wc)
Global *gCombCallbackBlock.MyCB, *gComboCallBackWorkingMem=AllocateMemory(128), gComboCallBack = wc\lpfnWndProc
Procedure ComboProc(hWnd, uMsg, wParam, lParam)
  Protected oldProc
  *gCombCallbackBlock = GetProp_(hWnd, "MyCB")
  If *gCombCallbackBlock
    oldProc = *gCombCallbackBlock\oldProc
  Else
    oldProc = gComboCallBack;#WM_DELETEITEM
  EndIf
  
  Select uMsg
  Case #WM_KEYDOWN
    *gCombCallbackBlock\lastKey = wParam
  Case #WM_NCDESTROY
    RemoveProp_(hWnd, "MyCB")
    FreeMemory(*gCombCallbackBlock)
  EndSelect
  ProcedureReturn CallWindowProc_(oldProc, hWnd, uMsg, wParam, lParam)
EndProcedure
Procedure WinCallback(hWnd, uMsg, wParam, lParam)
  Select uMsg
  Case #WM_DRAWITEM
    Protected *lpdis.DRAWITEMSTRUCT = lParam
    SetBkMode_(*lpdis\hDC, #TRANSPARENT)
    If *lpdis\CtlType = #ODT_COMBOBOX
      If *lpdis\itemState & #ODS_SELECTED
        If CallWindowProc_(gComboCallBack, *lpdis\hwndItem, #CB_GETITEMDATA, *lpdis\itemID, 0) And *lpdis\itemID > -1
          *lpdis\CtlType = GetSysColorBrush_(#COLOR_BTNFACE)
        Else
          *lpdis\CtlType = GetSysColorBrush_(#COLOR_HIGHLIGHT)
        EndIf
      Else
        *lpdis\CtlType = GetSysColorBrush_(#COLOR_WINDOW)
      EndIf
      FillRect_(*lpdis\hDC, *lpdis\rcItem, *lpdis\CtlType)
      *lpdis\rcItem\left+4
      
      If CallWindowProc_(gComboCallBack, *lpdis\hwndItem, #CB_GETLBTEXT, *lpdis\itemID, *gComboCallBackWorkingMem) > -1
        If CallWindowProc_(gComboCallBack, *lpdis\hwndItem, #CB_GETITEMDATA, *lpdis\itemID, 0);SendMessage_()
          SetTextColor_(*lpdis\hDC, #Gray);GetSysColor_(#COLOR_GRAYTEXT)
        EndIf
        DrawText_(*lpdis\hDC, *gComboCallBackWorkingMem, -1, *lpdis\rcItem, #DT_NOCLIP|#DT_VCENTER|#DT_SINGLELINE)
        SetTextColor_(*lpdis\hDC, GetSysColor_(#COLOR_WINDOWTEXT))
      EndIf
    EndIf
  Case #WM_COMMAND
    *gCombCallbackBlock = GetProp_(lParam, "MyCB"); it should be checked if pointer isn't NULL (too lazy for that)
    If *gCombCallbackBlock
      Select (wParam>>16) & $FFFF;HIWORD
      Case #CBN_SELCHANGE
        Protected Var = CallWindowProc_(gComboCallBack, lParam, #CB_GETCURSEL, 0, 0);SendMessage_()
        If Var <> *gCombCallbackBlock\prevItm
          If CallWindowProc_(gComboCallBack, lParam, #CB_GETITEMDATA, Var, 0);SendMessage_()
            If *gCombCallbackBlock\lastKey = 38 Or *gCombCallbackBlock\lastKey = 37;Up
              Protected i.i
              For i=Var-1 To 0 Step -1
                If CallWindowProc_(gComboCallBack, lParam, #CB_GETITEMDATA, i, 0) = #False
                  *gCombCallbackBlock\prevItm = i : Break
                EndIf
              Next
            ElseIf *gCombCallbackBlock\lastKey = 40 Or *gCombCallbackBlock\lastKey = 39;Down
              For i=Var+1 To CallWindowProc_(gComboCallBack, lParam, #CB_GETCOUNT, 0, 0)-1
                If CallWindowProc_(gComboCallBack, lParam, #CB_GETITEMDATA, i, 0) = #False
                  *gCombCallbackBlock\prevItm = i : Break
                EndIf
              Next
            ElseIf *gCombCallbackBlock\lastKey = 33;PgUp
              For i=0 To CallWindowProc_(gComboCallBack, lParam, #CB_GETCOUNT, 0, 0)-1
                If CallWindowProc_(gComboCallBack, lParam, #CB_GETITEMDATA, i, 0) = #False
                  *gCombCallbackBlock\prevItm = i : Break
                EndIf
              Next
            ElseIf *gCombCallbackBlock\lastKey = 34;PgDn
              For i=CallWindowProc_(gComboCallBack, lParam, #CB_GETCOUNT, 0, 0)-1 To 0 Step -1
                If CallWindowProc_(gComboCallBack, lParam, #CB_GETITEMDATA, i, 0) = #False
                  *gCombCallbackBlock\prevItm = i : Break
                EndIf
              Next
            EndIf
            CallWindowProc_(gComboCallBack, lParam, #CB_SETCURSEL, *gCombCallbackBlock\prevItm, 0);SendMessage_()
          Else
            *gCombCallbackBlock\prevItm = Var
          EndIf
        EndIf
      Case #CBN_DROPDOWN
        *gCombCallbackBlock\lastKey = 0
      EndSelect
    EndIf
  EndSelect
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
Procedure MakeMyCB(ID)
  *gCombCallbackBlock = AllocateMemory(SizeOf(MyCB))
  *gCombCallbackBlock\prevItm = SendMessage_(GadgetID(ID), #CB_GETCURSEL, 0, 0)
  *gCombCallbackBlock\oldProc = SetWindowLongPtr_(GadgetID(ID), #GWL_WNDPROC, @ComboProc())
  SetProp_(GadgetID(ID), "MyCB", *gCombCallbackBlock)
EndProcedure
;} end klaver code

Macro DisableComboGadgetItem( nComboGadget, nItem, bDisable )
  Debug SendMessage_(GadgetID(nComboGadget), #CB_SETITEMDATA, nItem, bDisable);
EndMacro

Procedure ResetLength(c)
  Protected cValue.s = GetGadgetText(c)
  ClearGadgetItems(c)
  AddGadgetItem(c,-1,"A (Should be disabled)")
  AddGadgetItem(c,-1,"B")
  AddGadgetItem(c,-1,"C")
  DisableComboGadgetItem(c,0,#True)
  
EndProcedure
Procedure OpenWindow_winTest()
  If OpenWindow(#winTest, 431, 40, 577, 389, "combo box issue", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered|#PB_Window_Invisible)
    If CreateStatusBar(#StatusBar_winTest, WindowID(#winTest))
      AddStatusBarField(577)
      StatusBarText(#StatusBar_winTest, 0, "[ Initialising ]", #PB_StatusBar_Center)
    EndIf
    ContainerGadget(#cntKeyEntry, -1, 97, 570, 235, #PB_Container_BorderLess)
      ComboBoxGadget(#cboKeyLength1, 285, 15, 106, 24, #CBS_OWNERDRAWFIXED|#CBS_HASSTRINGS)
    CloseGadgetList()
    ResetLength(#cboKeyLength1)
    SetWindowCallback(@WinCallback(), 0)
      
    HideWindow(#winTest,#False)
  EndIf
EndProcedure

OpenWindow_winTest()
Repeat
 Until WaitWindowEvent(10) = #PB_Event_CloseWindow
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Code acts differently in "run"

Post by IdeasVacuum »

Yeah, its "Debug" in the Macro - remove the debug keyword and the exe is fine.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Code acts differently in "run"

Post by jassing »

IdeasVacuum wrote:Yeah, its "Debug" in the Macro - remove the debug keyword and the exe is fine.
Seriously? D'oh!!! I had been trying to figure this out -- and clearly failed to notice that.
Thank you.
Post Reply