Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Pt.h>
- #include <assert.h>
- #include <stdbool.h>
- int cb_divider_drag00(PtWidget_t *divider, void *d, PtCallbackInfo_t *cbinfo) {
- d = d;
- static bool running = false; // avoid recursive invocation
- if (running) {
- return Pt_CONTINUE;
- }
- /* divider child is being resized using PtSetResource() */
- if (cbinfo->reason_subtype == Pt_CB_DIVIDER_SETRESOURCES) {
- return Pt_CONTINUE;
- }
- running = true;
- PtDividerCallback_t *dvdr_cb = cbinfo->cbdata;
- /* find the index of the widget with changed dimension */
- PtWidget_t *tmp = dvdr_cb->left;
- int pos = 0;
- /* this callback is called only when there are at least 2 child widgets
- in PtDivider */
- while ((tmp = PtWidgetBrotherBehind(tmp)) != NULL)
- pos++;
- /* recalculate the width of all PtDivider brothers in front and behind */
- PtWidget_t *row = divider;
- while ((row = PtWidgetBrotherBehind(row)) != NULL) {
- /* last internally added widget in PtDivider is always PtGroup
- containing the user-defined widgets */
- tmp = PtWidgetChildBack(row);
- tmp = PtWidgetChildBack(tmp);
- int i = 0;
- for (i = 0; i < pos; i++) {
- tmp = PtWidgetBrotherInFront(tmp);
- }
- /* only to eye-test if `tmp' is the right widget */
- PtSetResource(tmp, Pt_ARG_TEXT_STRING, "XXX", 0);
- /* this does nothing :( */
- PtSetResource(tmp, Pt_ARG_MIN_WIDTH,
- dvdr_cb->sizes[pos].to - dvdr_cb->sizes[pos -1].to, 0);
- PtSetResource(tmp, Pt_ARG_WIDTH,
- dvdr_cb->sizes[pos].to - dvdr_cb->sizes[pos -1].to, 0);
- }
- running = false;
- return Pt_CONTINUE;
- }
- int cb_resize00(PtWidget_t *w, void *d, PtCallbackInfo_t *cbinfo) {
- w = w;
- cbinfo = cbinfo;
- PtWidget_t *group = d;
- static int run = 0;
- /* just guessing; jump over a few initial internal calls */
- if (run < 5) {
- run++;
- return Pt_CONTINUE;
- }
- /* really nasty hack to overcome the fixed size/geometry issue (the
- value is unimportant) */
- PhDim_t dim;
- /* unfortunately, this needs to be called each time the width increases
- because one call to this callback extends the width by 1px (disregarding
- the dim variable) */
- if (PtSetResource(group, Pt_ARG_DIM, &dim, 0))
- PtExit(EXIT_FAILURE);
- return Pt_CONTINUE;
- }
- int main( int argc, char *argv[] ) {
- PtWidget_t *window, *container, *scroll, *group,
- *divider00, *divider01;
- PtArg_t args[20];
- int i = 0;
- PhRect_t rect;
- PhPoint_t point;
- PhDim_t dim;
- rect.ul.x = 0; rect.ul.y = 0; // upper left
- rect.lr.x = 9; rect.lr.y = 9; // lower right
- point.x = 0; point.y = 0;
- dim.w = 350; dim.h = 250;
- /* Create a window */
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_MAX_WIDTH, 350, 0);
- PtSetArg( &args[i++], Pt_ARG_MAX_HEIGHT, 350, 0);
- PtSetArg( &args[i++], Pt_ARG_WINDOW_TITLE, "demo window", 0 );
- if ( NULL == ( window = PtAppInit( NULL, &argc, argv, i, args ) ) ) {
- perror( "PtAppInit()" );
- return 1;
- }
- /* mimic the real setup (app where we have the nested widget hierarchy
- like the following) */
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_ANCHOR_FLAGS,
- Pt_BOTTOM_ANCHORED_BOTTOM | Pt_TOP_ANCHORED_TOP |
- Pt_LEFT_ANCHORED_LEFT | Pt_RIGHT_ANCHORED_RIGHT,
- Pt_IS_ANCHORED);
- container = PtCreateWidget( PtContainer, window, i, args );
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_ANCHOR_FLAGS,
- Pt_BOTTOM_ANCHORED_BOTTOM | Pt_TOP_ANCHORED_TOP |
- Pt_LEFT_ANCHORED_LEFT | Pt_RIGHT_ANCHORED_RIGHT,
- Pt_IS_ANCHORED);
- scroll = PtCreateWidget( PtScrollContainer, container, i, args );
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_GROUP_ORIENTATION, Pt_GROUP_VERTICAL, 0 );
- /* TODO exchange PtGroup for PtDivider to gain vertically adjustable
- heigh of rows
- wtf? in this case PtDivider does not get the size of it's childs (
- PtDivider widgets) and it misplaces them anyway */
- group = PtCreateWidget( PtGroup, scroll, i, args );
- PtCallback_t callbacks_resize[1];
- PtCallback_t callbacks_divider_drag[1];
- callbacks_resize[0].event_f = cb_resize00;
- callbacks_resize[0].data = group;
- callbacks_divider_drag[0].event_f = cb_divider_drag00;
- callbacks_divider_drag[0].data = group;
- i = 0;
- PtSetArg(&args[i++], Pt_CB_RESIZE, callbacks_resize,
- sizeof(callbacks_resize)/sizeof(callbacks_resize[0]));
- PtSetArg(&args[i++], Pt_CB_DIVIDER_DRAG, callbacks_divider_drag,
- sizeof(callbacks_divider_drag)/sizeof(callbacks_divider_drag[0]));
- PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
- PtSetArg( &args[i++], Pt_ARG_DIVIDER_FLAGS, Pt_FALSE, Pt_DIVIDER_RESIZE_BOTH );
- /* invoke CB if the subordinate childs changed geometry themselves */
- PtSetArg( &args[i++], Pt_ARG_FLAGS, Pt_TRUE, Pt_CALLBACKS_ACTIVE );
- /* disable PtDivider reaction on it's child's position&dimension changes */
- //PtSetArg( &args[i++], Pt_ARG_CONTAINER_FLAGS, Pt_FALSE, Pt_CHILD_MOVED_RESIZED);
- divider00 = PtCreateWidget( PtDivider, group, i, args );
- i = 0;
- PtSetArg(&args[i++], Pt_CB_RESIZE, callbacks_resize,
- sizeof(callbacks_resize)/sizeof(callbacks_resize[0]));
- PtSetArg(&args[i++], Pt_CB_DIVIDER_DRAG, callbacks_divider_drag,
- sizeof(callbacks_divider_drag)/sizeof(callbacks_divider_drag[0]));
- PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
- PtSetArg( &args[i++], Pt_ARG_DIVIDER_FLAGS, Pt_FALSE, Pt_DIVIDER_RESIZE_BOTH );
- PtSetArg( &args[i++], Pt_ARG_FLAGS, Pt_TRUE, Pt_CALLBACKS_ACTIVE );
- //PtSetArg( &args[i++], Pt_ARG_CONTAINER_FLAGS, Pt_FALSE, Pt_CHILD_MOVED_RESIZED);
- divider01 = PtCreateWidget( PtDivider, group, i, args );
- PtWidget_t *row00 = divider00,
- *row01 = divider01;
- /* row00 buttons */
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "T1", 0 );
- /* wtf? why does PtText get so narrow that no character is to be seen? */
- PtSetArg( &args[i++], Pt_ARG_WIDTH, 40, 0);
- PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
- PtCreateWidget( PtText, row00, i, args );
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "Button 2 (two)", 0);
- PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
- PtCreateWidget( PtButton, row00, i, args );
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "Butt 3", 0 );
- PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
- PtCreateWidget( PtButton, row00, i, args );
- /* row01 buttons */
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "B4", 0 );
- /* this is ignored by PtDivider */
- PtSetArg( &args[i++], Pt_ARG_MIN_WIDTH, 70, 0);
- /* because PtDivider ignores MIN_WIDTH, this is necessary to set the initial
- width */
- PtSetArg( &args[i++], Pt_ARG_WIDTH, 70, 0);
- PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
- PtCreateWidget( PtButton, row01, i, args );
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "Butt 5", 0 );
- PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
- PtCreateWidget( PtButton, row01, i, args );
- i = 0;
- PtSetArg( &args[i++], Pt_ARG_TEXT_STRING, "Buttonchen 6", 0 );
- PtSetArg( &args[i++], Pt_ARG_RESIZE_FLAGS, Pt_RESIZE_XY_ALWAYS, 0 );
- PtCreateWidget( PtButton, row01, i, args );
- PtRealizeWidget( window );
- PtMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement