(object) gadget width

This forum is for general developer support questions.
Post Reply
User avatar
mritter0
Posts: 214
Joined: Mon Aug 25, 2014 9:41 pm
Location: Bettendorf, IA, USA

(object) gadget width

Post by mritter0 »

I recently converted my project's gadget style from all

Code: Select all

struct Gadget *MyGad;
to

Code: Select all

Object *Objects[MY_GAD]
style based on Trixie's post on OS4Coding (I can't post on there for some reason).
http://www.os4coding.net/blog/trixie/re ... rogramming

I need to get the width of 2 gadgets when quitting so I can save them and re-open with same size. Used to just do

Code: Select all

MyGad->Width
How do I get the width of the gadget from an Object?

They are listbrowsers if it matters.
Workbench Explorer - A better way to browse drawers
User avatar
LyleHaze
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 525
Joined: Sat Jun 18, 2011 4:06 pm
Location: North Florida, near the Big Bend

Re: (object) gadget width

Post by LyleHaze »

Under most situations, you let layout.gadget handle all gadget sizing, and you just specify relative weights, along with hard minimums or maximums as desired.

But if you need the know the gadget width, try something like:

Code: Select all

int32 x;
if(IIntuition->GetAttr(GA_Width, Objects[MY_GAD], &x))
{
    IDOS->Printf("The Gadget Width is %ld\n", x);
}
else
{
    IDOS->Printf("Gadget width is unknown\n")
}
You can find some documentation in
gadget_gc/gadgetclass/--datasheet--
intuition.doc/intuition.library/GetAttr()

Since the gadgetclass is the base class for all gadgets, this should never fail.
Still, it's always a good idea to check for a valid return, which is why the
GetAttr() call is the condition of an if statement.
User avatar
mritter0
Posts: 214
Joined: Mon Aug 25, 2014 9:41 pm
Location: Bettendorf, IA, USA

Re: (object) gadget width

Post by mritter0 »

I have tried that and a few other methods, all return 0.

I am using it with CHILD_WeightedWidth at creation.
Workbench Explorer - A better way to browse drawers
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: (object) gadget width

Post by salass00 »

mritter0 wrote:I have tried that and a few other methods, all return 0.
Gadgetclass actually only implements GA_Width and GA_Height as settable attributes (OM_NEW, OM_SET).

You could try using a cast to access the Width field directly:

Code: Select all

width = ((struct Gadget *)Objects[MY_GAD])->Width;
User avatar
LyleHaze
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 525
Joined: Sat Jun 18, 2011 4:06 pm
Location: North Florida, near the Big Bend

Re: (object) gadget width

Post by LyleHaze »

I must have an old set of includes.
From the gadgetclass datasheet:

Code: Select all

	GA_Width (int16)
	GA_Height (int16)
	    These attribute corresponds to the Width and Height fields
	    in the Intuition Gadget structure.

	    Defaults to arbitrary positive number.

	    Applicability is (OM_NEW, OM_SET, OM_GET, OM_UPDATE)
Just to make sure, I put together a short example.
Just copy the entire code block into a file named gadwidth.c and execute it to compile.
Then run the result, which will be named GadWidth.

You'll get a window with a listbrowser gadget, and you can check the gadget width at any time by
entering Ctrl-D into the shell.. Feel free to re-size the window and check it a few times.

Ctrl-C into the shell will close the window.

Code: Select all

;/* gadwidth.c - example of reading a listgad width
SDK:gcc/bin/gcc -c gadwidth.c
SDK:gcc/bin/gcc -N -o GadWidth gadwidth.o
quit
*/

#include <proto/intuition.h>
#include <proto/utility.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <classes/window.h>
#include <gadgets/layout.h>
#include <gadgets/listbrowser.h>
#include <string.h>

#define BUFSIZE 256  // char bufffer for window title

