summaryrefslogtreecommitdiff
path: root/src/ui/window.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-04-27 11:59:28 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-04-27 11:59:28 +0300
commit9a7aa34e63132edb5906914ff99b861e64ee9099 (patch)
tree327e275b7f4fd6eb05150f7c03a87a72794d22a0 /src/ui/window.c
parent170209cf926e2f714d507fefaa6b30fd245811ad (diff)
Refactor: Added a proper Root object
`Root` encapsulates the root widget and the associated UI state.
Diffstat (limited to 'src/ui/window.c')
-rw-r--r--src/ui/window.c40
1 files changed, 19 insertions, 21 deletions
diff --git a/src/ui/window.c b/src/ui/window.c
index ea2a4ec0..627f16b1 100644
--- a/src/ui/window.c
+++ b/src/ui/window.c
@@ -165,23 +165,21 @@ static void setupUserInterface_Window(iWindow *d) {
165#if defined (iPlatformAppleDesktop) 165#if defined (iPlatformAppleDesktop)
166 insertMacMenus_(); 166 insertMacMenus_();
167#endif 167#endif
168 static iRootData rootData_; 168 setCurrent_Root(&d->root);
169 setCurrent_Root(NULL, &rootData_); 169 createUserInterface_Root(&d->root);
170 d->root = createUserInterface_Root();
171 setCurrent_Root(d->root, &rootData_);
172} 170}
173 171
174static void updateRootSize_Window_(iWindow *d, iBool notifyAlways) { 172static void updateRootSize_Window_(iWindow *d, iBool notifyAlways) {
175 iInt2 *size = &d->root->rect.size; 173 iInt2 *size = &d->root.widget->rect.size;
176 const iInt2 oldSize = *size; 174 const iInt2 oldSize = *size;
177 SDL_GetRendererOutputSize(d->render, &size->x, &size->y); 175 SDL_GetRendererOutputSize(d->render, &size->x, &size->y);
178 size->y -= d->keyboardHeight; 176 size->y -= d->keyboardHeight;
179 d->root->minSize = *size; 177 d->root.widget->minSize = *size;
180 if (notifyAlways || !isEqual_I2(oldSize, *size)) { 178 if (notifyAlways || !isEqual_I2(oldSize, *size)) {
181 updatePadding_Root(d->root); 179 updatePadding_Root(&d->root);
182 const iBool isHoriz = (d->place.lastNotifiedSize.x != size->x); 180 const iBool isHoriz = (d->place.lastNotifiedSize.x != size->x);
183 const iBool isVert = (d->place.lastNotifiedSize.y != size->y); 181 const iBool isVert = (d->place.lastNotifiedSize.y != size->y);
184 arrange_Widget(d->root); 182 arrange_Widget(d->root.widget);
185 postCommandf_App("window.resized width:%d height:%d horiz:%d vert:%d", 183 postCommandf_App("window.resized width:%d height:%d horiz:%d vert:%d",
186 size->x, 184 size->x,
187 size->y, 185 size->y,
@@ -196,8 +194,8 @@ static void updateRootSize_Window_(iWindow *d, iBool notifyAlways) {
196void drawWhileResizing_Window(iWindow *d, int w, int h) { 194void drawWhileResizing_Window(iWindow *d, int w, int h) {
197 /* This is called while a window resize is in progress, so we can be pretty confident 195 /* This is called while a window resize is in progress, so we can be pretty confident
198 the size has actually changed. */ 196 the size has actually changed. */
199 d->root->rect.size = coord_Window(d, w, h); 197 d->root.widget->rect.size = coord_Window(d, w, h);
200 arrange_Widget(d->root); 198 arrange_Widget(d->root.widget);
201 draw_Window(d); 199 draw_Window(d);
202} 200}
203 201
@@ -347,7 +345,7 @@ static SDL_Surface *loadImage_(const iBlock *data, int resized) {
347void init_Window(iWindow *d, iRect rect) { 345void init_Window(iWindow *d, iRect rect) {
348 theWindow_ = d; 346 theWindow_ = d;
349 d->win = NULL; 347 d->win = NULL;
350 d->root = NULL; 348 init_Root(&d->root);
351 iZap(d->cursors); 349 iZap(d->cursors);
352 d->place.initialPos = rect.pos; 350 d->place.initialPos = rect.pos;
353 d->place.normalRect = rect; 351 d->place.normalRect = rect;
@@ -460,7 +458,7 @@ void deinit_Window(iWindow *d) {
460 SDL_FreeCursor(d->cursors[i]); 458 SDL_FreeCursor(d->cursors[i]);
461 } 459 }
462 } 460 }
463 iReleasePtr(&d->root); 461 deinit_Root(&d->root);
464 deinit_Text(); 462 deinit_Text();
465 SDL_DestroyRenderer(d->render); 463 SDL_DestroyRenderer(d->render);
466 SDL_DestroyWindow(d->win); 464 SDL_DestroyWindow(d->win);
@@ -636,7 +634,7 @@ static iBool handleWindowEvent_Window_(iWindow *d, const SDL_WindowEvent *ev) {
636 return iTrue; 634 return iTrue;
637 } 635 }
638 case SDL_WINDOWEVENT_RESIZED: 636 case SDL_WINDOWEVENT_RESIZED:
639 updatePadding_Root(d->root); 637 updatePadding_Root(&d->root);
640 if (d->isMinimized) { 638 if (d->isMinimized) {
641 updateRootSize_Window_(d, iTrue); 639 updateRootSize_Window_(d, iTrue);
642 return iTrue; 640 return iTrue;
@@ -768,7 +766,7 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) {
768 event.button.x = pos.x; 766 event.button.x = pos.x;
769 event.button.y = pos.y; 767 event.button.y = pos.y;
770 } 768 }
771 iWidget *widget = d->root; 769 iWidget *widget = d->root.widget;
772 if (event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEWHEEL || 770 if (event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEWHEEL ||
773 event.type == SDL_MOUSEBUTTONUP || event.type == SDL_MOUSEBUTTONDOWN) { 771 event.type == SDL_MOUSEBUTTONUP || event.type == SDL_MOUSEBUTTONDOWN) {
774 if (mouseGrab_Widget()) { 772 if (mouseGrab_Widget()) {
@@ -796,7 +794,7 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) {
796 } 794 }
797 } 795 }
798 if (isMetricsChange_UserEvent(&event)) { 796 if (isMetricsChange_UserEvent(&event)) {
799 updateMetrics_Root(d->root); 797 updateMetrics_Root(&d->root);
800 } 798 }
801 if (isCommand_UserEvent(&event, "lang.changed")) { 799 if (isCommand_UserEvent(&event, "lang.changed")) {
802#if defined (iPlatformAppleDesktop) 800#if defined (iPlatformAppleDesktop)
@@ -805,8 +803,8 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) {
805 insertMacMenus_(); 803 insertMacMenus_();
806#endif 804#endif
807 invalidate_Window_(d); 805 invalidate_Window_(d);
808 updatePreferencesLayout_Widget(findChild_Widget(d->root, "prefs")); 806 updatePreferencesLayout_Widget(findChild_Widget(d->root.widget, "prefs"));
809 arrange_Widget(d->root); 807 arrange_Widget(d->root.widget);
810 //printTree_Widget(findChild_Widget(d->root, "prefs")); 808 //printTree_Widget(findChild_Widget(d->root, "prefs"));
811 } 809 }
812 if (oldHover != hover_Widget()) { 810 if (oldHover != hover_Widget()) {
@@ -823,7 +821,7 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) {
823 821
824iBool postContextClick_Window(iWindow *d, const SDL_MouseButtonEvent *ev) { 822iBool postContextClick_Window(iWindow *d, const SDL_MouseButtonEvent *ev) {
825 /* A context menu may still get triggered here. */ 823 /* A context menu may still get triggered here. */
826 const iWidget *hit = hitChild_Widget(d->root, init_I2(ev->x, ev->y)); 824 const iWidget *hit = hitChild_Widget(d->root.widget, init_I2(ev->x, ev->y));
827 while (hit && isEmpty_String(id_Widget(hit))) { 825 while (hit && isEmpty_String(id_Widget(hit))) {
828 hit = parent_Widget(hit); 826 hit = parent_Widget(hit);
829 } 827 }
@@ -868,7 +866,7 @@ void draw_Window(iWindow *d) {
868 /* Draw widgets. */ 866 /* Draw widgets. */
869 d->frameTime = SDL_GetTicks(); 867 d->frameTime = SDL_GetTicks();
870 if (isExposed_Window(d)) { 868 if (isExposed_Window(d)) {
871 draw_Widget(d->root); 869 draw_Widget(d->root.widget);
872#if defined (LAGRANGE_ENABLE_CUSTOM_FRAME) 870#if defined (LAGRANGE_ENABLE_CUSTOM_FRAME)
873 /* App icon. */ 871 /* App icon. */
874 const iWidget *appIcon = findChild_Widget(d->root, "winbar.icon"); 872 const iWidget *appIcon = findChild_Widget(d->root, "winbar.icon");
@@ -907,7 +905,7 @@ void resize_Window(iWindow *d, int w, int h) {
907 905
908void setTitle_Window(iWindow *d, const iString *title) { 906void setTitle_Window(iWindow *d, const iString *title) {
909 SDL_SetWindowTitle(d->win, cstr_String(title)); 907 SDL_SetWindowTitle(d->win, cstr_String(title));
910 iLabelWidget *bar = findChild_Widget(d->root, "winbar.title"); 908 iLabelWidget *bar = findChild_Widget(d->root.widget, "winbar.title");
911 if (bar) { 909 if (bar) {
912 updateText_LabelWidget(bar, title); 910 updateText_LabelWidget(bar, title);
913 } 911 }
@@ -942,7 +940,7 @@ uint32_t id_Window(const iWindow *d) {
942} 940}
943 941
944iInt2 rootSize_Window(const iWindow *d) { 942iInt2 rootSize_Window(const iWindow *d) {
945 return d && d->root ? d->root->rect.size : zero_I2(); 943 return d && d->root.widget ? d->root.widget->rect.size : zero_I2();
946} 944}
947 945
948iRect safeRootRect_Window(const iWindow *d) { 946iRect safeRootRect_Window(const iWindow *d) {