ui/qt/container.cpp

8 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 26 Mar 2025 21:47:04 +0100 (8 days ago)
changeset 519
8884c7fbe4cb
parent 516
4b31c74666d7
child 520
ea1bba55de44
permissions
-rw-r--r--

add box container (QT)

56
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
66
8d490d97aab8 added button and box container (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
29 #include <stdio.h>
56
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include "container.h"
519
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
31 #include "../common/object.h"
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
32
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
33 #include <cx/mempool.h>
56
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
66
8d490d97aab8 added button and box container (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
35 #include <QSpacerItem>
68
bd9fb6476b80 added tabview (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
36 #include <QStackedWidget>
66
8d490d97aab8 added button and box container (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
37
519
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
38 static void delete_container(UiContainerPrivate *ct) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
39 delete ct;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
40 }
130
212b63dd61be adds some qt stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 110
diff changeset
41
519
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
42 static void add_container(UiObject *obj, UiContainerPrivate *ct) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
43 UiContainerX *container = (UiContainerX*)ui_malloc(obj->ctx, sizeof(UiContainerX));
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
44 container->close = 0;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
45 container->container = ct;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
46 container->prev = NULL;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
47 container->next = NULL;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
48 cxMempoolRegister(obj->ctx->mp, ct, (cx_destructor_func)delete_container);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
49 uic_object_push_container(obj, container);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
50 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
51
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
52 /* -------------------- UiBoxContainer -------------------- */
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
53
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
54 UiBoxContainer::UiBoxContainer(QBoxLayout* box) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
55 this->box = box;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
56 box->setContentsMargins(QMargins(0,0,0,0));
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
57 box->setSpacing(0);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
58
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
59 ui_reset_layout(layout);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
60 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
61
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
62 void UiBoxContainer::add(QWidget* widget, bool fill) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
63 if(layout.fill != UI_LAYOUT_UNDEFINED) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
64 fill = ui_lb2bool(layout.fill);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
65 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
66
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
67 if(hasStretchedWidget && fill) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
68 fill = false;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
69 fprintf(stderr, "UiError: container has 2 filled widgets");
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
70 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
71
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
72 box->addWidget(widget, fill);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
73
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
74 if(!hasStretchedWidget) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
75 QSpacerItem *newspace = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
76 box->removeItem(space);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
77 box->addSpacerItem(newspace);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
78 space = newspace;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
79 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
80
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
81 if(fill) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
82 hasStretchedWidget = true;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
83 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
84 ui_reset_layout(layout);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
85 current = widget;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
86 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
87
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
88 UIWIDGET ui_box(UiObject *obj, UiContainerArgs args, QBoxLayout::Direction dir) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
89 UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
90 UI_APPLY_LAYOUT(ctn->layout, args);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
91
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
92 QWidget *widget = new QWidget();
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
93 QBoxLayout *box = new QBoxLayout(dir);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
94 widget->setLayout(box);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
95 ctn->add(widget, true);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
96
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
97
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
98
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
99 return widget;
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
100 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
101
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
102 UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs args) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
103 return ui_box(obj, args, QBoxLayout::TopToBottom);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
104 }
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
105
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
106 UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs args) {
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
107 return ui_box(obj, args, QBoxLayout::LeftToRight);
8884c7fbe4cb add box container (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
108 }

mercurial