struct Library *IntuitionBase = NULL, *UtilityBase = NULL;
struct IntuitionIFace *IIntuition = NULL;
struct UtilityIFace *IUtility = NULL;
struct ClassLibrary *WindowBase = NULL,*LayoutBase = NULL;
struct ClassLibrary *ListBrowserBase = NULL;
Class *WindowClass = NULL, *LayoutClass = NULL;
Class *ListBrowserClass = NULL;

// RETURN_OK or RETURN_FAIL
// cleans up for itself if FAIL
int32 openAll(void);
// always RETURN_FAIL
int32 closeAll(STRPTR why);

enum
{
   GID_MAIN = 0,
   GID_LISTBR,
   GID_MAX
};

enum
{
   WID_MAIN = 0,
   WID_MAX
};

int main(int argc, char **argv)
{
   struct Window *win[WID_MAX]; 
   Object *winOb[WID_MAX]; 
   Object *gadOb[GID_MAX]; 

   if(RETURN_OK != openAll())
   {
      return(RETURN_FAIL);
   }

   if((winOb[WID_MAIN] = IIntuition->NewObject(WindowClass, NULL,
      WA_SizeGadget, TRUE,
      WA_DragBar, TRUE,
      WA_CloseGadget, TRUE,
      WA_IDCMP, IDCMP_NEWSIZE,
      WINDOW_Position, WPOS_CENTERSCREEN,
      WINDOW_ParentGroup, gadOb[GID_MAIN] = IIntuition->NewObject(LayoutClass, NULL,
         LAYOUT_AddChild, gadOb[GID_LISTBR] = IIntuition->NewObject(ListBrowserClass, NULL,
            GA_ID, GID_LISTBR,
         TAG_END),
      TAG_END),
   TAG_END)))
   {
      if((win[WID_MAIN] = (struct Window *)IIntuition->IDoMethod(winOb[WID_MAIN], WM_OPEN, NULL)))
      {
         uint32 signal, signalmask, alive = 1;
         uint32 result, x;
         uint16 code;
         char winTitle[BUFSIZE];

         IDOS->Printf("Window is now open\n");
         IDOS->Printf("Change window size with size gadget\n");
         IDOS->Printf("Press Ctrl-D in this console to get gadget width\n");
         IDOS->Printf("Press Ctrl-C in this console to close window\n");

         IIntuition->GetAttr(WINDOW_SigMask, winOb[WID_MAIN], &signalmask);
         while(alive)
         {
            signal = IExec->Wait(signalmask | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);

            if(signal & signalmask)
            {
               while((result = IIntuition->IDoMethod(winOb[WID_MAIN], WM_HANDLEINPUT, &code))!= WMHI_LASTMSG)
               {
                  switch (result & WMHI_CLASSMASK)
                  {
                     case WMHI_CLOSEWINDOW:
                        win[WID_MAIN] = NULL;
                        alive = 0;
                        break;
                     case WMHI_NEWSIZE:
                        if(IIntuition->GetAttr(GA_Width, gadOb[GID_LISTBR], &x))
                        {
                           IUtility->SNPrintf(winTitle, BUFSIZE, "Width %ld", x);
                           IIntuition->SetWindowAttr(win[WID_MAIN], WA_Title, winTitle, strlen(winTitle +1));
                        }
                        break;
                  }
               }
            }

            if(signal & SIGBREAKF_CTRL_C)
            {
               alive = 0;
            }
            if(signal & SIGBREAKF_CTRL_D)
            {
               if(IIntuition->GetAttr(GA_Width, gadOb[GID_LISTBR], &x))
               {
                  IDOS->Printf("Gadget Width is %ld\n", x);
               }
            }
         }
      }
      IIntuition->DisposeObject(winOb[WID_MAIN]);
   }
   else
   {
      IDOS->Printf("Failed to create WinOb\n");
   }
   closeAll(NULL);                  

   return(RETURN_OK);
}

// RETURN_OK or RETURN_FAIL
// cleans up for itself if FAIL
int32 openAll(void)
{
   if(!(UtilityBase = IExec->OpenLibrary("utility.library", 53)))
   {
      return(closeAll("Failed to open utility.library"));
   }
   if(!(IUtility = (struct UtilityIFace *)IExec->GetInterface(
      UtilityBase, "main", 1, NULL)))
   {
      return(closeAll("Failed to get utility interface"));
   }

   if(!(IntuitionBase = IExec->OpenLibrary("intuition.library", 53)))
   {
      return(closeAll("Failed to open intuition.library"));
   }
   if(!(IIntuition = (struct IntuitionIFace *)IExec->GetInterface(IntuitionBase, "main", 1, NULL)))
   {
      return(closeAll("Failed to get intuition interface"));
   }

   if(!(WindowBase = IIntuition->OpenClass("window.class", 53, &WindowClass)))
   {
      return(closeAll("Failed to open window class"));
   }
   if(!(LayoutBase = IIntuition->OpenClass("gadgets/layout.gadget", 53, &LayoutClass)))
   {
      return(closeAll("Failed to open layout gadget"));
   }
   if(!(ListBrowserBase = IIntuition->OpenClass("gadgets/listbrowser.gadget", 53, &ListBrowserClass)))
   {
      return(closeAll("Failed to open listbrowser gadget"));
   }

   return(RETURN_OK);
}

// always RETURN_FAIL
int32 closeAll(STRPTR why)
{
   if(why)
   {
      IDOS->Printf("%s\n", why);
   }

   if(IIntuition)
   {
      IIntuition->CloseClass(ListBrowserBase);
      IIntuition->CloseClass(LayoutBase);
      IIntuition->CloseClass(WindowBase);
   }
   IExec->DropInterface((struct Interface *)IIntuition);
   IExec->CloseLibrary(IntuitionBase);
   IExec->DropInterface((struct Interface *)IUtility);
   IExec->CloseLibrary(UtilityBase);
   return(RETURN_FAIL);
}
Not exactly a well formed program, but it fits into one file for easy posting.

Have Fun!
:)

edit: it looks like the website adds characters to the text.
If you'd like a copy just PM me with your email address, and I'l send the code directly.

edit2: cleaned up a bit, and replaced tabs with spaces.. might work better.?
Last edited by LyleHaze on Tue Sep 23, 2014 4:16 pm, edited 1 time in total.
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: (object) gadget width

Post by salass00 »

LyleHaze wrote:I must have an old set of includes.
The autodoc entry is correct, I looked at the source code of gadgetclass in intuition.library but I missed some UnpackStructureTags() stuff. AFAICT getting of GA_Width attribute should be supported.
User avatar
mritter0
Posts: 214
Joined: Mon Aug 25, 2014 9:41 pm
Location: Bettendorf, IA, USA

Re: (object) gadget width

Post by mritter0 »

Got it working. Not sure what I was doing wrong the first time, but working now.

Thanks, guys!
Workbench Explorer - A better way to browse drawers
User avatar
trixie
Posts: 409
Joined: Thu Jun 30, 2011 2:54 pm
Location: Czech Republic

Re: (object) gadget width

Post by trixie »

@mritter0

The fact that with BOOPSI gadgets, the object pointer = struct Gadget pointer only holds good within the context of the current Intuition. Although casting works at the present moment, it's absolutely not future proof to peek data from the structure. Make a habit of using IIntuition->GetAttrs() to obtain object attributes. If a BOOPSI class does not support getting a particular attribute, there's probably a reason.
The Rear Window blog

AmigaOne X5000 @ 2GHz / 4GB RAM / Radeon RX 560 / ESI Juli@ / AmigaOS 4.1 Final Edition
SAM440ep-flex @ 667MHz / 1GB RAM / Radeon 9250 / AmigaOS 4.1 Final Edition
Post Reply